Execute all CVP tasks one by one
[osm/RO.git] / RO-SDN-arista / osm_rosdn_arista / aristaSwitch.py
1 # -*- coding: utf-8 -*-
2 ##
3 # Copyright 2019 Atos - CoE Telco NFV Team
4 # All Rights Reserved.
5 #
6 # Contributors: Oscar Luis Peral, Atos
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # 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, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: <oscarluis.peral@atos.net>
22 #
23 # Neither the name of Atos nor the names of its
24 # contributors may be used to endorse or promote products derived from
25 # this software without specific prior written permission.
26 #
27 # This work has been performed in the context of Arista Telefonica OSM PoC.
28 ##
29
30 from jsonrpclib import Server
31 import socket
32 import ssl
33
34
35 class AristaSwitch():
36 """
37 Used to run switch commands through eAPI and check command output
38 """
39
40 def __init__(self, name=None, host=None, user=None, passwd=None,
41 verify_ssl=False, unix_socket=None,
42 logger=None):
43
44 self.host = host
45 self.user = user
46 self.passwd = passwd
47
48 self.unix_socket = unix_socket
49 self.local_ep = Server(unix_socket) \
50 if unix_socket is not None else None
51
52 s = "https://{user}:{passwd}@{host}/command-api"
53 self.url = s.format(user=user, passwd=passwd, host=host)
54 self.ep = Server(self.url)
55 self.verify_ssl = verify_ssl
56 if not self.verify_ssl:
57 try:
58 ssl._create_default_https_context = ssl.\
59 _create_unverified_context
60 except AttributeError:
61 # Old python versions do not verify certs by default
62 pass
63
64 self.log = logger
65
66 def _multilinestr_to_list(self, multilinestr=None):
67 """
68 Returns a list, each item been one line of a (multi)line string
69 Handy for running multiple lines commands through one API call
70 """
71 mylist = \
72 [x.strip() for x in multilinestr.split('\n') if x.strip() != '']
73 return mylist
74
75 def run(self, cmds=None, timeout=10, local_run=False):
76 """
77 Runs commands through eAPI
78
79 If local_run is True eAPI call will be done using local unix socket
80 If local run is False eAPI call will be done using TCPIP
81 """
82 socket.setdefaulttimeout(timeout)
83
84 r = None
85
86 if type(cmds) is str:
87 run_list = self._multilinestr_to_list(cmds)
88
89 if type(cmds) is list:
90 run_list = cmds
91
92 if local_run:
93 ep = self.local_ep
94 ep_log = "local unix socket {}".format(str(self.unix_socket))
95 else:
96 ep = self.ep
97 ep_log = "tcpip socket {}".format(str(self.host))
98
99 self.log.debug("Calling eAPI at {} with commands {}".
100 format(ep_log, str(run_list)))
101
102 try:
103 r = ep.runCmds(1, run_list)
104 except Exception as e:
105 self.log.error(str(e))
106 raise(e)
107
108 return r