-
Notifications
You must be signed in to change notification settings - Fork 0
/
CUCMConnectorAXL.py
146 lines (133 loc) · 6.06 KB
/
CUCMConnectorAXL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
__author__ = "Alfonso Sandoval"
__version__ = "1.0.1"
__maintainer__ = "Alfonso Sandoval"
__status__ = "Production"
"""
Cisco CUCM connector class
This class creates a connection object via AXL\SOAP to the specified CUCM server using the specified AXL-enabled credentials
"""
import urllib3, logging
from ZeepDebugPlugin import *
from zeep import Client, xsd
from zeep.cache import SqliteCache
from zeep.transports import Transport
from requests import Session
from requests.auth import HTTPBasicAuth
from urllib3.exceptions import InsecureRequestWarning
class CUCMConnectorAXL:
def __init__(self,CUCM_IP,AXL_Username,AXL_Password,CUCM_Version = '11.5',debug = False,logger = False):
"""
Constructor initiates session establishment process when the instance is created
Parameters
----------
AXL_Username : string
AXL-enabled CUCM username
AXL_Password : string
AXL-enabled CUCM password
CUCM_IP : string
Target CUCM IP address
CUCM_Version : string. Default: 11.5
Target CUCM version
debug : boolean. Default: False
Toggle debug plugin for seeing incoming/outgoing SOAP requests in console
logger : logging instance. Default: False
Custom logger for ERROR-type messages handling
"""
self._AXL_Username = AXL_Username
self._AXL_Password = AXL_Password
self._CUCM_IP = CUCM_IP
self._CUCM_Version = CUCM_Version
self._debug = debug
self._logger = logger
self._CLIENT = ''
self._connect_cucm()
self._test_connection()
def _connect_cucm(self):
"""Session establishment with target CUCM node. Returns zeep object"""
try:
WSDL = f'schema/{self._CUCM_Version}/AXLAPI.wsdl'
if('9.' in self._CUCM_Version):
urllib3.disable_warnings()
urllib3.util.ssl_.DEFAULT_CIPHERS += 'HIGH:!DH:!aNULL'
WSDL = 'schema/9.1/AXLAPI.wsdl'
try:
urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST += 'HIGH:!DH:!aNULL'
except AttributeError:
pass
else:
urllib3.disable_warnings(InsecureRequestWarning)
BINDING_NAME = "{http://www.cisco.com/AXLAPIService/}AXLAPIBinding"
ADDRESS = "https://{ip}:8443/axl/".format(ip=self._CUCM_IP)
session = Session()
session.verify = False
session.auth = HTTPBasicAuth(self._AXL_Username, self._AXL_Password)
transport = Transport(cache=SqliteCache(), session=session, timeout=10)
if self._debug:
client = Client(wsdl=WSDL, transport=transport, plugins=[ZeepDebugPlugin()])
else:
client = Client(wsdl=WSDL, transport=transport)
self._CLIENT = client.create_service(BINDING_NAME, ADDRESS)
except FileExistsError:
self._CLIENT = False
if self._logger:
self._logger.error(f'Please verify the existance of the WSDL files corresponding to the CUCM version in the /schema folder' )
else:
logging.error( f'Please verify the existance of the WSDL files corresponding to the CUCM version in the /schema folder' )
pass
def _test_connection(self):
"""Test query for connection validation"""
if self._debug:
logging.basicConfig(level=logging.DEBUG)
logging.debug( f'Test connection query to: ({self._CUCM_IP}) ...')
try:
self._CLIENT.listCallManagerGroup(
searchCriteria = {'name':'%'},
returnedTags = {'name':''}
)
except Exception as err:
self._CLIENT = False
if 'Max retries exceeded' in str(err):
if self._logger:
self._logger.error(f'Server ({self._CUCM_IP}) is unreachable' )
else:
logging.error( f'Server ({self._CUCM_IP}) is unreachable' )
elif 'Unknown fault occured' in str(err):
if self._logger:
self._logger.error( f'Conection error to ({self._CUCM_IP}): Possible credentials mismatch' )
else:
logging.error( f'Conection error to ({self._CUCM_IP}): Possible credentials mismatch' )
else:
if self._logger:
self._logger.error(f'Conection error to ({self._CUCM_IP}): {err}')
else:
logging.error( f'Conection error to ({self._CUCM_IP}): {err}')
pass
def isValid(self):
"""Returns current self._CLIENT value. The value will be False if the test when creating the instance was not successful"""
return self._CLIENT
@staticmethod
def connector(CUCM_IP,AXL_Username,AXL_Password,CUCM_Version = '11.5',debug = False):
"""Returns a standalone connector. No class methods. For testing purposes"""
WSDL = f'schema/{CUCM_Version}/AXLAPI.wsdl'
if('9.' in CUCM_Version):
urllib3.disable_warnings()
urllib3.util.ssl_.DEFAULT_CIPHERS += 'HIGH:!DH:!aNULL'
WSDL = 'schema/9.1/AXLAPI.wsdl'
try:
urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST += 'HIGH:!DH:!aNULL'
except AttributeError:
pass
else:
urllib3.disable_warnings(InsecureRequestWarning)
BINDING_NAME = "{http://www.cisco.com/AXLAPIService/}AXLAPIBinding"
ADDRESS = "https://{ip}:8443/axl/".format(ip=CUCM_IP)
session = Session()
session.verify = False
session.auth = HTTPBasicAuth(AXL_Username, AXL_Password)
transport = Transport(cache=SqliteCache(), session=session, timeout=10)
if debug:
client = Client(wsdl=WSDL, transport=transport, plugins=[ZeepDebugPlugin()])
else:
client = Client(wsdl=WSDL, transport=transport)
return [client.create_service(BINDING_NAME, ADDRESS),client]