RIFT OSM R1 Initial Submission
[osm/SO.git] / rwlaunchpad / plugins / rwvns / rift / topmgr / rwtopdatastore.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18
19 import gi
20 gi.require_version('RwTypes', '1.0')
21 from gi.repository import (
22 IetfNetworkYang,
23 IetfNetworkTopologyYang,
24 IetfL2TopologyYang,
25 RwTopologyYang,
26 RwTypes
27 )
28 import logging
29 from gi.repository.RwTypes import RwStatus
30
31
32 class NwtopDataStore(object):
33 """ Common datastore for discovered and static topologies """
34 def __init__(self, log):
35 self._networks = {}
36 self._log = log
37
38 """ Deep copy utility for topology class """
39 def rwtop_copy_object(self, obj):
40 dup = obj.__class__()
41 dup.copy_from(obj)
42 return dup
43
44 """ Utility for updating L2 topology attributes """
45 def _update_l2_attr(self, current_elem, new_elem, new_l2_attr, attr_field_name):
46 if not getattr(current_elem, attr_field_name):
47 self._log.debug ("Creating L2 attributes..%s", l2_attr_field)
48 setattr(current_elem, attr_field_name, new_l2_attr)
49 return
50
51 for l2_attr_field in new_l2_attr.fields:
52 l2_elem_attr_value = getattr(new_l2_attr, l2_attr_field)
53 if l2_elem_attr_value:
54 self._log.debug ("Updating L2 attributes..%s", l2_attr_field)
55 setattr(getattr(current_elem, attr_field_name), l2_attr_field, getattr(new_l2_attr, l2_attr_field))
56
57 """ Utility for updating termination point attributes """
58 def _update_termination_point(self, current_node, new_node, new_tp):
59 current_tp = next((x for x in current_node.termination_point if x.tp_id == new_tp.tp_id), None)
60 if current_tp is None:
61 self._log.debug("Creating termination point..%s", new_tp)
62 # Add tp to current node
63 new_tp_dup = self.rwtop_copy_object(new_tp)
64 current_node.termination_point.append(new_tp_dup)
65 return
66 # Update current tp
67 for tp_field in new_tp.fields:
68 tp_field_value = getattr(new_tp, tp_field)
69 if tp_field_value:
70 self._log.debug("Updating termination point..%s", tp_field)
71 if (tp_field == 'tp_id'):
72 # Don't change key
73 pass
74 elif (tp_field == 'l2_termination_point_attributes'):
75 self._update_l2_attr(current_tp, new_tp, tp_field_value, tp_field)
76 elif (tp_field == 'supporting_termination_point'):
77 self._log.debug(tp_field)
78 else:
79 self._log.info("Updating termination point..Not implemented %s", tp_field)
80 #raise NotImplementedError
81
82 """ Utility for updating link attributes """
83 def _update_link(self, current_nw, new_nw, new_link):
84 current_link = next((x for x in current_nw.link if x.link_id == new_link.link_id), None)
85 if current_link is None:
86 # Add link to current nw
87 self._log.info("Creating link..%s", new_link )
88 new_link_dup = self.rwtop_copy_object(new_link)
89 current_nw.link.append(new_link_dup)
90 return
91 # Update current link
92 for link_field in new_link.fields:
93 link_field_value = getattr(new_link, link_field)
94 if link_field_value:
95 self._log.info("Updating link..%s", link_field)
96 if (link_field == 'link_id'):
97 # Don't change key
98 pass
99 elif (link_field == 'source'):
100 if getattr(link_field_value, 'source_node') is not None:
101 current_link.source.source_node = getattr(link_field_value, 'source_node')
102 if getattr(link_field_value, 'source_tp') is not None:
103 current_link.source.source_tp = getattr(link_field_value, 'source_tp')
104 elif (link_field == 'destination'):
105 if getattr(link_field_value, 'dest_node') is not None:
106 current_link.destination.dest_node = link_field_value.dest_node
107 if getattr(link_field_value, 'dest_tp') is not None:
108 current_link.destination.dest_tp = link_field_value.dest_tp
109 elif (link_field == 'l2_link_attributes'):
110 self._update_l2_attr(current_link, new_link, link_field_value, link_field)
111 elif (link_field == 'supporting_link'):
112 self._log.debug(link_field)
113 else:
114 self._log.info("Update link..Not implemented %s", link_field)
115 #raise NotImplementedError
116
117
118 """ Utility for updating node attributes """
119 def _update_node(self, current_nw, new_nw, new_node):
120 current_node = next((x for x in current_nw.node if x.node_id == new_node.node_id), None)
121 if current_node is None:
122 # Add node to current nw
123 self._log.debug("Creating node..%s", new_node)
124 new_node_dup = self.rwtop_copy_object(new_node)
125 current_nw.node.append(new_node_dup)
126 return
127 # Update current node
128 for node_field in new_node.fields:
129 node_field_value = getattr(new_node, node_field)
130 if node_field_value:
131 self._log.debug("Updating node..%s", node_field)
132 if (node_field == 'node_id'):
133 # Don't change key
134 pass
135 elif (node_field == 'l2_node_attributes'):
136 self._update_l2_attr(current_node, new_node, node_field_value, node_field)
137 elif (node_field == 'termination_point'):
138 for tp in new_node.termination_point:
139 self._update_termination_point(current_node, new_node, tp)
140 elif (node_field == 'supporting-node'):
141 self._log.debug(node_field)
142 else:
143 self._log.info("Update node..Not implemented %s", node_field)
144 #raise NotImplementedError
145
146
147 """ API for retrieving internal network """
148 def get_network(self, network_id):
149 if (network_id not in self._networks):
150 return None
151 return self._networks[network_id]
152
153 """ API for creating internal network """
154 def create_network(self, key, nw):
155 self._networks[key] = self.rwtop_copy_object(nw)
156
157 """ API for updating internal network """
158 def update_network(self, key, new_nw):
159 if key not in self._networks:
160 self._log.debug("Creating network..New_nw %s", new_nw)
161 self._networks[key] = self.rwtop_copy_object(new_nw)
162 return
163 # Iterating thru changed fields
164 for nw_field in new_nw.fields:
165 nw_field_value = getattr(new_nw, nw_field)
166 self._log.debug("Update nw..nw_field %s", nw_field)
167 if nw_field_value:
168 if (nw_field == 'node'):
169 for node in new_nw.node:
170 self._update_node(self._networks[key], new_nw, node)
171 elif (nw_field == 'network_id'):
172 # Don't change key
173 pass
174 elif (nw_field == 'link'):
175 for link in new_nw.link:
176 self._update_link(self._networks[key], new_nw, link)
177 elif (nw_field == 'network_types'):
178 self._networks[key].network_types.l2_network = self._networks[key].network_types.l2_network.new()
179 elif (nw_field == 'l2_network_attributes'):
180 self._update_l2_attr(self._networks[key], new_nw, nw_field_value, nw_field)
181 else:
182 self._log.info("Update nw..Not implemented %s", nw_field)
183 #raise NotImplementedError
184
185
186