blob: 4fa404c171fec9ef6704546cedeb838dc9e5ec5f [file] [log] [blame]
Chip Boling252c7772017-08-16 10:13:17 -05001# Copyright 2017-present Open Networking Foundation
Chip Boling7294b252017-06-15 16:16:55 -05002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
Chip Boling252c7772017-08-16 10:13:17 -05007# http://www.apache.org/licenses/LICENSE-2.0
Chip Boling7294b252017-06-15 16:16:55 -05008#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
Chip Boling7294b252017-06-15 16:16:55 -050014
Chip Boling252c7772017-08-16 10:13:17 -050015import xmltodict
16import re
Chip Boling7294b252017-06-15 16:16:55 -050017from enum import Enum
Chip Boling252c7772017-08-16 10:13:17 -050018from twisted.internet import reactor
19from twisted.internet.defer import inlineCallbacks, returnValue, succeed
Chip Boling5561d552017-07-07 15:11:26 -050020from voltha.core.flow_decomposer import *
Chip Boling7294b252017-06-15 16:16:55 -050021
22log = structlog.get_logger()
23
Chip Boling252c7772017-08-16 10:13:17 -050024EVC_NAME_FORMAT = 'VOLTHA-{}' # format(flow.id)
25EVC_NAME_REGEX_ALL = EVC_NAME_FORMAT.format('*')
Chip Boling7294b252017-06-15 16:16:55 -050026DEFAULT_STPID = 0x8100
27
28
29class EVC(object):
30 """
31 Class to wrap EVC functionality
32 """
33 class SwitchingMethod(Enum):
Chip Boling5561d552017-07-07 15:11:26 -050034 SINGLE_TAGGED = 1
35 DOUBLE_TAGGED = 2
36 MAC_SWITCHED = 3
37 DOUBLE_TAGGED_MAC_SWITCHED = 4
38 DEFAULT = SINGLE_TAGGED
39
40 @staticmethod
41 def xml(value):
42 if value is None:
43 value = EVC.SwitchingMethod.DEFAULT
44 if value == EVC.SwitchingMethod.SINGLE_TAGGED:
45 return '<single-tag-switched/>'
46 elif value == EVC.SwitchingMethod.DOUBLE_TAGGED:
47 return '<double-tag-switched/>'
48 elif value == EVC.SwitchingMethod.MAC_SWITCHED:
49 return '<mac-switched/>'
50 elif value == EVC.SwitchingMethod.DOUBLE_TAGGED_MAC_SWITCHED:
51 return '<double-tag-mac-switched/>'
52 raise ValueError('Invalid SwitchingMethod enumeration')
Chip Boling7294b252017-06-15 16:16:55 -050053
54 class Men2UniManipulation(Enum):
Chip Boling252c7772017-08-16 10:13:17 -050055 SYMMETRIC = 1
Chip Boling5561d552017-07-07 15:11:26 -050056 POP_OUT_TAG_ONLY = 2
Chip Boling252c7772017-08-16 10:13:17 -050057 DEFAULT = SYMMETRIC
Chip Boling5561d552017-07-07 15:11:26 -050058
59 @staticmethod
60 def xml(value):
61 if value is None:
62 value = EVC.Men2UniManipulation.DEFAULT
63 fmt = '<men-to-uni-tag-manipulation>{}</men-to-uni-tag-manipulation>'
Chip Boling252c7772017-08-16 10:13:17 -050064 if value == EVC.Men2UniManipulation.SYMMETRIC:
65 return fmt.format('<symmetric/>')
Chip Boling5561d552017-07-07 15:11:26 -050066 elif value == EVC.Men2UniManipulation.POP_OUT_TAG_ONLY:
67 return fmt.format('<pop-outer-tag-only/>')
68 raise ValueError('Invalid Men2UniManipulation enumeration')
Chip Boling7294b252017-06-15 16:16:55 -050069
70 class ElineFlowType(Enum):
Chip Boling5561d552017-07-07 15:11:26 -050071 NNI_TO_UNI = 1
72 UNI_TO_NNI = 2
73 NNI_TO_NNI = 3
Chip Boling252c7772017-08-16 10:13:17 -050074 UNI_TO_UNI = 4
75 ACL_FILTER = 5
76 UNKNOWN = 6
77 UNSUPPORTED = 7 # Or Invalid
Chip Boling7294b252017-06-15 16:16:55 -050078
79 def __init__(self, flow_entry):
80 self._installed = False
81 self._status_message = None
Chip Boling5561d552017-07-07 15:11:26 -050082 self._flow = flow_entry
83 self._name = self._create_name()
Chip Boling252c7772017-08-16 10:13:17 -050084 self._evc_maps = {} # Map Name -> evc-map
85 self._install_deferred = None
Chip Boling7294b252017-06-15 16:16:55 -050086
87 self._flow_type = EVC.ElineFlowType.UNKNOWN
88
89 # EVC related properties
Chip Boling7294b252017-06-15 16:16:55 -050090 self._enabled = True
Chip Boling7294b252017-06-15 16:16:55 -050091 self._men_ports = []
Chip Boling5561d552017-07-07 15:11:26 -050092 self._s_tag = None
93 self._stpid = None
94 self._switching_method = None
Chip Boling7294b252017-06-15 16:16:55 -050095
Chip Boling5561d552017-07-07 15:11:26 -050096 self._ce_vlan_preservation = None
97 self._men_to_uni_tag_manipulation = None
Chip Boling7294b252017-06-15 16:16:55 -050098
Chip Boling5561d552017-07-07 15:11:26 -050099 try:
100 self._valid = self._decode()
Chip Boling7294b252017-06-15 16:16:55 -0500101
Chip Boling5561d552017-07-07 15:11:26 -0500102 except Exception as e:
103 log.exception('Failure during EVC decode', e=e)
104 self._valid = False
Chip Boling7294b252017-06-15 16:16:55 -0500105
Chip Boling252c7772017-08-16 10:13:17 -0500106 def __str__(self):
107 return "EVC-{}: MEN: {}, S-Tag: {}".format(self._name, self._men_ports, self._s_tag)
108
Chip Boling5561d552017-07-07 15:11:26 -0500109 def _create_name(self):
110 #
111 # TODO: Take into account selection criteria and output to make the name
112 #
Chip Boling252c7772017-08-16 10:13:17 -0500113 return EVC_NAME_FORMAT.format(self._flow.flow_id)
Chip Boling7294b252017-06-15 16:16:55 -0500114
Chip Boling5561d552017-07-07 15:11:26 -0500115 @property
116 def name(self):
117 return self._name
Chip Boling7294b252017-06-15 16:16:55 -0500118
119 @property
120 def valid(self):
121 return self._valid
122
123 @property
124 def installed(self):
125 return self._installed
126
127 @property
128 def status(self):
129 return self._status_message
130
Chip Boling5561d552017-07-07 15:11:26 -0500131 @status.setter
132 def status(self, value):
133 self._status_message = value
134
135 @property
136 def s_tag(self):
137 return self._s_tag
138
139 @property
140 def stpid(self):
141 return self._stpid
142
143 @stpid.setter
144 def stpid(self, value):
145 assert self._stpid is None or self._stpid == value
146 self._stpid = value
147
148 @property
149 def switching_method(self):
150 return self._switching_method
151
152 @switching_method.setter
153 def switching_method(self, value):
154 assert self._switching_method is None or self._switching_method == value
155 self._switching_method = value
156
157 @property
158 def ce_vlan_preservation(self):
159 return self._ce_vlan_preservation
160
161 @ce_vlan_preservation.setter
162 def ce_vlan_preservation(self, value):
163 assert self._ce_vlan_preservation is None or self._ce_vlan_preservation == value
Chip Boling252c7772017-08-16 10:13:17 -0500164 self._ce_vlan_preservation = value
Chip Boling5561d552017-07-07 15:11:26 -0500165
166 @property
167 def men_to_uni_tag_manipulation(self):
168 return self._men_to_uni_tag_manipulation
169
170 @men_to_uni_tag_manipulation.setter
171 def men_to_uni_tag_manipulation(self, value):
172 assert self._men_to_uni_tag_manipulation is None or self._men_to_uni_tag_manipulation == value
173 self._men_to_uni_tag_manipulation = value
174
175 @property
176 def flow_entry(self):
177 return self._flow
178
179 @property
180 def evc_maps(self):
181 """
182 Get all EVC Maps that reference this EVC
183 :return: list of EVCMap
184 """
185 return self._evc_maps.values()
186
187 def add_evc_map(self, evc_map):
188 if self._evc_maps is not None:
189 self._evc_maps[evc_map.name] = evc_map
190
191 def remove_evc_map(self, evc_map):
192 if self._evc_maps is not None and evc_map.name in self._evc_maps:
193 del self._evc_maps[evc_map.name]
194
Chip Boling252c7772017-08-16 10:13:17 -0500195 def schedule_install(self):
196 """
197 Try to install EVC and all MAPs in a single operational sequence
198 """
199 if self._valid and self._install_deferred is None:
200 self._install_deferred = reactor.callLater(0, self._do_install)
201
202 return self._install_deferred
203
204 @staticmethod
205 def _xml_header(operation=None):
206 return '<evcs xmlns="http://www.adtran.com/ns/yang/adtran-evcs"><evc{}>'.\
207 format('' if operation is None else ' operation="{}"'.format(operation))
208
209 @staticmethod
210 def _xml_trailer():
211 return '</evc></evcs>'
212
Chip Boling5561d552017-07-07 15:11:26 -0500213 @inlineCallbacks
Chip Boling252c7772017-08-16 10:13:17 -0500214 def _do_install(self):
215 self._install_deferred = None
216
217 # Install the EVC if needed
218
Chip Boling5561d552017-07-07 15:11:26 -0500219 if self._valid and not self._installed:
Chip Boling252c7772017-08-16 10:13:17 -0500220 # TODO: Currently install EVC and then MAPs. Can do it all in a single edit-config operation
221
222 xml = EVC._xml_header()
Chip Boling5561d552017-07-07 15:11:26 -0500223 xml += '<name>{}</name>'.format(self.name)
Chip Boling252c7772017-08-16 10:13:17 -0500224 xml += '<enabled>{}</enabled>'.format('true' if self._enabled else 'false')
225
226 if self._ce_vlan_preservation is not None:
227 xml += '<ce-vlan-preservation>{}</ce-vlan-preservation>'.\
228 format('true' if self._ce_vlan_preservation else 'false')
Chip Boling7294b252017-06-15 16:16:55 -0500229
Chip Boling5561d552017-07-07 15:11:26 -0500230 if self._s_tag is not None:
231 xml += '<stag>{}</stag>'.format(self._s_tag)
232 xml += '<stag-tpid>{:#x}</stag-tpid>'.format(self._stpid or DEFAULT_STPID)
233 else:
234 xml += 'no-stag/'
Chip Boling7294b252017-06-15 16:16:55 -0500235
Chip Boling5561d552017-07-07 15:11:26 -0500236 for port in self._men_ports:
237 xml += '<men-ports>{}</men-ports>'.format(port)
Chip Boling7294b252017-06-15 16:16:55 -0500238
Chip Boling5561d552017-07-07 15:11:26 -0500239 xml += EVC.Men2UniManipulation.xml(self._men_to_uni_tag_manipulation)
240 xml += EVC.SwitchingMethod.xml(self._switching_method)
Chip Boling252c7772017-08-16 10:13:17 -0500241 xml += EVC._xml_trailer()
Chip Boling7294b252017-06-15 16:16:55 -0500242
Chip Boling5561d552017-07-07 15:11:26 -0500243 log.debug("Creating EVC {}: '{}'".format(self.name, xml))
244
245 try:
Chip Boling252c7772017-08-16 10:13:17 -0500246 # Set installed to true while request is in progress
247 self._installed = True
248 results = yield self._flow.handler.netconf_client.edit_config(xml, lock_timeout=30)
Chip Boling5561d552017-07-07 15:11:26 -0500249 self._installed = results.ok
Chip Boling252c7772017-08-16 10:13:17 -0500250
Chip Boling5561d552017-07-07 15:11:26 -0500251 if results.ok:
252 self.status = ''
253 else:
254 self.status = results.error # TODO: Save off error status
255
256 except Exception as e:
257 log.exception('Failed to install EVC', name=self.name, e=e)
258 raise
259
Chip Boling252c7772017-08-16 10:13:17 -0500260 # Install any associated EVC Maps
261
262 if self._installed:
263 for evc_map in self.evc_maps:
264 try:
265 results = yield evc_map.install()
266 pass # TODO: What to do on error?
267
268 except Exception as e:
269 evc_map.status = 'Exception during EVC-MAP Install: {}'.format(e.message)
270 log.exception(evc_map.status, e=e)
271
Chip Boling5561d552017-07-07 15:11:26 -0500272 returnValue(self._installed and self._valid)
273
274 @inlineCallbacks
Chip Boling7294b252017-06-15 16:16:55 -0500275 def remove(self):
Chip Boling252c7772017-08-16 10:13:17 -0500276 d, self._install_deferred = self._install_deferred, None
277 if d is not None:
278 d.cancel()
Chip Boling7294b252017-06-15 16:16:55 -0500279
Chip Boling252c7772017-08-16 10:13:17 -0500280 if self._installed:
281 xml = EVC._xml_header('delete') + '<name>{}</name>'.format(self.name) + EVC._xml_trailer()
282
283 log.debug('removing', evc=self.name, xml=xml)
Chip Boling5561d552017-07-07 15:11:26 -0500284
285 try:
Chip Boling252c7772017-08-16 10:13:17 -0500286 results = yield self._flow.handler.netconf_client.edit_config(xml, lock_timeout=30)
Chip Boling5561d552017-07-07 15:11:26 -0500287 self._installed = not results.ok
288 if results.ok:
289 self.status = ''
290 else:
291 self.status = results.error # TODO: Save off error status
292
293 except Exception as e:
Chip Boling252c7772017-08-16 10:13:17 -0500294 log.exception('removing', name=self.name, e=e)
Chip Boling5561d552017-07-07 15:11:26 -0500295 raise
296
297 # TODO: Do we remove evc-maps as well reference here or maybe have a 'delete' function?
Chip Boling7294b252017-06-15 16:16:55 -0500298 pass
299
Chip Boling5561d552017-07-07 15:11:26 -0500300 returnValue(not self._installed)
Chip Boling7294b252017-06-15 16:16:55 -0500301
Chip Boling5561d552017-07-07 15:11:26 -0500302 @inlineCallbacks
Chip Boling7294b252017-06-15 16:16:55 -0500303 def enable(self):
Chip Boling5561d552017-07-07 15:11:26 -0500304 if self.installed and not self._enabled:
Chip Boling252c7772017-08-16 10:13:17 -0500305 xml = EVC._xml_header() + '<name>{}</name>'.format(self.name)
306 xml += '<enabled>true</enabled>' + EVC._xml_trailer()
Chip Boling7294b252017-06-15 16:16:55 -0500307
Chip Boling252c7772017-08-16 10:13:17 -0500308 log.debug('enabling', evc=self.name, xml=xml)
Chip Boling5561d552017-07-07 15:11:26 -0500309
310 try:
Chip Boling252c7772017-08-16 10:13:17 -0500311 results = yield self._flow.handler.netconf_client.edit_config(xml, lock_timeout=30)
Chip Boling5561d552017-07-07 15:11:26 -0500312 self._enabled = results.ok
313 if results.ok:
314 self.status = ''
315 else:
316 self.status = results.error # TODO: Save off error status
317
318 except Exception as e:
Chip Boling252c7772017-08-16 10:13:17 -0500319 log.exception('enabling', name=self.name, e=e)
Chip Boling5561d552017-07-07 15:11:26 -0500320 raise
321
322 returnValue(self.installed and self._enabled)
323
324 @inlineCallbacks
Chip Boling7294b252017-06-15 16:16:55 -0500325 def disable(self):
Chip Boling5561d552017-07-07 15:11:26 -0500326 if self.installed and self._enabled:
Chip Boling252c7772017-08-16 10:13:17 -0500327 xml = EVC._xml_header() + '<name>{}</name>'.format(self.name)
328 xml += '<enabled>false</enabled>' + EVC._xml_trailer()
Chip Boling5561d552017-07-07 15:11:26 -0500329
Chip Boling252c7772017-08-16 10:13:17 -0500330 log.debug('disabling', evc=self.name, xml=xml)
Chip Boling5561d552017-07-07 15:11:26 -0500331
332 try:
Chip Boling252c7772017-08-16 10:13:17 -0500333 results = yield self._flow.handler.netconf_client.edit_config(xml, lock_timeout=30)
Chip Boling5561d552017-07-07 15:11:26 -0500334 self._enabled = not results.ok
335 if results.ok:
336 self.status = ''
337 else:
338 self.status = results.error # TODO: Save off error status
339
340 except Exception as e:
Chip Boling252c7772017-08-16 10:13:17 -0500341 log.exception('disabling', name=self.name, e=e)
Chip Boling5561d552017-07-07 15:11:26 -0500342 raise
343
344 returnValue(self.installed and not self._enabled)
345
346 @inlineCallbacks
347 def delete(self):
348 """
349 Remove from hardware and delete/clean-up
350 """
351 try:
352 self._valid = False
353 succeeded = yield self.remove()
354 # TODO: On timeout or other NETCONF error, should we schedule cleanup later?
355
356 except Exception:
357 succeeded = False
358
359 finally:
360 self._flow = None
361 self._evc_maps = None
362
363 returnValue(succeeded)
Chip Boling7294b252017-06-15 16:16:55 -0500364
365 def _decode(self):
366 """
Chip Boling5561d552017-07-07 15:11:26 -0500367 Examine flow rules and extract appropriate settings for this EVC
Chip Boling7294b252017-06-15 16:16:55 -0500368 """
Chip Boling5561d552017-07-07 15:11:26 -0500369 if self._flow.handler.is_nni_port(self._flow.in_port):
370 self._men_ports.append(self._flow.handler.get_port_name(self._flow.in_port))
Chip Boling7294b252017-06-15 16:16:55 -0500371 else:
Chip Boling5561d552017-07-07 15:11:26 -0500372 self._status_message = 'EVCs with UNI ports are not supported'
373 return False # UNI Ports handled in the EVC Maps
Chip Boling7294b252017-06-15 16:16:55 -0500374
Chip Boling5561d552017-07-07 15:11:26 -0500375 self._s_tag = self._flow.vlan_id
Chip Boling7294b252017-06-15 16:16:55 -0500376
Chip Boling252c7772017-08-16 10:13:17 -0500377 # if self._flow.inner_vid is not None:
378 # self._switching_method = EVC.SwitchingMethod.DOUBLE_TAGGED TODO: Future support
Chip Boling7294b252017-06-15 16:16:55 -0500379
Chip Boling5561d552017-07-07 15:11:26 -0500380 # Note: The following fields will get set when the first EVC-MAP
381 # is associated with this object. Once set, they cannot be changed to
382 # another value.
383 # self._stpid
384 # self._switching_method
385 # self._ce_vlan_preservation
386 # self._men_to_uni_tag_manipulation
Chip Boling7294b252017-06-15 16:16:55 -0500387 return True
388
389 # BULK operations
390
391 @staticmethod
Chip Boling252c7772017-08-16 10:13:17 -0500392 def remove_all(client, regex_=EVC_NAME_REGEX_ALL):
Chip Boling7294b252017-06-15 16:16:55 -0500393 """
Chip Boling252c7772017-08-16 10:13:17 -0500394 Remove all matching EVCs from hardware
395 :param client: (ncclient) NETCONF Client to use
Chip Boling7294b252017-06-15 16:16:55 -0500396 :param regex_: (String) Regular expression for name matching
Chip Boling252c7772017-08-16 10:13:17 -0500397 :return: (deferred)
Chip Boling7294b252017-06-15 16:16:55 -0500398 """
Chip Boling252c7772017-08-16 10:13:17 -0500399 # Do a 'get' on the evc config an you should get the names
400 get_xml = """
401 <filter xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
402 <evcs xmlns="http://www.adtran.com/ns/yang/adtran-evcs">
403 <evc><name/></evc>
404 </evcs>
405 </filter>
406 """
407 log.debug('query', xml=get_xml)
Chip Boling7294b252017-06-15 16:16:55 -0500408
Chip Boling252c7772017-08-16 10:13:17 -0500409 def request_failed(results, operation):
410 log.error('{}-failed'.format(operation), results=results)
411 # No further actions. Periodic poll later on will scrub any old EVCs if needed
412
413 def delete_complete(results):
414 log.debug('delete-complete', results=results)
415
416 def do_delete(rpc_reply, regexpr):
417 log.debug('query-complete', rpc_reply=rpc_reply)
418
419 if rpc_reply.ok:
420 result_dict = xmltodict.parse(rpc_reply.data_xml)
421 entries = result_dict['data']['evcs'] if 'evcs' in result_dict['data'] else {}
422
423 if 'evc' in entries:
424 p = re.compile(regexpr)
425
426 if isinstance(entries['evc'], list):
427 names = {entry['name'] for entry in entries['evc'] if 'name' in entry
428 and p.match(entry['name'])}
429 else:
430 names = set()
431 for item in entries['evc-map'].items():
432 if isinstance(item, tuple) and item[0] == 'name':
433 names.add(item[1])
434 break
435
436 if len(names) > 0:
437 del_xml = EVC._xml_header('delete')
438 for name in names:
439 del_xml += '<name>{}</name>'.format(name)
440 del_xml += EVC._xml_trailer()
441
442 log.debug('removing', xml=del_xml)
443 return client.edit_config(del_xml, lock_timeout=30)
444
445 return succeed('no entries')
446
447 d = client.get(get_xml)
448 d.addCallbacks(do_delete, request_failed, callbackArgs=[regex_], errbackArgs=['get'])
449 d.addCallbacks(delete_complete, request_failed, errbackArgs=['edit-config'])
450 return d