blob: 36cb2e152bcf9d3e9bcb6107c7eb3276fe6ca4b5 [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 Gowdrac5117452020-08-03 11:20:53 -070041from tp_state import TpState
Girish Gowdra5b499342020-06-16 14:45:51 -070042from pyvoltha.adapters.common.frameio.frameio import hexify
43from pyvoltha.adapters.common.kvstore.twisted_etcd_store import TwistedEtcdStore
44from pyvoltha.adapters.extensions.events.adapter_events import AdapterEvents
45from pyvoltha.adapters.extensions.events.device_events.onu.onu_active_event import OnuActiveEvent
46from pyvoltha.adapters.extensions.events.device_events.onu.onu_deleted_event import OnuDeletedEvent
47from pyvoltha.adapters.extensions.events.device_events.onu.onu_disabled_event import OnuDisabledEvent
48from pyvoltha.adapters.extensions.events.kpi.onu.onu_omci_pm import OnuOmciPmMetrics
49from pyvoltha.adapters.extensions.events.kpi.onu.onu_pm_metrics import OnuPmMetrics
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050050from pyvoltha.adapters.extensions.omci.omci_defs import EntityOperations, ReasonCodes
Girish Gowdra5b499342020-06-16 14:45:51 -070051from pyvoltha.adapters.extensions.omci.omci_entities import AniG, Tcont, MacBridgeServiceProfile
52from pyvoltha.adapters.extensions.omci.onu_device_entry import OnuDeviceEvents, \
53 OnuDeviceEntry, IN_SYNC_KEY
54from pyvoltha.adapters.extensions.omci.tasks.omci_test_request import OmciTestRequest
55from pyvoltha.common.tech_profile.tech_profile import TechProfile
56from pyvoltha.common.utils.registry import registry
57from twisted.internet import reactor
58from twisted.internet.defer import inlineCallbacks, returnValue
59from uni_port import RESERVED_TRANSPARENT_VLAN
60from uni_port import UniPort, UniType
61from voltha_protos.common_pb2 import OperStatus, ConnectStatus, AdminState
62from voltha_protos.device_pb2 import Port
63from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
64 InterAdapterOmciMessage, InterAdapterTechProfileDownloadMessage, InterAdapterDeleteGemPortMessage, \
65 InterAdapterDeleteTcontMessage
66from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC
67from voltha_protos.openolt_pb2 import OnuIndication
onkarkundargia1e2af22020-01-27 11:51:43 +053068from voltha_protos.voltha_pb2 import TestResponse
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050069
70OP = EntityOperations
71RC = ReasonCodes
72
Girish Gowdradc98d812020-03-20 13:04:58 -070073IS_MULTICAST = 'is_multicast'
Mahir Gunyel5de33fe2020-03-03 22:38:44 -080074GEM_PORT_ID = 'gemport_id'
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050075_STARTUP_RETRY_WAIT = 10
Mahir Gunyele9110a32020-02-20 14:56:50 -080076_PATH_SEPERATOR = "/"
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050077
78
79class BrcmOpenomciOnuHandler(object):
80
81 def __init__(self, adapter, device_id):
82 self.log = structlog.get_logger(device_id=device_id)
Matt Jeanneret08a8e862019-12-20 14:02:32 -050083 self.log.debug('starting-handler')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050084 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050085 self.core_proxy = adapter.core_proxy
86 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050087 self.parent_id = None
88 self.device_id = device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050089 self.proxy_address = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050090 self._enabled = False
Girish Gowdra322cca12020-08-09 15:55:54 -070091 self._is_device_active_and_reachable = False
Devmalya Paulffc89df2019-07-31 17:43:13 -040092 self.events = None
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050093 self._pm_metrics = None
94 self._pm_metrics_started = False
95 self._test_request = None
96 self._test_request_started = False
Girish Gowdradc98d812020-03-20 13:04:58 -070097 self._tp = dict() # tp_id -> technology profile definition in KV Store.
Matt Jeanneret5e331892019-12-07 21:31:45 -050098 self._reconciling = False
99
100 # Persisted onu configuration needed in case of reconciliation.
101 self._onu_persisted_state = {
102 'onu_id': None,
103 'intf_id': None,
104 'serial_number': None,
105 'admin_state': None,
106 'oper_state': None,
107 'uni_config': list()
108 }
109
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500110 self._unis = dict() # Port # -> UniPort
111
112 self._pon = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500113 self._pon_port_number = 100
114 self.logical_device_id = None
115
116 self._heartbeat = HeartBeat.create(self, device_id)
117
118 # Set up OpenOMCI environment
119 self._onu_omci_device = None
120 self._dev_info_loaded = False
121 self._deferred = None
122
123 self._in_sync_subscription = None
Matt Jeanneretf4113222019-08-14 19:44:34 -0400124 self._port_state_subscription = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500125 self._connectivity_subscription = None
126 self._capabilities_subscription = None
127
128 self.mac_bridge_service_profile_entity_id = 0x201
129 self.gal_enet_profile_entity_id = 0x1
130
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400131 # Stores information related to queued vlan filter tasks
132 # 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 -0400133 self._queued_vlan_filter_task = dict()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500134
Girish Gowdradc98d812020-03-20 13:04:58 -0700135 self._set_vlan = dict() # uni_id, tp_id -> set_vlan_id
Girish Gowdrac5117452020-08-03 11:20:53 -0700136 self._tp_state_map_per_uni = dict() # uni_id -> {dictionary tp_id->TpState}
Matt Jeanneret5e331892019-12-07 21:31:45 -0500137
138 # Paths from kv store
139 ONU_PATH = 'service/voltha/openonu'
140
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500141 # Initialize KV store client
142 self.args = registry('main').get_args()
Matt Jeanneret5e331892019-12-07 21:31:45 -0500143 host, port = self.args.etcd.split(':', 1)
144 self.tp_kv_client = TwistedEtcdStore(host, port, TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
145 self.onu_kv_client = TwistedEtcdStore(host, port, ONU_PATH)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500146
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500147 @property
148 def enabled(self):
149 return self._enabled
150
151 @enabled.setter
152 def enabled(self, value):
153 if self._enabled != value:
154 self._enabled = value
155
156 @property
157 def omci_agent(self):
158 return self.adapter.omci_agent
159
160 @property
161 def omci_cc(self):
162 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
163
164 @property
165 def heartbeat(self):
166 return self._heartbeat
167
168 @property
169 def uni_ports(self):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500170 return list(self._unis.values())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500171
Girish Gowdra322cca12020-08-09 15:55:54 -0700172 @property
173 def is_device_active_and_reachable(self):
174 return self._is_device_active_and_reachable
175
176 @is_device_active_and_reachable.setter
177 def is_device_active_and_reachable(self, value):
178 self._is_device_active_and_reachable = value
179
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500180 def uni_port(self, port_no_or_name):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500181 if isinstance(port_no_or_name, six.string_types):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500182 return next((uni for uni in self.uni_ports
183 if uni.name == port_no_or_name), None)
184
185 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
186 return next((uni for uni in self.uni_ports
Girish Gowdrae933cd32019-11-21 21:04:41 +0530187 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500188
189 @property
190 def pon_port(self):
191 return self._pon
192
Girish Gowdraa73ee452019-12-20 18:52:17 +0530193 @property
194 def onu_omci_device(self):
195 return self._onu_omci_device
196
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500197 def receive_message(self, msg):
198 if self.omci_cc is not None:
199 self.omci_cc.receive_message(msg)
200
201 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500202 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500203 def activate(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700204 self.log.debug('activate-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500205
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500206 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500207 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500208 assert device.proxy_address.device_id
209
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500210 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500211 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500212 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500213 if self.enabled is not True:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700214 self.log.info('activating-new-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500215 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500216 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500217 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500218 device.reason = 'activating-onu'
219
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500220 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400221 # pm_metrics requires a logical device id. For now set to just device_id
222 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500223
Matt Jeanneret5e331892019-12-07 21:31:45 -0500224 self._onu_persisted_state['serial_number'] = device.serial_number
225 try:
226 self.log.debug('updating-onu-state', device_id=self.device_id,
227 onu_persisted_state=self._onu_persisted_state)
228 yield self.onu_kv_client.set(self.device_id, json.dumps(self._onu_persisted_state))
229 except Exception as e:
230 self.log.error('could-not-store-onu-state', device_id=self.device_id,
231 onu_persisted_state=self._onu_persisted_state, e=e)
232 # if we cannot write to storage we can proceed, for now.
233 # later onu indications from the olt will have another chance
234
Matt Jeannereta32441c2019-03-07 05:16:37 -0500235 yield self.core_proxy.device_update(device)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500236 self.log.debug('device-updated', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500237
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700238 yield self._init_pon_state()
Matteo Scandolod8d73172019-11-26 12:15:15 -0700239 self.log.debug('pon state initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500240
Matt Jeanneret5e331892019-12-07 21:31:45 -0500241 yield self._init_metrics()
242 self.log.debug('metrics initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500243
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500244 self.enabled = True
245 else:
246 self.log.info('onu-already-activated')
247
248 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400249 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500250 def reconcile(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700251 self.log.debug('reconcile-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500252
Matt Jeanneret5e331892019-12-07 21:31:45 -0500253 if self._reconciling:
254 self.log.debug('already-running-reconcile-device', device_id=device.id, serial_number=device.serial_number)
255 return
256
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500257 # first we verify that we got parent reference and proxy info
258 assert device.parent_id
259 assert device.proxy_address.device_id
260
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700261 self.proxy_address = device.proxy_address
262 self.parent_id = device.parent_id
263 self._pon_port_number = device.parent_port_no
264
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500265 if self.enabled is not True:
Matt Jeanneret5e331892019-12-07 21:31:45 -0500266 self._reconciling = True
267 self.log.info('reconciling-openonu-device')
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700268 self.logical_device_id = self.device_id
Matt Jeanneret5e331892019-12-07 21:31:45 -0500269
270 try:
271 query_data = yield self.onu_kv_client.get(device.id)
272 self._onu_persisted_state = json.loads(query_data)
273 self.log.debug('restored-onu-state', device_id=self.device_id,
274 onu_persisted_state=self._onu_persisted_state)
275 except Exception as e:
276 self.log.error('no-stored-onu-state', device_id=device.id, e=e)
277 # there is nothing we can do without data. flag the device as UNKNOWN and cannot reconcile
278 # likely it will take manual steps to delete/re-add this onu
279 yield self.core_proxy.device_reason_update(self.device_id, "cannot-reconcile")
280 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.UNKNOWN)
281 return
282
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700283 self._init_pon_state()
Matt Jeanneret5e331892019-12-07 21:31:45 -0500284 self.log.debug('pon state 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._init_metrics()
287 self.log.debug('metrics initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500288
Matt Jeanneret5e331892019-12-07 21:31:45 -0500289 self._subscribe_to_events()
290 # need to restart omci start machines and reload mib database. once db is loaded we can finish reconcile
291 self._onu_omci_device.start(device)
292 self._heartbeat.enabled = True
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500293
294 self.enabled = True
295 else:
296 self.log.info('onu-already-activated')
297
298 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700299 def _init_pon_state(self):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500300 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 -0500301
302 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500303 self._pon.add_peer(self.parent_id, self._pon_port_number)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700304 self.log.debug('adding-pon-port-to-agent',
305 type=self._pon.get_port().type,
306 admin_state=self._pon.get_port().admin_state,
307 oper_status=self._pon.get_port().oper_status,
308 )
Matt Jeanneret0c287892019-02-28 11:48:00 -0500309
Matt Jeanneret5e331892019-12-07 21:31:45 -0500310 if not self._reconciling:
311 yield self.core_proxy.port_created(self.device_id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500312
Matteo Scandolod8d73172019-11-26 12:15:15 -0700313 self.log.debug('added-pon-port-to-agent',
314 type=self._pon.get_port().type,
315 admin_state=self._pon.get_port().admin_state,
316 oper_status=self._pon.get_port().oper_status,
317 )
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500318
319 # Create and start the OpenOMCI ONU Device Entry for this ONU
320 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500321 self.core_proxy,
322 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500323 support_classes=self.adapter.broadcom_omci,
324 custom_me_map=self.adapter.custom_me_entities())
325 # Port startup
326 if self._pon is not None:
327 self._pon.enabled = True
328
Matt Jeanneret5e331892019-12-07 21:31:45 -0500329 @inlineCallbacks
330 def _init_metrics(self):
331 self.log.debug('init-metrics', device_id=self.device_id, device_logical_id=self.logical_device_id)
332
333 serial_number = self._onu_persisted_state.get('serial_number')
334
335 ############################################################################
336 # Setup Alarm handler
337 self.events = AdapterEvents(self.core_proxy, self.device_id, self.logical_device_id,
338 serial_number)
339 ############################################################################
340 # Setup PM configuration for this device
341 # Pass in ONU specific options
342 kwargs = {
343 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
344 'heartbeat': self.heartbeat,
345 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
346 }
347 self.log.debug('create-pm-metrics', device_id=self.device_id, serial_number=serial_number)
348 self._pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
349 self.logical_device_id, serial_number,
350 grouped=True, freq_override=False, **kwargs)
351 pm_config = self._pm_metrics.make_proto()
352 self._onu_omci_device.set_pm_config(self._pm_metrics.omci_pm.openomci_interval_pm)
353 self.log.debug("initial-pm-config", device_id=self.device_id, serial_number=serial_number)
354
355 if not self._reconciling:
356 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
357
358 # Note, ONU ID and UNI intf set in add_uni_port method
359 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
360 ani_ports=[self._pon])
361
362 # Code to Run OMCI Test Action
363 kwargs_omci_test_action = {
364 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
365 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
366 }
367 self._test_request = OmciTestRequest(self.core_proxy,
368 self.omci_agent, self.device_id,
369 AniG, serial_number,
370 self.logical_device_id,
371 exclusive=False,
372 **kwargs_omci_test_action)
373
374 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500375 def delete(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700376 self.log.info('delete-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneret5e331892019-12-07 21:31:45 -0500377 try:
378 yield self.onu_kv_client.delete(device.id)
379 except Exception as e:
380 self.log.error('could-not-delete-onu-state', device_id=device.id, e=e)
381
Devmalya Paul1e1b1722020-05-07 02:51:15 -0400382 try:
383 self._deferred.cancel()
384 self._test_request.stop_collector()
385 self._pm_metrics.stop_collector()
386 self.log.debug('removing-openomci-statemachine')
387 self.omci_agent.remove_device(device.id, cleanup=True)
388 yield self.onu_deleted_event()
389 except Exception as e:
390 self.log.error('could-not-delete-onu', device_id=device.id, e=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500391
392 def _create_tconts(self, uni_id, us_scheduler):
393 alloc_id = us_scheduler['alloc_id']
394 q_sched_policy = us_scheduler['q_sched_policy']
395 self.log.debug('create-tcont', us_scheduler=us_scheduler)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800396 # TODO: revisit for multi tconts support
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800397 new_tconts = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500398 tcontdict = dict()
399 tcontdict['alloc-id'] = alloc_id
400 tcontdict['q_sched_policy'] = q_sched_policy
401 tcontdict['uni_id'] = uni_id
402
Matt Jeanneret3789d0d2020-01-19 09:03:42 -0500403 tcont = OnuTCont.create(self, tcont=tcontdict)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500404
Girish Gowdra7c1240c2020-07-15 15:06:42 -0700405 success = self._pon.add_tcont(tcont, True)
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500406 if success:
407 new_tconts.append(tcont)
408 self.log.debug('pon-add-tcont', tcont=tcont)
409
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800410 return new_tconts
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500411
412 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
413 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
414 self.log.debug('create-gemport',
415 gem_ports=gem_ports, direction=direction)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530416 new_gem_ports = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500417 for gem_port in gem_ports:
418 gemdict = dict()
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800419 if gem_port[IS_MULTICAST] == 'True':
420 gemdict[GEM_PORT_ID] = gem_port['multicast_gem_id']
421 gemdict[IS_MULTICAST] = True
422 else:
423 gemdict[GEM_PORT_ID] = gem_port[GEM_PORT_ID]
424 gemdict[IS_MULTICAST] = False
425
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500426 gemdict['direction'] = direction
427 gemdict['alloc_id_ref'] = alloc_id_ref
428 gemdict['encryption'] = gem_port['aes_encryption']
429 gemdict['discard_config'] = dict()
430 gemdict['discard_config']['max_probability'] = \
431 gem_port['discard_config']['max_probability']
432 gemdict['discard_config']['max_threshold'] = \
433 gem_port['discard_config']['max_threshold']
434 gemdict['discard_config']['min_threshold'] = \
435 gem_port['discard_config']['min_threshold']
436 gemdict['discard_policy'] = gem_port['discard_policy']
437 gemdict['max_q_size'] = gem_port['max_q_size']
438 gemdict['pbit_map'] = gem_port['pbit_map']
439 gemdict['priority_q'] = gem_port['priority_q']
440 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
441 gemdict['weight'] = gem_port['weight']
442 gemdict['uni_id'] = uni_id
443
444 gem_port = OnuGemPort.create(self, gem_port=gemdict)
445
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500446 success = self._pon.add_gem_port(gem_port, True)
447 if success:
448 new_gem_ports.append(gem_port)
449 self.log.debug('pon-add-gemport', gem_port=gem_port)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500450
Girish Gowdrae933cd32019-11-21 21:04:41 +0530451 return new_gem_ports
452
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800453 def _execute_queued_vlan_filter_tasks(self, uni_id, tp_id):
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400454 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
455 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
456 # or initial-mib-download is not done. Once the tp_task is completed, we execute
457 # such queued vlan-filter tasks
458 try:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800459 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 -0400460 self.log.info("executing-queued-vlan-filter-task",
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800461 uni_id=uni_id, tp_id=tp_id)
Mahir Gunyela982ec32020-02-25 12:30:37 -0800462 for filter_info in self._queued_vlan_filter_task[uni_id][tp_id]:
463 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800464 uni_id=uni_id, uni_port=filter_info.get("uni_port"),
465 match_vlan=filter_info.get("match_vlan"),
466 _set_vlan_vid=filter_info.get("set_vlan_vid"),
467 _set_vlan_pcp=filter_info.get("set_vlan_pcp"),
468 tp_id=filter_info.get("tp_id"))
Girish Gowdraaf98a082020-03-05 16:40:51 -0800469 # Now remove the entry from the dictionary
Girish Gowdraaf98a082020-03-05 16:40:51 -0800470 self.log.debug("executed-queued-vlan-filter-task",
471 uni_id=uni_id, tp_id=tp_id)
Girish Gowdraa63eda82020-05-12 13:40:04 -0700472
473 # Now delete the key entry for the tp_id once we have handled the
474 # queued vlan filter tasks for that tp_id
475 del self._queued_vlan_filter_task[uni_id][tp_id]
476 # If the queued vlan filter tasks for all the tp_ids on a given
477 # uni_id is handled, then delete the uni_id key
478 if len(self._queued_vlan_filter_task[uni_id]) == 0:
479 del self._queued_vlan_filter_task[uni_id]
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400480 except Exception as e:
481 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
482
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500483 def _do_tech_profile_configuration(self, uni_id, tp):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500484 us_scheduler = tp['us_scheduler']
485 alloc_id = us_scheduler['alloc_id']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800486 new_tconts = self._create_tconts(uni_id, us_scheduler)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500487 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800488 new_upstream_gems = self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500489 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800490 new_downstream_gems = self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
491
492 new_gems = []
493 new_gems.extend(new_upstream_gems)
494 new_gems.extend(new_downstream_gems)
495
496 return new_tconts, new_gems
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500497
Matt Jeanneret5e331892019-12-07 21:31:45 -0500498 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500499 def load_and_configure_tech_profile(self, uni_id, tp_path):
500 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
Mahir Gunyele9110a32020-02-20 14:56:50 -0800501 tp_id = self.extract_tp_id_from_path(tp_path)
Girish Gowdrac5117452020-08-03 11:20:53 -0700502 if tp_id not in self._tp_state_map_per_uni[uni_id]:
503 self._tp_state_map_per_uni[uni_id][tp_id] = TpState(self, uni_id, tp_path)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500504
Girish Gowdrac5117452020-08-03 11:20:53 -0700505 if not self._tp_state_map_per_uni[uni_id][tp_id].tp_setup_done:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500506 try:
Girish Gowdrac5117452020-08-03 11:20:53 -0700507 if self._tp_state_map_per_uni[uni_id][tp_id].tp_task_ref is not None:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500508 self.log.info("tech-profile-config-already-in-progress",
Girish Gowdrae933cd32019-11-21 21:04:41 +0530509 tp_path=tp_path)
Matt Jeanneret5e331892019-12-07 21:31:45 -0500510 returnValue(None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500511
Matt Jeanneret5e331892019-12-07 21:31:45 -0500512 tpstored = yield self.tp_kv_client.get(tp_path)
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500513 tpstring = tpstored.decode('ascii')
514 tp = json.loads(tpstring)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800515 self._tp[tp_id] = tp
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500516 self.log.debug("tp-instance", tp=tp)
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800517 tconts, gem_ports = self._do_tech_profile_configuration(uni_id, tp)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700518
William Kurkian3a206332019-04-29 11:05:47 -0400519 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500520 def success(_results):
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800521 self.log.info("tech-profile-config-done-successfully", uni_id=uni_id, tp_id=tp_id)
Girish Gowdrac5117452020-08-03 11:20:53 -0700522 if tp_id in self._tp_state_map_per_uni[uni_id]:
523 self._tp_state_map_per_uni[uni_id][tp_id].tp_task_ref = None
524 self._tp_state_map_per_uni[uni_id][tp_id].tp_setup_done = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400525 # Now execute any vlan filter tasks that were queued for later
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800526 reactor.callInThread(self._execute_queued_vlan_filter_tasks, uni_id, tp_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500527 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-success')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530528
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800529 # Execute mcast task
530 for gem in gem_ports:
Girish Gowdradc98d812020-03-20 13:04:58 -0700531 self.log.debug("checking-multicast-service-for-gem ", gem=gem)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800532 if gem.mcast is True:
Girish Gowdradc98d812020-03-20 13:04:58 -0700533 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 -0800534 reactor.callInThread(self.start_multicast_service, uni_id, tp_path)
535 self.log.debug("started_multicast_service-successfully", tconts=tconts, gems=gem_ports)
536 break
537
William Kurkian3a206332019-04-29 11:05:47 -0400538 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500539 def failure(_reason):
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800540 self.log.warn('tech-profile-config-failure-retrying', uni_id=uni_id, tp_id=tp_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +0530541 _reason=_reason)
Girish Gowdrac5117452020-08-03 11:20:53 -0700542 if tp_id in self._tp_state_map_per_uni[uni_id]:
543 self._tp_state_map_per_uni[uni_id][tp_id].tp_task_ref = None
544 retry = random.randint(1, 5)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500545 reactor.callLater(retry, self.load_and_configure_tech_profile,
546 uni_id, tp_path)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500547 yield self.core_proxy.device_reason_update(self.device_id,
548 'tech-profile-config-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500549
Mahir Gunyela982ec32020-02-25 12:30:37 -0800550 self.log.info('downloading-tech-profile-configuration', uni_id=uni_id, tp_id=tp_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530551 self.log.debug("tconts-gems-to-install", tconts=tconts, gem_ports=gem_ports)
552
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500553 self.log.debug("current-cached-tconts", tconts=list(self.pon_port.tconts.values()))
554 self.log.debug("current-cached-gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
555
Girish Gowdrac5117452020-08-03 11:20:53 -0700556 self._tp_state_map_per_uni[uni_id][tp_id].tp_task_ref = \
Mahir Gunyele9110a32020-02-20 14:56:50 -0800557 BrcmTpSetupTask(self.omci_agent, self, uni_id, tconts, gem_ports, tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500558 self._deferred = \
Girish Gowdrac5117452020-08-03 11:20:53 -0700559 self._onu_omci_device.task_runner.queue_task(self._tp_state_map_per_uni[uni_id][tp_id].
560 tp_task_ref)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500561 self._deferred.addCallbacks(success, failure)
562
563 except Exception as e:
564 self.log.exception("error-loading-tech-profile", e=e)
565 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700566 # There is an active tech-profile task ongoing on this UNI port. So, reschedule this task
567 # after a short interval
Girish Gowdrac5117452020-08-03 11:20:53 -0700568 for tpid in self._tp_state_map_per_uni[uni_id]:
569 if self._tp_state_map_per_uni[uni_id][tpid].tp_task_ref is not None:
570 self.log.debug("active-tp-tasks-in-progress-for-uni--scheduling-this-task-for-later",
571 uni_id=uni_id, tp_id=tpid)
572 retry = random.randint(1, 5)
573 reactor.callLater(retry, self.load_and_configure_tech_profile,
574 uni_id, tp_path)
575 return
Girish Gowdradc98d812020-03-20 13:04:58 -0700576
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 Gowdrac5117452020-08-03 11:20:53 -0700618 retry = 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 Gowdrac5117452020-08-03 11:20:53 -0700626 self._tp_state_map_per_uni[uni_id][tp_id].tp_task_ref = \
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 = \
Girish Gowdrac5117452020-08-03 11:20:53 -0700629 self._onu_omci_device.task_runner.queue_task(self._tp_state_map_per_uni[uni_id][tp_id].
630 tp_task_ref)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530631 self._deferred.addCallbacks(success, failure)
Girish Gowdradc98d812020-03-20 13:04:58 -0700632
Matt Jeanneret5e331892019-12-07 21:31:45 -0500633 @inlineCallbacks
Girish Gowdradc98d812020-03-20 13:04:58 -0700634 def start_multicast_service(self, uni_id, tp_path, retry_count=0):
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800635 self.log.debug("starting-multicast-service", uni_id=uni_id, tp_path=tp_path)
636 tp_id = self.extract_tp_id_from_path(tp_path)
637 if uni_id in self._set_vlan and tp_id in self._set_vlan[uni_id]:
638 try:
639 tp = self._tp[tp_id]
640 if tp is None:
Matt Jeanneret5e331892019-12-07 21:31:45 -0500641 tpstored = yield self.tp_kv_client.get(tp_path)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800642 tpstring = tpstored.decode('ascii')
643 tp = json.loads(tpstring)
644 if tp is None:
645 self.log.error("cannot-find-tp-to-start-multicast-service", uni_id=uni_id, tp_path=tp_path)
646 return
647 else:
648 self._tp[tp_id] = tp
649
650 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 -0700651
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800652 def success(_results):
653 self.log.debug('multicast-success', uni_id=uni_id)
654 self._multicast_task = None
655
656 def failure(_reason):
657 self.log.warn('multicast-failure', _reason=_reason)
Girish Gowdrac5117452020-08-03 11:20:53 -0700658 retry = random.randint(1, 5)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800659 reactor.callLater(retry, self.start_multicast_service,
Girish Gowdradc98d812020-03-20 13:04:58 -0700660 uni_id, tp_path)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800661
662 self.log.debug('starting-multicast-task', mcast_vlan_id=self._set_vlan[uni_id][tp_id])
663 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
664 for i in range(len(downstream_gem_port_attribute_list)):
665 if IS_MULTICAST in downstream_gem_port_attribute_list[i] and \
666 downstream_gem_port_attribute_list[i][IS_MULTICAST] == 'True':
Girish Gowdradc98d812020-03-20 13:04:58 -0700667 dynamic_access_control_list_table = downstream_gem_port_attribute_list[i][
668 'dynamic_access_control_list'].split("-")
669 static_access_control_list_table = downstream_gem_port_attribute_list[i][
670 'static_access_control_list'].split("-")
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800671 multicast_gem_id = downstream_gem_port_attribute_list[i]['multicast_gem_id']
672 break
673
674 self._multicast_task = BrcmMcastTask(self.omci_agent, self, self.device_id, uni_id, tp_id,
Girish Gowdradc98d812020-03-20 13:04:58 -0700675 self._set_vlan[uni_id][tp_id], dynamic_access_control_list_table,
676 static_access_control_list_table, multicast_gem_id)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800677 self._deferred = self._onu_omci_device.task_runner.queue_task(self._multicast_task)
678 self._deferred.addCallbacks(success, failure)
679 except Exception as e:
680 self.log.exception("error-loading-multicast", e=e)
681 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700682 if retry_count < 30:
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800683 retry_count = +1
Girish Gowdradc98d812020-03-20 13:04:58 -0700684 self.log.debug("going-to-wait-for-flow-to-learn-mcast-vlan", uni_id=uni_id, tp_id=tp_id,
685 retry=retry_count)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800686 reactor.callLater(0.5, self.start_multicast_service, uni_id, tp_path, retry_count)
687 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700688 self.log.error("mcast-vlan-not-configured-yet-failing-mcast-service-conf", uni_id=uni_id, tp_id=tp_id,
689 retry=retry_count)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530690
Girish Gowdraba4b1812020-07-17 12:21:26 -0700691 def _clear_alloc_id_gem_port_from_internal_cache(self, alloc_id=None, gem_port_id=None):
692 tcont = None
693 gem_port = None
694 if alloc_id is not None:
695 self.log.debug("current-cached-tconts", tconts=list(self.pon_port.tconts.values()))
696 for tc in list(self.pon_port.tconts.values()):
697 if tc.alloc_id == alloc_id:
698 self.log.info("removing-tcont-from-internal-cache",
699 alloc_id=alloc_id)
700 tcont = tc
701 self.pon_port.remove_tcont(tc.alloc_id, False)
702
703 if gem_port_id is not None:
704 self.log.debug("current-cached-gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
705 for gp in list(self.pon_port.gem_ports.values()):
706 if gp.gem_id == gem_port_id:
707 self.log.info("removing-gem-from-internal-cache",
708 gem_port_id=gem_port_id, direction=gp.direction)
709 gem_port = gp
710 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
711
712 return tcont, gem_port
713
Girish Gowdrac5117452020-08-03 11:20:53 -0700714 def _tcont_delete_complete(self, uni_id, tp_id):
715 if not self._tp_state_map_per_uni[uni_id][tp_id].is_all_pon_resource_delete_complete():
716 self.log.info("waiting-for-gem-port-delete-to-complete-before-clearing-tp-states")
717 retry = random.randint(1, 5)
718 reactor.callLater(retry, self._tcont_delete_complete, uni_id, tp_id)
719 return
720 self.log.info("tp-delete-complete")
721 # Clear TP states
722 self._tp_state_map_per_uni[uni_id][tp_id].reset_tp_state()
723 del self._tp_state_map_per_uni[uni_id][tp_id]
724
725 def delete_tech_profile(self, uni_id, tp_path, tcont=None, gem_port=None):
726 alloc_id = None
727 gem_port_id = None
Girish Gowdrae933cd32019-11-21 21:04:41 +0530728 try:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800729 tp_table_id = self.extract_tp_id_from_path(tp_path)
Girish Gowdraba4b1812020-07-17 12:21:26 -0700730 # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
731 # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
732 # due to additional tasks on different UNIs. So, it we cannot use the pon_port affter
733 # this initializer
Girish Gowdrac5117452020-08-03 11:20:53 -0700734 alloc_id = tcont.alloc_id if tcont is not None else None
735 gem_port_id = gem_port.gem_id if gem_port is not None else None
736 self._clear_alloc_id_gem_port_from_internal_cache(alloc_id, gem_port_id)
Girish Gowdraba4b1812020-07-17 12:21:26 -0700737
Girish Gowdrac5117452020-08-03 11:20:53 -0700738 if tp_table_id not in self._tp_state_map_per_uni[uni_id]:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800739 self.log.warn("tp-id-is-not-present", uni_id=uni_id, tp_id=tp_table_id)
Naga Manjunathe433c712020-01-02 17:27:20 +0530740 return
741
Girish Gowdrac5117452020-08-03 11:20:53 -0700742 if self._tp_state_map_per_uni[uni_id][tp_table_id].tp_setup_done is not True:
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800743 self.log.error("tp-download-is-not-done-in-order-to-process-tp-delete", uni_id=uni_id,
744 tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530745 return
746
747 if alloc_id is None and gem_port_id is None:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800748 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 +0530749 return
750
Girish Gowdrae933cd32019-11-21 21:04:41 +0530751 @inlineCallbacks
752 def success(_results):
753 if gem_port_id:
754 self.log.info("gem-port-delete-done-successfully")
Girish Gowdrac5117452020-08-03 11:20:53 -0700755 self._tp_state_map_per_uni[uni_id][tp_table_id].pon_resource_delete_complete(TpState.GEM_ID,
756 gem_port_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530757 if alloc_id:
758 self.log.info("tcont-delete-done-successfully")
759 # The deletion of TCONT marks the complete deletion of tech-profile
Girish Gowdrac5117452020-08-03 11:20:53 -0700760 self._tp_state_map_per_uni[uni_id][tp_table_id].pon_resource_delete_complete(TpState.ALLOC_ID,
761 alloc_id)
762 self._tcont_delete_complete(uni_id, tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530763
764 # TODO: There could be multiple TP on the UNI, and also the ONU.
765 # TODO: But the below reason updates for the whole device.
766 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-delete-success')
767
768 @inlineCallbacks
Girish Gowdraa73ee452019-12-20 18:52:17 +0530769 def failure(_reason):
Girish Gowdrae933cd32019-11-21 21:04:41 +0530770 self.log.warn('tech-profile-delete-failure-retrying',
771 _reason=_reason)
Girish Gowdrac5117452020-08-03 11:20:53 -0700772 retry = random.randint(1, 5)
773 _tcont = self._tp_state_map_per_uni[uni_id][tp_table_id].get_queued_resource_for_delete(TpState.ALLOC_ID, alloc_id)
774 _gem_port = self._tp_state_map_per_uni[uni_id][tp_table_id].get_queued_resource_for_delete(TpState.GEM_ID, gem_port_id)
775 reactor.callLater(retry, self.delete_tech_profile, uni_id, tp_path, _tcont, _gem_port)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500776 yield self.core_proxy.device_reason_update(self.device_id,
777 'tech-profile-config-delete-failure-retrying')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530778
779 self.log.info('deleting-tech-profile-configuration')
780
Girish Gowdraa73ee452019-12-20 18:52:17 +0530781 if tcont is None and gem_port is None:
782 if alloc_id is not None:
783 self.log.error("tcont-info-corresponding-to-alloc-id-not-found", alloc_id=alloc_id)
784 if gem_port_id is not None:
785 self.log.error("gem-port-info-corresponding-to-gem-port-id-not-found", gem_port_id=gem_port_id)
786 return
787
Girish Gowdrac5117452020-08-03 11:20:53 -0700788 self._tp_state_map_per_uni[uni_id][tp_table_id].tp_task_ref = \
Girish Gowdrae933cd32019-11-21 21:04:41 +0530789 BrcmTpDeleteTask(self.omci_agent, self, uni_id, tp_table_id,
790 tcont=tcont, gem_port=gem_port)
791 self._deferred = \
Girish Gowdrac5117452020-08-03 11:20:53 -0700792 self._onu_omci_device.task_runner.queue_task(self._tp_state_map_per_uni[uni_id][tp_table_id].
793 tp_task_ref)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530794 self._deferred.addCallbacks(success, failure)
795 except Exception as e:
796 self.log.exception("failed-to-delete-tp",
797 e=e, uni_id=uni_id, tp_path=tp_path,
798 alloc_id=alloc_id, gem_port_id=gem_port_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500799
Rohan Agrawalf0f8c292020-06-01 09:30:55 +0000800 def update_pm_config(self, device, pm_configs):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500801 # TODO: This has not been tested
Rohan Agrawalf0f8c292020-06-01 09:30:55 +0000802 self.log.info('update_pm_config', pm_configs=pm_configs)
803 self._pm_metrics.update(pm_configs)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500804
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800805 def remove_onu_flows(self, device, flows):
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500806 self.log.debug('remove-onu-flows')
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800807
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800808 # no point in removing omci flows if the device isnt reachable
809 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800810 device.admin_state != AdminState.ENABLED:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800811 self.log.warn("device-disabled-or-offline-skipping-remove-flow",
812 admin=device.admin_state, connect=device.connect_status)
813 return
814
815 for flow in flows:
816 # if incoming flow contains cookie, then remove from ONU
817 if flow.cookie:
818 self.log.debug("remove-flow", device_id=device.id, flow=flow)
819
820 def is_downstream(port):
821 return port == self._pon_port_number
822
823 def is_upstream(port):
824 return not is_downstream(port)
825
826 try:
827 _in_port = fd.get_in_port(flow)
828 assert _in_port is not None
829
830 _out_port = fd.get_out_port(flow) # may be None
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800831
832 if is_downstream(_in_port):
833 self.log.debug('downstream-flow-no-need-to-remove', in_port=_in_port, out_port=_out_port,
834 device_id=device.id)
835 # extended vlan tagging operation will handle it
836 continue
837 elif is_upstream(_in_port):
838 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
839 if fd.is_dhcp_flow(flow):
840 self.log.debug('The dhcp trap-to-host flow will be discarded', device_id=device.id)
841 return
842
Mahir Gunyel45610b42020-03-16 17:29:01 -0700843 _match_vlan_vid = None
844 for field in fd.get_ofb_fields(flow):
845 if field.type == fd.VLAN_VID:
846 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
847 _match_vlan_vid = RESERVED_TRANSPARENT_VLAN
848 else:
849 _match_vlan_vid = field.vlan_vid & 0xfff
850 self.log.debug('field-type-vlan-vid',
851 vlan=_match_vlan_vid)
852
853 _set_vlan_vid = None
854 _set_vlan_pcp = None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800855 # Retrieve the VLAN_VID that needs to be removed from the EVTO rule on the ONU.
856 for action in fd.get_actions(flow):
857 if action.type == fd.SET_FIELD:
858 _field = action.set_field.field.ofb_field
859 assert (action.set_field.field.oxm_class ==
860 OFPXMC_OPENFLOW_BASIC)
861 if _field.type == fd.VLAN_VID:
Mahir Gunyel45610b42020-03-16 17:29:01 -0700862 _set_vlan_vid = _field.vlan_vid & 0xfff
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800863 self.log.debug('vlan-vid-to-remove',
Mahir Gunyel45610b42020-03-16 17:29:01 -0700864 _vlan_vid=_set_vlan_vid, in_port=_in_port)
865 elif _field.type == fd.VLAN_PCP:
866 _set_vlan_pcp = _field.vlan_pcp
867 self.log.debug('set-field-type-vlan-pcp',
868 vlan_pcp=_set_vlan_pcp)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800869
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800870 uni_port = self.uni_port(_in_port)
871 uni_id = _in_port & 0xF
872 else:
873 raise Exception('port should be 1 or 2 by our convention')
874
875 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
876
877 tp_id = self.get_tp_id_in_flow(flow)
Girish Gowdradc98d812020-03-20 13:04:58 -0700878 # The vlan filter remove should be followed by a TP deleted for that TP ID.
879 # Use this information to re-schedule any vlan filter add tasks for the same TP ID again.
880 # First check if the TP download was done, before we access that TP delete is necessary
Girish Gowdrac5117452020-08-03 11:20:53 -0700881 if tp_id in self._tp_state_map_per_uni[uni_id] and \
882 self._tp_state_map_per_uni[uni_id][tp_id].tp_setup_done is True:
883 self._tp_state_map_per_uni[uni_id][tp_id].is_tp_delete_pending = True
884
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800885 # Deleting flow from ONU.
Mahir Gunyel45610b42020-03-16 17:29:01 -0700886 self._remove_vlan_filter_task(device, uni_id, uni_port=uni_port,
887 _set_vlan_pcp=_set_vlan_pcp,
888 _set_vlan_vid=_set_vlan_vid,
889 match_vlan=_match_vlan_vid,
890 tp_id=tp_id)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800891 # TODO:Delete TD task.
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800892 except Exception as e:
893 self.log.exception('failed-to-remove-flow', e=e)
894
895 def add_onu_flows(self, device, flows):
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500896 self.log.debug('add-onu-flows')
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800897
898 # no point in pushing omci flows if the device isnt reachable
899 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800900 device.admin_state != AdminState.ENABLED:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800901 self.log.warn("device-disabled-or-offline-skipping-flow-update",
902 admin=device.admin_state, connect=device.connect_status)
903 return
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800904
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800905 def is_downstream(port):
906 return port == self._pon_port_number
907
908 def is_upstream(port):
909 return not is_downstream(port)
910
911 for flow in flows:
912 # if incoming flow contains cookie, then add to ONU
913 if flow.cookie:
914 _type = None
915 _port = None
916 _vlan_vid = None
917 _udp_dst = None
918 _udp_src = None
919 _ipv4_dst = None
920 _ipv4_src = None
921 _metadata = None
922 _output = None
923 _push_tpid = None
924 _field = None
925 _set_vlan_vid = None
Mahir Gunyel45610b42020-03-16 17:29:01 -0700926 _set_vlan_pcp = None
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800927 _tunnel_id = None
Girish Gowdra6a73ad62020-06-11 13:40:16 -0700928 _proto = -1
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800929 self.log.debug("add-flow", device_id=device.id, flow=flow)
930
931 try:
932 _in_port = fd.get_in_port(flow)
933 assert _in_port is not None
934
935 _out_port = fd.get_out_port(flow) # may be None
936 tp_id = self.get_tp_id_in_flow(flow)
937 if is_downstream(_in_port):
938 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
939 # NOTE: We don't care downstream flow because we will copy vlan_id to upstream flow
940 # uni_port = self.uni_port(_out_port)
941 # uni_id = _out_port & 0xF
942 continue
943 elif is_upstream(_in_port):
944 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
945 uni_port = self.uni_port(_in_port)
946 uni_id = _in_port & 0xF
947 else:
948 raise Exception('port should be 1 or 2 by our convention')
949
950 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
951
952 for field in fd.get_ofb_fields(flow):
953 if field.type == fd.ETH_TYPE:
954 _type = field.eth_type
955 self.log.debug('field-type-eth-type',
956 eth_type=_type)
957
958 elif field.type == fd.IP_PROTO:
959 _proto = field.ip_proto
Girish Gowdra6a73ad62020-06-11 13:40:16 -0700960 if _proto == 2:
961 # Workaround for TT workflow - avoids installing invalid EVTO rule
962 self.log.debug("igmp-trap-flow")
963 break
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800964 self.log.debug('field-type-ip-proto',
965 ip_proto=_proto)
966
967 elif field.type == fd.IN_PORT:
968 _port = field.port
969 self.log.debug('field-type-in-port',
970 in_port=_port)
971 elif field.type == fd.TUNNEL_ID:
972 self.log.debug('field-type-tunnel-id')
973
974 elif field.type == fd.VLAN_VID:
Andrea Campanellacf916ea2020-02-14 10:03:58 +0100975 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
976 _vlan_vid = RESERVED_TRANSPARENT_VLAN
977 else:
978 _vlan_vid = field.vlan_vid & 0xfff
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800979 self.log.debug('field-type-vlan-vid',
980 vlan=_vlan_vid)
981
982 elif field.type == fd.VLAN_PCP:
983 _vlan_pcp = field.vlan_pcp
984 self.log.debug('field-type-vlan-pcp',
985 pcp=_vlan_pcp)
986
987 elif field.type == fd.UDP_DST:
988 _udp_dst = field.udp_dst
989 self.log.debug('field-type-udp-dst',
990 udp_dst=_udp_dst)
991
992 elif field.type == fd.UDP_SRC:
993 _udp_src = field.udp_src
994 self.log.debug('field-type-udp-src',
995 udp_src=_udp_src)
996
997 elif field.type == fd.IPV4_DST:
998 _ipv4_dst = field.ipv4_dst
999 self.log.debug('field-type-ipv4-dst',
1000 ipv4_dst=_ipv4_dst)
1001
1002 elif field.type == fd.IPV4_SRC:
1003 _ipv4_src = field.ipv4_src
1004 self.log.debug('field-type-ipv4-src',
1005 ipv4_dst=_ipv4_src)
1006
1007 elif field.type == fd.METADATA:
1008 _metadata = field.table_metadata
1009 self.log.debug('field-type-metadata',
1010 metadata=_metadata)
1011
1012 else:
1013 raise NotImplementedError('field.type={}'.format(
1014 field.type))
1015
Girish Gowdra6a73ad62020-06-11 13:40:16 -07001016 if _proto == 2:
1017 # Workaround for TT workflow - avoids installing invalid EVTO rule
1018 self.log.warn("skipping-igmp-trap-flow")
1019 continue
1020
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001021 for action in fd.get_actions(flow):
1022
1023 if action.type == fd.OUTPUT:
1024 _output = action.output.port
1025 self.log.debug('action-type-output',
1026 output=_output, in_port=_in_port)
1027
1028 elif action.type == fd.POP_VLAN:
1029 self.log.debug('action-type-pop-vlan',
1030 in_port=_in_port)
1031
1032 elif action.type == fd.PUSH_VLAN:
1033 _push_tpid = action.push.ethertype
1034 self.log.debug('action-type-push-vlan',
1035 push_tpid=_push_tpid, in_port=_in_port)
1036 if action.push.ethertype != 0x8100:
1037 self.log.error('unhandled-tpid',
1038 ethertype=action.push.ethertype)
1039
1040 elif action.type == fd.SET_FIELD:
1041 _field = action.set_field.field.ofb_field
1042 assert (action.set_field.field.oxm_class ==
1043 OFPXMC_OPENFLOW_BASIC)
1044 self.log.debug('action-type-set-field',
1045 field=_field, in_port=_in_port)
1046 if _field.type == fd.VLAN_VID:
1047 _set_vlan_vid = _field.vlan_vid & 0xfff
1048 self.log.debug('set-field-type-vlan-vid',
1049 vlan_vid=_set_vlan_vid)
1050 elif _field.type == fd.VLAN_PCP:
1051 _set_vlan_pcp = _field.vlan_pcp
1052 self.log.debug('set-field-type-vlan-pcp',
1053 vlan_pcp=_set_vlan_pcp)
1054 else:
1055 self.log.error('unsupported-action-set-field-type',
1056 field_type=_field.type)
1057 else:
1058 self.log.error('unsupported-action-type',
1059 action_type=action.type, in_port=_in_port)
1060
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001061 if self._set_vlan is not None:
1062 if uni_id not in self._set_vlan:
1063 self._set_vlan[uni_id] = dict()
1064 self._set_vlan[uni_id][tp_id] = _set_vlan_vid
1065 self.log.debug("set_vlan_id-for-tp", _set_vlan_vid=_set_vlan_vid, tp_id=tp_id)
1066
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001067 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
1068 # supported match and action criteria cannot be handled by omci and must be ignored.
1069 if (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid != RESERVED_TRANSPARENT_VLAN:
1070 self.log.warn('ignoring-flow-that-does-not-set-vlanid', set_vlan_vid=_set_vlan_vid)
1071 elif (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid == RESERVED_TRANSPARENT_VLAN:
1072 self.log.info('set-vlanid-any', uni_id=uni_id, uni_port=uni_port,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001073 _set_vlan_vid=_vlan_vid,
1074 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1075 tp_id=tp_id)
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001076 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1077 _set_vlan_vid=_vlan_vid,
1078 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1079 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001080 else:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001081 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, match_vlan=_vlan_vid,
1082 set_vlan_vid=_set_vlan_vid, _set_vlan_pcp=_set_vlan_pcp, ethType=_type)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001083 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1084 _set_vlan_vid=_set_vlan_vid,
1085 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1086 tp_id=tp_id)
1087
1088 except Exception as e:
1089 self.log.exception('failed-to-install-flow', e=e, flow=flow)
1090
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001091 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
1092 # flow decomposition that ultimately comes from onos
1093 def update_flow_table(self, device, flows):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001094 self.log.debug('update-flow-table', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001095
1096 #
1097 # We need to proxy through the OLT to get to the ONU
1098 # Configuration from here should be using OMCI
1099 #
1100 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
1101
1102 # no point in pushing omci flows if the device isnt reachable
1103 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdrae933cd32019-11-21 21:04:41 +05301104 device.admin_state != AdminState.ENABLED:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001105 self.log.warn("device-disabled-or-offline-skipping-flow-update",
1106 admin=device.admin_state, connect=device.connect_status)
1107 return
1108
1109 def is_downstream(port):
1110 return port == self._pon_port_number
1111
1112 def is_upstream(port):
1113 return not is_downstream(port)
1114
1115 for flow in flows:
1116 _type = None
1117 _port = None
1118 _vlan_vid = None
1119 _udp_dst = None
1120 _udp_src = None
1121 _ipv4_dst = None
1122 _ipv4_src = None
1123 _metadata = None
1124 _output = None
1125 _push_tpid = None
1126 _field = None
1127 _set_vlan_vid = None
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001128 _set_vlan_pcp = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -04001129 _tunnel_id = None
1130
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001131 try:
Girish Gowdraa73ee452019-12-20 18:52:17 +05301132 write_metadata = fd.get_write_metadata(flow)
1133 if write_metadata is None:
1134 self.log.error("do-not-process-flow-without-write-metadata")
1135 return
1136
1137 # extract tp id from flow
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001138 tp_id = self.get_tp_id_in_flow(flow)
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001139 self.log.debug("tp-id-in-flow", tp_id=tp_id)
Girish Gowdraa73ee452019-12-20 18:52:17 +05301140
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001141 _in_port = fd.get_in_port(flow)
1142 assert _in_port is not None
1143
1144 _out_port = fd.get_out_port(flow) # may be None
1145
1146 if is_downstream(_in_port):
1147 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
1148 uni_port = self.uni_port(_out_port)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301149 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001150 elif is_upstream(_in_port):
1151 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
1152 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001153 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001154 else:
1155 raise Exception('port should be 1 or 2 by our convention')
1156
1157 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
1158
1159 for field in fd.get_ofb_fields(flow):
1160 if field.type == fd.ETH_TYPE:
1161 _type = field.eth_type
1162 self.log.debug('field-type-eth-type',
1163 eth_type=_type)
1164
1165 elif field.type == fd.IP_PROTO:
1166 _proto = field.ip_proto
1167 self.log.debug('field-type-ip-proto',
1168 ip_proto=_proto)
1169
1170 elif field.type == fd.IN_PORT:
1171 _port = field.port
1172 self.log.debug('field-type-in-port',
1173 in_port=_port)
1174
1175 elif field.type == fd.VLAN_VID:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001176 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
1177 _vlan_vid = RESERVED_TRANSPARENT_VLAN
1178 else:
1179 _vlan_vid = field.vlan_vid & 0xfff
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001180 self.log.debug('field-type-vlan-vid',
1181 vlan=_vlan_vid)
1182
1183 elif field.type == fd.VLAN_PCP:
1184 _vlan_pcp = field.vlan_pcp
1185 self.log.debug('field-type-vlan-pcp',
1186 pcp=_vlan_pcp)
1187
1188 elif field.type == fd.UDP_DST:
1189 _udp_dst = field.udp_dst
1190 self.log.debug('field-type-udp-dst',
1191 udp_dst=_udp_dst)
1192
1193 elif field.type == fd.UDP_SRC:
1194 _udp_src = field.udp_src
1195 self.log.debug('field-type-udp-src',
1196 udp_src=_udp_src)
1197
1198 elif field.type == fd.IPV4_DST:
1199 _ipv4_dst = field.ipv4_dst
1200 self.log.debug('field-type-ipv4-dst',
1201 ipv4_dst=_ipv4_dst)
1202
1203 elif field.type == fd.IPV4_SRC:
1204 _ipv4_src = field.ipv4_src
1205 self.log.debug('field-type-ipv4-src',
1206 ipv4_dst=_ipv4_src)
1207
1208 elif field.type == fd.METADATA:
1209 _metadata = field.table_metadata
1210 self.log.debug('field-type-metadata',
1211 metadata=_metadata)
1212
Matt Jeanneretef06d0d2019-04-27 17:36:53 -04001213 elif field.type == fd.TUNNEL_ID:
1214 _tunnel_id = field.tunnel_id
1215 self.log.debug('field-type-tunnel-id',
1216 tunnel_id=_tunnel_id)
1217
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001218
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001219 else:
1220 raise NotImplementedError('field.type={}'.format(
1221 field.type))
1222
1223 for action in fd.get_actions(flow):
1224
1225 if action.type == fd.OUTPUT:
1226 _output = action.output.port
1227 self.log.debug('action-type-output',
1228 output=_output, in_port=_in_port)
1229
1230 elif action.type == fd.POP_VLAN:
1231 self.log.debug('action-type-pop-vlan',
1232 in_port=_in_port)
1233
1234 elif action.type == fd.PUSH_VLAN:
1235 _push_tpid = action.push.ethertype
1236 self.log.debug('action-type-push-vlan',
1237 push_tpid=_push_tpid, in_port=_in_port)
1238 if action.push.ethertype != 0x8100:
1239 self.log.error('unhandled-tpid',
1240 ethertype=action.push.ethertype)
1241
1242 elif action.type == fd.SET_FIELD:
1243 _field = action.set_field.field.ofb_field
1244 assert (action.set_field.field.oxm_class ==
1245 OFPXMC_OPENFLOW_BASIC)
1246 self.log.debug('action-type-set-field',
1247 field=_field, in_port=_in_port)
1248 if _field.type == fd.VLAN_VID:
1249 _set_vlan_vid = _field.vlan_vid & 0xfff
1250 self.log.debug('set-field-type-vlan-vid',
1251 vlan_vid=_set_vlan_vid)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001252 elif _field.type == fd.VLAN_PCP:
1253 _set_vlan_pcp = _field.vlan_pcp
1254 self.log.debug('set-field-type-vlan-pcp',
1255 vlan_pcp=_set_vlan_pcp)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001256 else:
1257 self.log.error('unsupported-action-set-field-type',
1258 field_type=_field.type)
1259 else:
1260 self.log.error('unsupported-action-type',
1261 action_type=action.type, in_port=_in_port)
1262
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001263 if self._set_vlan is not None:
1264 if uni_id not in self._set_vlan:
1265 self._set_vlan[uni_id] = dict()
1266 self._set_vlan[uni_id][tp_id] = _set_vlan_vid
1267 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 -04001268 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
1269 # supported match and action criteria cannot be handled by omci and must be ignored.
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001270 if (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid != RESERVED_TRANSPARENT_VLAN:
1271 self.log.warn('ignoring-flow-that-does-not-set-vlanid', set_vlan_vid=_set_vlan_vid)
1272 elif (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid == RESERVED_TRANSPARENT_VLAN:
1273 self.log.info('set-vlanid-any', uni_id=uni_id, uni_port=uni_port,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001274 _set_vlan_vid=_vlan_vid,
1275 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1276 tp_id=tp_id)
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001277 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1278 _set_vlan_vid=_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 else:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001282 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, match_vlan=_vlan_vid,
1283 set_vlan_vid=_set_vlan_vid, _set_vlan_pcp=_set_vlan_pcp, ethType=_type)
1284 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1285 _set_vlan_vid=_set_vlan_vid,
1286 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1287 tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001288 except Exception as e:
1289 self.log.exception('failed-to-install-flow', e=e, flow=flow)
1290
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001291 def _add_vlan_filter_task(self, device, uni_id, uni_port=None, match_vlan=0,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001292 _set_vlan_vid=None, _set_vlan_pcp=8, tp_id=0):
Girish Gowdrac5117452020-08-03 11:20:53 -07001293 if tp_id in self._tp_state_map_per_uni[uni_id] and \
1294 self._tp_state_map_per_uni[uni_id][tp_id].is_tp_delete_pending is True:
Girish Gowdradc98d812020-03-20 13:04:58 -07001295 self.log.debug("pending-del-tp--scheduling-add-vlan-filter-task-for-later")
Girish Gowdrac5117452020-08-03 11:20:53 -07001296 retry = random.randint(1, 5)
1297 reactor.callLater(retry, self._add_vlan_filter_task, device, uni_id, uni_port, match_vlan,
Girish Gowdradc98d812020-03-20 13:04:58 -07001298 _set_vlan_vid, _set_vlan_pcp, tp_id)
1299 return
1300
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001301 self.log.info('_adding_vlan_filter_task', uni_port=uni_port, uni_id=uni_id, tp_id=tp_id, match_vlan=match_vlan,
1302 vlan=_set_vlan_vid, vlan_pcp=_set_vlan_pcp)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001303 assert uni_port is not None
Girish Gowdrac5117452020-08-03 11:20:53 -07001304 if tp_id in self._tp_state_map_per_uni[uni_id] and \
1305 self._tp_state_map_per_uni[uni_id][tp_id].tp_setup_done is True:
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001306 @inlineCallbacks
1307 def success(_results):
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001308 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid, tp_id=tp_id,
1309 set_vlan_pcp=_set_vlan_pcp)
Matt Jeanneretd84c9072020-01-31 06:33:27 -05001310 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-pushed')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001311
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001312 @inlineCallbacks
1313 def failure(_reason):
Girish Gowdraa73ee452019-12-20 18:52:17 +05301314 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid, tp_id=tp_id)
Girish Gowdrac5117452020-08-03 11:20:53 -07001315 retry = random.randint(1, 5)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001316 reactor.callLater(retry,
1317 self._add_vlan_filter_task, device, uni_id, uni_port=uni_port,
1318 match_vlan=match_vlan, _set_vlan_vid=_set_vlan_vid,
1319 _set_vlan_pcp=_set_vlan_pcp, tp_id=tp_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -05001320 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-failed-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001321
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001322 self.log.info('setting-vlan-tag', uni_port=uni_port, uni_id=uni_id, tp_id=tp_id, match_vlan=match_vlan,
1323 vlan=_set_vlan_vid, vlan_pcp=_set_vlan_pcp)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001324 vlan_filter_add_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001325 match_vlan, _set_vlan_pcp, add_tag=True,
1326 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001327 self._deferred = self._onu_omci_device.task_runner.queue_task(vlan_filter_add_task)
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001328 self._deferred.addCallbacks(success, failure)
1329 else:
1330 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001331 uni_id=uni_id, tp_id=tp_id)
1332 if uni_id not in self._queued_vlan_filter_task:
1333 self._queued_vlan_filter_task[uni_id] = dict()
Mahir Gunyela982ec32020-02-25 12:30:37 -08001334 if tp_id not in self._queued_vlan_filter_task[uni_id]:
1335 self._queued_vlan_filter_task[uni_id][tp_id] = []
1336 self._queued_vlan_filter_task[uni_id][tp_id].append({"device": device,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001337 "uni_id": uni_id,
1338 "uni_port": uni_port,
1339 "match_vlan": match_vlan,
1340 "set_vlan_vid": _set_vlan_vid,
1341 "set_vlan_pcp": _set_vlan_pcp,
1342 "tp_id": tp_id})
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001343
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001344 def get_tp_id_in_flow(self, flow):
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001345 flow_metadata = fd.get_metadata_from_write_metadata(flow)
1346 tp_id = fd.get_tp_id_from_metadata(flow_metadata)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001347 return tp_id
1348
1349 def _remove_vlan_filter_task(self, device, uni_id, uni_port=None, match_vlan=0,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001350 _set_vlan_vid=None, _set_vlan_pcp=8, tp_id=0):
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001351 assert uni_port is not None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001352
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001353 @inlineCallbacks
1354 def success(_results):
1355 self.log.info('vlan-untagging-success', _results=_results)
1356 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-deleted')
1357
1358 @inlineCallbacks
1359 def failure(_reason):
1360 self.log.warn('vlan-untagging-failure', _reason=_reason)
1361 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-deletion-failed-retrying')
Girish Gowdrac5117452020-08-03 11:20:53 -07001362 retry = random.randint(1, 5)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001363 reactor.callLater(retry,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001364 self._remove_vlan_filter_task, device, uni_id,
ozgecanetsiace4e37f2020-07-20 10:16:00 +03001365 uni_port=uni_port, match_vlan=match_vlan, _set_vlan_vid=_set_vlan_vid,
1366 _set_vlan_pcp=_set_vlan_pcp, tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001367
1368 self.log.info("remove_vlan_filter_task", tp_id=tp_id)
1369 vlan_remove_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001370 match_vlan, _set_vlan_pcp, add_tag=False,
1371 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001372 self._deferred = self._onu_omci_device.task_runner.queue_task(vlan_remove_task)
1373 self._deferred.addCallbacks(success, failure)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001374
Matt Jeanneret5e331892019-12-07 21:31:45 -05001375 @inlineCallbacks
Matt Jeannereta32441c2019-03-07 05:16:37 -05001376 def process_inter_adapter_message(self, request):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001377 self.log.debug('process-inter-adapter-message', type=request.header.type, from_topic=request.header.from_topic,
1378 to_topic=request.header.to_topic, to_device_id=request.header.to_device_id)
Matt Jeanneret2101f3d2020-03-12 10:13:06 -04001379
1380 if not self.enabled:
1381 self.log.warn('device-not-activated')
1382 reactor.callLater(0.5, self.process_inter_adapter_message, request)
1383 return
1384
Matt Jeannereta32441c2019-03-07 05:16:37 -05001385 try:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001386
1387 update_onu_state = False
1388
Matt Jeannereta32441c2019-03-07 05:16:37 -05001389 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
1390 omci_msg = InterAdapterOmciMessage()
1391 request.body.Unpack(omci_msg)
Matteo Scandolod8d73172019-11-26 12:15:15 -07001392 self.log.debug('inter-adapter-recv-omci', omci_msg=hexify(omci_msg.message))
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001393
Matt Jeannereta32441c2019-03-07 05:16:37 -05001394 self.receive_message(omci_msg.message)
1395
1396 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
1397 onu_indication = OnuIndication()
1398 request.body.Unpack(onu_indication)
Matteo Scandolod8d73172019-11-26 12:15:15 -07001399 self.log.debug('inter-adapter-recv-onu-ind', onu_id=onu_indication.onu_id,
1400 oper_state=onu_indication.oper_state, admin_state=onu_indication.admin_state,
1401 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001402
Matt Jeanneret5e331892019-12-07 21:31:45 -05001403 update_onu_state = True
1404 self._onu_persisted_state['onu_id'] = onu_indication.onu_id
1405 self._onu_persisted_state['intf_id'] = onu_indication.intf_id
1406 self._onu_persisted_state['admin_state'] = onu_indication.admin_state
Mahir Gunyel45610b42020-03-16 17:29:01 -07001407 self._onu_persisted_state['oper_state'] = onu_indication.oper_state
Matt Jeanneret5e331892019-12-07 21:31:45 -05001408
Matt Jeannereta32441c2019-03-07 05:16:37 -05001409 if onu_indication.oper_state == "up":
Matt Jeanneret5e331892019-12-07 21:31:45 -05001410 yield self.create_interface(onu_indication)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301411 elif onu_indication.oper_state == "down" or onu_indication.oper_state == "unreachable":
Matt Jeanneret5e331892019-12-07 21:31:45 -05001412 yield self.update_interface(onu_indication)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001413 else:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001414 self.log.error("unknown-onu-indication", onu_id=onu_indication.onu_id,
1415 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001416
Matt Jeanneret3bfebff2019-04-12 18:25:03 -04001417 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
1418 tech_msg = InterAdapterTechProfileDownloadMessage()
1419 request.body.Unpack(tech_msg)
1420 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
1421
Matt Jeanneret5e331892019-12-07 21:31:45 -05001422 update_onu_state = self._update_onu_persisted_state(tech_msg.uni_id, tp_path=tech_msg.path)
1423 yield self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
Matt Jeanneret3bfebff2019-04-12 18:25:03 -04001424
Girish Gowdrae933cd32019-11-21 21:04:41 +05301425 elif request.header.type == InterAdapterMessageType.DELETE_GEM_PORT_REQUEST:
1426 del_gem_msg = InterAdapterDeleteGemPortMessage()
1427 request.body.Unpack(del_gem_msg)
1428 self.log.debug('inter-adapter-recv-del-gem', gem_del_msg=del_gem_msg)
Girish Gowdrac5117452020-08-03 11:20:53 -07001429 tp_id = self.extract_tp_id_from_path(del_gem_msg.tp_path)
1430 uni_id = del_gem_msg.uni_id
1431 gem_port = self._pon.get_gem_port(del_gem_msg.gem_port_id)
1432 self._tp_state_map_per_uni[uni_id][tp_id].queue_pending_delete_pon_resource(TpState.GEM_ID,
1433 gem_port)
Girish Gowdra322cca12020-08-09 15:55:54 -07001434 if self.is_device_active_and_reachable:
1435 self.delete_tech_profile(uni_id=del_gem_msg.uni_id,
1436 gem_port=gem_port,
1437 tp_path=del_gem_msg.tp_path)
1438 else:
1439 self.log.debug("device-unreachable--clearing-gem-id-from-local-cache")
1440 if tp_id in self._tp_state_map_per_uni[uni_id]:
1441 self._tp_state_map_per_uni[uni_id][tp_id].pon_resource_delete_complete(TpState.GEM_ID,
1442 gem_port.gem_id)
1443 self._clear_alloc_id_gem_port_from_internal_cache(None, gem_port.gem_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301444
1445 elif request.header.type == InterAdapterMessageType.DELETE_TCONT_REQUEST:
1446 del_tcont_msg = InterAdapterDeleteTcontMessage()
1447 request.body.Unpack(del_tcont_msg)
1448 self.log.debug('inter-adapter-recv-del-tcont', del_tcont_msg=del_tcont_msg)
1449
Matt Jeanneret5e331892019-12-07 21:31:45 -05001450 # Removal of the tcont/alloc id mapping represents the removal of the tech profile
1451 update_onu_state = self._update_onu_persisted_state(del_tcont_msg.uni_id, tp_path=None)
Girish Gowdrac5117452020-08-03 11:20:53 -07001452 tp_id = self.extract_tp_id_from_path(del_tcont_msg.tp_path)
1453 uni_id = del_tcont_msg.uni_id
1454 tcont = self._pon.get_tcont(del_tcont_msg.alloc_id)
1455 self._tp_state_map_per_uni[uni_id][tp_id].queue_pending_delete_pon_resource(TpState.ALLOC_ID,
1456 tcont)
Girish Gowdra322cca12020-08-09 15:55:54 -07001457 if self.is_device_active_and_reachable:
1458 self.delete_tech_profile(uni_id=del_tcont_msg.uni_id,
1459 tcont=tcont,
1460 tp_path=del_tcont_msg.tp_path)
1461 else:
1462 self.log.debug("device-unreachable--clearing-tcont-from-local-cache")
1463 if tp_id in self._tp_state_map_per_uni[uni_id]:
1464 self._tp_state_map_per_uni[uni_id][tp_id].pon_resource_delete_complete(TpState.ALLOC_ID,
1465 tcont.alloc_id)
1466 self._tp_state_map_per_uni[uni_id][tp_id].tp_setup_done = False
1467 self._clear_alloc_id_gem_port_from_internal_cache(tcont.alloc_id, None)
1468
Matt Jeannereta32441c2019-03-07 05:16:37 -05001469 else:
1470 self.log.error("inter-adapter-unhandled-type", request=request)
1471
Matt Jeanneret5e331892019-12-07 21:31:45 -05001472 if update_onu_state:
1473 try:
1474 self.log.debug('updating-onu-state', device_id=self.device_id,
1475 onu_persisted_state=self._onu_persisted_state)
1476 yield self.onu_kv_client.set(self.device_id, json.dumps(self._onu_persisted_state))
1477 except Exception as e:
1478 self.log.error('could-not-store-onu-state', device_id=self.device_id,
1479 onu_persisted_state=self._onu_persisted_state, e=e)
1480 # at this point omci is started and/or indications being processed
1481 # later indications may have a chance to write this state out again
1482
Matt Jeannereta32441c2019-03-07 05:16:37 -05001483 except Exception as e:
1484 self.log.exception("error-processing-inter-adapter-message", e=e)
1485
Matt Jeanneret5e331892019-12-07 21:31:45 -05001486 def _update_onu_persisted_state(self, uni_id, tp_path):
1487 # persist the uni and tech profile path for later reconciliation. update only if changed
1488 update_onu_state = False
1489 found = False
1490 for entry in self._onu_persisted_state.get('uni_config', list()):
1491 if entry.get('uni_id') == uni_id:
1492 found = True
1493 if entry.get('tp_path') != tp_path:
1494 update_onu_state = True
1495 entry['tp_path'] = tp_path
1496
1497 if not found:
1498 update_onu_state = True
1499 uni_tp = {
1500 'uni_id': uni_id,
1501 'tp_path': tp_path
1502 }
1503 self._onu_persisted_state['uni_config'].append(uni_tp)
1504
1505 return update_onu_state
1506
Matt Jeannereta32441c2019-03-07 05:16:37 -05001507 # Called each time there is an onu "up" indication from the olt handler
1508 @inlineCallbacks
1509 def create_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001510 self.log.info('create-interface', onu_id=onu_indication.onu_id,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001511 serial_number=onu_indication.serial_number)
Amit Ghosh028eb202020-02-17 13:34:00 +00001512
1513 # Ignore if onu_indication is received for an already running ONU
1514 if self._onu_omci_device is not None and self._onu_omci_device.active:
1515 self.log.warn('received-onu-indication-for-active-onu', onu_indication=onu_indication)
1516 return
1517
Matt Jeanneretc083f462019-03-11 15:02:01 -04001518 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
1519 connect_status=ConnectStatus.REACHABLE)
1520
Matt Jeannereta32441c2019-03-07 05:16:37 -05001521 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001522
1523 self.log.debug('starting-openomci-statemachine')
1524 self._subscribe_to_events()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001525 onu_device.reason = "starting-openomci"
Girish Gowdrae933cd32019-11-21 21:04:41 +05301526 reactor.callLater(1, self._onu_omci_device.start, onu_device)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001527 yield self.core_proxy.device_reason_update(self.device_id, onu_device.reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001528 self._heartbeat.enabled = True
1529
Matt Jeanneret42dad792020-02-01 09:28:27 -05001530 # Called each time there is an onu "down" indication from the olt handler
Matt Jeannereta32441c2019-03-07 05:16:37 -05001531 @inlineCallbacks
1532 def update_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001533 self.log.info('update-interface', onu_id=onu_indication.onu_id,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001534 serial_number=onu_indication.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001535
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -04001536 if onu_indication.oper_state == 'down' or onu_indication.oper_state == "unreachable":
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001537 self.log.debug('stopping-openomci-statemachine', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001538 reactor.callLater(0, self._onu_omci_device.stop)
1539
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001540 self._tp = dict()
1541
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001542 # Let TP download happen again
Girish Gowdrac5117452020-08-03 11:20:53 -07001543 for uni_id in self._tp_state_map_per_uni:
Girish Gowdra322cca12020-08-09 15:55:54 -07001544 for tp_id in self._tp_state_map_per_uni[uni_id]:
1545 self._tp_state_map_per_uni[uni_id][tp_id].tp_setup_done = False
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001546
Matt Jeanneretf4113222019-08-14 19:44:34 -04001547 yield self.disable_ports(lock_ports=False)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001548 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
1549 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.DISCOVERED,
1550 connect_status=ConnectStatus.UNREACHABLE)
Girish Gowdra322cca12020-08-09 15:55:54 -07001551 self.is_device_active_and_reachable = False
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001552 else:
1553 self.log.debug('not-changing-openomci-statemachine')
1554
Matt Jeanneretf4113222019-08-14 19:44:34 -04001555 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001556 def disable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001557 self.log.info('disable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001558 try:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001559 yield self.disable_ports(lock_ports=True, device_disabled=True)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001560 yield self.core_proxy.device_reason_update(self.device_id, "omci-admin-lock")
1561 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.UNKNOWN)
Girish Gowdra322cca12020-08-09 15:55:54 -07001562 self.is_device_active_and_reachable = False
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001563 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001564 self.log.exception('exception-in-onu-disable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001565
William Kurkian3a206332019-04-29 11:05:47 -04001566 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001567 def reenable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001568 self.log.info('reenable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001569 try:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001570 yield self.core_proxy.device_state_update(device.id,
1571 oper_status=OperStatus.ACTIVE,
1572 connect_status=ConnectStatus.REACHABLE)
Girish Gowdra322cca12020-08-09 15:55:54 -07001573 self.is_device_active_and_reachable = True
Matt Jeanneretf4113222019-08-14 19:44:34 -04001574 yield self.core_proxy.device_reason_update(self.device_id, 'onu-reenabled')
1575 yield self.enable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001576 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001577 self.log.exception('exception-in-onu-reenable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001578
William Kurkian3a206332019-04-29 11:05:47 -04001579 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001580 def reboot(self):
1581 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -04001582 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001583 if device.connect_status != ConnectStatus.REACHABLE:
1584 self.log.error("device-unreachable")
1585 return
1586
William Kurkian3a206332019-04-29 11:05:47 -04001587 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001588 def success(_results):
1589 self.log.info('reboot-success', _results=_results)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001590 yield self.core_proxy.device_reason_update(self.device_id, 'rebooting')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001591
1592 def failure(_reason):
1593 self.log.info('reboot-failure', _reason=_reason)
1594
1595 self._deferred = self._onu_omci_device.reboot()
1596 self._deferred.addCallbacks(success, failure)
1597
William Kurkian3a206332019-04-29 11:05:47 -04001598 @inlineCallbacks
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001599 def disable_ports(self, lock_ports=True, device_disabled=False):
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001600 self.log.info('disable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001601
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001602 # TODO: for now only support the first UNI given no requirement for multiple uni yet. Also needed to reduce flow
1603 # load on the core
Matt Jeanneretf4113222019-08-14 19:44:34 -04001604 for port in self.uni_ports:
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001605 if port.mac_bridge_port_num == 1:
1606 port.operstatus = OperStatus.UNKNOWN
1607 self.log.info('disable-port', device_id=self.device_id, port=port)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001608 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number,
1609 port.operstatus)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001610
1611 if lock_ports is True:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001612 self.lock_ports(lock=True, device_disabled=device_disabled)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001613
William Kurkian3a206332019-04-29 11:05:47 -04001614 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001615 def enable_ports(self):
1616 self.log.info('enable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001617
Matt Jeanneretf4113222019-08-14 19:44:34 -04001618 self.lock_ports(lock=False)
1619
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001620 # TODO: for now only support the first UNI given no requirement for multiple uni yet. Also needed to reduce flow
1621 # load on the core
1622 # Given by default all unis are initially active according to omci alarming, we must mimic this.
Matt Jeanneretf4113222019-08-14 19:44:34 -04001623 for port in self.uni_ports:
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001624 if port.mac_bridge_port_num == 1:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001625 port.operstatus = OperStatus.ACTIVE
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001626 self.log.info('enable-port', device_id=self.device_id, port=port)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001627 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number,
1628 port.operstatus)
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001629
1630 # TODO: Normally we would want any uni ethernet link down or uni ethernet link up alarms to register in the core,
1631 # but practically olt provisioning cannot handle the churn of links up, down, then up again typical on startup.
1632 #
1633 # Basically the link state sequence:
1634 # 1) per omci default alarm state, all unis are initially up (no link down alarms received yet)
1635 # 2) a link state down alarm is received for all uni, given the lock command, and also because most unis have nothing plugged in
1636 # 3) a link state up alarm is received for the uni plugged in.
1637 #
1638 # Given the olt (BAL) has to provision all uni, de-provision all uni, and re-provision one uni in quick succession
1639 # and cannot (bug?), we have to skip this and leave uni ports as assumed active. Also all the link state activity
1640 # would have a ripple effect through the core to the controller as well. And is it really worth it?
1641 '''
Matt Jeanneretf4113222019-08-14 19:44:34 -04001642 @inlineCallbacks
1643 def port_state_handler(self, _topic, msg):
1644 self.log.info("port-state-change", _topic=_topic, msg=msg)
1645
1646 onu_id = msg['onu_id']
1647 port_no = msg['port_number']
1648 serial_number = msg['serial_number']
1649 port_status = msg['port_status']
1650 uni_port = self.uni_port(int(port_no))
1651
1652 self.log.debug("port-state-parsed-message", onu_id=onu_id, port_no=port_no, serial_number=serial_number,
1653 port_status=port_status)
1654
1655 if port_status is True:
1656 uni_port.operstatus = OperStatus.ACTIVE
1657 self.log.info('link-up', device_id=self.device_id, port=uni_port)
1658 else:
1659 uni_port.operstatus = OperStatus.UNKNOWN
1660 self.log.info('link-down', device_id=self.device_id, port=uni_port)
1661
1662 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 -05001663 '''
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001664
1665 # Called just before openomci state machine is started. These listen for events from selected state machines,
1666 # most importantly, mib in sync. Which ultimately leads to downloading the mib
1667 def _subscribe_to_events(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001668 self.log.debug('subscribe-to-events')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001669
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001670 bus = self._onu_omci_device.event_bus
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001671
1672 # OMCI MIB Database sync status
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001673 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1674 OnuDeviceEvents.MibDatabaseSyncEvent)
1675 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
1676
1677 # OMCI Capabilities
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001678 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1679 OnuDeviceEvents.OmciCapabilitiesEvent)
1680 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
1681
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001682 # TODO: these alarms seem to be unreliable depending on the environment
1683 # 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 -08001684 # topic = OnuDeviceEntry.event_bus_topic(self.device_id,
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001685 # OnuDeviceEvents.PortEvent)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001686 # self._port_state_subscription = bus.subscribe(topic, self.port_state_handler)
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001687
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001688 # Called when the mib is in sync
1689 def in_sync_handler(self, _topic, msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001690 self.log.debug('in-sync-handler', _topic=_topic, msg=msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001691 if self._in_sync_subscription is not None:
1692 try:
1693 in_sync = msg[IN_SYNC_KEY]
1694
1695 if in_sync:
1696 # Only call this once
1697 bus = self._onu_omci_device.event_bus
1698 bus.unsubscribe(self._in_sync_subscription)
1699 self._in_sync_subscription = None
1700
1701 # Start up device_info load
1702 self.log.debug('running-mib-sync')
1703 reactor.callLater(0, self._mib_in_sync)
1704
1705 except Exception as e:
1706 self.log.exception('in-sync', e=e)
1707
1708 def capabilties_handler(self, _topic, _msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001709 self.log.debug('capabilities-handler', _topic=_topic, msg=_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001710 if self._capabilities_subscription is not None:
1711 self.log.debug('capabilities-handler-done')
1712
1713 # 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 -04001714 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001715 def _mib_in_sync(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001716 self.log.debug('mib-in-sync')
Matt Jeanneretc083f462019-03-11 15:02:01 -04001717 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001718
Matt Jeanneret5e331892019-12-07 21:31:45 -05001719 # only notify core if this is a new device. otherwise do not have reconcile generating
1720 # a lot of needless message churn
1721 if not self._reconciling:
1722 yield self.core_proxy.device_reason_update(self.device_id, 'discovery-mibsync-complete')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001723
1724 if self._dev_info_loaded:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001725 self.log.debug('device-info-already-loaded')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001726 else:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001727 # new onu or adapter was restarted. fill up our local data
1728 yield self._load_device_data(device)
1729
1730 if self._check_mib_downloaded():
1731 self.log.debug('mib-already-downloaded')
1732 if not self._reconciling:
1733 yield self.core_proxy.device_state_update(device.id,
1734 oper_status=OperStatus.ACTIVE,
1735 connect_status=ConnectStatus.REACHABLE)
Girish Gowdra322cca12020-08-09 15:55:54 -07001736 self.is_device_active_and_reachable = True
Matt Jeanneret5e331892019-12-07 21:31:45 -05001737 yield self.enable_ports()
1738 else:
1739 self._download_mib(device)
1740
1741 if self._reconciling:
1742 yield self._restore_tech_profile()
1743 self._start_monitoring()
1744 self._reconciling = False
1745 self.log.debug('reconcile-finished')
1746
1747 def _download_mib(self, device):
1748 self.log.debug('downloading-initial-mib-configuration')
1749
1750 @inlineCallbacks
1751 def success(_results):
1752 self.log.debug('mib-download-success', _results=_results)
1753 yield self.core_proxy.device_state_update(device.id,
1754 oper_status=OperStatus.ACTIVE,
1755 connect_status=ConnectStatus.REACHABLE)
Girish Gowdra322cca12020-08-09 15:55:54 -07001756 self.is_device_active_and_reachable = True
Matt Jeanneret5e331892019-12-07 21:31:45 -05001757 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-downloaded')
1758 self._mib_download_task = None
1759 yield self.enable_ports()
1760 yield self.onu_active_event()
1761 self._start_monitoring()
1762
1763 @inlineCallbacks
1764 def failure(_reason):
1765 self.log.warn('mib-download-failure-retrying', _reason=_reason)
1766 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1767 reactor.callLater(retry, self._mib_in_sync)
1768 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-download-failure-retrying')
1769
1770 # start by locking all the unis till mib sync and initial mib is downloaded
1771 # this way we can capture the port down/up events when we are ready
1772 self.lock_ports(lock=True)
1773
1774 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1775 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1776 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1777 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1778 self._deferred.addCallbacks(success, failure)
1779
1780 def _start_monitoring(self):
1781 self.log.debug('starting-monitoring')
1782
1783 # Start collecting stats from the device after a brief pause
1784 if not self._pm_metrics_started:
1785 self._pm_metrics_started = True
Rohan Agrawal36a4e442020-06-29 11:10:32 +00001786 pmstart = _STARTUP_RETRY_WAIT * (random.randint(1, self._pm_metrics.max_skew))
Matt Jeanneret5e331892019-12-07 21:31:45 -05001787 reactor.callLater(pmstart, self._pm_metrics.start_collector)
1788
1789 # Start test requests after a brief pause
1790 if not self._test_request_started:
1791 self._test_request_started = True
1792 tststart = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1793 reactor.callLater(tststart, self._test_request.start_collector)
1794
1795 def _check_mib_downloaded(self):
1796 self.log.debug('checking-mib-downloaded')
1797 results = False
1798
1799 mac_bridges = self.onu_omci_device.query_mib(MacBridgeServiceProfile.class_id)
1800 self.log.debug('mac-bridges', mac_bridges=mac_bridges)
1801
1802 for k, v in mac_bridges.items():
1803 if not isinstance(v, dict):
1804 continue
1805 # found at least one mac bridge, good enough to say its done, break out
1806 self.log.debug('found-mac-bridge-mib-download-has-been-done', omci_key=k, omci_value=v)
1807 results = True
1808 break
1809
1810 return results
1811
1812 @inlineCallbacks
1813 def _load_device_data(self, device):
1814 self.log.debug('loading-device-data-from-mib', device_id=device.id)
1815
1816 omci_dev = self._onu_omci_device
1817 config = omci_dev.configuration
1818
1819 try:
1820 # sort the lists so we get consistent port ordering.
1821 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
1822 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
1823 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
1824 veip_list = sorted(config.veip_entities) if config.veip_entities else []
1825
1826 if ani_list is None or (pptp_list is None and veip_list is None):
1827 yield self.core_proxy.device_reason_update(self.device_id, 'onu-missing-required-elements')
1828 raise Exception("onu-missing-required-elements")
1829
1830 # Currently logging the ani, pptp, veip, and uni for information purposes.
1831 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
1832 # And in some ONU the UNI-G list is incomplete or incorrect...
1833 for entity_id in ani_list:
1834 ani_value = config.ani_g_entities[entity_id]
1835 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
1836
1837 for entity_id in uni_list:
1838 uni_value = config.uni_g_entities[entity_id]
1839 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
1840
1841 uni_entities = OrderedDict()
1842 for entity_id in pptp_list:
1843 pptp_value = config.pptp_entities[entity_id]
1844 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
1845 uni_entities[entity_id] = UniType.PPTP
1846
1847 for entity_id in veip_list:
1848 veip_value = config.veip_entities[entity_id]
1849 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
1850 uni_entities[entity_id] = UniType.VEIP
1851
1852 uni_id = 0
1853 for entity_id, uni_type in uni_entities.items():
1854 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
Girish Gowdrac5117452020-08-03 11:20:53 -07001855 self._tp_state_map_per_uni[uni_id] = dict()
Matt Jeanneret5e331892019-12-07 21:31:45 -05001856 uni_id += 1
1857
1858 if self._unis:
1859 self._dev_info_loaded = True
1860 else:
1861 yield self.core_proxy.device_reason_update(self.device_id, 'no-usable-unis')
1862 raise Exception("no-usable-unis")
1863
1864 except Exception as e:
1865 self.log.exception('device-info-load', e=e)
1866 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001867
Matt Jeanneretc083f462019-03-11 15:02:01 -04001868 @inlineCallbacks
1869 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matt Jeanneret5e331892019-12-07 21:31:45 -05001870 self.log.debug('add-uni-port', entity_id=entity_id, uni_id=uni_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001871
Matt Jeanneret5e331892019-12-07 21:31:45 -05001872 intf_id = self._onu_persisted_state.get('intf_id')
1873 onu_id = self._onu_persisted_state.get('onu_id')
1874 uni_no = self.mk_uni_port_num(intf_id, onu_id, uni_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001875
1876 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1877 uni_name = "uni-{}".format(uni_no)
1878
Girish Gowdrae933cd32019-11-21 21:04:41 +05301879 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001880
1881 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 -04001882 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001883
Girish Gowdra5b499342020-06-16 14:45:51 -07001884 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name,
1885 device.parent_port_no, device.serial_number,
1886 uni_type,)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001887 uni_port.entity_id = entity_id
1888 uni_port.enabled = True
1889 uni_port.mac_bridge_port_num = mac_bridge_port_num
1890
1891 self.log.debug("created-uni-port", uni=uni_port)
1892
Matt Jeanneret5e331892019-12-07 21:31:45 -05001893 if not self._reconciling:
1894 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001895
1896 self._unis[uni_port.port_number] = uni_port
1897
Matt Jeanneret5e331892019-12-07 21:31:45 -05001898 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=onu_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301899 uni_ports=self.uni_ports,
1900 serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001901
Matt Jeanneret5e331892019-12-07 21:31:45 -05001902 @inlineCallbacks
1903 def _restore_tech_profile(self):
1904 self.log.debug("reconcile-restoring-tech-profile-tcont-gem-config")
1905
1906 # for every uni that has tech profile config reload all its tcont/alloc_id and gem from the tp path
1907 for entry in self._onu_persisted_state.get('uni_config', list()):
1908 uni_id = entry.get('uni_id')
1909 tp_path = entry.get('tp_path')
1910 if tp_path:
1911 tpstored = yield self.tp_kv_client.get(tp_path)
1912 tpstring = tpstored.decode('ascii')
1913 tp = json.loads(tpstring)
1914
1915 self.log.debug("restoring-tp-instance", tp=tp)
1916
1917 # re-run tech profile config that stores gem and tconts in the self._pon object
1918 # this does not actually re-run the omci, just rebuilds our local data store
1919 self._do_tech_profile_configuration(uni_id, tp)
1920
1921 tp_id = self.extract_tp_id_from_path(tp_path)
1922
1923 # rebuild cache dicts so tp updates and deletes dont get KeyErrors
Girish Gowdrac5117452020-08-03 11:20:53 -07001924 if uni_id not in self._tp_state_map_per_uni:
1925 self._tp_state_map_per_uni[uni_id] = dict()
Matt Jeanneret5e331892019-12-07 21:31:45 -05001926
Girish Gowdrac5117452020-08-03 11:20:53 -07001927 if tp_id not in self._tp_state_map_per_uni[uni_id]:
1928 self._tp_state_map_per_uni[uni_id][tp_id] = TpState(self, uni_id, tp_path)
Matt Jeanneret5e331892019-12-07 21:31:45 -05001929
Girish Gowdrac5117452020-08-03 11:20:53 -07001930 self._tp_state_map_per_uni[uni_id][tp_id].tp_setup_done = True
Matt Jeanneret5e331892019-12-07 21:31:45 -05001931 else:
1932 self.log.debug("no-assigned-tp-instance", uni_id=uni_id)
1933
1934 # for every loaded tcont from tp check the mib database for its entity_id
1935 # needed for later tp deletes/adds
1936 tcont_idents = self.onu_omci_device.query_mib(Tcont.class_id)
1937 self.log.debug('tcont-idents', tcont_idents=tcont_idents)
1938
1939 for k, v in tcont_idents.items():
1940 if not isinstance(v, dict):
1941 continue
1942 alloc_check = v.get('attributes', {}).get('alloc_id', 0)
1943 tcont = self._pon.tconts.get(alloc_check)
1944 if tcont:
1945 tcont.entity_id = k
1946 self.log.debug('reassigning-tcont-entity-id', entity_id=tcont.entity_id,
1947 alloc_id=tcont.alloc_id)
1948
Matt Jeanneretc083f462019-03-11 15:02:01 -04001949 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1950 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
Amit Ghosh65400f12019-11-21 12:04:12 +00001951 MAX_PONS_PER_OLT = 256
1952 MAX_ONUS_PER_PON = 256
Matt Jeanneretc083f462019-03-11 15:02:01 -04001953 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001954
Matt Jeanneretc083f462019-03-11 15:02:01 -04001955 assert intf_id < MAX_PONS_PER_OLT
1956 assert onu_id < MAX_ONUS_PER_PON
1957 assert uni_id < MAX_UNIS_PER_ONU
Amit Ghosh65400f12019-11-21 12:04:12 +00001958 return intf_id << 12 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001959
1960 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001961 def onu_active_event(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001962 self.log.debug('onu-active-event')
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001963 try:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001964 # TODO: this is expensive for just getting the olt serial number. replace with direct api call
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001965 parent_device = yield self.core_proxy.get_device(self.parent_id)
1966 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001967 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001968
Matt Jeanneret5e331892019-12-07 21:31:45 -05001969 intf_id = self._onu_persisted_state.get('intf_id')
1970 onu_id = self._onu_persisted_state.get('onu_id')
1971 onu_serial = self._onu_persisted_state.get('serial_number')
1972
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001973 self.log.debug("onu-indication-context-data",
Matt Jeanneret5e331892019-12-07 21:31:45 -05001974 pon_id=intf_id,
1975 onu_id=onu_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301976 registration_id=self.device_id,
1977 device_id=self.device_id,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001978 onu_serial_number=onu_serial,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301979 olt_serial_number=olt_serial_number,
1980 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001981
Devmalya Paulffc89df2019-07-31 17:43:13 -04001982 self.log.debug("Trying-to-raise-onu-active-event")
1983 OnuActiveEvent(self.events, self.device_id,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001984 intf_id,
1985 onu_serial,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001986 str(self.device_id),
Girish Gowdrae933cd32019-11-21 21:04:41 +05301987 olt_serial_number, raised_ts,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001988 onu_id=onu_id).send(True)
Devmalya Paulffc89df2019-07-31 17:43:13 -04001989 except Exception as active_event_error:
1990 self.log.exception('onu-activated-event-error',
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001991 errmsg=active_event_error)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001992
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001993 @inlineCallbacks
1994 def onu_disabled_event(self):
1995 self.log.debug('onu-disabled-event')
1996 try:
1997 device = yield self.core_proxy.get_device(self.device_id)
1998 parent_device = yield self.core_proxy.get_device(self.parent_id)
1999 olt_serial_number = parent_device.serial_number
2000 raised_ts = arrow.utcnow().timestamp
Devmalya Paul1e1b1722020-05-07 02:51:15 -04002001 intf_id = self._onu_persisted_state.get('intf_id')
2002 onu_id = self._onu_persisted_state.get('onu_id')
2003 onu_serial = self._onu_persisted_state.get('serial_number')
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04002004
2005 self.log.debug("onu-indication-context-data",
Devmalya Paul1e1b1722020-05-07 02:51:15 -04002006 pon_id=intf_id,
2007 onu_id=onu_id,
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04002008 registration_id=self.device_id,
2009 device_id=self.device_id,
Devmalya Paul1e1b1722020-05-07 02:51:15 -04002010 onu_serial_number=onu_serial,
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04002011 olt_serial_number=olt_serial_number,
2012 raised_ts=raised_ts)
2013
2014 self.log.debug("Trying-to-raise-onu-disabled-event")
2015 OnuDisabledEvent(self.events, self.device_id,
Devmalya Paul1e1b1722020-05-07 02:51:15 -04002016 intf_id,
Girish Gowdradc98d812020-03-20 13:04:58 -07002017 device.serial_number,
2018 str(self.device_id),
2019 olt_serial_number, raised_ts,
Devmalya Paul1e1b1722020-05-07 02:51:15 -04002020 onu_id=onu_id).send(True)
2021 except Exception as disable_event_error:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04002022 self.log.exception('onu-disabled-event-error',
Devmalya Paul1e1b1722020-05-07 02:51:15 -04002023 errmsg=disable_event_error)
2024
2025 @inlineCallbacks
2026 def onu_deleted_event(self):
2027 self.log.debug('onu-deleted-event')
2028 try:
2029 device = yield self.core_proxy.get_device(self.device_id)
2030 parent_device = yield self.core_proxy.get_device(self.parent_id)
2031 olt_serial_number = parent_device.serial_number
2032 raised_ts = arrow.utcnow().timestamp
2033 intf_id = self._onu_persisted_state.get('intf_id')
2034 onu_id = self._onu_persisted_state.get('onu_id')
2035 serial_number = self._onu_persisted_state.get('serial_number')
2036
2037 self.log.debug("onu-deleted-event-context-data",
2038 pon_id=intf_id,
2039 onu_id=onu_id,
2040 registration_id=self.device_id,
2041 device_id=self.device_id,
2042 onu_serial_number=serial_number,
2043 olt_serial_number=olt_serial_number,
2044 raised_ts=raised_ts)
2045
2046 OnuDeletedEvent(self.events, self.device_id,
2047 intf_id,
2048 serial_number,
2049 str(self.device_id),
2050 olt_serial_number, raised_ts,
2051 onu_id=onu_id).send(True)
2052 except Exception as deleted_event_error:
2053 self.log.exception('onu-deleted-event-error',
2054 errmsg=deleted_event_error)
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04002055
2056 def lock_ports(self, lock=True, device_disabled=False):
Matt Jeanneretf4113222019-08-14 19:44:34 -04002057
2058 def success(response):
2059 self.log.debug('set-onu-ports-state', lock=lock, response=response)
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04002060 if device_disabled:
2061 self.onu_disabled_event()
Matt Jeanneretf4113222019-08-14 19:44:34 -04002062
2063 def failure(response):
2064 self.log.error('cannot-set-onu-ports-state', lock=lock, response=response)
2065
2066 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=lock)
2067 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
2068 self._deferred.addCallbacks(success, failure)
Mahir Gunyele9110a32020-02-20 14:56:50 -08002069
2070 def extract_tp_id_from_path(self, tp_path):
2071 # tp_path is of the format <technology>/<table_id>/<uni_port_name>
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08002072 tp_id = int(tp_path.split(_PATH_SEPERATOR)[1])
2073 return tp_id
onkarkundargia1e2af22020-01-27 11:51:43 +05302074
2075 def start_omci_test_action(self, device, uuid):
2076 """
2077
2078 :param device:
2079 :return:
2080 """
2081 # Code to Run OMCI Test Action
2082 self.log.info('Omci-test-action-request-On', request=device.id)
2083 kwargs_omci_test_action = {
2084 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
2085 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
2086 }
2087 serial_number = device.serial_number
2088 if device.connect_status != ConnectStatus.REACHABLE or device.admin_state != AdminState.ENABLED:
2089 return (TestResponse(result=TestResponse.FAILURE))
2090 test_request = OmciTestRequest(self.core_proxy,
2091 self.omci_agent, self.device_id, AniG,
2092 serial_number,
2093 self.logical_device_id, exclusive=False,
2094 uuid=uuid,
2095 **kwargs_omci_test_action)
2096 test_request.perform_test_omci()
2097 return (TestResponse(result=TestResponse.SUCCESS))