blob: 0cb381f1f7c5dc8cc17e0665002f8c98db840a1d [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 Jeanneret2e3cb8d2019-11-16 09:22:41 -050021from __future__ import absolute_import
22import six
Devmalya Paulffc89df2019-07-31 17:43:13 -040023import arrow
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050024import structlog
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050025import json
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050026
27from collections import OrderedDict
28
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050029from twisted.internet import reactor
30from twisted.internet.defer import DeferredQueue, inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050031
32from heartbeat import HeartBeat
Devmalya Paulffc89df2019-07-31 17:43:13 -040033from pyvoltha.adapters.extensions.events.device_events.onu.onu_active_event import OnuActiveEvent
34from pyvoltha.adapters.extensions.events.kpi.onu.onu_pm_metrics import OnuPmMetrics
35from pyvoltha.adapters.extensions.events.kpi.onu.onu_omci_pm import OnuOmciPmMetrics
36from pyvoltha.adapters.extensions.events.adapter_events import AdapterEvents
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050037
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050038import pyvoltha.common.openflow.utils as fd
39from pyvoltha.common.utils.registry import registry
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050040from pyvoltha.common.utils.nethelpers import mac_str_to_tuple
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050041from pyvoltha.common.config.config_backend import ConsulStore
42from pyvoltha.common.config.config_backend import EtcdStore
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050043from voltha_protos.logical_device_pb2 import LogicalPort
William Kurkian8235c1e2019-03-05 12:58:28 -050044from voltha_protos.common_pb2 import OperStatus, ConnectStatus, AdminState
Matt Jeanneretc083f462019-03-11 15:02:01 -040045from 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 -040046from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
47 InterAdapterOmciMessage, PortCapability, InterAdapterTechProfileDownloadMessage
Matt Jeannereta32441c2019-03-07 05:16:37 -050048from voltha_protos.openolt_pb2 import OnuIndication
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050049from pyvoltha.adapters.extensions.omci.onu_configuration import OMCCVersion
50from pyvoltha.adapters.extensions.omci.onu_device_entry import OnuDeviceEvents, \
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050051 OnuDeviceEntry, IN_SYNC_KEY
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050052from omci.brcm_mib_download_task import BrcmMibDownloadTask
53from omci.brcm_tp_service_specific_task import BrcmTpServiceSpecificTask
54from omci.brcm_uni_lock_task import BrcmUniLockTask
55from omci.brcm_vlan_filter_task import BrcmVlanFilterTask
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050056from onu_gem_port import OnuGemPort
57from onu_tcont import OnuTCont
58from pon_port import PonPort
59from uni_port import UniPort, UniType
60from onu_traffic_descriptor import OnuTrafficDescriptor
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050061from pyvoltha.common.tech_profile.tech_profile import TechProfile
onkarkundargiaae99712019-09-23 15:02:52 +053062from pyvoltha.adapters.extensions.omci.tasks.omci_test_request import OmciTestRequest
63from pyvoltha.adapters.extensions.omci.omci_entities import AniG
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050064from pyvoltha.adapters.extensions.omci.omci_defs import EntityOperations, ReasonCodes
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050065
66OP = EntityOperations
67RC = ReasonCodes
68
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050069log = structlog.get_logger()
70
71_STARTUP_RETRY_WAIT = 20
72
73
74class BrcmOpenomciOnuHandler(object):
75
76 def __init__(self, adapter, device_id):
77 self.log = structlog.get_logger(device_id=device_id)
78 self.log.debug('function-entry')
79 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050080 self.core_proxy = adapter.core_proxy
81 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050082 self.parent_adapter = None
83 self.parent_id = None
84 self.device_id = device_id
85 self.incoming_messages = DeferredQueue()
86 self.event_messages = DeferredQueue()
87 self.proxy_address = None
88 self.tx_id = 0
89 self._enabled = False
Devmalya Paulffc89df2019-07-31 17:43:13 -040090 self.events = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050091 self.pm_metrics = None
92 self._omcc_version = OMCCVersion.Unknown
93 self._total_tcont_count = 0 # From ANI-G ME
94 self._qos_flexibility = 0 # From ONT2_G ME
95
96 self._onu_indication = None
97 self._unis = dict() # Port # -> UniPort
98
99 self._pon = None
100 # TODO: probably shouldnt be hardcoded, determine from olt maybe?
101 self._pon_port_number = 100
102 self.logical_device_id = None
103
104 self._heartbeat = HeartBeat.create(self, device_id)
105
106 # Set up OpenOMCI environment
107 self._onu_omci_device = None
108 self._dev_info_loaded = False
109 self._deferred = None
110
111 self._in_sync_subscription = None
112 self._connectivity_subscription = None
113 self._capabilities_subscription = None
114
115 self.mac_bridge_service_profile_entity_id = 0x201
116 self.gal_enet_profile_entity_id = 0x1
117
118 self._tp_service_specific_task = dict()
119 self._tech_profile_download_done = dict()
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400120 # Stores information related to queued vlan filter tasks
121 # Dictionary with key being uni_id and value being device,uni port ,uni id and vlan id
122
123 self._queued_vlan_filter_task = dict()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500124
125 # Initialize KV store client
126 self.args = registry('main').get_args()
127 if self.args.backend == 'etcd':
128 host, port = self.args.etcd.split(':', 1)
129 self.kv_client = EtcdStore(host, port,
130 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
131 elif self.args.backend == 'consul':
132 host, port = self.args.consul.split(':', 1)
133 self.kv_client = ConsulStore(host, port,
134 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
135 else:
136 self.log.error('Invalid-backend')
137 raise Exception("Invalid-backend-for-kv-store")
138
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500139 @property
140 def enabled(self):
141 return self._enabled
142
143 @enabled.setter
144 def enabled(self, value):
145 if self._enabled != value:
146 self._enabled = value
147
148 @property
149 def omci_agent(self):
150 return self.adapter.omci_agent
151
152 @property
153 def omci_cc(self):
154 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
155
156 @property
157 def heartbeat(self):
158 return self._heartbeat
159
160 @property
161 def uni_ports(self):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500162 return list(self._unis.values())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500163
164 def uni_port(self, port_no_or_name):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500165 if isinstance(port_no_or_name, six.string_types):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500166 return next((uni for uni in self.uni_ports
167 if uni.name == port_no_or_name), None)
168
169 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
170 return next((uni for uni in self.uni_ports
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400171 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500172
173 @property
174 def pon_port(self):
175 return self._pon
176
177 def receive_message(self, msg):
178 if self.omci_cc is not None:
179 self.omci_cc.receive_message(msg)
180
Matt Jeanneretc083f462019-03-11 15:02:01 -0400181 def get_ofp_port_info(self, device, port_no):
182 self.log.info('get_ofp_port_info', port_no=port_no, device_id=device.id)
183 cap = OFPPF_1GB_FD | OFPPF_FIBER
184
185 hw_addr=mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
186 ((device.parent_port_no >> 8 & 0xff),
187 device.parent_port_no & 0xff,
188 (port_no >> 16) & 0xff,
189 (port_no >> 8) & 0xff,
190 port_no & 0xff))
191
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400192 uni_port = self.uni_port(int(port_no))
193 name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
194 self.log.debug('ofp_port_name', port_no=port_no, name=name)
195
Matt Jeanneretc083f462019-03-11 15:02:01 -0400196 return PortCapability(
197 port=LogicalPort(
198 ofp_port=ofp_port(
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400199 name=name,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400200 hw_addr=hw_addr,
201 config=0,
202 state=OFPPS_LIVE,
203 curr=cap,
204 advertised=cap,
205 peer=cap,
206 curr_speed=OFPPF_1GB_FD,
207 max_speed=OFPPF_1GB_FD
208 ),
209 device_id=device.id,
210 device_port_no=port_no
211 )
212 )
213
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500214 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500215 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500216 def activate(self, device):
217 self.log.debug('function-entry', device=device)
218
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500219 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500220 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500221 assert device.proxy_address.device_id
222
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500223 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500224 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500225 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500226 if self.enabled is not True:
227 self.log.info('activating-new-onu')
228 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500229 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500230 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500231 device.reason = 'activating-onu'
232
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500233 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400234 # pm_metrics requires a logical device id. For now set to just device_id
235 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500236
Matt Jeannereta32441c2019-03-07 05:16:37 -0500237 yield self.core_proxy.device_update(device)
Mahir Gunyel0e1588a2019-06-27 06:12:47 -0700238 self.log.debug('device updated', device=device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500239
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700240 yield self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500241
Mahir Gunyel0e1588a2019-06-27 06:12:47 -0700242 self.log.debug('pon state initialized', device=device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500243 ############################################################################
Devmalya Paulffc89df2019-07-31 17:43:13 -0400244 # Setup Alarm handler
245 self.events = AdapterEvents(self.core_proxy, device.id, self.logical_device_id,
246 device.serial_number)
247 ############################################################################
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500248 # Setup PM configuration for this device
249 # Pass in ONU specific options
250 kwargs = {
251 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
252 'heartbeat': self.heartbeat,
253 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
254 }
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400255 self.log.debug('create-OnuPmMetrics', serial_number=device.serial_number)
Devmalya Paulffc89df2019-07-31 17:43:13 -0400256 self.pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400257 self.logical_device_id, device.serial_number,
258 grouped=True, freq_override=False, **kwargs)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500259 pm_config = self.pm_metrics.make_proto()
260 self._onu_omci_device.set_pm_config(self.pm_metrics.omci_pm.openomci_interval_pm)
261 self.log.info("initial-pm-config", pm_config=pm_config)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500262 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500263
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500264 # Note, ONU ID and UNI intf set in add_uni_port method
Devmalya Paulffc89df2019-07-31 17:43:13 -0400265 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500266 ani_ports=[self._pon])
aishwaryarana01a98d9fe2019-05-08 12:09:06 -0500267
268 #Start collecting stats from the device after a brief pause
269 reactor.callLater(10, self.pm_metrics.start_collector)
270
onkarkundargiaae99712019-09-23 15:02:52 +0530271 # Code to Run OMCI Test Action
272 kwargs_omci_test_action = {
273 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
274 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
275 }
276 serial_number = device.serial_number
277 test_request = OmciTestRequest(self.core_proxy,
278 self.omci_agent, self.device_id,
279 AniG, serial_number,
280 self.logical_device_id,
281 exclusive=False,
282 **kwargs_omci_test_action)
283 reactor.callLater(60, test_request.start_collector)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500284 self.enabled = True
285 else:
286 self.log.info('onu-already-activated')
287
288 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400289 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500290 def reconcile(self, device):
291 self.log.debug('function-entry', device=device)
292
293 # first we verify that we got parent reference and proxy info
294 assert device.parent_id
295 assert device.proxy_address.device_id
296
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700297 self.proxy_address = device.proxy_address
298 self.parent_id = device.parent_id
299 self._pon_port_number = device.parent_port_no
300
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500301 if self.enabled is not True:
302 self.log.info('reconciling-broadcom-onu-device')
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700303 self.logical_device_id = self.device_id
304 self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500305
306 # need to restart state machines on vcore restart. there is no indication to do it for us.
307 self._onu_omci_device.start()
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700308 yield self.core_proxy.device_reason_update(self.device_id, "restarting-openomci")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500309
310 # TODO: this is probably a bit heavy handed
311 # Force a reboot for now. We need indications to reflow to reassign tconts and gems given vcore went away
312 # This may not be necessary when mib resync actually works
313 reactor.callLater(1, self.reboot)
314
315 self.enabled = True
316 else:
317 self.log.info('onu-already-activated')
318
319 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700320 def _init_pon_state(self):
321 self.log.debug('function-entry', deviceId=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500322
323 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500324 self._pon.add_peer(self.parent_id, self._pon_port_number)
325 self.log.debug('adding-pon-port-to-agent', pon=self._pon.get_port())
326
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700327 yield self.core_proxy.port_created(self.device_id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500328
Matt Jeanneret0c287892019-02-28 11:48:00 -0500329 self.log.debug('added-pon-port-to-agent', pon=self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500330
331 # Create and start the OpenOMCI ONU Device Entry for this ONU
332 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500333 self.core_proxy,
334 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500335 support_classes=self.adapter.broadcom_omci,
336 custom_me_map=self.adapter.custom_me_entities())
337 # Port startup
338 if self._pon is not None:
339 self._pon.enabled = True
340
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500341 def delete(self, device):
342 self.log.info('delete-onu', device=device)
343 if self.parent_adapter:
344 try:
345 self.parent_adapter.delete_child_device(self.parent_id, device)
346 except AttributeError:
347 self.log.debug('parent-device-delete-child-not-implemented')
348 else:
349 self.log.debug("parent-adapter-not-available")
350
351 def _create_tconts(self, uni_id, us_scheduler):
352 alloc_id = us_scheduler['alloc_id']
353 q_sched_policy = us_scheduler['q_sched_policy']
354 self.log.debug('create-tcont', us_scheduler=us_scheduler)
355
356 tcontdict = dict()
357 tcontdict['alloc-id'] = alloc_id
358 tcontdict['q_sched_policy'] = q_sched_policy
359 tcontdict['uni_id'] = uni_id
360
361 # TODO: Not sure what to do with any of this...
362 tddata = dict()
363 tddata['name'] = 'not-sure-td-profile'
364 tddata['fixed-bandwidth'] = "not-sure-fixed"
365 tddata['assured-bandwidth'] = "not-sure-assured"
366 tddata['maximum-bandwidth'] = "not-sure-max"
367 tddata['additional-bw-eligibility-indicator'] = "not-sure-additional"
368
369 td = OnuTrafficDescriptor.create(tddata)
370 tcont = OnuTCont.create(self, tcont=tcontdict, td=td)
371
372 self._pon.add_tcont(tcont)
373
374 self.log.debug('pon-add-tcont', tcont=tcont)
375
376 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
377 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
378 self.log.debug('create-gemport',
379 gem_ports=gem_ports, direction=direction)
380
381 for gem_port in gem_ports:
382 gemdict = dict()
383 gemdict['gemport_id'] = gem_port['gemport_id']
384 gemdict['direction'] = direction
385 gemdict['alloc_id_ref'] = alloc_id_ref
386 gemdict['encryption'] = gem_port['aes_encryption']
387 gemdict['discard_config'] = dict()
388 gemdict['discard_config']['max_probability'] = \
389 gem_port['discard_config']['max_probability']
390 gemdict['discard_config']['max_threshold'] = \
391 gem_port['discard_config']['max_threshold']
392 gemdict['discard_config']['min_threshold'] = \
393 gem_port['discard_config']['min_threshold']
394 gemdict['discard_policy'] = gem_port['discard_policy']
395 gemdict['max_q_size'] = gem_port['max_q_size']
396 gemdict['pbit_map'] = gem_port['pbit_map']
397 gemdict['priority_q'] = gem_port['priority_q']
398 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
399 gemdict['weight'] = gem_port['weight']
400 gemdict['uni_id'] = uni_id
401
402 gem_port = OnuGemPort.create(self, gem_port=gemdict)
403
404 self._pon.add_gem_port(gem_port)
405
406 self.log.debug('pon-add-gemport', gem_port=gem_port)
407
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400408 def _execute_queued_vlan_filter_tasks(self, uni_id):
409 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
410 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
411 # or initial-mib-download is not done. Once the tp_task is completed, we execute
412 # such queued vlan-filter tasks
413 try:
414 if uni_id in self._queued_vlan_filter_task:
415 self.log.info("executing-queued-vlan-filter-task",
416 uni_id=uni_id)
417 filter_info = self._queued_vlan_filter_task[uni_id]
418 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
419 uni_id, filter_info.get("uni_port"), filter_info.get("set_vlan_vid"))
420 # Now remove the entry from the dictionary
421 self._queued_vlan_filter_task[uni_id].clear()
422 self.log.debug("executed-queued-vlan-filter-task",
423 uni_id=uni_id)
424 except Exception as e:
425 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
426
427
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500428 def _do_tech_profile_configuration(self, uni_id, tp):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500429 us_scheduler = tp['us_scheduler']
430 alloc_id = us_scheduler['alloc_id']
431 self._create_tconts(uni_id, us_scheduler)
432 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
433 self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
434 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
435 self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
436
437 def load_and_configure_tech_profile(self, uni_id, tp_path):
438 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
439
440 if uni_id not in self._tp_service_specific_task:
441 self._tp_service_specific_task[uni_id] = dict()
442
443 if uni_id not in self._tech_profile_download_done:
444 self._tech_profile_download_done[uni_id] = dict()
445
446 if tp_path not in self._tech_profile_download_done[uni_id]:
447 self._tech_profile_download_done[uni_id][tp_path] = False
448
449 if not self._tech_profile_download_done[uni_id][tp_path]:
450 try:
451 if tp_path in self._tp_service_specific_task[uni_id]:
452 self.log.info("tech-profile-config-already-in-progress",
453 tp_path=tp_path)
454 return
455
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500456 tpstored = self.kv_client[tp_path]
457 tpstring = tpstored.decode('ascii')
458 tp = json.loads(tpstring)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500459 self.log.debug("tp-instance", tp=tp)
460 self._do_tech_profile_configuration(uni_id, tp)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700461
William Kurkian3a206332019-04-29 11:05:47 -0400462 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500463 def success(_results):
464 self.log.info("tech-profile-config-done-successfully")
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700465 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-success')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500466 if tp_path in self._tp_service_specific_task[uni_id]:
467 del self._tp_service_specific_task[uni_id][tp_path]
468 self._tech_profile_download_done[uni_id][tp_path] = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400469 # Now execute any vlan filter tasks that were queued for later
470 self._execute_queued_vlan_filter_tasks(uni_id)
William Kurkian3a206332019-04-29 11:05:47 -0400471 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500472 def failure(_reason):
473 self.log.warn('tech-profile-config-failure-retrying',
474 _reason=_reason)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700475 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500476 if tp_path in self._tp_service_specific_task[uni_id]:
477 del self._tp_service_specific_task[uni_id][tp_path]
478 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self.load_and_configure_tech_profile,
479 uni_id, tp_path)
480
481 self.log.info('downloading-tech-profile-configuration')
482 self._tp_service_specific_task[uni_id][tp_path] = \
483 BrcmTpServiceSpecificTask(self.omci_agent, self, uni_id)
484 self._deferred = \
485 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
486 self._deferred.addCallbacks(success, failure)
487
488 except Exception as e:
489 self.log.exception("error-loading-tech-profile", e=e)
490 else:
491 self.log.info("tech-profile-config-already-done")
492
493 def update_pm_config(self, device, pm_config):
494 # TODO: This has not been tested
495 self.log.info('update_pm_config', pm_config=pm_config)
496 self.pm_metrics.update(pm_config)
497
498 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
499 # flow decomposition that ultimately comes from onos
500 def update_flow_table(self, device, flows):
501 self.log.debug('function-entry', device=device, flows=flows)
502
503 #
504 # We need to proxy through the OLT to get to the ONU
505 # Configuration from here should be using OMCI
506 #
507 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
508
509 # no point in pushing omci flows if the device isnt reachable
510 if device.connect_status != ConnectStatus.REACHABLE or \
511 device.admin_state != AdminState.ENABLED:
512 self.log.warn("device-disabled-or-offline-skipping-flow-update",
513 admin=device.admin_state, connect=device.connect_status)
514 return
515
516 def is_downstream(port):
517 return port == self._pon_port_number
518
519 def is_upstream(port):
520 return not is_downstream(port)
521
522 for flow in flows:
523 _type = None
524 _port = None
525 _vlan_vid = None
526 _udp_dst = None
527 _udp_src = None
528 _ipv4_dst = None
529 _ipv4_src = None
530 _metadata = None
531 _output = None
532 _push_tpid = None
533 _field = None
534 _set_vlan_vid = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400535 _tunnel_id = None
536
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500537 self.log.debug('bulk-flow-update', device_id=device.id, flow=flow)
538 try:
539 _in_port = fd.get_in_port(flow)
540 assert _in_port is not None
541
542 _out_port = fd.get_out_port(flow) # may be None
543
544 if is_downstream(_in_port):
545 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
546 uni_port = self.uni_port(_out_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400547 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500548 elif is_upstream(_in_port):
549 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
550 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400551 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500552 else:
553 raise Exception('port should be 1 or 2 by our convention')
554
555 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
556
557 for field in fd.get_ofb_fields(flow):
558 if field.type == fd.ETH_TYPE:
559 _type = field.eth_type
560 self.log.debug('field-type-eth-type',
561 eth_type=_type)
562
563 elif field.type == fd.IP_PROTO:
564 _proto = field.ip_proto
565 self.log.debug('field-type-ip-proto',
566 ip_proto=_proto)
567
568 elif field.type == fd.IN_PORT:
569 _port = field.port
570 self.log.debug('field-type-in-port',
571 in_port=_port)
572
573 elif field.type == fd.VLAN_VID:
574 _vlan_vid = field.vlan_vid & 0xfff
575 self.log.debug('field-type-vlan-vid',
576 vlan=_vlan_vid)
577
578 elif field.type == fd.VLAN_PCP:
579 _vlan_pcp = field.vlan_pcp
580 self.log.debug('field-type-vlan-pcp',
581 pcp=_vlan_pcp)
582
583 elif field.type == fd.UDP_DST:
584 _udp_dst = field.udp_dst
585 self.log.debug('field-type-udp-dst',
586 udp_dst=_udp_dst)
587
588 elif field.type == fd.UDP_SRC:
589 _udp_src = field.udp_src
590 self.log.debug('field-type-udp-src',
591 udp_src=_udp_src)
592
593 elif field.type == fd.IPV4_DST:
594 _ipv4_dst = field.ipv4_dst
595 self.log.debug('field-type-ipv4-dst',
596 ipv4_dst=_ipv4_dst)
597
598 elif field.type == fd.IPV4_SRC:
599 _ipv4_src = field.ipv4_src
600 self.log.debug('field-type-ipv4-src',
601 ipv4_dst=_ipv4_src)
602
603 elif field.type == fd.METADATA:
604 _metadata = field.table_metadata
605 self.log.debug('field-type-metadata',
606 metadata=_metadata)
607
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400608 elif field.type == fd.TUNNEL_ID:
609 _tunnel_id = field.tunnel_id
610 self.log.debug('field-type-tunnel-id',
611 tunnel_id=_tunnel_id)
612
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500613 else:
614 raise NotImplementedError('field.type={}'.format(
615 field.type))
616
617 for action in fd.get_actions(flow):
618
619 if action.type == fd.OUTPUT:
620 _output = action.output.port
621 self.log.debug('action-type-output',
622 output=_output, in_port=_in_port)
623
624 elif action.type == fd.POP_VLAN:
625 self.log.debug('action-type-pop-vlan',
626 in_port=_in_port)
627
628 elif action.type == fd.PUSH_VLAN:
629 _push_tpid = action.push.ethertype
630 self.log.debug('action-type-push-vlan',
631 push_tpid=_push_tpid, in_port=_in_port)
632 if action.push.ethertype != 0x8100:
633 self.log.error('unhandled-tpid',
634 ethertype=action.push.ethertype)
635
636 elif action.type == fd.SET_FIELD:
637 _field = action.set_field.field.ofb_field
638 assert (action.set_field.field.oxm_class ==
639 OFPXMC_OPENFLOW_BASIC)
640 self.log.debug('action-type-set-field',
641 field=_field, in_port=_in_port)
642 if _field.type == fd.VLAN_VID:
643 _set_vlan_vid = _field.vlan_vid & 0xfff
644 self.log.debug('set-field-type-vlan-vid',
645 vlan_vid=_set_vlan_vid)
646 else:
647 self.log.error('unsupported-action-set-field-type',
648 field_type=_field.type)
649 else:
650 self.log.error('unsupported-action-type',
651 action_type=action.type, in_port=_in_port)
652
Matt Jeanneret810148b2019-09-29 12:44:01 -0400653 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
654 # supported match and action criteria cannot be handled by omci and must be ignored.
655 if _set_vlan_vid is None or _set_vlan_vid == 0:
656 self.log.warn('ignoring-flow-that-does-not-set-vlanid')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500657 else:
Matt Jeanneret810148b2019-09-29 12:44:01 -0400658 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 -0400659 self._add_vlan_filter_task(device, uni_id, uni_port, _set_vlan_vid)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500660 except Exception as e:
661 self.log.exception('failed-to-install-flow', e=e, flow=flow)
662
663
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400664
665 def _add_vlan_filter_task(self, device,uni_id, uni_port, _set_vlan_vid):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500666 assert uni_port is not None
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400667 if uni_id in self._tech_profile_download_done and self._tech_profile_download_done[uni_id] != {}:
668 @inlineCallbacks
669 def success(_results):
670 self.log.info('vlan-tagging-success', 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-pushed')
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400672 self._vlan_filter_task = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500673
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400674 @inlineCallbacks
675 def failure(_reason):
676 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700677 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-failed-retrying')
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400678 self._vlan_filter_task = reactor.callLater(_STARTUP_RETRY_WAIT,
679 self._add_vlan_filter_task, device,uni_port.port_number, uni_port, _set_vlan_vid)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500680
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400681 self.log.info('setting-vlan-tag')
Matt Jeanneret810148b2019-09-29 12:44:01 -0400682 self._vlan_filter_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400683 self._deferred = self._onu_omci_device.task_runner.queue_task(self._vlan_filter_task)
684 self._deferred.addCallbacks(success, failure)
685 else:
686 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
687 uni_id=uni_id)
Matt Jeanneret810148b2019-09-29 12:44:01 -0400688 self._queued_vlan_filter_task[uni_id] = {"device": device,
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400689 "uni_id":uni_id,
690 "uni_port": uni_port,
691 "set_vlan_vid": _set_vlan_vid}
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500692
693 def get_tx_id(self):
694 self.log.debug('function-entry')
695 self.tx_id += 1
696 return self.tx_id
697
Matt Jeannereta32441c2019-03-07 05:16:37 -0500698 def process_inter_adapter_message(self, request):
699 self.log.debug('process-inter-adapter-message', msg=request)
700 try:
701 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
702 omci_msg = InterAdapterOmciMessage()
703 request.body.Unpack(omci_msg)
704 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500705
Matt Jeannereta32441c2019-03-07 05:16:37 -0500706 self.receive_message(omci_msg.message)
707
708 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
709 onu_indication = OnuIndication()
710 request.body.Unpack(onu_indication)
711 self.log.debug('inter-adapter-recv-onu-ind', onu_indication=onu_indication)
712
713 if onu_indication.oper_state == "up":
714 self.create_interface(onu_indication)
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -0400715 elif onu_indication.oper_state == "down" or onu_indication.oper_state=="unreachable":
Matt Jeannereta32441c2019-03-07 05:16:37 -0500716 self.update_interface(onu_indication)
717 else:
718 self.log.error("unknown-onu-indication", onu_indication=onu_indication)
719
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400720 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
721 tech_msg = InterAdapterTechProfileDownloadMessage()
722 request.body.Unpack(tech_msg)
723 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
724
725 self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
726
Matt Jeannereta32441c2019-03-07 05:16:37 -0500727 else:
728 self.log.error("inter-adapter-unhandled-type", request=request)
729
730 except Exception as e:
731 self.log.exception("error-processing-inter-adapter-message", e=e)
732
733 # Called each time there is an onu "up" indication from the olt handler
734 @inlineCallbacks
735 def create_interface(self, onu_indication):
736 self.log.debug('function-entry', onu_indication=onu_indication)
737 self._onu_indication = onu_indication
738
Matt Jeanneretc083f462019-03-11 15:02:01 -0400739 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
740 connect_status=ConnectStatus.REACHABLE)
741
Matt Jeannereta32441c2019-03-07 05:16:37 -0500742 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500743
744 self.log.debug('starting-openomci-statemachine')
745 self._subscribe_to_events()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500746 onu_device.reason = "starting-openomci"
Mahir Gunyelfe6ac432019-09-04 10:17:14 -0700747 reactor.callLater(1, self._onu_omci_device.start,onu_device)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700748 yield self.core_proxy.device_reason_update(self.device_id, onu_device.reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500749 self._heartbeat.enabled = True
750
751 # Currently called each time there is an onu "down" indication from the olt handler
752 # TODO: possibly other reasons to "update" from the olt?
Matt Jeannereta32441c2019-03-07 05:16:37 -0500753 @inlineCallbacks
754 def update_interface(self, onu_indication):
755 self.log.debug('function-entry', onu_indication=onu_indication)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500756
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -0400757 if onu_indication.oper_state == 'down' or onu_indication.oper_state == "unreachable":
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500758 self.log.debug('stopping-openomci-statemachine')
759 reactor.callLater(0, self._onu_omci_device.stop)
760
761 # Let TP download happen again
762 for uni_id in self._tp_service_specific_task:
763 self._tp_service_specific_task[uni_id].clear()
764 for uni_id in self._tech_profile_download_done:
765 self._tech_profile_download_done[uni_id].clear()
766
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700767 self.disable_ports()
768 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
769 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.DISCOVERED,
770 connect_status=ConnectStatus.UNREACHABLE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500771 else:
772 self.log.debug('not-changing-openomci-statemachine')
773
774 # Not currently called by olt or anything else
William Kurkian3a206332019-04-29 11:05:47 -0400775 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500776 def remove_interface(self, data):
777 self.log.debug('function-entry', data=data)
778
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500779 self.log.debug('stopping-openomci-statemachine')
780 reactor.callLater(0, self._onu_omci_device.stop)
781
782 # Let TP download happen again
783 for uni_id in self._tp_service_specific_task:
784 self._tp_service_specific_task[uni_id].clear()
785 for uni_id in self._tech_profile_download_done:
786 self._tech_profile_download_done[uni_id].clear()
787
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700788 self.disable_ports()
789 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500790
791 # TODO: im sure there is more to do here
792
793 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400794 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500795 def remove_gemport(self, data):
796 self.log.debug('remove-gemport', data=data)
William Kurkian3a206332019-04-29 11:05:47 -0400797 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500798 if device.connect_status != ConnectStatus.REACHABLE:
799 self.log.error('device-unreachable')
800 return
801
802 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400803 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500804 def remove_tcont(self, tcont_data, traffic_descriptor_data):
805 self.log.debug('remove-tcont', tcont_data=tcont_data, traffic_descriptor_data=traffic_descriptor_data)
William Kurkian3a206332019-04-29 11:05:47 -0400806 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500807 if device.connect_status != ConnectStatus.REACHABLE:
808 self.log.error('device-unreachable')
809 return
810
811 # TODO: Create some omci task that encompases this what intended
812
813 # Not currently called. Would be called presumably from the olt handler
814 def create_multicast_gemport(self, data):
815 self.log.debug('function-entry', data=data)
816
817 # TODO: create objects and populate for later omci calls
818
819 def disable(self, device):
820 self.log.debug('function-entry', device=device)
821 try:
822 self.log.info('sending-uni-lock-towards-device', device=device)
823
Matt Jeanneret80766692019-05-03 09:58:38 -0400824 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500825 def stop_anyway(reason):
826 # proceed with disable regardless if we could reach the onu. for example onu is unplugged
827 self.log.debug('stopping-openomci-statemachine')
828 reactor.callLater(0, self._onu_omci_device.stop)
829
830 # Let TP download happen again
831 for uni_id in self._tp_service_specific_task:
832 self._tp_service_specific_task[uni_id].clear()
833 for uni_id in self._tech_profile_download_done:
834 self._tech_profile_download_done[uni_id].clear()
835
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700836 self.disable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500837 device.oper_status = OperStatus.UNKNOWN
838 device.reason = "omci-admin-lock"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400839 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500840
841 # lock all the unis
842 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=True)
843 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
844 self._deferred.addCallbacks(stop_anyway, stop_anyway)
845 except Exception as e:
846 log.exception('exception-in-onu-disable', exception=e)
847
William Kurkian3a206332019-04-29 11:05:47 -0400848 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500849 def reenable(self, device):
850 self.log.debug('function-entry', device=device)
851 try:
852 # Start up OpenOMCI state machines for this device
853 # this will ultimately resync mib and unlock unis on successful redownloading the mib
854 self.log.debug('restarting-openomci-statemachine')
855 self._subscribe_to_events()
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700856 yield self.core_proxy.device_reason_update(self.device_id, "restarting-openomci")
serkant.uluderya2cb65f72019-09-30 14:01:51 -0700857 reactor.callLater(1, self._onu_omci_device.start, device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500858 self._heartbeat.enabled = True
859 except Exception as e:
860 log.exception('exception-in-onu-reenable', exception=e)
861
William Kurkian3a206332019-04-29 11:05:47 -0400862 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500863 def reboot(self):
864 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -0400865 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500866 if device.connect_status != ConnectStatus.REACHABLE:
867 self.log.error("device-unreachable")
868 return
869
William Kurkian3a206332019-04-29 11:05:47 -0400870 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500871 def success(_results):
872 self.log.info('reboot-success', _results=_results)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700873 self.disable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500874 device.connect_status = ConnectStatus.UNREACHABLE
875 device.oper_status = OperStatus.DISCOVERED
876 device.reason = "rebooting"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400877 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500878
879 def failure(_reason):
880 self.log.info('reboot-failure', _reason=_reason)
881
882 self._deferred = self._onu_omci_device.reboot()
883 self._deferred.addCallbacks(success, failure)
884
William Kurkian3a206332019-04-29 11:05:47 -0400885 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700886 def disable_ports(self):
887 self.log.info('disable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500888
889 # Disable all ports on that device
Matt Jeanneret80766692019-05-03 09:58:38 -0400890 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500891
William Kurkian3a206332019-04-29 11:05:47 -0400892 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700893 def enable_ports(self):
894 self.log.info('enable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500895
Matt Jeanneret80766692019-05-03 09:58:38 -0400896 # Enable all ports on that device
897 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.ACTIVE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500898
899 # Called just before openomci state machine is started. These listen for events from selected state machines,
900 # most importantly, mib in sync. Which ultimately leads to downloading the mib
901 def _subscribe_to_events(self):
902 self.log.debug('function-entry')
903
904 # OMCI MIB Database sync status
905 bus = self._onu_omci_device.event_bus
906 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
907 OnuDeviceEvents.MibDatabaseSyncEvent)
908 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
909
910 # OMCI Capabilities
911 bus = self._onu_omci_device.event_bus
912 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
913 OnuDeviceEvents.OmciCapabilitiesEvent)
914 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
915
916 # Called when the mib is in sync
917 def in_sync_handler(self, _topic, msg):
918 self.log.debug('function-entry', _topic=_topic, msg=msg)
919 if self._in_sync_subscription is not None:
920 try:
921 in_sync = msg[IN_SYNC_KEY]
922
923 if in_sync:
924 # Only call this once
925 bus = self._onu_omci_device.event_bus
926 bus.unsubscribe(self._in_sync_subscription)
927 self._in_sync_subscription = None
928
929 # Start up device_info load
930 self.log.debug('running-mib-sync')
931 reactor.callLater(0, self._mib_in_sync)
932
933 except Exception as e:
934 self.log.exception('in-sync', e=e)
935
936 def capabilties_handler(self, _topic, _msg):
937 self.log.debug('function-entry', _topic=_topic, msg=_msg)
938 if self._capabilities_subscription is not None:
939 self.log.debug('capabilities-handler-done')
940
941 # Mib is in sync, we can now query what we learned and actually start pushing ME (download) to the ONU.
942 # Currently uses a basic mib download task that create a bridge with a single gem port and uni, only allowing EAP
943 # Implement your own MibDownloadTask if you wish to setup something different by default
Matt Jeanneretc083f462019-03-11 15:02:01 -0400944 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500945 def _mib_in_sync(self):
946 self.log.debug('function-entry')
947
948 omci = self._onu_omci_device
949 in_sync = omci.mib_db_in_sync
950
Matt Jeanneretc083f462019-03-11 15:02:01 -0400951 device = yield self.core_proxy.get_device(self.device_id)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700952 yield self.core_proxy.device_reason_update(self.device_id, 'discovery-mibsync-complete')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500953
954 if not self._dev_info_loaded:
955 self.log.info('loading-device-data-from-mib', in_sync=in_sync, already_loaded=self._dev_info_loaded)
956
957 omci_dev = self._onu_omci_device
958 config = omci_dev.configuration
959
960 # TODO: run this sooner somehow. shouldnt have to wait for mib sync to push an initial download
961 # In Sync, we can register logical ports now. Ideally this could occur on
962 # the first time we received a successful (no timeout) OMCI Rx response.
963 try:
964
965 # sort the lists so we get consistent port ordering.
966 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
967 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
968 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
969 veip_list = sorted(config.veip_entities) if config.veip_entities else []
970
971 if ani_list is None or (pptp_list is None and veip_list is None):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500972 self.log.warn("no-ani-or-unis")
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700973 yield self.core_proxy.device_reason_update(self.device_id, 'onu-missing-required-elements')
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
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -05001002 for entity_id, uni_type in uni_entities.items():
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001003 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:
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001015 yield self.core_proxy.device_reason_update(self.device_id, 'no-usable-unis')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001016 self.log.warn("no-usable-unis")
1017 raise Exception("no-usable-unis")
1018
1019 except Exception as e:
1020 self.log.exception('device-info-load', e=e)
1021 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1022
1023 else:
1024 self.log.info('device-info-already-loaded', in_sync=in_sync, already_loaded=self._dev_info_loaded)
1025
1026 if self._dev_info_loaded:
Matt Jeanneretad9a0f12019-05-09 14:05:49 -04001027 if device.admin_state == AdminState.PREPROVISIONED or device.admin_state == AdminState.ENABLED:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001028
1029 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001030 def success(_results):
1031 self.log.info('mib-download-success', _results=_results)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001032 yield self.enable_ports()
Matt Jeanneretc083f462019-03-11 15:02:01 -04001033 yield self.core_proxy.device_state_update(device.id,
1034 oper_status=OperStatus.ACTIVE, connect_status=ConnectStatus.REACHABLE)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001035 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-downloaded')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001036 self._mib_download_task = None
Devmalya Paulffc89df2019-07-31 17:43:13 -04001037 yield self.onu_active_event()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001038
Matt Jeanneretc083f462019-03-11 15:02:01 -04001039 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001040 def failure(_reason):
1041 self.log.warn('mib-download-failure-retrying', _reason=_reason)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001042 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001043 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1044
1045 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1046 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1047 self.log.info('downloading-initial-mib-configuration')
1048 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1049 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1050 self._deferred.addCallbacks(success, failure)
1051 else:
1052 self.log.info('admin-down-disabling')
1053 self.disable(device)
1054 else:
1055 self.log.info('device-info-not-loaded-skipping-mib-download')
1056
Matt Jeanneretc083f462019-03-11 15:02:01 -04001057 @inlineCallbacks
1058 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001059 self.log.debug('function-entry')
1060
Matt Jeanneretc083f462019-03-11 15:02:01 -04001061 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 -05001062
1063 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1064 uni_name = "uni-{}".format(uni_no)
1065
1066 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
1067
1068 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 -04001069 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001070
1071 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1072 uni_port.entity_id = entity_id
1073 uni_port.enabled = True
1074 uni_port.mac_bridge_port_num = mac_bridge_port_num
1075
1076 self.log.debug("created-uni-port", uni=uni_port)
1077
Matt Jeanneretc083f462019-03-11 15:02:01 -04001078 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001079
1080 self._unis[uni_port.port_number] = uni_port
1081
1082 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=self._onu_indication.onu_id,
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -05001083 uni_ports=self.uni_ports, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001084
Matt Jeanneretc083f462019-03-11 15:02:01 -04001085 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1086 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
1087 MAX_PONS_PER_OLT = 16
Mahir Gunyel0e1588a2019-06-27 06:12:47 -07001088 MAX_ONUS_PER_PON = 128
Matt Jeanneretc083f462019-03-11 15:02:01 -04001089 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001090
Matt Jeanneretc083f462019-03-11 15:02:01 -04001091 assert intf_id < MAX_PONS_PER_OLT
1092 assert onu_id < MAX_ONUS_PER_PON
1093 assert uni_id < MAX_UNIS_PER_ONU
Matt Jeanneret3b7db442019-04-22 16:29:48 -04001094 return intf_id << 11 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001095
1096 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001097 def onu_active_event(self):
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001098 self.log.debug('function-entry')
1099 try:
1100 device = yield self.core_proxy.get_device(self.device_id)
1101 parent_device = yield self.core_proxy.get_device(self.parent_id)
1102 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001103 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001104
1105 self.log.debug("onu-indication-context-data",
1106 pon_id=self._onu_indication.intf_id,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001107 onu_id=self._onu_indication.onu_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001108 registration_id=self.device_id,
1109 device_id=self.device_id,
1110 onu_serial_number=device.serial_number,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001111 olt_serial_number=olt_serial_number,
1112 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001113
Devmalya Paulffc89df2019-07-31 17:43:13 -04001114 self.log.debug("Trying-to-raise-onu-active-event")
1115 OnuActiveEvent(self.events, self.device_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001116 self._onu_indication.intf_id,
1117 device.serial_number,
1118 str(self.device_id),
Devmalya Paulffc89df2019-07-31 17:43:13 -04001119 olt_serial_number,raised_ts,
1120 onu_id=self._onu_indication.onu_id).send(True)
1121 except Exception as active_event_error:
1122 self.log.exception('onu-activated-event-error',
1123 errmsg=active_event_error.message)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001124