blob: 82b6c900e4bbea23164fc985cccd68901ef328a3 [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
Devmalya Paul7e0be4a2019-05-08 05:18:04 -040030from pyvoltha.adapters.extensions.alarms.onu.onu_active_alarm import OnuActiveAlarm
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050031from pyvoltha.adapters.extensions.kpi.onu.onu_pm_metrics import OnuPmMetrics
32from pyvoltha.adapters.extensions.kpi.onu.onu_omci_pm import OnuOmciPmMetrics
33from pyvoltha.adapters.extensions.alarms.adapter_alarms import AdapterAlarms
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050034
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050035import pyvoltha.common.openflow.utils as fd
36from pyvoltha.common.utils.registry import registry
37from pyvoltha.common.config.config_backend import ConsulStore
38from pyvoltha.common.config.config_backend import EtcdStore
William Kurkian8235c1e2019-03-05 12:58:28 -050039from voltha_protos.common_pb2 import OperStatus, ConnectStatus, AdminState
Matt Jeanneretc083f462019-03-11 15:02:01 -040040from 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 -040041from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
42 InterAdapterOmciMessage, PortCapability, InterAdapterTechProfileDownloadMessage
Matt Jeannereta32441c2019-03-07 05:16:37 -050043from voltha_protos.openolt_pb2 import OnuIndication
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050044from pyvoltha.adapters.extensions.omci.onu_configuration import OMCCVersion
45from pyvoltha.adapters.extensions.omci.onu_device_entry import OnuDeviceEvents, \
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050046 OnuDeviceEntry, IN_SYNC_KEY
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050047from omci.brcm_mib_download_task import BrcmMibDownloadTask
48from omci.brcm_tp_service_specific_task import BrcmTpServiceSpecificTask
49from omci.brcm_uni_lock_task import BrcmUniLockTask
50from omci.brcm_vlan_filter_task import BrcmVlanFilterTask
51from onu_gem_port import *
52from onu_tcont import *
53from pon_port import *
54from uni_port import *
55from onu_traffic_descriptor import *
56from pyvoltha.common.tech_profile.tech_profile import TechProfile
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050057
58OP = EntityOperations
59RC = ReasonCodes
60
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050061log = structlog.get_logger()
62
63_STARTUP_RETRY_WAIT = 20
64
65
66class BrcmOpenomciOnuHandler(object):
67
68 def __init__(self, adapter, device_id):
69 self.log = structlog.get_logger(device_id=device_id)
70 self.log.debug('function-entry')
71 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050072 self.core_proxy = adapter.core_proxy
73 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050074 self.parent_adapter = None
75 self.parent_id = None
76 self.device_id = device_id
77 self.incoming_messages = DeferredQueue()
78 self.event_messages = DeferredQueue()
79 self.proxy_address = None
80 self.tx_id = 0
81 self._enabled = False
82 self.alarms = None
83 self.pm_metrics = None
84 self._omcc_version = OMCCVersion.Unknown
85 self._total_tcont_count = 0 # From ANI-G ME
86 self._qos_flexibility = 0 # From ONT2_G ME
87
88 self._onu_indication = None
89 self._unis = dict() # Port # -> UniPort
90
91 self._pon = None
92 # TODO: probably shouldnt be hardcoded, determine from olt maybe?
93 self._pon_port_number = 100
94 self.logical_device_id = None
95
96 self._heartbeat = HeartBeat.create(self, device_id)
97
98 # Set up OpenOMCI environment
99 self._onu_omci_device = None
100 self._dev_info_loaded = False
101 self._deferred = None
102
103 self._in_sync_subscription = None
104 self._connectivity_subscription = None
105 self._capabilities_subscription = None
106
107 self.mac_bridge_service_profile_entity_id = 0x201
108 self.gal_enet_profile_entity_id = 0x1
109
110 self._tp_service_specific_task = dict()
111 self._tech_profile_download_done = dict()
112
113 # Initialize KV store client
114 self.args = registry('main').get_args()
115 if self.args.backend == 'etcd':
116 host, port = self.args.etcd.split(':', 1)
117 self.kv_client = EtcdStore(host, port,
118 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
119 elif self.args.backend == 'consul':
120 host, port = self.args.consul.split(':', 1)
121 self.kv_client = ConsulStore(host, port,
122 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
123 else:
124 self.log.error('Invalid-backend')
125 raise Exception("Invalid-backend-for-kv-store")
126
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500127 @property
128 def enabled(self):
129 return self._enabled
130
131 @enabled.setter
132 def enabled(self, value):
133 if self._enabled != value:
134 self._enabled = value
135
136 @property
137 def omci_agent(self):
138 return self.adapter.omci_agent
139
140 @property
141 def omci_cc(self):
142 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
143
144 @property
145 def heartbeat(self):
146 return self._heartbeat
147
148 @property
149 def uni_ports(self):
150 return self._unis.values()
151
152 def uni_port(self, port_no_or_name):
153 if isinstance(port_no_or_name, (str, unicode)):
154 return next((uni for uni in self.uni_ports
155 if uni.name == port_no_or_name), None)
156
157 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
158 return next((uni for uni in self.uni_ports
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400159 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500160
161 @property
162 def pon_port(self):
163 return self._pon
164
165 def receive_message(self, msg):
166 if self.omci_cc is not None:
167 self.omci_cc.receive_message(msg)
168
Matt Jeanneretc083f462019-03-11 15:02:01 -0400169 def get_ofp_port_info(self, device, port_no):
170 self.log.info('get_ofp_port_info', port_no=port_no, device_id=device.id)
171 cap = OFPPF_1GB_FD | OFPPF_FIBER
172
173 hw_addr=mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
174 ((device.parent_port_no >> 8 & 0xff),
175 device.parent_port_no & 0xff,
176 (port_no >> 16) & 0xff,
177 (port_no >> 8) & 0xff,
178 port_no & 0xff))
179
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400180 uni_port = self.uni_port(int(port_no))
181 name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
182 self.log.debug('ofp_port_name', port_no=port_no, name=name)
183
Matt Jeanneretc083f462019-03-11 15:02:01 -0400184 return PortCapability(
185 port=LogicalPort(
186 ofp_port=ofp_port(
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400187 name=name,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400188 hw_addr=hw_addr,
189 config=0,
190 state=OFPPS_LIVE,
191 curr=cap,
192 advertised=cap,
193 peer=cap,
194 curr_speed=OFPPF_1GB_FD,
195 max_speed=OFPPF_1GB_FD
196 ),
197 device_id=device.id,
198 device_port_no=port_no
199 )
200 )
201
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500202 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500203 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500204 def activate(self, device):
205 self.log.debug('function-entry', device=device)
206
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500207 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500208 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500209 assert device.proxy_address.device_id
210
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500211 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500212 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500213 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500214 if self.enabled is not True:
215 self.log.info('activating-new-onu')
216 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500217 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500218 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500219 device.reason = 'activating-onu'
220
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500221 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400222 # pm_metrics requires a logical device id. For now set to just device_id
223 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500224
Matt Jeannereta32441c2019-03-07 05:16:37 -0500225 yield self.core_proxy.device_update(device)
226
227 yield self.core_proxy.device_state_update(device.id, oper_status=OperStatus.DISCOVERED,
228 connect_status=ConnectStatus.REACHABLE)
229
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500230
231 self.log.debug('set-device-discovered')
232
Devmalya Paul7e0be4a2019-05-08 05:18:04 -0400233 yield self._init_pon_state(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500234
235 ############################################################################
236 # Setup PM configuration for this device
237 # Pass in ONU specific options
238 kwargs = {
239 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
240 'heartbeat': self.heartbeat,
241 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
242 }
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400243 self.log.debug('create-OnuPmMetrics', serial_number=device.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500244 self.pm_metrics = OnuPmMetrics(self.core_proxy, self.device_id,
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400245 self.logical_device_id, device.serial_number,
246 grouped=True, freq_override=False, **kwargs)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500247 pm_config = self.pm_metrics.make_proto()
248 self._onu_omci_device.set_pm_config(self.pm_metrics.omci_pm.openomci_interval_pm)
249 self.log.info("initial-pm-config", pm_config=pm_config)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500250 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500251
252 ############################################################################
253 # Setup Alarm handler
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400254 self.alarms = AdapterAlarms(self.core_proxy, device.id, self.logical_device_id,
255 device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500256 # Note, ONU ID and UNI intf set in add_uni_port method
257 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.alarms,
258 ani_ports=[self._pon])
aishwaryarana01a98d9fe2019-05-08 12:09:06 -0500259
260 #Start collecting stats from the device after a brief pause
261 reactor.callLater(10, self.pm_metrics.start_collector)
262
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500263 self.enabled = True
264 else:
265 self.log.info('onu-already-activated')
266
267 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400268 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500269 def reconcile(self, device):
270 self.log.debug('function-entry', device=device)
271
272 # first we verify that we got parent reference and proxy info
273 assert device.parent_id
274 assert device.proxy_address.device_id
275
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500276 if self.enabled is not True:
277 self.log.info('reconciling-broadcom-onu-device')
278
279 self._init_pon_state(device)
280
281 # need to restart state machines on vcore restart. there is no indication to do it for us.
282 self._onu_omci_device.start()
283 device.reason = "restarting-openomci"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400284 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500285
286 # TODO: this is probably a bit heavy handed
287 # Force a reboot for now. We need indications to reflow to reassign tconts and gems given vcore went away
288 # This may not be necessary when mib resync actually works
289 reactor.callLater(1, self.reboot)
290
291 self.enabled = True
292 else:
293 self.log.info('onu-already-activated')
294
295 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500296 def _init_pon_state(self, device):
297 self.log.debug('function-entry', device=device)
298
299 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500300 self._pon.add_peer(self.parent_id, self._pon_port_number)
301 self.log.debug('adding-pon-port-to-agent', pon=self._pon.get_port())
302
Matt Jeannereta32441c2019-03-07 05:16:37 -0500303 yield self.core_proxy.port_created(device.id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500304
Matt Jeanneret0c287892019-02-28 11:48:00 -0500305 self.log.debug('added-pon-port-to-agent', pon=self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500306
307 # Create and start the OpenOMCI ONU Device Entry for this ONU
308 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500309 self.core_proxy,
310 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500311 support_classes=self.adapter.broadcom_omci,
312 custom_me_map=self.adapter.custom_me_entities())
313 # Port startup
314 if self._pon is not None:
315 self._pon.enabled = True
316
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500317 def delete(self, device):
318 self.log.info('delete-onu', device=device)
319 if self.parent_adapter:
320 try:
321 self.parent_adapter.delete_child_device(self.parent_id, device)
322 except AttributeError:
323 self.log.debug('parent-device-delete-child-not-implemented')
324 else:
325 self.log.debug("parent-adapter-not-available")
326
327 def _create_tconts(self, uni_id, us_scheduler):
328 alloc_id = us_scheduler['alloc_id']
329 q_sched_policy = us_scheduler['q_sched_policy']
330 self.log.debug('create-tcont', us_scheduler=us_scheduler)
331
332 tcontdict = dict()
333 tcontdict['alloc-id'] = alloc_id
334 tcontdict['q_sched_policy'] = q_sched_policy
335 tcontdict['uni_id'] = uni_id
336
337 # TODO: Not sure what to do with any of this...
338 tddata = dict()
339 tddata['name'] = 'not-sure-td-profile'
340 tddata['fixed-bandwidth'] = "not-sure-fixed"
341 tddata['assured-bandwidth'] = "not-sure-assured"
342 tddata['maximum-bandwidth'] = "not-sure-max"
343 tddata['additional-bw-eligibility-indicator'] = "not-sure-additional"
344
345 td = OnuTrafficDescriptor.create(tddata)
346 tcont = OnuTCont.create(self, tcont=tcontdict, td=td)
347
348 self._pon.add_tcont(tcont)
349
350 self.log.debug('pon-add-tcont', tcont=tcont)
351
352 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
353 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
354 self.log.debug('create-gemport',
355 gem_ports=gem_ports, direction=direction)
356
357 for gem_port in gem_ports:
358 gemdict = dict()
359 gemdict['gemport_id'] = gem_port['gemport_id']
360 gemdict['direction'] = direction
361 gemdict['alloc_id_ref'] = alloc_id_ref
362 gemdict['encryption'] = gem_port['aes_encryption']
363 gemdict['discard_config'] = dict()
364 gemdict['discard_config']['max_probability'] = \
365 gem_port['discard_config']['max_probability']
366 gemdict['discard_config']['max_threshold'] = \
367 gem_port['discard_config']['max_threshold']
368 gemdict['discard_config']['min_threshold'] = \
369 gem_port['discard_config']['min_threshold']
370 gemdict['discard_policy'] = gem_port['discard_policy']
371 gemdict['max_q_size'] = gem_port['max_q_size']
372 gemdict['pbit_map'] = gem_port['pbit_map']
373 gemdict['priority_q'] = gem_port['priority_q']
374 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
375 gemdict['weight'] = gem_port['weight']
376 gemdict['uni_id'] = uni_id
377
378 gem_port = OnuGemPort.create(self, gem_port=gemdict)
379
380 self._pon.add_gem_port(gem_port)
381
382 self.log.debug('pon-add-gemport', gem_port=gem_port)
383
384 def _do_tech_profile_configuration(self, uni_id, tp):
385 num_of_tconts = tp['num_of_tconts']
386 us_scheduler = tp['us_scheduler']
387 alloc_id = us_scheduler['alloc_id']
388 self._create_tconts(uni_id, us_scheduler)
389 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
390 self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
391 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
392 self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
393
394 def load_and_configure_tech_profile(self, uni_id, tp_path):
395 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
396
397 if uni_id not in self._tp_service_specific_task:
398 self._tp_service_specific_task[uni_id] = dict()
399
400 if uni_id not in self._tech_profile_download_done:
401 self._tech_profile_download_done[uni_id] = dict()
402
403 if tp_path not in self._tech_profile_download_done[uni_id]:
404 self._tech_profile_download_done[uni_id][tp_path] = False
405
406 if not self._tech_profile_download_done[uni_id][tp_path]:
407 try:
408 if tp_path in self._tp_service_specific_task[uni_id]:
409 self.log.info("tech-profile-config-already-in-progress",
410 tp_path=tp_path)
411 return
412
413 tp = self.kv_client[tp_path]
414 tp = ast.literal_eval(tp)
415 self.log.debug("tp-instance", tp=tp)
416 self._do_tech_profile_configuration(uni_id, tp)
William Kurkian3a206332019-04-29 11:05:47 -0400417
418 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500419 def success(_results):
420 self.log.info("tech-profile-config-done-successfully")
William Kurkian3a206332019-04-29 11:05:47 -0400421 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500422 device.reason = 'tech-profile-config-download-success'
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400423 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500424 if tp_path in self._tp_service_specific_task[uni_id]:
425 del self._tp_service_specific_task[uni_id][tp_path]
426 self._tech_profile_download_done[uni_id][tp_path] = True
427
William Kurkian3a206332019-04-29 11:05:47 -0400428 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500429 def failure(_reason):
430 self.log.warn('tech-profile-config-failure-retrying',
431 _reason=_reason)
William Kurkian3a206332019-04-29 11:05:47 -0400432 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500433 device.reason = 'tech-profile-config-download-failure-retrying'
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400434 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500435 if tp_path in self._tp_service_specific_task[uni_id]:
436 del self._tp_service_specific_task[uni_id][tp_path]
437 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self.load_and_configure_tech_profile,
438 uni_id, tp_path)
439
440 self.log.info('downloading-tech-profile-configuration')
441 self._tp_service_specific_task[uni_id][tp_path] = \
442 BrcmTpServiceSpecificTask(self.omci_agent, self, uni_id)
443 self._deferred = \
444 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
445 self._deferred.addCallbacks(success, failure)
446
447 except Exception as e:
448 self.log.exception("error-loading-tech-profile", e=e)
449 else:
450 self.log.info("tech-profile-config-already-done")
451
452 def update_pm_config(self, device, pm_config):
453 # TODO: This has not been tested
454 self.log.info('update_pm_config', pm_config=pm_config)
455 self.pm_metrics.update(pm_config)
456
457 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
458 # flow decomposition that ultimately comes from onos
459 def update_flow_table(self, device, flows):
460 self.log.debug('function-entry', device=device, flows=flows)
461
462 #
463 # We need to proxy through the OLT to get to the ONU
464 # Configuration from here should be using OMCI
465 #
466 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
467
468 # no point in pushing omci flows if the device isnt reachable
469 if device.connect_status != ConnectStatus.REACHABLE or \
470 device.admin_state != AdminState.ENABLED:
471 self.log.warn("device-disabled-or-offline-skipping-flow-update",
472 admin=device.admin_state, connect=device.connect_status)
473 return
474
475 def is_downstream(port):
476 return port == self._pon_port_number
477
478 def is_upstream(port):
479 return not is_downstream(port)
480
481 for flow in flows:
482 _type = None
483 _port = None
484 _vlan_vid = None
485 _udp_dst = None
486 _udp_src = None
487 _ipv4_dst = None
488 _ipv4_src = None
489 _metadata = None
490 _output = None
491 _push_tpid = None
492 _field = None
493 _set_vlan_vid = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400494 _tunnel_id = None
495
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500496 self.log.debug('bulk-flow-update', device_id=device.id, flow=flow)
497 try:
498 _in_port = fd.get_in_port(flow)
499 assert _in_port is not None
500
501 _out_port = fd.get_out_port(flow) # may be None
502
503 if is_downstream(_in_port):
504 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
505 uni_port = self.uni_port(_out_port)
506 elif is_upstream(_in_port):
507 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
508 uni_port = self.uni_port(_in_port)
509 else:
510 raise Exception('port should be 1 or 2 by our convention')
511
512 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
513
514 for field in fd.get_ofb_fields(flow):
515 if field.type == fd.ETH_TYPE:
516 _type = field.eth_type
517 self.log.debug('field-type-eth-type',
518 eth_type=_type)
519
520 elif field.type == fd.IP_PROTO:
521 _proto = field.ip_proto
522 self.log.debug('field-type-ip-proto',
523 ip_proto=_proto)
524
525 elif field.type == fd.IN_PORT:
526 _port = field.port
527 self.log.debug('field-type-in-port',
528 in_port=_port)
529
530 elif field.type == fd.VLAN_VID:
531 _vlan_vid = field.vlan_vid & 0xfff
532 self.log.debug('field-type-vlan-vid',
533 vlan=_vlan_vid)
534
535 elif field.type == fd.VLAN_PCP:
536 _vlan_pcp = field.vlan_pcp
537 self.log.debug('field-type-vlan-pcp',
538 pcp=_vlan_pcp)
539
540 elif field.type == fd.UDP_DST:
541 _udp_dst = field.udp_dst
542 self.log.debug('field-type-udp-dst',
543 udp_dst=_udp_dst)
544
545 elif field.type == fd.UDP_SRC:
546 _udp_src = field.udp_src
547 self.log.debug('field-type-udp-src',
548 udp_src=_udp_src)
549
550 elif field.type == fd.IPV4_DST:
551 _ipv4_dst = field.ipv4_dst
552 self.log.debug('field-type-ipv4-dst',
553 ipv4_dst=_ipv4_dst)
554
555 elif field.type == fd.IPV4_SRC:
556 _ipv4_src = field.ipv4_src
557 self.log.debug('field-type-ipv4-src',
558 ipv4_dst=_ipv4_src)
559
560 elif field.type == fd.METADATA:
561 _metadata = field.table_metadata
562 self.log.debug('field-type-metadata',
563 metadata=_metadata)
564
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400565 elif field.type == fd.TUNNEL_ID:
566 _tunnel_id = field.tunnel_id
567 self.log.debug('field-type-tunnel-id',
568 tunnel_id=_tunnel_id)
569
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500570 else:
571 raise NotImplementedError('field.type={}'.format(
572 field.type))
573
574 for action in fd.get_actions(flow):
575
576 if action.type == fd.OUTPUT:
577 _output = action.output.port
578 self.log.debug('action-type-output',
579 output=_output, in_port=_in_port)
580
581 elif action.type == fd.POP_VLAN:
582 self.log.debug('action-type-pop-vlan',
583 in_port=_in_port)
584
585 elif action.type == fd.PUSH_VLAN:
586 _push_tpid = action.push.ethertype
587 self.log.debug('action-type-push-vlan',
588 push_tpid=_push_tpid, in_port=_in_port)
589 if action.push.ethertype != 0x8100:
590 self.log.error('unhandled-tpid',
591 ethertype=action.push.ethertype)
592
593 elif action.type == fd.SET_FIELD:
594 _field = action.set_field.field.ofb_field
595 assert (action.set_field.field.oxm_class ==
596 OFPXMC_OPENFLOW_BASIC)
597 self.log.debug('action-type-set-field',
598 field=_field, in_port=_in_port)
599 if _field.type == fd.VLAN_VID:
600 _set_vlan_vid = _field.vlan_vid & 0xfff
601 self.log.debug('set-field-type-vlan-vid',
602 vlan_vid=_set_vlan_vid)
603 else:
604 self.log.error('unsupported-action-set-field-type',
605 field_type=_field.type)
606 else:
607 self.log.error('unsupported-action-type',
608 action_type=action.type, in_port=_in_port)
609
610 # TODO: We only set vlan omci flows. Handle omci matching ethertypes at some point in another task
611 if _type is not None:
612 self.log.warn('ignoring-flow-with-ethType', ethType=_type)
613 elif _set_vlan_vid is None or _set_vlan_vid == 0:
614 self.log.warn('ignorning-flow-that-does-not-set-vlanid')
615 else:
616 self.log.warn('set-vlanid', uni_id=uni_port.port_number, set_vlan_vid=_set_vlan_vid)
617 self._add_vlan_filter_task(device, uni_port, _set_vlan_vid)
618
619 except Exception as e:
620 self.log.exception('failed-to-install-flow', e=e, flow=flow)
621
622
623 def _add_vlan_filter_task(self, device, uni_port, _set_vlan_vid):
624 assert uni_port is not None
625
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400626 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500627 def success(_results):
628 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid)
629 device.reason = 'omci-flows-pushed'
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 = None
632
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400633 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500634 def failure(_reason):
635 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid)
636 device.reason = 'omci-flows-failed-retrying'
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400637 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500638 self._vlan_filter_task = reactor.callLater(_STARTUP_RETRY_WAIT,
639 self._add_vlan_filter_task, device, uni_port, _set_vlan_vid)
640
641 self.log.info('setting-vlan-tag')
642 self._vlan_filter_task = BrcmVlanFilterTask(self.omci_agent, self.device_id, uni_port, _set_vlan_vid)
643 self._deferred = self._onu_omci_device.task_runner.queue_task(self._vlan_filter_task)
644 self._deferred.addCallbacks(success, failure)
645
646 def get_tx_id(self):
647 self.log.debug('function-entry')
648 self.tx_id += 1
649 return self.tx_id
650
Matt Jeannereta32441c2019-03-07 05:16:37 -0500651 def process_inter_adapter_message(self, request):
652 self.log.debug('process-inter-adapter-message', msg=request)
653 try:
654 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
655 omci_msg = InterAdapterOmciMessage()
656 request.body.Unpack(omci_msg)
657 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500658
Matt Jeannereta32441c2019-03-07 05:16:37 -0500659 self.receive_message(omci_msg.message)
660
661 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
662 onu_indication = OnuIndication()
663 request.body.Unpack(onu_indication)
664 self.log.debug('inter-adapter-recv-onu-ind', onu_indication=onu_indication)
665
666 if onu_indication.oper_state == "up":
667 self.create_interface(onu_indication)
668 elif onu_indication.oper_state == "down":
669 self.update_interface(onu_indication)
670 else:
671 self.log.error("unknown-onu-indication", onu_indication=onu_indication)
672
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400673 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
674 tech_msg = InterAdapterTechProfileDownloadMessage()
675 request.body.Unpack(tech_msg)
676 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
677
678 self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
679
Matt Jeannereta32441c2019-03-07 05:16:37 -0500680 else:
681 self.log.error("inter-adapter-unhandled-type", request=request)
682
683 except Exception as e:
684 self.log.exception("error-processing-inter-adapter-message", e=e)
685
686 # Called each time there is an onu "up" indication from the olt handler
687 @inlineCallbacks
688 def create_interface(self, onu_indication):
689 self.log.debug('function-entry', onu_indication=onu_indication)
690 self._onu_indication = onu_indication
691
Matt Jeanneretc083f462019-03-11 15:02:01 -0400692 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
693 connect_status=ConnectStatus.REACHABLE)
694
Matt Jeannereta32441c2019-03-07 05:16:37 -0500695 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500696
697 self.log.debug('starting-openomci-statemachine')
698 self._subscribe_to_events()
699 reactor.callLater(1, self._onu_omci_device.start)
700 onu_device.reason = "starting-openomci"
Matt Jeannereta32441c2019-03-07 05:16:37 -0500701 yield self.core_proxy.device_update(onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500702 self._heartbeat.enabled = True
703
704 # Currently called each time there is an onu "down" indication from the olt handler
705 # TODO: possibly other reasons to "update" from the olt?
Matt Jeannereta32441c2019-03-07 05:16:37 -0500706 @inlineCallbacks
707 def update_interface(self, onu_indication):
708 self.log.debug('function-entry', onu_indication=onu_indication)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500709
Matt Jeannereta32441c2019-03-07 05:16:37 -0500710 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500711
Matt Jeannereta32441c2019-03-07 05:16:37 -0500712 if onu_indication.oper_state == 'down':
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500713 self.log.debug('stopping-openomci-statemachine')
714 reactor.callLater(0, self._onu_omci_device.stop)
715
716 # Let TP download happen again
717 for uni_id in self._tp_service_specific_task:
718 self._tp_service_specific_task[uni_id].clear()
719 for uni_id in self._tech_profile_download_done:
720 self._tech_profile_download_done[uni_id].clear()
721
722 self.disable_ports(onu_device)
723 onu_device.reason = "stopping-openomci"
Chaitrashree G S01257fa2019-05-24 06:49:49 -0400724 yield self.core_proxy.device_update(onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500725 onu_device.connect_status = ConnectStatus.UNREACHABLE
726 onu_device.oper_status = OperStatus.DISCOVERED
Chaitrashree G S01257fa2019-05-24 06:49:49 -0400727 yield self.core_proxy.device_state_update(self.device_id, onu_device.oper_status,onu_device.connect_status)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500728 else:
729 self.log.debug('not-changing-openomci-statemachine')
730
731 # Not currently called by olt or anything else
William Kurkian3a206332019-04-29 11:05:47 -0400732 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500733 def remove_interface(self, data):
734 self.log.debug('function-entry', data=data)
735
William Kurkian3a206332019-04-29 11:05:47 -0400736 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500737
738 self.log.debug('stopping-openomci-statemachine')
739 reactor.callLater(0, self._onu_omci_device.stop)
740
741 # Let TP download happen again
742 for uni_id in self._tp_service_specific_task:
743 self._tp_service_specific_task[uni_id].clear()
744 for uni_id in self._tech_profile_download_done:
745 self._tech_profile_download_done[uni_id].clear()
746
747 self.disable_ports(onu_device)
748 onu_device.reason = "stopping-openomci"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400749 yield self.core_proxy.device_update(onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500750
751 # TODO: im sure there is more to do here
752
753 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400754 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500755 def remove_gemport(self, data):
756 self.log.debug('remove-gemport', data=data)
William Kurkian3a206332019-04-29 11:05:47 -0400757 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500758 if device.connect_status != ConnectStatus.REACHABLE:
759 self.log.error('device-unreachable')
760 return
761
762 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400763 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500764 def remove_tcont(self, tcont_data, traffic_descriptor_data):
765 self.log.debug('remove-tcont', tcont_data=tcont_data, traffic_descriptor_data=traffic_descriptor_data)
William Kurkian3a206332019-04-29 11:05:47 -0400766 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500767 if device.connect_status != ConnectStatus.REACHABLE:
768 self.log.error('device-unreachable')
769 return
770
771 # TODO: Create some omci task that encompases this what intended
772
773 # Not currently called. Would be called presumably from the olt handler
774 def create_multicast_gemport(self, data):
775 self.log.debug('function-entry', data=data)
776
777 # TODO: create objects and populate for later omci calls
778
779 def disable(self, device):
780 self.log.debug('function-entry', device=device)
781 try:
782 self.log.info('sending-uni-lock-towards-device', device=device)
783
Matt Jeanneret80766692019-05-03 09:58:38 -0400784 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500785 def stop_anyway(reason):
786 # proceed with disable regardless if we could reach the onu. for example onu is unplugged
787 self.log.debug('stopping-openomci-statemachine')
788 reactor.callLater(0, self._onu_omci_device.stop)
789
790 # Let TP download happen again
791 for uni_id in self._tp_service_specific_task:
792 self._tp_service_specific_task[uni_id].clear()
793 for uni_id in self._tech_profile_download_done:
794 self._tech_profile_download_done[uni_id].clear()
795
796 self.disable_ports(device)
797 device.oper_status = OperStatus.UNKNOWN
798 device.reason = "omci-admin-lock"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400799 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500800
801 # lock all the unis
802 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=True)
803 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
804 self._deferred.addCallbacks(stop_anyway, stop_anyway)
805 except Exception as e:
806 log.exception('exception-in-onu-disable', exception=e)
807
William Kurkian3a206332019-04-29 11:05:47 -0400808 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500809 def reenable(self, device):
810 self.log.debug('function-entry', device=device)
811 try:
812 # Start up OpenOMCI state machines for this device
813 # this will ultimately resync mib and unlock unis on successful redownloading the mib
814 self.log.debug('restarting-openomci-statemachine')
815 self._subscribe_to_events()
816 device.reason = "restarting-openomci"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400817 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500818 reactor.callLater(1, self._onu_omci_device.start)
819 self._heartbeat.enabled = True
820 except Exception as e:
821 log.exception('exception-in-onu-reenable', exception=e)
822
William Kurkian3a206332019-04-29 11:05:47 -0400823 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500824 def reboot(self):
825 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -0400826 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500827 if device.connect_status != ConnectStatus.REACHABLE:
828 self.log.error("device-unreachable")
829 return
830
William Kurkian3a206332019-04-29 11:05:47 -0400831 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500832 def success(_results):
833 self.log.info('reboot-success', _results=_results)
834 self.disable_ports(device)
835 device.connect_status = ConnectStatus.UNREACHABLE
836 device.oper_status = OperStatus.DISCOVERED
837 device.reason = "rebooting"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400838 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500839
840 def failure(_reason):
841 self.log.info('reboot-failure', _reason=_reason)
842
843 self._deferred = self._onu_omci_device.reboot()
844 self._deferred.addCallbacks(success, failure)
845
William Kurkian3a206332019-04-29 11:05:47 -0400846 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500847 def disable_ports(self, onu_device):
Matt Jeanneret80766692019-05-03 09:58:38 -0400848 self.log.info('disable-ports', device_id=self.device_id, onu_device=onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500849
850 # Disable all ports on that device
Matt Jeanneret80766692019-05-03 09:58:38 -0400851 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500852
William Kurkian3a206332019-04-29 11:05:47 -0400853 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500854 def enable_ports(self, onu_device):
855 self.log.info('enable-ports', device_id=self.device_id, onu_device=onu_device)
856
Matt Jeanneret80766692019-05-03 09:58:38 -0400857 # Enable all ports on that device
858 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.ACTIVE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500859
860 # Called just before openomci state machine is started. These listen for events from selected state machines,
861 # most importantly, mib in sync. Which ultimately leads to downloading the mib
862 def _subscribe_to_events(self):
863 self.log.debug('function-entry')
864
865 # OMCI MIB Database sync status
866 bus = self._onu_omci_device.event_bus
867 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
868 OnuDeviceEvents.MibDatabaseSyncEvent)
869 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
870
871 # OMCI Capabilities
872 bus = self._onu_omci_device.event_bus
873 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
874 OnuDeviceEvents.OmciCapabilitiesEvent)
875 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
876
877 # Called when the mib is in sync
878 def in_sync_handler(self, _topic, msg):
879 self.log.debug('function-entry', _topic=_topic, msg=msg)
880 if self._in_sync_subscription is not None:
881 try:
882 in_sync = msg[IN_SYNC_KEY]
883
884 if in_sync:
885 # Only call this once
886 bus = self._onu_omci_device.event_bus
887 bus.unsubscribe(self._in_sync_subscription)
888 self._in_sync_subscription = None
889
890 # Start up device_info load
891 self.log.debug('running-mib-sync')
892 reactor.callLater(0, self._mib_in_sync)
893
894 except Exception as e:
895 self.log.exception('in-sync', e=e)
896
897 def capabilties_handler(self, _topic, _msg):
898 self.log.debug('function-entry', _topic=_topic, msg=_msg)
899 if self._capabilities_subscription is not None:
900 self.log.debug('capabilities-handler-done')
901
902 # Mib is in sync, we can now query what we learned and actually start pushing ME (download) to the ONU.
903 # Currently uses a basic mib download task that create a bridge with a single gem port and uni, only allowing EAP
904 # Implement your own MibDownloadTask if you wish to setup something different by default
Matt Jeanneretc083f462019-03-11 15:02:01 -0400905 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500906 def _mib_in_sync(self):
907 self.log.debug('function-entry')
908
909 omci = self._onu_omci_device
910 in_sync = omci.mib_db_in_sync
911
Matt Jeanneretc083f462019-03-11 15:02:01 -0400912 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500913 device.reason = 'discovery-mibsync-complete'
Matt Jeanneretc083f462019-03-11 15:02:01 -0400914 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500915
916 if not self._dev_info_loaded:
917 self.log.info('loading-device-data-from-mib', in_sync=in_sync, already_loaded=self._dev_info_loaded)
918
919 omci_dev = self._onu_omci_device
920 config = omci_dev.configuration
921
922 # TODO: run this sooner somehow. shouldnt have to wait for mib sync to push an initial download
923 # In Sync, we can register logical ports now. Ideally this could occur on
924 # the first time we received a successful (no timeout) OMCI Rx response.
925 try:
926
927 # sort the lists so we get consistent port ordering.
928 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
929 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
930 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
931 veip_list = sorted(config.veip_entities) if config.veip_entities else []
932
933 if ani_list is None or (pptp_list is None and veip_list is None):
934 device.reason = 'onu-missing-required-elements'
935 self.log.warn("no-ani-or-unis")
Matt Jeanneretc083f462019-03-11 15:02:01 -0400936 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500937 raise Exception("onu-missing-required-elements")
938
939 # Currently logging the ani, pptp, veip, and uni for information purposes.
940 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
941 # And in some ONU the UNI-G list is incomplete or incorrect...
942 for entity_id in ani_list:
943 ani_value = config.ani_g_entities[entity_id]
944 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
945 # TODO: currently only one OLT PON port/ANI, so this works out. With NGPON there will be 2..?
946 self._total_tcont_count = ani_value.get('total-tcont-count')
947 self.log.debug("set-total-tcont-count", tcont_count=self._total_tcont_count)
948
949 for entity_id in uni_list:
950 uni_value = config.uni_g_entities[entity_id]
951 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
952
953 uni_entities = OrderedDict()
954 for entity_id in pptp_list:
955 pptp_value = config.pptp_entities[entity_id]
956 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
957 uni_entities[entity_id] = UniType.PPTP
958
959 for entity_id in veip_list:
960 veip_value = config.veip_entities[entity_id]
961 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
962 uni_entities[entity_id] = UniType.VEIP
963
964 uni_id = 0
965 for entity_id, uni_type in uni_entities.iteritems():
966 try:
Matt Jeanneretc083f462019-03-11 15:02:01 -0400967 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500968 uni_id += 1
969 except AssertionError as e:
970 self.log.warn("could not add UNI", entity_id=entity_id, uni_type=uni_type, e=e)
971
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500972 self._qos_flexibility = config.qos_configuration_flexibility or 0
973 self._omcc_version = config.omcc_version or OMCCVersion.Unknown
974
975 if self._unis:
976 self._dev_info_loaded = True
977 else:
978 device.reason = 'no-usable-unis'
Matt Jeanneretc083f462019-03-11 15:02:01 -0400979 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500980 self.log.warn("no-usable-unis")
981 raise Exception("no-usable-unis")
982
983 except Exception as e:
984 self.log.exception('device-info-load', e=e)
985 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
986
987 else:
988 self.log.info('device-info-already-loaded', in_sync=in_sync, already_loaded=self._dev_info_loaded)
989
990 if self._dev_info_loaded:
Matt Jeanneretad9a0f12019-05-09 14:05:49 -0400991 if device.admin_state == AdminState.PREPROVISIONED or device.admin_state == AdminState.ENABLED:
Matt Jeanneretc083f462019-03-11 15:02:01 -0400992
993 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500994 def success(_results):
995 self.log.info('mib-download-success', _results=_results)
Matt Jeanneretc083f462019-03-11 15:02:01 -0400996 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500997 device.reason = 'initial-mib-downloaded'
Chaitrashree G S01257fa2019-05-24 06:49:49 -0400998 yield self.enable_ports(device)
Matt Jeanneretc083f462019-03-11 15:02:01 -0400999 yield self.core_proxy.device_state_update(device.id,
1000 oper_status=OperStatus.ACTIVE, connect_status=ConnectStatus.REACHABLE)
1001 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001002 self._mib_download_task = None
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001003 yield self.onu_active_alarm()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001004
Matt Jeanneretc083f462019-03-11 15:02:01 -04001005 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001006 def failure(_reason):
1007 self.log.warn('mib-download-failure-retrying', _reason=_reason)
1008 device.reason = 'initial-mib-download-failure-retrying'
Matt Jeanneretc083f462019-03-11 15:02:01 -04001009 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001010 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1011
1012 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1013 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1014 self.log.info('downloading-initial-mib-configuration')
1015 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1016 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1017 self._deferred.addCallbacks(success, failure)
1018 else:
1019 self.log.info('admin-down-disabling')
1020 self.disable(device)
1021 else:
1022 self.log.info('device-info-not-loaded-skipping-mib-download')
1023
Matt Jeanneretc083f462019-03-11 15:02:01 -04001024 @inlineCallbacks
1025 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001026 self.log.debug('function-entry')
1027
Matt Jeanneretc083f462019-03-11 15:02:01 -04001028 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 -05001029
1030 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1031 uni_name = "uni-{}".format(uni_no)
1032
1033 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
1034
1035 self.log.debug('uni-port-inputs', uni_no=uni_no, uni_id=uni_id, uni_name=uni_name, uni_type=uni_type,
1036 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num)
1037
1038 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1039 uni_port.entity_id = entity_id
1040 uni_port.enabled = True
1041 uni_port.mac_bridge_port_num = mac_bridge_port_num
1042
1043 self.log.debug("created-uni-port", uni=uni_port)
1044
Matt Jeanneretc083f462019-03-11 15:02:01 -04001045 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001046
1047 self._unis[uni_port.port_number] = uni_port
1048
1049 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=self._onu_indication.onu_id,
1050 uni_ports=self._unis.values())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001051
Matt Jeanneretc083f462019-03-11 15:02:01 -04001052 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1053 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
1054 MAX_PONS_PER_OLT = 16
1055 MAX_ONUS_PER_PON = 32
1056 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001057
Matt Jeanneretc083f462019-03-11 15:02:01 -04001058 assert intf_id < MAX_PONS_PER_OLT
1059 assert onu_id < MAX_ONUS_PER_PON
1060 assert uni_id < MAX_UNIS_PER_ONU
Matt Jeanneret3b7db442019-04-22 16:29:48 -04001061 return intf_id << 11 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001062
1063 @inlineCallbacks
1064 def onu_active_alarm(self):
1065 self.log.debug('function-entry')
1066 try:
1067 device = yield self.core_proxy.get_device(self.device_id)
1068 parent_device = yield self.core_proxy.get_device(self.parent_id)
1069 olt_serial_number = parent_device.serial_number
1070
1071 self.log.debug("onu-indication-context-data",
1072 pon_id=self._onu_indication.intf_id,
1073 registration_id=self.device_id,
1074 device_id=self.device_id,
1075 onu_serial_number=device.serial_number,
1076 olt_serial_number=olt_serial_number)
1077
1078 self.log.debug("Trying to raise alarm")
1079 OnuActiveAlarm(self.alarms, self.device_id,
1080 self._onu_indication.intf_id,
1081 device.serial_number,
1082 str(self.device_id),
1083 olt_serial_number).raise_alarm()
1084 except Exception as active_alarm_error:
1085 self.log.exception('onu-activated-alarm-error',
1086 errmsg=active_alarm_error.message)
1087