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