4 # Copyright (c) 2020 Project CHIP Authors
5 # Copyright (c) 2016-2017 Nest Labs, Inc.
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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.
23 # Implements ChipStateLoad class that sets up virtual network topology.
30 from happy.ReturnMsg import ReturnMsg
31 from happy.Utils import *
33 import happy.HappyStateLoad
36 from ChipState import ChipState
39 options["quiet"] = False
40 options["json_file"] = None
42 LOG_TEXT_PREFIX = "[localhost] ChipStateLoad: "
49 class ChipStateLoad(ChipState):
50 def __init__(self, opts=options):
51 ChipState.__init__(self)
53 self.quiet = opts["quiet"]
54 self.new_json_file = opts["json_file"]
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)
62 if not os.path.exists(self.new_json_file):
63 emsg = "Cannot find the configuration file {}".format(
65 self.logger.error(LOG_TEXT_PREFIX + emsg)
68 self.new_json_file = os.path.realpath(self.new_json_file)
70 emsg = "Loading Chip Topology from file {}.".format(self.new_json_file)
71 self.logger.debug(LOG_TEXT_PREFIX + emsg)
73 def __load_JSON(self):
74 emsg = "Import state file {}.".format(self.new_json_file)
75 self.logger.debug(LOG_TEXT_PREFIX + emsg)
78 with open(self.new_json_file, 'r') as jfile:
79 json_data = jfile.read()
81 self.chip_topology = json.loads(json_data)
84 emsg = "Failed to load JSON state file: {}".format(
86 self.logger.error(LOG_TEXT_PREFIX + emsg)
89 def __load_network_topology(self):
90 emsg = "Loading network topology."
91 self.logger.debug(LOG_TEXT_PREFIX + emsg)
93 options = happy.HappyStateLoad.option()
94 options["quiet"] = self.quiet
95 options["json_file"] = self.new_json_file
97 happyLoad = happy.HappyStateLoad.HappyStateLoad(options)
102 def __post_check(self):
106 with self.getStateLockManager():
112 self.__load_network_topology()