huawei_template.py
GET_POLICY_TEMPLATE = """
<qos xmlns="http://www.huawei.com/netconf/vrp/huawei-qos">
<qosIfQoss>
<qosIfPolicyApplys>
<qosIfPolicyApply>
<ifName> {{ port }} </ifName>
<policyName></policyName>
</qosIfPolicyApply>
</qosIfPolicyApplys>
</qosIfQoss>
</qos>
"""
GET_BEHAVIOR_TEMPLATE = """
<qos xmlns="http://www.huawei.com/netconf/vrp/huawei-qos">
<qosCbQos>
<qosPolicys>
<qosPolicy>
<policyName> {{ policy }} </policyName>
<qosPolicyNodes>
<qosPolicyNode>
<behaviorName></behaviorName>
</qosPolicyNode>
</qosPolicyNodes>
</qosPolicy>
</qosPolicys>
</qosCbQos>
</qos>
"""
GET_CIR_TEMPLATE = """
<qos xmlns="http://www.huawei.com/netconf/vrp/huawei-qos">
<qosCbQos>
<qosBehaviors>
<qosBehavior>
<behaviorName> {{ behavior }} </behaviorName>
<qosActCars>
<qosActCar>
<actionType></actionType>
<cir></cir>
</qosActCar>
</qosActCars>
</qosBehavior>
</qosBehaviors>
</qosCbQos>
</qos>
"""
SET_CIR_TEMPLATE = """
<config>
<qos xmlns="http://www.huawei.com/netconf/vrp/huawei-qos">
<qosCbQos>
<qosBehaviors>
<qosBehavior>
<behaviorName> {{ behavior }} </behaviorName>
<qosActCars>
<qosActCar nc:operation="merge" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<actionType>car</actionType>
<cir> {{ cir }} </cir>
<cbs> {{ cbs }} </cbs>
<pbs> {{ pbs }} </pbs>
</qosActCar>
</qosActCars>
</qosBehavior>
</qosBehaviors>
</qosCbQos>
</qos>
</config>
"""
h3c_template.py
GET_INDEX_TEMPLATE = """
<top xmlns="http://www.h3c.com/netconf/data:1.0">
<Ifmgr>
<Interfaces>
<Interface>
<Name> {{ port }} </Name>
<IfIndex></IfIndex>
</Interface>
</Interfaces>
</Ifmgr>
</top>
"""
GET_POLICY_TEMPLATE = """
<top xmlns="http://www.h3c.com/netconf/data:1.0">
<MQC>
<InterfacePolicy>
<Application>
<IfIndex> {{ index }} </IfIndex>
<PolicyName></PolicyName>
</Application>
</InterfacePolicy>
</MQC>
</top>
"""
GET_BEHAVIOR_TEMPLATE = """
<top xmlns="http://www.h3c.com/netconf/data:1.0">
<MQC>
<CBMaps>
<CBMap>
<PolicyName> {{ policy }} </PolicyName>
<BehaviorName></BehaviorName>
</CBMap>
</CBMaps>
</MQC>
</top>
"""
GET_CIR_TEMPLATE = """
<top xmlns="http://www.h3c.com/netconf/data:1.0">
<MQC>
<CAR>
<Action>
<BehaviorName> {{ behavior }} </BehaviorName>
<CIR></CIR>
</Action>
</CAR>
</MQC>
</top>
"""
SET_CIR_TEMPLATE = """
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
<top xmlns="http://www.h3c.com/netconf/config:1.0">
<MQC>
<CAR>
<Action>
<BehaviorName> {{ behavior }} </BehaviorName>
<CIR> {{ cir }} </CIR>
<CBS> {{ cbs }} </CBS>
<EBS> {{ ebs }} </EBS>
</Action>
</CAR>
</MQC>
</top>
</config>
"""
cetconfLib.py
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from ncclient import manager
from lxml import etree
class NetConfLib(object):
def __init__(self, manufacturer, ssh_ip, ssh_port, ssh_username, ssh_password):
manufacturer_list = ['huawei', 'h3c']
if manufacturer not in manufacturer_list:
raise ValueError('Device manufacturer is error.')
self.data = None
self.cursor = None
self.manufacturer = manufacturer
link_device_params = {
'host': ssh_ip,
'port': ssh_port,
'username': ssh_username,
'password': ssh_password,
'allow_agent': False,
'look_for_keys': False,
'hostkey_verify': False,
'device_params': {'name': manufacturer}
}
self.cursor = manager.connect(**link_device_params)
def get_icnd(self, template, label, model='subtree'):
obj = self.cursor.get(filter=(model, template))
xml = obj.xml.replace('<?xml version=\"1.0\" encoding=\"UTF-8\"?>', '')
html = etree.HTML(xml)
value = html.xpath(f'//{label.lower()}/text()')
result = ''.join(set(value))
return result
def set_icnd(self, template, model='running'):
if '<ok/>' in self.cursor.edit_config(target=model, config=template).xml:
return True
else:
return False
def get_cir(self, port):
if self.manufacturer == 'huawei':
from .huawei_template import GET_POLICY_TEMPLATE, GET_BEHAVIOR_TEMPLATE, GET_CIR_TEMPLATE
policy = self.get_icnd(GET_POLICY_TEMPLATE.replace('{{ port }}', port), 'policyName')
behavior = self.get_icnd(GET_BEHAVIOR_TEMPLATE.replace('{{ policy }}', policy), 'behaviorName')
cir = self.get_icnd(GET_CIR_TEMPLATE.replace('{{ behavior }}', behavior), 'cir')
get_huawei_data = {'policy': policy, 'behavior': behavior, 'cir': int(cir)}
return get_huawei_data
if self.manufacturer == 'h3c':
from .h3c_template import GET_INDEX_TEMPLATE, GET_POLICY_TEMPLATE, GET_BEHAVIOR_TEMPLATE, GET_CIR_TEMPLATE
index = self.get_icnd(GET_INDEX_TEMPLATE.replace('{{ port }}', port), 'IfIndex')
policy = self.get_icnd(GET_POLICY_TEMPLATE.replace('{{ index }}', index), 'policyName')
behavior = self.get_icnd(GET_BEHAVIOR_TEMPLATE.replace('{{ policy }}', policy), 'behaviorName')
cir = self.get_icnd(GET_CIR_TEMPLATE.replace('{{ behavior }}', behavior), 'cir')
get_h3c_data = {'policy': policy, 'behavior': behavior, 'cir': int(cir)}
return get_h3c_data
def set_cir(self, behavior, cir):
if 1000 <= cir <= 4000:
cbs = 192000
elif 5000 <= cir <= 9000:
cbs = 960000
elif 10000 <= cir <= 19000:
cbs = 1920000
elif 20000 <= cir <= 29000:
cbs = 3840000
elif 30000 <= cir <= 39000:
cbs = 5760000
elif 40000 <= cir <= 49000:
cbs = 7680000
elif 50000 <= cir <= 59000:
cbs = 9600000
elif 60000 <= cir <= 69000:
cbs = 11520000
elif 70000 <= cir <= 79000:
cbs = 13440000
elif 80000 <= cir <= 82000:
cbs = 15360000
elif 83000 <= cir:
cbs = 16000000
else:
cbs = 16000000
if self.manufacturer == 'huawei':
from .huawei_template import SET_CIR_TEMPLATE
set_cir_template = SET_CIR_TEMPLATE\
.replace('{{ behavior }}', behavior)\
.replace('{{ cir }}', str(cir))\
.replace('{{ cbs }}', str(cbs))\
.replace('{{ pbs }}', str(cbs))
result = self.set_icnd(set_cir_template)
return result
if self.manufacturer == 'h3c':
from .h3c_template import SET_CIR_TEMPLATE
set_cir_template = SET_CIR_TEMPLATE\
.replace('{{ behavior }}', behavior)\
.replace('{{ cir }}', str(cir))\
.replace('{{ cbs }}', str(cbs))\
.replace('{{ ebs }}', str(cbs))
result = self.set_icnd(set_cir_template)
return result
if __name__ == '__main__':
ncl = NetConfLib(
manufacturer='huawei', ssh_ip=xxx.xxx.xxx.xx', ssh_port=22,
ssh_username='xxx', ssh_password='xxx'
)
speed = ncl.get_cir(port='xxx')
print(speed)
status = ncl.set_cir(behavior=speed.get('behavior'), cir=10000)
print(status)
speed = ncl.get_cir(port='xxx')
print(speed)