blob: a740d8274aa4c5b8dec6ca93141324c310a404c8 [file] [log] [blame]
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001#
2# Copyright 2017 the original author or authors.
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 implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17"""
18Broadcom OpenOMCI OLT/ONU adapter handler.
19"""
20
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050021import ast
22import structlog
23
24from collections import OrderedDict
25
26from twisted.internet import reactor, task
27from twisted.internet.defer import DeferredQueue, inlineCallbacks, returnValue, TimeoutError
28
29from heartbeat import HeartBeat
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050030from pyvoltha.adapters.extensions.kpi.onu.onu_pm_metrics import OnuPmMetrics
31from pyvoltha.adapters.extensions.kpi.onu.onu_omci_pm import OnuOmciPmMetrics
32from pyvoltha.adapters.extensions.alarms.adapter_alarms import AdapterAlarms
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050033
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050034import pyvoltha.common.openflow.utils as fd
35from pyvoltha.common.utils.registry import registry
36from pyvoltha.common.config.config_backend import ConsulStore
37from pyvoltha.common.config.config_backend import EtcdStore
William Kurkian8235c1e2019-03-05 12:58:28 -050038from voltha_protos.common_pb2 import OperStatus, ConnectStatus, AdminState
Matt Jeanneretc083f462019-03-11 15:02:01 -040039from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, ofp_port, OFPPS_LIVE, OFPPF_FIBER, OFPPF_1GB_FD
Matt Jeanneret3bfebff2019-04-12 18:25:03 -040040from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
41 InterAdapterOmciMessage, PortCapability, InterAdapterTechProfileDownloadMessage
Matt Jeannereta32441c2019-03-07 05:16:37 -050042from voltha_protos.openolt_pb2 import OnuIndication
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050043from pyvoltha.adapters.extensions.omci.onu_configuration import OMCCVersion
44from pyvoltha.adapters.extensions.omci.onu_device_entry import OnuDeviceEvents, \
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050045 OnuDeviceEntry, IN_SYNC_KEY
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050046from omci.brcm_mib_download_task import BrcmMibDownloadTask
47from omci.brcm_tp_service_specific_task import BrcmTpServiceSpecificTask
48from omci.brcm_uni_lock_task import BrcmUniLockTask
49from omci.brcm_vlan_filter_task import BrcmVlanFilterTask
50from onu_gem_port import *
51from onu_tcont import *
52from pon_port import *
53from uni_port import *
54from onu_traffic_descriptor import *
55from pyvoltha.common.tech_profile.tech_profile import TechProfile
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050056
57OP = EntityOperations
58RC = ReasonCodes
59
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050060log = structlog.get_logger()
61
62_STARTUP_RETRY_WAIT = 20
63
64
65class BrcmOpenomciOnuHandler(object):
66
67 def __init__(self, adapter, device_id):
68 self.log = structlog.get_logger(device_id=device_id)
69 self.log.debug('function-entry')
70 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050071 self.core_proxy = adapter.core_proxy
72 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050073 self.parent_adapter = None
74 self.parent_id = None
75 self.device_id = device_id
76 self.incoming_messages = DeferredQueue()
77 self.event_messages = DeferredQueue()
78 self.proxy_address = None
79 self.tx_id = 0
80 self._enabled = False
81 self.alarms = None
82 self.pm_metrics = None
83 self._omcc_version = OMCCVersion.Unknown
84 self._total_tcont_count = 0 # From ANI-G ME
85 self._qos_flexibility = 0 # From ONT2_G ME
86
87 self._onu_indication = None
88 self._unis = dict() # Port # -> UniPort
89
90 self._pon = None
91 # TODO: probably shouldnt be hardcoded, determine from olt maybe?
92 self._pon_port_number = 100
93 self.logical_device_id = None
94
95 self._heartbeat = HeartBeat.create(self, device_id)
96
97 # Set up OpenOMCI environment
98 self._onu_omci_device = None
99 self._dev_info_loaded = False
100 self._deferred = None
101
102 self._in_sync_subscription = None
103 self._connectivity_subscription = None
104 self._capabilities_subscription = None
105
106 self.mac_bridge_service_profile_entity_id = 0x201
107 self.gal_enet_profile_entity_id = 0x1
108
109 self._tp_service_specific_task = dict()
110 self._tech_profile_download_done = dict()
111
112 # Initialize KV store client
113 self.args = registry('main').get_args()
114 if self.args.backend == 'etcd':
115 host, port = self.args.etcd.split(':', 1)
116 self.kv_client = EtcdStore(host, port,
117 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
118 elif self.args.backend == 'consul':
119 host, port = self.args.consul.split(':', 1)
120 self.kv_client = ConsulStore(host, port,
121 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
122 else:
123 self.log.error('Invalid-backend')
124 raise Exception("Invalid-backend-for-kv-store")
125
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500126 @property
127 def enabled(self):
128 return self._enabled
129
130 @enabled.setter
131 def enabled(self, value):
132 if self._enabled != value:
133 self._enabled = value
134
135 @property
136 def omci_agent(self):
137 return self.adapter.omci_agent
138
139 @property
140 def omci_cc(self):
141 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
142
143 @property
144 def heartbeat(self):
145 return self._heartbeat
146
147 @property
148 def uni_ports(self):
149 return self._unis.values()
150
151 def uni_port(self, port_no_or_name):
152 if isinstance(port_no_or_name, (str, unicode)):
153 return next((uni for uni in self.uni_ports
154 if uni.name == port_no_or_name), None)
155
156 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
157 return next((uni for uni in self.uni_ports
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400158 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500159
160 @property
161 def pon_port(self):
162 return self._pon
163
164 def receive_message(self, msg):
165 if self.omci_cc is not None:
166 self.omci_cc.receive_message(msg)
167
Matt Jeanneretc083f462019-03-11 15:02:01 -0400168 def get_ofp_port_info(self, device, port_no):
169 self.log.info('get_ofp_port_info', port_no=port_no, device_id=device.id)
170 cap = OFPPF_1GB_FD | OFPPF_FIBER
171
172 hw_addr=mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
173 ((device.parent_port_no >> 8 & 0xff),
174 device.parent_port_no & 0xff,
175 (port_no >> 16) & 0xff,
176 (port_no >> 8) & 0xff,
177 port_no & 0xff))
178
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400179 uni_port = self.uni_port(int(port_no))
180 name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
181 self.log.debug('ofp_port_name', port_no=port_no, name=name)
182
Matt Jeanneretc083f462019-03-11 15:02:01 -0400183 return PortCapability(
184 port=LogicalPort(
185 ofp_port=ofp_port(
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400186 name=name,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400187 hw_addr=hw_addr,
188 config=0,
189 state=OFPPS_LIVE,
190 curr=cap,
191 advertised=cap,
192 peer=cap,
193 curr_speed=OFPPF_1GB_FD,
194 max_speed=OFPPF_1GB_FD
195 ),
196 device_id=device.id,
197 device_port_no=port_no
198 )
199 )
200
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500201 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500202 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500203 def activate(self, device):
204 self.log.debug('function-entry', device=device)
205
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500206 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500207 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500208 assert device.proxy_address.device_id
209
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500210 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500211 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500212 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500213 if self.enabled is not True:
214 self.log.info('activating-new-onu')
215 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500216 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500217 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500218 device.reason = 'activating-onu'
219
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500220 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400221 # pm_metrics requires a logical device id. For now set to just device_id
222 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500223
Matt Jeannereta32441c2019-03-07 05:16:37 -0500224 yield self.core_proxy.device_update(device)
225
226 yield self.core_proxy.device_state_update(device.id, oper_status=OperStatus.DISCOVERED,
227 connect_status=ConnectStatus.REACHABLE)
228
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500229
230 self.log.debug('set-device-discovered')
231
232 self._init_pon_state(device)
233
234 ############################################################################
235 # Setup PM configuration for this device
236 # Pass in ONU specific options
237 kwargs = {
238 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
239 'heartbeat': self.heartbeat,
240 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
241 }
Matt Jeannereta32441c2019-03-07 05:16:37 -0500242 self.pm_metrics = OnuPmMetrics(self.core_proxy, self.device_id,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500243 self.logical_device_id, grouped=True,
244 freq_override=False, **kwargs)
245 pm_config = self.pm_metrics.make_proto()
246 self._onu_omci_device.set_pm_config(self.pm_metrics.omci_pm.openomci_interval_pm)
247 self.log.info("initial-pm-config", pm_config=pm_config)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500248 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500249
250 ############################################################################
251 # Setup Alarm handler
Matt Jeannereta32441c2019-03-07 05:16:37 -0500252 self.alarms = AdapterAlarms(self.core_proxy, device.id, self.logical_device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500253 # Note, ONU ID and UNI intf set in add_uni_port method
254 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.alarms,
255 ani_ports=[self._pon])
256 self.enabled = True
257 else:
258 self.log.info('onu-already-activated')
259
260 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400261 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500262 def reconcile(self, device):
263 self.log.debug('function-entry', device=device)
264
265 # first we verify that we got parent reference and proxy info
266 assert device.parent_id
267 assert device.proxy_address.device_id
268
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500269 if self.enabled is not True:
270 self.log.info('reconciling-broadcom-onu-device')
271
272 self._init_pon_state(device)
273
274 # need to restart state machines on vcore restart. there is no indication to do it for us.
275 self._onu_omci_device.start()
276 device.reason = "restarting-openomci"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400277 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500278
279 # TODO: this is probably a bit heavy handed
280 # Force a reboot for now. We need indications to reflow to reassign tconts and gems given vcore went away
281 # This may not be necessary when mib resync actually works
282 reactor.callLater(1, self.reboot)
283
284 self.enabled = True
285 else:
286 self.log.info('onu-already-activated')
287
288 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500289 def _init_pon_state(self, device):
290 self.log.debug('function-entry', device=device)
291
292 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500293 self._pon.add_peer(self.parent_id, self._pon_port_number)
294 self.log.debug('adding-pon-port-to-agent', pon=self._pon.get_port())
295
Matt Jeannereta32441c2019-03-07 05:16:37 -0500296 yield self.core_proxy.port_created(device.id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500297
Matt Jeanneret0c287892019-02-28 11:48:00 -0500298 self.log.debug('added-pon-port-to-agent', pon=self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500299
300 # Create and start the OpenOMCI ONU Device Entry for this ONU
301 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500302 self.core_proxy,
303 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500304 support_classes=self.adapter.broadcom_omci,
305 custom_me_map=self.adapter.custom_me_entities())
306 # Port startup
307 if self._pon is not None:
308 self._pon.enabled = True
309
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500310 def delete(self, device):
311 self.log.info('delete-onu', device=device)
312 if self.parent_adapter:
313 try:
314 self.parent_adapter.delete_child_device(self.parent_id, device)
315 except AttributeError:
316 self.log.debug('parent-device-delete-child-not-implemented')
317 else:
318 self.log.debug("parent-adapter-not-available")
319
320 def _create_tconts(self, uni_id, us_scheduler):
321 alloc_id = us_scheduler['alloc_id']
322 q_sched_policy = us_scheduler['q_sched_policy']
323 self.log.debug('create-tcont', us_scheduler=us_scheduler)
324
325 tcontdict = dict()
326 tcontdict['alloc-id'] = alloc_id
327 tcontdict['q_sched_policy'] = q_sched_policy
328 tcontdict['uni_id'] = uni_id
329
330 # TODO: Not sure what to do with any of this...
331 tddata = dict()
332 tddata['name'] = 'not-sure-td-profile'
333 tddata['fixed-bandwidth'] = "not-sure-fixed"
334 tddata['assured-bandwidth'] = "not-sure-assured"
335 tddata['maximum-bandwidth'] = "not-sure-max"
336 tddata['additional-bw-eligibility-indicator'] = "not-sure-additional"
337
338 td = OnuTrafficDescriptor.create(tddata)
339 tcont = OnuTCont.create(self, tcont=tcontdict, td=td)
340
341 self._pon.add_tcont(tcont)
342
343 self.log.debug('pon-add-tcont', tcont=tcont)
344
345 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
346 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
347 self.log.debug('create-gemport',
348 gem_ports=gem_ports, direction=direction)
349
350 for gem_port in gem_ports:
351 gemdict = dict()
352 gemdict['gemport_id'] = gem_port['gemport_id']
353 gemdict['direction'] = direction
354 gemdict['alloc_id_ref'] = alloc_id_ref
355 gemdict['encryption'] = gem_port['aes_encryption']
356 gemdict['discard_config'] = dict()
357 gemdict['discard_config']['max_probability'] = \
358 gem_port['discard_config']['max_probability']
359 gemdict['discard_config']['max_threshold'] = \
360 gem_port['discard_config']['max_threshold']
361 gemdict['discard_config']['min_threshold'] = \
362 gem_port['discard_config']['min_threshold']
363 gemdict['discard_policy'] = gem_port['discard_policy']
364 gemdict['max_q_size'] = gem_port['max_q_size']
365 gemdict['pbit_map'] = gem_port['pbit_map']
366 gemdict['priority_q'] = gem_port['priority_q']
367 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
368 gemdict['weight'] = gem_port['weight']
369 gemdict['uni_id'] = uni_id
370
371 gem_port = OnuGemPort.create(self, gem_port=gemdict)
372
373 self._pon.add_gem_port(gem_port)
374
375 self.log.debug('pon-add-gemport', gem_port=gem_port)
376
377 def _do_tech_profile_configuration(self, uni_id, tp):
378 num_of_tconts = tp['num_of_tconts']
379 us_scheduler = tp['us_scheduler']
380 alloc_id = us_scheduler['alloc_id']
381 self._create_tconts(uni_id, us_scheduler)
382 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
383 self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
384 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
385 self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
386
387 def load_and_configure_tech_profile(self, uni_id, tp_path):
388 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
389
390 if uni_id not in self._tp_service_specific_task:
391 self._tp_service_specific_task[uni_id] = dict()
392
393 if uni_id not in self._tech_profile_download_done:
394 self._tech_profile_download_done[uni_id] = dict()
395
396 if tp_path not in self._tech_profile_download_done[uni_id]:
397 self._tech_profile_download_done[uni_id][tp_path] = False
398
399 if not self._tech_profile_download_done[uni_id][tp_path]:
400 try:
401 if tp_path in self._tp_service_specific_task[uni_id]:
402 self.log.info("tech-profile-config-already-in-progress",
403 tp_path=tp_path)
404 return
405
406 tp = self.kv_client[tp_path]
407 tp = ast.literal_eval(tp)
408 self.log.debug("tp-instance", tp=tp)
409 self._do_tech_profile_configuration(uni_id, tp)
William Kurkian3a206332019-04-29 11:05:47 -0400410
411 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500412 def success(_results):
413 self.log.info("tech-profile-config-done-successfully")
William Kurkian3a206332019-04-29 11:05:47 -0400414 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500415 device.reason = 'tech-profile-config-download-success'
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400416 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500417 if tp_path in self._tp_service_specific_task[uni_id]:
418 del self._tp_service_specific_task[uni_id][tp_path]
419 self._tech_profile_download_done[uni_id][tp_path] = True
420
William Kurkian3a206332019-04-29 11:05:47 -0400421 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500422 def failure(_reason):
423 self.log.warn('tech-profile-config-failure-retrying',
424 _reason=_reason)
William Kurkian3a206332019-04-29 11:05:47 -0400425 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500426 device.reason = 'tech-profile-config-download-failure-retrying'
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400427 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500428 if tp_path in self._tp_service_specific_task[uni_id]:
429 del self._tp_service_specific_task[uni_id][tp_path]
430 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self.load_and_configure_tech_profile,
431 uni_id, tp_path)
432
433 self.log.info('downloading-tech-profile-configuration')
434 self._tp_service_specific_task[uni_id][tp_path] = \
435 BrcmTpServiceSpecificTask(self.omci_agent, self, uni_id)
436 self._deferred = \
437 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
438 self._deferred.addCallbacks(success, failure)
439
440 except Exception as e:
441 self.log.exception("error-loading-tech-profile", e=e)
442 else:
443 self.log.info("tech-profile-config-already-done")
444
445 def update_pm_config(self, device, pm_config):
446 # TODO: This has not been tested
447 self.log.info('update_pm_config', pm_config=pm_config)
448 self.pm_metrics.update(pm_config)
449
450 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
451 # flow decomposition that ultimately comes from onos
452 def update_flow_table(self, device, flows):
453 self.log.debug('function-entry', device=device, flows=flows)
454
455 #
456 # We need to proxy through the OLT to get to the ONU
457 # Configuration from here should be using OMCI
458 #
459 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
460
461 # no point in pushing omci flows if the device isnt reachable
462 if device.connect_status != ConnectStatus.REACHABLE or \
463 device.admin_state != AdminState.ENABLED:
464 self.log.warn("device-disabled-or-offline-skipping-flow-update",
465 admin=device.admin_state, connect=device.connect_status)
466 return
467
468 def is_downstream(port):
469 return port == self._pon_port_number
470
471 def is_upstream(port):
472 return not is_downstream(port)
473
474 for flow in flows:
475 _type = None
476 _port = None
477 _vlan_vid = None
478 _udp_dst = None
479 _udp_src = None
480 _ipv4_dst = None
481 _ipv4_src = None
482 _metadata = None
483 _output = None
484 _push_tpid = None
485 _field = None
486 _set_vlan_vid = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400487 _tunnel_id = None
488
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500489 self.log.debug('bulk-flow-update', device_id=device.id, flow=flow)
490 try:
491 _in_port = fd.get_in_port(flow)
492 assert _in_port is not None
493
494 _out_port = fd.get_out_port(flow) # may be None
495
496 if is_downstream(_in_port):
497 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
498 uni_port = self.uni_port(_out_port)
499 elif is_upstream(_in_port):
500 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
501 uni_port = self.uni_port(_in_port)
502 else:
503 raise Exception('port should be 1 or 2 by our convention')
504
505 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
506
507 for field in fd.get_ofb_fields(flow):
508 if field.type == fd.ETH_TYPE:
509 _type = field.eth_type
510 self.log.debug('field-type-eth-type',
511 eth_type=_type)
512
513 elif field.type == fd.IP_PROTO:
514 _proto = field.ip_proto
515 self.log.debug('field-type-ip-proto',
516 ip_proto=_proto)
517
518 elif field.type == fd.IN_PORT:
519 _port = field.port
520 self.log.debug('field-type-in-port',
521 in_port=_port)
522
523 elif field.type == fd.VLAN_VID:
524 _vlan_vid = field.vlan_vid & 0xfff
525 self.log.debug('field-type-vlan-vid',
526 vlan=_vlan_vid)
527
528 elif field.type == fd.VLAN_PCP:
529 _vlan_pcp = field.vlan_pcp
530 self.log.debug('field-type-vlan-pcp',
531 pcp=_vlan_pcp)
532
533 elif field.type == fd.UDP_DST:
534 _udp_dst = field.udp_dst
535 self.log.debug('field-type-udp-dst',
536 udp_dst=_udp_dst)
537
538 elif field.type == fd.UDP_SRC:
539 _udp_src = field.udp_src
540 self.log.debug('field-type-udp-src',
541 udp_src=_udp_src)
542
543 elif field.type == fd.IPV4_DST:
544 _ipv4_dst = field.ipv4_dst
545 self.log.debug('field-type-ipv4-dst',
546 ipv4_dst=_ipv4_dst)
547
548 elif field.type == fd.IPV4_SRC:
549 _ipv4_src = field.ipv4_src
550 self.log.debug('field-type-ipv4-src',
551 ipv4_dst=_ipv4_src)
552
553 elif field.type == fd.METADATA:
554 _metadata = field.table_metadata
555 self.log.debug('field-type-metadata',
556 metadata=_metadata)
557
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400558 elif field.type == fd.TUNNEL_ID:
559 _tunnel_id = field.tunnel_id
560 self.log.debug('field-type-tunnel-id',
561 tunnel_id=_tunnel_id)
562
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500563 else:
564 raise NotImplementedError('field.type={}'.format(
565 field.type))
566
567 for action in fd.get_actions(flow):
568
569 if action.type == fd.OUTPUT:
570 _output = action.output.port
571 self.log.debug('action-type-output',
572 output=_output, in_port=_in_port)
573
574 elif action.type == fd.POP_VLAN:
575 self.log.debug('action-type-pop-vlan',
576 in_port=_in_port)
577
578 elif action.type == fd.PUSH_VLAN:
579 _push_tpid = action.push.ethertype
580 self.log.debug('action-type-push-vlan',
581 push_tpid=_push_tpid, in_port=_in_port)
582 if action.push.ethertype != 0x8100:
583 self.log.error('unhandled-tpid',
584 ethertype=action.push.ethertype)
585
586 elif action.type == fd.SET_FIELD:
587 _field = action.set_field.field.ofb_field
588 assert (action.set_field.field.oxm_class ==
589 OFPXMC_OPENFLOW_BASIC)
590 self.log.debug('action-type-set-field',
591 field=_field, in_port=_in_port)
592 if _field.type == fd.VLAN_VID:
593 _set_vlan_vid = _field.vlan_vid & 0xfff
594 self.log.debug('set-field-type-vlan-vid',
595 vlan_vid=_set_vlan_vid)
596 else:
597 self.log.error('unsupported-action-set-field-type',
598 field_type=_field.type)
599 else:
600 self.log.error('unsupported-action-type',
601 action_type=action.type, in_port=_in_port)
602
603 # TODO: We only set vlan omci flows. Handle omci matching ethertypes at some point in another task
604 if _type is not None:
605 self.log.warn('ignoring-flow-with-ethType', ethType=_type)
606 elif _set_vlan_vid is None or _set_vlan_vid == 0:
607 self.log.warn('ignorning-flow-that-does-not-set-vlanid')
608 else:
609 self.log.warn('set-vlanid', uni_id=uni_port.port_number, set_vlan_vid=_set_vlan_vid)
610 self._add_vlan_filter_task(device, uni_port, _set_vlan_vid)
611
612 except Exception as e:
613 self.log.exception('failed-to-install-flow', e=e, flow=flow)
614
615
616 def _add_vlan_filter_task(self, device, uni_port, _set_vlan_vid):
617 assert uni_port is not None
618
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400619 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500620 def success(_results):
621 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid)
622 device.reason = 'omci-flows-pushed'
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400623 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500624 self._vlan_filter_task = None
625
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400626 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500627 def failure(_reason):
628 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid)
629 device.reason = 'omci-flows-failed-retrying'
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400630 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500631 self._vlan_filter_task = reactor.callLater(_STARTUP_RETRY_WAIT,
632 self._add_vlan_filter_task, device, uni_port, _set_vlan_vid)
633
634 self.log.info('setting-vlan-tag')
635 self._vlan_filter_task = BrcmVlanFilterTask(self.omci_agent, self.device_id, uni_port, _set_vlan_vid)
636 self._deferred = self._onu_omci_device.task_runner.queue_task(self._vlan_filter_task)
637 self._deferred.addCallbacks(success, failure)
638
639 def get_tx_id(self):
640 self.log.debug('function-entry')
641 self.tx_id += 1
642 return self.tx_id
643
Matt Jeannereta32441c2019-03-07 05:16:37 -0500644 def process_inter_adapter_message(self, request):
645 self.log.debug('process-inter-adapter-message', msg=request)
646 try:
647 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
648 omci_msg = InterAdapterOmciMessage()
649 request.body.Unpack(omci_msg)
650 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500651
Matt Jeannereta32441c2019-03-07 05:16:37 -0500652 self.receive_message(omci_msg.message)
653
654 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
655 onu_indication = OnuIndication()
656 request.body.Unpack(onu_indication)
657 self.log.debug('inter-adapter-recv-onu-ind', onu_indication=onu_indication)
658
659 if onu_indication.oper_state == "up":
660 self.create_interface(onu_indication)
661 elif onu_indication.oper_state == "down":
662 self.update_interface(onu_indication)
663 else:
664 self.log.error("unknown-onu-indication", onu_indication=onu_indication)
665
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400666 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
667 tech_msg = InterAdapterTechProfileDownloadMessage()
668 request.body.Unpack(tech_msg)
669 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
670
671 self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
672
Matt Jeannereta32441c2019-03-07 05:16:37 -0500673 else:
674 self.log.error("inter-adapter-unhandled-type", request=request)
675
676 except Exception as e:
677 self.log.exception("error-processing-inter-adapter-message", e=e)
678
679 # Called each time there is an onu "up" indication from the olt handler
680 @inlineCallbacks
681 def create_interface(self, onu_indication):
682 self.log.debug('function-entry', onu_indication=onu_indication)
683 self._onu_indication = onu_indication
684
Matt Jeanneretc083f462019-03-11 15:02:01 -0400685 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
686 connect_status=ConnectStatus.REACHABLE)
687
Matt Jeannereta32441c2019-03-07 05:16:37 -0500688 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500689
690 self.log.debug('starting-openomci-statemachine')
691 self._subscribe_to_events()
692 reactor.callLater(1, self._onu_omci_device.start)
693 onu_device.reason = "starting-openomci"
Matt Jeannereta32441c2019-03-07 05:16:37 -0500694 yield self.core_proxy.device_update(onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500695 self._heartbeat.enabled = True
696
697 # Currently called each time there is an onu "down" indication from the olt handler
698 # TODO: possibly other reasons to "update" from the olt?
Matt Jeannereta32441c2019-03-07 05:16:37 -0500699 @inlineCallbacks
700 def update_interface(self, onu_indication):
701 self.log.debug('function-entry', onu_indication=onu_indication)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500702
Matt Jeannereta32441c2019-03-07 05:16:37 -0500703 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500704
Matt Jeannereta32441c2019-03-07 05:16:37 -0500705 if onu_indication.oper_state == 'down':
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500706 self.log.debug('stopping-openomci-statemachine')
707 reactor.callLater(0, self._onu_omci_device.stop)
708
709 # Let TP download happen again
710 for uni_id in self._tp_service_specific_task:
711 self._tp_service_specific_task[uni_id].clear()
712 for uni_id in self._tech_profile_download_done:
713 self._tech_profile_download_done[uni_id].clear()
714
715 self.disable_ports(onu_device)
716 onu_device.reason = "stopping-openomci"
717 onu_device.connect_status = ConnectStatus.UNREACHABLE
718 onu_device.oper_status = OperStatus.DISCOVERED
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400719 yield self.core_proxy.device_update(onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500720 else:
721 self.log.debug('not-changing-openomci-statemachine')
722
723 # Not currently called by olt or anything else
William Kurkian3a206332019-04-29 11:05:47 -0400724 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500725 def remove_interface(self, data):
726 self.log.debug('function-entry', data=data)
727
William Kurkian3a206332019-04-29 11:05:47 -0400728 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500729
730 self.log.debug('stopping-openomci-statemachine')
731 reactor.callLater(0, self._onu_omci_device.stop)
732
733 # Let TP download happen again
734 for uni_id in self._tp_service_specific_task:
735 self._tp_service_specific_task[uni_id].clear()
736 for uni_id in self._tech_profile_download_done:
737 self._tech_profile_download_done[uni_id].clear()
738
739 self.disable_ports(onu_device)
740 onu_device.reason = "stopping-openomci"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400741 yield self.core_proxy.device_update(onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500742
743 # TODO: im sure there is more to do here
744
745 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400746 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500747 def remove_gemport(self, data):
748 self.log.debug('remove-gemport', data=data)
William Kurkian3a206332019-04-29 11:05:47 -0400749 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500750 if device.connect_status != ConnectStatus.REACHABLE:
751 self.log.error('device-unreachable')
752 return
753
754 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400755 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500756 def remove_tcont(self, tcont_data, traffic_descriptor_data):
757 self.log.debug('remove-tcont', tcont_data=tcont_data, traffic_descriptor_data=traffic_descriptor_data)
William Kurkian3a206332019-04-29 11:05:47 -0400758 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500759 if device.connect_status != ConnectStatus.REACHABLE:
760 self.log.error('device-unreachable')
761 return
762
763 # TODO: Create some omci task that encompases this what intended
764
765 # Not currently called. Would be called presumably from the olt handler
766 def create_multicast_gemport(self, data):
767 self.log.debug('function-entry', data=data)
768
769 # TODO: create objects and populate for later omci calls
770
771 def disable(self, device):
772 self.log.debug('function-entry', device=device)
773 try:
774 self.log.info('sending-uni-lock-towards-device', device=device)
775
Matt Jeanneret80766692019-05-03 09:58:38 -0400776 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500777 def stop_anyway(reason):
778 # proceed with disable regardless if we could reach the onu. for example onu is unplugged
779 self.log.debug('stopping-openomci-statemachine')
780 reactor.callLater(0, self._onu_omci_device.stop)
781
782 # Let TP download happen again
783 for uni_id in self._tp_service_specific_task:
784 self._tp_service_specific_task[uni_id].clear()
785 for uni_id in self._tech_profile_download_done:
786 self._tech_profile_download_done[uni_id].clear()
787
788 self.disable_ports(device)
789 device.oper_status = OperStatus.UNKNOWN
790 device.reason = "omci-admin-lock"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400791 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500792
793 # lock all the unis
794 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=True)
795 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
796 self._deferred.addCallbacks(stop_anyway, stop_anyway)
797 except Exception as e:
798 log.exception('exception-in-onu-disable', exception=e)
799
William Kurkian3a206332019-04-29 11:05:47 -0400800 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500801 def reenable(self, device):
802 self.log.debug('function-entry', device=device)
803 try:
804 # Start up OpenOMCI state machines for this device
805 # this will ultimately resync mib and unlock unis on successful redownloading the mib
806 self.log.debug('restarting-openomci-statemachine')
807 self._subscribe_to_events()
808 device.reason = "restarting-openomci"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400809 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500810 reactor.callLater(1, self._onu_omci_device.start)
811 self._heartbeat.enabled = True
812 except Exception as e:
813 log.exception('exception-in-onu-reenable', exception=e)
814
William Kurkian3a206332019-04-29 11:05:47 -0400815 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500816 def reboot(self):
817 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -0400818 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500819 if device.connect_status != ConnectStatus.REACHABLE:
820 self.log.error("device-unreachable")
821 return
822
William Kurkian3a206332019-04-29 11:05:47 -0400823 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500824 def success(_results):
825 self.log.info('reboot-success', _results=_results)
826 self.disable_ports(device)
827 device.connect_status = ConnectStatus.UNREACHABLE
828 device.oper_status = OperStatus.DISCOVERED
829 device.reason = "rebooting"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400830 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500831
832 def failure(_reason):
833 self.log.info('reboot-failure', _reason=_reason)
834
835 self._deferred = self._onu_omci_device.reboot()
836 self._deferred.addCallbacks(success, failure)
837
William Kurkian3a206332019-04-29 11:05:47 -0400838 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500839 def disable_ports(self, onu_device):
Matt Jeanneret80766692019-05-03 09:58:38 -0400840 self.log.info('disable-ports', device_id=self.device_id, onu_device=onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500841
842 # Disable all ports on that device
Matt Jeanneret80766692019-05-03 09:58:38 -0400843 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500844
William Kurkian3a206332019-04-29 11:05:47 -0400845 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500846 def enable_ports(self, onu_device):
847 self.log.info('enable-ports', device_id=self.device_id, onu_device=onu_device)
848
Matt Jeanneret80766692019-05-03 09:58:38 -0400849 # Enable all ports on that device
850 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.ACTIVE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500851
852 # Called just before openomci state machine is started. These listen for events from selected state machines,
853 # most importantly, mib in sync. Which ultimately leads to downloading the mib
854 def _subscribe_to_events(self):
855 self.log.debug('function-entry')
856
857 # OMCI MIB Database sync status
858 bus = self._onu_omci_device.event_bus
859 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
860 OnuDeviceEvents.MibDatabaseSyncEvent)
861 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
862
863 # OMCI Capabilities
864 bus = self._onu_omci_device.event_bus
865 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
866 OnuDeviceEvents.OmciCapabilitiesEvent)
867 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
868
869 # Called when the mib is in sync
870 def in_sync_handler(self, _topic, msg):
871 self.log.debug('function-entry', _topic=_topic, msg=msg)
872 if self._in_sync_subscription is not None:
873 try:
874 in_sync = msg[IN_SYNC_KEY]
875
876 if in_sync:
877 # Only call this once
878 bus = self._onu_omci_device.event_bus
879 bus.unsubscribe(self._in_sync_subscription)
880 self._in_sync_subscription = None
881
882 # Start up device_info load
883 self.log.debug('running-mib-sync')
884 reactor.callLater(0, self._mib_in_sync)
885
886 except Exception as e:
887 self.log.exception('in-sync', e=e)
888
889 def capabilties_handler(self, _topic, _msg):
890 self.log.debug('function-entry', _topic=_topic, msg=_msg)
891 if self._capabilities_subscription is not None:
892 self.log.debug('capabilities-handler-done')
893
894 # Mib is in sync, we can now query what we learned and actually start pushing ME (download) to the ONU.
895 # Currently uses a basic mib download task that create a bridge with a single gem port and uni, only allowing EAP
896 # Implement your own MibDownloadTask if you wish to setup something different by default
Matt Jeanneretc083f462019-03-11 15:02:01 -0400897 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500898 def _mib_in_sync(self):
899 self.log.debug('function-entry')
900
901 omci = self._onu_omci_device
902 in_sync = omci.mib_db_in_sync
903
Matt Jeanneretc083f462019-03-11 15:02:01 -0400904 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500905 device.reason = 'discovery-mibsync-complete'
Matt Jeanneretc083f462019-03-11 15:02:01 -0400906 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500907
908 if not self._dev_info_loaded:
909 self.log.info('loading-device-data-from-mib', in_sync=in_sync, already_loaded=self._dev_info_loaded)
910
911 omci_dev = self._onu_omci_device
912 config = omci_dev.configuration
913
914 # TODO: run this sooner somehow. shouldnt have to wait for mib sync to push an initial download
915 # In Sync, we can register logical ports now. Ideally this could occur on
916 # the first time we received a successful (no timeout) OMCI Rx response.
917 try:
918
919 # sort the lists so we get consistent port ordering.
920 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
921 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
922 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
923 veip_list = sorted(config.veip_entities) if config.veip_entities else []
924
925 if ani_list is None or (pptp_list is None and veip_list is None):
926 device.reason = 'onu-missing-required-elements'
927 self.log.warn("no-ani-or-unis")
Matt Jeanneretc083f462019-03-11 15:02:01 -0400928 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500929 raise Exception("onu-missing-required-elements")
930
931 # Currently logging the ani, pptp, veip, and uni for information purposes.
932 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
933 # And in some ONU the UNI-G list is incomplete or incorrect...
934 for entity_id in ani_list:
935 ani_value = config.ani_g_entities[entity_id]
936 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
937 # TODO: currently only one OLT PON port/ANI, so this works out. With NGPON there will be 2..?
938 self._total_tcont_count = ani_value.get('total-tcont-count')
939 self.log.debug("set-total-tcont-count", tcont_count=self._total_tcont_count)
940
941 for entity_id in uni_list:
942 uni_value = config.uni_g_entities[entity_id]
943 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
944
945 uni_entities = OrderedDict()
946 for entity_id in pptp_list:
947 pptp_value = config.pptp_entities[entity_id]
948 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
949 uni_entities[entity_id] = UniType.PPTP
950
951 for entity_id in veip_list:
952 veip_value = config.veip_entities[entity_id]
953 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
954 uni_entities[entity_id] = UniType.VEIP
955
956 uni_id = 0
957 for entity_id, uni_type in uni_entities.iteritems():
958 try:
Matt Jeanneretc083f462019-03-11 15:02:01 -0400959 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500960 uni_id += 1
961 except AssertionError as e:
962 self.log.warn("could not add UNI", entity_id=entity_id, uni_type=uni_type, e=e)
963
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500964 self._qos_flexibility = config.qos_configuration_flexibility or 0
965 self._omcc_version = config.omcc_version or OMCCVersion.Unknown
966
967 if self._unis:
968 self._dev_info_loaded = True
969 else:
970 device.reason = 'no-usable-unis'
Matt Jeanneretc083f462019-03-11 15:02:01 -0400971 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500972 self.log.warn("no-usable-unis")
973 raise Exception("no-usable-unis")
974
975 except Exception as e:
976 self.log.exception('device-info-load', e=e)
977 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
978
979 else:
980 self.log.info('device-info-already-loaded', in_sync=in_sync, already_loaded=self._dev_info_loaded)
981
982 if self._dev_info_loaded:
983 if device.admin_state == AdminState.ENABLED:
Matt Jeanneretc083f462019-03-11 15:02:01 -0400984
985 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500986 def success(_results):
987 self.log.info('mib-download-success', _results=_results)
Matt Jeanneretc083f462019-03-11 15:02:01 -0400988 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500989 device.reason = 'initial-mib-downloaded'
Matt Jeanneretc083f462019-03-11 15:02:01 -0400990 yield self.core_proxy.device_state_update(device.id,
991 oper_status=OperStatus.ACTIVE, connect_status=ConnectStatus.REACHABLE)
992 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500993 self._mib_download_task = None
994
Matt Jeanneretc083f462019-03-11 15:02:01 -0400995 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500996 def failure(_reason):
997 self.log.warn('mib-download-failure-retrying', _reason=_reason)
998 device.reason = 'initial-mib-download-failure-retrying'
Matt Jeanneretc083f462019-03-11 15:02:01 -0400999 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001000 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1001
1002 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1003 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1004 self.log.info('downloading-initial-mib-configuration')
1005 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1006 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1007 self._deferred.addCallbacks(success, failure)
1008 else:
1009 self.log.info('admin-down-disabling')
1010 self.disable(device)
1011 else:
1012 self.log.info('device-info-not-loaded-skipping-mib-download')
1013
Matt Jeanneretc083f462019-03-11 15:02:01 -04001014 @inlineCallbacks
1015 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001016 self.log.debug('function-entry')
1017
Matt Jeanneretc083f462019-03-11 15:02:01 -04001018 uni_no = self.mk_uni_port_num(self._onu_indication.intf_id, self._onu_indication.onu_id, uni_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001019
1020 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1021 uni_name = "uni-{}".format(uni_no)
1022
1023 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
1024
1025 self.log.debug('uni-port-inputs', uni_no=uni_no, uni_id=uni_id, uni_name=uni_name, uni_type=uni_type,
1026 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num)
1027
1028 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1029 uni_port.entity_id = entity_id
1030 uni_port.enabled = True
1031 uni_port.mac_bridge_port_num = mac_bridge_port_num
1032
1033 self.log.debug("created-uni-port", uni=uni_port)
1034
Matt Jeanneretc083f462019-03-11 15:02:01 -04001035 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001036
1037 self._unis[uni_port.port_number] = uni_port
1038
1039 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=self._onu_indication.onu_id,
1040 uni_ports=self._unis.values())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001041
Matt Jeanneretc083f462019-03-11 15:02:01 -04001042 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1043 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
1044 MAX_PONS_PER_OLT = 16
1045 MAX_ONUS_PER_PON = 32
1046 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001047
Matt Jeanneretc083f462019-03-11 15:02:01 -04001048 assert intf_id < MAX_PONS_PER_OLT
1049 assert onu_id < MAX_ONUS_PER_PON
1050 assert uni_id < MAX_UNIS_PER_ONU
Matt Jeanneret3b7db442019-04-22 16:29:48 -04001051 return intf_id << 11 | onu_id << 4 | uni_id