Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / test_driver / happy / lib / ChipStateLoad.py
1 #!/usr/bin/env python3
2
3 #
4 #    Copyright (c) 2020 Project CHIP Authors
5 #    Copyright (c) 2016-2017 Nest Labs, Inc.
6 #    All rights reserved.
7 #
8 #    Licensed under the Apache License, Version 2.0 (the "License");
9 #    you may not use this file except in compliance with the License.
10 #    You may obtain a copy of the License at
11 #
12 #        http://www.apache.org/licenses/LICENSE-2.0
13 #
14 #    Unless required by applicable law or agreed to in writing, software
15 #    distributed under the License is distributed on an "AS IS" BASIS,
16 #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 #    See the License for the specific language governing permissions and
18 #    limitations under the License.
19 #
20
21 ##
22 #    @file
23 #       Implements ChipStateLoad class that sets up virtual network topology.
24 #
25
26 import json
27 import os
28 import sys
29
30 from happy.ReturnMsg import ReturnMsg
31 from happy.Utils import *
32
33 import happy.HappyStateLoad
34
35 from Chip import Chip
36 from ChipState import ChipState
37
38 options = {}
39 options["quiet"] = False
40 options["json_file"] = None
41
42 LOG_TEXT_PREFIX = "[localhost] ChipStateLoad: "
43
44
45 def option():
46     return options.copy()
47
48
49 class ChipStateLoad(ChipState):
50     def __init__(self, opts=options):
51         ChipState.__init__(self)
52
53         self.quiet = opts["quiet"]
54         self.new_json_file = opts["json_file"]
55
56     def __pre_check(self):
57         if self.new_json_file is None:
58             emsg = "Missing name of file that specifies virtual network topology."
59             self.logger.error(LOG_TEXT_PREFIX + emsg)
60             self.exit()
61
62         if not os.path.exists(self.new_json_file):
63             emsg = "Cannot find the configuration file {}".format(
64                 self.new_json_file)
65             self.logger.error(LOG_TEXT_PREFIX + emsg)
66             self.exit()
67
68         self.new_json_file = os.path.realpath(self.new_json_file)
69
70         emsg = "Loading Chip Topology from file {}.".format(self.new_json_file)
71         self.logger.debug(LOG_TEXT_PREFIX + emsg)
72
73     def __load_JSON(self):
74         emsg = "Import state file {}.".format(self.new_json_file)
75         self.logger.debug(LOG_TEXT_PREFIX + emsg)
76
77         try:
78             with open(self.new_json_file, 'r') as jfile:
79                 json_data = jfile.read()
80
81             self.chip_topology = json.loads(json_data)
82
83         except Exception:
84             emsg = "Failed to load JSON state file: {}".format(
85                 self.new_json_file)
86             self.logger.error(LOG_TEXT_PREFIX + emsg)
87             self.exit()
88
89     def __load_network_topology(self):
90         emsg = "Loading network topology."
91         self.logger.debug(LOG_TEXT_PREFIX + emsg)
92
93         options = happy.HappyStateLoad.option()
94         options["quiet"] = self.quiet
95         options["json_file"] = self.new_json_file
96
97         happyLoad = happy.HappyStateLoad.HappyStateLoad(options)
98         happyLoad.run()
99
100         self.readState()
101
102     def __post_check(self):
103         pass
104
105     def run(self):
106         with self.getStateLockManager():
107
108             self.__pre_check()
109
110             self.__load_JSON()
111
112             self.__load_network_topology()
113
114             self.__post_check()
115
116         return ReturnMsg(0)