| 1 |
# 3rd-party |
| 2 |
from lxml.builder import E |
| 3 |
|
| 4 |
# local |
| 5 |
from jnpr.junos.cfg import Resource |
| 6 |
from jnpr.junos import JXML |
| 7 |
from jnpr.junos.cfg.phyport.base import PhyPortBase |
| 8 |
|
| 9 |
|
| 10 |
class PhyPortSwitch(PhyPortBase): |
| 11 |
PORT_SPEED = { |
| 12 |
"auto": "auto-negotiation", |
| 13 |
"10m": "ethernet-10m", |
| 14 |
"100m": "ethernet-100m", |
| 15 |
"1g": "ethernet-1g", |
| 16 |
} |
| 17 |
|
| 18 |
# ----------------------------------------------------------------------- |
| 19 |
# XML readers |
| 20 |
# ----------------------------------------------------------------------- |
| 21 |
|
| 22 |
def _xml_to_py(self, has_xml, has_py): |
| 23 |
PhyPortBase._xml_to_py(self, has_xml, has_py) |
| 24 |
|
| 25 |
# speed, duplex, loopback are all under 'ether-options' |
| 26 |
ethopts = has_xml.find("ether-options") |
| 27 |
if ethopts is None: |
| 28 |
return |
| 29 |
|
| 30 |
if ethopts.find("loopback") is not None: |
| 31 |
has_py["loopback"] = True |
| 32 |
|
| 33 |
speed = ethopts.find("speed") |
| 34 |
if speed is not None: |
| 35 |
# take the first child element |
| 36 |
has_py["speed"] = speed[0].tag |
| 37 |
PhyPortBase._set_invert(has_py, "speed", self.PORT_SPEED) |
| 38 |
|
| 39 |
Resource.copyifexists(ethopts, "link-mode", has_py, "duplex") |
| 40 |
if "duplex" in has_py: |
| 41 |
PhyPortBase._set_invert(has_py, "duplex", self.PORT_DUPLEX) |
| 42 |
|
| 43 |
# ----------------------------------------------------------------------- |
| 44 |
# XML writers |
| 45 |
# ----------------------------------------------------------------------- |
| 46 |
|
| 47 |
def _xml_hook_build_change_begin(self, xml): |
| 48 |
if any([this in self.should for this in ["speed", "duplex", "loopback"]]): |
| 49 |
self._ethopts = E("ether-options") |
| 50 |
xml.append(self._ethopts) |
| 51 |
|
| 52 |
def _xml_change_speed(self, xml): |
| 53 |
speed_tag = self.PORT_SPEED.get(self.speed) |
| 54 |
add_this = E.speed(JXML.DEL) if speed_tag is None else E.speed(E(speed_tag)) |
| 55 |
self._ethopts.append(add_this) |
| 56 |
return True |
| 57 |
|
| 58 |
def _xml_change_duplex(self, xml): |
| 59 |
value = self.PORT_DUPLEX.get(self.duplex) |
| 60 |
Resource.xml_set_or_delete(self._ethopts, "link-mode", value) |
| 61 |
return True |
| 62 |
|
| 63 |
def _xml_change_loopback(self, xml): |
| 64 |
self._ethopts.append(Resource.xmltag_set_or_del("loopback", self.loopback)) |
| 65 |
return True |