Skip to content

Commit

Permalink
Merge pull request #1398 from minefuto/junos_get_vlans
Browse files Browse the repository at this point in the history
Add get_vlans for Junos
  • Loading branch information
mirceaulinic authored Mar 22, 2021
2 parents 4befe3c + 87dcb22 commit ba5b267
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 0 deletions.
33 changes: 33 additions & 0 deletions napalm/junos/junos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2362,3 +2362,36 @@ def get_network_instances(self, name=""):
if name not in network_instances:
return {}
return {name: network_instances[name]}

def get_vlans(self):
result = {}
switch_style = self.device.facts.get("switch_style", "")
if switch_style == "VLAN_L2NG":
vlan = junos_views.junos_vlans_table_switch_l2ng(self.device)
elif switch_style == "BRIDGE_DOMAIN":
vlan = junos_views.junos_vlans_table(self.device)
elif switch_style == "VLAN":
vlan = junos_views.junos_vlans_table_switch(self.device)
else: # switch_style == "NONE"
return result

vlan.get()
unmatch_pattern = "l2rtb-interface-name|None|l2ng-l2rtb-vlan-member-interface"
for vlan_id, vlan_data in vlan.items():
_vlan_data = {}
for k, v in vlan_data:
if k == "vlan_name":
_vlan_data["name"] = v
if k == "interfaces":
if v is None:
_vlan_data["interfaces"] = []
elif isinstance(v, str):
if bool(re.match(unmatch_pattern, v)):
_vlan_data["interfaces"] = []
else:
_vlan_data["interfaces"] = [v.replace("*", "")]
else:
_vlan_data["interfaces"] = [_v.replace("*", "") for _v in v]

result[vlan_id] = _vlan_data
return result
37 changes: 37 additions & 0 deletions napalm/junos/utils/junos_views.yml
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,40 @@ junos_nw_instances_view:
instance_type: {instance-type: unicode}
interfaces: {interface/name | bridge-domains/domain/interface/name: unicode}
route_distinguisher: {route-distinguisher/rd-type: unicode}

###
### Get Vlans
###

junos_vlans_table:
rpc: get-bridge-instance-information
item: l2ald-bridge-instance-group
key: l2rtb-bridge-vlan
view: junos_vlans_view

junos_vlans_view:
fields:
vlan_name: l2rtb-bridging-domain
interfaces: l2rtb-interface-name

junos_vlans_table_switch:
rpc: get-vlan-information
item: vlan
key: vlan-tag
view: junos_vlans_view_switch

junos_vlans_view_switch:
fields:
vlan_name: vlan-name
interfaces: vlan-detail/vlan-member-list/vlan-member/vlan-member-interface

junos_vlans_table_switch_l2ng:
rpc: get-vlan-information
item: l2ng-l2ald-vlan-instance-group
key: l2ng-l2rtb-vlan-tag
view: junos_vlans_view_switch_l2ng

