blob: a2510d93c6db24e41d2a3754b9b04b897bda5f26 [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
Devmalya Paulffc89df2019-07-31 17:43:13 -040022import arrow
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050023import structlog
24
25from collections import OrderedDict
26
27from twisted.internet import reactor, task
28from twisted.internet.defer import DeferredQueue, inlineCallbacks, returnValue, TimeoutError
29
30from heartbeat import HeartBeat
Devmalya Paulffc89df2019-07-31 17:43:13 -040031from pyvoltha.adapters.extensions.events.device_events.onu.onu_active_event import OnuActiveEvent
32from pyvoltha.adapters.extensions.events.kpi.onu.onu_pm_metrics import OnuPmMetrics
33from pyvoltha.adapters.extensions.events.kpi.onu.onu_omci_pm import OnuOmciPmMetrics
34from pyvoltha.adapters.extensions.events.adapter_events import AdapterEvents
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050035
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050036import pyvoltha.common.openflow.utils as fd
37from pyvoltha.common.utils.registry import registry
38from pyvoltha.common.config.config_backend import ConsulStore
39from pyvoltha.common.config.config_backend import EtcdStore
William Kurkian8235c1e2019-03-05 12:58:28 -050040from voltha_protos.common_pb2 import OperStatus, ConnectStatus, AdminState
Matt Jeanneretc083f462019-03-11 15:02:01 -040041from 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 -040042from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
43 InterAdapterOmciMessage, PortCapability, InterAdapterTechProfileDownloadMessage
Matt Jeannereta32441c2019-03-07 05:16:37 -050044from voltha_protos.openolt_pb2 import OnuIndication
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050045from pyvoltha.adapters.extensions.omci.onu_configuration import OMCCVersion
46from pyvoltha.adapters.extensions.omci.onu_device_entry import OnuDeviceEvents, \
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050047 OnuDeviceEntry, IN_SYNC_KEY
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050048from omci.brcm_mib_download_task import BrcmMibDownloadTask
49from omci.brcm_tp_service_specific_task import BrcmTpServiceSpecificTask
50from omci.brcm_uni_lock_task import BrcmUniLockTask
51from omci.brcm_vlan_filter_task import BrcmVlanFilterTask
52from onu_gem_port import *
53from onu_tcont import *
54from pon_port import *
55from uni_port import *
56from onu_traffic_descriptor import *
57from pyvoltha.common.tech_profile.tech_profile import TechProfile
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050058
59OP = EntityOperations
60RC = ReasonCodes
61
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050062log = structlog.get_logger()
63
64_STARTUP_RETRY_WAIT = 20
65
66
67class BrcmOpenomciOnuHandler(object):
68
69 def __init__(self, adapter, device_id):
70 self.log = structlog.get_logger(device_id=device_id)
71 self.log.debug('function-entry')
72 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050073 self.core_proxy = adapter.core_proxy
74 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050075 self.parent_adapter = None
76 self.parent_id = None
77 self.device_id = device_id
78 self.incoming_messages = DeferredQueue()
79 self.event_messages = DeferredQueue()
80 self.proxy_address = None
81 self.tx_id = 0
82 self._enabled = False
Devmalya Paulffc89df2019-07-31 17:43:13 -040083 self.events = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050084 self.pm_metrics = None
85 self._omcc_version = OMCCVersion.Unknown
86 self._total_tcont_count = 0 # From ANI-G ME
87 self._qos_flexibility = 0 # From ONT2_G ME
88
89 self._onu_indication = None
90 self._unis = dict() # Port # -> UniPort
91
92 self._pon = None
93 # TODO: probably shouldnt be hardcoded, determine from olt maybe?
94 self._pon_port_number = 100
95 self.logical_device_id = None
96
97 self._heartbeat = HeartBeat.create(self, device_id)
98
99 # Set up OpenOMCI environment
100 self._onu_omci_device = None
101 self._dev_info_loaded = False
102 self._deferred = None
103
104 self._in_sync_subscription = None
105 self._connectivity_subscription = None
106 self._capabilities_subscription = None
107
108 self.mac_bridge_service_profile_entity_id = 0x201
109 self.gal_enet_profile_entity_id = 0x1
110
111 self._tp_service_specific_task = dict()
112 self._tech_profile_download_done = dict()
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400113 # Stores information related to queued vlan filter tasks
114 # Dictionary with key being uni_id and value being device,uni port ,uni id and vlan id
115
116 self._queued_vlan_filter_task = dict()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500117
118 # Initialize KV store client
119 self.args = registry('main').get_args()
120 if self.args.backend == 'etcd':
121 host, port = self.args.etcd.split(':', 1)
122 self.kv_client = EtcdStore(host, port,
123 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
124 elif self.args.backend == 'consul':
125 host, port = self.args.consul.split(':', 1)
126 self.kv_client = ConsulStore(host, port,
127 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
128 else:
129 self.log.error('Invalid-backend')
130 raise Exception("Invalid-backend-for-kv-store")
131
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500132 @property
133 def enabled(self):
134 return self._enabled
135
136 @enabled.setter
137 def enabled(self, value):
138 if self._enabled != value:
139 self._enabled = value
140
141 @property
142 def omci_agent(self):
143 return self.adapter.omci_agent
144
145 @property
146 def omci_cc(self):
147 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
148
149 @property
150 def heartbeat(self):
151 return self._heartbeat
152
153 @property
154 def uni_ports(self):
155 return self._unis.values()
156
157 def uni_port(self, port_no_or_name):
158 if isinstance(port_no_or_name, (str, unicode)):
159 return next((uni for uni in self.uni_ports
160 if uni.name == port_no_or_name), None)
161
162 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
163 return next((uni for uni in self.uni_ports
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400164 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500165
166 @property
167 def pon_port(self):
168 return self._pon
169
170 def receive_message(self, msg):
171 if self.omci_cc is not None:
172 self.omci_cc.receive_message(msg)
173
Matt Jeanneretc083f462019-03-11 15:02:01 -0400174 def get_ofp_port_info(self, device, port_no):
175 self.log.info('get_ofp_port_info', port_no=port_no, device_id=device.id)
176 cap = OFPPF_1GB_FD | OFPPF_FIBER
177
178 hw_addr=mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
179 ((device.parent_port_no >> 8 & 0xff),
180 device.parent_port_no & 0xff,
181 (port_no >> 16) & 0xff,
182 (port_no >> 8) & 0xff,
183 port_no & 0xff))
184
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400185 uni_port = self.uni_port(int(port_no))
186 name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
187 self.log.debug('ofp_port_name', port_no=port_no, name=name)
188
Matt Jeanneretc083f462019-03-11 15:02:01 -0400189 return PortCapability(
190 port=LogicalPort(
191 ofp_port=ofp_port(
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400192 name=name,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400193 hw_addr=hw_addr,
194 config=0,
195 state=OFPPS_LIVE,
196 curr=cap,
197 advertised=cap,
198 peer=cap,
199 curr_speed=OFPPF_1GB_FD,
200 max_speed=OFPPF_1GB_FD
201 ),
202 device_id=device.id,
203 device_port_no=port_no
204 )
205 )
206
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500207 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500208 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500209 def activate(self, device):
210 self.log.debug('function-entry', device=device)
211
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500212 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500213 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500214 assert device.proxy_address.device_id
215
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500216 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500217 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500218 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500219 if self.enabled is not True:
220 self.log.info('activating-new-onu')
221 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500222 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500223 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500224 device.reason = 'activating-onu'
225
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500226 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400227 # pm_metrics requires a logical device id. For now set to just device_id
228 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500229
Matt Jeannereta32441c2019-03-07 05:16:37 -0500230 yield self.core_proxy.device_update(device)
Mahir Gunyel0e1588a2019-06-27 06:12:47 -0700231 #We commented out the line below because it is now being done in openolt-adapter,
232 #in onuDiscovery step. Line can be removed after tests.
233 #yield self.core_proxy.device_state_update(device.id, oper_status=OperStatus.DISCOVERED,
234 # connect_status=ConnectStatus.REACHABLE)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500235
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500236
Mahir Gunyel0e1588a2019-06-27 06:12:47 -0700237 self.log.debug('device updated', device=device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500238
Devmalya Paul7e0be4a2019-05-08 05:18:04 -0400239 yield self._init_pon_state(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500240
Mahir Gunyel0e1588a2019-06-27 06:12:47 -0700241 self.log.debug('pon state initialized', device=device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500242 ############################################################################
Devmalya Paulffc89df2019-07-31 17:43:13 -0400243 # Setup Alarm handler
244 self.events = AdapterEvents(self.core_proxy, device.id, self.logical_device_id,
245 device.serial_number)
246 ############################################################################
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500247 # Setup PM configuration for this device
248 # Pass in ONU specific options
249 kwargs = {
250 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
251 'heartbeat': self.heartbeat,
252 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
253 }
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400254 self.log.debug('create-OnuPmMetrics', serial_number=device.serial_number)
Devmalya Paulffc89df2019-07-31 17:43:13 -0400255 self.pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400256 self.logical_device_id, device.serial_number,
257 grouped=True, freq_override=False, **kwargs)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500258 pm_config = self.pm_metrics.make_proto()
259 self._onu_omci_device.set_pm_config(self.pm_metrics.omci_pm.openomci_interval_pm)
260 self.log.info("initial-pm-config", pm_config=pm_config)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500261 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500262
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500263 # Note, ONU ID and UNI intf set in add_uni_port method
Devmalya Paulffc89df2019-07-31 17:43:13 -0400264 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500265 ani_ports=[self._pon])
aishwaryarana01a98d9fe2019-05-08 12:09:06 -0500266
267 #Start collecting stats from the device after a brief pause
268 reactor.callLater(10, self.pm_metrics.start_collector)
269
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500270 self.enabled = True
271 else:
272 self.log.info('onu-already-activated')
273
274 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400275 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500276 def reconcile(self, device):
277 self.log.debug('function-entry', device=device)
278
279 # first we verify that we got parent reference and proxy info
280 assert device.parent_id
281 assert device.proxy_address.device_id
282
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500283 if self.enabled is not True:
284 self.log.info('reconciling-broadcom-onu-device')
285
286 self._init_pon_state(device)
287
288 # need to restart state machines on vcore restart. there is no indication to do it for us.
289 self._onu_omci_device.start()
290 device.reason = "restarting-openomci"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400291 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500292
293 # TODO: this is probably a bit heavy handed
294 # Force a reboot for now. We need indications to reflow to reassign tconts and gems given vcore went away
295 # This may not be necessary when mib resync actually works
296 reactor.callLater(1, self.reboot)
297
298 self.enabled = True
299 else:
300 self.log.info('onu-already-activated')
301
302 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500303 def _init_pon_state(self, device):
304 self.log.debug('function-entry', device=device)
305
306 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500307 self._pon.add_peer(self.parent_id, self._pon_port_number)
308 self.log.debug('adding-pon-port-to-agent', pon=self._pon.get_port())
309
Matt Jeannereta32441c2019-03-07 05:16:37 -0500310 yield self.core_proxy.port_created(device.id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500311
Matt Jeanneret0c287892019-02-28 11:48:00 -0500312 self.log.debug('added-pon-port-to-agent', pon=self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500313
314 # Create and start the OpenOMCI ONU Device Entry for this ONU
315 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500316 self.core_proxy,
317 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500318 support_classes=self.adapter.broadcom_omci,
319 custom_me_map=self.adapter.custom_me_entities())
320 # Port startup
321 if self._pon is not None:
322 self._pon.enabled = True
323
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500324 def delete(self, device):
325 self.log.info('delete-onu', device=device)
326 if self.parent_adapter:
327 try:
328 self.parent_adapter.delete_child_device(self.parent_id, device)
329 except AttributeError:
330 self.log.debug('parent-device-delete-child-not-implemented')
331 else:
332 self.log.debug("parent-adapter-not-available")
333
334 def _create_tconts(self, uni_id, us_scheduler):
335 alloc_id = us_scheduler['alloc_id']
336 q_sched_policy = us_scheduler['q_sched_policy']
337 self.log.debug('create-tcont', us_scheduler=us_scheduler)
338
339 tcontdict = dict()
340 tcontdict['alloc-id'] = alloc_id
341 tcontdict['q_sched_policy'] = q_sched_policy
342 tcontdict['uni_id'] = uni_id
343
344 # TODO: Not sure what to do with any of this...
345 tddata = dict()
346 tddata['name'] = 'not-sure-td-profile'
347 tddata['fixed-bandwidth'] = "not-sure-fixed"
348 tddata['assured-bandwidth'] = "not-sure-assured"
349 tddata['maximum-bandwidth'] = "not-sure-max"
350 tddata['additional-bw-eligibility-indicator'] = "not-sure-additional"
351
352 td = OnuTrafficDescriptor.create(tddata)
353 tcont = OnuTCont.create(self, tcont=tcontdict, td=td)
354
355 self._pon.add_tcont(tcont)
356
357 self.log.debug('pon-add-tcont', tcont=tcont)
358
359 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
360 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
361 self.log.debug('create-gemport',
362 gem_ports=gem_ports, direction=direction)
363
364 for gem_port in gem_ports:
365 gemdict = dict()
366 gemdict['gemport_id'] = gem_port['gemport_id']
367 gemdict['direction'] = direction
368 gemdict['alloc_id_ref'] = alloc_id_ref
369 gemdict['encryption'] = gem_port['aes_encryption']
370 gemdict['discard_config'] = dict()
371 gemdict['discard_config']['max_probability'] = \
372 gem_port['discard_config']['max_probability']
373 gemdict['discard_config']['max_threshold'] = \
374 gem_port['discard_config']['max_threshold']
375 gemdict['discard_config']['min_threshold'] = \
376 gem_port['discard_config']['min_threshold']
377 gemdict['discard_policy'] = gem_port['discard_policy']
378 gemdict['max_q_size'] = gem_port['max_q_size']
379 gemdict['pbit_map'] = gem_port['pbit_map']
380 gemdict['priority_q'] = gem_port['priority_q']
381 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
382 gemdict['weight'] = gem_port['weight']
383 gemdict['uni_id'] = uni_id
384
385 gem_port = OnuGemPort.create(self, gem_port=gemdict)
386
387 self._pon.add_gem_port(gem_port)
388
389 self.log.debug('pon-add-gemport', gem_port=gem_port)
390
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400391 def _execute_queued_vlan_filter_tasks(self, uni_id):
392 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
393 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
394 # or initial-mib-download is not done. Once the tp_task is completed, we execute
395 # such queued vlan-filter tasks
396 try:
397 if uni_id in self._queued_vlan_filter_task:
398 self.log.info("executing-queued-vlan-filter-task",
399 uni_id=uni_id)
400 filter_info = self._queued_vlan_filter_task[uni_id]
401 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
402 uni_id, filter_info.get("uni_port"), filter_info.get("set_vlan_vid"))
403 # Now remove the entry from the dictionary
404 self._queued_vlan_filter_task[uni_id].clear()
405 self.log.debug("executed-queued-vlan-filter-task",
406 uni_id=uni_id)
407 except Exception as e:
408 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
409
410
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500411 def _do_tech_profile_configuration(self, uni_id, tp):
412 num_of_tconts = tp['num_of_tconts']
413 us_scheduler = tp['us_scheduler']
414 alloc_id = us_scheduler['alloc_id']
415 self._create_tconts(uni_id, us_scheduler)
416 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
417 self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
418 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
419 self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
420
421 def load_and_configure_tech_profile(self, uni_id, tp_path):
422 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
423
424 if uni_id not in self._tp_service_specific_task:
425 self._tp_service_specific_task[uni_id] = dict()
426
427 if uni_id not in self._tech_profile_download_done:
428 self._tech_profile_download_done[uni_id] = dict()
429
430 if tp_path not in self._tech_profile_download_done[uni_id]:
431 self._tech_profile_download_done[uni_id][tp_path] = False
432
433 if not self._tech_profile_download_done[uni_id][tp_path]:
434 try:
435 if tp_path in self._tp_service_specific_task[uni_id]:
436 self.log.info("tech-profile-config-already-in-progress",
437 tp_path=tp_path)
438 return
439
440 tp = self.kv_client[tp_path]
441 tp = ast.literal_eval(tp)
442 self.log.debug("tp-instance", tp=tp)
443 self._do_tech_profile_configuration(uni_id, tp)
William Kurkian3a206332019-04-29 11:05:47 -0400444
445 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500446 def success(_results):
447 self.log.info("tech-profile-config-done-successfully")
William Kurkian3a206332019-04-29 11:05:47 -0400448 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500449 device.reason = 'tech-profile-config-download-success'
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400450 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500451 if tp_path in self._tp_service_specific_task[uni_id]:
452 del self._tp_service_specific_task[uni_id][tp_path]
453 self._tech_profile_download_done[uni_id][tp_path] = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400454 # Now execute any vlan filter tasks that were queued for later
455 self._execute_queued_vlan_filter_tasks(uni_id)
William Kurkian3a206332019-04-29 11:05:47 -0400456 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500457 def failure(_reason):
458 self.log.warn('tech-profile-config-failure-retrying',
459 _reason=_reason)
William Kurkian3a206332019-04-29 11:05:47 -0400460 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500461 device.reason = 'tech-profile-config-download-failure-retrying'
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400462 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500463 if tp_path in self._tp_service_specific_task[uni_id]:
464 del self._tp_service_specific_task[uni_id][tp_path]
465 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self.load_and_configure_tech_profile,
466 uni_id, tp_path)
467
468 self.log.info('downloading-tech-profile-configuration')
469 self._tp_service_specific_task[uni_id][tp_path] = \
470 BrcmTpServiceSpecificTask(self.omci_agent, self, uni_id)
471 self._deferred = \
472 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
473 self._deferred.addCallbacks(success, failure)
474
475 except Exception as e:
476 self.log.exception("error-loading-tech-profile", e=e)
477 else:
478 self.log.info("tech-profile-config-already-done")
479
480 def update_pm_config(self, device, pm_config):
481 # TODO: This has not been tested
482 self.log.info('update_pm_config', pm_config=pm_config)
483 self.pm_metrics.update(pm_config)
484
485 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
486 # flow decomposition that ultimately comes from onos
487 def update_flow_table(self, device, flows):
488 self.log.debug('function-entry', device=device, flows=flows)
489
490 #
491 # We need to proxy through the OLT to get to the ONU
492 # Configuration from here should be using OMCI
493 #
494 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
495
496 # no point in pushing omci flows if the device isnt reachable
497 if device.connect_status != ConnectStatus.REACHABLE or \
498 device.admin_state != AdminState.ENABLED:
499 self.log.warn("device-disabled-or-offline-skipping-flow-update",
500 admin=device.admin_state, connect=device.connect_status)
501 return
502
503 def is_downstream(port):
504 return port == self._pon_port_number
505
506 def is_upstream(port):
507 return not is_downstream(port)
508
509 for flow in flows:
510 _type = None
511 _port = None
512 _vlan_vid = None
513 _udp_dst = None
514 _udp_src = None
515 _ipv4_dst = None
516 _ipv4_src = None
517 _metadata = None
518 _output = None
519 _push_tpid = None
520 _field = None
521 _set_vlan_vid = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400522 _tunnel_id = None
523
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500524 self.log.debug('bulk-flow-update', device_id=device.id, flow=flow)
525 try:
526 _in_port = fd.get_in_port(flow)
527 assert _in_port is not None
528
529 _out_port = fd.get_out_port(flow) # may be None
530
531 if is_downstream(_in_port):
532 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
533 uni_port = self.uni_port(_out_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400534 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500535 elif is_upstream(_in_port):
536 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
537 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400538 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500539 else:
540 raise Exception('port should be 1 or 2 by our convention')
541
542 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
543
544 for field in fd.get_ofb_fields(flow):
545 if field.type == fd.ETH_TYPE:
546 _type = field.eth_type
547 self.log.debug('field-type-eth-type',
548 eth_type=_type)
549
550 elif field.type == fd.IP_PROTO:
551 _proto = field.ip_proto
552 self.log.debug('field-type-ip-proto',
553 ip_proto=_proto)
554
555 elif field.type == fd.IN_PORT:
556 _port = field.port
557 self.log.debug('field-type-in-port',
558 in_port=_port)
559
560 elif field.type == fd.VLAN_VID:
561 _vlan_vid = field.vlan_vid & 0xfff
562 self.log.debug('field-type-vlan-vid',
563 vlan=_vlan_vid)
564
565 elif field.type == fd.VLAN_PCP:
566 _vlan_pcp = field.vlan_pcp
567 self.log.debug('field-type-vlan-pcp',
568 pcp=_vlan_pcp)
569
570 elif field.type == fd.UDP_DST:
571 _udp_dst = field.udp_dst
572 self.log.debug('field-type-udp-dst',
573 udp_dst=_udp_dst)
574
575 elif field.type == fd.UDP_SRC:
576 _udp_src = field.udp_src
577 self.log.debug('field-type-udp-src',
578 udp_src=_udp_src)
579
580 elif field.type == fd.IPV4_DST:
581 _ipv4_dst = field.ipv4_dst
582 self.log.debug('field-type-ipv4-dst',
583 ipv4_dst=_ipv4_dst)
584
585 elif field.type == fd.IPV4_SRC:
586 _ipv4_src = field.ipv4_src
587 self.log.debug('field-type-ipv4-src',
588 ipv4_dst=_ipv4_src)
589
590 elif field.type == fd.METADATA:
591 _metadata = field.table_metadata
592 self.log.debug('field-type-metadata',
593 metadata=_metadata)
594
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400595 elif field.type == fd.TUNNEL_ID:
596 _tunnel_id = field.tunnel_id
597 self.log.debug('field-type-tunnel-id',
598 tunnel_id=_tunnel_id)
599
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500600 else:
601 raise NotImplementedError('field.type={}'.format(
602 field.type))
603
604 for action in fd.get_actions(flow):
605
606 if action.type == fd.OUTPUT:
607 _output = action.output.port
608 self.log.debug('action-type-output',
609 output=_output, in_port=_in_port)
610
611 elif action.type == fd.POP_VLAN:
612 self.log.debug('action-type-pop-vlan',
613 in_port=_in_port)
614
615 elif action.type == fd.PUSH_VLAN:
616 _push_tpid = action.push.ethertype
617 self.log.debug('action-type-push-vlan',
618 push_tpid=_push_tpid, in_port=_in_port)
619 if action.push.ethertype != 0x8100:
620 self.log.error('unhandled-tpid',
621 ethertype=action.push.ethertype)
622
623 elif action.type == fd.SET_FIELD:
624 _field = action.set_field.field.ofb_field
625 assert (action.set_field.field.oxm_class ==
626 OFPXMC_OPENFLOW_BASIC)
627 self.log.debug('action-type-set-field',
628 field=_field, in_port=_in_port)
629 if _field.type == fd.VLAN_VID:
630 _set_vlan_vid = _field.vlan_vid & 0xfff
631 self.log.debug('set-field-type-vlan-vid',
632 vlan_vid=_set_vlan_vid)
633 else:
634 self.log.error('unsupported-action-set-field-type',
635 field_type=_field.type)
636 else:
637 self.log.error('unsupported-action-type',
638 action_type=action.type, in_port=_in_port)
639
640 # TODO: We only set vlan omci flows. Handle omci matching ethertypes at some point in another task
641 if _type is not None:
642 self.log.warn('ignoring-flow-with-ethType', ethType=_type)
643 elif _set_vlan_vid is None or _set_vlan_vid == 0:
644 self.log.warn('ignorning-flow-that-does-not-set-vlanid')
645 else:
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400646 self.log.warn('set-vlanid', uni_id=uni_id, uni_port=uni_port, set_vlan_vid=_set_vlan_vid)
647 self._add_vlan_filter_task(device, uni_id, uni_port, _set_vlan_vid)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500648 except Exception as e:
649 self.log.exception('failed-to-install-flow', e=e, flow=flow)
650
651
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400652
653 def _add_vlan_filter_task(self, device,uni_id, uni_port, _set_vlan_vid):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500654 assert uni_port is not None
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400655 if uni_id in self._tech_profile_download_done and self._tech_profile_download_done[uni_id] != {}:
656 @inlineCallbacks
657 def success(_results):
658 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid)
659 device.reason = 'omci-flows-pushed'
660 yield self.core_proxy.device_update(device)
661 self._vlan_filter_task = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500662
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400663 @inlineCallbacks
664 def failure(_reason):
665 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid)
666 device.reason = 'omci-flows-failed-retrying'
667 yield self.core_proxy.device_update(device)
668 self._vlan_filter_task = reactor.callLater(_STARTUP_RETRY_WAIT,
669 self._add_vlan_filter_task, device,uni_port.port_number, uni_port, _set_vlan_vid)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500670
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400671 self.log.info('setting-vlan-tag')
672 self._vlan_filter_task = BrcmVlanFilterTask(self.omci_agent, self.device_id, uni_port, _set_vlan_vid)
673 self._deferred = self._onu_omci_device.task_runner.queue_task(self._vlan_filter_task)
674 self._deferred.addCallbacks(success, failure)
675 else:
676 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
677 uni_id=uni_id)
678 self._queued_vlan_filter_task[uni_id] = {"device": device, \
679 "uni_id":uni_id,
680 "uni_port": uni_port,
681 "set_vlan_vid": _set_vlan_vid}
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500682
683 def get_tx_id(self):
684 self.log.debug('function-entry')
685 self.tx_id += 1
686 return self.tx_id
687
Matt Jeannereta32441c2019-03-07 05:16:37 -0500688 def process_inter_adapter_message(self, request):
689 self.log.debug('process-inter-adapter-message', msg=request)
690 try:
691 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
692 omci_msg = InterAdapterOmciMessage()
693 request.body.Unpack(omci_msg)
694 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500695
Matt Jeannereta32441c2019-03-07 05:16:37 -0500696 self.receive_message(omci_msg.message)
697
698 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
699 onu_indication = OnuIndication()
700 request.body.Unpack(onu_indication)
701 self.log.debug('inter-adapter-recv-onu-ind', onu_indication=onu_indication)
702
703 if onu_indication.oper_state == "up":
704 self.create_interface(onu_indication)
705 elif onu_indication.oper_state == "down":
706 self.update_interface(onu_indication)
707 else:
708 self.log.error("unknown-onu-indication", onu_indication=onu_indication)
709
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400710 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
711 tech_msg = InterAdapterTechProfileDownloadMessage()
712 request.body.Unpack(tech_msg)
713 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
714
715 self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
716
Matt Jeannereta32441c2019-03-07 05:16:37 -0500717 else:
718 self.log.error("inter-adapter-unhandled-type", request=request)
719
720 except Exception as e:
721 self.log.exception("error-processing-inter-adapter-message", e=e)
722
723 # Called each time there is an onu "up" indication from the olt handler
724 @inlineCallbacks
725 def create_interface(self, onu_indication):
726 self.log.debug('function-entry', onu_indication=onu_indication)
727 self._onu_indication = onu_indication
728
Matt Jeanneretc083f462019-03-11 15:02:01 -0400729 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
730 connect_status=ConnectStatus.REACHABLE)
731
Matt Jeannereta32441c2019-03-07 05:16:37 -0500732 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500733
734 self.log.debug('starting-openomci-statemachine')
735 self._subscribe_to_events()
736 reactor.callLater(1, self._onu_omci_device.start)
737 onu_device.reason = "starting-openomci"
Matt Jeannereta32441c2019-03-07 05:16:37 -0500738 yield self.core_proxy.device_update(onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500739 self._heartbeat.enabled = True
740
741 # Currently called each time there is an onu "down" indication from the olt handler
742 # TODO: possibly other reasons to "update" from the olt?
Matt Jeannereta32441c2019-03-07 05:16:37 -0500743 @inlineCallbacks
744 def update_interface(self, onu_indication):
745 self.log.debug('function-entry', onu_indication=onu_indication)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500746
Matt Jeannereta32441c2019-03-07 05:16:37 -0500747 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500748
Matt Jeannereta32441c2019-03-07 05:16:37 -0500749 if onu_indication.oper_state == 'down':
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500750 self.log.debug('stopping-openomci-statemachine')
751 reactor.callLater(0, self._onu_omci_device.stop)
752
753 # Let TP download happen again
754 for uni_id in self._tp_service_specific_task:
755 self._tp_service_specific_task[uni_id].clear()
756 for uni_id in self._tech_profile_download_done:
757 self._tech_profile_download_done[uni_id].clear()
758
759 self.disable_ports(onu_device)
760 onu_device.reason = "stopping-openomci"
Chaitrashree G S01257fa2019-05-24 06:49:49 -0400761 yield self.core_proxy.device_update(onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500762 onu_device.connect_status = ConnectStatus.UNREACHABLE
763 onu_device.oper_status = OperStatus.DISCOVERED
Chaitrashree G S01257fa2019-05-24 06:49:49 -0400764 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 -0500765 else:
766 self.log.debug('not-changing-openomci-statemachine')
767
768 # Not currently called by olt or anything else
William Kurkian3a206332019-04-29 11:05:47 -0400769 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500770 def remove_interface(self, data):
771 self.log.debug('function-entry', data=data)
772
William Kurkian3a206332019-04-29 11:05:47 -0400773 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500774
775 self.log.debug('stopping-openomci-statemachine')
776 reactor.callLater(0, self._onu_omci_device.stop)
777
778 # Let TP download happen again
779 for uni_id in self._tp_service_specific_task:
780 self._tp_service_specific_task[uni_id].clear()
781 for uni_id in self._tech_profile_download_done:
782 self._tech_profile_download_done[uni_id].clear()
783
784 self.disable_ports(onu_device)
785 onu_device.reason = "stopping-openomci"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400786 yield self.core_proxy.device_update(onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500787
788 # TODO: im sure there is more to do here
789
790 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400791 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500792 def remove_gemport(self, data):
793 self.log.debug('remove-gemport', data=data)
William Kurkian3a206332019-04-29 11:05:47 -0400794 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500795 if device.connect_status != ConnectStatus.REACHABLE:
796 self.log.error('device-unreachable')
797 return
798
799 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400800 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500801 def remove_tcont(self, tcont_data, traffic_descriptor_data):
802 self.log.debug('remove-tcont', tcont_data=tcont_data, traffic_descriptor_data=traffic_descriptor_data)
William Kurkian3a206332019-04-29 11:05:47 -0400803 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500804 if device.connect_status != ConnectStatus.REACHABLE:
805 self.log.error('device-unreachable')
806 return
807
808 # TODO: Create some omci task that encompases this what intended
809
810 # Not currently called. Would be called presumably from the olt handler
811 def create_multicast_gemport(self, data):
812 self.log.debug('function-entry', data=data)
813
814 # TODO: create objects and populate for later omci calls
815
816 def disable(self, device):
817 self.log.debug('function-entry', device=device)
818 try:
819 self.log.info('sending-uni-lock-towards-device', device=device)
820
Matt Jeanneret80766692019-05-03 09:58:38 -0400821 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500822 def stop_anyway(reason):
823 # proceed with disable regardless if we could reach the onu. for example onu is unplugged
824 self.log.debug('stopping-openomci-statemachine')
825 reactor.callLater(0, self._onu_omci_device.stop)
826
827 # Let TP download happen again
828 for uni_id in self._tp_service_specific_task:
829 self._tp_service_specific_task[uni_id].clear()
830 for uni_id in self._tech_profile_download_done:
831 self._tech_profile_download_done[uni_id].clear()
832
833 self.disable_ports(device)
834 device.oper_status = OperStatus.UNKNOWN
835 device.reason = "omci-admin-lock"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400836 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500837
838 # lock all the unis
839 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=True)
840 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
841 self._deferred.addCallbacks(stop_anyway, stop_anyway)
842 except Exception as e:
843 log.exception('exception-in-onu-disable', exception=e)
844
William Kurkian3a206332019-04-29 11:05:47 -0400845 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500846 def reenable(self, device):
847 self.log.debug('function-entry', device=device)
848 try:
849 # Start up OpenOMCI state machines for this device
850 # this will ultimately resync mib and unlock unis on successful redownloading the mib
851 self.log.debug('restarting-openomci-statemachine')
852 self._subscribe_to_events()
853 device.reason = "restarting-openomci"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400854 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500855 reactor.callLater(1, self._onu_omci_device.start)
856 self._heartbeat.enabled = True
857 except Exception as e:
858 log.exception('exception-in-onu-reenable', exception=e)
859
William Kurkian3a206332019-04-29 11:05:47 -0400860 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500861 def reboot(self):
862 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -0400863 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500864 if device.connect_status != ConnectStatus.REACHABLE:
865 self.log.error("device-unreachable")
866 return
867
William Kurkian3a206332019-04-29 11:05:47 -0400868 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500869 def success(_results):
870 self.log.info('reboot-success', _results=_results)
871 self.disable_ports(device)
872 device.connect_status = ConnectStatus.UNREACHABLE
873 device.oper_status = OperStatus.DISCOVERED
874 device.reason = "rebooting"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400875 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500876
877 def failure(_reason):
878 self.log.info('reboot-failure', _reason=_reason)
879
880 self._deferred = self._onu_omci_device.reboot()
881 self._deferred.addCallbacks(success, failure)
882
William Kurkian3a206332019-04-29 11:05:47 -0400883 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500884 def disable_ports(self, onu_device):
Matt Jeanneret80766692019-05-03 09:58:38 -0400885 self.log.info('disable-ports', device_id=self.device_id, onu_device=onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500886
887 # Disable all ports on that device
Matt Jeanneret80766692019-05-03 09:58:38 -0400888 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500889
William Kurkian3a206332019-04-29 11:05:47 -0400890 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500891 def enable_ports(self, onu_device):
892 self.log.info('enable-ports', device_id=self.device_id, onu_device=onu_device)
893
Matt Jeanneret80766692019-05-03 09:58:38 -0400894 # Enable all ports on that device
895 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.ACTIVE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500896
897 # Called just before openomci state machine is started. These listen for events from selected state machines,
898 # most importantly, mib in sync. Which ultimately leads to downloading the mib
899 def _subscribe_to_events(self):
900 self.log.debug('function-entry')
901
902 # OMCI MIB Database sync status
903 bus = self._onu_omci_device.event_bus
904 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
905 OnuDeviceEvents.MibDatabaseSyncEvent)
906 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
907
908 # OMCI Capabilities
909 bus = self._onu_omci_device.event_bus
910 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
911 OnuDeviceEvents.OmciCapabilitiesEvent)
912 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
913
914 # Called when the mib is in sync
915 def in_sync_handler(self, _topic, msg):
916 self.log.debug('function-entry', _topic=_topic, msg=msg)
917 if self._in_sync_subscription is not None:
918 try:
919 in_sync = msg[IN_SYNC_KEY]
920
921 if in_sync:
922 # Only call this once
923 bus = self._onu_omci_device.event_bus
924 bus.unsubscribe(self._in_sync_subscription)
925 self._in_sync_subscription = None
926
927 # Start up device_info load
928 self.log.debug('running-mib-sync')
929 reactor.callLater(0, self._mib_in_sync)
930
931 except Exception as e:
932 self.log.exception('in-sync', e=e)
933
934 def capabilties_handler(self, _topic, _msg):
935 self.log.debug('function-entry', _topic=_topic, msg=_msg)
936 if self._capabilities_subscription is not None:
937 self.log.debug('capabilities-handler-done')
938
939 # Mib is in sync, we can now query what we learned and actually start pushing ME (download) to the ONU.
940 # Currently uses a basic mib download task that create a bridge with a single gem port and uni, only allowing EAP
941 # Implement your own MibDownloadTask if you wish to setup something different by default
Matt Jeanneretc083f462019-03-11 15:02:01 -0400942 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500943 def _mib_in_sync(self):
944 self.log.debug('function-entry')
945
946 omci = self._onu_omci_device
947 in_sync = omci.mib_db_in_sync
948
Matt Jeanneretc083f462019-03-11 15:02:01 -0400949 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500950 device.reason = 'discovery-mibsync-complete'
Matt Jeanneretc083f462019-03-11 15:02:01 -0400951 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500952
953 if not self._dev_info_loaded:
954 self.log.info('loading-device-data-from-mib', in_sync=in_sync, already_loaded=self._dev_info_loaded)
955
956 omci_dev = self._onu_omci_device
957 config = omci_dev.configuration
958
959 # TODO: run this sooner somehow. shouldnt have to wait for mib sync to push an initial download
960 # In Sync, we can register logical ports now. Ideally this could occur on
961 # the first time we received a successful (no timeout) OMCI Rx response.
962 try:
963
964 # sort the lists so we get consistent port ordering.
965 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
966 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
967 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
968 veip_list = sorted(config.veip_entities) if config.veip_entities else []
969
970 if ani_list is None or (pptp_list is None and veip_list is None):
971 device.reason = 'onu-missing-required-elements'
972 self.log.warn("no-ani-or-unis")
Matt Jeanneretc083f462019-03-11 15:02:01 -0400973 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500974 raise Exception("onu-missing-required-elements")
975
976 # Currently logging the ani, pptp, veip, and uni for information purposes.
977 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
978 # And in some ONU the UNI-G list is incomplete or incorrect...
979 for entity_id in ani_list:
980 ani_value = config.ani_g_entities[entity_id]
981 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
982 # TODO: currently only one OLT PON port/ANI, so this works out. With NGPON there will be 2..?
983 self._total_tcont_count = ani_value.get('total-tcont-count')
984 self.log.debug("set-total-tcont-count", tcont_count=self._total_tcont_count)
985
986 for entity_id in uni_list:
987 uni_value = config.uni_g_entities[entity_id]
988 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
989
990 uni_entities = OrderedDict()
991 for entity_id in pptp_list:
992 pptp_value = config.pptp_entities[entity_id]
993 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
994 uni_entities[entity_id] = UniType.PPTP
995
996 for entity_id in veip_list:
997 veip_value = config.veip_entities[entity_id]
998 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
999 uni_entities[entity_id] = UniType.VEIP
1000
1001 uni_id = 0
1002 for entity_id, uni_type in uni_entities.iteritems():
1003 try:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001004 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001005 uni_id += 1
1006 except AssertionError as e:
1007 self.log.warn("could not add UNI", entity_id=entity_id, uni_type=uni_type, e=e)
1008
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001009 self._qos_flexibility = config.qos_configuration_flexibility or 0
1010 self._omcc_version = config.omcc_version or OMCCVersion.Unknown
1011
1012 if self._unis:
1013 self._dev_info_loaded = True
1014 else:
1015 device.reason = 'no-usable-unis'
Matt Jeanneretc083f462019-03-11 15:02:01 -04001016 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001017 self.log.warn("no-usable-unis")
1018 raise Exception("no-usable-unis")
1019
1020 except Exception as e:
1021 self.log.exception('device-info-load', e=e)
1022 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1023
1024 else:
1025 self.log.info('device-info-already-loaded', in_sync=in_sync, already_loaded=self._dev_info_loaded)
1026
1027 if self._dev_info_loaded:
Matt Jeanneretad9a0f12019-05-09 14:05:49 -04001028 if device.admin_state == AdminState.PREPROVISIONED or device.admin_state == AdminState.ENABLED:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001029
1030 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001031 def success(_results):
1032 self.log.info('mib-download-success', _results=_results)
Matt Jeanneretc083f462019-03-11 15:02:01 -04001033 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001034 device.reason = 'initial-mib-downloaded'
Chaitrashree G S01257fa2019-05-24 06:49:49 -04001035 yield self.enable_ports(device)
Matt Jeanneretc083f462019-03-11 15:02:01 -04001036 yield self.core_proxy.device_state_update(device.id,
1037 oper_status=OperStatus.ACTIVE, connect_status=ConnectStatus.REACHABLE)
1038 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001039 self._mib_download_task = None
Devmalya Paulffc89df2019-07-31 17:43:13 -04001040 yield self.onu_active_event()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001041
Matt Jeanneretc083f462019-03-11 15:02:01 -04001042 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001043 def failure(_reason):
1044 self.log.warn('mib-download-failure-retrying', _reason=_reason)
1045 device.reason = 'initial-mib-download-failure-retrying'
Matt Jeanneretc083f462019-03-11 15:02:01 -04001046 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001047 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1048
1049 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1050 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1051 self.log.info('downloading-initial-mib-configuration')
1052 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1053 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1054 self._deferred.addCallbacks(success, failure)
1055 else:
1056 self.log.info('admin-down-disabling')
1057 self.disable(device)
1058 else:
1059 self.log.info('device-info-not-loaded-skipping-mib-download')
1060
Matt Jeanneretc083f462019-03-11 15:02:01 -04001061 @inlineCallbacks
1062 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001063 self.log.debug('function-entry')
1064
Matt Jeanneretc083f462019-03-11 15:02:01 -04001065 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 -05001066
1067 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1068 uni_name = "uni-{}".format(uni_no)
1069
1070 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
1071
1072 self.log.debug('uni-port-inputs', uni_no=uni_no, uni_id=uni_id, uni_name=uni_name, uni_type=uni_type,
Yongjie Zhang286099c2019-08-06 13:39:07 -04001073 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001074
1075 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1076 uni_port.entity_id = entity_id
1077 uni_port.enabled = True
1078 uni_port.mac_bridge_port_num = mac_bridge_port_num
1079
1080 self.log.debug("created-uni-port", uni=uni_port)
1081
Matt Jeanneretc083f462019-03-11 15:02:01 -04001082 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001083
1084 self._unis[uni_port.port_number] = uni_port
1085
1086 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=self._onu_indication.onu_id,
Yongjie Zhang286099c2019-08-06 13:39:07 -04001087 uni_ports=self._unis.values(), serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001088
Matt Jeanneretc083f462019-03-11 15:02:01 -04001089 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1090 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
1091 MAX_PONS_PER_OLT = 16
Mahir Gunyel0e1588a2019-06-27 06:12:47 -07001092 MAX_ONUS_PER_PON = 128
Matt Jeanneretc083f462019-03-11 15:02:01 -04001093 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001094
Matt Jeanneretc083f462019-03-11 15:02:01 -04001095 assert intf_id < MAX_PONS_PER_OLT
1096 assert onu_id < MAX_ONUS_PER_PON
1097 assert uni_id < MAX_UNIS_PER_ONU
Matt Jeanneret3b7db442019-04-22 16:29:48 -04001098 return intf_id << 11 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001099
1100 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001101 def onu_active_event(self):
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001102 self.log.debug('function-entry')
1103 try:
1104 device = yield self.core_proxy.get_device(self.device_id)
1105 parent_device = yield self.core_proxy.get_device(self.parent_id)
1106 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001107 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001108
1109 self.log.debug("onu-indication-context-data",
1110 pon_id=self._onu_indication.intf_id,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001111 onu_id=self._onu_indication.onu_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001112 registration_id=self.device_id,
1113 device_id=self.device_id,
1114 onu_serial_number=device.serial_number,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001115 olt_serial_number=olt_serial_number,
1116 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001117
Devmalya Paulffc89df2019-07-31 17:43:13 -04001118 self.log.debug("Trying-to-raise-onu-active-event")
1119 OnuActiveEvent(self.events, self.device_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001120 self._onu_indication.intf_id,
1121 device.serial_number,
1122 str(self.device_id),
Devmalya Paulffc89df2019-07-31 17:43:13 -04001123 olt_serial_number,raised_ts,
1124 onu_id=self._onu_indication.onu_id).send(True)
1125 except Exception as active_event_error:
1126 self.log.exception('onu-activated-event-error',
1127 errmsg=active_event_error.message)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001128