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

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.