CSIT-526 HC test: DHCP relay
[csit.git] / resources / libraries / python / honeycomb / HoneycombSetup.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Implementation of keywords for Honeycomb setup."""
15
16 from robot.api import logger
17
18 from resources.libraries.python.HTTPRequest import HTTPRequest, HTTPCodes, \
19     HTTPRequestError
20 from resources.libraries.python.constants import Constants as Const
21 from resources.libraries.python.honeycomb.HoneycombUtil import HoneycombError
22 from resources.libraries.python.honeycomb.HoneycombUtil \
23     import HoneycombUtil as HcUtil
24 from resources.libraries.python.ssh import SSH
25 from resources.libraries.python.topology import NodeType
26
27
28 class HoneycombSetup(object):
29     """Implements keywords for Honeycomb setup.
30
31     The keywords implemented in this class make possible to:
32     - start Honeycomb,
33     - stop Honeycomb,
34     - check the Honeycomb start-up state,
35     - check the Honeycomb shutdown state,
36     - add VPP to the topology.
37     """
38
39     def __init__(self):
40         pass
41
42     @staticmethod
43     def start_honeycomb_on_duts(*nodes):
44         """Start Honeycomb on specified DUT nodes.
45
46         This keyword starts the Honeycomb service on specified DUTs.
47         The keyword just starts the Honeycomb and does not check its startup
48         state. Use the keyword "Check Honeycomb Startup State" to check if the
49         Honeycomb is up and running.
50         Honeycomb must be installed in "/opt" directory, otherwise the start
51         will fail.
52         :param nodes: List of nodes to start Honeycomb on.
53         :type nodes: list
54         :raises HoneycombError: If Honeycomb fails to start.
55         """
56
57         HoneycombSetup.print_environment(nodes)
58
59         logger.console("\nStarting Honeycomb service ...")
60
61         cmd = "sudo service honeycomb start"
62
63         for node in nodes:
64             if node['type'] == NodeType.DUT:
65                 ssh = SSH()
66                 ssh.connect(node)
67                 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
68                 if int(ret_code) != 0:
69                     raise HoneycombError('Node {0} failed to start Honeycomb.'.
70                                          format(node['host']))
71                 else:
72                     logger.info("Starting the Honeycomb service on node {0} is "
73                                 "in progress ...".format(node['host']))
74
75     @staticmethod
76     def stop_honeycomb_on_duts(*nodes):
77         """Stop the Honeycomb service on specified DUT nodes.
78
79         This keyword stops the Honeycomb service on specified nodes. It just
80         stops the Honeycomb and does not check its shutdown state. Use the
81         keyword "Check Honeycomb Shutdown State" to check if Honeycomb has
82         stopped.
83         :param nodes: List of nodes to stop Honeycomb on.
84         :type nodes: list
85         :raises HoneycombError: If Honeycomb failed to stop.
86         """
87         logger.console("\nShutting down Honeycomb service ...")
88
89         cmd = "sudo service honeycomb stop"
90         errors = []
91
92         for node in nodes:
93             if node['type'] == NodeType.DUT:
94                 ssh = SSH()
95                 ssh.connect(node)
96                 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
97                 if int(ret_code) != 0:
98                     errors.append(node['host'])
99                 else:
100                     logger.info("Stopping the Honeycomb service on node {0} is "
101                                 "in progress ...".format(node['host']))
102         if errors:
103             raise HoneycombError('Node(s) {0} failed to stop Honeycomb.'.
104                                  format(errors))
105
106     @staticmethod
107     def restart_honeycomb_and_vpp_on_duts(*nodes):
108         """Restart the Honeycomb service on specified DUT nodes.
109
110         Use the keyword "Check Honeycomb Startup State" to check when Honeycomb
111         is fully restarted.
112         :param nodes: List of nodes to restart Honeycomb on.
113         :type nodes: list
114         :raises HoneycombError: If Honeycomb failed to restart.
115         """
116         logger.console("\nRestarting Honeycomb service ...")
117
118         cmd = "sudo service honeycomb restart && sudo service vpp restart"
119         errors = []
120
121         for node in nodes:
122             if node['type'] == NodeType.DUT:
123                 ssh = SSH()
124                 ssh.connect(node)
125                 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
126                 if int(ret_code) != 0:
127                     errors.append(node['host'])
128                 else:
129                     logger.info("Restart of Honeycomb and VPP on node {0} is "
130                                 "in progress ...".format(node['host']))
131         if errors:
132             raise HoneycombError('Node(s) {0} failed to restart Honeycomb'
133                                  ' and/or VPP.'.
134                                  format(errors))
135
136     @staticmethod
137     def check_honeycomb_startup_state(*nodes):
138         """Check state of Honeycomb service during startup on specified nodes.
139
140         Reads html path from template file oper_vpp_version.url.
141
142         Honeycomb nodes reply with connection refused or the following status
143         codes depending on startup progress: codes 200, 401, 403, 404, 500, 503
144
145         :param nodes: List of DUT nodes starting Honeycomb.
146         :type nodes: list
147         :return: True if all GETs returned code 200(OK).
148         :rtype bool
149         """
150         path = HcUtil.read_path_from_url_file("oper_vpp_version")
151         expected_status_codes = (HTTPCodes.UNAUTHORIZED,
152                                  HTTPCodes.FORBIDDEN,
153                                  HTTPCodes.NOT_FOUND,
154                                  HTTPCodes.SERVICE_UNAVAILABLE,
155                                  HTTPCodes.INTERNAL_SERVER_ERROR)
156
157         for node in nodes:
158             if node['type'] == NodeType.DUT:
159                 HoneycombSetup.print_ports(node)
160                 status_code, _ = HTTPRequest.get(node, path, timeout=10,
161                                                  enable_logging=False)
162                 if status_code == HTTPCodes.OK:
163                     logger.info("Honeycomb on node {0} is up and running".
164                                 format(node['host']))
165                 elif status_code in expected_status_codes:
166                     if status_code == HTTPCodes.UNAUTHORIZED:
167                         logger.info('Unauthorized. If this triggers keyword '
168                                     'timeout, verify Honeycomb username and '
169                                     'password.')
170                     raise HoneycombError('Honeycomb on node {0} running but '
171                                          'not yet ready.'.format(node['host']),
172                                          enable_logging=False)
173                 else:
174                     raise HoneycombError('Unexpected return code: {0}.'.
175                                          format(status_code))
176
177                 status_code, _ = HcUtil.get_honeycomb_data(
178                     node, "config_vpp_interfaces")
179                 if status_code != HTTPCodes.OK:
180                     raise HoneycombError('Honeycomb on node {0} running but '
181                                          'not yet ready.'.format(node['host']),
182                                          enable_logging=False)
183         return True
184
185     @staticmethod
186     def check_honeycomb_shutdown_state(*nodes):
187         """Check state of Honeycomb service during shutdown on specified nodes.
188
189         Honeycomb nodes reply with connection refused or the following status
190         codes depending on shutdown progress: codes 200, 404.
191
192         :param nodes: List of DUT nodes stopping Honeycomb.
193         :type nodes: list
194         :return: True if all GETs fail to connect.
195         :rtype bool
196         """
197         cmd = "ps -ef | grep -v grep | grep honeycomb"
198         for node in nodes:
199             if node['type'] == NodeType.DUT:
200                 try:
201                     status_code, _ = HTTPRequest.get(node, '/index.html',
202                                                      timeout=5,
203                                                      enable_logging=False)
204                     if status_code == HTTPCodes.OK:
205                         raise HoneycombError('Honeycomb on node {0} is still '
206                                              'running.'.format(node['host']),
207                                              enable_logging=False)
208                     elif status_code == HTTPCodes.NOT_FOUND:
209                         raise HoneycombError('Honeycomb on node {0} is shutting'
210                                              ' down.'.format(node['host']),
211                                              enable_logging=False)
212                     else:
213                         raise HoneycombError('Unexpected return code: {0}.'.
214                                              format(status_code))
215                 except HTTPRequestError:
216                     logger.debug('Connection refused, checking the process '
217                                  'state ...')
218                     ssh = SSH()
219                     ssh.connect(node)
220                     (ret_code, _, _) = ssh.exec_command_sudo(cmd)
221                     if ret_code == 0:
222                         raise HoneycombError('Honeycomb on node {0} is still '
223                                              'running.'.format(node['host']),
224                                              enable_logging=False)
225                     else:
226                         logger.info("Honeycomb on node {0} has stopped".
227                                     format(node['host']))
228         return True
229
230     @staticmethod
231     def configure_unsecured_access(*nodes):
232         """Configure Honeycomb to allow restconf requests through insecure HTTP
233         used by tests. By default this is only allowed for localhost.
234
235          :param nodes: All nodes in test topology.
236          :type nodes: dict
237          :raises HoneycombError: If the configuration could not be changed.
238          """
239         # TODO: Modify tests to use HTTPS instead.
240
241         find = "restconf-binding-address"
242         replace = '\\"restconf-binding-address\\": \\"0.0.0.0\\",'
243
244         argument = '"/{0}/c\\ {1}"'.format(find, replace)
245         path = "{0}/config/honeycomb.json".format(Const.REMOTE_HC_DIR)
246         command = "sed -i {0} {1}".format(argument, path)
247
248         ssh = SSH()
249         for node in nodes:
250             if node['type'] == NodeType.DUT:
251                 ssh.connect(node)
252                 (ret_code, _, stderr) = ssh.exec_command_sudo(command)
253                 if ret_code != 0:
254                     raise HoneycombError("Failed to modify configuration on "
255                                          "node {0}, {1}".format(node, stderr))
256
257     @staticmethod
258     def print_environment(nodes):
259         """Print information about the nodes to log. The information is defined
260         by commands in cmds tuple at the beginning of this method.
261
262         :param nodes: List of DUT nodes to get information about.
263         :type nodes: list
264         """
265
266         # TODO: When everything is set and running in VIRL env, transform this
267         # method to a keyword checking the environment.
268
269         cmds = ("uname -a",
270                 "df -lh",
271                 "echo $JAVA_HOME",
272                 "echo $PATH",
273                 "which java",
274                 "java -version",
275                 "dpkg --list | grep openjdk",
276                 "ls -la /opt/honeycomb")
277
278         for node in nodes:
279             if node['type'] == NodeType.DUT:
280                 logger.info("Checking node {} ...".format(node['host']))
281                 for cmd in cmds:
282                     logger.info("Command: {}".format(cmd))
283                     ssh = SSH()
284                     ssh.connect(node)
285                     ssh.exec_command_sudo(cmd)
286
287     @staticmethod
288     def print_ports(node):
289         """Uses "sudo netstat -anp | grep java" to print port where a java
290         application listens.
291
292         :param node: Honeycomb node where we want to print the ports.
293         :type node: dict
294         """
295
296         cmds = ("netstat -anp | grep java",
297                 "ps -ef | grep [h]oneycomb")
298
299         logger.info("Checking node {} ...".format(node['host']))
300         for cmd in cmds:
301             logger.info("Command: {}".format(cmd))
302             ssh = SSH()
303             ssh.connect(node)
304             ssh.exec_command_sudo(cmd)
305
306     @staticmethod
307     def configure_log_level(node, level):
308         """Set Honeycomb logging to the specified level.
309
310         :param node: Honeycomb node.
311         :param level: Log level (INFO, DEBUG, TRACE).
312         :type node: dict
313         :type level: str
314         """
315
316         find = 'logger name=\\"io.fd\\"'
317         replace = '<logger name=\\"io.fd\\" level=\\"{0}\\"/>'.format(level)
318
319         argument = '"/{0}/c\\ {1}"'.format(find, replace)
320         path = "{0}/config/logback.xml".format(Const.REMOTE_HC_DIR)
321         command = "sed -i {0} {1}".format(argument, path)
322
323         ssh = SSH()
324         ssh.connect(node)
325         (ret_code, _, stderr) = ssh.exec_command_sudo(command)
326         if ret_code != 0:
327             raise HoneycombError("Failed to modify configuration on "
328                                  "node {0}, {1}".format(node, stderr))
329
330     @staticmethod
331     def enable_module_features(node, *features):
332         """Configure Honeycomb to use VPP modules that are disabled by default.
333
334         ..Note:: If the module is not enabled in VPP, Honeycomb will
335         be unable to establish VPP connection.
336
337         :param node: Honeycomb node.
338         :param features: Features to enable.
339         :type node: dict
340         :type features: string
341         :raises HoneycombError: If the configuration could not be changed.
342          """
343
344         disabled_features = {
345             "NSH": "io.fd.hc2vpp.vppnsh.impl.VppNshModule"
346         }
347
348         ssh = SSH()
349         ssh.connect(node)
350
351         for feature in features:
352             if feature in disabled_features.keys():
353                 # uncomment by replacing the entire line
354                 find = replace = "{0}".format(disabled_features[feature])
355
356                 argument = '"/{0}/c\\ {1}"'.format(find, replace)
357                 path = "{0}/modules/*module-config"\
358                     .format(Const.REMOTE_HC_DIR)
359                 command = "sed -i {0} {1}".format(argument, path)
360
361                 (ret_code, _, stderr) = ssh.exec_command_sudo(command)
362                 if ret_code != 0:
363                     raise HoneycombError("Failed to modify configuration on "
364                                          "node {0}, {1}".format(node, stderr))
365             else:
366                 raise HoneycombError(
367                     "Unrecognized feature {0}.".format(feature))
368
369     @staticmethod
370     def copy_java_libraries(node):
371         """Copy Java libraries installed by vpp-api-java package to honeycomb
372         lib folder.
373
374         This is a (temporary?) workaround for jvpp version mismatches.
375
376         :param node: Honeycomb node
377         :type node: dict
378         """
379
380         ssh = SSH()
381         ssh.connect(node)
382         (_, stdout, _) = ssh.exec_command_sudo(
383             "ls /usr/share/java | grep ^jvpp-*")
384
385         files = stdout.split("\n")[:-1]
386         for item in files:
387             # example filenames:
388             # jvpp-registry-17.04.jar
389             # jvpp-core-17.04.jar
390
391             parts = item.split("-")
392             version = "{0}-SNAPSHOT".format(parts[2][:5])
393             artifact_id = "{0}-{1}".format(parts[0], parts[1])
394
395             directory = "{0}/lib/io/fd/vpp/{1}/{2}".format(
396                 Const.REMOTE_HC_DIR, artifact_id, version)
397             cmd = "sudo mkdir -p {0}; " \
398                   "sudo cp /usr/share/java/{1} {0}/{2}-{3}.jar".format(
399                     directory, item, artifact_id, version)
400
401             (ret_code, _, stderr) = ssh.exec_command(cmd)
402             if ret_code != 0:
403                 raise HoneycombError("Failed to copy JVPP libraries on "
404                                      "node {0}, {1}".format(node, stderr))
405
406     @staticmethod
407     def find_odl_client(node):
408         """Check if there is a karaf directory in home.
409
410         :param node: Honeycomb node.
411         :type node: dict
412         :returns: True if ODL client is present on node, else False.
413         :rtype: bool
414         """
415
416         ssh = SSH()
417         ssh.connect(node)
418         (ret_code, stdout, _) = ssh.exec_command_sudo(
419             "ls ~ | grep karaf")
420
421         logger.debug(stdout)
422         return not bool(ret_code)
423
424     @staticmethod
425     def start_odl_client(node):
426         """Start ODL client on the specified node.
427
428         karaf should be located in home directory, and VPP and Honeycomb should
429         already be running, otherwise the start will fail.
430         :param node: Nodes to start ODL client on.
431         :type node: dict
432         :raises HoneycombError: If Honeycomb fails to start.
433         """
434
435         logger.console("\nStarting ODL client ...")
436
437         cmd = "~/*karaf*/bin/start"
438
439         ssh = SSH()
440         ssh.connect(node)
441         (ret_code, _, _) = ssh.exec_command_sudo(cmd)
442         if int(ret_code) != 0:
443             raise HoneycombError('Node {0} failed to start ODL.'.
444                                  format(node['host']))
445         else:
446             logger.info("Starting the ODL client on node {0} is "
447                         "in progress ...".format(node['host']))
448
449     @staticmethod
450     def check_odl_startup_state(node):
451         """Check the status of ODL client startup.
452
453         :param node: Honeycomb node.
454         :param node: dict
455         :returns: True when ODL is started.
456         :rtype: bool
457         :raises HoneycombError: When the response is not code 200: OK.
458         """
459
460         path = HcUtil.read_path_from_url_file(
461             "odl_client/odl_netconf_connector")
462         expected_status_codes = (HTTPCodes.UNAUTHORIZED,
463                                  HTTPCodes.FORBIDDEN,
464                                  HTTPCodes.NOT_FOUND,
465                                  HTTPCodes.SERVICE_UNAVAILABLE,
466                                  HTTPCodes.INTERNAL_SERVER_ERROR)
467
468         status_code, _ = HTTPRequest.get(node, path, timeout=10,
469                                          enable_logging=False)
470         if status_code == HTTPCodes.OK:
471             logger.info("ODL client on node {0} is up and running".
472                         format(node['host']))
473         elif status_code in expected_status_codes:
474             if status_code == HTTPCodes.UNAUTHORIZED:
475                 logger.info('Unauthorized. If this triggers keyword '
476                             'timeout, verify username and password.')
477             raise HoneycombError('ODL client on node {0} running but '
478                                  'not yet ready.'.format(node['host']),
479                                  enable_logging=False)
480         else:
481             raise HoneycombError('Unexpected return code: {0}.'.
482                                  format(status_code))
483         return True
484
485     @staticmethod
486     def mount_honeycomb_on_odl(node):
487         """Tell ODL client to mount Honeycomb instance over netconf.
488
489         :param node: Honeycomb node.
490         :type node: dict
491         :raises HoneycombError: When the response is not code 200: OK.
492         """
493
494         path = HcUtil.read_path_from_url_file(
495             "odl_client/odl_netconf_connector")
496
497         url_file = "{0}/{1}".format(Const.RESOURCES_TPL_HC,
498                                     "odl_client/mount_honeycomb.xml")
499
500         with open(url_file) as template:
501             data = template.read()
502
503         status_code, _ = HTTPRequest.post(
504             node, path, headers={"Content-Type": "application/xml"},
505             payload=data, timeout=10, enable_logging=False)
506
507         if status_code == HTTPCodes.OK:
508             logger.info("ODL mount point configured successfully.")
509         elif status_code == HTTPCodes.CONFLICT:
510             logger.warn("ODL mount point was already configured.")
511         else:
512             raise HoneycombError('Mount point configuration not successful')