diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ifmgr-cfg/nc-read-xr-ifmgr-cfg-20-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ifmgr-cfg/nc-read-xr-ifmgr-cfg-20-ydk.py new file mode 100755 index 0000000..1be5db5 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ifmgr-cfg/nc-read-xr-ifmgr-cfg-20-ydk.py @@ -0,0 +1,135 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Read all data for model Cisco-IOS-XR-ifmgr-cfg. + +Outputs is to emulate the CLI show command + +usage: show-config-interfaces-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ifmgr_cfg \ + as xr_ifmgr_cfg +from datetime import timedelta +import logging + +def process_interface_configurations(interface_configurations): + spacing = 0 + show_interface_config = str() + + # iterate over interface configurations + for interface_configuration in interface_configurations.interface_configuration: + show_interface_config = show_interface_config + '\n' + \ + (' ' * spacing) + \ + "interface " + \ + interface_configuration.interface_name + + spacing += 1 + if interface_configuration.vrf: + show_interface_config = show_interface_config + '\n' + \ + (' ' * spacing) + \ + "vrf " + interface_configuration.vrf + if interface_configuration.ipv4_network.addresses.primary: + show_interface_config = show_interface_config + '\n' + \ + (' ' * spacing) + \ + "ipv4 address " + \ + interface_configuration.ipv4_network.addresses.primary.address + + show_interface_config = show_interface_config + \ + " " + \ + interface_configuration.ipv4_network.addresses.primary.netmask + + for secondary in interface_configuration.ipv4_network.addresses.secondaries.secondary: + show_interface_config = show_interface_config + '\n' + \ + (' ' * spacing) + \ + "ipv4 address " + \ + secondary.address + if interface_configuration.ipv6_network.addresses.regular_addresses: + for regular_address in \ + interface_configuration.ipv6_network.addresses.regular_addresses.regular_address: + + show_interface_config = show_interface_config + '\n' + \ + (' ' * spacing) + \ + "ipv6 address " + \ + regular_address.address + + show_interface_config = show_interface_config + \ + "/" + \ + str(regular_address.prefix_length) + + if interface_configuration.shutdown: + show_interface_config = show_interface_config + '\n' + \ + (' ' * spacing) + \ + "shutdown" + spacing -= 1 + show_interface_config = show_interface_config + '\n' + \ + (' ' * spacing) + \ + "!" + # return formatted string + return(show_interface_config) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + interface_configurations = xr_ifmgr_cfg.InterfaceConfigurations() # create object + + # read data from NETCONF device + interface_configurations = crud.read(provider, interface_configurations) + print(process_interface_configurations(interface_configurations)) # process object data + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-infra-rsi-cfg/nc-create-xr-infra-rsi-cfg-40-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-infra-rsi-cfg/nc-create-xr-infra-rsi-cfg-40-ydk.py new file mode 100755 index 0000000..28f5c87 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-infra-rsi-cfg/nc-create-xr-infra-rsi-cfg-40-ydk.py @@ -0,0 +1,172 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Create configuration for model Cisco-IOS-XR-infra-rsi-cfg. +Creates using a generic model and command line arguments. + +usage: nc-create-xr-infra-rsi-cfg-40-ydk.py [-h] [-v] device name af saf direction as + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + name VRF name + af Address Family Name (ipv4/ipv6) + saf sub address family (unicast/multicast) + direction route (import/export) + as AS Number (as_:as_index) + + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_infra_rsi_cfg \ + as xr_infra_rsi_cfg +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_cfg \ + as xr_ipv4_bgp_cfg +from ydk.types import Empty +import logging + + +def af_enum(af): + af = af.lower() + if af == "ipv4": + return xr_infra_rsi_cfg.VrfAddressFamilyEnum.ipv4 + elif af == "ipv6": + return xr_infra_rsi_cfg.VrfAddressFamilyEnum.ipv6 + else: + print "Invalid AF name" + exit(1) + +def saf_enum(saf): + saf = saf.lower() + if saf == "unicast": + return xr_infra_rsi_cfg.VrfSubAddressFamilyEnum.unicast + elif saf == "multicast": + return xr_infra_rsi_cfg.VrfSubAddressFamilyEnum.multicast + else: + print "Invalid SAF" + exit(1) + + +def config_vrfs(vrfs, name, af, saf, direction, as_): + """Add config data to vrfs object.""" + vrf = vrfs.Vrf() + vrf.vrf_name = name + vrf.create = Empty() + + # Get af information + af_name_enum = af_enum(af) + saf_name_enum = saf_enum(saf) + as_list = as_.split(':') + if len(as_list) < 2: + print "Incorrect AS format" + exit(1) + + # address family + af = vrf.afs.Af() + af.af_name = af_name_enum + af.saf_name = saf_name_enum + af.topology_name = "default" + af.create = Empty() + + # import route targets + if direction.lower() == "import": + route_target = af.bgp.import_route_targets.route_targets.RouteTarget() + route_target.type = xr_ipv4_bgp_cfg.BgpVrfRouteTargetEnum.as_ + as_or_four_byte_as = route_target.AsOrFourByteAs() + as_or_four_byte_as.as_xx = 0 + as_or_four_byte_as.as_ = int(as_list[0]) + as_or_four_byte_as.as_index = int(as_list[1]) + as_or_four_byte_as.stitching_rt = 0 + route_target.as_or_four_byte_as.append(as_or_four_byte_as) + af.bgp.import_route_targets.route_targets.route_target.append(route_target) + + # export route targets + elif direction.lower() == "export": + route_target = af.bgp.export_route_targets.route_targets.RouteTarget() + route_target.type = xr_ipv4_bgp_cfg.BgpVrfRouteTargetEnum.as_ + as_or_four_byte_as = route_target.AsOrFourByteAs() + as_or_four_byte_as.as_xx = 0 + as_or_four_byte_as.as_ = int(as_list[0]) + as_or_four_byte_as.as_index = int(as_list[1]) + as_or_four_byte_as.stitching_rt = 0 + route_target.as_or_four_byte_as.append(as_or_four_byte_as) + af.bgp.export_route_targets.route_targets.route_target.append(route_target) + + else: + print "invalid direction" + exit(1) + + # append address family and vrf + vrf.afs.af.append(af) + vrfs.vrf.append(vrf) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + parser.add_argument("name", + help="VRF name") + parser.add_argument("af", + help="AF name (ipv4/ipv6)") + parser.add_argument("saf", + help="sub AF name (unicast/multicast)") + parser.add_argument("direction", + help="route (import/export)") + parser.add_argument("as_", + help="AS (as_:as_index)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + vrfs = xr_infra_rsi_cfg.Vrfs() # create object + config_vrfs(vrfs, args.name, args.af, args.saf, args.direction, args.as_) # add object configuration + + # create configuration on NETCONF device + crud.create(provider, vrfs) + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-infra-rsi-cfg/nc-create-xr-infra-rsi-cfg-40-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-infra-rsi-cfg/nc-create-xr-infra-rsi-cfg-40-ydk.txt new file mode 100644 index 0000000..2430430 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-infra-rsi-cfg/nc-create-xr-infra-rsi-cfg-40-ydk.txt @@ -0,0 +1,6 @@ +vrf name + address-family af saf + direction route-target + as +! +end diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-infra-rsi-cfg/nc-read-xr-infra-rsi-cfg-20-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-infra-rsi-cfg/nc-read-xr-infra-rsi-cfg-20-ydk.py new file mode 100755 index 0000000..fb2f262 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-infra-rsi-cfg/nc-read-xr-infra-rsi-cfg-20-ydk.py @@ -0,0 +1,135 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Read all data for model Cisco-IOS-XR-ipv4-vrfs-oper. + +Outputs is to emulate the CLI show command + +usage: show-config-global-vrf-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_infra_rsi_cfg \ + as xr_infra_rsi_cfg +from datetime import timedelta +import logging + +def process_vrfs(vrfs): + spacing = 0 + """Process data in vrfs object.""" + if vrfs: + show_vrfs_config = str() + else: + return "" + # iterate on vrfs + for vrf in vrfs.vrf: + show_vrfs_config = show_vrfs_config + '\n' + (' ' * spacing) + \ + "vrf " + vrf.vrf_name + + # iterate on AFs + spacing += 1 + for af in vrf.afs.af: + show_vrfs_config = show_vrfs_config + '\n' + (' ' * spacing) + \ + "address-family " + af.af_name.name + \ + " " + af.saf_name.name + + #iterate on import route target + spacing += 1 + if af.bgp.import_route_targets.route_targets.route_target: + show_vrfs_config = show_vrfs_config + '\n' + (' ' * spacing) + \ + "import route-target " + spacing += 1 + for route_target in af.bgp.import_route_targets.route_targets.route_target: + for as_or_four_byte_as in route_target.as_or_four_byte_as: + show_vrfs_config = show_vrfs_config + '\n' + (' ' * spacing) + \ + str(as_or_four_byte_as.as_) + ":" + \ + str(as_or_four_byte_as.as_index) + spacing -= 1 + show_vrfs_config = show_vrfs_config + '\n' + (' ' * spacing) + "!" + spacing -= 1 + + + #iterate on export route target + spacing += 1 + if af.bgp.export_route_targets.route_targets.route_target: + show_vrfs_config = show_vrfs_config + '\n' + (' ' * spacing) + \ + "export route-target " + spacing += 1 + for route_target in af.bgp.export_route_targets.route_targets.route_target: + for as_or_four_byte_as in route_target.as_or_four_byte_as: + show_vrfs_config = show_vrfs_config + '\n' + (' ' * spacing) + \ + str(as_or_four_byte_as.as_) + ":" + \ + str(as_or_four_byte_as.as_index) + spacing -= 1 + show_vrfs_config = show_vrfs_config + '\n' + (' ' * spacing) + "!" + spacing -= 1 + + show_vrfs_config = show_vrfs_config + '\n' + (' ' * spacing) + "!" + spacing -= 1 + show_vrfs_config = show_vrfs_config + '\n' + (' ' * spacing) + "!" + return(show_vrfs_config) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + vrfs = xr_infra_rsi_cfg.Vrfs() # create object + + # read data from NETCONF device + vrfs = crud.read(provider, vrfs) + print(process_vrfs(vrfs)) # process object data + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-create-xr-ip-static-cfg-40-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-create-xr-ip-static-cfg-40-ydk.py new file mode 100755 index 0000000..b80e7b7 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-create-xr-ip-static-cfg-40-ydk.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Create configuration for model Cisco-IOS-XR-ip-static-cfg. +Creates using a generic model and command line arguments. + +usage: nc-create-xr-ip-static-cfg-40-ydk.py [-h] [-v] device prefix refixlen interface + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + prefix ip of prefix + prefixlen length of prefix + interface next hop interface name + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ip_static_cfg \ + as xr_ip_static_cfg +import logging + +def config_router_static(router_static, prefix, prefixlen, interface): + """Add config data to router_static object.""" + # create prefix and prefix length + vrf_unicast = router_static.default_vrf.address_family.vrfipv4.vrf_unicast + vrf_prefix = vrf_unicast.vrf_prefixes.VrfPrefix() + vrf_prefix.prefix = prefix + vrf_prefix.prefix_length = int(prefixlen) + + # create nexthop interface + vrf_next_hop_interface_name = vrf_prefix.vrf_route. \ + vrf_next_hop_table.VrfNextHopInterfaceName() + vrf_next_hop_interface_name.interface_name = interface + + # append static route + vrf_prefix.vrf_route.vrf_next_hop_table.vrf_next_hop_interface_name. \ + append(vrf_next_hop_interface_name) + vrf_unicast.vrf_prefixes.vrf_prefix.append(vrf_prefix) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + parser.add_argument("prefix", + help="Prefix IP") + parser.add_argument("prefixlen", + help="Length of prefix") + parser.add_argument("interface", + help="interface of next hop?") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + router_static = xr_ip_static_cfg.RouterStatic() # create object + config_router_static(router_static, args.prefix, + args.prefixlen, args.interface) # add object configuration + + # create configuration on NETCONF device + crud.create(provider, router_static) + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-create-xr-ip-static-cfg-40-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-create-xr-ip-static-cfg-40-ydk.txt new file mode 100644 index 0000000..95c209e --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-create-xr-ip-static-cfg-40-ydk.txt @@ -0,0 +1,5 @@ +router static + address-family ipv4 unicast + prefix/prefixlen interface + ! +! \ No newline at end of file diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-create-xr-ip-static-cfg-41-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-create-xr-ip-static-cfg-41-ydk.py new file mode 100755 index 0000000..b939cac --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-create-xr-ip-static-cfg-41-ydk.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Create configuration for model Cisco-IOS-XR-ip-static-cfg. +Creates using a generic model and command line arguments. + +usage: nc-create-xr-ip-static-cfg-41-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + prefix ip of prefix + prefixlen length of prefix + interface next hop interface name + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse + +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ip_static_cfg \ + as xr_ip_static_cfg +import logging + +#Take in arguments for prefix and interface name +def config_router_static(router_static, prefix, prefixlen, interface): + """Add config data to router_static object.""" + # create prefix and prefix length + vrf_unicast = router_static.default_vrf.address_family.vrfipv6.vrf_unicast + vrf_prefix = vrf_unicast.vrf_prefixes.VrfPrefix() + vrf_prefix.prefix = prefix + vrf_prefix.prefix_length = int(prefixlen) + + # create nexthop interface + vrf_next_hop_interface_name = vrf_prefix.vrf_route. \ + vrf_next_hop_table.VrfNextHopInterfaceName() + vrf_next_hop_interface_name.interface_name = interface + + # append static route + vrf_prefix.vrf_route.vrf_next_hop_table.vrf_next_hop_interface_name. \ + append(vrf_next_hop_interface_name) + vrf_unicast.vrf_prefixes.vrf_prefix.append(vrf_prefix) + +#allow to take a variable for the prefix and interface name +#make one for ipv4 and one for ipv6 +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + parser.add_argument("prefix", + help="Prefix IP") + parser.add_argument("prefixlen", + help="Length of prefix") + parser.add_argument("interface", + help="interface of next hop?") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + router_static = xr_ip_static_cfg.RouterStatic() # create object + config_router_static(router_static, args.prefix, + args.prefixlen, args.interface) # add object configuration + + # create configuration on NETCONF device + crud.create(provider, router_static) + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-create-xr-ip-static-cfg-41-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-create-xr-ip-static-cfg-41-ydk.txt new file mode 100644 index 0000000..68f6694 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-create-xr-ip-static-cfg-41-ydk.txt @@ -0,0 +1,5 @@ +router static + address-family ipv6 unicast + prefix/prefixlen interface + ! +! \ No newline at end of file diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-read-xr-ip-static-cfg-20-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-read-xr-ip-static-cfg-20-ydk.py new file mode 100755 index 0000000..2241d93 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ip-static-cfg/nc-read-xr-ip-static-cfg-20-ydk.py @@ -0,0 +1,134 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Read all data for model Cisco-IOS-XR-ip-static-cfg. + +Outputs is to emulate the CLI show command + +usage: show-config-static-route-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ip_static_cfg \ + as xr_ip_static_cfg +from datetime import timedelta +import logging + +def process_router_static(router_static): + spacing = 0 + """Process data in router_static object.""" + if router_static: + show_router_static_config = str() + else: + show_router_static_config = "No static instances found" + + # ipv4 unicast routes + spacing += 1 + if router_static.default_vrf.address_family.vrfipv4.vrf_unicast.vrf_prefixes.vrf_prefix: + show_router_static_config = show_router_static_config + '\n' + \ + (' ' * spacing) + "address-family ipv4 unicast" + spacing += 1 + for vrf in router_static.default_vrf.address_family.vrfipv4.vrf_unicast.vrf_prefixes.vrf_prefix: + + for interface in vrf.vrf_route.vrf_next_hop_table.vrf_next_hop_interface_name: + route_string = (' ' * spacing) + \ + vrf.prefix + '/' + \ + str(vrf.prefix_length) + \ + ' ' + interface.interface_name + show_router_static_config = show_router_static_config + '\n' + \ + route_string + spacing -= 1 + show_router_static_config = show_router_static_config + \ + '\n' + (' ' * spacing) + '!' + + # ipv6 unicast routes + if router_static.default_vrf.address_family.vrfipv6.vrf_unicast.vrf_prefixes.vrf_prefix: + show_router_static_config = show_router_static_config + '\n' + \ + (' ' * spacing) + "address-family ipv6 unicast" + spacing += 1 + for vrf in router_static.default_vrf.address_family.vrfipv6.vrf_unicast.vrf_prefixes.vrf_prefix: + + for interface in vrf.vrf_route.vrf_next_hop_table.vrf_next_hop_interface_name: + route_string = (' ' * spacing) + \ + vrf.prefix + '/' + \ + str(vrf.prefix_length) + \ + ' ' + interface.interface_name + show_router_static_config = show_router_static_config + '\n' + \ + route_string + spacing -= 1 + show_router_static_config = show_router_static_config + \ + '\n' + (' ' * spacing) +'!' + + spacing -= 1 + show_router_static_config = show_router_static_config + \ + (' ' * spacing) +'\n!' + # return formatted string + if show_router_static_config.replace("!", "").strip() == "": + return "" + show_router_static_config = "router static" + show_router_static_config + return(show_router_static_config) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + router_static = xr_ip_static_cfg.RouterStatic() # create object + + # read data from NETCONF device + router_static = crud.read(provider, router_static) + print(process_router_static(router_static)) # process object data + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-95-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-95-ydk.py new file mode 100755 index 0000000..be46c33 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-95-ydk.py @@ -0,0 +1,146 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Create configuration for model Cisco-IOS-XR-ipv4-bgp-cfg. + +usage: bgp-ipv4-cfg-RR-ydk [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse + +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_cfg \ + as xr_ipv4_bgp_cfg +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_datatypes \ + as xr_ipv4_bgp_datatypes +from ydk.types import Empty +import logging + + +def config_bgp(bgp): + """Add config data to bgp object.""" + # global configuration + instance = bgp.Instance() + instance.instance_name = "default" + instance_as = instance.InstanceAs() + instance_as.as_ = 0 + four_byte_as = instance_as.FourByteAs() + four_byte_as.as_ = 100 + four_byte_as.bgp_running = Empty() + four_byte_as.default_vrf.global_.router_id = "200.1.1.2" + # global address family + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + # global address family for vpnv4 + global_af2 = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af2.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.vp_nv4_unicast + global_af2.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af2) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + + # configure rr-client neighbor group + neighbor_groups = four_byte_as.default_vrf.bgp_entity.neighbor_groups + neighbor_group = neighbor_groups.NeighborGroup() + neighbor_group.neighbor_group_name = "rr-client" + neighbor_group.create = Empty() + # remote AS + neighbor_group.remote_as.as_xx = 0 + neighbor_group.remote_as.as_yy = 100 + neighbor_groups.neighbor_group.append(neighbor_group) + # ipv4 unicast + neighbor_group_af = neighbor_group.neighbor_group_afs.NeighborGroupAf() + neighbor_group_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + neighbor_group_af.route_reflector_client = True + neighbor_group_af.activate = Empty() + neighbor_group_afs = neighbor_group.neighbor_group_afs + neighbor_group_afs.neighbor_group_af.append(neighbor_group_af) + # vpnv4 unicast + neighbor_group_af2 = neighbor_group.neighbor_group_afs.NeighborGroupAf() + neighbor_group_af2.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.vp_nv4_unicast + neighbor_group_af2.route_reflector_client = True + neighbor_group_af2.activate = Empty() + neighbor_group_afs2 = neighbor_group.neighbor_group_afs + neighbor_group_afs2.neighbor_group_af.append(neighbor_group_af2) + + # configure neighbor 80.1.1.1 + neighbor = four_byte_as.default_vrf.bgp_entity.neighbors.Neighbor() + neighbor.neighbor_address = "80.1.1.1" + neighbor.neighbor_group_add_member = "rr-client" + four_byte_as.default_vrf.bgp_entity.neighbors.neighbor.append(neighbor) + + # configure neighbor 200.1.1.1 + neighbor = four_byte_as.default_vrf.bgp_entity.neighbors.Neighbor() + neighbor.neighbor_address = "200.1.1.1" + neighbor.neighbor_group_add_member = "rr-client" + four_byte_as.default_vrf.bgp_entity.neighbors.neighbor.append(neighbor) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + bgp = xr_ipv4_bgp_cfg.Bgp() # create object + config_bgp(bgp) # add object configuration + + # create configuration on NETCONF device + crud.create(provider, bgp) + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-95-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-95-ydk.txt new file mode 100644 index 0000000..354d17e --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-95-ydk.txt @@ -0,0 +1,23 @@ +router bgp 100 + bgp router-id 200.1.1.2 + address-family ipv4 unicast + ! + address-family vpnv4 unicast + ! + neighbor-group rr-client + remote-as 100 + address-family ipv4 unicast + route-reflector-client + ! + address-family vpnv4 unicast + route-reflector-client + ! + ! + neighbor 80.1.1.1 + use neighbor-group rr-client + ! + neighbor 200.1.1.1 + use neighbor-group rr-client + ! +! +end diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-96-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-96-ydk.py new file mode 100755 index 0000000..37240eb --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-96-ydk.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Create configuration for model Cisco-IOS-XR-ipv4-bgp-cfg. + +usage: bgp-ipv4-cfg-CE2-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + + +from argparse import ArgumentParser +from urlparse import urlparse + +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_cfg \ + as xr_ipv4_bgp_cfg +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_datatypes \ + as xr_ipv4_bgp_datatypes +from ydk.types import Empty +import logging + + +def config_bgp(bgp): + """Add config data to bgp object.""" + # global configuration + instance = bgp.Instance() + instance.instance_name = "default" + instance_as = instance.InstanceAs() + instance_as.as_ = 0 + four_byte_as = instance_as.FourByteAs() + four_byte_as.as_ = 100 + four_byte_as.bgp_running = Empty() + four_byte_as.default_vrf.global_.router_id = "110.1.1.2" + # global address family + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + connected_routes = global_af.ConnectedRoutes() + static_routes = global_af.StaticRoutes() + global_af.connected_routes = connected_routes + global_af.static_routes = static_routes + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + # global address family for vpnv4 + global_af2 = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af2.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.vp_nv4_unicast + global_af2.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af2) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + + # configure neighbor 110.1.1.1 + neighbor = four_byte_as.default_vrf.bgp_entity.neighbors.Neighbor() + neighbor.neighbor_address = "110.1.1.1" + neighbor.remote_as.as_xx = 0 + neighbor.remote_as.as_yy = 100 + # address family ipv4 + neighbor_af = neighbor.neighbor_afs.NeighborAf() + neighbor_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + neighbor_af.route_policy_in = "pass-all" + neighbor_af.route_policy_out = "pass-all" + neighbor_af.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af) + # address family vpnv4 + neighbor_af2 = neighbor.neighbor_afs.NeighborAf() + neighbor_af2.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.vp_nv4_unicast + neighbor_af2.route_policy_in = "pass-all" + neighbor_af2.route_policy_out = "pass-all" + neighbor_af2.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af2) + four_byte_as.default_vrf.bgp_entity.neighbors.neighbor.append(neighbor) + + + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + bgp = xr_ipv4_bgp_cfg.Bgp() # create object + config_bgp(bgp) # add object configuration + + # create configuration on NETCONF device + crud.create(provider, bgp) + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-96-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-96-ydk.txt new file mode 100644 index 0000000..15401f9 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-96-ydk.txt @@ -0,0 +1,21 @@ +router bgp 300 + bgp router-id 110.1.1.2 + address-family ipv4 unicast + redistribute connected + redistribute static + ! + address-family vpnv4 unicast + ! + neighbor 110.1.1.1 + remote-as 100 + address-family ipv4 unicast + route-policy pass-all in + route-policy pass-all out + ! + address-family vpnv4 unicast + route-policy pass-all in + route-policy pass-all out + ! + ! +! +end \ No newline at end of file diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-97-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-97-ydk.py new file mode 100755 index 0000000..742c583 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-97-ydk.py @@ -0,0 +1,185 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Create configuration for model Cisco-IOS-XR-ipv4-bgp-cfg. + +usage: bgp-ipv4-cfg-CE1-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_cfg \ + as xr_ipv4_bgp_cfg +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_datatypes \ + as xr_ipv4_bgp_datatypes +from ydk.types import Empty +import logging + + +def config_bgp(bgp): + """Add config data to bgp object.""" + # global configuration + instance = bgp.Instance() + instance.instance_name = "default" + instance_as = instance.InstanceAs() + instance_as.as_ = 0 + four_byte_as = instance_as.FourByteAs() + four_byte_as.as_ = 200 + four_byte_as.bgp_running = Empty() + four_byte_as.default_vrf.global_.router_id = "10.1.1.2" + # global address family + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + connected_routes = global_af.ConnectedRoutes() + static_routes = global_af.StaticRoutes() + global_af.connected_routes = connected_routes + global_af.static_routes = static_routes + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + # global address family for vpnv4 + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.vp_nv4_unicast + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + # global address family for ipv6 + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv6_unicast + connected_routes = global_af.ConnectedRoutes() + static_routes = global_af.StaticRoutes() + global_af.connected_routes = connected_routes + global_af.static_routes = static_routes + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + # global address family for vpnv6 + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.vp_nv6_unicast + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + # global address family for l2vpn + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.l2vpnevpn + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + + # configure neighbor 2001::6 + neighbor = four_byte_as.default_vrf.bgp_entity.neighbors.Neighbor() + neighbor.neighbor_address = "2001::6" + neighbor.remote_as.as_xx = 0 + neighbor.remote_as.as_yy = 100 + # address family ipv4 + neighbor_af = neighbor.neighbor_afs.NeighborAf() + neighbor_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + neighbor_af.route_policy_in = "pass-all" + neighbor_af.route_policy_out = "pass-all" + neighbor_af.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af) + # address family ipv6 + neighbor_af2 = neighbor.neighbor_afs.NeighborAf() + neighbor_af2.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv6_unicast + neighbor_af2.route_policy_in = "pass-all" + neighbor_af2.route_policy_out = "pass-all" + neighbor_af2.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af2) + four_byte_as.default_vrf.bgp_entity.neighbors.neighbor.append(neighbor) + + # configure neighbor 10.1.1.1 + neighbor = four_byte_as.default_vrf.bgp_entity.neighbors.Neighbor() + neighbor.neighbor_address = "10.1.1.1" + neighbor.remote_as.as_xx = 0 + neighbor.remote_as.as_yy = 100 + # address family ipv4 + neighbor_af = neighbor.neighbor_afs.NeighborAf() + neighbor_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + neighbor_af.route_policy_in = "pass-all" + neighbor_af.route_policy_out = "pass-all" + neighbor_af.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af) + # address family vpnv4 + neighbor_af2 = neighbor.neighbor_afs.NeighborAf() + neighbor_af2.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.vp_nv4_unicast + neighbor_af2.route_policy_in = "pass-all" + neighbor_af2.route_policy_out = "pass-all" + neighbor_af2.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af2) + four_byte_as.default_vrf.bgp_entity.neighbors.neighbor.append(neighbor) + + + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + bgp = xr_ipv4_bgp_cfg.Bgp() # create object + config_bgp(bgp) # add object configuration + + # create configuration on NETCONF device + crud.create(provider, bgp) + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-97-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-97-ydk.txt new file mode 100644 index 0000000..5fd7b4a --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-97-ydk.txt @@ -0,0 +1,40 @@ +router bgp 200 + bgp router-id 10.1.1.2 + address-family ipv4 unicast + redistribute connected + redistribute static + ! + address-family vpnv4 unicast + ! + address-family ipv6 unicast + redistribute connected + redistribute static + ! + address-family vpnv6 unicast + ! + address-family l2vpn evpn + ! + neighbor 2001::6 + remote-as 100 + address-family ipv4 unicast + route-policy pass-all in + route-policy pass-all out + ! + address-family ipv6 unicast + route-policy pass-all in + route-policy pass-all out + ! + ! + neighbor 10.1.1.1 + remote-as 100 + address-family ipv4 unicast + route-policy pass-all in + route-policy pass-all out + ! + address-family vpnv4 unicast + route-policy pass-all in + route-policy pass-all out + ! + ! +! +end \ No newline at end of file diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-98-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-98-ydk.py new file mode 100755 index 0000000..8085520 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-98-ydk.py @@ -0,0 +1,254 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Create configuration for model Cisco-IOS-XR-ipv4-bgp-cfg. + +usage: bgp-ipv4-cfg-PE2-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_cfg \ + as xr_ipv4_bgp_cfg +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_datatypes \ + as xr_ipv4_bgp_datatypes +from ydk.types import Empty +import logging + + +def config_bgp(bgp): + """Add config data to bgp object.""" + # global configuration + instance = bgp.Instance() + instance.instance_name = "default" + instance_as = instance.InstanceAs() + instance_as.as_ = 0 + four_byte_as = instance_as.FourByteAs() + four_byte_as.as_ = 100 + four_byte_as.bgp_running = Empty() + four_byte_as.default_vrf.global_.router_id = "110.1.1.1" + # global address family + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + connected_routes = global_af.ConnectedRoutes() + static_routes = global_af.StaticRoutes() + global_af.connected_routes = connected_routes + global_af.static_routes = static_routes + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + # global address family for vpnv4 + global_af2 = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af2.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.vp_nv4_unicast + global_af2.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af2) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + + # configure neighbor group ce + neighbor_groups = four_byte_as.default_vrf.bgp_entity.neighbor_groups + neighbor_group = neighbor_groups.NeighborGroup() + neighbor_group.neighbor_group_name = "ce" + neighbor_group.create = Empty() + # remote AS + neighbor_group.remote_as.as_xx = 0 + neighbor_group.remote_as.as_yy = 200 + neighbor_groups.neighbor_group.append(neighbor_group) + # ipv4 unicast + neighbor_group_af = neighbor_group.neighbor_group_afs.NeighborGroupAf() + neighbor_group_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + neighbor_group_af.route_policy_in = "passall" + neighbor_group_af.route_policy_out = "passall" + neighbor_group_af.activate = Empty() + neighbor_group_afs = neighbor_group.neighbor_group_afs + neighbor_group_afs.neighbor_group_af.append(neighbor_group_af) + + # configure neighbor 80.1.1.2 + neighbor = four_byte_as.default_vrf.bgp_entity.neighbors.Neighbor() + neighbor.neighbor_address = "80.1.1.2" + neighbor.remote_as.as_xx = 0 + neighbor.remote_as.as_yy = 100 + # address family ipv4 + neighbor_af = neighbor.neighbor_afs.NeighborAf() + neighbor_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + neighbor_af.route_policy_in = "passall" + neighbor_af.route_policy_out = "passall" + neighbor_af.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af) + # address family vpnv4 + neighbor_af2 = neighbor.neighbor_afs.NeighborAf() + neighbor_af2.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.vp_nv4_unicast + neighbor_af2.route_policy_in = "passall" + neighbor_af2.route_policy_out = "passall" + neighbor_af2.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af2) + four_byte_as.default_vrf.bgp_entity.neighbors.neighbor.append(neighbor) + + # VRF 1 + vrf = four_byte_as.vrfs.Vrf() + vrf.vrf_name = "1" + # vrf global + vrf.vrf_global.route_distinguisher.type = xr_ipv4_bgp_cfg.BgpRouteDistinguisherEnum.four_byte_as + vrf.vrf_global.route_distinguisher.as_xx = 0 + vrf.vrf_global.route_distinguisher.as_ = 11 + vrf.vrf_global.route_distinguisher.as_index = 11 + vrf.vrf_global.exists = Empty() + # vrf global af + vrf_global_af = vrf.vrf_global.vrf_global_afs.VrfGlobalAf() + vrf_global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + static_routes = vrf_global_af.StaticRoutes() + vrf_global_af.static_routes = static_routes + vrf_global_af.enable = Empty() + vrf.vrf_global.vrf_global_afs.vrf_global_af.append(vrf_global_af) + # vrf neighbor + vrf_neighbor = vrf.vrf_neighbors.VrfNeighbor() + vrf_neighbor.neighbor_address = "110.1.1.2" + vrf_neighbor.remote_as.as_xx = 0 + vrf_neighbor.remote_as.as_yy = 300 + vrf_neighbor.neighbor_group_add_member = "ce" + # vrf neighbor af + vrf_neighbor_af = vrf_neighbor.vrf_neighbor_afs.VrfNeighborAf() + vrf_neighbor_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + vrf_neighbor_af.route_policy_in = "passall" + vrf_neighbor_af.route_policy_out = "passall" + vrf_neighbor_af.activate = Empty() + vrf_neighbor.vrf_neighbor_afs.vrf_neighbor_af.append(vrf_neighbor_af) + vrf.vrf_neighbors.vrf_neighbor.append(vrf_neighbor) + four_byte_as.vrfs.vrf.append(vrf) + + # VRF 2 + vrf = four_byte_as.vrfs.Vrf() + vrf.vrf_name = "2" + # vrf global + vrf.vrf_global.route_distinguisher.type = xr_ipv4_bgp_cfg.BgpRouteDistinguisherEnum.four_byte_as + vrf.vrf_global.route_distinguisher.as_xx = 0 + vrf.vrf_global.route_distinguisher.as_ = 12 + vrf.vrf_global.route_distinguisher.as_index = 12 + vrf.vrf_global.exists = Empty() + # vrf global af + vrf_global_af = vrf.vrf_global.vrf_global_afs.VrfGlobalAf() + vrf_global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + vrf_global_af.table_policy = "allow-only-pfxlen-32" + vrf_global_af.enable = Empty() + vrf.vrf_global.vrf_global_afs.vrf_global_af.append(vrf_global_af) + # vrf neighbor + vrf_neighbor = vrf.vrf_neighbors.VrfNeighbor() + vrf_neighbor.neighbor_address = "120.1.1.2" + vrf_neighbor.neighbor_group_add_member = "ce" + # vrf neighbor af + vrf.vrf_neighbors.vrf_neighbor.append(vrf_neighbor) + four_byte_as.vrfs.vrf.append(vrf) + + # VRF 3 + vrf = four_byte_as.vrfs.Vrf() + vrf.vrf_name = "3" + # vrf global + vrf.vrf_global.route_distinguisher.type = xr_ipv4_bgp_cfg.BgpRouteDistinguisherEnum.four_byte_as + vrf.vrf_global.route_distinguisher.as_xx = 0 + vrf.vrf_global.route_distinguisher.as_ = 13 + vrf.vrf_global.route_distinguisher.as_index = 13 + vrf.vrf_global.exists = Empty() + # vrf global af + vrf_global_af = vrf.vrf_global.vrf_global_afs.VrfGlobalAf() + vrf_global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + vrf_global_af.table_policy = "allow-only-pfxlen-32" + vrf_global_af.enable = Empty() + vrf.vrf_global.vrf_global_afs.vrf_global_af.append(vrf_global_af) + # vrf neighbor + vrf_neighbor = vrf.vrf_neighbors.VrfNeighbor() + vrf_neighbor.neighbor_address = "130.1.1.2" + vrf_neighbor.neighbor_group_add_member = "ce" + # vrf neighbor af + vrf.vrf_neighbors.vrf_neighbor.append(vrf_neighbor) + four_byte_as.vrfs.vrf.append(vrf) + + # VRF 4 + vrf = four_byte_as.vrfs.Vrf() + vrf.vrf_name = "4" + # vrf global + vrf.vrf_global.route_distinguisher.type = xr_ipv4_bgp_cfg.BgpRouteDistinguisherEnum.four_byte_as + vrf.vrf_global.route_distinguisher.as_xx = 0 + vrf.vrf_global.route_distinguisher.as_ = 14 + vrf.vrf_global.route_distinguisher.as_index = 14 + vrf.vrf_global.exists = Empty() + # vrf global af + vrf_global_af = vrf.vrf_global.vrf_global_afs.VrfGlobalAf() + vrf_global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + vrf_global_af.table_policy = "allow-only-pfxlen-32" + vrf_global_af.enable = Empty() + vrf.vrf_global.vrf_global_afs.vrf_global_af.append(vrf_global_af) + # vrf neighbor + vrf_neighbor = vrf.vrf_neighbors.VrfNeighbor() + vrf_neighbor.neighbor_address = "140.1.1.2" + vrf_neighbor.neighbor_group_add_member = "ce" + # vrf neighbor af + vrf.vrf_neighbors.vrf_neighbor.append(vrf_neighbor) + four_byte_as.vrfs.vrf.append(vrf) + + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + bgp = xr_ipv4_bgp_cfg.Bgp() # create object + config_bgp(bgp) # add object configuration + + # create configuration on NETCONF device + crud.create(provider, bgp) + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-98-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-98-ydk.txt new file mode 100644 index 0000000..c1a11de --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-98-ydk.txt @@ -0,0 +1,69 @@ +router bgp 100 + bgp router-id 100.1.1.1 + address-family ipv4 unicast + redistribute connected + redistribute static + ! + address-family vpnv4 unicast + ! + neighbor-group ce + remote-as 200 + address-family ipv4 unicast + route-policy passall in + route-policy passall out + ! + ! + neighbor 80.1.1.2 + remote-as 100 + address-family ipv4 unicast + route-policy passall in + route-policy passall out + ! + address-family vpnv4 unicast + route-policy passall in + route-policy passall out + ! + ! + vrf 1 + rd 11:11 + address-family ipv4 unicast + redistribute static + ! + neighbor 110.1.1.2 + remote-as 300 + use neighbor-group ce + address-family ipv4 unicast + route-policy passall in + route-policy passall out + ! + ! + ! + vrf 2 + rd 12:12 + address-family ipv4 unicast + table-policy allow-only-pfxlen-32 + ! + neighbor 120.1.1.2 + use neighbor-group ce + ! + ! + vrf 3 + rd 13:13 + address-family ipv4 unicast + table-policy allow-only-pfxlen-32 + ! + neighbor 130.1.1.2 + use neighbor-group ce + ! + ! + vrf 4 + rd 14:14 + address-family ipv4 unicast + table-policy allow-only-pfxlen-32 + ! + neighbor 140.1.1.2 + use neighbor-group ce + ! + ! +! +end \ No newline at end of file diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-99-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-99-ydk.py new file mode 100755 index 0000000..c0ce27a --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-99-ydk.py @@ -0,0 +1,357 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Create configuration for model Cisco-IOS-XR-ipv4-bgp-cfg. + +usage: bgp-ipv4-cfg-PE1-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + + +from argparse import ArgumentParser +from urlparse import urlparse + +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_cfg \ + as xr_ipv4_bgp_cfg +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_datatypes \ + as xr_ipv4_bgp_datatypes +from ydk.types import Empty +import logging +import sys +#sys.path[1:1] = ['/nobackup/paquach/moonshine-dev/xrut/modules'] + +#import xrut +#routers = xrut.routers + +#print routers + +def config_bgp(bgp): + """Add config data to bgp object.""" + # global configuration + instance = bgp.Instance() + instance.instance_name = "default" + instance_as = instance.InstanceAs() + instance_as.as_ = 0 + four_byte_as = instance_as.FourByteAs() + four_byte_as.as_ = 100 + four_byte_as.bgp_running = Empty() + four_byte_as.default_vrf.global_.router_id = "10.1.1.1" + # global address family + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + connected_routes = global_af.ConnectedRoutes() + connected_routes.route_policy_name = "passall" + global_af.connected_routes = connected_routes + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + # global address family for vpnv4 + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.vp_nv4_unicast + global_af.vrf_all.label_mode.label_allocation_mode = "per-ce" + global_af.vrf_all.enable = Empty() + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + # global address family for ipv6 + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv6_unicast + connected_routes = global_af.ConnectedRoutes() + connected_routes.route_policy_name = "passall" + static_routes = global_af.StaticRoutes() + global_af.connected_routes = connected_routes + global_af.static_routes = static_routes + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + # global address family for vpnv6 + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.vp_nv6_unicast + global_af.vrf_all.label_mode.label_allocation_mode = "per-ce" + global_af.vrf_all.enable = Empty() + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + # global address family for l2vpn + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.l2vpnevpn + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + # global address family for link state link state + global_af = four_byte_as.default_vrf.global_.global_afs.GlobalAf() + global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.lsls + global_af.enable = Empty() + four_byte_as.default_vrf.global_.global_afs.global_af.append(global_af) + instance_as.four_byte_as.append(four_byte_as) + instance.instance_as.append(instance_as) + bgp.instance.append(instance) + + # configure neighbor group ce + neighbor_groups = four_byte_as.default_vrf.bgp_entity.neighbor_groups + neighbor_group = neighbor_groups.NeighborGroup() + neighbor_group.neighbor_group_name = "ce" + neighbor_group.create = Empty() + # remote AS + neighbor_group.remote_as.as_xx = 0 + neighbor_group.remote_as.as_yy = 200 + neighbor_groups.neighbor_group.append(neighbor_group) + # ipv4 unicast + neighbor_group_af = neighbor_group.neighbor_group_afs.NeighborGroupAf() + neighbor_group_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + neighbor_group_af.route_policy_in = "passall" + neighbor_group_af.route_policy_out = "passall" + neighbor_group_af.activate = Empty() + neighbor_group_afs = neighbor_group.neighbor_group_afs + neighbor_group_afs.neighbor_group_af.append(neighbor_group_af) + + # configure neighbor 3001::5 + neighbor = four_byte_as.default_vrf.bgp_entity.neighbors.Neighbor() + neighbor.neighbor_address = "3001::5" + neighbor.remote_as.as_xx = 0 + neighbor.remote_as.as_yy = 100 + # address family ipv4 + neighbor_af = neighbor.neighbor_afs.NeighborAf() + neighbor_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + neighbor_af.route_policy_in = "passall" + neighbor_af.route_policy_out = "passall" + neighbor_af.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af) + # address family ipv6 + neighbor_af2 = neighbor.neighbor_afs.NeighborAf() + neighbor_af2.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv6_unicast + neighbor_af2.route_policy_in = "passall" + neighbor_af2.route_policy_out = "passall" + neighbor_af2.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af2) + four_byte_as.default_vrf.bgp_entity.neighbors.neighbor.append(neighbor) + + # configure neighbor 200.1.1.2 + neighbor = four_byte_as.default_vrf.bgp_entity.neighbors.Neighbor() + neighbor.neighbor_address = "200.1.1.2" + neighbor.remote_as.as_xx = 0 + neighbor.remote_as.as_yy = 100 + # address family ipv4 + neighbor_af = neighbor.neighbor_afs.NeighborAf() + neighbor_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + neighbor_af.route_policy_in = "passall" + neighbor_af.route_policy_out = "passall" + neighbor_af.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af) + # address family vpnv4 + neighbor_af2 = neighbor.neighbor_afs.NeighborAf() + neighbor_af2.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.vp_nv4_unicast + neighbor_af2.route_policy_in = "passall" + neighbor_af2.route_policy_out = "passall" + neighbor_af2.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af2) + four_byte_as.default_vrf.bgp_entity.neighbors.neighbor.append(neighbor) + # address family link-state + neighbor_af = neighbor.neighbor_afs.NeighborAf() + neighbor_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.lsls + neighbor_af.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af) + four_byte_as.default_vrf.bgp_entity.neighbors.neighbor.append(neighbor) + + # configure neighbor 2001::208:28:2:1 + neighbor = four_byte_as.default_vrf.bgp_entity.neighbors.Neighbor() + neighbor.neighbor_address = "2001::208:28:2:1" + neighbor.remote_as.as_xx = 0 + neighbor.remote_as.as_yy = 1 + neighbor.update_source_interface = "Loopback0" + # address family ipv4 + neighbor_af = neighbor.neighbor_afs.NeighborAf() + neighbor_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv6_unicast + neighbor_af.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af) + # address family link-state + neighbor_af = neighbor.neighbor_afs.NeighborAf() + neighbor_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.lsls + neighbor_af.activate = Empty() + neighbor.neighbor_afs.neighbor_af.append(neighbor_af) + four_byte_as.default_vrf.bgp_entity.neighbors.neighbor.append(neighbor) + + # VRF 1 + vrf = four_byte_as.vrfs.Vrf() + vrf.vrf_name = "1" + # vrf global + vrf.vrf_global.route_distinguisher.type = xr_ipv4_bgp_cfg.BgpRouteDistinguisherEnum.four_byte_as + vrf.vrf_global.route_distinguisher.as_xx = 0 + vrf.vrf_global.route_distinguisher.as_ = 1 + vrf.vrf_global.route_distinguisher.as_index = 1 + vrf.vrf_global.exists = Empty() + # vrf global af + vrf_global_af = vrf.vrf_global.vrf_global_afs.VrfGlobalAf() + vrf_global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + static_routes = vrf_global_af.StaticRoutes() + vrf_global_af.static_routes = static_routes + vrf_global_af.enable = Empty() + vrf.vrf_global.vrf_global_afs.vrf_global_af.append(vrf_global_af) + # vrf neighbor + vrf_neighbor = vrf.vrf_neighbors.VrfNeighbor() + vrf_neighbor.neighbor_address = "10.1.1.2" + vrf_neighbor.neighbor_group_add_member = "ce" + # vrf neighbor af + vrf_neighbor_af = vrf_neighbor.vrf_neighbor_afs.VrfNeighborAf() + vrf_neighbor_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + vrf_neighbor_af.activate = Empty() + vrf_neighbor.vrf_neighbor_afs.vrf_neighbor_af.append(vrf_neighbor_af) + vrf.vrf_neighbors.vrf_neighbor.append(vrf_neighbor) + four_byte_as.vrfs.vrf.append(vrf) + + # VRF 2 + vrf = four_byte_as.vrfs.Vrf() + vrf.vrf_name = "2" + # vrf global + vrf.vrf_global.route_distinguisher.type = xr_ipv4_bgp_cfg.BgpRouteDistinguisherEnum.four_byte_as + vrf.vrf_global.route_distinguisher.as_xx = 0 + vrf.vrf_global.route_distinguisher.as_ = 2 + vrf.vrf_global.route_distinguisher.as_index = 2 + vrf.vrf_global.exists = Empty() + # vrf global af + vrf_global_af = vrf.vrf_global.vrf_global_afs.VrfGlobalAf() + vrf_global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + static_routes = vrf_global_af.StaticRoutes() + vrf_global_af.static_routes = static_routes + vrf_global_af.enable = Empty() + vrf.vrf_global.vrf_global_afs.vrf_global_af.append(vrf_global_af) + # vrf global af ipv6 + vrf_global_af = vrf.vrf_global.vrf_global_afs.VrfGlobalAf() + vrf_global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv6_unicast + static_routes = vrf_global_af.StaticRoutes() + vrf_global_af.static_routes = static_routes + vrf_global_af.enable = Empty() + vrf.vrf_global.vrf_global_afs.vrf_global_af.append(vrf_global_af) + # vrf neighbor + vrf_neighbor = vrf.vrf_neighbors.VrfNeighbor() + vrf_neighbor.neighbor_address = "2001::5" + vrf_neighbor.remote_as.as_xx = 0 + vrf_neighbor.remote_as.as_yy = 200 + # vrf neighbor af + vrf_neighbor_af = vrf_neighbor.vrf_neighbor_afs.VrfNeighborAf() + vrf_neighbor_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv6_unicast + vrf_neighbor_af.route_policy_in = "passall" + vrf_neighbor_af.route_policy_out = "passall" + vrf_neighbor_af.activate = Empty() + vrf_neighbor.vrf_neighbor_afs.vrf_neighbor_af.append(vrf_neighbor_af) + vrf.vrf_neighbors.vrf_neighbor.append(vrf_neighbor) + four_byte_as.vrfs.vrf.append(vrf) + + # VRF 3 + vrf = four_byte_as.vrfs.Vrf() + vrf.vrf_name = "3" + # vrf global + vrf.vrf_global.route_distinguisher.type = xr_ipv4_bgp_cfg.BgpRouteDistinguisherEnum.four_byte_as + vrf.vrf_global.route_distinguisher.as_xx = 0 + vrf.vrf_global.route_distinguisher.as_ = 3 + vrf.vrf_global.route_distinguisher.as_index = 3 + vrf.vrf_global.exists = Empty() + # vrf global af + vrf_global_af = vrf.vrf_global.vrf_global_afs.VrfGlobalAf() + vrf_global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + vrf_global_af.table_policy = "allow-only-pfxlen-32" + vrf_global_af.enable = Empty() + vrf.vrf_global.vrf_global_afs.vrf_global_af.append(vrf_global_af) + # vrf neighbor + vrf_neighbor = vrf.vrf_neighbors.VrfNeighbor() + vrf_neighbor.neighbor_address = "30.1.1.2" + vrf_neighbor.neighbor_group_add_member = "ce" + # vrf neighbor af + vrf.vrf_neighbors.vrf_neighbor.append(vrf_neighbor) + four_byte_as.vrfs.vrf.append(vrf) + + # VRF 4 + vrf = four_byte_as.vrfs.Vrf() + vrf.vrf_name = "4" + # vrf global + vrf.vrf_global.route_distinguisher.type = xr_ipv4_bgp_cfg.BgpRouteDistinguisherEnum.four_byte_as + vrf.vrf_global.route_distinguisher.as_xx = 0 + vrf.vrf_global.route_distinguisher.as_ = 4 + vrf.vrf_global.route_distinguisher.as_index = 4 + vrf.vrf_global.exists = Empty() + # vrf global af + vrf_global_af = vrf.vrf_global.vrf_global_afs.VrfGlobalAf() + vrf_global_af.af_name = xr_ipv4_bgp_datatypes.BgpAddressFamilyEnum.ipv4_unicast + vrf_global_af.table_policy = "allow-only-pfxlen-32" + vrf_global_af.enable = Empty() + vrf.vrf_global.vrf_global_afs.vrf_global_af.append(vrf_global_af) + # vrf neighbor + vrf_neighbor = vrf.vrf_neighbors.VrfNeighbor() + vrf_neighbor.neighbor_address = "40.1.1.2" + vrf_neighbor.neighbor_group_add_member = "ce" + # vrf neighbor af + vrf.vrf_neighbors.vrf_neighbor.append(vrf_neighbor) + four_byte_as.vrfs.vrf.append(vrf) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + bgp = xr_ipv4_bgp_cfg.Bgp() # create object + config_bgp(bgp) # add object configuration + + # create configuration on NETCONF device + crud.create(provider, bgp) + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-99-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-99-ydk.txt new file mode 100644 index 0000000..fb7d244 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-create-xr-ipv4-bgp-cfg-99-ydk.txt @@ -0,0 +1,109 @@ +router bgp 100 + bgp router-id 10.1.1.1 + address-family ipv4 unicast + redistribute connected route-policy passall + ! + address-family vpnv4 unicast + vrf all + label mode per-ce + ! + ! + address-family ipv6 unicast + redistribute connected route-policy passall + redistribute static + ! + address-family vpnv6 unicast + vrf all + label mode per-ce + ! + ! + address-family l2vpn evpn + ! + address-family link-state link-state + ! + neighbor-group ce + remote-as 200 + address-family ipv4 unicast + route-policy passall in + route-policy passall out + ! + ! + neighbor 3001::5 + remote-as 100 + address-family ipv4 unicast + route-policy passall in + route-policy passall out + ! + address-family ipv6 unicast + route-policy passall in + route-policy passall out + ! + ! + neighbor 200.1.1.2 + remote-as 100 + address-family ipv4 unicast + route-policy passall in + route-policy passall out + ! + address-family vpnv4 unicast + route-policy passall in + route-policy passall out + ! + address-family link-state link-state + ! + ! + neighbor 2001::108:28:2:1 + remote-as 1 + update-source Loopback0 + address-family ipv6 unicast + ! + address-family link-state link-state + ! + ! + vrf 1 + rd 1:1 + address-family ipv4 unicast + redistribute static + ! + neighbor 10.1.1.2 + use neighbor-group ce + address-family ipv4 unicast + ! + ! + ! + vrf 2 + rd 2:2 + address-family ipv4 unicast + redistribute static + ! + address-family ipv6 unicast + redistribute static + ! + neighbor 2001::5 + remote-as 200 + address-family ipv6 unicast + route-policy passall in + route-policy passall out + ! + ! + ! + vrf 3 + rd 3:3 + address-family ipv4 unicast + table-policy allow-only-pfxlen-32 + ! + neighbor 30.1.1.2 + use neighbor-group ce + ! + ! + vrf 4 + rd 4:4 + address-family ipv4 unicast + table-policy allow-only-pfxlen-32 + ! + neighbor 40.1.1.2 + use neighbor-group ce + ! + ! +! +end \ No newline at end of file diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-read-xr-ipv4-bgp-cfg-20-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-read-xr-ipv4-bgp-cfg-20-ydk.py new file mode 100755 index 0000000..9ffb3fb --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-cfg/nc-read-xr-ipv4-bgp-cfg-20-ydk.py @@ -0,0 +1,304 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Read all data for model Cisco-IOS-XR-ipv4-bgp-cfg. + +Outputs is to emulate the CLI show command + +usage: show-config-bgp-ipv4-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_cfg \ + as xr_ipv4_bgp_cfg +from datetime import timedelta +import logging + +def get_name(enum): + #interpret enum + return { + 0 : "IPv4 unicast", + 1 : "IPv4 multicast", + 2 : "IPv4 labeled-unicast", + 3 : "IPv4 tunnel", + 4 : "VPNv4 unicast", + 5 : "IPv6 unicast", + 6 : "IPv6 multicast", + 7 : "IPv6 labeled-unicast", + 8 : "VPNv6 unicast", + 9 : "IPv4 MDT", + 10 : "L2VPN VPLS-VPWS", + 11 : "IPv4 rt-filter", + 12 : "IPv4 MVPN", + 13 : "IPv6 MVPN", + 14 : "L2VPN EVPN", + 15 : "Link-state link-state" + }.get(enum, "Error") + +def process_bgp(bgp): + """Process data in bgp object.""" + spacing = 0 + + if bgp.instance: + show_bgp_adj = str() + else: + show_bgp_adj = "No BGP instances found" + + # iterate over all instances + for instance in bgp.instance: + for instance_as in instance.instance_as: + for four_byte_as in instance_as.four_byte_as: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "bgp router " + str(four_byte_as.as_) + show_bgp_adj = show_bgp_adj + '\n' + (' ' * (spacing+1)) + \ + "bgp router-id " + \ + str(four_byte_as.default_vrf.global_.router_id) + + # iterate over all global af + for global_af in four_byte_as.default_vrf.global_.global_afs.global_af: + spacing += 1 + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "address family " + get_name(global_af.af_name.value) + + # check for interior configs + spacing += 1 + if global_af.connected_routes: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "redistribute connected " + if global_af.connected_routes.route_policy_name: + show_bgp_adj = show_bgp_adj + "route policy " + \ + global_af.connected_routes.route_policy_name + if global_af.static_routes: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "redistribute static " + if global_af.static_routes.route_policy_name: + show_bgp_adj = show_bgp_adj + "route policy " + \ + global_af.static_routes.route_policy_name + + if global_af.vrf_all.label_mode.label_allocation_mode: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "vrf all" + show_bgp_adj = show_bgp_adj + '\n' + (' ' * (spacing+1)) + \ + "label mode " + \ + str(global_af.vrf_all.label_mode.label_allocation_mode) + show_bgp_adj = show_bgp_adj + '\n' + \ + (' ' * spacing) + '!' + spacing -= 1 + show_bgp_adj = show_bgp_adj + '\n' + \ + (' ' * spacing) + '!' + spacing -= 1 + + # iterate over neighbor groups + for neighbor_group in four_byte_as.default_vrf.bgp_entity.neighbor_groups.neighbor_group: + spacing += 1 + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "neighbor-group " + neighbor_group.neighbor_group_name + spacing += 1 + if neighbor_group.remote_as: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "remote-as " + str(neighbor_group.remote_as.as_xx) + \ + ":" + str(neighbor_group.remote_as.as_yy) + if neighbor_group.update_source_interface: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "update-source " + neighbor_group.update_source_interface + + # show the neighbor group AF information + for neighbor_group_af in neighbor_group.neighbor_group_afs.neighbor_group_af: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "address-family " + get_name(neighbor_group_af.af_name.value) + if neighbor_group_af.route_reflector_client: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * (spacing+1)) + \ + "route-reflector-client" + if neighbor_group_af.route_policy_in: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * (spacing+1)) + \ + "route-policy " + neighbor_group_af.route_policy_in + \ + " in" + if neighbor_group_af.route_policy_out: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * (spacing+1)) + \ + "route-policy " + neighbor_group_af.route_policy_out + \ + " out" + show_bgp_adj = show_bgp_adj + '\n' + \ + (' ' * spacing) + '!' + spacing -= 1 + show_bgp_adj = show_bgp_adj + '\n' + \ + (' ' * spacing) + '!' + spacing -= 1 + + # iterate over neighbors + for neighbor in four_byte_as.default_vrf.bgp_entity.neighbors.neighbor: + spacing += 1 + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "neighbor " + neighbor.neighbor_address + spacing += 1 + if not(neighbor.remote_as.as_xx == None): + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "remote-as " + str(neighbor.remote_as.as_xx) + \ + ":" + str(neighbor.remote_as.as_yy) + if neighbor.update_source_interface: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "update-source " + neighbor.update_source_interface + if neighbor.neighbor_group_add_member: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "use neighbor-group " + neighbor.neighbor_group_add_member + + # check for neighbor AF + for neighbor_af in neighbor.neighbor_afs.neighbor_af: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "address-family " + get_name(neighbor_af.af_name.value) + if neighbor_af.route_policy_in: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * (spacing+1)) + \ + "route-policy " + neighbor_af.route_policy_in + \ + " in" + if neighbor_af.route_policy_out: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * (spacing+1)) + \ + "route-policy " + neighbor_af.route_policy_out + \ + " out" + show_bgp_adj = show_bgp_adj + '\n' + \ + (' ' * spacing) + '!' + spacing -= 1 + show_bgp_adj = show_bgp_adj + '\n' + \ + (' ' * spacing) + '!' + spacing -= 1 + + # iterate over bgp vrf + for vrf in four_byte_as.vrfs.vrf: + spacing += 1 + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "vrf " + vrf.vrf_name + spacing += 1 + if vrf.vrf_global.route_distinguisher: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "rd " + str(vrf.vrf_global.route_distinguisher.as_xx) + \ + "." + str(vrf.vrf_global.route_distinguisher.as_) + \ + ":" + str(vrf.vrf_global.route_distinguisher.as_index) + + # check for vrf AF + for vrf_global_af in vrf.vrf_global.vrf_global_afs.vrf_global_af: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "address-family " + get_name(vrf_global_af.af_name.value) + spacing += 1 + if vrf_global_af.connected_routes: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "redistribute connected " + if vrf_global_af.connected_routes.route_policy_name: + show_bgp_adj = show_bgp_adj + "route policy " + \ + vrf_global_af.connected_routes.route_policy_name + if vrf_global_af.static_routes: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "redistribute static " + if vrf_global_af.static_routes.route_policy_name: + show_bgp_adj = show_bgp_adj + "route policy " + \ + vrf_global_af.static_routes.route_policy_name + if vrf_global_af.table_policy: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "table-policy " + vrf_global_af.table_policy + spacing -= 1 + show_bgp_adj = show_bgp_adj + '\n' + \ + (' ' * spacing) + '!' + + # Check for vrf neighbors + for vrf_neighbor in vrf.vrf_neighbors.vrf_neighbor: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "neighbor " + vrf_neighbor.neighbor_address + spacing += 1 + if vrf_neighbor.neighbor_group_add_member: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "use neighbor-group " + vrf_neighbor.neighbor_group_add_member + if not (vrf_neighbor.remote_as.as_xx == None): + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "remote-as " + str(vrf_neighbor.remote_as.as_xx) + \ + ":" + str(vrf_neighbor.remote_as.as_yy) + + # check for bgp vrf neighbor's AF + for vrf_neighbor_af in vrf_neighbor.vrf_neighbor_afs.vrf_neighbor_af: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * spacing) + \ + "address-family " + get_name(vrf_neighbor_af.af_name.value) + spacing += 1 + if vrf_neighbor_af.route_policy_in: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * (spacing+1)) + \ + "route-policy " + vrf_neighbor_af.route_policy_in + \ + " in" + if vrf_neighbor_af.route_policy_out: + show_bgp_adj = show_bgp_adj + '\n' + (' ' * (spacing+1)) + \ + "route-policy " + vrf_neighbor_af.route_policy_out + \ + " out" + spacing -= 1 + show_bgp_adj = show_bgp_adj + '\n' + \ + (' ' * spacing) + '!' + spacing -= 1 + show_bgp_adj = show_bgp_adj + '\n' + \ + (' ' * spacing) + '!' + + spacing -= 1 + show_bgp_adj = show_bgp_adj + '\n' + \ + (' ' * spacing) + '!' + spacing -= 1 + show_bgp_adj = show_bgp_adj + '\n' + \ + (' ' * spacing) + '!' + # return formatted string + return(show_bgp_adj) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + bgp = xr_ipv4_bgp_cfg.Bgp() # create object + + # read data from NETCONF device + bgp = crud.read(provider, bgp) + print(process_bgp(bgp)) # process object data + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-20-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-20-ydk.py new file mode 100755 index 0000000..6eae4c1 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-20-ydk.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Read all data for model Cisco-IOS-XR-ipv4-bgp-oper. + +usage: nc-read-xr-ipv4-bgp-oper-20-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_oper \ + as xr_ipv4_bgp_oper +from datetime import timedelta +import logging +import time +start_time = time.time() +def get_nbr_state(enum): + #interpret enum + return { + 0 : "DontCare", + 1 : "Idle", + 2 : "Connect", + 3 : "Active", + 4 : "OpenSent", + 5 : "OpenConfirm", + 6 : "Established", + 7 : "Closing", + 8 : "ClosingSync" + }.get(enum, "Error") + +def get_nsr_state(enum): + return { + 0 : "None", + 1 : "OPER_DOWN", + 2 : "TCP Sync in progress", + 3 : "TCP Sync Phase 2", + 4 : "BGP Sync in progress", + 5 : "Neighbor Ready" + }.get(enum, "Error") + + +def process_bgp(bgp): + """Process data in bgp object.""" + # format string for bgp adjacency header + bgp_header = ("BGP Sessions:\n" + "Neighbor Vrf Spk AS " + "InQ OutQ NBRState NSRState") + + # format string for bgp adjacency row + bgp_row = ("{neighbor:<16} {vrf:<20} {spk:>3} {bgp_as:>5} " + "{inq:>5} {outq:>5} {nbr_state:<12} {nsr_state:<8}") + # format string for bgp adjacency trailer + bgp_trailer = "Total Neighbor count: {count}" + + if bgp.instances.instance: + show_bgp_adj = str() + else: + show_bgp_adj = "No BGP instances found" + count = 0 + + # iterate over all instances + for instance in bgp.instances.instance: + show_bgp_adj += bgp_header + # iterate over all neighbors + for neighbor in instance.instance_active.default_vrf.neighbors.neighbor: + count += 1 + # iterate over all adjacencies + vrf = neighbor.vrf_name + spk = neighbor.speaker_id + bgp_as = neighbor.remote_as + inq = neighbor.messages_queued_in + outq = neighbor.messages_queued_out + nbr_state = get_nbr_state(neighbor.connection_state.value) + nsr_state = get_nsr_state(neighbor.nsr_state.value) + + show_bgp_adj += ("\n" + + bgp_row.format(neighbor=neighbor.neighbor_address, + vrf=vrf, + spk=spk, + bgp_as=bgp_as, + inq=inq, + outq=outq, + nbr_state=nbr_state, + nsr_state=nsr_state)) + + for vrf in instance.instance_active.vrfs.vrf: + for neighbor in vrf.neighbors.neighbor: + count += 1 + # iterate over all adjacencies + vrf = neighbor.vrf_name + spk = neighbor.speaker_id + bgp_as = neighbor.remote_as + inq = neighbor.messages_queued_in + outq = neighbor.messages_queued_out + nbr_state = get_nbr_state(neighbor.connection_state.value) + nsr_state = get_nsr_state(neighbor.nsr_state.value) + + show_bgp_adj += ("\n" + + bgp_row.format(neighbor=neighbor.neighbor_address, + vrf=vrf, + spk=spk, + bgp_as=bgp_as, + inq=inq, + outq=outq, + nbr_state=nbr_state, + nsr_state=nsr_state)) + + show_bgp_adj += ("\n\n" + + bgp_trailer.format(count=count)) + return(show_bgp_adj) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + bgp = xr_ipv4_bgp_oper.Bgp() # create object + + # read data from NETCONF device + bgp = crud.read(provider, bgp) + print(process_bgp(bgp)) # process object data + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-20-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-20-ydk.txt new file mode 100644 index 0000000..0f06d58 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-20-ydk.txt @@ -0,0 +1,9 @@ +BGP Sessions: +Neighbor Vrf Spk AS InQ OutQ NBRState NSRState +80.1.1.2 default 0 100 0 0 Established None +110.1.1.2 1 0 300 0 0 Idle None +120.1.1.2 2 0 200 0 0 Idle None +130.1.1.2 3 0 200 0 0 Idle None +140.1.1.2 4 0 200 0 0 Idle None + +Total Neighbor count: 5 diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-21-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-21-ydk.py new file mode 100755 index 0000000..5b9d4e0 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-21-ydk.py @@ -0,0 +1,203 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Read all data for model Cisco-IOS-XR-ipv4-bgp-oper. + +usage: nc-read-xr-ipv4-bgp-oper-21-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_oper \ + as xr_ipv4_bgp_oper +from datetime import timedelta +import logging + +def get_status(path): + status = str() + if path.is_aggregation_suppressed: + status += 's' + if path.is_path_damped: + status += 'd' + if path.is_path_history_held: + status += 'h' + if path.is_path_valid: + status += '*' + if path.is_best_path: + status += '>' + if path.is_internal_path: + status += 'i' + if path.rib_failed: + status += 'r' + if path.is_path_stale: + status += 'S' + if path.is_path_nexthop_discarded: + status += 'N' + return status + +def process_bgp(bgp): + """Process data in bgp object.""" + # format string for bgp adjacency header + bgp_table_header = ("\n Network Next Hop Metric LocPrf " + "Weight Path") + + # format string for bgp adjacency row + bgp_row = ("{status:<3}{network:<16} {nexthop:<20} {metric:>3} {locprf:>9} " + "{weight:>8} {path:<5}") + + bgp_header = ("BGP router identifier {routerid}, local AS number {local_as}\n" + "BGP generic scan interval {gscan} secs\n" + "Non-stop routing is {nsr}\n" + "BGP table state: {tstate}\n" + "Table ID: {tid} RD version: {rd}\n" + "BGP main routing table version {rt_version}\n" + "BGP NSR Initial initsync version {nsr_ver} ({nsr_conv})\n" + "BGP NSR/ISSU Sync-Group versions {sg_ver}\n" + "BGP scan interval {bgp_scan} secs\n" + "\n" + "Status codes: s suppressed, d damped, h history, * valid, > best\n" + "i - internal, r RIB-failure, S stale, N Nexthop-discard\n" + "Origin codes: i - IGP, e - EGP, ? - incomplete" + + ) + # format string for bgp adjacency trailer + show_bgp_table = str() + + if bgp.instances.instance: + show_bgp_adj = str() + else: + show_bgp_adj = "No BGP instances found" + count = 0 + + # iterate over all instances + for instance in bgp.instances.instance: + # Show global configs + global_process_info = instance.instance_active.default_vrf.global_process_info + routerid = global_process_info.vrf.router_id + local_as = global_process_info.global_.local_as + gscan = global_process_info.global_.generic_scan_period + nsr = "enabled" if global_process_info.vrf.is_nsr else "disabled" + + # Show AF configs + af = instance.instance_active.default_vrf.afs.af[0] + vrf = af.global_af_process_info.vrf + tstate = "Active" if vrf.table_is_active else "Inactive" + tid = hex(vrf.table_id) + rd = vrf.rd_version + rt_version = vrf.table_version + nsr_ver = vrf.nsr_conv_version + nsr_conv = "Reached" if vrf.nsr_is_conv else "Not Reached" + sg_ver = str(af.global_af_process_info.global_.syncgrp_version[0].entry) + \ + '/' + str(af.global_af_process_info.global_.syncgrp_version[1].entry) + bgp_scan = af.global_af_process_info.global_.scanner_period + + # Show the table information + for path in af.path_table.path: + path_information = path.path_information + hop_ipv4_addr = path_information.next_hop.ipv4_address + ip = path.network + '/' + str(path.prefix_length) + metric = path_information.aigp_metric + locprf = path.attributes_after_policy_in.common_attributes.local_preference + weight = path_information.path_weight + status = get_status(path_information) + path_ = str(path.attributes_after_policy_in.common_attributes.neighbor_as) + if path.attributes_after_policy_in.common_attributes.origin == 0: + path_ += " i" + elif path.attributes_after_policy_in.common_attributes.origin == 1: + path_ += " e" + elif path.attributes_after_policy_in.common_attributes.origin == 2: + path_ += " ?" + show_bgp_table += ('\n' + + bgp_row.format(status = status, + network=ip, + nexthop=hop_ipv4_addr, + metric=metric, + locprf=locprf, + weight=weight, + path=path_ + )) + + show_bgp_adj += ('\n' + + bgp_header.format(routerid=routerid, + local_as=local_as, + gscan=gscan, + nsr=nsr, + tstate=tstate, + tid=tid, + rd=rd, + rt_version=rt_version, + nsr_ver=nsr_ver, + nsr_conv=nsr_conv, + sg_ver=sg_ver, + bgp_scan=bgp_scan + )) + + show_bgp_adj += bgp_table_header + show_bgp_adj += show_bgp_table + show_bgp_adj += ("\n\n") + + return(show_bgp_adj) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + bgp = xr_ipv4_bgp_oper.Bgp() # create object + + # read data from NETCONF device + bgp = crud.read(provider, bgp) + print(process_bgp(bgp)) # process object data + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-21-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-21-ydk.txt new file mode 100644 index 0000000..c0c3d30 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-21-ydk.txt @@ -0,0 +1,22 @@ +BGP router identifier 110.1.1.1, local AS number 100 +BGP generic scan interval 60 secs +Non-stop routing is enabled +BGP table state: Active +Table ID: 0xe0000000 RD version: 10 +BGP main routing table version 10 +BGP NSR Initial initsync version 2 (Reached) +BGP NSR/ISSU Sync-Group versions 0/0 +BGP scan interval 60 secs + +Status codes: s suppressed, d damped, h history, * valid, > best +i - internal, r RIB-failure, S stale, N Nexthop-discard +Origin codes: i - IGP, e - EGP, ? - incomplete + Network Next Hop Metric LocPrf Weight Path +*>i5.5.5.0/24 200.1.1.1 0 100 0 0 ? +*>i14.14.14.0/24 200.1.1.1 0 100 0 0 ? +*>i16.16.16.0/24 200.1.1.1 0 100 0 0 ? +*> 19.19.19.0/24 0.0.0.0 0 100 32768 0 ? +*> 80.1.1.0/24 0.0.0.0 0 100 32768 0 ? +*>i192.168.0.5/32 200.1.1.1 0 100 0 0 ? +*> 192.168.0.7/32 0.0.0.0 0 100 32768 0 ? +*>i200.1.1.0/24 200.1.1.1 0 100 0 0 ? \ No newline at end of file diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-22-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-22-ydk.py new file mode 100755 index 0000000..608874c --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-22-ydk.py @@ -0,0 +1,207 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Read all data for model Cisco-IOS-XR-ipv4-bgp-oper. + +usage: nc-read-xr-ipv4-bgp-oper-22-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_oper \ + as xr_ipv4_bgp_oper +from datetime import timedelta +import logging + +def get_status(path): + status = str() + if path.is_aggregation_suppressed: + status += 's' + if path.is_path_damped: + status += 'd' + if path.is_path_history_held: + status += 'h' + if path.is_path_valid: + status += '*' + if path.is_best_path: + status += '>' + if path.is_internal_path: + status += 'i' + if path.rib_failed: + status += 'r' + if path.is_path_stale: + status += 'S' + if path.is_path_nexthop_discarded: + status += 'N' + return status + +def process_bgp(bgp): + """Process data in bgp object.""" + # format string for bgp adjacency header + bgp_table_header = ("\n Network Next Hop Metric LocPrf " + "Weight Path") + + # format string for bgp adjacency row + bgp_row = ("{status:<3}{network:<16} {nexthop:<20} {metric:>3} {locprf:>9} " + "{weight:>8} {path:<5}") + + bgp_header = ("BGP router identifier {routerid}, local AS number {local_as}\n" + "BGP generic scan interval {gscan} secs\n" + "Non-stop routing is {nsr}\n" + "BGP table state: {tstate}\n" + "Table ID: {tid} RD version: {rd}\n" + "BGP main routing table version {rt_version}\n" + "BGP NSR Initial initsync version {nsr_ver} ({nsr_conv})\n" + "BGP NSR/ISSU Sync-Group versions {sg_ver}\n" + "BGP scan interval {bgp_scan} secs\n" + "\n" + "Status codes: s suppressed, d damped, h history, * valid, > best\n" + "i - internal, r RIB-failure, S stale, N Nexthop-discard\n" + "Origin codes: i - IGP, e - EGP, ? - incomplete\n" +# "Route Distinguisher: {rd}:{route_distinguisher}" + ) + + + if bgp.instances.instance: + show_bgp_adj = str() + show_bgp_table = str() + else: + show_bgp_adj = "No BGP instances found" + count = 0 + + # iterate over all instances + for instance in bgp.instances.instance: + # Show global configs + global_process_info = instance.instance_active.default_vrf.global_process_info + routerid = global_process_info.vrf.router_id + local_as = global_process_info.global_.local_as + gscan = global_process_info.global_.generic_scan_period + nsr = "enabled" if global_process_info.vrf.is_nsr else "disabled" + + af =instance.instance_active.default_vrf.afs.af[1] + vrf = af.global_af_process_info.vrf + tstate = "Active" if vrf.table_is_active else "Inactive" + tid = hex(vrf.table_id) + rd = vrf.rd_version + rt_version = vrf.table_version + nsr_ver = vrf.nsr_conv_version + nsr_conv = "Reached" if vrf.nsr_is_conv else "Not Reached" + sg_ver = str(af.global_af_process_info.global_.syncgrp_version[0].entry) + \ + '/' + str(af.global_af_process_info.global_.syncgrp_version[1].entry) + bgp_scan = af.global_af_process_info.global_.scanner_period + + previous_rd = '' + for path in af.path_table.path: + if path.route_distinguisher != previous_rd: + rd2 = int(path.route_distinguisher[-4:],16) + rd1 = int(path.route_distinguisher[-8:-4],16) + show_bgp_table += "\nRoute Distinguisher: {rd1}:{rd2}".format(rd1=rd1, + rd2=rd2) + previous_rd = path.route_distinguisher + path_information = path.path_information + hop_ipv4_addr = path_information.next_hop.ipv4_address + ip = path.network + '/' + str(path.prefix_length) + metric = path_information.aigp_metric + locprf = path.attributes_after_policy_in.common_attributes.local_preference + weight = path_information.path_weight + status = get_status(path_information) + path_id = str(path.attributes_after_policy_in.common_attributes.neighbor_as) + if path.attributes_after_policy_in.common_attributes.origin == 0: + path_id += " i" + elif path.attributes_after_policy_in.common_attributes.origin == 1: + path_id += " e" + elif path.attributes_after_policy_in.common_attributes.origin == 2: + path_id += " ?" + show_bgp_table += ('\n' + + bgp_row.format(status = status, + network=ip, + nexthop=hop_ipv4_addr, + metric=metric, + locprf=locprf, + weight=weight, + path=path_id + )) + show_bgp_adj += ('\n' + + bgp_header.format(routerid=routerid, + local_as=local_as, + gscan=gscan, + nsr=nsr, + tstate=tstate, + tid=tid, + rd=rd, + rt_version=rt_version, + nsr_ver=nsr_ver, + nsr_conv=nsr_conv, + sg_ver=sg_ver, + bgp_scan=bgp_scan, + )) + + show_bgp_adj += bgp_table_header + show_bgp_adj += show_bgp_table + show_bgp_adj += ("\n\n") + return(show_bgp_adj) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + bgp = xr_ipv4_bgp_oper.Bgp() # create object + + # read data from NETCONF device + bgp = crud.read(provider, bgp) + print(process_bgp(bgp)) # process object data + + provider.close() + # print (time.time() - start) + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-22-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-22-ydk.txt new file mode 100644 index 0000000..db9046c --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-22-ydk.txt @@ -0,0 +1,77 @@ +BGP router identifier 110.1.1.1, local AS number 100 +BGP generic scan interval 60 secs +Non-stop routing is enabled +BGP table state: Active +Table ID: 0x0 RD version: 0 +BGP main routing table version 76 +BGP NSR Initial initsync version 72 (Reached) +BGP NSR/ISSU Sync-Group versions 0/0 +BGP scan interval 60 secs + +Status codes: s suppressed, d damped, h history, * valid, > best +i - internal, r RIB-failure, S stale, N Nexthop-discard +Origin codes: i - IGP, e - EGP, ? - incomplete + + Network Next Hop Metric LocPrf Weight Path +Route Distinguisher: 1:1 +*>i10.1.1.0/24 200.1.1.1 0 100 0 200 ? +*>i10.10.10.0/24 200.1.1.1 0 100 0 200 ? +*>i13.13.13.0/24 200.1.1.1 0 100 0 200 ? +*>i77.11.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.11.78.0/24 200.1.1.1 0 100 0 200 ? +*>i77.12.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.13.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.14.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.15.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.16.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.17.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.18.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.19.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.20.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.21.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.22.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.23.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.24.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.25.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.26.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.27.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.28.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.29.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.30.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.31.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.32.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.33.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.77.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.79.77.0/24 200.1.1.1 0 100 0 200 ? +*>i192.168.0.3/32 200.1.1.1 0 100 0 200 ? +Route Distinguisher: 11:11 +*>i10.1.1.0/24 200.1.1.1 0 100 0 200 ? +*>i10.10.10.0/24 200.1.1.1 0 100 0 200 ? +*>i13.13.13.0/24 200.1.1.1 0 100 0 200 ? +*>i77.11.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.11.78.0/24 200.1.1.1 0 100 0 200 ? +*>i77.12.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.13.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.14.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.15.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.16.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.17.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.18.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.19.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.20.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.21.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.22.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.23.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.24.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.25.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.26.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.27.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.28.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.29.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.30.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.31.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.32.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.33.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.77.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.79.77.0/24 200.1.1.1 0 100 0 200 ? +*>i192.168.0.3/32 200.1.1.1 0 100 0 200 ? diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-23-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-23-ydk.py new file mode 100755 index 0000000..4f12327 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-23-ydk.py @@ -0,0 +1,267 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Read all data for model Cisco-IOS-XR-ipv4-bgp-oper. + +usage: nc-read-xr-ipv4-bgp-oper-23-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_oper \ + as xr_ipv4_bgp_oper +import logging + +def get_name(enum): + return { + 0 : "IPv4 unicast", + 1 : "IPv4 multicast", + 2 : "IPv4 labeled-unicast", + 3 : "IPv4 tunnel", + 4 : "VPNv4 unicast", + 5 : "IPv6 unicast", + 6 : "IPv6 multicast", + 7 : "IPv6 labeled-unicast", + 8 : "VPNv6 unicast", + 9 : "IPv4 MDT", + 10 : "L2VPN VPLS-VPWS", + 11 : "IPv4 rt-filter", + 12 : "IPv4 MVPN", + 13 : "IPv6 MVPN", + 14 : "L2VPN EVPN", + 15 : "Link-state link-state" + }.get(enum, "Error") + +def int_to_time(time_int): + return "%02d:%02d:%02d" % (time_int/60/60, (time_int/60)%60, time_int%60) + +def get_status(next_hop_af): + ''' + Use the binary representation of the status + to find the actual status of device + ''' + status = str() + binary = bin(next_hop_af.nexthop_gateway_info[0].nexthop_status) + binary = '00' + binary[2:] + stat_list = [binary[i:i+2] for i in range(0, len(binary), 2)] + stat_list = list(reversed(stat_list)) + + if stat_list[0] == '01': + status += "[R]" + elif stat_list[0] == '10': + status += "[UR]" + if stat_list[1] == '01': + status += "[C]" + elif stat_list[1] == '10': + status += "[NC]" + if stat_list[2] == '01': + status += "[L]" + elif stat_list[2] == '10': + status += "[NL]" + if stat_list[3][1] == '1': + status += "[PR]" + elif stat_list[3][0] == '1': + status += "[I]" + return status + +def get_rib_event(enum): + return { + 0 : "(Cri)", + 1 : "(Non-Cri)", + 2 : "(Reg)" + }.get(enum, "Error") + + +def process_bgp(bgp): + """Process data in bgp object.""" + # format string for bgp adjacency header + bgp_table_header = ("\nNext Hop Status Metric " + "Tbl-ID Notf LastRIBEvent RefCount") + + # format string for bgp adjacency row + bgp_row = ("{next_hop:<16} {status:<19} {metric:<6} {tblid:<12} " + "{notf:<4} {ribtime:<8}{ribevent:<9} {refcount:<8}") + + bgp_header = ("Total Nexthop Processing\n" + " Time Spent: {tot_time} seconds\n" + "\n" + "Maximum Nexthop Processing\n" + " Received: {rcvd_time} \n" + " Bestpaths Deleted: {del_bp}\n" + " Bestpaths Changed: {chg_bp}\n" + " Time Spent: {bp_time} seconds\n" + "\n" + "Last Notification Processing\n" + " Received: {rcvd_ln}\n" + " Time Spent: {ln_time} seconds" + ) + + af_info = ("\n" + "Gateway Address Family: {af}\n" + "Table ID: {tid}\n" + "Nexthop Count: {nh_count}\n" + "Critical Trigger Delay: {ctd} ms\n" + "Non-critical Trigger Delay: {nctd} ms\n" + "\n" + "Nexthop Version: {nh_ver}, RIB version: {rib_ver}\n" + "EPE Table Version: {epet_ver}, EPE Label version: {epel_ver}\n" + "EPE Downloaded Version: {eped_ver}, EPE Standby version: {epes_ver}\n" + "\n" + "Status Codes: R/UR Reachable/Unreachable\n" + " C/NC Connected/Not-connected\n" + " L/NL Local/Non-local\n" + " PR Pending Registration\n" + " I Invalid (Policy drop)\n" + ) + + if bgp.instances.instance: + show_bgp_adj = str() + show_row = str() + else: + show_bgp_adj = "No BGP instances found" + count = 0 + + # iterate over all instances + # time is in ms + for instance in bgp.instances.instance: + default_vrf = instance.instance_active.default_vrf + tot_time = default_vrf.next_hop_vrf.total_processing_time/1000.0 + rcvd_time = int_to_time(default_vrf.next_hop_vrf.max_proc_notification_time) + del_bp = default_vrf.next_hop_vrf.max_notification_bestpath_deletes + chg_bp = default_vrf.next_hop_vrf.max_notification_bestpath_changes + bp_time = default_vrf.next_hop_vrf.maximum_processing_time/1000.0 + rcvd_ln = int_to_time(default_vrf.next_hop_vrf.last_notificationication_time) + ln_time = default_vrf.next_hop_vrf.last_notification_processing_time/1000.0 + show_bgp_adj += ('\n' + + bgp_header.format(tot_time=tot_time, + rcvd_time=rcvd_time, + del_bp=del_bp, + chg_bp=chg_bp, + bp_time=bp_time, + rcvd_ln=rcvd_ln, + ln_time=ln_time + )) + + # Show af information + for af in default_vrf.afs.af: + for next_hop_address_family in af.next_hop_address_families.next_hop_address_family: + af = get_name(next_hop_address_family.next_hop_af_name.value) + tid = hex(next_hop_address_family.next_hop_af_vrf_af.nh_table_id) + nh_count = next_hop_address_family.next_hop_af_vrf_af.total_nexthops + ctd = next_hop_address_family.next_hop_af_vrf_af.critical_trigger_delay + nctd = next_hop_address_family.next_hop_af_vrf_af.non_critical_trigger_delay + nh_ver = next_hop_address_family.next_hop_af_vrf_af.nh_nexthop_version + rib_ver = next_hop_address_family.next_hop_af_vrf_af.nh_rib_version + epet_ver = next_hop_address_family.next_hop_af_vrf_af.epe_table_version + epel_ver = next_hop_address_family.next_hop_af_vrf_af.epe_label_version + eped_ver = next_hop_address_family.next_hop_af_vrf_af.epe_downloaded_version + epes_ver = next_hop_address_family.next_hop_af_vrf_af.epe_standby_version + + show_bgp_adj += ('\n' + + af_info.format(af=af, + tid=tid, + nh_count=nh_count, + ctd=ctd, + nctd=nctd, + nh_ver=nh_ver, + rib_ver=rib_ver, + epet_ver=epet_ver, + epel_ver=epel_ver, + eped_ver=eped_ver, + epes_ver=epes_ver + )) + + # Show next hop information + for next_hop_af in next_hop_address_family.next_hop_afs.next_hop_af: + next_hop = next_hop_af.next_hop_address + status = get_status(next_hop_af) + metric = next_hop_af.nexthop_gateway_info[0].nexthop_metric + tblid = hex(next_hop_af.nexthop_gateway_info[0].nexthop_tableid) + notf = str(next_hop_af.nexthop_gateway_info[0].critical_events) + \ + '/' + str(next_hop_af.nexthop_gateway_info[0].non_critical_events) + ribtime = int_to_time(next_hop_af.nexthop_gateway_info[0].last_event_since) + ribevent = get_rib_event(next_hop_af.nexthop_gateway_info[0].last_event_type.value) + refcount = str(next_hop_af.nexthop_reference_count) + \ + "/" + str(next_hop_af.nh_reference_count_total) + + show_row += ('\n' + + bgp_row.format(next_hop=next_hop, + status=status, + metric=metric, + tblid=tblid, + notf=notf, + ribtime=ribtime, + ribevent=ribevent, + refcount=refcount + )) + show_bgp_adj += bgp_table_header + show_bgp_adj += show_row + break + + # return formatted string + return(show_bgp_adj) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + bgp = xr_ipv4_bgp_oper.Bgp() # create object + + # read data from NETCONF device + bgp = crud.read(provider, bgp) + print(process_bgp(bgp)) # process object data + + provider.close() + # print (time.time() - start) + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-23-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-23-ydk.txt new file mode 100644 index 0000000..ba4bd3d --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-23-ydk.txt @@ -0,0 +1,32 @@ +Total Nexthop Processing + Time Spent: 0.0 seconds + +Maximum Nexthop Processing + Received: 00:00:00 + Bestpaths Deleted: 0 + Bestpaths Changed: 0 + Time Spent: 0.0 seconds + +Last Notification Processing + Received: 03:44:52 + Time Spent: 0.0 seconds + +Gateway Address Family: IPv4 unicast +Table ID: 0xe0000000 +Nexthop Count: 3 +Critical Trigger Delay: 3000 ms +Non-critical Trigger Delay: 10000 ms + +Nexthop Version: 1, RIB version: 1 +EPE Table Version: 1, EPE Label version: 1 +EPE Downloaded Version: 1, EPE Standby version: 0 + +Status Codes: R/UR Reachable/Unreachable + C/NC Connected/Not-connected + L/NL Local/Non-local + PR Pending Registration + I Invalid (Policy drop) + +Next Hop Status Metric Tbl-ID Notf LastRIBEvent RefCount +80.1.1.2 [R][C][NL] 0 0xe0000000 1/0 03:44:49(Cri) 0/3 +200.1.1.1 [R][NC][NL] 2 0xe0000000 0/0 03:42:54(Reg) 5/67 \ No newline at end of file diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-24-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-24-ydk.py new file mode 100755 index 0000000..09a5844 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-24-ydk.py @@ -0,0 +1,234 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Read all data for model Cisco-IOS-XR-ipv4-bgp-oper. +Reads using a generic model and command line arguments. + +usage: nc-read-xr-ipv4-bgp-oper-24-ydk.py [-h] [-v] device name + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + name VRF name + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_bgp_oper \ + as xr_ipv4_bgp_oper +from datetime import timedelta +import logging + +def get_rd(rd): + rd = rd[-8:] + rd = str(int(rd[:4], 16)) + ":" + \ + str(int(rd[-4:], 16)) + return rd + +def get_status(path): + status = str() + if path.is_aggregation_suppressed: + status += 's' + if path.is_path_damped: + status += 'd' + if path.is_path_history_held: + status += 'h' + if path.is_path_valid: + status += '*' + if path.is_best_path: + status += '>' + if path.is_internal_path: + status += 'i' + if path.rib_failed: + status += 'r' + if path.is_path_stale: + status += 'S' + if path.is_path_nexthop_discarded: + status += 'N' + return status + +def process_bgp(bgp, name): + """Process data in bgp object.""" + # format string for bgp adjacency header + bgp_table_header = ("\n Network Next Hop Metric LocPrf " + "Weight Path" + "\nRoute Distinguisher: {rd}") + + # format string for bgp adjacency row + bgp_row = ("{status:<3}{network:<16} {nexthop:<20} {metric:>3} {locprf:>9} " + "{weight:>8} {path:<5}") + + bgp_header = ("BGP VRF {vrf_name}, state: {vrf_state}\n" + "BGP Route Distinguisher {rd}\n" + "VRF ID: {vrf_id}\n" + "BGP router identifier {routerid}, local AS number {local_as}\n" + "Non-stop routing is {nsr}\n" + "BGP table state: {tstate}\n" + "Table ID: {tid} RD version: {rd_version}\n" + "BGP main routing table version {rt_version}\n" + "BGP NSR Initial initsync version {nsr_ver} ({nsr_conv})\n" + "BGP NSR/ISSU Sync-Group versions {sg_ver}\n" + "\n" + "Status codes: s suppressed, d damped, h history, * valid, > best\n" + "i - internal, r RIB-failure, S stale, N Nexthop-discard\n" + "Origin codes: i - IGP, e - EGP, ? - incomplete\n" + ) + + if bgp.instances.instance: + show_bgp_adj = str() + show_bgp_table = str() + else: + show_bgp_adj = "No BGP instances found" + count = 0 + + # iterate over all instances + for instance in bgp.instances.instance: + # Show global configs + # Check if vrf exists + vrf = None + for vrf_ in instance.instance_active.vrfs.vrf: + if vrf_.vrf_name == name: + vrf = vrf_ + break + if not vrf: + return "VRF %s not found" % name + + + vrf_name = vrf.global_process_info.vrf_name + vrf_state = "Active" \ + if vrf.global_process_info.vrf.vrf_is_active \ + else "Inactive" + rd = get_rd(vrf.information.route_distinguisher) + vrf_id = hex(vrf.global_process_info.vrfid) + global_process_info = vrf.global_process_info + routerid = global_process_info.vrf.router_id + local_as = global_process_info.global_.local_as + gscan = global_process_info.global_.generic_scan_period + nsr = "enabled" \ + if global_process_info.vrf.is_nsr \ + else "disabled" + + # Show AF information + af = vrf.afs.af[0] + vrf_af = af.global_af_process_info.vrf + tstate = "Active" if vrf_af.table_is_active else "Inactive" + tid = hex(vrf_af.table_id) + rd_version = vrf_af.rd_version + rt_version = vrf_af.table_version + nsr_ver = vrf_af.nsr_conv_version + nsr_conv = "Reached" \ + if vrf_af.nsr_is_conv \ + else "Not Reached" + sg_ver = str(af.global_af_process_info.global_.syncgrp_version[0].entry) + \ + '/' + str(af.global_af_process_info.global_.syncgrp_version[1].entry) + + # Show path information + for path in af.path_table.path: + path_information = path.path_information + hop_ipv4_addr = path_information.next_hop.ipv4_address + ip = path.network + '/' + str(path.prefix_length) + metric = path_information.aigp_metric + locprf = path.attributes_after_policy_in.common_attributes.local_preference + weight = path_information.path_weight + status = get_status(path_information) + path_id = str(path.attributes_after_policy_in.common_attributes.neighbor_as) + if path.attributes_after_policy_in.common_attributes.origin == 0: + path_id += " i" + elif path.attributes_after_policy_in.common_attributes.origin == 1: + path_id += " e" + elif path.attributes_after_policy_in.common_attributes.origin == 2: + path_id += " ?" + show_bgp_table += ('\n' + + bgp_row.format(status = status, + network=ip, + nexthop=hop_ipv4_addr, + metric=metric, + locprf=locprf, + weight=weight, + path=path_id + )) + show_bgp_adj += ('\n' + + bgp_header.format(vrf_name=vrf_name, + vrf_state=vrf_state, + rd=rd, + vrf_id=vrf_id, + routerid=routerid, + local_as=local_as, + gscan=gscan, + nsr=nsr, + tstate=tstate, + tid=tid, + rd_version=rd_version, + rt_version=rt_version, + nsr_ver=nsr_ver, + nsr_conv=nsr_conv, + sg_ver=sg_ver + )) + + show_bgp_adj += bgp_table_header.format(rd=rd) + show_bgp_adj += show_bgp_table + show_bgp_adj += ("\n\n") + return(show_bgp_adj) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + parser.add_argument("name", + help="VRF name") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + bgp = xr_ipv4_bgp_oper.Bgp() # create object + + # read data from NETCONF device + bgp = crud.read(provider, bgp) + print(process_bgp(bgp, args.name)) # process object data + + provider.close() + #print (time.time() - start) + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-24-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-24-ydk.txt new file mode 100644 index 0000000..d1c4a0e --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-bgp-oper/nc-read-xr-ipv4-bgp-oper-24-ydk.txt @@ -0,0 +1,47 @@ +BGP VRF 1, state: Active +BGP Route Distinguisher 11:11 +VRF ID: 0x60000002 +BGP router identifier 110.1.1.1, local AS number 100 +Non-stop routing is enabled +BGP table state: Active +Table ID: 0xe0000011 RD version: 73 +BGP main routing table version 76 +BGP NSR Initial initsync version 72 (Reached) +BGP NSR/ISSU Sync-Group versions 0/0 + +Status codes: s suppressed, d damped, h history, * valid, > best +i - internal, r RIB-failure, S stale, N Nexthop-discard +Origin codes: i - IGP, e - EGP, ? - incomplete + + Network Next Hop Metric LocPrf Weight Path +Route Distinguisher: 11:11 +*>i10.1.1.0/24 200.1.1.1 0 100 0 200 ? +*>i10.10.10.0/24 200.1.1.1 0 100 0 200 ? +*>i13.13.13.0/24 200.1.1.1 0 100 0 200 ? +*>i77.11.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.11.78.0/24 200.1.1.1 0 100 0 200 ? +*>i77.12.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.13.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.14.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.15.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.16.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.17.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.18.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.19.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.20.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.21.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.22.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.23.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.24.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.25.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.26.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.27.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.28.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.29.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.30.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.31.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.32.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.33.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.77.77.0/24 200.1.1.1 0 100 0 200 ? +*>i77.79.77.0/24 200.1.1.1 0 100 0 200 ? +*>i192.168.0.3/32 200.1.1.1 0 100 0 200 ? \ No newline at end of file diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-create-xr-ipv4-ospf-cfg-40-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-create-xr-ipv4-ospf-cfg-40-ydk.py new file mode 100755 index 0000000..8af36e5 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-create-xr-ipv4-ospf-cfg-40-ydk.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Create configuration for model Cisco-IOS-XR-ipv4-ospf-cfg. +Creates using a generic model and command line arguments. + +usage: nc-create-xr-ipv4-ospf-cfg-40-ydk.py [-h] [-v] device name + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + name Name of OSPF + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_ospf_cfg \ + as xr_ipv4_ospf_cfg +from ydk.types import Empty +import logging + +#1 interface at a time? +def config_ospf(ospf, name): + """Add config data to ospf object.""" + # OSPF process + process = ospf.processes.Process() + process.process_name = name + process.start = Empty() + + # Distribute Link-State + process.distribute = process.Distribute() + + # append area/process config + ospf.processes.process.append(process) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + parser.add_argument("name", + help="Name of the OSPF") + + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + ospf = xr_ipv4_ospf_cfg.Ospf() # create object + config_ospf(ospf, args.name) # add object configuration + + # create configuration on NETCONF device + crud.create(provider, ospf) + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-create-xr-ipv4-ospf-cfg-40-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-create-xr-ipv4-ospf-cfg-40-ydk.txt new file mode 100644 index 0000000..a8bb010 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-create-xr-ipv4-ospf-cfg-40-ydk.txt @@ -0,0 +1,4 @@ +router ospf name + distribute link-state +! +end \ No newline at end of file diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-create-xr-ipv4-ospf-cfg-41-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-create-xr-ipv4-ospf-cfg-41-ydk.py new file mode 100755 index 0000000..b4b4959 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-create-xr-ipv4-ospf-cfg-41-ydk.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Create configuration for model Cisco-IOS-XR-ipv4-ospf-cfg. +Creates using a generic model and command line arguments. + +usage: nc-create-xr-ipv4-ospf-cfg-41-ydk.py [-h] [-v] device name areaID interface + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + name Name of OSPF + areaid Area ID + interface Name of the interface + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_ospf_cfg \ + as xr_ipv4_ospf_cfg +from ydk.types import Empty +import logging + +def config_ospf(ospf, name, areaid, interface): + """Add config data to ospf object.""" + # OSPF process + process = ospf.processes.Process() + process.process_name = name + process.start = Empty() + + # Area 0 + area_area_id = process.default_vrf.area_addresses.AreaAreaId() + area_area_id.area_id = int(areaid) + area_area_id.running = Empty() + + # loopback interface passive + name_scope = area_area_id.name_scopes.NameScope() + name_scope.interface_name = interface + name_scope.running = Empty() + area_area_id.name_scopes.name_scope.append(name_scope) + + # append area/process config + process.default_vrf.area_addresses.area_area_id.append(area_area_id) + ospf.processes.process.append(process) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + parser.add_argument("name", + help="Name of the OSPF") + parser.add_argument("areaid", + help="Area ID") + parser.add_argument("interface", + help="Name of the interface") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + ospf = xr_ipv4_ospf_cfg.Ospf() # create object + config_ospf(ospf, args.name, args.areaid, args.interface) # add object configuration + + # create configuration on NETCONF device + crud.create(provider, ospf) + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-create-xr-ipv4-ospf-cfg-41-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-create-xr-ipv4-ospf-cfg-41-ydk.txt new file mode 100644 index 0000000..8b3a16d --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-create-xr-ipv4-ospf-cfg-41-ydk.txt @@ -0,0 +1,8 @@ +router ospf name + router-id 172.16.255.1 + area areaid + interface interface + ! + ! +! +end \ No newline at end of file diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-read-xr-ipv4-ospf-cfg-20-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-read-xr-ipv4-ospf-cfg-20-ydk.py new file mode 100755 index 0000000..1863a3a --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-ipv4-ospf-cfg/nc-read-xr-ipv4-ospf-cfg-20-ydk.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Read all data for model Cisco-IOS-XR-ipv4-ospf-cfg. + +Outputs is to emulate the CLI show command + +usage: show-config-ospf-ipv4-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ipv4_ospf_cfg \ + as xr_ipv4_ospf_cfg +from datetime import timedelta +import logging + +def process_ospf(ospf): + """Process data in ospf object.""" + spacing = 0 + if ospf.processes.process: + show_ospf_config = str() + else: + show_ospf_config = "" + + for process in ospf.processes.process: + show_ospf_config = show_ospf_config + '\n' + (' ' * spacing) + \ + "router ospf " + process.process_name + if process.distribute: + show_ospf_config = show_ospf_config + '\n' + (' ' * spacing) + \ + "distribute link-state" + for area in process.default_vrf.area_addresses.area_area_id: + spacing += 1 + show_ospf_config = show_ospf_config + '\n' + (' ' * spacing) + \ + 'area ' + str(area.area_id) + for name in area.name_scopes.name_scope: + spacing += 1 + show_ospf_config = show_ospf_config + '\n' + (' ' * spacing) + \ + 'interface ' + name.interface_name + show_ospf_config = show_ospf_config + '\n' + \ + (' ' * spacing) + '!' + spacing -= 1 + show_ospf_config = show_ospf_config + '\n' + \ + (' ' * spacing) + '!' + spacing -= 1 + show_ospf_config = show_ospf_config + '\n' + \ + (' ' * spacing) + '!' + + + + # return formatted string + return(show_ospf_config) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + ospf = xr_ipv4_ospf_cfg.Ospf() # create object + + # read data from NETCONF device + ospf = crud.read(provider, ospf) + print(process_ospf(ospf)) # process object data + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-create-xr-policy-repository-cfg-29-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-create-xr-policy-repository-cfg-29-ydk.py new file mode 100755 index 0000000..b8dba7c --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-create-xr-policy-repository-cfg-29-ydk.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Create configuration for model Cisco-IOS-XR-policy-repository-cfg. + +usage: nc-create-xr-policy-repository-cfg-29-ydk.py [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse + +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_policy_repository_cfg \ + as xr_policy_repository_cfg +import logging + +def config_routing_policy(routing_policy): + """Add config data to routing_policy object.""" + route_policy_name = "allow-only-pfxlen-32" + rpl_route_policy = """ + route-policy allow-only-pfxlen-32 + if destination in (0.0.0.0/0 eq 32) then + pass + endif + end-policy + """ + # configure RPL policy + route_policy = routing_policy.route_policies.RoutePolicy() + route_policy.route_policy_name = route_policy_name + route_policy.rpl_route_policy = rpl_route_policy + routing_policy.route_policies.route_policy.append(route_policy) + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + routing_policy = xr_policy_repository_cfg.RoutingPolicy() # create object + config_routing_policy(routing_policy) # add object configuration + + # create configuration on NETCONF device + crud.create(provider, routing_policy) + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-create-xr-policy-repository-cfg-29-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-create-xr-policy-repository-cfg-29-ydk.txt new file mode 100644 index 0000000..6b69955 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-create-xr-policy-repository-cfg-29-ydk.txt @@ -0,0 +1,6 @@ +route-policy allow-only-pfxlen-32 + if destination in (0.0.0.0/0 eq 32) then + pass + endif +end-policy +! \ No newline at end of file diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-create-xr-policy-repository-cfg-40-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-create-xr-policy-repository-cfg-40-ydk.py new file mode 100755 index 0000000..3dd695f --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-create-xr-policy-repository-cfg-40-ydk.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Create configuration for model Cisco-IOS-XR-policy-repository-cfg. +Creates using a generic model and command line arguments. + +usage: nc-create-xr-policy-repository-cfg-40-ydk.py [-h] [-v] device name status + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + name Policy Name + status Pass or Drop +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_policy_repository_cfg \ + as xr_policy_repository_cfg +import logging + +def config_routing_policy(routing_policy, name, status): + """Add config data to routing_policy object.""" + route_policy_name = name + rpl_route_policy = """ + route-policy """ + name + """ + """ + status + """ + end-policy + """ + # configure RPL policy + route_policy = routing_policy.route_policies.RoutePolicy() + route_policy.route_policy_name = route_policy_name + route_policy.rpl_route_policy = rpl_route_policy + routing_policy.route_policies.route_policy.append(route_policy) + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + parser.add_argument("name", + help="Name of the policy") + parser.add_argument("status", + help="Drop or Pass") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + routing_policy = xr_policy_repository_cfg.RoutingPolicy() # create object + config_routing_policy(routing_policy, args.name, args.status) # add object configuration + + # create configuration on NETCONF device + crud.create(provider, routing_policy) + + provider.close() + exit() +# End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-create-xr-policy-repository-cfg-40-ydk.txt b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-create-xr-policy-repository-cfg-40-ydk.txt new file mode 100644 index 0000000..b066004 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-create-xr-policy-repository-cfg-40-ydk.txt @@ -0,0 +1,4 @@ +route-policy name + status +end-policy +! \ No newline at end of file diff --git a/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-read-xr-policy-repository-cfg-20-ydk.py b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-read-xr-policy-repository-cfg-20-ydk.py new file mode 100755 index 0000000..2a766c0 --- /dev/null +++ b/samples/basic/crud/models/cisco-ios-xr/Cisco-IOS-XR-policy-repository-cfg/nc-read-xr-policy-repository-cfg-20-ydk.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python +# +# Copyright 2016 Cisco Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Read all data for model Cisco-IOS-XR-routing-repository-cfg. + +Outputs is to emulate the CLI show command + +usage: show-config-route-policy-ydk [-h] [-v] device + +positional arguments: + device NETCONF device (ssh://user:password@host:port) + +optional arguments: + -h, --help show this help message and exit + -v, --verbose print debugging messages +""" + +from argparse import ArgumentParser +from urlparse import urlparse +from ydk.services import CRUDService +from ydk.providers import NetconfServiceProvider +from ydk.models.cisco_ios_xr import Cisco_IOS_XR_policy_repository_cfg \ + as xr_policy_repository_cfg +from datetime import timedelta +import logging + + +def process_routing_policy(routing_policy): + spacing = 0 + show_routing_policy_config = str() + """Process data in routing_policy object.""" + for route_policy in routing_policy.route_policies.route_policy: + show_routing_policy_config = show_routing_policy_config + '\n' + \ + route_policy.rpl_route_policy + \ + "!" + # return formatted string + return(show_routing_policy_config) + + +if __name__ == "__main__": + """Execute main program.""" + parser = ArgumentParser() + parser.add_argument("-v", "--verbose", help="print debugging messages", + action="store_true") + parser.add_argument("device", + help="NETCONF device (ssh://user:password@host:port)") + args = parser.parse_args() + device = urlparse(args.device) + + # log debug messages if verbose argument specified + if args.verbose: + logger = logging.getLogger("ydk") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + formatter = logging.Formatter(("%(asctime)s - %(name)s - " + "%(levelname)s - %(message)s")) + handler.setFormatter(formatter) + logger.addHandler(handler) + + # create NETCONF provider + provider = NetconfServiceProvider(address=device.hostname, + port=device.port, + username=device.username, + password=device.password, + protocol=device.scheme) + # create CRUD service + crud = CRUDService() + + routing_policy = xr_policy_repository_cfg.RoutingPolicy() # create object + + # read data from NETCONF device + routing_policy = crud.read(provider, routing_policy) + print(process_routing_policy(routing_policy)) # process object data + + provider.close() + + + + + import sys, os + sys.path[1:1] = ['/nobackup/paquach/moonshine-dev/xrut/modules'] + + import xrut, utng, common + routers = xrut.routers + + print "routers: ", routers + exit() + + + # End of script diff --git a/samples/basic/crud/models/cisco-ios-xr/best b/samples/basic/crud/models/cisco-ios-xr/best new file mode 100644 index 0000000..e69de29