blob: 1eecd7a54341d34ec66ab2ce045fcdefdb3cc6a9 [file] [log] [blame]
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001#
2# Copyright 2017 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17"""
18Broadcom OpenOMCI OLT/ONU adapter handler.
19"""
20
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050021from __future__ import absolute_import
22import six
Devmalya Paulffc89df2019-07-31 17:43:13 -040023import arrow
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050024import structlog
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050025import json
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050026import random
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050027
28from collections import OrderedDict
29
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050030from twisted.internet import reactor
Matt Jeanneret5e331892019-12-07 21:31:45 -050031from twisted.internet.defer import inlineCallbacks, returnValue
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050032
33from heartbeat import HeartBeat
Devmalya Paulffc89df2019-07-31 17:43:13 -040034from pyvoltha.adapters.extensions.events.device_events.onu.onu_active_event import OnuActiveEvent
Devmalya Paule2e5f2b2020-03-08 18:50:33 -040035from pyvoltha.adapters.extensions.events.device_events.onu.onu_disabled_event import OnuDisabledEvent
Devmalya Paul1e1b1722020-05-07 02:51:15 -040036from pyvoltha.adapters.extensions.events.device_events.onu.onu_deleted_event import OnuDeletedEvent
Devmalya Paulffc89df2019-07-31 17:43:13 -040037from pyvoltha.adapters.extensions.events.kpi.onu.onu_pm_metrics import OnuPmMetrics
38from pyvoltha.adapters.extensions.events.kpi.onu.onu_omci_pm import OnuOmciPmMetrics
39from pyvoltha.adapters.extensions.events.adapter_events import AdapterEvents
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050040
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050041import pyvoltha.common.openflow.utils as fd
42from pyvoltha.common.utils.registry import registry
Matteo Scandolod8d73172019-11-26 12:15:15 -070043from pyvoltha.adapters.common.frameio.frameio import hexify
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050044from pyvoltha.common.utils.nethelpers import mac_str_to_tuple
Matt Jeanneret5e331892019-12-07 21:31:45 -050045from pyvoltha.adapters.common.kvstore.twisted_etcd_store import TwistedEtcdStore
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050046from voltha_protos.logical_device_pb2 import LogicalPort
William Kurkian8235c1e2019-03-05 12:58:28 -050047from voltha_protos.common_pb2 import OperStatus, ConnectStatus, AdminState
Matt Jeanneretf4113222019-08-14 19:44:34 -040048from voltha_protos.device_pb2 import Port
Girish Gowdra4c11ddb2020-03-03 11:33:24 -080049from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, ofp_port, OFPPS_LIVE, OFPPS_LINK_DOWN, \
Matt Jeanneretf4113222019-08-14 19:44:34 -040050 OFPPF_FIBER, OFPPF_1GB_FD
Matt Jeanneret3bfebff2019-04-12 18:25:03 -040051from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
Girish Gowdrae933cd32019-11-21 21:04:41 +053052 InterAdapterOmciMessage, PortCapability, InterAdapterTechProfileDownloadMessage, InterAdapterDeleteGemPortMessage, \
53 InterAdapterDeleteTcontMessage
Matt Jeannereta32441c2019-03-07 05:16:37 -050054from voltha_protos.openolt_pb2 import OnuIndication
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050055from pyvoltha.adapters.extensions.omci.onu_device_entry import OnuDeviceEvents, \
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050056 OnuDeviceEntry, IN_SYNC_KEY
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050057from omci.brcm_mib_download_task import BrcmMibDownloadTask
Girish Gowdrae933cd32019-11-21 21:04:41 +053058from omci.brcm_tp_setup_task import BrcmTpSetupTask
59from omci.brcm_tp_delete_task import BrcmTpDeleteTask
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050060from omci.brcm_uni_lock_task import BrcmUniLockTask
61from omci.brcm_vlan_filter_task import BrcmVlanFilterTask
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050062from onu_gem_port import OnuGemPort
63from onu_tcont import OnuTCont
64from pon_port import PonPort
Mahir Gunyel5de33fe2020-03-03 22:38:44 -080065from omci.brcm_mcast_task import BrcmMcastTask
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050066from uni_port import UniPort, UniType
Andrea Campanellacf916ea2020-02-14 10:03:58 +010067from uni_port import RESERVED_TRANSPARENT_VLAN
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050068from pyvoltha.common.tech_profile.tech_profile import TechProfile
onkarkundargiaae99712019-09-23 15:02:52 +053069from pyvoltha.adapters.extensions.omci.tasks.omci_test_request import OmciTestRequest
Matt Jeanneret5e331892019-12-07 21:31:45 -050070from pyvoltha.adapters.extensions.omci.omci_entities import AniG, Tcont, MacBridgeServiceProfile
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050071from pyvoltha.adapters.extensions.omci.omci_defs import EntityOperations, ReasonCodes
onkarkundargia1e2af22020-01-27 11:51:43 +053072from voltha_protos.voltha_pb2 import TestResponse
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050073
74OP = EntityOperations
75RC = ReasonCodes
76
Girish Gowdradc98d812020-03-20 13:04:58 -070077IS_MULTICAST = 'is_multicast'
Mahir Gunyel5de33fe2020-03-03 22:38:44 -080078GEM_PORT_ID = 'gemport_id'
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050079_STARTUP_RETRY_WAIT = 10
Mahir Gunyele9110a32020-02-20 14:56:50 -080080_PATH_SEPERATOR = "/"
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050081
82
83class BrcmOpenomciOnuHandler(object):
84
85 def __init__(self, adapter, device_id):
86 self.log = structlog.get_logger(device_id=device_id)
Matt Jeanneret08a8e862019-12-20 14:02:32 -050087 self.log.debug('starting-handler')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050088 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050089 self.core_proxy = adapter.core_proxy
90 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050091 self.parent_id = None
92 self.device_id = device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050093 self.proxy_address = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050094 self._enabled = False
Devmalya Paulffc89df2019-07-31 17:43:13 -040095 self.events = None
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050096 self._pm_metrics = None
97 self._pm_metrics_started = False
98 self._test_request = None
99 self._test_request_started = False
Girish Gowdradc98d812020-03-20 13:04:58 -0700100 self._tp = dict() # tp_id -> technology profile definition in KV Store.
Matt Jeanneret5e331892019-12-07 21:31:45 -0500101 self._reconciling = False
102
103 # Persisted onu configuration needed in case of reconciliation.
104 self._onu_persisted_state = {
105 'onu_id': None,
106 'intf_id': None,
107 'serial_number': None,
108 'admin_state': None,
109 'oper_state': None,
110 'uni_config': list()
111 }
112
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500113 self._unis = dict() # Port # -> UniPort
114
115 self._pon = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500116 self._pon_port_number = 100
117 self.logical_device_id = None
118
119 self._heartbeat = HeartBeat.create(self, device_id)
120
121 # Set up OpenOMCI environment
122 self._onu_omci_device = None
123 self._dev_info_loaded = False
124 self._deferred = None
125
126 self._in_sync_subscription = None
Matt Jeanneretf4113222019-08-14 19:44:34 -0400127 self._port_state_subscription = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500128 self._connectivity_subscription = None
129 self._capabilities_subscription = None
130
131 self.mac_bridge_service_profile_entity_id = 0x201
132 self.gal_enet_profile_entity_id = 0x1
133
134 self._tp_service_specific_task = dict()
135 self._tech_profile_download_done = dict()
Girish Gowdradc98d812020-03-20 13:04:58 -0700136
137 # When the vlan filter is being removed for a given TP ID on a given UNI,
138 # mark that we are expecting a tp delete to happen for this UNI.
139 # Unless the TP delete is complete to not allow new vlan add tasks to this TP ID
140 self._pending_delete_tp = dict()
141
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400142 # Stores information related to queued vlan filter tasks
143 # 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 -0400144 self._queued_vlan_filter_task = dict()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500145
Girish Gowdradc98d812020-03-20 13:04:58 -0700146 self._set_vlan = dict() # uni_id, tp_id -> set_vlan_id
Matt Jeanneret5e331892019-12-07 21:31:45 -0500147
148 # Paths from kv store
149 ONU_PATH = 'service/voltha/openonu'
150
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500151 # Initialize KV store client
152 self.args = registry('main').get_args()
Matt Jeanneret5e331892019-12-07 21:31:45 -0500153 host, port = self.args.etcd.split(':', 1)
154 self.tp_kv_client = TwistedEtcdStore(host, port, TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
155 self.onu_kv_client = TwistedEtcdStore(host, port, ONU_PATH)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500156
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500157 @property
158 def enabled(self):
159 return self._enabled
160
161 @enabled.setter
162 def enabled(self, value):
163 if self._enabled != value:
164 self._enabled = value
165
166 @property
167 def omci_agent(self):
168 return self.adapter.omci_agent
169
170 @property
171 def omci_cc(self):
172 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
173
174 @property
175 def heartbeat(self):
176 return self._heartbeat
177
178 @property
179 def uni_ports(self):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500180 return list(self._unis.values())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500181
182 def uni_port(self, port_no_or_name):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500183 if isinstance(port_no_or_name, six.string_types):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500184 return next((uni for uni in self.uni_ports
185 if uni.name == port_no_or_name), None)
186
187 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
188 return next((uni for uni in self.uni_ports
Girish Gowdrae933cd32019-11-21 21:04:41 +0530189 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500190
191 @property
192 def pon_port(self):
193 return self._pon
194
Girish Gowdraa73ee452019-12-20 18:52:17 +0530195 @property
196 def onu_omci_device(self):
197 return self._onu_omci_device
198
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500199 def receive_message(self, msg):
200 if self.omci_cc is not None:
201 self.omci_cc.receive_message(msg)
202
Matt Jeanneretc083f462019-03-11 15:02:01 -0400203 def get_ofp_port_info(self, device, port_no):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500204 self.log.debug('get-ofp-port-info', port_no=port_no, device_id=device.id)
Matt Jeanneretc083f462019-03-11 15:02:01 -0400205 cap = OFPPF_1GB_FD | OFPPF_FIBER
206
Girish Gowdrae933cd32019-11-21 21:04:41 +0530207 hw_addr = mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
208 ((device.parent_port_no >> 8 & 0xff),
209 device.parent_port_no & 0xff,
210 (port_no >> 16) & 0xff,
211 (port_no >> 8) & 0xff,
212 port_no & 0xff))
Matt Jeanneretc083f462019-03-11 15:02:01 -0400213
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400214 uni_port = self.uni_port(int(port_no))
215 name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500216 self.log.debug('ofp-port-name', port_no=port_no, name=name, uni_port=uni_port)
Matt Jeanneretf4113222019-08-14 19:44:34 -0400217
218 ofstate = OFPPS_LINK_DOWN
219 if uni_port.operstatus is OperStatus.ACTIVE:
220 ofstate = OFPPS_LIVE
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400221
Matt Jeanneretc083f462019-03-11 15:02:01 -0400222 return PortCapability(
223 port=LogicalPort(
224 ofp_port=ofp_port(
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400225 name=name,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400226 hw_addr=hw_addr,
227 config=0,
Matt Jeanneretf4113222019-08-14 19:44:34 -0400228 state=ofstate,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400229 curr=cap,
230 advertised=cap,
231 peer=cap,
232 curr_speed=OFPPF_1GB_FD,
233 max_speed=OFPPF_1GB_FD
234 ),
235 device_id=device.id,
236 device_port_no=port_no
237 )
238 )
239
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500240 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500241 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500242 def activate(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700243 self.log.debug('activate-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500244
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500245 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500246 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500247 assert device.proxy_address.device_id
248
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500249 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500250 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500251 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500252 if self.enabled is not True:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700253 self.log.info('activating-new-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500254 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500255 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500256 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500257 device.reason = 'activating-onu'
258
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500259 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400260 # pm_metrics requires a logical device id. For now set to just device_id
261 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500262
Matt Jeanneret5e331892019-12-07 21:31:45 -0500263 self._onu_persisted_state['serial_number'] = device.serial_number
264 try:
265 self.log.debug('updating-onu-state', device_id=self.device_id,
266 onu_persisted_state=self._onu_persisted_state)
267 yield self.onu_kv_client.set(self.device_id, json.dumps(self._onu_persisted_state))
268 except Exception as e:
269 self.log.error('could-not-store-onu-state', device_id=self.device_id,
270 onu_persisted_state=self._onu_persisted_state, e=e)
271 # if we cannot write to storage we can proceed, for now.
272 # later onu indications from the olt will have another chance
273
Matt Jeannereta32441c2019-03-07 05:16:37 -0500274 yield self.core_proxy.device_update(device)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500275 self.log.debug('device-updated', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500276
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700277 yield self._init_pon_state()
Matteo Scandolod8d73172019-11-26 12:15:15 -0700278 self.log.debug('pon state initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500279
Matt Jeanneret5e331892019-12-07 21:31:45 -0500280 yield self._init_metrics()
281 self.log.debug('metrics initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500282
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500283 self.enabled = True
284 else:
285 self.log.info('onu-already-activated')
286
287 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400288 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500289 def reconcile(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700290 self.log.debug('reconcile-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500291
Matt Jeanneret5e331892019-12-07 21:31:45 -0500292 if self._reconciling:
293 self.log.debug('already-running-reconcile-device', device_id=device.id, serial_number=device.serial_number)
294 return
295
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500296 # first we verify that we got parent reference and proxy info
297 assert device.parent_id
298 assert device.proxy_address.device_id
299
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700300 self.proxy_address = device.proxy_address
301 self.parent_id = device.parent_id
302 self._pon_port_number = device.parent_port_no
303
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500304 if self.enabled is not True:
Matt Jeanneret5e331892019-12-07 21:31:45 -0500305 self._reconciling = True
306 self.log.info('reconciling-openonu-device')
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700307 self.logical_device_id = self.device_id
Matt Jeanneret5e331892019-12-07 21:31:45 -0500308
309 try:
310 query_data = yield self.onu_kv_client.get(device.id)
311 self._onu_persisted_state = json.loads(query_data)
312 self.log.debug('restored-onu-state', device_id=self.device_id,
313 onu_persisted_state=self._onu_persisted_state)
314 except Exception as e:
315 self.log.error('no-stored-onu-state', device_id=device.id, e=e)
316 # there is nothing we can do without data. flag the device as UNKNOWN and cannot reconcile
317 # likely it will take manual steps to delete/re-add this onu
318 yield self.core_proxy.device_reason_update(self.device_id, "cannot-reconcile")
319 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.UNKNOWN)
320 return
321
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700322 self._init_pon_state()
Matt Jeanneret5e331892019-12-07 21:31:45 -0500323 self.log.debug('pon state initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500324
Matt Jeanneret5e331892019-12-07 21:31:45 -0500325 self._init_metrics()
326 self.log.debug('metrics initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500327
Matt Jeanneret5e331892019-12-07 21:31:45 -0500328 self._subscribe_to_events()
329 # need to restart omci start machines and reload mib database. once db is loaded we can finish reconcile
330 self._onu_omci_device.start(device)
331 self._heartbeat.enabled = True
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500332
333 self.enabled = True
334 else:
335 self.log.info('onu-already-activated')
336
337 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700338 def _init_pon_state(self):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500339 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 -0500340
341 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500342 self._pon.add_peer(self.parent_id, self._pon_port_number)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700343 self.log.debug('adding-pon-port-to-agent',
344 type=self._pon.get_port().type,
345 admin_state=self._pon.get_port().admin_state,
346 oper_status=self._pon.get_port().oper_status,
347 )
Matt Jeanneret0c287892019-02-28 11:48:00 -0500348
Matt Jeanneret5e331892019-12-07 21:31:45 -0500349 if not self._reconciling:
350 yield self.core_proxy.port_created(self.device_id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500351
Matteo Scandolod8d73172019-11-26 12:15:15 -0700352 self.log.debug('added-pon-port-to-agent',
353 type=self._pon.get_port().type,
354 admin_state=self._pon.get_port().admin_state,
355 oper_status=self._pon.get_port().oper_status,
356 )
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500357
358 # Create and start the OpenOMCI ONU Device Entry for this ONU
359 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500360 self.core_proxy,
361 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500362 support_classes=self.adapter.broadcom_omci,
363 custom_me_map=self.adapter.custom_me_entities())
364 # Port startup
365 if self._pon is not None:
366 self._pon.enabled = True
367
Matt Jeanneret5e331892019-12-07 21:31:45 -0500368 @inlineCallbacks
369 def _init_metrics(self):
370 self.log.debug('init-metrics', device_id=self.device_id, device_logical_id=self.logical_device_id)
371
372 serial_number = self._onu_persisted_state.get('serial_number')
373
374 ############################################################################
375 # Setup Alarm handler
376 self.events = AdapterEvents(self.core_proxy, self.device_id, self.logical_device_id,
377 serial_number)
378 ############################################################################
379 # Setup PM configuration for this device
380 # Pass in ONU specific options
381 kwargs = {
382 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
383 'heartbeat': self.heartbeat,
384 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
385 }
386 self.log.debug('create-pm-metrics', device_id=self.device_id, serial_number=serial_number)
387 self._pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
388 self.logical_device_id, serial_number,
389 grouped=True, freq_override=False, **kwargs)
390 pm_config = self._pm_metrics.make_proto()
391 self._onu_omci_device.set_pm_config(self._pm_metrics.omci_pm.openomci_interval_pm)
392 self.log.debug("initial-pm-config", device_id=self.device_id, serial_number=serial_number)
393
394 if not self._reconciling:
395 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
396
397 # Note, ONU ID and UNI intf set in add_uni_port method
398 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
399 ani_ports=[self._pon])
400
401 # Code to Run OMCI Test Action
402 kwargs_omci_test_action = {
403 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
404 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
405 }
406 self._test_request = OmciTestRequest(self.core_proxy,
407 self.omci_agent, self.device_id,
408 AniG, serial_number,
409 self.logical_device_id,
410 exclusive=False,
411 **kwargs_omci_test_action)
412
413 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500414 def delete(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700415 self.log.info('delete-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneret5e331892019-12-07 21:31:45 -0500416 try:
417 yield self.onu_kv_client.delete(device.id)
418 except Exception as e:
419 self.log.error('could-not-delete-onu-state', device_id=device.id, e=e)
420
Devmalya Paul1e1b1722020-05-07 02:51:15 -0400421 try:
422 self._deferred.cancel()
423 self._test_request.stop_collector()
424 self._pm_metrics.stop_collector()
425 self.log.debug('removing-openomci-statemachine')
426 self.omci_agent.remove_device(device.id, cleanup=True)
427 yield self.onu_deleted_event()
428 except Exception as e:
429 self.log.error('could-not-delete-onu', device_id=device.id, e=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500430
431 def _create_tconts(self, uni_id, us_scheduler):
432 alloc_id = us_scheduler['alloc_id']
433 q_sched_policy = us_scheduler['q_sched_policy']
434 self.log.debug('create-tcont', us_scheduler=us_scheduler)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800435 # TODO: revisit for multi tconts support
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800436 new_tconts = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500437 tcontdict = dict()
438 tcontdict['alloc-id'] = alloc_id
439 tcontdict['q_sched_policy'] = q_sched_policy
440 tcontdict['uni_id'] = uni_id
441
Matt Jeanneret3789d0d2020-01-19 09:03:42 -0500442 tcont = OnuTCont.create(self, tcont=tcontdict)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500443
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500444 success = self._pon.add_tcont(tcont)
445 if success:
446 new_tconts.append(tcont)
447 self.log.debug('pon-add-tcont', tcont=tcont)
448
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800449 return new_tconts
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500450
451 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
452 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
453 self.log.debug('create-gemport',
454 gem_ports=gem_ports, direction=direction)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530455 new_gem_ports = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500456 for gem_port in gem_ports:
457 gemdict = dict()
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800458 if gem_port[IS_MULTICAST] == 'True':
459 gemdict[GEM_PORT_ID] = gem_port['multicast_gem_id']
460 gemdict[IS_MULTICAST] = True
461 else:
462 gemdict[GEM_PORT_ID] = gem_port[GEM_PORT_ID]
463 gemdict[IS_MULTICAST] = False
464
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500465 gemdict['direction'] = direction
466 gemdict['alloc_id_ref'] = alloc_id_ref
467 gemdict['encryption'] = gem_port['aes_encryption']
468 gemdict['discard_config'] = dict()
469 gemdict['discard_config']['max_probability'] = \
470 gem_port['discard_config']['max_probability']
471 gemdict['discard_config']['max_threshold'] = \
472 gem_port['discard_config']['max_threshold']
473 gemdict['discard_config']['min_threshold'] = \
474 gem_port['discard_config']['min_threshold']
475 gemdict['discard_policy'] = gem_port['discard_policy']
476 gemdict['max_q_size'] = gem_port['max_q_size']
477 gemdict['pbit_map'] = gem_port['pbit_map']
478 gemdict['priority_q'] = gem_port['priority_q']
479 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
480 gemdict['weight'] = gem_port['weight']
481 gemdict['uni_id'] = uni_id
482
483 gem_port = OnuGemPort.create(self, gem_port=gemdict)
484
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500485 success = self._pon.add_gem_port(gem_port, True)
486 if success:
487 new_gem_ports.append(gem_port)
488 self.log.debug('pon-add-gemport', gem_port=gem_port)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500489
Girish Gowdrae933cd32019-11-21 21:04:41 +0530490 return new_gem_ports
491
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800492 def _execute_queued_vlan_filter_tasks(self, uni_id, tp_id):
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400493 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
494 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
495 # or initial-mib-download is not done. Once the tp_task is completed, we execute
496 # such queued vlan-filter tasks
497 try:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800498 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 -0400499 self.log.info("executing-queued-vlan-filter-task",
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800500 uni_id=uni_id, tp_id=tp_id)
Mahir Gunyela982ec32020-02-25 12:30:37 -0800501 for filter_info in self._queued_vlan_filter_task[uni_id][tp_id]:
502 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800503 uni_id=uni_id, uni_port=filter_info.get("uni_port"),
504 match_vlan=filter_info.get("match_vlan"),
505 _set_vlan_vid=filter_info.get("set_vlan_vid"),
506 _set_vlan_pcp=filter_info.get("set_vlan_pcp"),
507 tp_id=filter_info.get("tp_id"))
Girish Gowdraaf98a082020-03-05 16:40:51 -0800508 # Now remove the entry from the dictionary
Girish Gowdraaf98a082020-03-05 16:40:51 -0800509 self.log.debug("executed-queued-vlan-filter-task",
510 uni_id=uni_id, tp_id=tp_id)
Girish Gowdraa63eda82020-05-12 13:40:04 -0700511
512 # Now delete the key entry for the tp_id once we have handled the
513 # queued vlan filter tasks for that tp_id
514 del self._queued_vlan_filter_task[uni_id][tp_id]
515 # If the queued vlan filter tasks for all the tp_ids on a given
516 # uni_id is handled, then delete the uni_id key
517 if len(self._queued_vlan_filter_task[uni_id]) == 0:
518 del self._queued_vlan_filter_task[uni_id]
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400519 except Exception as e:
520 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
521
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500522 def _do_tech_profile_configuration(self, uni_id, tp):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500523 us_scheduler = tp['us_scheduler']
524 alloc_id = us_scheduler['alloc_id']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800525 new_tconts = self._create_tconts(uni_id, us_scheduler)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500526 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800527 new_upstream_gems = self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500528 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800529 new_downstream_gems = self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
530
531 new_gems = []
532 new_gems.extend(new_upstream_gems)
533 new_gems.extend(new_downstream_gems)
534
535 return new_tconts, new_gems
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500536
Matt Jeanneret5e331892019-12-07 21:31:45 -0500537 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500538 def load_and_configure_tech_profile(self, uni_id, tp_path):
539 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
Mahir Gunyele9110a32020-02-20 14:56:50 -0800540 tp_id = self.extract_tp_id_from_path(tp_path)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500541 if uni_id not in self._tp_service_specific_task:
542 self._tp_service_specific_task[uni_id] = dict()
543
544 if uni_id not in self._tech_profile_download_done:
545 self._tech_profile_download_done[uni_id] = dict()
546
Mahir Gunyele9110a32020-02-20 14:56:50 -0800547 if tp_id not in self._tech_profile_download_done[uni_id]:
548 self._tech_profile_download_done[uni_id][tp_id] = False
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500549
Mahir Gunyele9110a32020-02-20 14:56:50 -0800550 if not self._tech_profile_download_done[uni_id][tp_id]:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500551 try:
552 if tp_path in self._tp_service_specific_task[uni_id]:
553 self.log.info("tech-profile-config-already-in-progress",
Girish Gowdrae933cd32019-11-21 21:04:41 +0530554 tp_path=tp_path)
Matt Jeanneret5e331892019-12-07 21:31:45 -0500555 returnValue(None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500556
Matt Jeanneret5e331892019-12-07 21:31:45 -0500557 tpstored = yield self.tp_kv_client.get(tp_path)
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500558 tpstring = tpstored.decode('ascii')
559 tp = json.loads(tpstring)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800560 self._tp[tp_id] = tp
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500561 self.log.debug("tp-instance", tp=tp)
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800562 tconts, gem_ports = self._do_tech_profile_configuration(uni_id, tp)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700563
William Kurkian3a206332019-04-29 11:05:47 -0400564 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500565 def success(_results):
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800566 self.log.info("tech-profile-config-done-successfully", uni_id=uni_id, tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500567 if tp_path in self._tp_service_specific_task[uni_id]:
568 del self._tp_service_specific_task[uni_id][tp_path]
Mahir Gunyele9110a32020-02-20 14:56:50 -0800569 self._tech_profile_download_done[uni_id][tp_id] = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400570 # Now execute any vlan filter tasks that were queued for later
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800571 reactor.callInThread(self._execute_queued_vlan_filter_tasks, uni_id, tp_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500572 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-success')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530573
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800574 # Execute mcast task
575 for gem in gem_ports:
Girish Gowdradc98d812020-03-20 13:04:58 -0700576 self.log.debug("checking-multicast-service-for-gem ", gem=gem)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800577 if gem.mcast is True:
Girish Gowdradc98d812020-03-20 13:04:58 -0700578 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 -0800579 reactor.callInThread(self.start_multicast_service, uni_id, tp_path)
580 self.log.debug("started_multicast_service-successfully", tconts=tconts, gems=gem_ports)
581 break
582
William Kurkian3a206332019-04-29 11:05:47 -0400583 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500584 def failure(_reason):
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800585 self.log.warn('tech-profile-config-failure-retrying', uni_id=uni_id, tp_id=tp_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +0530586 _reason=_reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500587 if tp_path in self._tp_service_specific_task[uni_id]:
588 del self._tp_service_specific_task[uni_id][tp_path]
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800589 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500590 reactor.callLater(retry, self.load_and_configure_tech_profile,
591 uni_id, tp_path)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500592 yield self.core_proxy.device_reason_update(self.device_id,
593 'tech-profile-config-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500594
Mahir Gunyela982ec32020-02-25 12:30:37 -0800595 self.log.info('downloading-tech-profile-configuration', uni_id=uni_id, tp_id=tp_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530596 self.log.debug("tconts-gems-to-install", tconts=tconts, gem_ports=gem_ports)
597
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500598 self.log.debug("current-cached-tconts", tconts=list(self.pon_port.tconts.values()))
599 self.log.debug("current-cached-gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
600
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500601 self._tp_service_specific_task[uni_id][tp_path] = \
Mahir Gunyele9110a32020-02-20 14:56:50 -0800602 BrcmTpSetupTask(self.omci_agent, self, uni_id, tconts, gem_ports, tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500603 self._deferred = \
Girish Gowdrae933cd32019-11-21 21:04:41 +0530604 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500605 self._deferred.addCallbacks(success, failure)
606
607 except Exception as e:
608 self.log.exception("error-loading-tech-profile", e=e)
609 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700610 # There is an active tech-profile task ongoing on this UNI port. So, reschedule this task
611 # after a short interval
612 if uni_id in self._tp_service_specific_task and len(self._tp_service_specific_task[uni_id]):
613 self.log.debug("active-tp-tasks-in-progress-for-uni--scheduling-this-task-for-later",
614 uni_id=uni_id, tp_path=tp_path)
615 reactor.callLater(0.2, self.load_and_configure_tech_profile,
616 uni_id, tp_path)
617 return
618
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500619 self.log.info("tech-profile-config-already-done")
Girish Gowdradc98d812020-03-20 13:04:58 -0700620
Girish Gowdrae933cd32019-11-21 21:04:41 +0530621 # Could be a case where TP exists but new gem-ports are getting added dynamically
Matt Jeanneret5e331892019-12-07 21:31:45 -0500622 tpstored = yield self.tp_kv_client.get(tp_path)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530623 tpstring = tpstored.decode('ascii')
624 tp = json.loads(tpstring)
625 upstream_gems = []
626 downstream_gems = []
627 # Find out the new Gem ports that are getting added afresh.
628 for gp in tp['upstream_gem_port_attribute_list']:
629 if self.pon_port.gem_port(gp['gemport_id'], "upstream"):
630 # gem port already exists
631 continue
632 upstream_gems.append(gp)
633 for gp in tp['downstream_gem_port_attribute_list']:
634 if self.pon_port.gem_port(gp['gemport_id'], "downstream"):
635 # gem port already exists
636 continue
637 downstream_gems.append(gp)
638
639 us_scheduler = tp['us_scheduler']
640 alloc_id = us_scheduler['alloc_id']
641
642 if len(upstream_gems) > 0 or len(downstream_gems) > 0:
643 self.log.info("installing-new-gem-ports", upstream_gems=upstream_gems, downstream_gems=downstream_gems)
644 new_upstream_gems = self._create_gemports(uni_id, upstream_gems, alloc_id, "UPSTREAM")
645 new_downstream_gems = self._create_gemports(uni_id, downstream_gems, alloc_id, "DOWNSTREAM")
646 new_gems = []
647 new_gems.extend(new_upstream_gems)
648 new_gems.extend(new_downstream_gems)
649
650 def success(_results):
651 self.log.info("new-gem-ports-successfully-installed", result=_results)
652
653 def failure(_reason):
654 self.log.warn('new-gem-port-install-failed--retrying',
655 _reason=_reason)
656 # Remove gem ports from cache. We will re-add them during the retry
657 for gp in new_gems:
658 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
659
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800660 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500661 reactor.callLater(retry, self.load_and_configure_tech_profile,
662 uni_id, tp_path)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530663
664 self._tp_service_specific_task[uni_id][tp_path] = \
Mahir Gunyele9110a32020-02-20 14:56:50 -0800665 BrcmTpSetupTask(self.omci_agent, self, uni_id, [], new_gems, tp_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530666 self._deferred = \
667 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
668 self._deferred.addCallbacks(success, failure)
Girish Gowdradc98d812020-03-20 13:04:58 -0700669
Matt Jeanneret5e331892019-12-07 21:31:45 -0500670 @inlineCallbacks
Girish Gowdradc98d812020-03-20 13:04:58 -0700671 def start_multicast_service(self, uni_id, tp_path, retry_count=0):
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800672 self.log.debug("starting-multicast-service", uni_id=uni_id, tp_path=tp_path)
673 tp_id = self.extract_tp_id_from_path(tp_path)
674 if uni_id in self._set_vlan and tp_id in self._set_vlan[uni_id]:
675 try:
676 tp = self._tp[tp_id]
677 if tp is None:
Matt Jeanneret5e331892019-12-07 21:31:45 -0500678 tpstored = yield self.tp_kv_client.get(tp_path)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800679 tpstring = tpstored.decode('ascii')
680 tp = json.loads(tpstring)
681 if tp is None:
682 self.log.error("cannot-find-tp-to-start-multicast-service", uni_id=uni_id, tp_path=tp_path)
683 return
684 else:
685 self._tp[tp_id] = tp
686
687 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 -0700688
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800689 def success(_results):
690 self.log.debug('multicast-success', uni_id=uni_id)
691 self._multicast_task = None
692
693 def failure(_reason):
694 self.log.warn('multicast-failure', _reason=_reason)
Girish Gowdradc98d812020-03-20 13:04:58 -0700695 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800696 reactor.callLater(retry, self.start_multicast_service,
Girish Gowdradc98d812020-03-20 13:04:58 -0700697 uni_id, tp_path)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800698
699 self.log.debug('starting-multicast-task', mcast_vlan_id=self._set_vlan[uni_id][tp_id])
700 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
701 for i in range(len(downstream_gem_port_attribute_list)):
702 if IS_MULTICAST in downstream_gem_port_attribute_list[i] and \
703 downstream_gem_port_attribute_list[i][IS_MULTICAST] == 'True':
Girish Gowdradc98d812020-03-20 13:04:58 -0700704 dynamic_access_control_list_table = downstream_gem_port_attribute_list[i][
705 'dynamic_access_control_list'].split("-")
706 static_access_control_list_table = downstream_gem_port_attribute_list[i][
707 'static_access_control_list'].split("-")
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800708 multicast_gem_id = downstream_gem_port_attribute_list[i]['multicast_gem_id']
709 break
710
711 self._multicast_task = BrcmMcastTask(self.omci_agent, self, self.device_id, uni_id, tp_id,
Girish Gowdradc98d812020-03-20 13:04:58 -0700712 self._set_vlan[uni_id][tp_id], dynamic_access_control_list_table,
713 static_access_control_list_table, multicast_gem_id)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800714 self._deferred = self._onu_omci_device.task_runner.queue_task(self._multicast_task)
715 self._deferred.addCallbacks(success, failure)
716 except Exception as e:
717 self.log.exception("error-loading-multicast", e=e)
718 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700719 if retry_count < 30:
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800720 retry_count = +1
Girish Gowdradc98d812020-03-20 13:04:58 -0700721 self.log.debug("going-to-wait-for-flow-to-learn-mcast-vlan", uni_id=uni_id, tp_id=tp_id,
722 retry=retry_count)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800723 reactor.callLater(0.5, self.start_multicast_service, uni_id, tp_path, retry_count)
724 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700725 self.log.error("mcast-vlan-not-configured-yet-failing-mcast-service-conf", uni_id=uni_id, tp_id=tp_id,
726 retry=retry_count)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530727
728 def delete_tech_profile(self, uni_id, tp_path, alloc_id=None, gem_port_id=None):
729 try:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800730 tp_table_id = self.extract_tp_id_from_path(tp_path)
Naga Manjunathe433c712020-01-02 17:27:20 +0530731 if not uni_id in self._tech_profile_download_done:
732 self.log.warn("tp-key-is-not-present", uni_id=uni_id)
733 return
734
Mahir Gunyele9110a32020-02-20 14:56:50 -0800735 if not tp_table_id in self._tech_profile_download_done[uni_id]:
736 self.log.warn("tp-id-is-not-present", uni_id=uni_id, tp_id=tp_table_id)
Naga Manjunathe433c712020-01-02 17:27:20 +0530737 return
738
Mahir Gunyele9110a32020-02-20 14:56:50 -0800739 if self._tech_profile_download_done[uni_id][tp_table_id] is not True:
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800740 self.log.error("tp-download-is-not-done-in-order-to-process-tp-delete", uni_id=uni_id,
741 tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530742 return
743
744 if alloc_id is None and gem_port_id is None:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800745 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 +0530746 return
747
748 # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
749 # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
750 # due to additional tasks on different UNIs. So, it we cannot use the pon_port affter
751 # this initializer
752 tcont = None
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500753 self.log.debug("current-cached-tconts", tconts=list(self.pon_port.tconts.values()))
Girish Gowdrae933cd32019-11-21 21:04:41 +0530754 for tc in list(self.pon_port.tconts.values()):
755 if tc.alloc_id == alloc_id:
756 tcont = tc
757 self.pon_port.remove_tcont(tc.alloc_id, False)
758
759 gem_port = None
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500760 self.log.debug("current-cached-gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
Girish Gowdrae933cd32019-11-21 21:04:41 +0530761 for gp in list(self.pon_port.gem_ports.values()):
762 if gp.gem_id == gem_port_id:
763 gem_port = gp
764 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
765
Girish Gowdrae933cd32019-11-21 21:04:41 +0530766 @inlineCallbacks
767 def success(_results):
768 if gem_port_id:
769 self.log.info("gem-port-delete-done-successfully")
770 if alloc_id:
771 self.log.info("tcont-delete-done-successfully")
772 # The deletion of TCONT marks the complete deletion of tech-profile
773 try:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800774 del self._tech_profile_download_done[uni_id][tp_table_id]
Girish Gowdradc98d812020-03-20 13:04:58 -0700775 self.log.debug("tp-profile-download-flag-cleared", uni_id=uni_id, tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530776 del self._tp_service_specific_task[uni_id][tp_path]
Girish Gowdradc98d812020-03-20 13:04:58 -0700777 self.log.debug("tp-service-specific-task-cleared", uni_id=uni_id, tp_id=tp_table_id)
778 del self._pending_delete_tp[uni_id][tp_table_id]
779 self.log.debug("pending-delete-tp-task-flag-cleared", uni_id=uni_id, tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530780 except Exception as ex:
781 self.log.error("del-tp-state-info", e=ex)
782
783 # TODO: There could be multiple TP on the UNI, and also the ONU.
784 # TODO: But the below reason updates for the whole device.
785 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-delete-success')
786
787 @inlineCallbacks
Girish Gowdraa73ee452019-12-20 18:52:17 +0530788 def failure(_reason):
Girish Gowdrae933cd32019-11-21 21:04:41 +0530789 self.log.warn('tech-profile-delete-failure-retrying',
790 _reason=_reason)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500791 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
792 reactor.callLater(retry, self.delete_tech_profile, uni_id, tp_path, alloc_id, gem_port_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500793 yield self.core_proxy.device_reason_update(self.device_id,
794 'tech-profile-config-delete-failure-retrying')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530795
796 self.log.info('deleting-tech-profile-configuration')
797
Girish Gowdraa73ee452019-12-20 18:52:17 +0530798 if tcont is None and gem_port is None:
799 if alloc_id is not None:
800 self.log.error("tcont-info-corresponding-to-alloc-id-not-found", alloc_id=alloc_id)
801 if gem_port_id is not None:
802 self.log.error("gem-port-info-corresponding-to-gem-port-id-not-found", gem_port_id=gem_port_id)
803 return
804
Girish Gowdrae933cd32019-11-21 21:04:41 +0530805 self._tp_service_specific_task[uni_id][tp_path] = \
806 BrcmTpDeleteTask(self.omci_agent, self, uni_id, tp_table_id,
807 tcont=tcont, gem_port=gem_port)
808 self._deferred = \
809 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
810 self._deferred.addCallbacks(success, failure)
811 except Exception as e:
812 self.log.exception("failed-to-delete-tp",
813 e=e, uni_id=uni_id, tp_path=tp_path,
814 alloc_id=alloc_id, gem_port_id=gem_port_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500815
Rohan Agrawalf0f8c292020-06-01 09:30:55 +0000816 def update_pm_config(self, device, pm_configs):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500817 # TODO: This has not been tested
Rohan Agrawalf0f8c292020-06-01 09:30:55 +0000818 self.log.info('update_pm_config', pm_configs=pm_configs)
819 self._pm_metrics.update(pm_configs)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500820
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800821 def remove_onu_flows(self, device, flows):
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500822 self.log.debug('remove-onu-flows')
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800823
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800824 # no point in removing omci flows if the device isnt reachable
825 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800826 device.admin_state != AdminState.ENABLED:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800827 self.log.warn("device-disabled-or-offline-skipping-remove-flow",
828 admin=device.admin_state, connect=device.connect_status)
829 return
830
831 for flow in flows:
832 # if incoming flow contains cookie, then remove from ONU
833 if flow.cookie:
834 self.log.debug("remove-flow", device_id=device.id, flow=flow)
835
836 def is_downstream(port):
837 return port == self._pon_port_number
838
839 def is_upstream(port):
840 return not is_downstream(port)
841
842 try:
843 _in_port = fd.get_in_port(flow)
844 assert _in_port is not None
845
846 _out_port = fd.get_out_port(flow) # may be None
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800847
848 if is_downstream(_in_port):
849 self.log.debug('downstream-flow-no-need-to-remove', in_port=_in_port, out_port=_out_port,
850 device_id=device.id)
851 # extended vlan tagging operation will handle it
852 continue
853 elif is_upstream(_in_port):
854 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
855 if fd.is_dhcp_flow(flow):
856 self.log.debug('The dhcp trap-to-host flow will be discarded', device_id=device.id)
857 return
858
Mahir Gunyel45610b42020-03-16 17:29:01 -0700859 _match_vlan_vid = None
860 for field in fd.get_ofb_fields(flow):
861 if field.type == fd.VLAN_VID:
862 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
863 _match_vlan_vid = RESERVED_TRANSPARENT_VLAN
864 else:
865 _match_vlan_vid = field.vlan_vid & 0xfff
866 self.log.debug('field-type-vlan-vid',
867 vlan=_match_vlan_vid)
868
869 _set_vlan_vid = None
870 _set_vlan_pcp = None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800871 # Retrieve the VLAN_VID that needs to be removed from the EVTO rule on the ONU.
872 for action in fd.get_actions(flow):
873 if action.type == fd.SET_FIELD:
874 _field = action.set_field.field.ofb_field
875 assert (action.set_field.field.oxm_class ==
876 OFPXMC_OPENFLOW_BASIC)
877 if _field.type == fd.VLAN_VID:
Mahir Gunyel45610b42020-03-16 17:29:01 -0700878 _set_vlan_vid = _field.vlan_vid & 0xfff
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800879 self.log.debug('vlan-vid-to-remove',
Mahir Gunyel45610b42020-03-16 17:29:01 -0700880 _vlan_vid=_set_vlan_vid, in_port=_in_port)
881 elif _field.type == fd.VLAN_PCP:
882 _set_vlan_pcp = _field.vlan_pcp
883 self.log.debug('set-field-type-vlan-pcp',
884 vlan_pcp=_set_vlan_pcp)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800885
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800886 uni_port = self.uni_port(_in_port)
887 uni_id = _in_port & 0xF
888 else:
889 raise Exception('port should be 1 or 2 by our convention')
890
891 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
892
893 tp_id = self.get_tp_id_in_flow(flow)
Girish Gowdradc98d812020-03-20 13:04:58 -0700894 # The vlan filter remove should be followed by a TP deleted for that TP ID.
895 # Use this information to re-schedule any vlan filter add tasks for the same TP ID again.
896 # First check if the TP download was done, before we access that TP delete is necessary
Mahir Gunyel45610b42020-03-16 17:29:01 -0700897 if uni_id in self._tech_profile_download_done and tp_id in self._tech_profile_download_done[
898 uni_id] and \
Girish Gowdradc98d812020-03-20 13:04:58 -0700899 self._tech_profile_download_done[uni_id][tp_id] is True:
900 if uni_id not in self._pending_delete_tp:
901 self._pending_delete_tp[uni_id] = dict()
902 self._pending_delete_tp[uni_id][tp_id] = True
903 else:
904 self._pending_delete_tp[uni_id][tp_id] = True
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800905 # Deleting flow from ONU.
Mahir Gunyel45610b42020-03-16 17:29:01 -0700906 self._remove_vlan_filter_task(device, uni_id, uni_port=uni_port,
907 _set_vlan_pcp=_set_vlan_pcp,
908 _set_vlan_vid=_set_vlan_vid,
909 match_vlan=_match_vlan_vid,
910 tp_id=tp_id)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800911 # TODO:Delete TD task.
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800912 except Exception as e:
913 self.log.exception('failed-to-remove-flow', e=e)
914
915 def add_onu_flows(self, device, flows):
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500916 self.log.debug('add-onu-flows')
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800917
918 # no point in pushing omci flows if the device isnt reachable
919 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800920 device.admin_state != AdminState.ENABLED:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800921 self.log.warn("device-disabled-or-offline-skipping-flow-update",
922 admin=device.admin_state, connect=device.connect_status)
923 return
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800924
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800925 def is_downstream(port):
926 return port == self._pon_port_number
927
928 def is_upstream(port):
929 return not is_downstream(port)
930
931 for flow in flows:
932 # if incoming flow contains cookie, then add to ONU
933 if flow.cookie:
934 _type = None
935 _port = None
936 _vlan_vid = None
937 _udp_dst = None
938 _udp_src = None
939 _ipv4_dst = None
940 _ipv4_src = None
941 _metadata = None
942 _output = None
943 _push_tpid = None
944 _field = None
945 _set_vlan_vid = None
Mahir Gunyel45610b42020-03-16 17:29:01 -0700946 _set_vlan_pcp = None
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800947 _tunnel_id = None
Girish Gowdra6a73ad62020-06-11 13:40:16 -0700948 _proto = -1
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800949 self.log.debug("add-flow", device_id=device.id, flow=flow)
950
951 try:
952 _in_port = fd.get_in_port(flow)
953 assert _in_port is not None
954
955 _out_port = fd.get_out_port(flow) # may be None
956 tp_id = self.get_tp_id_in_flow(flow)
957 if is_downstream(_in_port):
958 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
959 # NOTE: We don't care downstream flow because we will copy vlan_id to upstream flow
960 # uni_port = self.uni_port(_out_port)
961 # uni_id = _out_port & 0xF
962 continue
963 elif is_upstream(_in_port):
964 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
965 uni_port = self.uni_port(_in_port)
966 uni_id = _in_port & 0xF
967 else:
968 raise Exception('port should be 1 or 2 by our convention')
969
970 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
971
972 for field in fd.get_ofb_fields(flow):
973 if field.type == fd.ETH_TYPE:
974 _type = field.eth_type
975 self.log.debug('field-type-eth-type',
976 eth_type=_type)
977
978 elif field.type == fd.IP_PROTO:
979 _proto = field.ip_proto
Girish Gowdra6a73ad62020-06-11 13:40:16 -0700980 if _proto == 2:
981 # Workaround for TT workflow - avoids installing invalid EVTO rule
982 self.log.debug("igmp-trap-flow")
983 break
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800984 self.log.debug('field-type-ip-proto',
985 ip_proto=_proto)
986
987 elif field.type == fd.IN_PORT:
988 _port = field.port
989 self.log.debug('field-type-in-port',
990 in_port=_port)
991 elif field.type == fd.TUNNEL_ID:
992 self.log.debug('field-type-tunnel-id')
993
994 elif field.type == fd.VLAN_VID:
Andrea Campanellacf916ea2020-02-14 10:03:58 +0100995 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
996 _vlan_vid = RESERVED_TRANSPARENT_VLAN
997 else:
998 _vlan_vid = field.vlan_vid & 0xfff
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800999 self.log.debug('field-type-vlan-vid',
1000 vlan=_vlan_vid)
1001
1002 elif field.type == fd.VLAN_PCP:
1003 _vlan_pcp = field.vlan_pcp
1004 self.log.debug('field-type-vlan-pcp',
1005 pcp=_vlan_pcp)
1006
1007 elif field.type == fd.UDP_DST:
1008 _udp_dst = field.udp_dst
1009 self.log.debug('field-type-udp-dst',
1010 udp_dst=_udp_dst)
1011
1012 elif field.type == fd.UDP_SRC:
1013 _udp_src = field.udp_src
1014 self.log.debug('field-type-udp-src',
1015 udp_src=_udp_src)
1016
1017 elif field.type == fd.IPV4_DST:
1018 _ipv4_dst = field.ipv4_dst
1019 self.log.debug('field-type-ipv4-dst',
1020 ipv4_dst=_ipv4_dst)
1021
1022 elif field.type == fd.IPV4_SRC:
1023 _ipv4_src = field.ipv4_src
1024 self.log.debug('field-type-ipv4-src',
1025 ipv4_dst=_ipv4_src)
1026
1027 elif field.type == fd.METADATA:
1028 _metadata = field.table_metadata
1029 self.log.debug('field-type-metadata',
1030 metadata=_metadata)
1031
1032 else:
1033 raise NotImplementedError('field.type={}'.format(
1034 field.type))
1035
Girish Gowdra6a73ad62020-06-11 13:40:16 -07001036 if _proto == 2:
1037 # Workaround for TT workflow - avoids installing invalid EVTO rule
1038 self.log.warn("skipping-igmp-trap-flow")
1039 continue
1040
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001041 for action in fd.get_actions(flow):
1042
1043 if action.type == fd.OUTPUT:
1044 _output = action.output.port
1045 self.log.debug('action-type-output',
1046 output=_output, in_port=_in_port)
1047
1048 elif action.type == fd.POP_VLAN:
1049 self.log.debug('action-type-pop-vlan',
1050 in_port=_in_port)
1051
1052 elif action.type == fd.PUSH_VLAN:
1053 _push_tpid = action.push.ethertype
1054 self.log.debug('action-type-push-vlan',
1055 push_tpid=_push_tpid, in_port=_in_port)
1056 if action.push.ethertype != 0x8100:
1057 self.log.error('unhandled-tpid',
1058 ethertype=action.push.ethertype)
1059
1060 elif action.type == fd.SET_FIELD:
1061 _field = action.set_field.field.ofb_field
1062 assert (action.set_field.field.oxm_class ==
1063 OFPXMC_OPENFLOW_BASIC)
1064 self.log.debug('action-type-set-field',
1065 field=_field, in_port=_in_port)
1066 if _field.type == fd.VLAN_VID:
1067 _set_vlan_vid = _field.vlan_vid & 0xfff
1068 self.log.debug('set-field-type-vlan-vid',
1069 vlan_vid=_set_vlan_vid)
1070 elif _field.type == fd.VLAN_PCP:
1071 _set_vlan_pcp = _field.vlan_pcp
1072 self.log.debug('set-field-type-vlan-pcp',
1073 vlan_pcp=_set_vlan_pcp)
1074 else:
1075 self.log.error('unsupported-action-set-field-type',
1076 field_type=_field.type)
1077 else:
1078 self.log.error('unsupported-action-type',
1079 action_type=action.type, in_port=_in_port)
1080
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001081 if self._set_vlan is not None:
1082 if uni_id not in self._set_vlan:
1083 self._set_vlan[uni_id] = dict()
1084 self._set_vlan[uni_id][tp_id] = _set_vlan_vid
1085 self.log.debug("set_vlan_id-for-tp", _set_vlan_vid=_set_vlan_vid, tp_id=tp_id)
1086
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001087 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
1088 # supported match and action criteria cannot be handled by omci and must be ignored.
1089 if (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid != RESERVED_TRANSPARENT_VLAN:
1090 self.log.warn('ignoring-flow-that-does-not-set-vlanid', set_vlan_vid=_set_vlan_vid)
1091 elif (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid == RESERVED_TRANSPARENT_VLAN:
1092 self.log.info('set-vlanid-any', uni_id=uni_id, uni_port=uni_port,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001093 _set_vlan_vid=_vlan_vid,
1094 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1095 tp_id=tp_id)
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001096 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1097 _set_vlan_vid=_vlan_vid,
1098 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1099 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001100 else:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001101 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, match_vlan=_vlan_vid,
1102 set_vlan_vid=_set_vlan_vid, _set_vlan_pcp=_set_vlan_pcp, ethType=_type)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001103 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1104 _set_vlan_vid=_set_vlan_vid,
1105 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1106 tp_id=tp_id)
1107
1108 except Exception as e:
1109 self.log.exception('failed-to-install-flow', e=e, flow=flow)
1110
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001111 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
1112 # flow decomposition that ultimately comes from onos
1113 def update_flow_table(self, device, flows):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001114 self.log.debug('update-flow-table', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001115
1116 #
1117 # We need to proxy through the OLT to get to the ONU
1118 # Configuration from here should be using OMCI
1119 #
1120 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
1121
1122 # no point in pushing omci flows if the device isnt reachable
1123 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdrae933cd32019-11-21 21:04:41 +05301124 device.admin_state != AdminState.ENABLED:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001125 self.log.warn("device-disabled-or-offline-skipping-flow-update",
1126 admin=device.admin_state, connect=device.connect_status)
1127 return
1128
1129 def is_downstream(port):
1130 return port == self._pon_port_number
1131
1132 def is_upstream(port):
1133 return not is_downstream(port)
1134
1135 for flow in flows:
1136 _type = None
1137 _port = None
1138 _vlan_vid = None
1139 _udp_dst = None
1140 _udp_src = None
1141 _ipv4_dst = None
1142 _ipv4_src = None
1143 _metadata = None
1144 _output = None
1145 _push_tpid = None
1146 _field = None
1147 _set_vlan_vid = None
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001148 _set_vlan_pcp = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -04001149 _tunnel_id = None
1150
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001151 try:
Girish Gowdraa73ee452019-12-20 18:52:17 +05301152 write_metadata = fd.get_write_metadata(flow)
1153 if write_metadata is None:
1154 self.log.error("do-not-process-flow-without-write-metadata")
1155 return
1156
1157 # extract tp id from flow
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001158 tp_id = self.get_tp_id_in_flow(flow)
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001159 self.log.debug("tp-id-in-flow", tp_id=tp_id)
Girish Gowdraa73ee452019-12-20 18:52:17 +05301160
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001161 _in_port = fd.get_in_port(flow)
1162 assert _in_port is not None
1163
1164 _out_port = fd.get_out_port(flow) # may be None
1165
1166 if is_downstream(_in_port):
1167 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
1168 uni_port = self.uni_port(_out_port)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301169 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001170 elif is_upstream(_in_port):
1171 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
1172 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001173 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001174 else:
1175 raise Exception('port should be 1 or 2 by our convention')
1176
1177 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
1178
1179 for field in fd.get_ofb_fields(flow):
1180 if field.type == fd.ETH_TYPE:
1181 _type = field.eth_type
1182 self.log.debug('field-type-eth-type',
1183 eth_type=_type)
1184
1185 elif field.type == fd.IP_PROTO:
1186 _proto = field.ip_proto
1187 self.log.debug('field-type-ip-proto',
1188 ip_proto=_proto)
1189
1190 elif field.type == fd.IN_PORT:
1191 _port = field.port
1192 self.log.debug('field-type-in-port',
1193 in_port=_port)
1194
1195 elif field.type == fd.VLAN_VID:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001196 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
1197 _vlan_vid = RESERVED_TRANSPARENT_VLAN
1198 else:
1199 _vlan_vid = field.vlan_vid & 0xfff
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001200 self.log.debug('field-type-vlan-vid',
1201 vlan=_vlan_vid)
1202
1203 elif field.type == fd.VLAN_PCP:
1204 _vlan_pcp = field.vlan_pcp
1205 self.log.debug('field-type-vlan-pcp',
1206 pcp=_vlan_pcp)
1207
1208 elif field.type == fd.UDP_DST:
1209 _udp_dst = field.udp_dst
1210 self.log.debug('field-type-udp-dst',
1211 udp_dst=_udp_dst)
1212
1213 elif field.type == fd.UDP_SRC:
1214 _udp_src = field.udp_src
1215 self.log.debug('field-type-udp-src',
1216 udp_src=_udp_src)
1217
1218 elif field.type == fd.IPV4_DST:
1219 _ipv4_dst = field.ipv4_dst
1220 self.log.debug('field-type-ipv4-dst',
1221 ipv4_dst=_ipv4_dst)
1222
1223 elif field.type == fd.IPV4_SRC:
1224 _ipv4_src = field.ipv4_src
1225 self.log.debug('field-type-ipv4-src',
1226 ipv4_dst=_ipv4_src)
1227
1228 elif field.type == fd.METADATA:
1229 _metadata = field.table_metadata
1230 self.log.debug('field-type-metadata',
1231 metadata=_metadata)
1232
Matt Jeanneretef06d0d2019-04-27 17:36:53 -04001233 elif field.type == fd.TUNNEL_ID:
1234 _tunnel_id = field.tunnel_id
1235 self.log.debug('field-type-tunnel-id',
1236 tunnel_id=_tunnel_id)
1237
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001238
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001239 else:
1240 raise NotImplementedError('field.type={}'.format(
1241 field.type))
1242
1243 for action in fd.get_actions(flow):
1244
1245 if action.type == fd.OUTPUT:
1246 _output = action.output.port
1247 self.log.debug('action-type-output',
1248 output=_output, in_port=_in_port)
1249
1250 elif action.type == fd.POP_VLAN:
1251 self.log.debug('action-type-pop-vlan',
1252 in_port=_in_port)
1253
1254 elif action.type == fd.PUSH_VLAN:
1255 _push_tpid = action.push.ethertype
1256 self.log.debug('action-type-push-vlan',
1257 push_tpid=_push_tpid, in_port=_in_port)
1258 if action.push.ethertype != 0x8100:
1259 self.log.error('unhandled-tpid',
1260 ethertype=action.push.ethertype)
1261
1262 elif action.type == fd.SET_FIELD:
1263 _field = action.set_field.field.ofb_field
1264 assert (action.set_field.field.oxm_class ==
1265 OFPXMC_OPENFLOW_BASIC)
1266 self.log.debug('action-type-set-field',
1267 field=_field, in_port=_in_port)
1268 if _field.type == fd.VLAN_VID:
1269 _set_vlan_vid = _field.vlan_vid & 0xfff
1270 self.log.debug('set-field-type-vlan-vid',
1271 vlan_vid=_set_vlan_vid)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001272 elif _field.type == fd.VLAN_PCP:
1273 _set_vlan_pcp = _field.vlan_pcp
1274 self.log.debug('set-field-type-vlan-pcp',
1275 vlan_pcp=_set_vlan_pcp)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001276 else:
1277 self.log.error('unsupported-action-set-field-type',
1278 field_type=_field.type)
1279 else:
1280 self.log.error('unsupported-action-type',
1281 action_type=action.type, in_port=_in_port)
1282
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001283 if self._set_vlan is not None:
1284 if uni_id not in self._set_vlan:
1285 self._set_vlan[uni_id] = dict()
1286 self._set_vlan[uni_id][tp_id] = _set_vlan_vid
1287 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 -04001288 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
1289 # supported match and action criteria cannot be handled by omci and must be ignored.
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001290 if (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid != RESERVED_TRANSPARENT_VLAN:
1291 self.log.warn('ignoring-flow-that-does-not-set-vlanid', set_vlan_vid=_set_vlan_vid)
1292 elif (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid == RESERVED_TRANSPARENT_VLAN:
1293 self.log.info('set-vlanid-any', uni_id=uni_id, uni_port=uni_port,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001294 _set_vlan_vid=_vlan_vid,
1295 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1296 tp_id=tp_id)
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001297 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1298 _set_vlan_vid=_vlan_vid,
1299 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1300 tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001301 else:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001302 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, match_vlan=_vlan_vid,
1303 set_vlan_vid=_set_vlan_vid, _set_vlan_pcp=_set_vlan_pcp, ethType=_type)
1304 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1305 _set_vlan_vid=_set_vlan_vid,
1306 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1307 tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001308 except Exception as e:
1309 self.log.exception('failed-to-install-flow', e=e, flow=flow)
1310
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001311 def _add_vlan_filter_task(self, device, uni_id, uni_port=None, match_vlan=0,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001312 _set_vlan_vid=None, _set_vlan_pcp=8, tp_id=0):
Girish Gowdradc98d812020-03-20 13:04:58 -07001313 if uni_id in self._pending_delete_tp and tp_id in self._pending_delete_tp[uni_id] and \
1314 self._pending_delete_tp[uni_id][tp_id] is True:
1315 self.log.debug("pending-del-tp--scheduling-add-vlan-filter-task-for-later")
1316 reactor.callLater(0.2, self._add_vlan_filter_task, device, uni_id, uni_port, match_vlan,
1317 _set_vlan_vid, _set_vlan_pcp, tp_id)
1318 return
1319
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001320 self.log.info('_adding_vlan_filter_task', uni_port=uni_port, uni_id=uni_id, tp_id=tp_id, match_vlan=match_vlan,
1321 vlan=_set_vlan_vid, vlan_pcp=_set_vlan_pcp)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001322 assert uni_port is not None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001323 if uni_id in self._tech_profile_download_done and tp_id in self._tech_profile_download_done[uni_id] and \
1324 self._tech_profile_download_done[uni_id][tp_id] is True:
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001325 @inlineCallbacks
1326 def success(_results):
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001327 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid, tp_id=tp_id,
1328 set_vlan_pcp=_set_vlan_pcp)
Matt Jeanneretd84c9072020-01-31 06:33:27 -05001329 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-pushed')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001330
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001331 @inlineCallbacks
1332 def failure(_reason):
Girish Gowdraa73ee452019-12-20 18:52:17 +05301333 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid, tp_id=tp_id)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001334 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1335 reactor.callLater(retry,
1336 self._add_vlan_filter_task, device, uni_id, uni_port=uni_port,
1337 match_vlan=match_vlan, _set_vlan_vid=_set_vlan_vid,
1338 _set_vlan_pcp=_set_vlan_pcp, tp_id=tp_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -05001339 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-failed-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001340
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001341 self.log.info('setting-vlan-tag', uni_port=uni_port, uni_id=uni_id, tp_id=tp_id, match_vlan=match_vlan,
1342 vlan=_set_vlan_vid, vlan_pcp=_set_vlan_pcp)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001343 vlan_filter_add_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001344 match_vlan, _set_vlan_pcp, add_tag=True,
1345 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001346 self._deferred = self._onu_omci_device.task_runner.queue_task(vlan_filter_add_task)
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001347 self._deferred.addCallbacks(success, failure)
1348 else:
1349 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001350 uni_id=uni_id, tp_id=tp_id)
1351 if uni_id not in self._queued_vlan_filter_task:
1352 self._queued_vlan_filter_task[uni_id] = dict()
Mahir Gunyela982ec32020-02-25 12:30:37 -08001353 if tp_id not in self._queued_vlan_filter_task[uni_id]:
1354 self._queued_vlan_filter_task[uni_id][tp_id] = []
1355 self._queued_vlan_filter_task[uni_id][tp_id].append({"device": device,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001356 "uni_id": uni_id,
1357 "uni_port": uni_port,
1358 "match_vlan": match_vlan,
1359 "set_vlan_vid": _set_vlan_vid,
1360 "set_vlan_pcp": _set_vlan_pcp,
1361 "tp_id": tp_id})
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001362
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001363 def get_tp_id_in_flow(self, flow):
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001364 flow_metadata = fd.get_metadata_from_write_metadata(flow)
1365 tp_id = fd.get_tp_id_from_metadata(flow_metadata)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001366 return tp_id
1367
1368 def _remove_vlan_filter_task(self, device, uni_id, uni_port=None, match_vlan=0,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001369 _set_vlan_vid=None, _set_vlan_pcp=8, tp_id=0):
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001370 assert uni_port is not None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001371
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001372 @inlineCallbacks
1373 def success(_results):
1374 self.log.info('vlan-untagging-success', _results=_results)
1375 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-deleted')
1376
1377 @inlineCallbacks
1378 def failure(_reason):
1379 self.log.warn('vlan-untagging-failure', _reason=_reason)
1380 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-deletion-failed-retrying')
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001381 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001382 reactor.callLater(retry,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001383 self._remove_vlan_filter_task, device, uni_id,
1384 add_tag=False, uni_port=uni_port)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001385
1386 self.log.info("remove_vlan_filter_task", tp_id=tp_id)
1387 vlan_remove_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001388 match_vlan, _set_vlan_pcp, add_tag=False,
1389 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001390 self._deferred = self._onu_omci_device.task_runner.queue_task(vlan_remove_task)
1391 self._deferred.addCallbacks(success, failure)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001392
Matt Jeanneret5e331892019-12-07 21:31:45 -05001393 @inlineCallbacks
Matt Jeannereta32441c2019-03-07 05:16:37 -05001394 def process_inter_adapter_message(self, request):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001395 self.log.debug('process-inter-adapter-message', type=request.header.type, from_topic=request.header.from_topic,
1396 to_topic=request.header.to_topic, to_device_id=request.header.to_device_id)
Matt Jeanneret2101f3d2020-03-12 10:13:06 -04001397
1398 if not self.enabled:
1399 self.log.warn('device-not-activated')
1400 reactor.callLater(0.5, self.process_inter_adapter_message, request)
1401 return
1402
Matt Jeannereta32441c2019-03-07 05:16:37 -05001403 try:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001404
1405 update_onu_state = False
1406
Matt Jeannereta32441c2019-03-07 05:16:37 -05001407 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
1408 omci_msg = InterAdapterOmciMessage()
1409 request.body.Unpack(omci_msg)
Matteo Scandolod8d73172019-11-26 12:15:15 -07001410 self.log.debug('inter-adapter-recv-omci', omci_msg=hexify(omci_msg.message))
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001411
Matt Jeannereta32441c2019-03-07 05:16:37 -05001412 self.receive_message(omci_msg.message)
1413
1414 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
1415 onu_indication = OnuIndication()
1416 request.body.Unpack(onu_indication)
Matteo Scandolod8d73172019-11-26 12:15:15 -07001417 self.log.debug('inter-adapter-recv-onu-ind', onu_id=onu_indication.onu_id,
1418 oper_state=onu_indication.oper_state, admin_state=onu_indication.admin_state,
1419 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001420
Matt Jeanneret5e331892019-12-07 21:31:45 -05001421 update_onu_state = True
1422 self._onu_persisted_state['onu_id'] = onu_indication.onu_id
1423 self._onu_persisted_state['intf_id'] = onu_indication.intf_id
1424 self._onu_persisted_state['admin_state'] = onu_indication.admin_state
Mahir Gunyel45610b42020-03-16 17:29:01 -07001425 self._onu_persisted_state['oper_state'] = onu_indication.oper_state
Matt Jeanneret5e331892019-12-07 21:31:45 -05001426
Matt Jeannereta32441c2019-03-07 05:16:37 -05001427 if onu_indication.oper_state == "up":
Matt Jeanneret5e331892019-12-07 21:31:45 -05001428 yield self.create_interface(onu_indication)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301429 elif onu_indication.oper_state == "down" or onu_indication.oper_state == "unreachable":
Matt Jeanneret5e331892019-12-07 21:31:45 -05001430 yield self.update_interface(onu_indication)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001431 else:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001432 self.log.error("unknown-onu-indication", onu_id=onu_indication.onu_id,
1433 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001434
Matt Jeanneret3bfebff2019-04-12 18:25:03 -04001435 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
1436 tech_msg = InterAdapterTechProfileDownloadMessage()
1437 request.body.Unpack(tech_msg)
1438 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
1439
Matt Jeanneret5e331892019-12-07 21:31:45 -05001440 update_onu_state = self._update_onu_persisted_state(tech_msg.uni_id, tp_path=tech_msg.path)
1441 yield self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
Matt Jeanneret3bfebff2019-04-12 18:25:03 -04001442
Girish Gowdrae933cd32019-11-21 21:04:41 +05301443 elif request.header.type == InterAdapterMessageType.DELETE_GEM_PORT_REQUEST:
1444 del_gem_msg = InterAdapterDeleteGemPortMessage()
1445 request.body.Unpack(del_gem_msg)
1446 self.log.debug('inter-adapter-recv-del-gem', gem_del_msg=del_gem_msg)
1447
1448 self.delete_tech_profile(uni_id=del_gem_msg.uni_id,
1449 gem_port_id=del_gem_msg.gem_port_id,
1450 tp_path=del_gem_msg.tp_path)
1451
1452 elif request.header.type == InterAdapterMessageType.DELETE_TCONT_REQUEST:
1453 del_tcont_msg = InterAdapterDeleteTcontMessage()
1454 request.body.Unpack(del_tcont_msg)
1455 self.log.debug('inter-adapter-recv-del-tcont', del_tcont_msg=del_tcont_msg)
1456
Matt Jeanneret5e331892019-12-07 21:31:45 -05001457 # Removal of the tcont/alloc id mapping represents the removal of the tech profile
1458 update_onu_state = self._update_onu_persisted_state(del_tcont_msg.uni_id, tp_path=None)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301459 self.delete_tech_profile(uni_id=del_tcont_msg.uni_id,
1460 alloc_id=del_tcont_msg.alloc_id,
1461 tp_path=del_tcont_msg.tp_path)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001462 else:
1463 self.log.error("inter-adapter-unhandled-type", request=request)
1464
Matt Jeanneret5e331892019-12-07 21:31:45 -05001465 if update_onu_state:
1466 try:
1467 self.log.debug('updating-onu-state', device_id=self.device_id,
1468 onu_persisted_state=self._onu_persisted_state)
1469 yield self.onu_kv_client.set(self.device_id, json.dumps(self._onu_persisted_state))
1470 except Exception as e:
1471 self.log.error('could-not-store-onu-state', device_id=self.device_id,
1472 onu_persisted_state=self._onu_persisted_state, e=e)
1473 # at this point omci is started and/or indications being processed
1474 # later indications may have a chance to write this state out again
1475
Matt Jeannereta32441c2019-03-07 05:16:37 -05001476 except Exception as e:
1477 self.log.exception("error-processing-inter-adapter-message", e=e)
1478
Matt Jeanneret5e331892019-12-07 21:31:45 -05001479 def _update_onu_persisted_state(self, uni_id, tp_path):
1480 # persist the uni and tech profile path for later reconciliation. update only if changed
1481 update_onu_state = False
1482 found = False
1483 for entry in self._onu_persisted_state.get('uni_config', list()):
1484 if entry.get('uni_id') == uni_id:
1485 found = True
1486 if entry.get('tp_path') != tp_path:
1487 update_onu_state = True
1488 entry['tp_path'] = tp_path
1489
1490 if not found:
1491 update_onu_state = True
1492 uni_tp = {
1493 'uni_id': uni_id,
1494 'tp_path': tp_path
1495 }
1496 self._onu_persisted_state['uni_config'].append(uni_tp)
1497
1498 return update_onu_state
1499
Matt Jeannereta32441c2019-03-07 05:16:37 -05001500 # Called each time there is an onu "up" indication from the olt handler
1501 @inlineCallbacks
1502 def create_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001503 self.log.info('create-interface', onu_id=onu_indication.onu_id,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001504 serial_number=onu_indication.serial_number)
Amit Ghosh028eb202020-02-17 13:34:00 +00001505
1506 # Ignore if onu_indication is received for an already running ONU
1507 if self._onu_omci_device is not None and self._onu_omci_device.active:
1508 self.log.warn('received-onu-indication-for-active-onu', onu_indication=onu_indication)
1509 return
1510
Matt Jeanneretc083f462019-03-11 15:02:01 -04001511 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
1512 connect_status=ConnectStatus.REACHABLE)
1513
Matt Jeannereta32441c2019-03-07 05:16:37 -05001514 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001515
1516 self.log.debug('starting-openomci-statemachine')
1517 self._subscribe_to_events()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001518 onu_device.reason = "starting-openomci"
Girish Gowdrae933cd32019-11-21 21:04:41 +05301519 reactor.callLater(1, self._onu_omci_device.start, onu_device)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001520 yield self.core_proxy.device_reason_update(self.device_id, onu_device.reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001521 self._heartbeat.enabled = True
1522
Matt Jeanneret42dad792020-02-01 09:28:27 -05001523 # Called each time there is an onu "down" indication from the olt handler
Matt Jeannereta32441c2019-03-07 05:16:37 -05001524 @inlineCallbacks
1525 def update_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001526 self.log.info('update-interface', onu_id=onu_indication.onu_id,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001527 serial_number=onu_indication.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001528
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -04001529 if onu_indication.oper_state == 'down' or onu_indication.oper_state == "unreachable":
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001530 self.log.debug('stopping-openomci-statemachine', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001531 reactor.callLater(0, self._onu_omci_device.stop)
1532
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001533 self._tp = dict()
1534
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001535 # Let TP download happen again
1536 for uni_id in self._tp_service_specific_task:
1537 self._tp_service_specific_task[uni_id].clear()
1538 for uni_id in self._tech_profile_download_done:
1539 self._tech_profile_download_done[uni_id].clear()
1540
Matt Jeanneretf4113222019-08-14 19:44:34 -04001541 yield self.disable_ports(lock_ports=False)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001542 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
1543 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.DISCOVERED,
1544 connect_status=ConnectStatus.UNREACHABLE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001545 else:
1546 self.log.debug('not-changing-openomci-statemachine')
1547
Matt Jeanneretf4113222019-08-14 19:44:34 -04001548 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001549 def disable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001550 self.log.info('disable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001551 try:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001552 yield self.disable_ports(lock_ports=True, device_disabled=True)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001553 yield self.core_proxy.device_reason_update(self.device_id, "omci-admin-lock")
1554 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001555 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001556 self.log.exception('exception-in-onu-disable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001557
William Kurkian3a206332019-04-29 11:05:47 -04001558 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001559 def reenable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001560 self.log.info('reenable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001561 try:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001562 yield self.core_proxy.device_state_update(device.id,
1563 oper_status=OperStatus.ACTIVE,
1564 connect_status=ConnectStatus.REACHABLE)
1565 yield self.core_proxy.device_reason_update(self.device_id, 'onu-reenabled')
1566 yield self.enable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001567 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001568 self.log.exception('exception-in-onu-reenable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001569
William Kurkian3a206332019-04-29 11:05:47 -04001570 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001571 def reboot(self):
1572 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -04001573 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001574 if device.connect_status != ConnectStatus.REACHABLE:
1575 self.log.error("device-unreachable")
1576 return
1577
William Kurkian3a206332019-04-29 11:05:47 -04001578 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001579 def success(_results):
1580 self.log.info('reboot-success', _results=_results)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001581 yield self.core_proxy.device_reason_update(self.device_id, 'rebooting')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001582
1583 def failure(_reason):
1584 self.log.info('reboot-failure', _reason=_reason)
1585
1586 self._deferred = self._onu_omci_device.reboot()
1587 self._deferred.addCallbacks(success, failure)
1588
William Kurkian3a206332019-04-29 11:05:47 -04001589 @inlineCallbacks
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001590 def disable_ports(self, lock_ports=True, device_disabled=False):
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001591 self.log.info('disable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001592
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001593 # TODO: for now only support the first UNI given no requirement for multiple uni yet. Also needed to reduce flow
1594 # load on the core
Matt Jeanneretf4113222019-08-14 19:44:34 -04001595 for port in self.uni_ports:
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001596 if port.mac_bridge_port_num == 1:
1597 port.operstatus = OperStatus.UNKNOWN
1598 self.log.info('disable-port', device_id=self.device_id, port=port)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001599 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number,
1600 port.operstatus)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001601
1602 if lock_ports is True:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001603 self.lock_ports(lock=True, device_disabled=device_disabled)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001604
William Kurkian3a206332019-04-29 11:05:47 -04001605 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001606 def enable_ports(self):
1607 self.log.info('enable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001608
Matt Jeanneretf4113222019-08-14 19:44:34 -04001609 self.lock_ports(lock=False)
1610
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001611 # TODO: for now only support the first UNI given no requirement for multiple uni yet. Also needed to reduce flow
1612 # load on the core
1613 # Given by default all unis are initially active according to omci alarming, we must mimic this.
Matt Jeanneretf4113222019-08-14 19:44:34 -04001614 for port in self.uni_ports:
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001615 if port.mac_bridge_port_num == 1:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001616 port.operstatus = OperStatus.ACTIVE
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001617 self.log.info('enable-port', device_id=self.device_id, port=port)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001618 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number,
1619 port.operstatus)
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001620
1621 # TODO: Normally we would want any uni ethernet link down or uni ethernet link up alarms to register in the core,
1622 # but practically olt provisioning cannot handle the churn of links up, down, then up again typical on startup.
1623 #
1624 # Basically the link state sequence:
1625 # 1) per omci default alarm state, all unis are initially up (no link down alarms received yet)
1626 # 2) a link state down alarm is received for all uni, given the lock command, and also because most unis have nothing plugged in
1627 # 3) a link state up alarm is received for the uni plugged in.
1628 #
1629 # Given the olt (BAL) has to provision all uni, de-provision all uni, and re-provision one uni in quick succession
1630 # and cannot (bug?), we have to skip this and leave uni ports as assumed active. Also all the link state activity
1631 # would have a ripple effect through the core to the controller as well. And is it really worth it?
1632 '''
Matt Jeanneretf4113222019-08-14 19:44:34 -04001633 @inlineCallbacks
1634 def port_state_handler(self, _topic, msg):
1635 self.log.info("port-state-change", _topic=_topic, msg=msg)
1636
1637 onu_id = msg['onu_id']
1638 port_no = msg['port_number']
1639 serial_number = msg['serial_number']
1640 port_status = msg['port_status']
1641 uni_port = self.uni_port(int(port_no))
1642
1643 self.log.debug("port-state-parsed-message", onu_id=onu_id, port_no=port_no, serial_number=serial_number,
1644 port_status=port_status)
1645
1646 if port_status is True:
1647 uni_port.operstatus = OperStatus.ACTIVE
1648 self.log.info('link-up', device_id=self.device_id, port=uni_port)
1649 else:
1650 uni_port.operstatus = OperStatus.UNKNOWN
1651 self.log.info('link-down', device_id=self.device_id, port=uni_port)
1652
1653 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 -05001654 '''
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001655
1656 # Called just before openomci state machine is started. These listen for events from selected state machines,
1657 # most importantly, mib in sync. Which ultimately leads to downloading the mib
1658 def _subscribe_to_events(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001659 self.log.debug('subscribe-to-events')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001660
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001661 bus = self._onu_omci_device.event_bus
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001662
1663 # OMCI MIB Database sync status
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001664 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1665 OnuDeviceEvents.MibDatabaseSyncEvent)
1666 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
1667
1668 # OMCI Capabilities
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001669 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1670 OnuDeviceEvents.OmciCapabilitiesEvent)
1671 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
1672
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001673 # TODO: these alarms seem to be unreliable depending on the environment
1674 # 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 -08001675 # topic = OnuDeviceEntry.event_bus_topic(self.device_id,
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001676 # OnuDeviceEvents.PortEvent)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001677 # self._port_state_subscription = bus.subscribe(topic, self.port_state_handler)
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001678
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001679 # Called when the mib is in sync
1680 def in_sync_handler(self, _topic, msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001681 self.log.debug('in-sync-handler', _topic=_topic, msg=msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001682 if self._in_sync_subscription is not None:
1683 try:
1684 in_sync = msg[IN_SYNC_KEY]
1685
1686 if in_sync:
1687 # Only call this once
1688 bus = self._onu_omci_device.event_bus
1689 bus.unsubscribe(self._in_sync_subscription)
1690 self._in_sync_subscription = None
1691
1692 # Start up device_info load
1693 self.log.debug('running-mib-sync')
1694 reactor.callLater(0, self._mib_in_sync)
1695
1696 except Exception as e:
1697 self.log.exception('in-sync', e=e)
1698
1699 def capabilties_handler(self, _topic, _msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001700 self.log.debug('capabilities-handler', _topic=_topic, msg=_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001701 if self._capabilities_subscription is not None:
1702 self.log.debug('capabilities-handler-done')
1703
1704 # 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 -04001705 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001706 def _mib_in_sync(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001707 self.log.debug('mib-in-sync')
Matt Jeanneretc083f462019-03-11 15:02:01 -04001708 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001709
Matt Jeanneret5e331892019-12-07 21:31:45 -05001710 # only notify core if this is a new device. otherwise do not have reconcile generating
1711 # a lot of needless message churn
1712 if not self._reconciling:
1713 yield self.core_proxy.device_reason_update(self.device_id, 'discovery-mibsync-complete')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001714
1715 if self._dev_info_loaded:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001716 self.log.debug('device-info-already-loaded')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001717 else:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001718 # new onu or adapter was restarted. fill up our local data
1719 yield self._load_device_data(device)
1720
1721 if self._check_mib_downloaded():
1722 self.log.debug('mib-already-downloaded')
1723 if not self._reconciling:
1724 yield self.core_proxy.device_state_update(device.id,
1725 oper_status=OperStatus.ACTIVE,
1726 connect_status=ConnectStatus.REACHABLE)
1727 yield self.enable_ports()
1728 else:
1729 self._download_mib(device)
1730
1731 if self._reconciling:
1732 yield self._restore_tech_profile()
1733 self._start_monitoring()
1734 self._reconciling = False
1735 self.log.debug('reconcile-finished')
1736
1737 def _download_mib(self, device):
1738 self.log.debug('downloading-initial-mib-configuration')
1739
1740 @inlineCallbacks
1741 def success(_results):
1742 self.log.debug('mib-download-success', _results=_results)
1743 yield self.core_proxy.device_state_update(device.id,
1744 oper_status=OperStatus.ACTIVE,
1745 connect_status=ConnectStatus.REACHABLE)
1746 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-downloaded')
1747 self._mib_download_task = None
1748 yield self.enable_ports()
1749 yield self.onu_active_event()
1750 self._start_monitoring()
1751
1752 @inlineCallbacks
1753 def failure(_reason):
1754 self.log.warn('mib-download-failure-retrying', _reason=_reason)
1755 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1756 reactor.callLater(retry, self._mib_in_sync)
1757 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-download-failure-retrying')
1758
1759 # start by locking all the unis till mib sync and initial mib is downloaded
1760 # this way we can capture the port down/up events when we are ready
1761 self.lock_ports(lock=True)
1762
1763 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1764 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1765 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1766 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1767 self._deferred.addCallbacks(success, failure)
1768
1769 def _start_monitoring(self):
1770 self.log.debug('starting-monitoring')
1771
1772 # Start collecting stats from the device after a brief pause
1773 if not self._pm_metrics_started:
1774 self._pm_metrics_started = True
1775 pmstart = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1776 reactor.callLater(pmstart, self._pm_metrics.start_collector)
1777
1778 # Start test requests after a brief pause
1779 if not self._test_request_started:
1780 self._test_request_started = True
1781 tststart = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1782 reactor.callLater(tststart, self._test_request.start_collector)
1783
1784 def _check_mib_downloaded(self):
1785 self.log.debug('checking-mib-downloaded')
1786 results = False
1787
1788 mac_bridges = self.onu_omci_device.query_mib(MacBridgeServiceProfile.class_id)
1789 self.log.debug('mac-bridges', mac_bridges=mac_bridges)
1790
1791 for k, v in mac_bridges.items():
1792 if not isinstance(v, dict):
1793 continue
1794 # found at least one mac bridge, good enough to say its done, break out
1795 self.log.debug('found-mac-bridge-mib-download-has-been-done', omci_key=k, omci_value=v)
1796 results = True
1797 break
1798
1799 return results
1800
1801 @inlineCallbacks
1802 def _load_device_data(self, device):
1803 self.log.debug('loading-device-data-from-mib', device_id=device.id)
1804
1805 omci_dev = self._onu_omci_device
1806 config = omci_dev.configuration
1807
1808 try:
1809 # sort the lists so we get consistent port ordering.
1810 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
1811 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
1812 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
1813 veip_list = sorted(config.veip_entities) if config.veip_entities else []
1814
1815 if ani_list is None or (pptp_list is None and veip_list is None):
1816 yield self.core_proxy.device_reason_update(self.device_id, 'onu-missing-required-elements')
1817 raise Exception("onu-missing-required-elements")
1818
1819 # Currently logging the ani, pptp, veip, and uni for information purposes.
1820 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
1821 # And in some ONU the UNI-G list is incomplete or incorrect...
1822 for entity_id in ani_list:
1823 ani_value = config.ani_g_entities[entity_id]
1824 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
1825
1826 for entity_id in uni_list:
1827 uni_value = config.uni_g_entities[entity_id]
1828 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
1829
1830 uni_entities = OrderedDict()
1831 for entity_id in pptp_list:
1832 pptp_value = config.pptp_entities[entity_id]
1833 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
1834 uni_entities[entity_id] = UniType.PPTP
1835
1836 for entity_id in veip_list:
1837 veip_value = config.veip_entities[entity_id]
1838 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
1839 uni_entities[entity_id] = UniType.VEIP
1840
1841 uni_id = 0
1842 for entity_id, uni_type in uni_entities.items():
1843 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
1844 uni_id += 1
1845
1846 if self._unis:
1847 self._dev_info_loaded = True
1848 else:
1849 yield self.core_proxy.device_reason_update(self.device_id, 'no-usable-unis')
1850 raise Exception("no-usable-unis")
1851
1852 except Exception as e:
1853 self.log.exception('device-info-load', e=e)
1854 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001855
Matt Jeanneretc083f462019-03-11 15:02:01 -04001856 @inlineCallbacks
1857 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matt Jeanneret5e331892019-12-07 21:31:45 -05001858 self.log.debug('add-uni-port', entity_id=entity_id, uni_id=uni_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001859
Matt Jeanneret5e331892019-12-07 21:31:45 -05001860 intf_id = self._onu_persisted_state.get('intf_id')
1861 onu_id = self._onu_persisted_state.get('onu_id')
1862 uni_no = self.mk_uni_port_num(intf_id, onu_id, uni_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001863
1864 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1865 uni_name = "uni-{}".format(uni_no)
1866
Girish Gowdrae933cd32019-11-21 21:04:41 +05301867 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001868
1869 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 -04001870 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001871
1872 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1873 uni_port.entity_id = entity_id
1874 uni_port.enabled = True
1875 uni_port.mac_bridge_port_num = mac_bridge_port_num
1876
1877 self.log.debug("created-uni-port", uni=uni_port)
1878
Matt Jeanneret5e331892019-12-07 21:31:45 -05001879 if not self._reconciling:
1880 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001881
1882 self._unis[uni_port.port_number] = uni_port
1883
Matt Jeanneret5e331892019-12-07 21:31:45 -05001884 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=onu_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301885 uni_ports=self.uni_ports,
1886 serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001887
Matt Jeanneret5e331892019-12-07 21:31:45 -05001888 @inlineCallbacks
1889 def _restore_tech_profile(self):
1890 self.log.debug("reconcile-restoring-tech-profile-tcont-gem-config")
1891
1892 # for every uni that has tech profile config reload all its tcont/alloc_id and gem from the tp path
1893 for entry in self._onu_persisted_state.get('uni_config', list()):
1894 uni_id = entry.get('uni_id')
1895 tp_path = entry.get('tp_path')
1896 if tp_path:
1897 tpstored = yield self.tp_kv_client.get(tp_path)
1898 tpstring = tpstored.decode('ascii')
1899 tp = json.loads(tpstring)
1900
1901 self.log.debug("restoring-tp-instance", tp=tp)
1902
1903 # re-run tech profile config that stores gem and tconts in the self._pon object
1904 # this does not actually re-run the omci, just rebuilds our local data store
1905 self._do_tech_profile_configuration(uni_id, tp)
1906
1907 tp_id = self.extract_tp_id_from_path(tp_path)
1908
1909 # rebuild cache dicts so tp updates and deletes dont get KeyErrors
1910 if uni_id not in self._tp_service_specific_task:
1911 self._tp_service_specific_task[uni_id] = dict()
1912
1913 if uni_id not in self._tech_profile_download_done:
1914 self._tech_profile_download_done[uni_id] = dict()
1915
1916 if tp_id not in self._tech_profile_download_done[uni_id]:
1917 self._tech_profile_download_done[uni_id][tp_id] = True
1918 else:
1919 self.log.debug("no-assigned-tp-instance", uni_id=uni_id)
1920
1921 # for every loaded tcont from tp check the mib database for its entity_id
1922 # needed for later tp deletes/adds
1923 tcont_idents = self.onu_omci_device.query_mib(Tcont.class_id)
1924 self.log.debug('tcont-idents', tcont_idents=tcont_idents)
1925
1926 for k, v in tcont_idents.items():
1927 if not isinstance(v, dict):
1928 continue
1929 alloc_check = v.get('attributes', {}).get('alloc_id', 0)
1930 tcont = self._pon.tconts.get(alloc_check)
1931 if tcont:
1932 tcont.entity_id = k
1933 self.log.debug('reassigning-tcont-entity-id', entity_id=tcont.entity_id,
1934 alloc_id=tcont.alloc_id)
1935
Matt Jeanneretc083f462019-03-11 15:02:01 -04001936 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1937 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
Amit Ghosh65400f12019-11-21 12:04:12 +00001938 MAX_PONS_PER_OLT = 256
1939 MAX_ONUS_PER_PON = 256
Matt Jeanneretc083f462019-03-11 15:02:01 -04001940 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001941
Matt Jeanneretc083f462019-03-11 15:02:01 -04001942 assert intf_id < MAX_PONS_PER_OLT
1943 assert onu_id < MAX_ONUS_PER_PON
1944 assert uni_id < MAX_UNIS_PER_ONU
Amit Ghosh65400f12019-11-21 12:04:12 +00001945 return intf_id << 12 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001946
1947 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001948 def onu_active_event(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001949 self.log.debug('onu-active-event')
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001950 try:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001951 # TODO: this is expensive for just getting the olt serial number. replace with direct api call
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001952 parent_device = yield self.core_proxy.get_device(self.parent_id)
1953 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001954 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001955
Matt Jeanneret5e331892019-12-07 21:31:45 -05001956 intf_id = self._onu_persisted_state.get('intf_id')
1957 onu_id = self._onu_persisted_state.get('onu_id')
1958 onu_serial = self._onu_persisted_state.get('serial_number')
1959
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001960 self.log.debug("onu-indication-context-data",
Matt Jeanneret5e331892019-12-07 21:31:45 -05001961 pon_id=intf_id,
1962 onu_id=onu_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301963 registration_id=self.device_id,
1964 device_id=self.device_id,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001965 onu_serial_number=onu_serial,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301966 olt_serial_number=olt_serial_number,
1967 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001968
Devmalya Paulffc89df2019-07-31 17:43:13 -04001969 self.log.debug("Trying-to-raise-onu-active-event")
1970 OnuActiveEvent(self.events, self.device_id,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001971 intf_id,
1972 onu_serial,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001973 str(self.device_id),
Girish Gowdrae933cd32019-11-21 21:04:41 +05301974 olt_serial_number, raised_ts,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001975 onu_id=onu_id).send(True)
Devmalya Paulffc89df2019-07-31 17:43:13 -04001976 except Exception as active_event_error:
1977 self.log.exception('onu-activated-event-error',
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001978 errmsg=active_event_error)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001979
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001980 @inlineCallbacks
1981 def onu_disabled_event(self):
1982 self.log.debug('onu-disabled-event')
1983 try:
1984 device = yield self.core_proxy.get_device(self.device_id)
1985 parent_device = yield self.core_proxy.get_device(self.parent_id)
1986 olt_serial_number = parent_device.serial_number
1987 raised_ts = arrow.utcnow().timestamp
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001988 intf_id = self._onu_persisted_state.get('intf_id')
1989 onu_id = self._onu_persisted_state.get('onu_id')
1990 onu_serial = self._onu_persisted_state.get('serial_number')
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001991
1992 self.log.debug("onu-indication-context-data",
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001993 pon_id=intf_id,
1994 onu_id=onu_id,
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001995 registration_id=self.device_id,
1996 device_id=self.device_id,
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001997 onu_serial_number=onu_serial,
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001998 olt_serial_number=olt_serial_number,
1999 raised_ts=raised_ts)
2000
2001 self.log.debug("Trying-to-raise-onu-disabled-event")
2002 OnuDisabledEvent(self.events, self.device_id,
Devmalya Paul1e1b1722020-05-07 02:51:15 -04002003 intf_id,
Girish Gowdradc98d812020-03-20 13:04:58 -07002004 device.serial_number,
2005 str(self.device_id),
2006 olt_serial_number, raised_ts,
Devmalya Paul1e1b1722020-05-07 02:51:15 -04002007 onu_id=onu_id).send(True)
2008 except Exception as disable_event_error:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04002009 self.log.exception('onu-disabled-event-error',
Devmalya Paul1e1b1722020-05-07 02:51:15 -04002010 errmsg=disable_event_error)
2011
2012 @inlineCallbacks
2013 def onu_deleted_event(self):
2014 self.log.debug('onu-deleted-event')
2015 try:
2016 device = yield self.core_proxy.get_device(self.device_id)
2017 parent_device = yield self.core_proxy.get_device(self.parent_id)
2018 olt_serial_number = parent_device.serial_number
2019 raised_ts = arrow.utcnow().timestamp
2020 intf_id = self._onu_persisted_state.get('intf_id')
2021 onu_id = self._onu_persisted_state.get('onu_id')
2022 serial_number = self._onu_persisted_state.get('serial_number')
2023
2024 self.log.debug("onu-deleted-event-context-data",
2025 pon_id=intf_id,
2026 onu_id=onu_id,
2027 registration_id=self.device_id,
2028 device_id=self.device_id,
2029 onu_serial_number=serial_number,
2030 olt_serial_number=olt_serial_number,
2031 raised_ts=raised_ts)
2032
2033 OnuDeletedEvent(self.events, self.device_id,
2034 intf_id,
2035 serial_number,
2036 str(self.device_id),
2037 olt_serial_number, raised_ts,
2038 onu_id=onu_id).send(True)
2039 except Exception as deleted_event_error:
2040 self.log.exception('onu-deleted-event-error',
2041 errmsg=deleted_event_error)
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04002042
2043 def lock_ports(self, lock=True, device_disabled=False):
Matt Jeanneretf4113222019-08-14 19:44:34 -04002044
2045 def success(response):
2046 self.log.debug('set-onu-ports-state', lock=lock, response=response)
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04002047 if device_disabled:
2048 self.onu_disabled_event()
Matt Jeanneretf4113222019-08-14 19:44:34 -04002049
2050 def failure(response):
2051 self.log.error('cannot-set-onu-ports-state', lock=lock, response=response)
2052
2053 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=lock)
2054 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
2055 self._deferred.addCallbacks(success, failure)
Mahir Gunyele9110a32020-02-20 14:56:50 -08002056
2057 def extract_tp_id_from_path(self, tp_path):
2058 # tp_path is of the format <technology>/<table_id>/<uni_port_name>
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08002059 tp_id = int(tp_path.split(_PATH_SEPERATOR)[1])
2060 return tp_id
onkarkundargia1e2af22020-01-27 11:51:43 +05302061
2062 def start_omci_test_action(self, device, uuid):
2063 """
2064
2065 :param device:
2066 :return:
2067 """
2068 # Code to Run OMCI Test Action
2069 self.log.info('Omci-test-action-request-On', request=device.id)
2070 kwargs_omci_test_action = {
2071 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
2072 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
2073 }
2074 serial_number = device.serial_number
2075 if device.connect_status != ConnectStatus.REACHABLE or device.admin_state != AdminState.ENABLED:
2076 return (TestResponse(result=TestResponse.FAILURE))
2077 test_request = OmciTestRequest(self.core_proxy,
2078 self.omci_agent, self.device_id, AniG,
2079 serial_number,
2080 self.logical_device_id, exclusive=False,
2081 uuid=uuid,
2082 **kwargs_omci_test_action)
2083 test_request.perform_test_omci()
2084 return (TestResponse(result=TestResponse.SUCCESS))