backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / lib / of_config / capable_switch.py
1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2013 Isaku Yamahata <yamahata at private email ne 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 import ncclient
18 import ncclient.manager
19 import ncclient.xml_
20
21 from ryu import exception as ryu_exc
22 from ryu.lib import of_config
23 from ryu.lib.of_config import constants as ofc_consts
24 from ryu.lib.of_config import classes as ofc
25
26
27 # TODO: When we re-organize ncclient, its NCClientError will be
28 #       subclass of RyuException.
29 class OFConfigCapableSwitchNotFound(ryu_exc.RyuException,
30                                     ncclient.NCClientError):
31     message = 'OpenFlow Capable Switch not found'
32
33
34 def get_ns_tag(tag):
35     if tag[0] == '{':
36         return tuple(tag[1:].split('}', 1))
37     return (None, tag)
38
39
40 class OFCapableSwitch(object):
41     def __init__(self, connect_method='connect_ssh', *args, **kwargs):
42         super(OFCapableSwitch, self).__init__()
43         self._connect_method = connect_method
44         self._connect_args = args
45         self._connect_kwargs = kwargs
46         self.version = None
47         self.namespace = None
48
49         connect = getattr(ncclient.manager, self._connect_method)
50         self.netconf = connect(*self._connect_args, **self._connect_kwargs)
51
52     def close_session(self):
53         if self.netconf:
54             self.netconf.close_session()
55             self.netconf = None
56
57     def __enter__(self):
58         return self
59
60     def __exit__(self):
61         self.close_session()
62
63     def client_capabilities(self):
64         return self.netconf.client_capabilities
65
66     def server_capabilities(self):
67         return self.netconf.server_capabilities
68
69     def _find_capable_switch(self, tree):
70         capable_switch = None
71         for element in tree:
72             ns, tag = get_ns_tag(element.tag)
73             if tag != ofc_consts.CAPABLE_SWITCH:
74                 continue
75
76             # assumes that <get> returns only single capable switch
77             assert capable_switch is None
78
79             capable_switch = element
80             if not self.version:
81                 versions = [(version, ns_) for version, ns_ in
82                             of_config.OFCONFIG_YANG_NAMESPACES.items()
83                             if ns == ns_]
84                 if versions:
85                     assert len(versions) == 1
86                     version = versions[0]
87                     self.version, self.namespace = version
88
89         if not capable_switch:
90             raise OFConfigCapableSwitchNotFound()
91
92         return capable_switch
93
94     def _find_capable_switch_xml(self, tree):
95         return ncclient.xml_.to_xml(self._find_capable_switch(tree))
96
97     def raw_get(self, filter=None):
98         reply = self.netconf.get(filter)
99         return self._find_capable_switch_xml(reply.data_ele)
100
101     def raw_get_config(self, source, filter=None):
102         reply = self.netconf.get_config(source, filter)
103         return self._find_capable_switch_xml(reply.data_ele)
104
105     def raw_edit_config(self, target, config, default_operation=None,
106                         test_option=None, error_option=None):
107         self.netconf.edit_config(target, config,
108                                  default_operation, test_option, error_option)
109
110     def get(self):
111         return ofc.OFCapableSwitchType.from_xml(self.raw_get())
112
113     def get_config(self, source):
114         return ofc.OFCapableSwitchType.from_xml(self.raw_get_config(source))
115
116     def edit_config(self, target, capable_switch, default_operation=None):
117         xml = ofc.NETCONF_Config(capable_switch=capable_switch).to_xml()
118         self.raw_edit_config(target, xml, default_operation)
119
120     def delete_config(self, source):
121         self.netconf.delete_config(source)
122
123     def copy_config(self, source, target):
124         self.netconf.copy_config(source, target)
125
126     def commit(self):
127         self.netconf.commit()
128
129     def discard_changes(self):
130         self.netconf.discard_changes()
131
132     # TODO: more netconf operations
133     # TODO: convinience(higher level) methods