| 1 |
# vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 |
# |
| 3 |
# Copyright 2013 Cisco Systems, Inc. All rights reserved. |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 |
# not use this file except in compliance with the License. You may obtain |
| 7 |
# a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 |
# License for the specific language governing permissions and limitations |
| 15 |
# under the License. |
| 16 |
# |
| 17 |
# @author: Hareesh Puthalath, Cisco Systems, Inc. |
| 18 |
|
| 19 |
import sys |
| 20 |
import logging |
| 21 |
from ncclient import manager |
| 22 |
|
| 23 |
log = logging.getLogger(__name__) |
| 24 |
|
| 25 |
# Various IOS Snippets |
| 26 |
|
| 27 |
CREATE_VRF = """ |
| 28 |
<config> |
| 29 |
<cli-config-data> |
| 30 |
<cmd>ip routing</cmd> |
| 31 |
<cmd>ip vrf %s</cmd> |
| 32 |
</cli-config-data> |
| 33 |
</config> |
| 34 |
""" |
| 35 |
|
| 36 |
|
| 37 |
REMOVE_VRF = """ |
| 38 |
<config> |
| 39 |
<cli-config-data> |
| 40 |
<cmd>ip routing</cmd> |
| 41 |
<cmd>no ip vrf %s</cmd> |
| 42 |
</cli-config-data> |
| 43 |
</config> |
| 44 |
""" |
| 45 |
|
| 46 |
CREATE_SUBINTERFACE = """ |
| 47 |
<config> |
| 48 |
<cli-config-data> |
| 49 |
<cmd>interface %s</cmd> |
| 50 |
<cmd>encapsulation dot1Q %s</cmd> |
| 51 |
<cmd>ip vrf forwarding %s</cmd> |
| 52 |
<cmd>ip address %s %s</cmd> |
| 53 |
</cli-config-data> |
| 54 |
</config> |
| 55 |
|
| 56 |
""" |
| 57 |
|
| 58 |
REMOVE_SUBINTERFACE = """ |
| 59 |
<config> |
| 60 |
<cli-config-data> |
| 61 |
<cmd>no interface %s</cmd> |
| 62 |
</cli-config-data> |
| 63 |
</config> |
| 64 |
""" |
| 65 |
|
| 66 |
|
| 67 |
def csr_connect(host, port, user, password): |
| 68 |
return manager.connect(host=host, |
| 69 |
port=port, |
| 70 |
username=user, |
| 71 |
password=password, |
| 72 |
device_params={'name': "csr"}, |
| 73 |
timeout=30 |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
def create_vrf(conn, vrf_name): |
| 78 |
try: |
| 79 |
confstr = CREATE_VRF % vrf_name |
| 80 |
rpc_obj = conn.edit_config(target='running', config=confstr) |
| 81 |
_check_response(rpc_obj, "CREATE_VRF") |
| 82 |
except Exception: |
| 83 |
log.exception("Exception in creating VRF %s" % vrf_name) |
| 84 |
|
| 85 |
|
| 86 |
def create_subinterface(conn, subinterface, vlan_id, vrf_name, ip, mask): |
| 87 |
try: |
| 88 |
confstr = CREATE_SUBINTERFACE % (subinterface, vlan_id, |
| 89 |
vrf_name, ip, mask) |
| 90 |
rpc_obj = conn.edit_config(target='running', config=confstr) |
| 91 |
_check_response(rpc_obj, 'CREATE_SUBINTERFACE') |
| 92 |
except Exception: |
| 93 |
log.exception("Exception in creating subinterface %s" % subinterface) |
| 94 |
|
| 95 |
|
| 96 |
def remove_vrf(conn, vrf_name): |
| 97 |
try: |
| 98 |
confstr = REMOVE_VRF % vrf_name |
| 99 |
rpc_obj = conn.edit_config(target='running', config=confstr) |
| 100 |
_check_response(rpc_obj, "REMOVE_VRF") |
| 101 |
except Exception: |
| 102 |
log.exception("Exception in removing VRF %s" % vrf_name) |
| 103 |
|
| 104 |
|
| 105 |
def remove_subinterface(conn, subinterface): |
| 106 |
try: |
| 107 |
confstr = REMOVE_SUBINTERFACE % (subinterface) |
| 108 |
rpc_obj = conn.edit_config(target='running', config=confstr) |
| 109 |
_check_response(rpc_obj, 'REMOVE_SUBINTERFACE') |
| 110 |
except Exception: |
| 111 |
log.exception("Exception in removing subinterface %s" % subinterface) |
| 112 |
|
| 113 |
|
| 114 |
def _check_response(rpc_obj, snippet_name): |
| 115 |
log.debug("RPCReply for %s is %s" % (snippet_name, rpc_obj.xml)) |
| 116 |
xml_str = rpc_obj.xml |
| 117 |
if "<ok />" in xml_str: |
| 118 |
log.info("%s successful" % snippet_name) |
| 119 |
else: |
| 120 |
log.error("Cannot successfully execute: %s" % snippet_name) |
| 121 |
|
| 122 |
|
| 123 |
def test_csr(host, user, password): |
| 124 |
with csr_connect(host, port=22, user=user, password=password) as m: |
| 125 |
create_vrf(m, "test_vrf") |
| 126 |
create_subinterface(m, "GigabitEthernet 1.500", |
| 127 |
'500', 'test_vrf', |
| 128 |
'192.168.0.1', '255.255.255.0') |
| 129 |
#Optional |
| 130 |
remove_vrf(m, "test_vrf") |
| 131 |
remove_subinterface(m, "GigabitEthernet 1.500") |
| 132 |
|
| 133 |
|
| 134 |
if __name__ == '__main__': |
| 135 |
logging.basicConfig(level=logging.DEBUG) |
| 136 |
test_csr(sys.argv[1], sys.argv[2], sys.argv[3]) |