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 ChipStateUnload class that tears down virtual network topology.
30 from happy.ReturnMsg import ReturnMsg
31 from happy.Utils import *
33 import happy.HappyStateUnload
35 from ChipState import ChipState
38 options["quiet"] = False
39 options["json_file"] = None
46 class ChipStateUnload(ChipState):
48 Deletes a Chip-enabled virtual network topology based on the state described
49 in a JSON file. If the current Happy state does not match the specified JSON
50 file, a partial deletion of the topology might occur.
51 chip-state-unload [-h --help] [-q --quiet] [-f --file <JSON_FILE>]
52 -f --file Required. A valid JSON file with the topology to delete.
54 $ chip-state-unload mychipstate.json
55 Deletes the Chip-enabled network topology based on the state described in
62 def __init__(self, opts=options):
63 ChipState.__init__(self)
65 self.quiet = opts["quiet"]
66 self.old_json_file = opts["json_file"]
68 def __pre_check(self):
69 # Check if the name of the new node is given
70 if self.old_json_file is None:
71 emsg = "Missing name of file that specifies virtual network topology."
72 self.logger.error("[localhost] HappyStateUnload: %s" % (emsg))
75 # Check if json file exists
76 if not os.path.exists(self.old_json_file):
77 emsg = "Cannot find the configuration file %s" % (
79 self.logger.error("[localhost] HappyStateUnload: %s" % emsg)
82 self.old_json_file = os.path.realpath(self.old_json_file)
84 emsg = "Unloading Chip Fabric from file %s." % (self.old_json_file)
85 self.logger.debug("[localhost] HappyStateUnload: %s" % emsg)
87 def __load_JSON(self):
88 emsg = "Import state file %s." % (self.old_json_file)
89 self.logger.debug("[localhost] ChipStateUnload: %s" % (emsg))
92 with open(self.old_json_file, 'r') as jfile:
93 json_data = jfile.read()
95 self.chip_topology = json.loads(json_data)
98 emsg = "Failed to load JSON state file: %s" % (self.old_json_file)
99 self.logger.error("[localhost] HappyStateUnload: %s" % emsg)
102 def __unload_network_topology(self):
103 emsg = "Unloading network topology."
104 self.logger.debug("[localhost] ChipStateUnload: %s" % (emsg))
106 options = happy.HappyStateUnload.option()
107 options["quiet"] = self.quiet
108 options["json_file"] = self.old_json_file
110 happyUnload = happy.HappyStateUnload.HappyStateUnload(options)
113 def __post_check(self):
114 emsg = "Unloading Chip Topologym completed."
115 self.logger.debug("[localhost] ChipStateUnload: %s" % (emsg))
118 with self.getStateLockManager():
126 self.__unload_network_topology()