1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
# Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
# Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp>
#
# 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.
# convenient classes to manipulate OF-Config XML
# in a little more pythonic way.
# currently assuming OF-Config 1.1.1.
from os_ken.lib import stringify
from lxml import objectify
import lxml.etree as ET
_ns_of111 = 'urn:onf:of111:config:yang'
_ns_netconf = 'urn:ietf:params:xml:ns:netconf:base:1.0'
_nsmap = {
'of111': _ns_of111,
'nc': _ns_netconf,
}
def _pythonify(name):
return name.replace('-', '_')
class _e(object):
def __init__(self, name, is_list):
self.name = name
self.cls = None
self.is_list = is_list
# complexType
class _ct(_e):
def __init__(self, name, cls, is_list):
super(_ct, self).__init__(name, is_list)
self.cls = cls
class _Base(stringify.StringifyMixin):
_M = objectify.ElementMaker(annotate=False,
namespace=_ns_of111,
nsmap=_nsmap)
def __init__(self, **kwargs):
for e in self._ELEMENTS:
k = _pythonify(e.name)
try:
v = kwargs.pop(k)
assert e.name not in kwargs
except KeyError:
k = e.name
try:
v = kwargs.pop(k)
except KeyError:
if e.is_list:
v = []
else:
v = None
setattr(self, k, v)
if kwargs:
raise TypeError('unknown kwargs %s' % kwargs)
def to_et(self, tag):
def convert(v):
if isinstance(v, _Base):
return v.to_et(e.name)
elif isinstance(v, objectify.ObjectifiedElement):
assert ET.QName(v.tag).localname == itag
return v
return self._M(itag, v)
args = []
for e in self._ELEMENTS:
itag = e.name
k = _pythonify(itag)
v = getattr(self, k)
if v is None:
continue
if isinstance(v, list):
assert e.is_list
ele = list(map(convert, v))
else:
assert not e.is_list
ele = [convert(v)]
args.extend(ele)
return self._M(tag, *args)
def to_xml(self, tag):
e = self.to_et(tag)
return ET.tostring(e, pretty_print=True)
@classmethod
def from_xml(cls, xmlstring):
et = objectify.fromstring(xmlstring)
return cls.from_et(et)
@classmethod
def from_et(cls, et):
def convert(v):
if e.cls is not None:
return e.cls.from_et(v)
return v
kwargs = {}
for e in cls._ELEMENTS:
try:
v = et[e.name]
except AttributeError:
continue
assert isinstance(v, objectify.ObjectifiedElement)
if len(v) == 1:
v = convert(v)
if e.is_list:
v = [v]
else:
assert e.is_list
v = list(map(convert, v))
k = _pythonify(e.name)
assert k not in kwargs
kwargs[k] = v
return cls(**kwargs)
def __getattribute__(self, k):
return stringify.StringifyMixin.__getattribute__(self, _pythonify(k))
def __setattr__(self, k, v):
stringify.StringifyMixin.__setattr__(self, _pythonify(k), v)
class _Unimpl(_Base):
_ELEMENTS = [
_e('raw_et', is_list=False),
]
def to_et(self, tag):
assert self.raw_et.tag == tag
return self.raw_et
@classmethod
def from_et(cls, et):
return cls(raw_et=et)
|