Skip to content
This repository has been archived by the owner on May 15, 2019. It is now read-only.

Dbarrosop/simplifications #68

Merged
merged 9 commits into from
Jul 1, 2017
2 changes: 1 addition & 1 deletion napalm_yang/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def resolve_rule(rule, attribute, keys, extra_vars=None, translation_model=None,
elif isinstance(rule, str):
if rule in ["unnecessary"]:
return [{"mode": "skip", "reason": rule}]
elif rule in ["not_implemented"]:
elif rule in ["not_implemented", "not_supported"]:
return [{"mode": "gate", "reason": rule}]
else:
raise Exception("Not sure what to do with rule {} on attribute {}".format(rule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interfaces:
_process:
- path: interfaces
from: interfaces.0
regexp: "(?P<value>\\w+(\\w|-|\\/)*\\d+)$"
config:
_process: unnecessary
hold-time:
Expand All @@ -37,6 +38,10 @@ interfaces:
counters:
_process:
- path: interfaceCounters
default: {}
when: "{{ not interface_key.startswith('Loopback') }}"
- mode: gate
when: "{{ interface_key.startswith('Loopback') }}"
in-broadcast-pkts:
_process:
- path: inBroadcastPkts
Expand All @@ -49,39 +54,31 @@ interfaces:
in-multicast-pkts:
_process:
- path: inMulticastPkts
_process: not_implemented
in-octets:
_process:
- path: inOctets
_process: not_implemented
in-unicast-pkts:
_process:
- path: inUcastPkts
_process: not_implemented
in-unknown-protos:
_process: not_implemented
last-clear:
_process: not_implemented
out-broadcast-pkts:
_process:
- path: outBroadcastPkts
_process: not_implemented
out-discards:
_process:
- path: outDiscards
_process: not_implemented
out-errors:
_process:
- path: totalOutErrors
_process: not_implemented
out-multicast-pkts:
_process:
- path: outMulticastPkts
_process: not_implemented
out-octets:
_process:
- path: outOctets
_process: not_implemented
out-unicast-pkts:
_process:
- path: outUcastPkts
Expand All @@ -106,7 +103,8 @@ interfaces:
_process:
- path: mtu
name:
_process: not_implemented
_process:
- path: name
oper-status:
_process:
- mode: map
Expand All @@ -121,59 +119,63 @@ interfaces:
_process:
- mode: map
path: hardware
default: Unspecified
map:
"ethernet": ethernetCsmacd
"Unspecified": null
ethernet: ethernetCsmacd
management: ethernetCsmacd
loopback: softwareLoopback
portchannel: ieee8023adLag
vlan: l3ipvlan
subinterfaces:
_process: not_implemented
_process: unnecessary
subinterface:
_process: not_implemented
_process:
- path: interfaces
from: interfaces.0
regexp: "{{interface_key}}\\.(?P<value>\\d+)"
config:
_process: unnecessary
state:
_process: not_implemented
_process: unnecessary
admin-status:
_process: not_implemented
_process:
- mode: map
path: interfaceStatus
map:
"disabled": DOWN
"errdisabled": DOWN
"connected": UP
"notconnect": DOWN
counters:
_process: not_implemented
in-broadcast-pkts:
_process: not_implemented
in-discards:
_process: not_implemented
in-errors:
_process: not_implemented
in-multicast-pkts:
_process: not_implemented
in-octets:
_process: not_implemented
in-unicast-pkts:
_process: not_implemented
in-unknown-protos:
_process: not_implemented
last-clear:
_process: not_implemented
out-broadcast-pkts:
_process: not_implemented
out-discards:
_process: not_implemented
out-errors:
_process: not_implemented
out-multicast-pkts:
_process: not_implemented
out-octets:
_process: not_implemented
out-unicast-pkts:
_process: not_implemented
_process: not_supported
description:
_process: not_implemented
_process:
- path: description
enabled:
_process: not_implemented
_process:
- mode: map
path: interfaceStatus
default: disabled
map:
"disabled": false
"errdisabled": true
"connected": true
"notconnect": true
ifindex:
_process: not_implemented
index:
_process: not_implemented
last-change:
_process: not_implemented
name:
_process: not_implemented
_process:
- path: name
oper-status:
_process: not_implemented
_process:
- mode: map
path: lineProtocolStatus
map:
"disabled": DOWN
"errdisabled": DOWN
"connected": UP
"notconnect": DOWN
"lowerlayerdown": LOWER_LAYER_DOWN
16 changes: 11 additions & 5 deletions napalm_yang/parsers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
class BaseParser(object):

@staticmethod
def resolve_path(my_dict, path):
def resolve_path(my_dict, path, default=None):
if path is None:
return

b = my_dict
for p in path.split("."):
path_split = path.split(".")
for i, p in enumerate(path_split):
try:
b = b[p]
except TypeError:
b = b[int(p)]
try:
b = b[p]
except TypeError:
b = b[int(p)]
except KeyError:
if i == len(path_split) - 1 and default is not None:
return default
raise
return b

@classmethod
Expand Down
18 changes: 14 additions & 4 deletions napalm_yang/parsers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,24 @@ def init_native(cls, native):

@classmethod
def _parse_list_default(cls, mapping, data, key=None):
d = cls.resolve_path(data, mapping.get("path", ""))
d = cls.resolve_path(data, mapping["path"], mapping.get("default"))

regexp = mapping.get('regexp', None)
if regexp:
regexp = re.compile(regexp)
if isinstance(d, dict):
for k, v in d.items():
if regexp:
match = regexp.match(k)
if match:
k = match.group('value')
else:
continue
yield k, v, {}

@classmethod
def _parse_leaf_default(cls, mapping, data, check_default=True, check_presence=False):
d = cls.resolve_path(data, mapping["path"])
d = cls.resolve_path(data, mapping["path"], mapping.get("default"))
if d and not check_presence:
regexp = mapping.get('regexp', None)
if regexp:
Expand All @@ -47,13 +57,13 @@ def _parse_leaf_default(cls, mapping, data, check_default=True, check_presence=F

@classmethod
def _parse_container_default(cls, mapping, data):
d = cls.resolve_path(data, mapping["path"])
d = cls.resolve_path(data, mapping["path"], mapping.get("default"))
return d, {}

@classmethod
def _parse_leaf_map(cls, mapping, data):
v = cls._parse_leaf_default(mapping, data)
return mapping['map'][v]
return mapping['map'][v.lower()]

@classmethod
def _parse_leaf_is_present(cls, mapping, data):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,110 @@
"state": {
"admin-status": "UP",
"counters": {
"in-broadcast-pkts": 2630,
"in-discards": 0,
"in-errors": 0,
"out-unicast-pkts": 104
"out-multicast-pkts": 875697,
"out-octets": 114560338
},
"description": "",
"description": "This is a description",
"enabled": true,
"mtu": 1500,
"mtu": 9214,
"name": "Ethernet1",
"oper-status": "UP",
"type": "ethernetCsmacd"
}
},
"Ethernet2": {
"name": "Ethernet2",
"state": {
"admin-status": "DOWN",
"counters": {
"out-multicast-pkts": 56,
"out-octets": 7268
},
"description": "so much oc",
"enabled": false,
"mtu": 1500,
"name": "Ethernet2",
"oper-status": "DOWN",
"type": "ethernetCsmacd"
},
"subinterfaces": {
"subinterface": {
"1": {
"index": "1",
"state": {
"admin-status": "DOWN",
"description": "another subiface",
"enabled": true,
"name": "Ethernet2.1",
"oper-status": "LOWER_LAYER_DOWN"
}
},
"2": {
"index": "2",
"state": {
"admin-status": "DOWN",
"description": "asdasdasd",
"enabled": true,
"name": "Ethernet2.2",
"oper-status": "LOWER_LAYER_DOWN"
}
}
}
}
},
"Loopback1": {
"name": "Loopback1",
"state": {
"admin-status": "UP",
"description": "a loopback",
"enabled": true,
"mtu": 65535,
"name": "Loopback1",
"oper-status": "UP",
"type": "softwareLoopback"
}
},
"Management1": {
"name": "Management1",
"state": {
"admin-status": "UP",
"counters": {
"in-broadcast-pkts": 19,
"in-discards": 0,
"in-errors": 0,
"out-unicast-pkts": 758
"in-broadcast-pkts": 4,
"in-octets": 4116864,
"out-octets": 13968476,
"out-unicast-pkts": 58110
},
"description": "",
"enabled": true,
"mtu": 1500,
"name": "Management1",
"oper-status": "UP",
"type": "ethernetCsmacd"
}
},
"Port-Channel1": {
"name": "Port-Channel1",
"state": {
"admin-status": "UP",
"description": "blah",
"enabled": true,
"mtu": 9000,
"name": "Port-Channel1",
"oper-status": "DOWN",
"type": "ieee8023adLag"
},
"subinterfaces": {
"subinterface": {
"1": {
"index": "1",
"state": {
"admin-status": "DOWN",
"enabled": true,
"name": "Port-Channel1.1",
"oper-status": "LOWER_LAYER_DOWN"
}
}
}
}
}
}
}
Expand Down
Loading