blob: cd221a5aeea3c0a25fd5924a65cd22ef905aa1f3 [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
onkarkundargiaae99712019-09-23 15:02:52 +053058from pyvoltha.adapters.extensions.omci.tasks.omci_test_request import OmciTestRequest
59from pyvoltha.adapters.extensions.omci.omci_entities import AniG
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050060
61OP = EntityOperations
62RC = ReasonCodes
63
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050064log = structlog.get_logger()
65
66_STARTUP_RETRY_WAIT = 20
67
68
69class BrcmOpenomciOnuHandler(object):
70
71 def __init__(self, adapter, device_id):
72 self.log = structlog.get_logger(device_id=device_id)
73 self.log.debug('function-entry')
74 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050075 self.core_proxy = adapter.core_proxy
76 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050077 self.parent_adapter = None
78 self.parent_id = None
79 self.device_id = device_id
80 self.incoming_messages = DeferredQueue()
81 self.event_messages = DeferredQueue()
82 self.proxy_address = None
83 self.tx_id = 0
84 self._enabled = False
Devmalya Paulffc89df2019-07-31 17:43:13 -040085 self.events = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050086 self.pm_metrics = None
87 self._omcc_version = OMCCVersion.Unknown
88 self._total_tcont_count = 0 # From ANI-G ME
89 self._qos_flexibility = 0 # From ONT2_G ME
90
91 self._onu_indication = None
92 self._unis = dict() # Port # -> UniPort
93
94 self._pon = None
95 # TODO: probably shouldnt be hardcoded, determine from olt maybe?
96 self._pon_port_number = 100
97 self.logical_device_id = None
98
99 self._heartbeat = HeartBeat.create(self, device_id)
100
101 # Set up OpenOMCI environment
102 self._onu_omci_device = None
103 self._dev_info_loaded = False
104 self._deferred = None
105
106 self._in_sync_subscription = None
107 self._connectivity_subscription = None
108 self._capabilities_subscription = None
109
110 self.mac_bridge_service_profile_entity_id = 0x201
111 self.gal_enet_profile_entity_id = 0x1
112
113 self._tp_service_specific_task = dict()
114 self._tech_profile_download_done = dict()
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400115 # Stores information related to queued vlan filter tasks
116 # Dictionary with key being uni_id and value being device,uni port ,uni id and vlan id
117
118 self._queued_vlan_filter_task = dict()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500119
120 # Initialize KV store client
121 self.args = registry('main').get_args()
122 if self.args.backend == 'etcd':
123 host, port = self.args.etcd.split(':', 1)
124 self.kv_client = EtcdStore(host, port,
125 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
126 elif self.args.backend == 'consul':
127 host, port = self.args.consul.split(':', 1)
128 self.kv_client = ConsulStore(host, port,
129 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
130 else:
131 self.log.error('Invalid-backend')
132 raise Exception("Invalid-backend-for-kv-store")
133
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500134 @property
135 def enabled(self):
136 return self._enabled
137
138 @enabled.setter
139 def enabled(self, value):
140 if self._enabled != value:
141 self._enabled = value
142
143 @property
144 def omci_agent(self):
145 return self.adapter.omci_agent
146
147 @property
148 def omci_cc(self):
149 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
150
151 @property
152 def heartbeat(self):
153 return self._heartbeat
154
155 @property
156 def uni_ports(self):
157 return self._unis.values()
158
159 def uni_port(self, port_no_or_name):
160 if isinstance(port_no_or_name, (str, unicode)):
161 return next((uni for uni in self.uni_ports
162 if uni.name == port_no_or_name), None)
163
164 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
165 return next((uni for uni in self.uni_ports
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400166 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500167
168 @property
169 def pon_port(self):
170 return self._pon
171
172 def receive_message(self, msg):
173 if self.omci_cc is not None:
174 self.omci_cc.receive_message(msg)
175
Matt Jeanneretc083f462019-03-11 15:02:01 -0400176 def get_ofp_port_info(self, device, port_no):
177 self.log.info('get_ofp_port_info', port_no=port_no, device_id=device.id)
178 cap = OFPPF_1GB_FD | OFPPF_FIBER
179
180 hw_addr=mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
181 ((device.parent_port_no >> 8 & 0xff),
182 device.parent_port_no & 0xff,
183 (port_no >> 16) & 0xff,
184 (port_no >> 8) & 0xff,
185 port_no & 0xff))
186
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400187 uni_port = self.uni_port(int(port_no))
188 name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
189 self.log.debug('ofp_port_name', port_no=port_no, name=name)
190
Matt Jeanneretc083f462019-03-11 15:02:01 -0400191 return PortCapability(
192 port=LogicalPort(
193 ofp_port=ofp_port(
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400194 name=name,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400195 hw_addr=hw_addr,
196 config=0,
197 state=OFPPS_LIVE,
198 curr=cap,
199 advertised=cap,
200 peer=cap,
201 curr_speed=OFPPF_1GB_FD,
202 max_speed=OFPPF_1GB_FD
203 ),
204 device_id=device.id,
205 device_port_no=port_no
206 )
207 )
208
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500209 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500210 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500211 def activate(self, device):
212 self.log.debug('function-entry', device=device)
213
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500214 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500215 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500216 assert device.proxy_address.device_id
217
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500218 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500219 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500220 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500221 if self.enabled is not True:
222 self.log.info('activating-new-onu')
223 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500224 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500225 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500226 device.reason = 'activating-onu'
227
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500228 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400229 # pm_metrics requires a logical device id. For now set to just device_id
230 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500231
Matt Jeannereta32441c2019-03-07 05:16:37 -0500232 yield self.core_proxy.device_update(device)
Mahir Gunyel0e1588a2019-06-27 06:12:47 -0700233 self.log.debug('device updated', device=device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500234
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700235 yield self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500236
Mahir Gunyel0e1588a2019-06-27 06:12:47 -0700237 self.log.debug('pon state initialized', device=device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500238 ############################################################################
Devmalya Paulffc89df2019-07-31 17:43:13 -0400239 # Setup Alarm handler
240 self.events = AdapterEvents(self.core_proxy, device.id, self.logical_device_id,
241 device.serial_number)
242 ############################################################################
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500243 # Setup PM configuration for this device
244 # Pass in ONU specific options
245 kwargs = {
246 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
247 'heartbeat': self.heartbeat,
248 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
249 }
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400250 self.log.debug('create-OnuPmMetrics', serial_number=device.serial_number)
Devmalya Paulffc89df2019-07-31 17:43:13 -0400251 self.pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400252 self.logical_device_id, device.serial_number,
253 grouped=True, freq_override=False, **kwargs)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500254 pm_config = self.pm_metrics.make_proto()
255 self._onu_omci_device.set_pm_config(self.pm_metrics.omci_pm.openomci_interval_pm)
256 self.log.info("initial-pm-config", pm_config=pm_config)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500257 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500258
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500259 # Note, ONU ID and UNI intf set in add_uni_port method
Devmalya Paulffc89df2019-07-31 17:43:13 -0400260 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500261 ani_ports=[self._pon])
aishwaryarana01a98d9fe2019-05-08 12:09:06 -0500262
263 #Start collecting stats from the device after a brief pause
264 reactor.callLater(10, self.pm_metrics.start_collector)
265
onkarkundargiaae99712019-09-23 15:02:52 +0530266 # Code to Run OMCI Test Action
267 kwargs_omci_test_action = {
268 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
269 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
270 }
271 serial_number = device.serial_number
272 test_request = OmciTestRequest(self.core_proxy,
273 self.omci_agent, self.device_id,
274 AniG, serial_number,
275 self.logical_device_id,
276 exclusive=False,
277 **kwargs_omci_test_action)
278 reactor.callLater(60, test_request.start_collector)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500279 self.enabled = True
280 else:
281 self.log.info('onu-already-activated')
282
283 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400284 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500285 def reconcile(self, device):
286 self.log.debug('function-entry', device=device)
287
288 # first we verify that we got parent reference and proxy info
289 assert device.parent_id
290 assert device.proxy_address.device_id
291
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700292 self.proxy_address = device.proxy_address
293 self.parent_id = device.parent_id
294 self._pon_port_number = device.parent_port_no
295
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500296 if self.enabled is not True:
297 self.log.info('reconciling-broadcom-onu-device')
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700298 self.logical_device_id = self.device_id
299 self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500300
301 # need to restart state machines on vcore restart. there is no indication to do it for us.
302 self._onu_omci_device.start()
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700303 yield self.core_proxy.device_reason_update(self.device_id, "restarting-openomci")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500304
305 # TODO: this is probably a bit heavy handed
306 # Force a reboot for now. We need indications to reflow to reassign tconts and gems given vcore went away
307 # This may not be necessary when mib resync actually works
308 reactor.callLater(1, self.reboot)
309
310 self.enabled = True
311 else:
312 self.log.info('onu-already-activated')
313
314 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700315 def _init_pon_state(self):
316 self.log.debug('function-entry', deviceId=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500317
318 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500319 self._pon.add_peer(self.parent_id, self._pon_port_number)
320 self.log.debug('adding-pon-port-to-agent', pon=self._pon.get_port())
321
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700322 yield self.core_proxy.port_created(self.device_id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500323
Matt Jeanneret0c287892019-02-28 11:48:00 -0500324 self.log.debug('added-pon-port-to-agent', pon=self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500325
326 # Create and start the OpenOMCI ONU Device Entry for this ONU
327 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500328 self.core_proxy,
329 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500330 support_classes=self.adapter.broadcom_omci,
331 custom_me_map=self.adapter.custom_me_entities())
332 # Port startup
333 if self._pon is not None:
334 self._pon.enabled = True
335
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500336 def delete(self, device):
337 self.log.info('delete-onu', device=device)
338 if self.parent_adapter:
339 try:
340 self.parent_adapter.delete_child_device(self.parent_id, device)
341 except AttributeError:
342 self.log.debug('parent-device-delete-child-not-implemented')
343 else:
344 self.log.debug("parent-adapter-not-available")
345
346 def _create_tconts(self, uni_id, us_scheduler):
347 alloc_id = us_scheduler['alloc_id']
348 q_sched_policy = us_scheduler['q_sched_policy']
349 self.log.debug('create-tcont', us_scheduler=us_scheduler)
350
351 tcontdict = dict()
352 tcontdict['alloc-id'] = alloc_id
353 tcontdict['q_sched_policy'] = q_sched_policy
354 tcontdict['uni_id'] = uni_id
355
356 # TODO: Not sure what to do with any of this...
357 tddata = dict()
358 tddata['name'] = 'not-sure-td-profile'
359 tddata['fixed-bandwidth'] = "not-sure-fixed"
360 tddata['assured-bandwidth'] = "not-sure-assured"
361 tddata['maximum-bandwidth'] = "not-sure-max"
362 tddata['additional-bw-eligibility-indicator'] = "not-sure-additional"
363
364 td = OnuTrafficDescriptor.create(tddata)
365 tcont = OnuTCont.create(self, tcont=tcontdict, td=td)
366
367 self._pon.add_tcont(tcont)
368
369 self.log.debug('pon-add-tcont', tcont=tcont)
370
371 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
372 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
373 self.log.debug('create-gemport',
374 gem_ports=gem_ports, direction=direction)
375
376 for gem_port in gem_ports:
377 gemdict = dict()
378 gemdict['gemport_id'] = gem_port['gemport_id']
379 gemdict['direction'] = direction
380 gemdict['alloc_id_ref'] = alloc_id_ref
381 gemdict['encryption'] = gem_port['aes_encryption']
382 gemdict['discard_config'] = dict()
383 gemdict['discard_config']['max_probability'] = \
384 gem_port['discard_config']['max_probability']
385 gemdict['discard_config']['max_threshold'] = \
386 gem_port['discard_config']['max_threshold']
387 gemdict['discard_config']['min_threshold'] = \
388 gem_port['discard_config']['min_threshold']
389 gemdict['discard_policy'] = gem_port['discard_policy']
390 gemdict['max_q_size'] = gem_port['max_q_size']
391 gemdict['pbit_map'] = gem_port['pbit_map']
392 gemdict['priority_q'] = gem_port['priority_q']
393 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
394 gemdict['weight'] = gem_port['weight']
395 gemdict['uni_id'] = uni_id
396
397 gem_port = OnuGemPort.create(self, gem_port=gemdict)
398
399 self._pon.add_gem_port(gem_port)
400
401 self.log.debug('pon-add-gemport', gem_port=gem_port)
402
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400403 def _execute_queued_vlan_filter_tasks(self, uni_id):
404 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
405 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
406 # or initial-mib-download is not done. Once the tp_task is completed, we execute
407 # such queued vlan-filter tasks
408 try:
409 if uni_id in self._queued_vlan_filter_task:
410 self.log.info("executing-queued-vlan-filter-task",
411 uni_id=uni_id)
412 filter_info = self._queued_vlan_filter_task[uni_id]
413 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
414 uni_id, filter_info.get("uni_port"), filter_info.get("set_vlan_vid"))
415 # Now remove the entry from the dictionary
416 self._queued_vlan_filter_task[uni_id].clear()
417 self.log.debug("executed-queued-vlan-filter-task",
418 uni_id=uni_id)
419 except Exception as e:
420 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
421
422
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500423 def _do_tech_profile_configuration(self, uni_id, tp):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500424 us_scheduler = tp['us_scheduler']
425 alloc_id = us_scheduler['alloc_id']
426 self._create_tconts(uni_id, us_scheduler)
427 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
428 self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
429 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
430 self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
431
432 def load_and_configure_tech_profile(self, uni_id, tp_path):
433 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
434
435 if uni_id not in self._tp_service_specific_task:
436 self._tp_service_specific_task[uni_id] = dict()
437
438 if uni_id not in self._tech_profile_download_done:
439 self._tech_profile_download_done[uni_id] = dict()
440
441 if tp_path not in self._tech_profile_download_done[uni_id]:
442 self._tech_profile_download_done[uni_id][tp_path] = False
443
444 if not self._tech_profile_download_done[uni_id][tp_path]:
445 try:
446 if tp_path in self._tp_service_specific_task[uni_id]:
447 self.log.info("tech-profile-config-already-in-progress",
448 tp_path=tp_path)
449 return
450
451 tp = self.kv_client[tp_path]
452 tp = ast.literal_eval(tp)
453 self.log.debug("tp-instance", tp=tp)
454 self._do_tech_profile_configuration(uni_id, tp)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700455
William Kurkian3a206332019-04-29 11:05:47 -0400456 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500457 def success(_results):
458 self.log.info("tech-profile-config-done-successfully")
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700459 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-success')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500460 if tp_path in self._tp_service_specific_task[uni_id]:
461 del self._tp_service_specific_task[uni_id][tp_path]
462 self._tech_profile_download_done[uni_id][tp_path] = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400463 # Now execute any vlan filter tasks that were queued for later
464 self._execute_queued_vlan_filter_tasks(uni_id)
William Kurkian3a206332019-04-29 11:05:47 -0400465 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500466 def failure(_reason):
467 self.log.warn('tech-profile-config-failure-retrying',
468 _reason=_reason)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700469 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500470 if tp_path in self._tp_service_specific_task[uni_id]:
471 del self._tp_service_specific_task[uni_id][tp_path]
472 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self.load_and_configure_tech_profile,
473 uni_id, tp_path)
474
475 self.log.info('downloading-tech-profile-configuration')
476 self._tp_service_specific_task[uni_id][tp_path] = \
477 BrcmTpServiceSpecificTask(self.omci_agent, self, uni_id)
478 self._deferred = \
479 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
480 self._deferred.addCallbacks(success, failure)
481
482 except Exception as e:
483 self.log.exception("error-loading-tech-profile", e=e)
484 else:
485 self.log.info("tech-profile-config-already-done")
486
487 def update_pm_config(self, device, pm_config):
488 # TODO: This has not been tested
489 self.log.info('update_pm_config', pm_config=pm_config)
490 self.pm_metrics.update(pm_config)
491
492 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
493 # flow decomposition that ultimately comes from onos
494 def update_flow_table(self, device, flows):
495 self.log.debug('function-entry', device=device, flows=flows)
496
497 #
498 # We need to proxy through the OLT to get to the ONU
499 # Configuration from here should be using OMCI
500 #
501 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
502
503 # no point in pushing omci flows if the device isnt reachable
504 if device.connect_status != ConnectStatus.REACHABLE or \
505 device.admin_state != AdminState.ENABLED:
506 self.log.warn("device-disabled-or-offline-skipping-flow-update",
507 admin=device.admin_state, connect=device.connect_status)
508 return
509
510 def is_downstream(port):
511 return port == self._pon_port_number
512
513 def is_upstream(port):
514 return not is_downstream(port)
515
516 for flow in flows:
517 _type = None
518 _port = None
519 _vlan_vid = None
520 _udp_dst = None
521 _udp_src = None
522 _ipv4_dst = None
523 _ipv4_src = None
524 _metadata = None
525 _output = None
526 _push_tpid = None
527 _field = None
528 _set_vlan_vid = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400529 _tunnel_id = None
530
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500531 self.log.debug('bulk-flow-update', device_id=device.id, flow=flow)
532 try:
533 _in_port = fd.get_in_port(flow)
534 assert _in_port is not None
535
536 _out_port = fd.get_out_port(flow) # may be None
537
538 if is_downstream(_in_port):
539 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
540 uni_port = self.uni_port(_out_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400541 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500542 elif is_upstream(_in_port):
543 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
544 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400545 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500546 else:
547 raise Exception('port should be 1 or 2 by our convention')
548
549 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
550
551 for field in fd.get_ofb_fields(flow):
552 if field.type == fd.ETH_TYPE:
553 _type = field.eth_type
554 self.log.debug('field-type-eth-type',
555 eth_type=_type)
556
557 elif field.type == fd.IP_PROTO:
558 _proto = field.ip_proto
559 self.log.debug('field-type-ip-proto',
560 ip_proto=_proto)
561
562 elif field.type == fd.IN_PORT:
563 _port = field.port
564 self.log.debug('field-type-in-port',
565 in_port=_port)
566
567 elif field.type == fd.VLAN_VID:
568 _vlan_vid = field.vlan_vid & 0xfff
569 self.log.debug('field-type-vlan-vid',
570 vlan=_vlan_vid)
571
572 elif field.type == fd.VLAN_PCP:
573 _vlan_pcp = field.vlan_pcp
574 self.log.debug('field-type-vlan-pcp',
575 pcp=_vlan_pcp)
576
577 elif field.type == fd.UDP_DST:
578 _udp_dst = field.udp_dst
579 self.log.debug('field-type-udp-dst',
580 udp_dst=_udp_dst)
581
582 elif field.type == fd.UDP_SRC:
583 _udp_src = field.udp_src
584 self.log.debug('field-type-udp-src',
585 udp_src=_udp_src)
586
587 elif field.type == fd.IPV4_DST:
588 _ipv4_dst = field.ipv4_dst
589 self.log.debug('field-type-ipv4-dst',
590 ipv4_dst=_ipv4_dst)
591
592 elif field.type == fd.IPV4_SRC:
593 _ipv4_src = field.ipv4_src
594 self.log.debug('field-type-ipv4-src',
595 ipv4_dst=_ipv4_src)
596
597 elif field.type == fd.METADATA:
598 _metadata = field.table_metadata
599 self.log.debug('field-type-metadata',
600 metadata=_metadata)
601
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400602 elif field.type == fd.TUNNEL_ID:
603 _tunnel_id = field.tunnel_id
604 self.log.debug('field-type-tunnel-id',
605 tunnel_id=_tunnel_id)
606
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500607 else:
608 raise NotImplementedError('field.type={}'.format(
609 field.type))
610
611 for action in fd.get_actions(flow):
612
613 if action.type == fd.OUTPUT:
614 _output = action.output.port
615 self.log.debug('action-type-output',
616 output=_output, in_port=_in_port)
617
618 elif action.type == fd.POP_VLAN:
619 self.log.debug('action-type-pop-vlan',
620 in_port=_in_port)
621
622 elif action.type == fd.PUSH_VLAN:
623 _push_tpid = action.push.ethertype
624 self.log.debug('action-type-push-vlan',
625 push_tpid=_push_tpid, in_port=_in_port)
626 if action.push.ethertype != 0x8100:
627 self.log.error('unhandled-tpid',
628 ethertype=action.push.ethertype)
629
630 elif action.type == fd.SET_FIELD:
631 _field = action.set_field.field.ofb_field
632 assert (action.set_field.field.oxm_class ==
633 OFPXMC_OPENFLOW_BASIC)
634 self.log.debug('action-type-set-field',
635 field=_field, in_port=_in_port)
636 if _field.type == fd.VLAN_VID:
637 _set_vlan_vid = _field.vlan_vid & 0xfff
638 self.log.debug('set-field-type-vlan-vid',
639 vlan_vid=_set_vlan_vid)
640 else:
641 self.log.error('unsupported-action-set-field-type',
642 field_type=_field.type)
643 else:
644 self.log.error('unsupported-action-type',
645 action_type=action.type, in_port=_in_port)
646
Matt Jeanneret810148b2019-09-29 12:44:01 -0400647 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
648 # supported match and action criteria cannot be handled by omci and must be ignored.
649 if _set_vlan_vid is None or _set_vlan_vid == 0:
650 self.log.warn('ignoring-flow-that-does-not-set-vlanid')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500651 else:
Matt Jeanneret810148b2019-09-29 12:44:01 -0400652 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, set_vlan_vid=_set_vlan_vid)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400653 self._add_vlan_filter_task(device, uni_id, uni_port, _set_vlan_vid)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500654 except Exception as e:
655 self.log.exception('failed-to-install-flow', e=e, flow=flow)
656
657
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400658
659 def _add_vlan_filter_task(self, device,uni_id, uni_port, _set_vlan_vid):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500660 assert uni_port is not None
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400661 if uni_id in self._tech_profile_download_done and self._tech_profile_download_done[uni_id] != {}:
662 @inlineCallbacks
663 def success(_results):
664 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700665 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-pushed')
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400666 self._vlan_filter_task = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500667
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400668 @inlineCallbacks
669 def failure(_reason):
670 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700671 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-failed-retrying')
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400672 self._vlan_filter_task = reactor.callLater(_STARTUP_RETRY_WAIT,
673 self._add_vlan_filter_task, device,uni_port.port_number, uni_port, _set_vlan_vid)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500674
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400675 self.log.info('setting-vlan-tag')
Matt Jeanneret810148b2019-09-29 12:44:01 -0400676 self._vlan_filter_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400677 self._deferred = self._onu_omci_device.task_runner.queue_task(self._vlan_filter_task)
678 self._deferred.addCallbacks(success, failure)
679 else:
680 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
681 uni_id=uni_id)
Matt Jeanneret810148b2019-09-29 12:44:01 -0400682 self._queued_vlan_filter_task[uni_id] = {"device": device,
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400683 "uni_id":uni_id,
684 "uni_port": uni_port,
685 "set_vlan_vid": _set_vlan_vid}
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500686
687 def get_tx_id(self):
688 self.log.debug('function-entry')
689 self.tx_id += 1
690 return self.tx_id
691
Matt Jeannereta32441c2019-03-07 05:16:37 -0500692 def process_inter_adapter_message(self, request):
693 self.log.debug('process-inter-adapter-message', msg=request)
694 try:
695 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
696 omci_msg = InterAdapterOmciMessage()
697 request.body.Unpack(omci_msg)
698 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500699
Matt Jeannereta32441c2019-03-07 05:16:37 -0500700 self.receive_message(omci_msg.message)
701
702 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
703 onu_indication = OnuIndication()
704 request.body.Unpack(onu_indication)
705 self.log.debug('inter-adapter-recv-onu-ind', onu_indication=onu_indication)
706
707 if onu_indication.oper_state == "up":
708 self.create_interface(onu_indication)
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -0400709 elif onu_indication.oper_state == "down" or onu_indication.oper_state=="unreachable":
Matt Jeannereta32441c2019-03-07 05:16:37 -0500710 self.update_interface(onu_indication)
711 else:
712 self.log.error("unknown-onu-indication", onu_indication=onu_indication)
713
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400714 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
715 tech_msg = InterAdapterTechProfileDownloadMessage()
716 request.body.Unpack(tech_msg)
717 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
718
719 self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
720
Matt Jeannereta32441c2019-03-07 05:16:37 -0500721 else:
722 self.log.error("inter-adapter-unhandled-type", request=request)
723
724 except Exception as e:
725 self.log.exception("error-processing-inter-adapter-message", e=e)
726
727 # Called each time there is an onu "up" indication from the olt handler
728 @inlineCallbacks
729 def create_interface(self, onu_indication):
730 self.log.debug('function-entry', onu_indication=onu_indication)
731 self._onu_indication = onu_indication
732
Matt Jeanneretc083f462019-03-11 15:02:01 -0400733 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
734 connect_status=ConnectStatus.REACHABLE)
735
Matt Jeannereta32441c2019-03-07 05:16:37 -0500736 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500737
738 self.log.debug('starting-openomci-statemachine')
739 self._subscribe_to_events()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500740 onu_device.reason = "starting-openomci"
Mahir Gunyelfe6ac432019-09-04 10:17:14 -0700741 reactor.callLater(1, self._onu_omci_device.start,onu_device)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700742 yield self.core_proxy.device_reason_update(self.device_id, onu_device.reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500743 self._heartbeat.enabled = True
744
745 # Currently called each time there is an onu "down" indication from the olt handler
746 # TODO: possibly other reasons to "update" from the olt?
Matt Jeannereta32441c2019-03-07 05:16:37 -0500747 @inlineCallbacks
748 def update_interface(self, onu_indication):
749 self.log.debug('function-entry', onu_indication=onu_indication)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500750
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -0400751 if onu_indication.oper_state == 'down' or onu_indication.oper_state == "unreachable":
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500752 self.log.debug('stopping-openomci-statemachine')
753 reactor.callLater(0, self._onu_omci_device.stop)
754
755 # Let TP download happen again
756 for uni_id in self._tp_service_specific_task:
757 self._tp_service_specific_task[uni_id].clear()
758 for uni_id in self._tech_profile_download_done:
759 self._tech_profile_download_done[uni_id].clear()
760
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700761 self.disable_ports()
762 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
763 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.DISCOVERED,
764 connect_status=ConnectStatus.UNREACHABLE)
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
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500773 self.log.debug('stopping-openomci-statemachine')
774 reactor.callLater(0, self._onu_omci_device.stop)
775
776 # Let TP download happen again
777 for uni_id in self._tp_service_specific_task:
778 self._tp_service_specific_task[uni_id].clear()
779 for uni_id in self._tech_profile_download_done:
780 self._tech_profile_download_done[uni_id].clear()
781
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700782 self.disable_ports()
783 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500784
785 # TODO: im sure there is more to do here
786
787 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400788 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500789 def remove_gemport(self, data):
790 self.log.debug('remove-gemport', data=data)
William Kurkian3a206332019-04-29 11:05:47 -0400791 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500792 if device.connect_status != ConnectStatus.REACHABLE:
793 self.log.error('device-unreachable')
794 return
795
796 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400797 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500798 def remove_tcont(self, tcont_data, traffic_descriptor_data):
799 self.log.debug('remove-tcont', tcont_data=tcont_data, traffic_descriptor_data=traffic_descriptor_data)
William Kurkian3a206332019-04-29 11:05:47 -0400800 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500801 if device.connect_status != ConnectStatus.REACHABLE:
802 self.log.error('device-unreachable')
803 return
804
805 # TODO: Create some omci task that encompases this what intended
806
807 # Not currently called. Would be called presumably from the olt handler
808 def create_multicast_gemport(self, data):
809 self.log.debug('function-entry', data=data)
810
811 # TODO: create objects and populate for later omci calls
812
813 def disable(self, device):
814 self.log.debug('function-entry', device=device)
815 try:
816 self.log.info('sending-uni-lock-towards-device', device=device)
817
Matt Jeanneret80766692019-05-03 09:58:38 -0400818 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500819 def stop_anyway(reason):
820 # proceed with disable regardless if we could reach the onu. for example onu is unplugged
821 self.log.debug('stopping-openomci-statemachine')
822 reactor.callLater(0, self._onu_omci_device.stop)
823
824 # Let TP download happen again
825 for uni_id in self._tp_service_specific_task:
826 self._tp_service_specific_task[uni_id].clear()
827 for uni_id in self._tech_profile_download_done:
828 self._tech_profile_download_done[uni_id].clear()
829
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700830 self.disable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500831 device.oper_status = OperStatus.UNKNOWN
832 device.reason = "omci-admin-lock"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400833 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500834
835 # lock all the unis
836 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=True)
837 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
838 self._deferred.addCallbacks(stop_anyway, stop_anyway)
839 except Exception as e:
840 log.exception('exception-in-onu-disable', exception=e)
841
William Kurkian3a206332019-04-29 11:05:47 -0400842 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500843 def reenable(self, device):
844 self.log.debug('function-entry', device=device)
845 try:
846 # Start up OpenOMCI state machines for this device
847 # this will ultimately resync mib and unlock unis on successful redownloading the mib
848 self.log.debug('restarting-openomci-statemachine')
849 self._subscribe_to_events()
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700850 yield self.core_proxy.device_reason_update(self.device_id, "restarting-openomci")
serkant.uluderya2cb65f72019-09-30 14:01:51 -0700851 reactor.callLater(1, self._onu_omci_device.start, device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500852 self._heartbeat.enabled = True
853 except Exception as e:
854 log.exception('exception-in-onu-reenable', exception=e)
855
William Kurkian3a206332019-04-29 11:05:47 -0400856 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500857 def reboot(self):
858 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -0400859 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500860 if device.connect_status != ConnectStatus.REACHABLE:
861 self.log.error("device-unreachable")
862 return
863
William Kurkian3a206332019-04-29 11:05:47 -0400864 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500865 def success(_results):
866 self.log.info('reboot-success', _results=_results)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700867 self.disable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500868 device.connect_status = ConnectStatus.UNREACHABLE
869 device.oper_status = OperStatus.DISCOVERED
870 device.reason = "rebooting"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400871 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500872
873 def failure(_reason):
874 self.log.info('reboot-failure', _reason=_reason)
875
876 self._deferred = self._onu_omci_device.reboot()
877 self._deferred.addCallbacks(success, failure)
878
William Kurkian3a206332019-04-29 11:05:47 -0400879 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700880 def disable_ports(self):
881 self.log.info('disable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500882
883 # Disable all ports on that device
Matt Jeanneret80766692019-05-03 09:58:38 -0400884 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500885
William Kurkian3a206332019-04-29 11:05:47 -0400886 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700887 def enable_ports(self):
888 self.log.info('enable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500889
Matt Jeanneret80766692019-05-03 09:58:38 -0400890 # Enable all ports on that device
891 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.ACTIVE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500892
893 # Called just before openomci state machine is started. These listen for events from selected state machines,
894 # most importantly, mib in sync. Which ultimately leads to downloading the mib
895 def _subscribe_to_events(self):
896 self.log.debug('function-entry')
897
898 # OMCI MIB Database sync status
899 bus = self._onu_omci_device.event_bus
900 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
901 OnuDeviceEvents.MibDatabaseSyncEvent)
902 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
903
904 # OMCI Capabilities
905 bus = self._onu_omci_device.event_bus
906 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
907 OnuDeviceEvents.OmciCapabilitiesEvent)
908 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
909
910 # Called when the mib is in sync
911 def in_sync_handler(self, _topic, msg):
912 self.log.debug('function-entry', _topic=_topic, msg=msg)
913 if self._in_sync_subscription is not None:
914 try:
915 in_sync = msg[IN_SYNC_KEY]
916
917 if in_sync:
918 # Only call this once
919 bus = self._onu_omci_device.event_bus
920 bus.unsubscribe(self._in_sync_subscription)
921 self._in_sync_subscription = None
922
923 # Start up device_info load
924 self.log.debug('running-mib-sync')
925 reactor.callLater(0, self._mib_in_sync)
926
927 except Exception as e:
928 self.log.exception('in-sync', e=e)
929
930 def capabilties_handler(self, _topic, _msg):
931 self.log.debug('function-entry', _topic=_topic, msg=_msg)
932 if self._capabilities_subscription is not None:
933 self.log.debug('capabilities-handler-done')
934
935 # Mib is in sync, we can now query what we learned and actually start pushing ME (download) to the ONU.
936 # Currently uses a basic mib download task that create a bridge with a single gem port and uni, only allowing EAP
937 # Implement your own MibDownloadTask if you wish to setup something different by default
Matt Jeanneretc083f462019-03-11 15:02:01 -0400938 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500939 def _mib_in_sync(self):
940 self.log.debug('function-entry')
941
942 omci = self._onu_omci_device
943 in_sync = omci.mib_db_in_sync
944
Matt Jeanneretc083f462019-03-11 15:02:01 -0400945 device = yield self.core_proxy.get_device(self.device_id)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700946 yield self.core_proxy.device_reason_update(self.device_id, 'discovery-mibsync-complete')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500947
948 if not self._dev_info_loaded:
949 self.log.info('loading-device-data-from-mib', in_sync=in_sync, already_loaded=self._dev_info_loaded)
950
951 omci_dev = self._onu_omci_device
952 config = omci_dev.configuration
953
954 # TODO: run this sooner somehow. shouldnt have to wait for mib sync to push an initial download
955 # In Sync, we can register logical ports now. Ideally this could occur on
956 # the first time we received a successful (no timeout) OMCI Rx response.
957 try:
958
959 # sort the lists so we get consistent port ordering.
960 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
961 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
962 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
963 veip_list = sorted(config.veip_entities) if config.veip_entities else []
964
965 if ani_list is None or (pptp_list is None and veip_list is None):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500966 self.log.warn("no-ani-or-unis")
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700967 yield self.core_proxy.device_reason_update(self.device_id, 'onu-missing-required-elements')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500968 raise Exception("onu-missing-required-elements")
969
970 # Currently logging the ani, pptp, veip, and uni for information purposes.
971 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
972 # And in some ONU the UNI-G list is incomplete or incorrect...
973 for entity_id in ani_list:
974 ani_value = config.ani_g_entities[entity_id]
975 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
976 # TODO: currently only one OLT PON port/ANI, so this works out. With NGPON there will be 2..?
977 self._total_tcont_count = ani_value.get('total-tcont-count')
978 self.log.debug("set-total-tcont-count", tcont_count=self._total_tcont_count)
979
980 for entity_id in uni_list:
981 uni_value = config.uni_g_entities[entity_id]
982 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
983
984 uni_entities = OrderedDict()
985 for entity_id in pptp_list:
986 pptp_value = config.pptp_entities[entity_id]
987 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
988 uni_entities[entity_id] = UniType.PPTP
989
990 for entity_id in veip_list:
991 veip_value = config.veip_entities[entity_id]
992 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
993 uni_entities[entity_id] = UniType.VEIP
994
995 uni_id = 0
996 for entity_id, uni_type in uni_entities.iteritems():
997 try:
Matt Jeanneretc083f462019-03-11 15:02:01 -0400998 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500999 uni_id += 1
1000 except AssertionError as e:
1001 self.log.warn("could not add UNI", entity_id=entity_id, uni_type=uni_type, e=e)
1002
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001003 self._qos_flexibility = config.qos_configuration_flexibility or 0
1004 self._omcc_version = config.omcc_version or OMCCVersion.Unknown
1005
1006 if self._unis:
1007 self._dev_info_loaded = True
1008 else:
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001009 yield self.core_proxy.device_reason_update(self.device_id, 'no-usable-unis')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001010 self.log.warn("no-usable-unis")
1011 raise Exception("no-usable-unis")
1012
1013 except Exception as e:
1014 self.log.exception('device-info-load', e=e)
1015 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1016
1017 else:
1018 self.log.info('device-info-already-loaded', in_sync=in_sync, already_loaded=self._dev_info_loaded)
1019
1020 if self._dev_info_loaded:
Matt Jeanneretad9a0f12019-05-09 14:05:49 -04001021 if device.admin_state == AdminState.PREPROVISIONED or device.admin_state == AdminState.ENABLED:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001022
1023 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001024 def success(_results):
1025 self.log.info('mib-download-success', _results=_results)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001026 yield self.enable_ports()
Matt Jeanneretc083f462019-03-11 15:02:01 -04001027 yield self.core_proxy.device_state_update(device.id,
1028 oper_status=OperStatus.ACTIVE, connect_status=ConnectStatus.REACHABLE)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001029 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-downloaded')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001030 self._mib_download_task = None
Devmalya Paulffc89df2019-07-31 17:43:13 -04001031 yield self.onu_active_event()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001032
Matt Jeanneretc083f462019-03-11 15:02:01 -04001033 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001034 def failure(_reason):
1035 self.log.warn('mib-download-failure-retrying', _reason=_reason)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001036 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001037 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1038
1039 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1040 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1041 self.log.info('downloading-initial-mib-configuration')
1042 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1043 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1044 self._deferred.addCallbacks(success, failure)
1045 else:
1046 self.log.info('admin-down-disabling')
1047 self.disable(device)
1048 else:
1049 self.log.info('device-info-not-loaded-skipping-mib-download')
1050
Matt Jeanneretc083f462019-03-11 15:02:01 -04001051 @inlineCallbacks
1052 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001053 self.log.debug('function-entry')
1054
Matt Jeanneretc083f462019-03-11 15:02:01 -04001055 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 -05001056
1057 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1058 uni_name = "uni-{}".format(uni_no)
1059
1060 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
1061
1062 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 -04001063 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001064
1065 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1066 uni_port.entity_id = entity_id
1067 uni_port.enabled = True
1068 uni_port.mac_bridge_port_num = mac_bridge_port_num
1069
1070 self.log.debug("created-uni-port", uni=uni_port)
1071
Matt Jeanneretc083f462019-03-11 15:02:01 -04001072 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001073
1074 self._unis[uni_port.port_number] = uni_port
1075
1076 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=self._onu_indication.onu_id,
Yongjie Zhang286099c2019-08-06 13:39:07 -04001077 uni_ports=self._unis.values(), serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001078
Matt Jeanneretc083f462019-03-11 15:02:01 -04001079 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1080 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
1081 MAX_PONS_PER_OLT = 16
Mahir Gunyel0e1588a2019-06-27 06:12:47 -07001082 MAX_ONUS_PER_PON = 128
Matt Jeanneretc083f462019-03-11 15:02:01 -04001083 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001084
Matt Jeanneretc083f462019-03-11 15:02:01 -04001085 assert intf_id < MAX_PONS_PER_OLT
1086 assert onu_id < MAX_ONUS_PER_PON
1087 assert uni_id < MAX_UNIS_PER_ONU
Matt Jeanneret3b7db442019-04-22 16:29:48 -04001088 return intf_id << 11 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001089
1090 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001091 def onu_active_event(self):
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001092 self.log.debug('function-entry')
1093 try:
1094 device = yield self.core_proxy.get_device(self.device_id)
1095 parent_device = yield self.core_proxy.get_device(self.parent_id)
1096 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001097 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001098
1099 self.log.debug("onu-indication-context-data",
1100 pon_id=self._onu_indication.intf_id,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001101 onu_id=self._onu_indication.onu_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001102 registration_id=self.device_id,
1103 device_id=self.device_id,
1104 onu_serial_number=device.serial_number,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001105 olt_serial_number=olt_serial_number,
1106 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001107
Devmalya Paulffc89df2019-07-31 17:43:13 -04001108 self.log.debug("Trying-to-raise-onu-active-event")
1109 OnuActiveEvent(self.events, self.device_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001110 self._onu_indication.intf_id,
1111 device.serial_number,
1112 str(self.device_id),
Devmalya Paulffc89df2019-07-31 17:43:13 -04001113 olt_serial_number,raised_ts,
1114 onu_id=self._onu_indication.onu_id).send(True)
1115 except Exception as active_event_error:
1116 self.log.exception('onu-activated-event-error',
1117 errmsg=active_event_error.message)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001118