1 # -*- coding: utf-8 -*-
3 # Copyright 2019 Atos - CoE Telco NFV Team
6 # Contributors: Oscar Luis Peral, Atos
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: <oscarluis.peral@atos.net>
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.
27 # This work has been performed in the context of Arista Telefonica OSM PoC.
30 from jsonrpclib
import Server
37 Used to run switch commands through eAPI and check command output
40 def __init__(self
, name
=None, host
=None, user
=None, passwd
=None,
41 verify_ssl
=False, unix_socket
=None,
48 self
.unix_socket
= unix_socket
49 self
.local_ep
= Server(unix_socket
) \
50 if unix_socket
is not None else None
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
:
58 ssl
._create
_default
_https
_context
= ssl
.\
59 _create_unverified_context
60 except AttributeError:
61 # Old python versions do not verify certs by default
66 def _multilinestr_to_list(self
, multilinestr
=None):
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
72 [x
.strip() for x
in multilinestr
.split('\n') if x
.strip() != '']
75 def run(self
, cmds
=None, timeout
=10, local_run
=False):
77 Runs commands through eAPI
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
82 socket
.setdefaulttimeout(timeout
)
87 run_list
= self
._multilinestr
_to
_list
(cmds
)
89 if type(cmds
) is list:
94 ep_log
= "local unix socket {}".format(str(self
.unix_socket
))
97 ep_log
= "tcpip socket {}".format(str(self
.host
))
99 self
.log
.debug("Calling eAPI at {} with commands {}".
100 format(ep_log
, str(run_list
)))
103 r
= ep
.runCmds(1, run_list
)
104 except Exception as e
:
105 self
.log
.error(str(e
))