1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Implementation of keywords for Honeycomb setup."""
16 from xml.etree import ElementTree as ET
18 from robot.api import logger
20 from resources.libraries.python.topology import NodeType
21 from resources.libraries.python.ssh import SSH
22 from resources.libraries.python.HTTPRequest import HTTPRequest, HTTPCodes, \
24 from resources.libraries.python.HoneycombUtil import HoneycombUtil as HcUtil
25 from resources.libraries.python.HoneycombUtil import HoneycombError
26 from resources.libraries.python.constants import Constants as Const
29 class HoneycombSetup(object):
30 """Implements keywords for Honeycomb setup.
32 The keywords implemented in this class make possible to:
35 - check the Honeycomb start-up state,
36 - check the Honeycomb shutdown state,
37 - add VPP to the topology.
44 def start_honeycomb_on_all_duts(nodes):
45 """Start Honeycomb on all DUT nodes in topology.
47 This keyword starts the Honeycomb service on all DUTs. The keyword just
48 starts the Honeycomb and does not check its startup state. Use the
49 keyword "Check Honeycomb Startup State" to check if the Honeycomb is up
51 Honeycomb must be installed in "/opt" directory, otherwise the start
53 :param nodes: All nodes in topology.
55 :raises HoneycombError: If Honeycomb fails to start.
57 logger.console("Starting Honeycomb service ...")
59 cmd = "{0}/start".format(Const.REMOTE_HC_DIR)
61 for node in nodes.values():
62 if node['type'] == NodeType.DUT:
65 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
66 if int(ret_code) != 0:
67 raise HoneycombError('Node {0} failed to start Honeycomb.'.
70 logger.info("Starting the Honeycomb service on node {0} is "
71 "in progress ...".format(node['host']))
74 def stop_honeycomb_on_all_duts(nodes):
75 """Stop the Honeycomb service on all DUTs.
77 This keyword stops the Honeycomb service on all nodes. It just stops the
78 Honeycomb and does not check its shutdown state. Use the keyword "Check
79 Honeycomb Shutdown State" to check if Honeycomb has stopped.
80 :param nodes: Nodes in topology.
82 :raises HoneycombError: If Honeycomb failed to stop.
84 logger.console("Shutting down Honeycomb service ...")
86 cmd = "{0}/stop".format(Const.REMOTE_HC_DIR)
89 for node in nodes.values():
90 if node['type'] == NodeType.DUT:
93 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
94 if int(ret_code) != 0:
95 errors.append(node['host'])
97 logger.info("Stopping the Honeycomb service on node {0} is "
98 "in progress ...".format(node['host']))
100 raise HoneycombError('Node(s) {0} failed to stop Honeycomb.'.
104 def check_honeycomb_startup_state(nodes):
105 """Check state of Honeycomb service during startup.
107 Reads html path from template file oper_vpp_version.url.
109 Honeycomb node replies with connection refused or the following status
110 codes depending on startup progress: codes 200, 401, 403, 404, 503
112 :param nodes: Nodes in topology.
114 :return: True if all GETs returned code 200(OK).
118 path = HcUtil.read_path_from_url_file("oper_vpp_version")
119 expected_status_codes = (HTTPCodes.UNAUTHORIZED,
122 HTTPCodes.SERVICE_UNAVAILABLE)
124 for node in nodes.values():
125 if node['type'] == NodeType.DUT:
126 status_code, _ = HTTPRequest.get(node, path, timeout=10,
127 enable_logging=False)
128 if status_code == HTTPCodes.OK:
129 logger.info("Honeycomb on node {0} is up and running".
130 format(node['host']))
131 elif status_code in expected_status_codes:
132 if status_code == HTTPCodes.UNAUTHORIZED:
133 logger.info('Unauthorized. If this triggers keyword '
134 'timeout, verify Honeycomb username and '
136 raise HoneycombError('Honeycomb on node {0} running but '
137 'not yet ready.'.format(node['host']),
138 enable_logging=False)
140 raise HoneycombError('Unexpected return code: {0}.'.
145 def check_honeycomb_shutdown_state(nodes):
146 """Check state of Honeycomb service during shutdown.
148 Honeycomb node replies with connection refused or the following status
149 codes depending on shutdown progress: codes 200, 404.
151 :param nodes: Nodes in topology.
153 :return: True if all GETs fail to connect.
157 cmd = "ps -ef | grep -v grep | grep karaf"
158 for node in nodes.values():
159 if node['type'] == NodeType.DUT:
161 status_code, _ = HTTPRequest.get(node, '/index.html',
163 enable_logging=False)
164 if status_code == HTTPCodes.OK:
165 raise HoneycombError('Honeycomb on node {0} is still '
166 'running.'.format(node['host']),
167 enable_logging=False)
168 elif status_code == HTTPCodes.NOT_FOUND:
169 raise HoneycombError('Honeycomb on node {0} is shutting'
170 ' down.'.format(node['host']),
171 enable_logging=False)
173 raise HoneycombError('Unexpected return code: {0}.'.
175 except HTTPRequestError:
176 logger.debug('Connection refused, checking the process '
180 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
182 raise HoneycombError('Honeycomb on node {0} is still '
183 'running.'.format(node['host']),
184 enable_logging=False)
186 logger.info("Honeycomb on node {0} has stopped".
187 format(node['host']))
191 def add_vpp_to_honeycomb_network_topology(nodes):
192 """Add vpp node to Honeycomb network topology.
194 :param nodes: All nodes in test topology.
196 :return: Status code and response content from PUT requests.
198 :raises HoneycombError: If a node was not added to Honeycomb topology.
200 Reads HTML path from template file config_topology_node.url.
201 Path to the node to be added, e.g.:
202 ("/restconf/config/network-topology:network-topology"
203 "/topology/topology-netconf/node/")
204 There must be "/" at the end, as generated node name is added at the
207 Reads payload data from template file add_vpp_to_topology.xml.
208 Information about node as XML structure, e.g.:
209 <node xmlns="urn:TBD:params:xml:ns:yang:network-topology">
213 <host xmlns="urn:opendaylight:netconf-node-topology">
216 <port xmlns="urn:opendaylight:netconf-node-topology">
219 <username xmlns="urn:opendaylight:netconf-node-topology">
222 <password xmlns="urn:opendaylight:netconf-node-topology">
225 <tcp-only xmlns="urn:opendaylight:netconf-node-topology">
228 <keepalive-delay xmlns="urn:opendaylight:netconf-node-topology">
232 NOTE: The placeholders:
238 MUST be there as they are replaced by correct values.
241 path = HcUtil.read_path_from_url_file("config_topology_node")
243 xml_data = ET.parse("{0}/add_vpp_to_topology.xml".
244 format(Const.RESOURCES_TPL_HC))
245 except ET.ParseError as err:
246 raise HoneycombError(repr(err))
247 data = ET.tostring(xml_data.getroot())
249 headers = {"Content-Type": "application/xml"}
253 for node_name, node in nodes.items():
254 if node['type'] == NodeType.DUT:
256 payload = data.format(
259 vpp_port=node['honeycomb']["netconf_port"],
260 user=node['honeycomb']["user"],
261 passwd=node['honeycomb']["passwd"])
262 status_code, resp = HTTPRequest.put(
264 path="{0}/{1}".format(path, node_name),
267 if status_code != HTTPCodes.OK:
268 raise HoneycombError(
269 "VPP {0} was not added to topology. "
270 "Status code: {1}.".format(node["host"],
273 status_codes.append(status_code)
274 responses.append(resp)
276 except HTTPRequestError as err:
277 raise HoneycombError("VPP {0} was not added to topology.".
278 format(node["host"]), repr(err))
279 return status_codes, responses