Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / test_driver / happy / lib / ChipStateUnload.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 ChipStateUnload class that tears down 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.HappyStateUnload
34
35 from ChipState import ChipState
36
37 options = {}
38 options["quiet"] = False
39 options["json_file"] = None
40
41
42 def option():
43     return options.copy()
44
45
46 class ChipStateUnload(ChipState):
47     """
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.
53     Example:
54     $ chip-state-unload mychipstate.json
55         Deletes the Chip-enabled network topology based on the state described in
56         mychipstate.json.
57     return:
58         0    success
59         1    fail
60     """
61
62     def __init__(self, opts=options):
63         ChipState.__init__(self)
64
65         self.quiet = opts["quiet"]
66         self.old_json_file = opts["json_file"]
67
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))
73             self.exit()
74
75         # Check if json file exists
76             if not os.path.exists(self.old_json_file):
77                 emsg = "Cannot find the configuration file %s" % (
78                     self.old_json_file)
79                 self.logger.error("[localhost] HappyStateUnload: %s" % emsg)
80                 self.exit()
81
82         self.old_json_file = os.path.realpath(self.old_json_file)
83
84         emsg = "Unloading Chip Fabric from file %s." % (self.old_json_file)
85         self.logger.debug("[localhost] HappyStateUnload: %s" % emsg)
86
87     def __load_JSON(self):
88         emsg = "Import state file %s." % (self.old_json_file)
89         self.logger.debug("[localhost] ChipStateUnload: %s" % (emsg))
90
91         try:
92             with open(self.old_json_file, 'r') as jfile:
93                 json_data = jfile.read()
94
95             self.chip_topology = json.loads(json_data)
96
97         except Exception:
98             emsg = "Failed to load JSON state file: %s" % (self.old_json_file)
99             self.logger.error("[localhost] HappyStateUnload: %s" % emsg)
100             self.exit()
101
102     def __unload_network_topology(self):
103         emsg = "Unloading network topology."
104         self.logger.debug("[localhost] ChipStateUnload: %s" % (emsg))
105
106         options = happy.HappyStateUnload.option()
107         options["quiet"] = self.quiet
108         options["json_file"] = self.old_json_file
109
110         happyUnload = happy.HappyStateUnload.HappyStateUnload(options)
111         happyUnload.run()
112
113     def __post_check(self):
114         emsg = "Unloading Chip Topologym  completed."
115         self.logger.debug("[localhost] ChipStateUnload: %s" % (emsg))
116
117     def run(self):
118         with self.getStateLockManager():
119
120             self.__pre_check()
121
122             self.__load_JSON()
123
124             self.__post_check()
125
126         self.__unload_network_topology()
127
128         return ReturnMsg(0)