Skip to content

Commit

Permalink
Relese a1
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceaulinic committed Jul 23, 2019
1 parent 306e65b commit 0a76cd8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 31 deletions.
75 changes: 47 additions & 28 deletions napalm_iosxr_grpc/xrgrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""
'''
gRPC-based NAPALM driver for IOS-XR.
"""
'''
from __future__ import unicode_literals

import json
import logging

import napalm.base.helpers
Expand All @@ -26,17 +27,21 @@
ConnectionException,
CommandErrorException,
SessionLockedException,
CommandTimeoutException
)

import grpc.framework.interfaces.face.face
from iosxr_grpc.cisco_grpc_client import CiscoGRPCClient

log = logging.getLogger(__name__)


class gRPCXRDriver(NetworkDriver):
"""
'''
gRPCXRDriver driver class.
"""
'''
def __init__(self, hostname, username, password, timeout=60, optional_args=None):
"""Constructor."""
'''Constructor.'''
self.device = None
self.hostname = hostname
self.username = username
Expand All @@ -45,29 +50,22 @@ def __init__(self, hostname, username, password, timeout=60, optional_args=None)
self.optional_args = {}
if optional_args:
self.optional_args = optional_args
self.port = self.optional_args.get("port", 57777)
self.tls_key = self.optional_args.get("tls_key")
self.tls_server_name = self.optional_args.get("tls_server_name")
self.port = self.optional_args.get('port', 57777)
self.tls_key = self.optional_args.get('tls_key')
self.tls_server_name = self.optional_args.get('tls_server_name')

def open(self):
"""Establish connection with the network device."""
log.debug("Establishing the connection over gRPC as %s@%s:%d", self.username, self.hostname, self.port)
self.driver = CiscoGRPCClient(
self.hostname,
self.port,
self.timeout,
self.username,
self.password
)
raise_err = None
raise_class = ConnectionException
err, result = self.driver.showcmdtextoutput("show clock")
def _execute(self, method, *args, **kwargs):
fmt = kwargs.pop('format', 'json')
try:
err, result = getattr(self.driver, method)(*args, **kwargs)
except grpc.framework.interfaces.face.face.ExpirationError as timeout_err:
raise CommandTimeoutException(timeout_err)
if err:
err_obj = {}
try:
err_obj = ast.literal_eval(err)
except (SyntaxError, ValueError) as parse_err:
log.error('Unable to parse the error return from %s', err, exc_info=True)
err_obj = json.loads(err)
except (json.decoder.JSONDecodeError, TypeError) as parse_err:
log.error('Unable to parse the error from %s', err, exc_info=True)
raise_class = None
if 'cisco-grpc:errors' in err_obj and 'error' in err_obj['cisco-grpc:errors']:
err_msg = err_obj['cisco-grpc:errors']['error'][0]
Expand All @@ -79,11 +77,32 @@ def open(self):
raise_err.error_severity = err_msg['error-severity']
log.error(err_msg['error-message'])
raise(raise_err)
if fmt == 'text':
return result
try:
ret = json.loads(result)
except (json.decoder.JSONDecodeError, TypeError) as decode_err:
log.error('Unable to process the return %s', result, exc_info=True)
raise
return ret

def get_facts(self):
"""Collecting facts from the device."""
def open(self):
'''Establish connection with the network device.'''
log.debug('Establishing the connection over gRPC as %s@%s:%d', self.username, self.hostname, self.port)
self.driver = CiscoGRPCClient(
self.hostname,
self.port,
self.timeout,
self.username,
self.password
)
log.debug('Executing "show clock" to check the connection')
result = self._execute('showcmdtextoutput', 'show clock', format='text')

def get_facts(self):
'''Collecting facts from the device.'''
pass

def close(self):
"""Disconnect."""
log.debug("Disconnecting from %s", self.hostname)
'''Disconnect.'''
log.debug('Disconnecting from %s', self.hostname)
13 changes: 10 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@
reqs = [r for r in fs.read().splitlines() if (len(r) > 0 and not r.startswith("#"))]

setup(
name="napalm-mos",
version="2.4.1",
packages=find_packages(),
name="napalm-iosxr-grpc",
version="0.0.1a1",
packages=find_packages(exclude=("test*",)),
author="Mircea Ulinic",
author_email="[email protected]",
description="Network Automation and Programmability Abstraction Layer with Multivendor support",
classifiers=[
"Topic :: Utilities",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Operating System :: POSIX :: Linux",
"Operating System :: MacOS",
],
Expand Down

0 comments on commit 0a76cd8

Please sign in to comment.