blob: 1aaff7d0e604b2f5aa525384dab815a907ddb1aa [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
Girish Gowdra5b499342020-06-16 14:45:51 -070022
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050023import json
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050024import random
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050025from collections import OrderedDict
26
Girish Gowdra5b499342020-06-16 14:45:51 -070027import arrow
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050028import pyvoltha.common.openflow.utils as fd
Girish Gowdra5b499342020-06-16 14:45:51 -070029import six
30import structlog
31from heartbeat import HeartBeat
32from omci.brcm_mcast_task import BrcmMcastTask
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050033from omci.brcm_mib_download_task import BrcmMibDownloadTask
Girish Gowdrae933cd32019-11-21 21:04:41 +053034from omci.brcm_tp_delete_task import BrcmTpDeleteTask
Girish Gowdra5b499342020-06-16 14:45:51 -070035from omci.brcm_tp_setup_task import BrcmTpSetupTask
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050036from omci.brcm_uni_lock_task import BrcmUniLockTask
37from omci.brcm_vlan_filter_task import BrcmVlanFilterTask
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050038from onu_gem_port import OnuGemPort
39from onu_tcont import OnuTCont
40from pon_port import PonPort
Girish Gowdra5b499342020-06-16 14:45:51 -070041from pyvoltha.adapters.common.frameio.frameio import hexify
42from pyvoltha.adapters.common.kvstore.twisted_etcd_store import TwistedEtcdStore
43from pyvoltha.adapters.extensions.events.adapter_events import AdapterEvents
44from pyvoltha.adapters.extensions.events.device_events.onu.onu_active_event import OnuActiveEvent
45from pyvoltha.adapters.extensions.events.device_events.onu.onu_deleted_event import OnuDeletedEvent
46from pyvoltha.adapters.extensions.events.device_events.onu.onu_disabled_event import OnuDisabledEvent
47from pyvoltha.adapters.extensions.events.kpi.onu.onu_omci_pm import OnuOmciPmMetrics
48from pyvoltha.adapters.extensions.events.kpi.onu.onu_pm_metrics import OnuPmMetrics
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050049from pyvoltha.adapters.extensions.omci.omci_defs import EntityOperations, ReasonCodes
Girish Gowdra5b499342020-06-16 14:45:51 -070050from pyvoltha.adapters.extensions.omci.omci_entities import AniG, Tcont, MacBridgeServiceProfile
51from pyvoltha.adapters.extensions.omci.onu_device_entry import OnuDeviceEvents, \
52 OnuDeviceEntry, IN_SYNC_KEY
53from pyvoltha.adapters.extensions.omci.tasks.omci_test_request import OmciTestRequest
54from pyvoltha.common.tech_profile.tech_profile import TechProfile
55from pyvoltha.common.utils.registry import registry
56from twisted.internet import reactor
57from twisted.internet.defer import inlineCallbacks, returnValue
58from uni_port import RESERVED_TRANSPARENT_VLAN
59from uni_port import UniPort, UniType
60from voltha_protos.common_pb2 import OperStatus, ConnectStatus, AdminState
61from voltha_protos.device_pb2 import Port
62from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
63 InterAdapterOmciMessage, InterAdapterTechProfileDownloadMessage, InterAdapterDeleteGemPortMessage, \
64 InterAdapterDeleteTcontMessage
65from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC
66from voltha_protos.openolt_pb2 import OnuIndication
onkarkundargia1e2af22020-01-27 11:51:43 +053067from voltha_protos.voltha_pb2 import TestResponse
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050068
69OP = EntityOperations
70RC = ReasonCodes
71
Girish Gowdradc98d812020-03-20 13:04:58 -070072IS_MULTICAST = 'is_multicast'
Mahir Gunyel5de33fe2020-03-03 22:38:44 -080073GEM_PORT_ID = 'gemport_id'
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050074_STARTUP_RETRY_WAIT = 10
Mahir Gunyele9110a32020-02-20 14:56:50 -080075_PATH_SEPERATOR = "/"
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050076
77
78class BrcmOpenomciOnuHandler(object):
79
80 def __init__(self, adapter, device_id):
81 self.log = structlog.get_logger(device_id=device_id)
Matt Jeanneret08a8e862019-12-20 14:02:32 -050082 self.log.debug('starting-handler')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050083 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050084 self.core_proxy = adapter.core_proxy
85 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050086 self.parent_id = None
87 self.device_id = device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050088 self.proxy_address = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050089 self._enabled = False
Devmalya Paulffc89df2019-07-31 17:43:13 -040090 self.events = None
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050091 self._pm_metrics = None
92 self._pm_metrics_started = False
93 self._test_request = None
94 self._test_request_started = False
Girish Gowdradc98d812020-03-20 13:04:58 -070095 self._tp = dict() # tp_id -> technology profile definition in KV Store.
Matt Jeanneret5e331892019-12-07 21:31:45 -050096 self._reconciling = False
97
98 # Persisted onu configuration needed in case of reconciliation.
99 self._onu_persisted_state = {
100 'onu_id': None,
101 'intf_id': None,
102 'serial_number': None,
103 'admin_state': None,
104 'oper_state': None,
105 'uni_config': list()
106 }
107
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500108 self._unis = dict() # Port # -> UniPort
109
110 self._pon = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500111 self._pon_port_number = 100
112 self.logical_device_id = None
113
114 self._heartbeat = HeartBeat.create(self, device_id)
115
116 # Set up OpenOMCI environment
117 self._onu_omci_device = None
118 self._dev_info_loaded = False
119 self._deferred = None
120
121 self._in_sync_subscription = None
Matt Jeanneretf4113222019-08-14 19:44:34 -0400122 self._port_state_subscription = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500123 self._connectivity_subscription = None
124 self._capabilities_subscription = None
125
126 self.mac_bridge_service_profile_entity_id = 0x201
127 self.gal_enet_profile_entity_id = 0x1
128
129 self._tp_service_specific_task = dict()
130 self._tech_profile_download_done = dict()
Girish Gowdradc98d812020-03-20 13:04:58 -0700131
132 # When the vlan filter is being removed for a given TP ID on a given UNI,
133 # mark that we are expecting a tp delete to happen for this UNI.
134 # Unless the TP delete is complete to not allow new vlan add tasks to this TP ID
135 self._pending_delete_tp = dict()
136
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400137 # Stores information related to queued vlan filter tasks
138 # Dictionary with key being uni_id and value being device,uni port ,uni id and vlan id
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400139 self._queued_vlan_filter_task = dict()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500140
Girish Gowdradc98d812020-03-20 13:04:58 -0700141 self._set_vlan = dict() # uni_id, tp_id -> set_vlan_id
Matt Jeanneret5e331892019-12-07 21:31:45 -0500142
143 # Paths from kv store
144 ONU_PATH = 'service/voltha/openonu'
145
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500146 # Initialize KV store client
147 self.args = registry('main').get_args()
Matt Jeanneret5e331892019-12-07 21:31:45 -0500148 host, port = self.args.etcd.split(':', 1)
149 self.tp_kv_client = TwistedEtcdStore(host, port, TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
150 self.onu_kv_client = TwistedEtcdStore(host, port, ONU_PATH)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500151
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500152 @property
153 def enabled(self):
154 return self._enabled
155
156 @enabled.setter
157 def enabled(self, value):
158 if self._enabled != value:
159 self._enabled = value
160
161 @property
162 def omci_agent(self):
163 return self.adapter.omci_agent
164
165 @property
166 def omci_cc(self):
167 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
168
169 @property
170 def heartbeat(self):
171 return self._heartbeat
172
173 @property
174 def uni_ports(self):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500175 return list(self._unis.values())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500176
177 def uni_port(self, port_no_or_name):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500178 if isinstance(port_no_or_name, six.string_types):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500179 return next((uni for uni in self.uni_ports
180 if uni.name == port_no_or_name), None)
181
182 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
183 return next((uni for uni in self.uni_ports
Girish Gowdrae933cd32019-11-21 21:04:41 +0530184 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500185
186 @property
187 def pon_port(self):
188 return self._pon
189
Girish Gowdraa73ee452019-12-20 18:52:17 +0530190 @property
191 def onu_omci_device(self):
192 return self._onu_omci_device
193
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500194 def receive_message(self, msg):
195 if self.omci_cc is not None:
196 self.omci_cc.receive_message(msg)
197
198 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500199 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500200 def activate(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700201 self.log.debug('activate-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500202
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500203 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500204 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500205 assert device.proxy_address.device_id
206
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500207 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500208 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500209 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500210 if self.enabled is not True:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700211 self.log.info('activating-new-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500212 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500213 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500214 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500215 device.reason = 'activating-onu'
216
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500217 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400218 # pm_metrics requires a logical device id. For now set to just device_id
219 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500220
Matt Jeanneret5e331892019-12-07 21:31:45 -0500221 self._onu_persisted_state['serial_number'] = device.serial_number
222 try:
223 self.log.debug('updating-onu-state', device_id=self.device_id,
224 onu_persisted_state=self._onu_persisted_state)
225 yield self.onu_kv_client.set(self.device_id, json.dumps(self._onu_persisted_state))
226 except Exception as e:
227 self.log.error('could-not-store-onu-state', device_id=self.device_id,
228 onu_persisted_state=self._onu_persisted_state, e=e)
229 # if we cannot write to storage we can proceed, for now.
230 # later onu indications from the olt will have another chance
231
Matt Jeannereta32441c2019-03-07 05:16:37 -0500232 yield self.core_proxy.device_update(device)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500233 self.log.debug('device-updated', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500234
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700235 yield self._init_pon_state()
Matteo Scandolod8d73172019-11-26 12:15:15 -0700236 self.log.debug('pon state initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500237
Matt Jeanneret5e331892019-12-07 21:31:45 -0500238 yield self._init_metrics()
239 self.log.debug('metrics initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500240
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500241 self.enabled = True
242 else:
243 self.log.info('onu-already-activated')
244
245 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400246 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500247 def reconcile(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700248 self.log.debug('reconcile-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500249
Matt Jeanneret5e331892019-12-07 21:31:45 -0500250 if self._reconciling:
251 self.log.debug('already-running-reconcile-device', device_id=device.id, serial_number=device.serial_number)
252 return
253
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500254 # first we verify that we got parent reference and proxy info
255 assert device.parent_id
256 assert device.proxy_address.device_id
257
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700258 self.proxy_address = device.proxy_address
259 self.parent_id = device.parent_id
260 self._pon_port_number = device.parent_port_no
261
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500262 if self.enabled is not True:
Matt Jeanneret5e331892019-12-07 21:31:45 -0500263 self._reconciling = True
264 self.log.info('reconciling-openonu-device')
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700265 self.logical_device_id = self.device_id
Matt Jeanneret5e331892019-12-07 21:31:45 -0500266
267 try:
268 query_data = yield self.onu_kv_client.get(device.id)
269 self._onu_persisted_state = json.loads(query_data)
270 self.log.debug('restored-onu-state', device_id=self.device_id,
271 onu_persisted_state=self._onu_persisted_state)
272 except Exception as e:
273 self.log.error('no-stored-onu-state', device_id=device.id, e=e)
274 # there is nothing we can do without data. flag the device as UNKNOWN and cannot reconcile
275 # likely it will take manual steps to delete/re-add this onu
276 yield self.core_proxy.device_reason_update(self.device_id, "cannot-reconcile")
277 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.UNKNOWN)
278 return
279
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700280 self._init_pon_state()
Matt Jeanneret5e331892019-12-07 21:31:45 -0500281 self.log.debug('pon state initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500282
Matt Jeanneret5e331892019-12-07 21:31:45 -0500283 self._init_metrics()
284 self.log.debug('metrics initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500285
Matt Jeanneret5e331892019-12-07 21:31:45 -0500286 self._subscribe_to_events()
287 # need to restart omci start machines and reload mib database. once db is loaded we can finish reconcile
288 self._onu_omci_device.start(device)
289 self._heartbeat.enabled = True
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500290
291 self.enabled = True
292 else:
293 self.log.info('onu-already-activated')
294
295 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700296 def _init_pon_state(self):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500297 self.log.debug('init-pon-state', device_id=self.device_id, device_logical_id=self.logical_device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500298
299 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500300 self._pon.add_peer(self.parent_id, self._pon_port_number)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700301 self.log.debug('adding-pon-port-to-agent',
302 type=self._pon.get_port().type,
303 admin_state=self._pon.get_port().admin_state,
304 oper_status=self._pon.get_port().oper_status,
305 )
Matt Jeanneret0c287892019-02-28 11:48:00 -0500306
Matt Jeanneret5e331892019-12-07 21:31:45 -0500307 if not self._reconciling:
308 yield self.core_proxy.port_created(self.device_id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500309
Matteo Scandolod8d73172019-11-26 12:15:15 -0700310 self.log.debug('added-pon-port-to-agent',
311 type=self._pon.get_port().type,
312 admin_state=self._pon.get_port().admin_state,
313 oper_status=self._pon.get_port().oper_status,
314 )
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500315
316 # Create and start the OpenOMCI ONU Device Entry for this ONU
317 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500318 self.core_proxy,
319 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500320 support_classes=self.adapter.broadcom_omci,
321 custom_me_map=self.adapter.custom_me_entities())
322 # Port startup
323 if self._pon is not None:
324 self._pon.enabled = True
325
Matt Jeanneret5e331892019-12-07 21:31:45 -0500326 @inlineCallbacks
327 def _init_metrics(self):
328 self.log.debug('init-metrics', device_id=self.device_id, device_logical_id=self.logical_device_id)
329
330 serial_number = self._onu_persisted_state.get('serial_number')
331
332 ############################################################################
333 # Setup Alarm handler
334 self.events = AdapterEvents(self.core_proxy, self.device_id, self.logical_device_id,
335 serial_number)
336 ############################################################################
337 # Setup PM configuration for this device
338 # Pass in ONU specific options
339 kwargs = {
340 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
341 'heartbeat': self.heartbeat,
342 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
343 }
344 self.log.debug('create-pm-metrics', device_id=self.device_id, serial_number=serial_number)
345 self._pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
346 self.logical_device_id, serial_number,
347 grouped=True, freq_override=False, **kwargs)
348 pm_config = self._pm_metrics.make_proto()
349 self._onu_omci_device.set_pm_config(self._pm_metrics.omci_pm.openomci_interval_pm)
350 self.log.debug("initial-pm-config", device_id=self.device_id, serial_number=serial_number)
351
352 if not self._reconciling:
353 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
354
355 # Note, ONU ID and UNI intf set in add_uni_port method
356 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
357 ani_ports=[self._pon])
358
359 # Code to Run OMCI Test Action
360 kwargs_omci_test_action = {
361 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
362 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
363 }
364 self._test_request = OmciTestRequest(self.core_proxy,
365 self.omci_agent, self.device_id,
366 AniG, serial_number,
367 self.logical_device_id,
368 exclusive=False,
369 **kwargs_omci_test_action)
370
371 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500372 def delete(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700373 self.log.info('delete-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneret5e331892019-12-07 21:31:45 -0500374 try:
375 yield self.onu_kv_client.delete(device.id)
376 except Exception as e:
377 self.log.error('could-not-delete-onu-state', device_id=device.id, e=e)
378
Devmalya Paul1e1b1722020-05-07 02:51:15 -0400379 try:
380 self._deferred.cancel()
381 self._test_request.stop_collector()
382 self._pm_metrics.stop_collector()
383 self.log.debug('removing-openomci-statemachine')
384 self.omci_agent.remove_device(device.id, cleanup=True)
385 yield self.onu_deleted_event()
386 except Exception as e:
387 self.log.error('could-not-delete-onu', device_id=device.id, e=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500388
389 def _create_tconts(self, uni_id, us_scheduler):
390 alloc_id = us_scheduler['alloc_id']
391 q_sched_policy = us_scheduler['q_sched_policy']
392 self.log.debug('create-tcont', us_scheduler=us_scheduler)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800393 # TODO: revisit for multi tconts support
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800394 new_tconts = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500395 tcontdict = dict()
396 tcontdict['alloc-id'] = alloc_id
397 tcontdict['q_sched_policy'] = q_sched_policy
398 tcontdict['uni_id'] = uni_id
399
Matt Jeanneret3789d0d2020-01-19 09:03:42 -0500400 tcont = OnuTCont.create(self, tcont=tcontdict)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500401
Girish Gowdra7c1240c2020-07-15 15:06:42 -0700402 success = self._pon.add_tcont(tcont, True)
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500403 if success:
404 new_tconts.append(tcont)
405 self.log.debug('pon-add-tcont', tcont=tcont)
406
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800407 return new_tconts
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500408
409 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
410 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
411 self.log.debug('create-gemport',
412 gem_ports=gem_ports, direction=direction)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530413 new_gem_ports = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500414 for gem_port in gem_ports:
415 gemdict = dict()
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800416 if gem_port[IS_MULTICAST] == 'True':
417 gemdict[GEM_PORT_ID] = gem_port['multicast_gem_id']
418 gemdict[IS_MULTICAST] = True
419 else:
420 gemdict[GEM_PORT_ID] = gem_port[GEM_PORT_ID]
421 gemdict[IS_MULTICAST] = False
422
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500423 gemdict['direction'] = direction
424 gemdict['alloc_id_ref'] = alloc_id_ref
425 gemdict['encryption'] = gem_port['aes_encryption']
426 gemdict['discard_config'] = dict()
427 gemdict['discard_config']['max_probability'] = \
428 gem_port['discard_config']['max_probability']
429 gemdict['discard_config']['max_threshold'] = \
430 gem_port['discard_config']['max_threshold']
431 gemdict['discard_config']['min_threshold'] = \
432 gem_port['discard_config']['min_threshold']
433 gemdict['discard_policy'] = gem_port['discard_policy']
434 gemdict['max_q_size'] = gem_port['max_q_size']
435 gemdict['pbit_map'] = gem_port['pbit_map']
436 gemdict['priority_q'] = gem_port['priority_q']
437 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
438 gemdict['weight'] = gem_port['weight']
439 gemdict['uni_id'] = uni_id
440
441 gem_port = OnuGemPort.create(self, gem_port=gemdict)
442
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500443 success = self._pon.add_gem_port(gem_port, True)
444 if success:
445 new_gem_ports.append(gem_port)
446 self.log.debug('pon-add-gemport', gem_port=gem_port)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500447
Girish Gowdrae933cd32019-11-21 21:04:41 +0530448 return new_gem_ports
449
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800450 def _execute_queued_vlan_filter_tasks(self, uni_id, tp_id):
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400451 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
452 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
453 # or initial-mib-download is not done. Once the tp_task is completed, we execute
454 # such queued vlan-filter tasks
455 try:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800456 if uni_id in self._queued_vlan_filter_task and tp_id in self._queued_vlan_filter_task[uni_id]:
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400457 self.log.info("executing-queued-vlan-filter-task",
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800458 uni_id=uni_id, tp_id=tp_id)
Mahir Gunyela982ec32020-02-25 12:30:37 -0800459 for filter_info in self._queued_vlan_filter_task[uni_id][tp_id]:
460 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800461 uni_id=uni_id, uni_port=filter_info.get("uni_port"),
462 match_vlan=filter_info.get("match_vlan"),
463 _set_vlan_vid=filter_info.get("set_vlan_vid"),
464 _set_vlan_pcp=filter_info.get("set_vlan_pcp"),
465 tp_id=filter_info.get("tp_id"))
Girish Gowdraaf98a082020-03-05 16:40:51 -0800466 # Now remove the entry from the dictionary
Girish Gowdraaf98a082020-03-05 16:40:51 -0800467 self.log.debug("executed-queued-vlan-filter-task",
468 uni_id=uni_id, tp_id=tp_id)
Girish Gowdraa63eda82020-05-12 13:40:04 -0700469
470 # Now delete the key entry for the tp_id once we have handled the
471 # queued vlan filter tasks for that tp_id
472 del self._queued_vlan_filter_task[uni_id][tp_id]
473 # If the queued vlan filter tasks for all the tp_ids on a given
474 # uni_id is handled, then delete the uni_id key
475 if len(self._queued_vlan_filter_task[uni_id]) == 0:
476 del self._queued_vlan_filter_task[uni_id]
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400477 except Exception as e:
478 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
479
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500480 def _do_tech_profile_configuration(self, uni_id, tp):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500481 us_scheduler = tp['us_scheduler']
482 alloc_id = us_scheduler['alloc_id']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800483 new_tconts = self._create_tconts(uni_id, us_scheduler)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500484 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800485 new_upstream_gems = self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500486 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800487 new_downstream_gems = self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
488
489 new_gems = []
490 new_gems.extend(new_upstream_gems)
491 new_gems.extend(new_downstream_gems)
492
493 return new_tconts, new_gems
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500494
Matt Jeanneret5e331892019-12-07 21:31:45 -0500495 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500496 def load_and_configure_tech_profile(self, uni_id, tp_path):
497 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
Mahir Gunyele9110a32020-02-20 14:56:50 -0800498 tp_id = self.extract_tp_id_from_path(tp_path)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500499 if uni_id not in self._tp_service_specific_task:
500 self._tp_service_specific_task[uni_id] = dict()
501
502 if uni_id not in self._tech_profile_download_done:
503 self._tech_profile_download_done[uni_id] = dict()
504
Mahir Gunyele9110a32020-02-20 14:56:50 -0800505 if tp_id not in self._tech_profile_download_done[uni_id]:
506 self._tech_profile_download_done[uni_id][tp_id] = False
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500507
Mahir Gunyele9110a32020-02-20 14:56:50 -0800508 if not self._tech_profile_download_done[uni_id][tp_id]:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500509 try:
510 if tp_path in self._tp_service_specific_task[uni_id]:
511 self.log.info("tech-profile-config-already-in-progress",
Girish Gowdrae933cd32019-11-21 21:04:41 +0530512 tp_path=tp_path)
Matt Jeanneret5e331892019-12-07 21:31:45 -0500513 returnValue(None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500514
Matt Jeanneret5e331892019-12-07 21:31:45 -0500515 tpstored = yield self.tp_kv_client.get(tp_path)
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500516 tpstring = tpstored.decode('ascii')
517 tp = json.loads(tpstring)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800518 self._tp[tp_id] = tp
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500519 self.log.debug("tp-instance", tp=tp)
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800520 tconts, gem_ports = self._do_tech_profile_configuration(uni_id, tp)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700521
William Kurkian3a206332019-04-29 11:05:47 -0400522 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500523 def success(_results):
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800524 self.log.info("tech-profile-config-done-successfully", uni_id=uni_id, tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500525 if tp_path in self._tp_service_specific_task[uni_id]:
526 del self._tp_service_specific_task[uni_id][tp_path]
Mahir Gunyele9110a32020-02-20 14:56:50 -0800527 self._tech_profile_download_done[uni_id][tp_id] = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400528 # Now execute any vlan filter tasks that were queued for later
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800529 reactor.callInThread(self._execute_queued_vlan_filter_tasks, uni_id, tp_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500530 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-success')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530531
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800532 # Execute mcast task
533 for gem in gem_ports:
Girish Gowdradc98d812020-03-20 13:04:58 -0700534 self.log.debug("checking-multicast-service-for-gem ", gem=gem)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800535 if gem.mcast is True:
Girish Gowdradc98d812020-03-20 13:04:58 -0700536 self.log.info("found-multicast-service-for-gem ", gem=gem, uni_id=uni_id, tp_id=tp_id)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800537 reactor.callInThread(self.start_multicast_service, uni_id, tp_path)
538 self.log.debug("started_multicast_service-successfully", tconts=tconts, gems=gem_ports)
539 break
540
William Kurkian3a206332019-04-29 11:05:47 -0400541 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500542 def failure(_reason):
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800543 self.log.warn('tech-profile-config-failure-retrying', uni_id=uni_id, tp_id=tp_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +0530544 _reason=_reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500545 if tp_path in self._tp_service_specific_task[uni_id]:
546 del self._tp_service_specific_task[uni_id][tp_path]
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800547 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500548 reactor.callLater(retry, self.load_and_configure_tech_profile,
549 uni_id, tp_path)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500550 yield self.core_proxy.device_reason_update(self.device_id,
551 'tech-profile-config-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500552
Mahir Gunyela982ec32020-02-25 12:30:37 -0800553 self.log.info('downloading-tech-profile-configuration', uni_id=uni_id, tp_id=tp_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530554 self.log.debug("tconts-gems-to-install", tconts=tconts, gem_ports=gem_ports)
555
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500556 self.log.debug("current-cached-tconts", tconts=list(self.pon_port.tconts.values()))
557 self.log.debug("current-cached-gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
558
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500559 self._tp_service_specific_task[uni_id][tp_path] = \
Mahir Gunyele9110a32020-02-20 14:56:50 -0800560 BrcmTpSetupTask(self.omci_agent, self, uni_id, tconts, gem_ports, tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500561 self._deferred = \
Girish Gowdrae933cd32019-11-21 21:04:41 +0530562 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500563 self._deferred.addCallbacks(success, failure)
564
565 except Exception as e:
566 self.log.exception("error-loading-tech-profile", e=e)
567 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700568 # There is an active tech-profile task ongoing on this UNI port. So, reschedule this task
569 # after a short interval
570 if uni_id in self._tp_service_specific_task and len(self._tp_service_specific_task[uni_id]):
571 self.log.debug("active-tp-tasks-in-progress-for-uni--scheduling-this-task-for-later",
572 uni_id=uni_id, tp_path=tp_path)
573 reactor.callLater(0.2, self.load_and_configure_tech_profile,
574 uni_id, tp_path)
575 return
576
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500577 self.log.info("tech-profile-config-already-done")
Girish Gowdradc98d812020-03-20 13:04:58 -0700578
Girish Gowdrae933cd32019-11-21 21:04:41 +0530579 # Could be a case where TP exists but new gem-ports are getting added dynamically
Matt Jeanneret5e331892019-12-07 21:31:45 -0500580 tpstored = yield self.tp_kv_client.get(tp_path)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530581 tpstring = tpstored.decode('ascii')
582 tp = json.loads(tpstring)
583 upstream_gems = []
584 downstream_gems = []
585 # Find out the new Gem ports that are getting added afresh.
586 for gp in tp['upstream_gem_port_attribute_list']:
587 if self.pon_port.gem_port(gp['gemport_id'], "upstream"):
588 # gem port already exists
589 continue
590 upstream_gems.append(gp)
591 for gp in tp['downstream_gem_port_attribute_list']:
592 if self.pon_port.gem_port(gp['gemport_id'], "downstream"):
593 # gem port already exists
594 continue
595 downstream_gems.append(gp)
596
597 us_scheduler = tp['us_scheduler']
598 alloc_id = us_scheduler['alloc_id']
599
600 if len(upstream_gems) > 0 or len(downstream_gems) > 0:
601 self.log.info("installing-new-gem-ports", upstream_gems=upstream_gems, downstream_gems=downstream_gems)
602 new_upstream_gems = self._create_gemports(uni_id, upstream_gems, alloc_id, "UPSTREAM")
603 new_downstream_gems = self._create_gemports(uni_id, downstream_gems, alloc_id, "DOWNSTREAM")
604 new_gems = []
605 new_gems.extend(new_upstream_gems)
606 new_gems.extend(new_downstream_gems)
607
608 def success(_results):
609 self.log.info("new-gem-ports-successfully-installed", result=_results)
610
611 def failure(_reason):
612 self.log.warn('new-gem-port-install-failed--retrying',
613 _reason=_reason)
614 # Remove gem ports from cache. We will re-add them during the retry
615 for gp in new_gems:
616 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
617
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800618 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500619 reactor.callLater(retry, self.load_and_configure_tech_profile,
620 uni_id, tp_path)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530621
Girish Gowdra8777c852020-07-23 12:00:23 -0700622 if self._pon.get_tcont(alloc_id) is None:
623 self.log.error("no-valid-tcont-reference-for-tp-id--not-installing-gem", alloc_id=alloc_id, tp_id=tp_id)
624 return
625
Girish Gowdrae933cd32019-11-21 21:04:41 +0530626 self._tp_service_specific_task[uni_id][tp_path] = \
Girish Gowdra8777c852020-07-23 12:00:23 -0700627 BrcmTpSetupTask(self.omci_agent, self, uni_id, [self._pon.get_tcont(alloc_id)], new_gems, tp_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530628 self._deferred = \
629 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
630 self._deferred.addCallbacks(success, failure)
Girish Gowdradc98d812020-03-20 13:04:58 -0700631
Matt Jeanneret5e331892019-12-07 21:31:45 -0500632 @inlineCallbacks
Girish Gowdradc98d812020-03-20 13:04:58 -0700633 def start_multicast_service(self, uni_id, tp_path, retry_count=0):
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800634 self.log.debug("starting-multicast-service", uni_id=uni_id, tp_path=tp_path)
635 tp_id = self.extract_tp_id_from_path(tp_path)
636 if uni_id in self._set_vlan and tp_id in self._set_vlan[uni_id]:
637 try:
638 tp = self._tp[tp_id]
639 if tp is None:
Matt Jeanneret5e331892019-12-07 21:31:45 -0500640 tpstored = yield self.tp_kv_client.get(tp_path)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800641 tpstring = tpstored.decode('ascii')
642 tp = json.loads(tpstring)
643 if tp is None:
644 self.log.error("cannot-find-tp-to-start-multicast-service", uni_id=uni_id, tp_path=tp_path)
645 return
646 else:
647 self._tp[tp_id] = tp
648
649 self.log.debug("mcast-vlan-learned-before", self._set_vlan[uni_id][tp_id], uni_id=uni_id, tp_id=tp_id)
Girish Gowdradc98d812020-03-20 13:04:58 -0700650
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800651 def success(_results):
652 self.log.debug('multicast-success', uni_id=uni_id)
653 self._multicast_task = None
654
655 def failure(_reason):
656 self.log.warn('multicast-failure', _reason=_reason)
Girish Gowdradc98d812020-03-20 13:04:58 -0700657 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800658 reactor.callLater(retry, self.start_multicast_service,
Girish Gowdradc98d812020-03-20 13:04:58 -0700659 uni_id, tp_path)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800660
661 self.log.debug('starting-multicast-task', mcast_vlan_id=self._set_vlan[uni_id][tp_id])
662 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
663 for i in range(len(downstream_gem_port_attribute_list)):
664 if IS_MULTICAST in downstream_gem_port_attribute_list[i] and \
665 downstream_gem_port_attribute_list[i][IS_MULTICAST] == 'True':
Girish Gowdradc98d812020-03-20 13:04:58 -0700666 dynamic_access_control_list_table = downstream_gem_port_attribute_list[i][
667 'dynamic_access_control_list'].split("-")
668 static_access_control_list_table = downstream_gem_port_attribute_list[i][
669 'static_access_control_list'].split("-")
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800670 multicast_gem_id = downstream_gem_port_attribute_list[i]['multicast_gem_id']
671 break
672
673 self._multicast_task = BrcmMcastTask(self.omci_agent, self, self.device_id, uni_id, tp_id,
Girish Gowdradc98d812020-03-20 13:04:58 -0700674 self._set_vlan[uni_id][tp_id], dynamic_access_control_list_table,
675 static_access_control_list_table, multicast_gem_id)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800676 self._deferred = self._onu_omci_device.task_runner.queue_task(self._multicast_task)
677 self._deferred.addCallbacks(success, failure)
678 except Exception as e:
679 self.log.exception("error-loading-multicast", e=e)
680 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700681 if retry_count < 30:
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800682 retry_count = +1
Girish Gowdradc98d812020-03-20 13:04:58 -0700683 self.log.debug("going-to-wait-for-flow-to-learn-mcast-vlan", uni_id=uni_id, tp_id=tp_id,
684 retry=retry_count)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800685 reactor.callLater(0.5, self.start_multicast_service, uni_id, tp_path, retry_count)
686 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700687 self.log.error("mcast-vlan-not-configured-yet-failing-mcast-service-conf", uni_id=uni_id, tp_id=tp_id,
688 retry=retry_count)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530689
Girish Gowdraba4b1812020-07-17 12:21:26 -0700690 def _clear_alloc_id_gem_port_from_internal_cache(self, alloc_id=None, gem_port_id=None):
691 tcont = None
692 gem_port = None
693 if alloc_id is not None:
694 self.log.debug("current-cached-tconts", tconts=list(self.pon_port.tconts.values()))
695 for tc in list(self.pon_port.tconts.values()):
696 if tc.alloc_id == alloc_id:
697 self.log.info("removing-tcont-from-internal-cache",
698 alloc_id=alloc_id)
699 tcont = tc
700 self.pon_port.remove_tcont(tc.alloc_id, False)
701
702 if gem_port_id is not None:
703 self.log.debug("current-cached-gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
704 for gp in list(self.pon_port.gem_ports.values()):
705 if gp.gem_id == gem_port_id:
706 self.log.info("removing-gem-from-internal-cache",
707 gem_port_id=gem_port_id, direction=gp.direction)
708 gem_port = gp
709 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
710
711 return tcont, gem_port
712
Girish Gowdrae933cd32019-11-21 21:04:41 +0530713 def delete_tech_profile(self, uni_id, tp_path, alloc_id=None, gem_port_id=None):
714 try:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800715 tp_table_id = self.extract_tp_id_from_path(tp_path)
Girish Gowdraba4b1812020-07-17 12:21:26 -0700716 # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
717 # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
718 # due to additional tasks on different UNIs. So, it we cannot use the pon_port affter
719 # this initializer
720 tcont, gem_port = self._clear_alloc_id_gem_port_from_internal_cache(alloc_id, gem_port_id)
721
722 if uni_id not in self._tech_profile_download_done:
Naga Manjunathe433c712020-01-02 17:27:20 +0530723 self.log.warn("tp-key-is-not-present", uni_id=uni_id)
724 return
725
Girish Gowdraba4b1812020-07-17 12:21:26 -0700726 if tp_table_id not in self._tech_profile_download_done[uni_id]:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800727 self.log.warn("tp-id-is-not-present", uni_id=uni_id, tp_id=tp_table_id)
Naga Manjunathe433c712020-01-02 17:27:20 +0530728 return
729
Mahir Gunyele9110a32020-02-20 14:56:50 -0800730 if self._tech_profile_download_done[uni_id][tp_table_id] is not True:
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800731 self.log.error("tp-download-is-not-done-in-order-to-process-tp-delete", uni_id=uni_id,
732 tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530733 return
734
735 if alloc_id is None and gem_port_id is None:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800736 self.log.error("alloc-id-and-gem-port-id-are-none", uni_id=uni_id, tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530737 return
738
Girish Gowdrae933cd32019-11-21 21:04:41 +0530739 @inlineCallbacks
740 def success(_results):
741 if gem_port_id:
742 self.log.info("gem-port-delete-done-successfully")
743 if alloc_id:
744 self.log.info("tcont-delete-done-successfully")
745 # The deletion of TCONT marks the complete deletion of tech-profile
746 try:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800747 del self._tech_profile_download_done[uni_id][tp_table_id]
Girish Gowdradc98d812020-03-20 13:04:58 -0700748 self.log.debug("tp-profile-download-flag-cleared", uni_id=uni_id, tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530749 del self._tp_service_specific_task[uni_id][tp_path]
Girish Gowdradc98d812020-03-20 13:04:58 -0700750 self.log.debug("tp-service-specific-task-cleared", uni_id=uni_id, tp_id=tp_table_id)
751 del self._pending_delete_tp[uni_id][tp_table_id]
752 self.log.debug("pending-delete-tp-task-flag-cleared", uni_id=uni_id, tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530753 except Exception as ex:
754 self.log.error("del-tp-state-info", e=ex)
755
756 # TODO: There could be multiple TP on the UNI, and also the ONU.
757 # TODO: But the below reason updates for the whole device.
758 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-delete-success')
759
760 @inlineCallbacks
Girish Gowdraa73ee452019-12-20 18:52:17 +0530761 def failure(_reason):
Girish Gowdrae933cd32019-11-21 21:04:41 +0530762 self.log.warn('tech-profile-delete-failure-retrying',
763 _reason=_reason)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500764 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
765 reactor.callLater(retry, self.delete_tech_profile, uni_id, tp_path, alloc_id, gem_port_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500766 yield self.core_proxy.device_reason_update(self.device_id,
767 'tech-profile-config-delete-failure-retrying')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530768
769 self.log.info('deleting-tech-profile-configuration')
770
Girish Gowdraa73ee452019-12-20 18:52:17 +0530771 if tcont is None and gem_port is None:
772 if alloc_id is not None:
773 self.log.error("tcont-info-corresponding-to-alloc-id-not-found", alloc_id=alloc_id)
774 if gem_port_id is not None:
775 self.log.error("gem-port-info-corresponding-to-gem-port-id-not-found", gem_port_id=gem_port_id)
776 return
777
Girish Gowdrae933cd32019-11-21 21:04:41 +0530778 self._tp_service_specific_task[uni_id][tp_path] = \
779 BrcmTpDeleteTask(self.omci_agent, self, uni_id, tp_table_id,
780 tcont=tcont, gem_port=gem_port)
781 self._deferred = \
782 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
783 self._deferred.addCallbacks(success, failure)
784 except Exception as e:
785 self.log.exception("failed-to-delete-tp",
786 e=e, uni_id=uni_id, tp_path=tp_path,
787 alloc_id=alloc_id, gem_port_id=gem_port_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500788
Rohan Agrawalf0f8c292020-06-01 09:30:55 +0000789 def update_pm_config(self, device, pm_configs):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500790 # TODO: This has not been tested
Rohan Agrawalf0f8c292020-06-01 09:30:55 +0000791 self.log.info('update_pm_config', pm_configs=pm_configs)
792 self._pm_metrics.update(pm_configs)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500793
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800794 def remove_onu_flows(self, device, flows):
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500795 self.log.debug('remove-onu-flows')
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800796
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800797 # no point in removing omci flows if the device isnt reachable
798 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800799 device.admin_state != AdminState.ENABLED:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800800 self.log.warn("device-disabled-or-offline-skipping-remove-flow",
801 admin=device.admin_state, connect=device.connect_status)
802 return
803
804 for flow in flows:
805 # if incoming flow contains cookie, then remove from ONU
806 if flow.cookie:
807 self.log.debug("remove-flow", device_id=device.id, flow=flow)
808
809 def is_downstream(port):
810 return port == self._pon_port_number
811
812 def is_upstream(port):
813 return not is_downstream(port)
814
815 try:
816 _in_port = fd.get_in_port(flow)
817 assert _in_port is not None
818
819 _out_port = fd.get_out_port(flow) # may be None
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800820
821 if is_downstream(_in_port):
822 self.log.debug('downstream-flow-no-need-to-remove', in_port=_in_port, out_port=_out_port,
823 device_id=device.id)
824 # extended vlan tagging operation will handle it
825 continue
826 elif is_upstream(_in_port):
827 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
828 if fd.is_dhcp_flow(flow):
829 self.log.debug('The dhcp trap-to-host flow will be discarded', device_id=device.id)
830 return
831
Mahir Gunyel45610b42020-03-16 17:29:01 -0700832 _match_vlan_vid = None
833 for field in fd.get_ofb_fields(flow):
834 if field.type == fd.VLAN_VID:
835 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
836 _match_vlan_vid = RESERVED_TRANSPARENT_VLAN
837 else:
838 _match_vlan_vid = field.vlan_vid & 0xfff
839 self.log.debug('field-type-vlan-vid',
840 vlan=_match_vlan_vid)
841
842 _set_vlan_vid = None
843 _set_vlan_pcp = None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800844 # Retrieve the VLAN_VID that needs to be removed from the EVTO rule on the ONU.
845 for action in fd.get_actions(flow):
846 if action.type == fd.SET_FIELD:
847 _field = action.set_field.field.ofb_field
848 assert (action.set_field.field.oxm_class ==
849 OFPXMC_OPENFLOW_BASIC)
850 if _field.type == fd.VLAN_VID:
Mahir Gunyel45610b42020-03-16 17:29:01 -0700851 _set_vlan_vid = _field.vlan_vid & 0xfff
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800852 self.log.debug('vlan-vid-to-remove',
Mahir Gunyel45610b42020-03-16 17:29:01 -0700853 _vlan_vid=_set_vlan_vid, in_port=_in_port)
854 elif _field.type == fd.VLAN_PCP:
855 _set_vlan_pcp = _field.vlan_pcp
856 self.log.debug('set-field-type-vlan-pcp',
857 vlan_pcp=_set_vlan_pcp)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800858
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800859 uni_port = self.uni_port(_in_port)
860 uni_id = _in_port & 0xF
861 else:
862 raise Exception('port should be 1 or 2 by our convention')
863
864 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
865
866 tp_id = self.get_tp_id_in_flow(flow)
Girish Gowdradc98d812020-03-20 13:04:58 -0700867 # The vlan filter remove should be followed by a TP deleted for that TP ID.
868 # Use this information to re-schedule any vlan filter add tasks for the same TP ID again.
869 # First check if the TP download was done, before we access that TP delete is necessary
Mahir Gunyel45610b42020-03-16 17:29:01 -0700870 if uni_id in self._tech_profile_download_done and tp_id in self._tech_profile_download_done[
871 uni_id] and \
Girish Gowdradc98d812020-03-20 13:04:58 -0700872 self._tech_profile_download_done[uni_id][tp_id] is True:
873 if uni_id not in self._pending_delete_tp:
874 self._pending_delete_tp[uni_id] = dict()
875 self._pending_delete_tp[uni_id][tp_id] = True
876 else:
877 self._pending_delete_tp[uni_id][tp_id] = True
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800878 # Deleting flow from ONU.
Mahir Gunyel45610b42020-03-16 17:29:01 -0700879 self._remove_vlan_filter_task(device, uni_id, uni_port=uni_port,
880 _set_vlan_pcp=_set_vlan_pcp,
881 _set_vlan_vid=_set_vlan_vid,
882 match_vlan=_match_vlan_vid,
883 tp_id=tp_id)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800884 # TODO:Delete TD task.
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800885 except Exception as e:
886 self.log.exception('failed-to-remove-flow', e=e)
887
888 def add_onu_flows(self, device, flows):
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500889 self.log.debug('add-onu-flows')
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800890
891 # no point in pushing omci flows if the device isnt reachable
892 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800893 device.admin_state != AdminState.ENABLED:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800894 self.log.warn("device-disabled-or-offline-skipping-flow-update",
895 admin=device.admin_state, connect=device.connect_status)
896 return
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800897
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800898 def is_downstream(port):
899 return port == self._pon_port_number
900
901 def is_upstream(port):
902 return not is_downstream(port)
903
904 for flow in flows:
905 # if incoming flow contains cookie, then add to ONU
906 if flow.cookie:
907 _type = None
908 _port = None
909 _vlan_vid = None
910 _udp_dst = None
911 _udp_src = None
912 _ipv4_dst = None
913 _ipv4_src = None
914 _metadata = None
915 _output = None
916 _push_tpid = None
917 _field = None
918 _set_vlan_vid = None
Mahir Gunyel45610b42020-03-16 17:29:01 -0700919 _set_vlan_pcp = None
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800920 _tunnel_id = None
Girish Gowdra6a73ad62020-06-11 13:40:16 -0700921 _proto = -1
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800922 self.log.debug("add-flow", device_id=device.id, flow=flow)
923
924 try:
925 _in_port = fd.get_in_port(flow)
926 assert _in_port is not None
927
928 _out_port = fd.get_out_port(flow) # may be None
929 tp_id = self.get_tp_id_in_flow(flow)
930 if is_downstream(_in_port):
931 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
932 # NOTE: We don't care downstream flow because we will copy vlan_id to upstream flow
933 # uni_port = self.uni_port(_out_port)
934 # uni_id = _out_port & 0xF
935 continue
936 elif is_upstream(_in_port):
937 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
938 uni_port = self.uni_port(_in_port)
939 uni_id = _in_port & 0xF
940 else:
941 raise Exception('port should be 1 or 2 by our convention')
942
943 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
944
945 for field in fd.get_ofb_fields(flow):
946 if field.type == fd.ETH_TYPE:
947 _type = field.eth_type
948 self.log.debug('field-type-eth-type',
949 eth_type=_type)
950
951 elif field.type == fd.IP_PROTO:
952 _proto = field.ip_proto
Girish Gowdra6a73ad62020-06-11 13:40:16 -0700953 if _proto == 2:
954 # Workaround for TT workflow - avoids installing invalid EVTO rule
955 self.log.debug("igmp-trap-flow")
956 break
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800957 self.log.debug('field-type-ip-proto',
958 ip_proto=_proto)
959
960 elif field.type == fd.IN_PORT:
961 _port = field.port
962 self.log.debug('field-type-in-port',
963 in_port=_port)
964 elif field.type == fd.TUNNEL_ID:
965 self.log.debug('field-type-tunnel-id')
966
967 elif field.type == fd.VLAN_VID:
Andrea Campanellacf916ea2020-02-14 10:03:58 +0100968 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
969 _vlan_vid = RESERVED_TRANSPARENT_VLAN
970 else:
971 _vlan_vid = field.vlan_vid & 0xfff
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800972 self.log.debug('field-type-vlan-vid',
973 vlan=_vlan_vid)
974
975 elif field.type == fd.VLAN_PCP:
976 _vlan_pcp = field.vlan_pcp
977 self.log.debug('field-type-vlan-pcp',
978 pcp=_vlan_pcp)
979
980 elif field.type == fd.UDP_DST:
981 _udp_dst = field.udp_dst
982 self.log.debug('field-type-udp-dst',
983 udp_dst=_udp_dst)
984
985 elif field.type == fd.UDP_SRC:
986 _udp_src = field.udp_src
987 self.log.debug('field-type-udp-src',
988 udp_src=_udp_src)
989
990 elif field.type == fd.IPV4_DST:
991 _ipv4_dst = field.ipv4_dst
992 self.log.debug('field-type-ipv4-dst',
993 ipv4_dst=_ipv4_dst)
994
995 elif field.type == fd.IPV4_SRC:
996 _ipv4_src = field.ipv4_src
997 self.log.debug('field-type-ipv4-src',
998 ipv4_dst=_ipv4_src)
999
1000 elif field.type == fd.METADATA:
1001 _metadata = field.table_metadata
1002 self.log.debug('field-type-metadata',
1003 metadata=_metadata)
1004
1005 else:
1006 raise NotImplementedError('field.type={}'.format(
1007 field.type))
1008
Girish Gowdra6a73ad62020-06-11 13:40:16 -07001009 if _proto == 2:
1010 # Workaround for TT workflow - avoids installing invalid EVTO rule
1011 self.log.warn("skipping-igmp-trap-flow")
1012 continue
1013
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001014 for action in fd.get_actions(flow):
1015
1016 if action.type == fd.OUTPUT:
1017 _output = action.output.port
1018 self.log.debug('action-type-output',
1019 output=_output, in_port=_in_port)
1020
1021 elif action.type == fd.POP_VLAN:
1022 self.log.debug('action-type-pop-vlan',
1023 in_port=_in_port)
1024
1025 elif action.type == fd.PUSH_VLAN:
1026 _push_tpid = action.push.ethertype
1027 self.log.debug('action-type-push-vlan',
1028 push_tpid=_push_tpid, in_port=_in_port)
1029 if action.push.ethertype != 0x8100:
1030 self.log.error('unhandled-tpid',
1031 ethertype=action.push.ethertype)
1032
1033 elif action.type == fd.SET_FIELD:
1034 _field = action.set_field.field.ofb_field
1035 assert (action.set_field.field.oxm_class ==
1036 OFPXMC_OPENFLOW_BASIC)
1037 self.log.debug('action-type-set-field',
1038 field=_field, in_port=_in_port)
1039 if _field.type == fd.VLAN_VID:
1040 _set_vlan_vid = _field.vlan_vid & 0xfff
1041 self.log.debug('set-field-type-vlan-vid',
1042 vlan_vid=_set_vlan_vid)
1043 elif _field.type == fd.VLAN_PCP:
1044 _set_vlan_pcp = _field.vlan_pcp
1045 self.log.debug('set-field-type-vlan-pcp',
1046 vlan_pcp=_set_vlan_pcp)
1047 else:
1048 self.log.error('unsupported-action-set-field-type',
1049 field_type=_field.type)
1050 else:
1051 self.log.error('unsupported-action-type',
1052 action_type=action.type, in_port=_in_port)
1053
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001054 if self._set_vlan is not None:
1055 if uni_id not in self._set_vlan:
1056 self._set_vlan[uni_id] = dict()
1057 self._set_vlan[uni_id][tp_id] = _set_vlan_vid
1058 self.log.debug("set_vlan_id-for-tp", _set_vlan_vid=_set_vlan_vid, tp_id=tp_id)
1059
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001060 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
1061 # supported match and action criteria cannot be handled by omci and must be ignored.
1062 if (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid != RESERVED_TRANSPARENT_VLAN:
1063 self.log.warn('ignoring-flow-that-does-not-set-vlanid', set_vlan_vid=_set_vlan_vid)
1064 elif (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid == RESERVED_TRANSPARENT_VLAN:
1065 self.log.info('set-vlanid-any', uni_id=uni_id, uni_port=uni_port,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001066 _set_vlan_vid=_vlan_vid,
1067 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1068 tp_id=tp_id)
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001069 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1070 _set_vlan_vid=_vlan_vid,
1071 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1072 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001073 else:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001074 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, match_vlan=_vlan_vid,
1075 set_vlan_vid=_set_vlan_vid, _set_vlan_pcp=_set_vlan_pcp, ethType=_type)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001076 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1077 _set_vlan_vid=_set_vlan_vid,
1078 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1079 tp_id=tp_id)
1080
1081 except Exception as e:
1082 self.log.exception('failed-to-install-flow', e=e, flow=flow)
1083
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001084 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
1085 # flow decomposition that ultimately comes from onos
1086 def update_flow_table(self, device, flows):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001087 self.log.debug('update-flow-table', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001088
1089 #
1090 # We need to proxy through the OLT to get to the ONU
1091 # Configuration from here should be using OMCI
1092 #
1093 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
1094
1095 # no point in pushing omci flows if the device isnt reachable
1096 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdrae933cd32019-11-21 21:04:41 +05301097 device.admin_state != AdminState.ENABLED:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001098 self.log.warn("device-disabled-or-offline-skipping-flow-update",
1099 admin=device.admin_state, connect=device.connect_status)
1100 return
1101
1102 def is_downstream(port):
1103 return port == self._pon_port_number
1104
1105 def is_upstream(port):
1106 return not is_downstream(port)
1107
1108 for flow in flows:
1109 _type = None
1110 _port = None
1111 _vlan_vid = None
1112 _udp_dst = None
1113 _udp_src = None
1114 _ipv4_dst = None
1115 _ipv4_src = None
1116 _metadata = None
1117 _output = None
1118 _push_tpid = None
1119 _field = None
1120 _set_vlan_vid = None
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001121 _set_vlan_pcp = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -04001122 _tunnel_id = None
1123
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001124 try:
Girish Gowdraa73ee452019-12-20 18:52:17 +05301125 write_metadata = fd.get_write_metadata(flow)
1126 if write_metadata is None:
1127 self.log.error("do-not-process-flow-without-write-metadata")
1128 return
1129
1130 # extract tp id from flow
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001131 tp_id = self.get_tp_id_in_flow(flow)
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001132 self.log.debug("tp-id-in-flow", tp_id=tp_id)
Girish Gowdraa73ee452019-12-20 18:52:17 +05301133
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001134 _in_port = fd.get_in_port(flow)
1135 assert _in_port is not None
1136
1137 _out_port = fd.get_out_port(flow) # may be None
1138
1139 if is_downstream(_in_port):
1140 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
1141 uni_port = self.uni_port(_out_port)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301142 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001143 elif is_upstream(_in_port):
1144 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
1145 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001146 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001147 else:
1148 raise Exception('port should be 1 or 2 by our convention')
1149
1150 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
1151
1152 for field in fd.get_ofb_fields(flow):
1153 if field.type == fd.ETH_TYPE:
1154 _type = field.eth_type
1155 self.log.debug('field-type-eth-type',
1156 eth_type=_type)
1157
1158 elif field.type == fd.IP_PROTO:
1159 _proto = field.ip_proto
1160 self.log.debug('field-type-ip-proto',
1161 ip_proto=_proto)
1162
1163 elif field.type == fd.IN_PORT:
1164 _port = field.port
1165 self.log.debug('field-type-in-port',
1166 in_port=_port)
1167
1168 elif field.type == fd.VLAN_VID:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001169 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
1170 _vlan_vid = RESERVED_TRANSPARENT_VLAN
1171 else:
1172 _vlan_vid = field.vlan_vid & 0xfff
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001173 self.log.debug('field-type-vlan-vid',
1174 vlan=_vlan_vid)
1175
1176 elif field.type == fd.VLAN_PCP:
1177 _vlan_pcp = field.vlan_pcp
1178 self.log.debug('field-type-vlan-pcp',
1179 pcp=_vlan_pcp)
1180
1181 elif field.type == fd.UDP_DST:
1182 _udp_dst = field.udp_dst
1183 self.log.debug('field-type-udp-dst',
1184 udp_dst=_udp_dst)
1185
1186 elif field.type == fd.UDP_SRC:
1187 _udp_src = field.udp_src
1188 self.log.debug('field-type-udp-src',
1189 udp_src=_udp_src)
1190
1191 elif field.type == fd.IPV4_DST:
1192 _ipv4_dst = field.ipv4_dst
1193 self.log.debug('field-type-ipv4-dst',
1194 ipv4_dst=_ipv4_dst)
1195
1196 elif field.type == fd.IPV4_SRC:
1197 _ipv4_src = field.ipv4_src
1198 self.log.debug('field-type-ipv4-src',
1199 ipv4_dst=_ipv4_src)
1200
1201 elif field.type == fd.METADATA:
1202 _metadata = field.table_metadata
1203 self.log.debug('field-type-metadata',
1204 metadata=_metadata)
1205
Matt Jeanneretef06d0d2019-04-27 17:36:53 -04001206 elif field.type == fd.TUNNEL_ID:
1207 _tunnel_id = field.tunnel_id
1208 self.log.debug('field-type-tunnel-id',
1209 tunnel_id=_tunnel_id)
1210
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001211
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001212 else:
1213 raise NotImplementedError('field.type={}'.format(
1214 field.type))
1215
1216 for action in fd.get_actions(flow):
1217
1218 if action.type == fd.OUTPUT:
1219 _output = action.output.port
1220 self.log.debug('action-type-output',
1221 output=_output, in_port=_in_port)
1222
1223 elif action.type == fd.POP_VLAN:
1224 self.log.debug('action-type-pop-vlan',
1225 in_port=_in_port)
1226
1227 elif action.type == fd.PUSH_VLAN:
1228 _push_tpid = action.push.ethertype
1229 self.log.debug('action-type-push-vlan',
1230 push_tpid=_push_tpid, in_port=_in_port)
1231 if action.push.ethertype != 0x8100:
1232 self.log.error('unhandled-tpid',
1233 ethertype=action.push.ethertype)
1234
1235 elif action.type == fd.SET_FIELD:
1236 _field = action.set_field.field.ofb_field
1237 assert (action.set_field.field.oxm_class ==
1238 OFPXMC_OPENFLOW_BASIC)
1239 self.log.debug('action-type-set-field',
1240 field=_field, in_port=_in_port)
1241 if _field.type == fd.VLAN_VID:
1242 _set_vlan_vid = _field.vlan_vid & 0xfff
1243 self.log.debug('set-field-type-vlan-vid',
1244 vlan_vid=_set_vlan_vid)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001245 elif _field.type == fd.VLAN_PCP:
1246 _set_vlan_pcp = _field.vlan_pcp
1247 self.log.debug('set-field-type-vlan-pcp',
1248 vlan_pcp=_set_vlan_pcp)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001249 else:
1250 self.log.error('unsupported-action-set-field-type',
1251 field_type=_field.type)
1252 else:
1253 self.log.error('unsupported-action-type',
1254 action_type=action.type, in_port=_in_port)
1255
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001256 if self._set_vlan is not None:
1257 if uni_id not in self._set_vlan:
1258 self._set_vlan[uni_id] = dict()
1259 self._set_vlan[uni_id][tp_id] = _set_vlan_vid
1260 self.log.debug("set_vlan_id-for-tp", _set_vlan_vid=_set_vlan_vid, tp_id=tp_id)
Matt Jeanneret810148b2019-09-29 12:44:01 -04001261 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
1262 # supported match and action criteria cannot be handled by omci and must be ignored.
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001263 if (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid != RESERVED_TRANSPARENT_VLAN:
1264 self.log.warn('ignoring-flow-that-does-not-set-vlanid', set_vlan_vid=_set_vlan_vid)
1265 elif (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid == RESERVED_TRANSPARENT_VLAN:
1266 self.log.info('set-vlanid-any', uni_id=uni_id, uni_port=uni_port,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001267 _set_vlan_vid=_vlan_vid,
1268 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1269 tp_id=tp_id)
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001270 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1271 _set_vlan_vid=_vlan_vid,
1272 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1273 tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001274 else:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001275 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, match_vlan=_vlan_vid,
1276 set_vlan_vid=_set_vlan_vid, _set_vlan_pcp=_set_vlan_pcp, ethType=_type)
1277 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1278 _set_vlan_vid=_set_vlan_vid,
1279 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1280 tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001281 except Exception as e:
1282 self.log.exception('failed-to-install-flow', e=e, flow=flow)
1283
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001284 def _add_vlan_filter_task(self, device, uni_id, uni_port=None, match_vlan=0,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001285 _set_vlan_vid=None, _set_vlan_pcp=8, tp_id=0):
Girish Gowdradc98d812020-03-20 13:04:58 -07001286 if uni_id in self._pending_delete_tp and tp_id in self._pending_delete_tp[uni_id] and \
1287 self._pending_delete_tp[uni_id][tp_id] is True:
1288 self.log.debug("pending-del-tp--scheduling-add-vlan-filter-task-for-later")
1289 reactor.callLater(0.2, self._add_vlan_filter_task, device, uni_id, uni_port, match_vlan,
1290 _set_vlan_vid, _set_vlan_pcp, tp_id)
1291 return
1292
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001293 self.log.info('_adding_vlan_filter_task', uni_port=uni_port, uni_id=uni_id, tp_id=tp_id, match_vlan=match_vlan,
1294 vlan=_set_vlan_vid, vlan_pcp=_set_vlan_pcp)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001295 assert uni_port is not None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001296 if uni_id in self._tech_profile_download_done and tp_id in self._tech_profile_download_done[uni_id] and \
1297 self._tech_profile_download_done[uni_id][tp_id] is True:
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001298 @inlineCallbacks
1299 def success(_results):
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001300 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid, tp_id=tp_id,
1301 set_vlan_pcp=_set_vlan_pcp)
Matt Jeanneretd84c9072020-01-31 06:33:27 -05001302 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-pushed')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001303
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001304 @inlineCallbacks
1305 def failure(_reason):
Girish Gowdraa73ee452019-12-20 18:52:17 +05301306 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid, tp_id=tp_id)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001307 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1308 reactor.callLater(retry,
1309 self._add_vlan_filter_task, device, uni_id, uni_port=uni_port,
1310 match_vlan=match_vlan, _set_vlan_vid=_set_vlan_vid,
1311 _set_vlan_pcp=_set_vlan_pcp, tp_id=tp_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -05001312 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-failed-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001313
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001314 self.log.info('setting-vlan-tag', uni_port=uni_port, uni_id=uni_id, tp_id=tp_id, match_vlan=match_vlan,
1315 vlan=_set_vlan_vid, vlan_pcp=_set_vlan_pcp)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001316 vlan_filter_add_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001317 match_vlan, _set_vlan_pcp, add_tag=True,
1318 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001319 self._deferred = self._onu_omci_device.task_runner.queue_task(vlan_filter_add_task)
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001320 self._deferred.addCallbacks(success, failure)
1321 else:
1322 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001323 uni_id=uni_id, tp_id=tp_id)
1324 if uni_id not in self._queued_vlan_filter_task:
1325 self._queued_vlan_filter_task[uni_id] = dict()
Mahir Gunyela982ec32020-02-25 12:30:37 -08001326 if tp_id not in self._queued_vlan_filter_task[uni_id]:
1327 self._queued_vlan_filter_task[uni_id][tp_id] = []
1328 self._queued_vlan_filter_task[uni_id][tp_id].append({"device": device,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001329 "uni_id": uni_id,
1330 "uni_port": uni_port,
1331 "match_vlan": match_vlan,
1332 "set_vlan_vid": _set_vlan_vid,
1333 "set_vlan_pcp": _set_vlan_pcp,
1334 "tp_id": tp_id})
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001335
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001336 def get_tp_id_in_flow(self, flow):
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001337 flow_metadata = fd.get_metadata_from_write_metadata(flow)
1338 tp_id = fd.get_tp_id_from_metadata(flow_metadata)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001339 return tp_id
1340
1341 def _remove_vlan_filter_task(self, device, uni_id, uni_port=None, match_vlan=0,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001342 _set_vlan_vid=None, _set_vlan_pcp=8, tp_id=0):
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001343 assert uni_port is not None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001344
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001345 @inlineCallbacks
1346 def success(_results):
1347 self.log.info('vlan-untagging-success', _results=_results)
1348 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-deleted')
1349
1350 @inlineCallbacks
1351 def failure(_reason):
1352 self.log.warn('vlan-untagging-failure', _reason=_reason)
1353 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-deletion-failed-retrying')
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001354 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001355 reactor.callLater(retry,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001356 self._remove_vlan_filter_task, device, uni_id,
ozgecanetsiace4e37f2020-07-20 10:16:00 +03001357 uni_port=uni_port, match_vlan=match_vlan, _set_vlan_vid=_set_vlan_vid,
1358 _set_vlan_pcp=_set_vlan_pcp, tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001359
1360 self.log.info("remove_vlan_filter_task", tp_id=tp_id)
1361 vlan_remove_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001362 match_vlan, _set_vlan_pcp, add_tag=False,
1363 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001364 self._deferred = self._onu_omci_device.task_runner.queue_task(vlan_remove_task)
1365 self._deferred.addCallbacks(success, failure)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001366
Matt Jeanneret5e331892019-12-07 21:31:45 -05001367 @inlineCallbacks
Matt Jeannereta32441c2019-03-07 05:16:37 -05001368 def process_inter_adapter_message(self, request):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001369 self.log.debug('process-inter-adapter-message', type=request.header.type, from_topic=request.header.from_topic,
1370 to_topic=request.header.to_topic, to_device_id=request.header.to_device_id)
Matt Jeanneret2101f3d2020-03-12 10:13:06 -04001371
1372 if not self.enabled:
1373 self.log.warn('device-not-activated')
1374 reactor.callLater(0.5, self.process_inter_adapter_message, request)
1375 return
1376
Matt Jeannereta32441c2019-03-07 05:16:37 -05001377 try:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001378
1379 update_onu_state = False
1380
Matt Jeannereta32441c2019-03-07 05:16:37 -05001381 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
1382 omci_msg = InterAdapterOmciMessage()
1383 request.body.Unpack(omci_msg)
Matteo Scandolod8d73172019-11-26 12:15:15 -07001384 self.log.debug('inter-adapter-recv-omci', omci_msg=hexify(omci_msg.message))
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001385
Matt Jeannereta32441c2019-03-07 05:16:37 -05001386 self.receive_message(omci_msg.message)
1387
1388 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
1389 onu_indication = OnuIndication()
1390 request.body.Unpack(onu_indication)
Matteo Scandolod8d73172019-11-26 12:15:15 -07001391 self.log.debug('inter-adapter-recv-onu-ind', onu_id=onu_indication.onu_id,
1392 oper_state=onu_indication.oper_state, admin_state=onu_indication.admin_state,
1393 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001394
Matt Jeanneret5e331892019-12-07 21:31:45 -05001395 update_onu_state = True
1396 self._onu_persisted_state['onu_id'] = onu_indication.onu_id
1397 self._onu_persisted_state['intf_id'] = onu_indication.intf_id
1398 self._onu_persisted_state['admin_state'] = onu_indication.admin_state
Mahir Gunyel45610b42020-03-16 17:29:01 -07001399 self._onu_persisted_state['oper_state'] = onu_indication.oper_state
Matt Jeanneret5e331892019-12-07 21:31:45 -05001400
Matt Jeannereta32441c2019-03-07 05:16:37 -05001401 if onu_indication.oper_state == "up":
Matt Jeanneret5e331892019-12-07 21:31:45 -05001402 yield self.create_interface(onu_indication)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301403 elif onu_indication.oper_state == "down" or onu_indication.oper_state == "unreachable":
Matt Jeanneret5e331892019-12-07 21:31:45 -05001404 yield self.update_interface(onu_indication)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001405 else:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001406 self.log.error("unknown-onu-indication", onu_id=onu_indication.onu_id,
1407 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001408
Matt Jeanneret3bfebff2019-04-12 18:25:03 -04001409 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
1410 tech_msg = InterAdapterTechProfileDownloadMessage()
1411 request.body.Unpack(tech_msg)
1412 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
1413
Matt Jeanneret5e331892019-12-07 21:31:45 -05001414 update_onu_state = self._update_onu_persisted_state(tech_msg.uni_id, tp_path=tech_msg.path)
1415 yield self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
Matt Jeanneret3bfebff2019-04-12 18:25:03 -04001416
Girish Gowdrae933cd32019-11-21 21:04:41 +05301417 elif request.header.type == InterAdapterMessageType.DELETE_GEM_PORT_REQUEST:
1418 del_gem_msg = InterAdapterDeleteGemPortMessage()
1419 request.body.Unpack(del_gem_msg)
1420 self.log.debug('inter-adapter-recv-del-gem', gem_del_msg=del_gem_msg)
1421
1422 self.delete_tech_profile(uni_id=del_gem_msg.uni_id,
1423 gem_port_id=del_gem_msg.gem_port_id,
1424 tp_path=del_gem_msg.tp_path)
1425
1426 elif request.header.type == InterAdapterMessageType.DELETE_TCONT_REQUEST:
1427 del_tcont_msg = InterAdapterDeleteTcontMessage()
1428 request.body.Unpack(del_tcont_msg)
1429 self.log.debug('inter-adapter-recv-del-tcont', del_tcont_msg=del_tcont_msg)
1430
Matt Jeanneret5e331892019-12-07 21:31:45 -05001431 # Removal of the tcont/alloc id mapping represents the removal of the tech profile
1432 update_onu_state = self._update_onu_persisted_state(del_tcont_msg.uni_id, tp_path=None)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301433 self.delete_tech_profile(uni_id=del_tcont_msg.uni_id,
1434 alloc_id=del_tcont_msg.alloc_id,
1435 tp_path=del_tcont_msg.tp_path)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001436 else:
1437 self.log.error("inter-adapter-unhandled-type", request=request)
1438
Matt Jeanneret5e331892019-12-07 21:31:45 -05001439 if update_onu_state:
1440 try:
1441 self.log.debug('updating-onu-state', device_id=self.device_id,
1442 onu_persisted_state=self._onu_persisted_state)
1443 yield self.onu_kv_client.set(self.device_id, json.dumps(self._onu_persisted_state))
1444 except Exception as e:
1445 self.log.error('could-not-store-onu-state', device_id=self.device_id,
1446 onu_persisted_state=self._onu_persisted_state, e=e)
1447 # at this point omci is started and/or indications being processed
1448 # later indications may have a chance to write this state out again
1449
Matt Jeannereta32441c2019-03-07 05:16:37 -05001450 except Exception as e:
1451 self.log.exception("error-processing-inter-adapter-message", e=e)
1452
Matt Jeanneret5e331892019-12-07 21:31:45 -05001453 def _update_onu_persisted_state(self, uni_id, tp_path):
1454 # persist the uni and tech profile path for later reconciliation. update only if changed
1455 update_onu_state = False
1456 found = False
1457 for entry in self._onu_persisted_state.get('uni_config', list()):
1458 if entry.get('uni_id') == uni_id:
1459 found = True
1460 if entry.get('tp_path') != tp_path:
1461 update_onu_state = True
1462 entry['tp_path'] = tp_path
1463
1464 if not found:
1465 update_onu_state = True
1466 uni_tp = {
1467 'uni_id': uni_id,
1468 'tp_path': tp_path
1469 }
1470 self._onu_persisted_state['uni_config'].append(uni_tp)
1471
1472 return update_onu_state
1473
Matt Jeannereta32441c2019-03-07 05:16:37 -05001474 # Called each time there is an onu "up" indication from the olt handler
1475 @inlineCallbacks
1476 def create_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001477 self.log.info('create-interface', onu_id=onu_indication.onu_id,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001478 serial_number=onu_indication.serial_number)
Amit Ghosh028eb202020-02-17 13:34:00 +00001479
1480 # Ignore if onu_indication is received for an already running ONU
1481 if self._onu_omci_device is not None and self._onu_omci_device.active:
1482 self.log.warn('received-onu-indication-for-active-onu', onu_indication=onu_indication)
1483 return
1484
Matt Jeanneretc083f462019-03-11 15:02:01 -04001485 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
1486 connect_status=ConnectStatus.REACHABLE)
1487
Matt Jeannereta32441c2019-03-07 05:16:37 -05001488 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001489
1490 self.log.debug('starting-openomci-statemachine')
1491 self._subscribe_to_events()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001492 onu_device.reason = "starting-openomci"
Girish Gowdrae933cd32019-11-21 21:04:41 +05301493 reactor.callLater(1, self._onu_omci_device.start, onu_device)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001494 yield self.core_proxy.device_reason_update(self.device_id, onu_device.reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001495 self._heartbeat.enabled = True
1496
Matt Jeanneret42dad792020-02-01 09:28:27 -05001497 # Called each time there is an onu "down" indication from the olt handler
Matt Jeannereta32441c2019-03-07 05:16:37 -05001498 @inlineCallbacks
1499 def update_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001500 self.log.info('update-interface', onu_id=onu_indication.onu_id,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001501 serial_number=onu_indication.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001502
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -04001503 if onu_indication.oper_state == 'down' or onu_indication.oper_state == "unreachable":
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001504 self.log.debug('stopping-openomci-statemachine', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001505 reactor.callLater(0, self._onu_omci_device.stop)
1506
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001507 self._tp = dict()
1508
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001509 # Let TP download happen again
1510 for uni_id in self._tp_service_specific_task:
1511 self._tp_service_specific_task[uni_id].clear()
1512 for uni_id in self._tech_profile_download_done:
1513 self._tech_profile_download_done[uni_id].clear()
1514
Matt Jeanneretf4113222019-08-14 19:44:34 -04001515 yield self.disable_ports(lock_ports=False)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001516 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
1517 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.DISCOVERED,
1518 connect_status=ConnectStatus.UNREACHABLE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001519 else:
1520 self.log.debug('not-changing-openomci-statemachine')
1521
Matt Jeanneretf4113222019-08-14 19:44:34 -04001522 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001523 def disable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001524 self.log.info('disable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001525 try:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001526 yield self.disable_ports(lock_ports=True, device_disabled=True)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001527 yield self.core_proxy.device_reason_update(self.device_id, "omci-admin-lock")
1528 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001529 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001530 self.log.exception('exception-in-onu-disable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001531
William Kurkian3a206332019-04-29 11:05:47 -04001532 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001533 def reenable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001534 self.log.info('reenable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001535 try:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001536 yield self.core_proxy.device_state_update(device.id,
1537 oper_status=OperStatus.ACTIVE,
1538 connect_status=ConnectStatus.REACHABLE)
1539 yield self.core_proxy.device_reason_update(self.device_id, 'onu-reenabled')
1540 yield self.enable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001541 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001542 self.log.exception('exception-in-onu-reenable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001543
William Kurkian3a206332019-04-29 11:05:47 -04001544 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001545 def reboot(self):
1546 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -04001547 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001548 if device.connect_status != ConnectStatus.REACHABLE:
1549 self.log.error("device-unreachable")
1550 return
1551
William Kurkian3a206332019-04-29 11:05:47 -04001552 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001553 def success(_results):
1554 self.log.info('reboot-success', _results=_results)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001555 yield self.core_proxy.device_reason_update(self.device_id, 'rebooting')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001556
1557 def failure(_reason):
1558 self.log.info('reboot-failure', _reason=_reason)
1559
1560 self._deferred = self._onu_omci_device.reboot()
1561 self._deferred.addCallbacks(success, failure)
1562
William Kurkian3a206332019-04-29 11:05:47 -04001563 @inlineCallbacks
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001564 def disable_ports(self, lock_ports=True, device_disabled=False):
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001565 self.log.info('disable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001566
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001567 # TODO: for now only support the first UNI given no requirement for multiple uni yet. Also needed to reduce flow
1568 # load on the core
Matt Jeanneretf4113222019-08-14 19:44:34 -04001569 for port in self.uni_ports:
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001570 if port.mac_bridge_port_num == 1:
1571 port.operstatus = OperStatus.UNKNOWN
1572 self.log.info('disable-port', device_id=self.device_id, port=port)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001573 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number,
1574 port.operstatus)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001575
1576 if lock_ports is True:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001577 self.lock_ports(lock=True, device_disabled=device_disabled)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001578
William Kurkian3a206332019-04-29 11:05:47 -04001579 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001580 def enable_ports(self):
1581 self.log.info('enable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001582
Matt Jeanneretf4113222019-08-14 19:44:34 -04001583 self.lock_ports(lock=False)
1584
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001585 # TODO: for now only support the first UNI given no requirement for multiple uni yet. Also needed to reduce flow
1586 # load on the core
1587 # Given by default all unis are initially active according to omci alarming, we must mimic this.
Matt Jeanneretf4113222019-08-14 19:44:34 -04001588 for port in self.uni_ports:
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001589 if port.mac_bridge_port_num == 1:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001590 port.operstatus = OperStatus.ACTIVE
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001591 self.log.info('enable-port', device_id=self.device_id, port=port)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001592 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number,
1593 port.operstatus)
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001594
1595 # TODO: Normally we would want any uni ethernet link down or uni ethernet link up alarms to register in the core,
1596 # but practically olt provisioning cannot handle the churn of links up, down, then up again typical on startup.
1597 #
1598 # Basically the link state sequence:
1599 # 1) per omci default alarm state, all unis are initially up (no link down alarms received yet)
1600 # 2) a link state down alarm is received for all uni, given the lock command, and also because most unis have nothing plugged in
1601 # 3) a link state up alarm is received for the uni plugged in.
1602 #
1603 # Given the olt (BAL) has to provision all uni, de-provision all uni, and re-provision one uni in quick succession
1604 # and cannot (bug?), we have to skip this and leave uni ports as assumed active. Also all the link state activity
1605 # would have a ripple effect through the core to the controller as well. And is it really worth it?
1606 '''
Matt Jeanneretf4113222019-08-14 19:44:34 -04001607 @inlineCallbacks
1608 def port_state_handler(self, _topic, msg):
1609 self.log.info("port-state-change", _topic=_topic, msg=msg)
1610
1611 onu_id = msg['onu_id']
1612 port_no = msg['port_number']
1613 serial_number = msg['serial_number']
1614 port_status = msg['port_status']
1615 uni_port = self.uni_port(int(port_no))
1616
1617 self.log.debug("port-state-parsed-message", onu_id=onu_id, port_no=port_no, serial_number=serial_number,
1618 port_status=port_status)
1619
1620 if port_status is True:
1621 uni_port.operstatus = OperStatus.ACTIVE
1622 self.log.info('link-up', device_id=self.device_id, port=uni_port)
1623 else:
1624 uni_port.operstatus = OperStatus.UNKNOWN
1625 self.log.info('link-down', device_id=self.device_id, port=uni_port)
1626
1627 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, uni_port.port_number, uni_port.operstatus)
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001628 '''
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001629
1630 # Called just before openomci state machine is started. These listen for events from selected state machines,
1631 # most importantly, mib in sync. Which ultimately leads to downloading the mib
1632 def _subscribe_to_events(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001633 self.log.debug('subscribe-to-events')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001634
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001635 bus = self._onu_omci_device.event_bus
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001636
1637 # OMCI MIB Database sync status
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001638 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1639 OnuDeviceEvents.MibDatabaseSyncEvent)
1640 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
1641
1642 # OMCI Capabilities
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001643 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1644 OnuDeviceEvents.OmciCapabilitiesEvent)
1645 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
1646
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001647 # TODO: these alarms seem to be unreliable depending on the environment
1648 # Listen for UNI link state alarms and set the oper_state based on that rather than assuming all UNI are up
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001649 # topic = OnuDeviceEntry.event_bus_topic(self.device_id,
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001650 # OnuDeviceEvents.PortEvent)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001651 # self._port_state_subscription = bus.subscribe(topic, self.port_state_handler)
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001652
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001653 # Called when the mib is in sync
1654 def in_sync_handler(self, _topic, msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001655 self.log.debug('in-sync-handler', _topic=_topic, msg=msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001656 if self._in_sync_subscription is not None:
1657 try:
1658 in_sync = msg[IN_SYNC_KEY]
1659
1660 if in_sync:
1661 # Only call this once
1662 bus = self._onu_omci_device.event_bus
1663 bus.unsubscribe(self._in_sync_subscription)
1664 self._in_sync_subscription = None
1665
1666 # Start up device_info load
1667 self.log.debug('running-mib-sync')
1668 reactor.callLater(0, self._mib_in_sync)
1669
1670 except Exception as e:
1671 self.log.exception('in-sync', e=e)
1672
1673 def capabilties_handler(self, _topic, _msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001674 self.log.debug('capabilities-handler', _topic=_topic, msg=_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001675 if self._capabilities_subscription is not None:
1676 self.log.debug('capabilities-handler-done')
1677
1678 # Mib is in sync, we can now query what we learned and actually start pushing ME (download) to the ONU.
Matt Jeanneretc083f462019-03-11 15:02:01 -04001679 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001680 def _mib_in_sync(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001681 self.log.debug('mib-in-sync')
Matt Jeanneretc083f462019-03-11 15:02:01 -04001682 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001683
Matt Jeanneret5e331892019-12-07 21:31:45 -05001684 # only notify core if this is a new device. otherwise do not have reconcile generating
1685 # a lot of needless message churn
1686 if not self._reconciling:
1687 yield self.core_proxy.device_reason_update(self.device_id, 'discovery-mibsync-complete')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001688
1689 if self._dev_info_loaded:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001690 self.log.debug('device-info-already-loaded')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001691 else:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001692 # new onu or adapter was restarted. fill up our local data
1693 yield self._load_device_data(device)
1694
1695 if self._check_mib_downloaded():
1696 self.log.debug('mib-already-downloaded')
1697 if not self._reconciling:
1698 yield self.core_proxy.device_state_update(device.id,
1699 oper_status=OperStatus.ACTIVE,
1700 connect_status=ConnectStatus.REACHABLE)
1701 yield self.enable_ports()
1702 else:
1703 self._download_mib(device)
1704
1705 if self._reconciling:
1706 yield self._restore_tech_profile()
1707 self._start_monitoring()
1708 self._reconciling = False
1709 self.log.debug('reconcile-finished')
1710
1711 def _download_mib(self, device):
1712 self.log.debug('downloading-initial-mib-configuration')
1713
1714 @inlineCallbacks
1715 def success(_results):
1716 self.log.debug('mib-download-success', _results=_results)
1717 yield self.core_proxy.device_state_update(device.id,
1718 oper_status=OperStatus.ACTIVE,
1719 connect_status=ConnectStatus.REACHABLE)
1720 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-downloaded')
1721 self._mib_download_task = None
1722 yield self.enable_ports()
1723 yield self.onu_active_event()
1724 self._start_monitoring()
1725
1726 @inlineCallbacks
1727 def failure(_reason):
1728 self.log.warn('mib-download-failure-retrying', _reason=_reason)
1729 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1730 reactor.callLater(retry, self._mib_in_sync)
1731 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-download-failure-retrying')
1732
1733 # start by locking all the unis till mib sync and initial mib is downloaded
1734 # this way we can capture the port down/up events when we are ready
1735 self.lock_ports(lock=True)
1736
1737 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1738 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1739 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1740 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1741 self._deferred.addCallbacks(success, failure)
1742
1743 def _start_monitoring(self):
1744 self.log.debug('starting-monitoring')
1745
1746 # Start collecting stats from the device after a brief pause
1747 if not self._pm_metrics_started:
1748 self._pm_metrics_started = True
1749 pmstart = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1750 reactor.callLater(pmstart, self._pm_metrics.start_collector)
1751
1752 # Start test requests after a brief pause
1753 if not self._test_request_started:
1754 self._test_request_started = True
1755 tststart = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1756 reactor.callLater(tststart, self._test_request.start_collector)
1757
1758 def _check_mib_downloaded(self):
1759 self.log.debug('checking-mib-downloaded')
1760 results = False
1761
1762 mac_bridges = self.onu_omci_device.query_mib(MacBridgeServiceProfile.class_id)
1763 self.log.debug('mac-bridges', mac_bridges=mac_bridges)
1764
1765 for k, v in mac_bridges.items():
1766 if not isinstance(v, dict):
1767 continue
1768 # found at least one mac bridge, good enough to say its done, break out
1769 self.log.debug('found-mac-bridge-mib-download-has-been-done', omci_key=k, omci_value=v)
1770 results = True
1771 break
1772
1773 return results
1774
1775 @inlineCallbacks
1776 def _load_device_data(self, device):
1777 self.log.debug('loading-device-data-from-mib', device_id=device.id)
1778
1779 omci_dev = self._onu_omci_device
1780 config = omci_dev.configuration
1781
1782 try:
1783 # sort the lists so we get consistent port ordering.
1784 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
1785 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
1786 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
1787 veip_list = sorted(config.veip_entities) if config.veip_entities else []
1788
1789 if ani_list is None or (pptp_list is None and veip_list is None):
1790 yield self.core_proxy.device_reason_update(self.device_id, 'onu-missing-required-elements')
1791 raise Exception("onu-missing-required-elements")
1792
1793 # Currently logging the ani, pptp, veip, and uni for information purposes.
1794 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
1795 # And in some ONU the UNI-G list is incomplete or incorrect...
1796 for entity_id in ani_list:
1797 ani_value = config.ani_g_entities[entity_id]
1798 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
1799
1800 for entity_id in uni_list:
1801 uni_value = config.uni_g_entities[entity_id]
1802 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
1803
1804 uni_entities = OrderedDict()
1805 for entity_id in pptp_list:
1806 pptp_value = config.pptp_entities[entity_id]
1807 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
1808 uni_entities[entity_id] = UniType.PPTP
1809
1810 for entity_id in veip_list:
1811 veip_value = config.veip_entities[entity_id]
1812 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
1813 uni_entities[entity_id] = UniType.VEIP
1814
1815 uni_id = 0
1816 for entity_id, uni_type in uni_entities.items():
1817 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
1818 uni_id += 1
1819
1820 if self._unis:
1821 self._dev_info_loaded = True
1822 else:
1823 yield self.core_proxy.device_reason_update(self.device_id, 'no-usable-unis')
1824 raise Exception("no-usable-unis")
1825
1826 except Exception as e:
1827 self.log.exception('device-info-load', e=e)
1828 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001829
Matt Jeanneretc083f462019-03-11 15:02:01 -04001830 @inlineCallbacks
1831 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matt Jeanneret5e331892019-12-07 21:31:45 -05001832 self.log.debug('add-uni-port', entity_id=entity_id, uni_id=uni_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001833
Matt Jeanneret5e331892019-12-07 21:31:45 -05001834 intf_id = self._onu_persisted_state.get('intf_id')
1835 onu_id = self._onu_persisted_state.get('onu_id')
1836 uni_no = self.mk_uni_port_num(intf_id, onu_id, uni_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001837
1838 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1839 uni_name = "uni-{}".format(uni_no)
1840
Girish Gowdrae933cd32019-11-21 21:04:41 +05301841 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001842
1843 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 -04001844 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001845
Girish Gowdra5b499342020-06-16 14:45:51 -07001846 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name,
1847 device.parent_port_no, device.serial_number,
1848 uni_type,)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001849 uni_port.entity_id = entity_id
1850 uni_port.enabled = True
1851 uni_port.mac_bridge_port_num = mac_bridge_port_num
1852
1853 self.log.debug("created-uni-port", uni=uni_port)
1854
Matt Jeanneret5e331892019-12-07 21:31:45 -05001855 if not self._reconciling:
1856 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001857
1858 self._unis[uni_port.port_number] = uni_port
1859
Matt Jeanneret5e331892019-12-07 21:31:45 -05001860 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=onu_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301861 uni_ports=self.uni_ports,
1862 serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001863
Matt Jeanneret5e331892019-12-07 21:31:45 -05001864 @inlineCallbacks
1865 def _restore_tech_profile(self):
1866 self.log.debug("reconcile-restoring-tech-profile-tcont-gem-config")
1867
1868 # for every uni that has tech profile config reload all its tcont/alloc_id and gem from the tp path
1869 for entry in self._onu_persisted_state.get('uni_config', list()):
1870 uni_id = entry.get('uni_id')
1871 tp_path = entry.get('tp_path')
1872 if tp_path:
1873 tpstored = yield self.tp_kv_client.get(tp_path)
1874 tpstring = tpstored.decode('ascii')
1875 tp = json.loads(tpstring)
1876
1877 self.log.debug("restoring-tp-instance", tp=tp)
1878
1879 # re-run tech profile config that stores gem and tconts in the self._pon object
1880 # this does not actually re-run the omci, just rebuilds our local data store
1881 self._do_tech_profile_configuration(uni_id, tp)
1882
1883 tp_id = self.extract_tp_id_from_path(tp_path)
1884
1885 # rebuild cache dicts so tp updates and deletes dont get KeyErrors
1886 if uni_id not in self._tp_service_specific_task:
1887 self._tp_service_specific_task[uni_id] = dict()
1888
1889 if uni_id not in self._tech_profile_download_done:
1890 self._tech_profile_download_done[uni_id] = dict()
1891
1892 if tp_id not in self._tech_profile_download_done[uni_id]:
1893 self._tech_profile_download_done[uni_id][tp_id] = True
1894 else:
1895 self.log.debug("no-assigned-tp-instance", uni_id=uni_id)
1896
1897 # for every loaded tcont from tp check the mib database for its entity_id
1898 # needed for later tp deletes/adds
1899 tcont_idents = self.onu_omci_device.query_mib(Tcont.class_id)
1900 self.log.debug('tcont-idents', tcont_idents=tcont_idents)
1901
1902 for k, v in tcont_idents.items():
1903 if not isinstance(v, dict):
1904 continue
1905 alloc_check = v.get('attributes', {}).get('alloc_id', 0)
1906 tcont = self._pon.tconts.get(alloc_check)
1907 if tcont:
1908 tcont.entity_id = k
1909 self.log.debug('reassigning-tcont-entity-id', entity_id=tcont.entity_id,
1910 alloc_id=tcont.alloc_id)
1911
Matt Jeanneretc083f462019-03-11 15:02:01 -04001912 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1913 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
Amit Ghosh65400f12019-11-21 12:04:12 +00001914 MAX_PONS_PER_OLT = 256
1915 MAX_ONUS_PER_PON = 256
Matt Jeanneretc083f462019-03-11 15:02:01 -04001916 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001917
Matt Jeanneretc083f462019-03-11 15:02:01 -04001918 assert intf_id < MAX_PONS_PER_OLT
1919 assert onu_id < MAX_ONUS_PER_PON
1920 assert uni_id < MAX_UNIS_PER_ONU
Amit Ghosh65400f12019-11-21 12:04:12 +00001921 return intf_id << 12 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001922
1923 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001924 def onu_active_event(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001925 self.log.debug('onu-active-event')
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001926 try:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001927 # TODO: this is expensive for just getting the olt serial number. replace with direct api call
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001928 parent_device = yield self.core_proxy.get_device(self.parent_id)
1929 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001930 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001931
Matt Jeanneret5e331892019-12-07 21:31:45 -05001932 intf_id = self._onu_persisted_state.get('intf_id')
1933 onu_id = self._onu_persisted_state.get('onu_id')
1934 onu_serial = self._onu_persisted_state.get('serial_number')
1935
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001936 self.log.debug("onu-indication-context-data",
Matt Jeanneret5e331892019-12-07 21:31:45 -05001937 pon_id=intf_id,
1938 onu_id=onu_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301939 registration_id=self.device_id,
1940 device_id=self.device_id,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001941 onu_serial_number=onu_serial,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301942 olt_serial_number=olt_serial_number,
1943 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001944
Devmalya Paulffc89df2019-07-31 17:43:13 -04001945 self.log.debug("Trying-to-raise-onu-active-event")
1946 OnuActiveEvent(self.events, self.device_id,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001947 intf_id,
1948 onu_serial,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001949 str(self.device_id),
Girish Gowdrae933cd32019-11-21 21:04:41 +05301950 olt_serial_number, raised_ts,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001951 onu_id=onu_id).send(True)
Devmalya Paulffc89df2019-07-31 17:43:13 -04001952 except Exception as active_event_error:
1953 self.log.exception('onu-activated-event-error',
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001954 errmsg=active_event_error)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001955
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001956 @inlineCallbacks
1957 def onu_disabled_event(self):
1958 self.log.debug('onu-disabled-event')
1959 try:
1960 device = yield self.core_proxy.get_device(self.device_id)
1961 parent_device = yield self.core_proxy.get_device(self.parent_id)
1962 olt_serial_number = parent_device.serial_number
1963 raised_ts = arrow.utcnow().timestamp
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001964 intf_id = self._onu_persisted_state.get('intf_id')
1965 onu_id = self._onu_persisted_state.get('onu_id')
1966 onu_serial = self._onu_persisted_state.get('serial_number')
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001967
1968 self.log.debug("onu-indication-context-data",
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001969 pon_id=intf_id,
1970 onu_id=onu_id,
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001971 registration_id=self.device_id,
1972 device_id=self.device_id,
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001973 onu_serial_number=onu_serial,
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001974 olt_serial_number=olt_serial_number,
1975 raised_ts=raised_ts)
1976
1977 self.log.debug("Trying-to-raise-onu-disabled-event")
1978 OnuDisabledEvent(self.events, self.device_id,
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001979 intf_id,
Girish Gowdradc98d812020-03-20 13:04:58 -07001980 device.serial_number,
1981 str(self.device_id),
1982 olt_serial_number, raised_ts,
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001983 onu_id=onu_id).send(True)
1984 except Exception as disable_event_error:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001985 self.log.exception('onu-disabled-event-error',
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001986 errmsg=disable_event_error)
1987
1988 @inlineCallbacks
1989 def onu_deleted_event(self):
1990 self.log.debug('onu-deleted-event')
1991 try:
1992 device = yield self.core_proxy.get_device(self.device_id)
1993 parent_device = yield self.core_proxy.get_device(self.parent_id)
1994 olt_serial_number = parent_device.serial_number
1995 raised_ts = arrow.utcnow().timestamp
1996 intf_id = self._onu_persisted_state.get('intf_id')
1997 onu_id = self._onu_persisted_state.get('onu_id')
1998 serial_number = self._onu_persisted_state.get('serial_number')
1999
2000 self.log.debug("onu-deleted-event-context-data",
2001 pon_id=intf_id,
2002 onu_id=onu_id,
2003 registration_id=self.device_id,
2004 device_id=self.device_id,
2005 onu_serial_number=serial_number,
2006 olt_serial_number=olt_serial_number,
2007 raised_ts=raised_ts)
2008
2009 OnuDeletedEvent(self.events, self.device_id,
2010 intf_id,
2011 serial_number,
2012 str(self.device_id),
2013 olt_serial_number, raised_ts,
2014 onu_id=onu_id).send(True)
2015 except Exception as deleted_event_error:
2016 self.log.exception('onu-deleted-event-error',
2017 errmsg=deleted_event_error)
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04002018
2019 def lock_ports(self, lock=True, device_disabled=False):
Matt Jeanneretf4113222019-08-14 19:44:34 -04002020
2021 def success(response):
2022 self.log.debug('set-onu-ports-state', lock=lock, response=response)
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04002023 if device_disabled:
2024 self.onu_disabled_event()
Matt Jeanneretf4113222019-08-14 19:44:34 -04002025
2026 def failure(response):
2027 self.log.error('cannot-set-onu-ports-state', lock=lock, response=response)
2028
2029 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=lock)
2030 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
2031 self._deferred.addCallbacks(success, failure)
Mahir Gunyele9110a32020-02-20 14:56:50 -08002032
2033 def extract_tp_id_from_path(self, tp_path):
2034 # tp_path is of the format <technology>/<table_id>/<uni_port_name>
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08002035 tp_id = int(tp_path.split(_PATH_SEPERATOR)[1])
2036 return tp_id
onkarkundargia1e2af22020-01-27 11:51:43 +05302037
2038 def start_omci_test_action(self, device, uuid):
2039 """
2040
2041 :param device:
2042 :return:
2043 """
2044 # Code to Run OMCI Test Action
2045 self.log.info('Omci-test-action-request-On', request=device.id)
2046 kwargs_omci_test_action = {
2047 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
2048 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
2049 }
2050 serial_number = device.serial_number
2051 if device.connect_status != ConnectStatus.REACHABLE or device.admin_state != AdminState.ENABLED:
2052 return (TestResponse(result=TestResponse.FAILURE))
2053 test_request = OmciTestRequest(self.core_proxy,
2054 self.omci_agent, self.device_id, AniG,
2055 serial_number,
2056 self.logical_device_id, exclusive=False,
2057 uuid=uuid,
2058 **kwargs_omci_test_action)
2059 test_request.perform_test_omci()
2060 return (TestResponse(result=TestResponse.SUCCESS))