junos_vlans_view_switch_l2ng:
fields:
vlan_name: l2ng-l2rtb-vlan-name
interfaces: l2ng-l2rtb-vlan-member/l2ng-l2rtb-vlan-member-interface
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"10": {
"name": "VLAN10",
"interfaces": [
"ge-2/0/1.0",
"ge-2/0/2.0",
"ge-2/0/3.0"
]
},
"2": {
"name": "VLAN2",
"interfaces": [
"ge-2/0/1.0"
]
},
"300": {
"name": "VLAN300",
"interfaces": []
},
"4000": {
"name": "VLAN4000",
"interfaces": [
"xe-1/0/0.0",
"xe-1/2/0.0"
]
},
"4001": {
"name": "VLAN4001",
"interfaces": []
}
}
2 changes: 2 additions & 0 deletions test/junos/mocked_data/test_get_vlans/bridge_domain/facts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
personality: MX
switch_style: BRIDGE_DOMAIN
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<l2ald-bridge-instance-information>
<l2ald-bridge-instance-group>
<l2rtb-brief-summary/>
<l2rtb-name>default-switch</l2rtb-name>
<l2rtb-bridging-domain>VLAN10</l2rtb-bridging-domain>
<l2rtb-bridge-vlan>10</l2rtb-bridge-vlan>
<l2rtb-interface-name>ge-2/0/1.0</l2rtb-interface-name>
<l2rtb-interface-name>ge-2/0/2.0</l2rtb-interface-name>
<l2rtb-interface-name>ge-2/0/3.0</l2rtb-interface-name>
</l2ald-bridge-instance-group>
<l2ald-bridge-instance-group>
<l2rtb-name>default-switch</l2rtb-name>
<l2rtb-bridging-domain>VLAN2</l2rtb-bridging-domain>
<l2rtb-bridge-vlan>2</l2rtb-bridge-vlan>
<l2rtb-interface-name>ge-2/0/1.0</l2rtb-interface-name>
</l2ald-bridge-instance-group>
<l2ald-bridge-instance-group>
<l2rtb-name>default-switch</l2rtb-name>
<l2rtb-bridging-domain>VLAN300</l2rtb-bridging-domain>
<l2rtb-bridge-vlan>300</l2rtb-bridge-vlan>
<l2rtb-interface-name/>
</l2ald-bridge-instance-group>
<l2ald-bridge-instance-group>
<l2rtb-name>default-switch</l2rtb-name>
<l2rtb-bridging-domain>VLAN4000</l2rtb-bridging-domain>
<l2rtb-bridge-vlan>4000</l2rtb-bridge-vlan>
<l2rtb-interface-name>xe-1/0/0.0</l2rtb-interface-name>
<l2rtb-interface-name>xe-1/2/0.0</l2rtb-interface-name>
</l2ald-bridge-instance-group>
<l2ald-bridge-instance-group>
<l2rtb-name>default-switch</l2rtb-name>
<l2rtb-bridging-domain>VLAN4001</l2rtb-bridging-domain>
<l2rtb-bridge-vlan>4001</l2rtb-bridge-vlan>
</l2ald-bridge-instance-group>
</l2ald-bridge-instance-information>
33 changes: 33 additions & 0 deletions test/junos/mocked_data/test_get_vlans/vlan/expected_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"10": {
"name": "VLAN10",
"interfaces": [
"ge-0/0/1.0",
"ge-0/0/2.0",
"ge-0/0/3.0"
]
},
"2": {
"name": "VLAN2",
"interfaces": [
"ge-0/0/1.0"
]
},
"300": {
"name": "VLAN300",
"interfaces": []
},
"4000": {
"name": "VLAN4000",
"interfaces": [
"ge-0/0/4.0",
"ge-0/0/5.0"
]
},
"0": {
"name": "default",
"interfaces": [
"ge-0/0/0.0"
]
}
}
2 changes: 2 additions & 0 deletions test/junos/mocked_data/test_get_vlans/vlan/facts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
personality: SWITCH
switch_style: VLAN
114 changes: 114 additions & 0 deletions test/junos/mocked_data/test_get_vlans/vlan/get-vlan-information.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<vlan-information>
<vlan-terse/>
<vlan>
<vlan-instance>0</vlan-instance>
<vlan-name>VLAN10</vlan-name>
<vlan-create-time>Fri Mar 12 11:32:21 2021
</vlan-create-time>
<vlan-status>Enabled</vlan-status>
<vlan-owner>static</vlan-owner>
<vlan-tag>10</vlan-tag>
<vlan-tag-string>10</vlan-tag-string>
<vlan-index>53</vlan-index>
<vlan-protocol-port>Port Mode</vlan-protocol-port>
<vlan-members-count>3</vlan-members-count>
<vlan-members-upcount>1</vlan-members-upcount>
<vlan-detail>
<vlan-member-list>
<vlan-member>
<vlan-member-interface>ge-0/0/1.0*</vlan-member-interface>
</vlan-member>
<vlan-member>
<vlan-member-interface>ge-0/0/2.0</vlan-member-interface>
</vlan-member>
<vlan-member>
<vlan-member-interface>ge-0/0/3.0</vlan-member-interface>
</vlan-member>
</vlan-member-list>
</vlan-detail>
</vlan>
<vlan>
<vlan-instance>0</vlan-instance>
<vlan-name>VLAN2</vlan-name>
<vlan-create-time>Fri Mar 12 11:32:21 2021
</vlan-create-time>
<vlan-status>Enabled</vlan-status>
<vlan-owner>static</vlan-owner>
<vlan-tag>2</vlan-tag>
<vlan-tag-string>2</vlan-tag-string>
<vlan-index>54</vlan-index>
<vlan-protocol-port>Port Mode</vlan-protocol-port>
<vlan-members-count>1</vlan-members-count>
<vlan-members-upcount>1</vlan-members-upcount>
<vlan-detail>
<vlan-member-list>
<vlan-member>
<vlan-member-interface>ge-0/0/1.0*</vlan-member-interface>
</vlan-member>
</vlan-member-list>
</vlan-detail>
</vlan>
<vlan>
<vlan-instance>0</vlan-instance>
<vlan-name>VLAN300</vlan-name>
<vlan-create-time>Fri Mar 12 11:32:21 2021
</vlan-create-time>
<vlan-status>Enabled</vlan-status>
<vlan-owner>static</vlan-owner>
<vlan-tag>300</vlan-tag>
<vlan-tag-string>300</vlan-tag-string>
<vlan-index>55</vlan-index>
<vlan-protocol-port>Port Mode</vlan-protocol-port>
<vlan-detail>
<vlan-member-list>
<vlan-member>
<vlan-member-interface>None</vlan-member-interface>
</vlan-member>
</vlan-member-list>
</vlan-detail>
</vlan>
<vlan>
<vlan-instance>0</vlan-instance>
<vlan-name>VLAN4000</vlan-name>
<vlan-create-time>Fri Mar 12 11:32:21 2021
</vlan-create-time>
<vlan-status>Enabled</vlan-status>
<vlan-owner>static</vlan-owner>
<vlan-tag>4000</vlan-tag>
<vlan-tag-string>4000</vlan-tag-string>
<vlan-index>56</vlan-index>
<vlan-protocol-port>Port Mode</vlan-protocol-port>
<vlan-members-count>2</vlan-members-count>
<vlan-members-upcount>0</vlan-members-upcount>
<vlan-detail>
<vlan-member-list>
<vlan-member>
<vlan-member-interface>ge-0/0/4.0</vlan-member-interface>
</vlan-member>
<vlan-member>
<vlan-member-interface>ge-0/0/5.0</vlan-member-interface>
</vlan-member>
</vlan-member-list>
</vlan-detail>
</vlan>
<vlan>
<vlan-instance>0</vlan-instance>
<vlan-name>default</vlan-name>
<vlan-create-time>Fri Dec 25 07:14:42 2020
</vlan-create-time>
<vlan-status>Enabled</vlan-status>
<vlan-owner>static</vlan-owner>
<vlan-tag>0</vlan-tag>
<vlan-index>2</vlan-index>
<vlan-protocol-port>Port Mode</vlan-protocol-port>
<vlan-members-count>1</vlan-members-count>
<vlan-members-upcount>1</vlan-members-upcount>
<vlan-detail>
<vlan-member-list>
<vlan-member>
<vlan-member-interface>ge-0/0/0.0*</vlan-member-interface>
</vlan-member>
</vlan-member-list>
</vlan-detail>
</vlan>
</vlan-information>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"10": {
"name": "VLAN10",
"interfaces": [
"xe-0/0/1.0",
"xe-0/0/2.0",
"xe-0/0/3.0"
]
},
"2": {
"name": "VLAN2",
"interfaces": [
"xe-0/0/1.0"
]
},
"300": {
"name": "VLAN300",
"interfaces": []
},
"4000": {
"name": "VLAN4000",
"interfaces": [
"xe-0/0/4.0",
"xe-0/0/5.0"
]
},
"1": {
"name": "default",
"interfaces": [
"xe-0/0/0.0"
]
}
}
2 changes: 2 additions & 0 deletions test/junos/mocked_data/test_get_vlans/vlan_l2ng/facts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
personality: SWITCH
switch_style: VLAN_L2NG
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<vlan-information>
<l2ng-l2ald-vlan-instance-group>
<l2ng-l2rtb-brief-summary/>
<l2ng-l2rtb-name>default-switch</l2ng-l2rtb-name>
<l2ng-l2rtb-vlan-name>VLAN10</l2ng-l2rtb-vlan-name>
<l2ng-l2rtb-vlan-tag>10</l2ng-l2rtb-vlan-tag>
<l2ng-l2rtb-vlan-member>
<l2ng-l2rtb-vlan-member-interface>xe-0/0/1.0</l2ng-l2rtb-vlan-member-interface>
</l2ng-l2rtb-vlan-member>
<l2ng-l2rtb-vlan-member>
<l2ng-l2rtb-vlan-member-interface>xe-0/0/2.0*</l2ng-l2rtb-vlan-member-interface>
</l2ng-l2rtb-vlan-member>
<l2ng-l2rtb-vlan-member>
<l2ng-l2rtb-vlan-member-interface>xe-0/0/3.0*</l2ng-l2rtb-vlan-member-interface>
</l2ng-l2rtb-vlan-member>
</l2ng-l2ald-vlan-instance-group>
<l2ng-l2ald-vlan-instance-group>
<l2ng-l2rtb-name>default-switch</l2ng-l2rtb-name>
<l2ng-l2rtb-vlan-name>VLAN2</l2ng-l2rtb-vlan-name>
<l2ng-l2rtb-vlan-tag>2</l2ng-l2rtb-vlan-tag>
<l2ng-l2rtb-vlan-member>
<l2ng-l2rtb-vlan-member-interface>xe-0/0/1.0</l2ng-l2rtb-vlan-member-interface>
</l2ng-l2rtb-vlan-member>
</l2ng-l2ald-vlan-instance-group>
<l2ng-l2ald-vlan-instance-group>
<l2ng-l2rtb-name>default-switch</l2ng-l2rtb-name>
<l2ng-l2rtb-vlan-name>VLAN300</l2ng-l2rtb-vlan-name>
<l2ng-l2rtb-vlan-tag>300</l2ng-l2rtb-vlan-tag>
<l2ng-l2rtb-vlan-member>
<l2ng-l2rtb-vlan-member-interface/>
</l2ng-l2rtb-vlan-member>
</l2ng-l2ald-vlan-instance-group>
<l2ng-l2ald-vlan-instance-group>
<l2ng-l2rtb-name>default-switch</l2ng-l2rtb-name>
<l2ng-l2rtb-vlan-name>VLAN4000</l2ng-l2rtb-vlan-name>
<l2ng-l2rtb-vlan-tag>4000</l2ng-l2rtb-vlan-tag>
<l2ng-l2rtb-vlan-member>
<l2ng-l2rtb-vlan-member-interface>xe-0/0/4.0*</l2ng-l2rtb-vlan-member-interface>
</l2ng-l2rtb-vlan-member>
<l2ng-l2rtb-vlan-member>
<l2ng-l2rtb-vlan-member-interface>xe-0/0/5.0*</l2ng-l2rtb-vlan-member-interface>
</l2ng-l2rtb-vlan-member>
</l2ng-l2ald-vlan-instance-group>
<l2ng-l2ald-vlan-instance-group>
<l2ng-l2rtb-name>default-switch</l2ng-l2rtb-name>
<l2ng-l2rtb-vlan-name>default</l2ng-l2rtb-vlan-name>
<l2ng-l2rtb-vlan-tag>1</l2ng-l2rtb-vlan-tag>
<l2ng-l2rtb-vlan-member>
<l2ng-l2rtb-vlan-member-interface>xe-0/0/0.0</l2ng-l2rtb-vlan-member-interface>
</l2ng-l2rtb-vlan-member>
</l2ng-l2ald-vlan-instance-group>
</vlan-information>

0 comments on commit ba5b267

Please sign in to comment.