blob: 6e36b7e03ad9832a921807ab75ec9e7313aebb89 [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 Paulffc89df2019-07-31 17:43:13 -040036from pyvoltha.adapters.extensions.events.kpi.onu.onu_pm_metrics import OnuPmMetrics
37from pyvoltha.adapters.extensions.events.kpi.onu.onu_omci_pm import OnuOmciPmMetrics
38from pyvoltha.adapters.extensions.events.adapter_events import AdapterEvents
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050039
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050040import pyvoltha.common.openflow.utils as fd
41from pyvoltha.common.utils.registry import registry
Matteo Scandolod8d73172019-11-26 12:15:15 -070042from pyvoltha.adapters.common.frameio.frameio import hexify
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050043from pyvoltha.common.utils.nethelpers import mac_str_to_tuple
Matt Jeanneret5e331892019-12-07 21:31:45 -050044from pyvoltha.adapters.common.kvstore.twisted_etcd_store import TwistedEtcdStore
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050045from voltha_protos.logical_device_pb2 import LogicalPort
William Kurkian8235c1e2019-03-05 12:58:28 -050046from voltha_protos.common_pb2 import OperStatus, ConnectStatus, AdminState
Matt Jeanneretf4113222019-08-14 19:44:34 -040047from voltha_protos.device_pb2 import Port
Girish Gowdra4c11ddb2020-03-03 11:33:24 -080048from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, ofp_port, OFPPS_LIVE, OFPPS_LINK_DOWN, \
Matt Jeanneretf4113222019-08-14 19:44:34 -040049 OFPPF_FIBER, OFPPF_1GB_FD
Matt Jeanneret3bfebff2019-04-12 18:25:03 -040050from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
Girish Gowdrae933cd32019-11-21 21:04:41 +053051 InterAdapterOmciMessage, PortCapability, InterAdapterTechProfileDownloadMessage, InterAdapterDeleteGemPortMessage, \
52 InterAdapterDeleteTcontMessage
Matt Jeannereta32441c2019-03-07 05:16:37 -050053from voltha_protos.openolt_pb2 import OnuIndication
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050054from pyvoltha.adapters.extensions.omci.onu_device_entry import OnuDeviceEvents, \
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050055 OnuDeviceEntry, IN_SYNC_KEY
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050056from omci.brcm_mib_download_task import BrcmMibDownloadTask
Girish Gowdrae933cd32019-11-21 21:04:41 +053057from omci.brcm_tp_setup_task import BrcmTpSetupTask
58from omci.brcm_tp_delete_task import BrcmTpDeleteTask
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050059from omci.brcm_uni_lock_task import BrcmUniLockTask
60from omci.brcm_vlan_filter_task import BrcmVlanFilterTask
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050061from onu_gem_port import OnuGemPort
62from onu_tcont import OnuTCont
63from pon_port import PonPort
Mahir Gunyel5de33fe2020-03-03 22:38:44 -080064from omci.brcm_mcast_task import BrcmMcastTask
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050065from uni_port import UniPort, UniType
Andrea Campanellacf916ea2020-02-14 10:03:58 +010066from uni_port import RESERVED_TRANSPARENT_VLAN
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050067from pyvoltha.common.tech_profile.tech_profile import TechProfile
onkarkundargiaae99712019-09-23 15:02:52 +053068from pyvoltha.adapters.extensions.omci.tasks.omci_test_request import OmciTestRequest
Matt Jeanneret5e331892019-12-07 21:31:45 -050069from pyvoltha.adapters.extensions.omci.omci_entities import AniG, Tcont, MacBridgeServiceProfile
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050070from pyvoltha.adapters.extensions.omci.omci_defs import EntityOperations, ReasonCodes
onkarkundargia1e2af22020-01-27 11:51:43 +053071from voltha_protos.voltha_pb2 import TestResponse
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050072
73OP = EntityOperations
74RC = ReasonCodes
75
Girish Gowdradc98d812020-03-20 13:04:58 -070076IS_MULTICAST = 'is_multicast'
Mahir Gunyel5de33fe2020-03-03 22:38:44 -080077GEM_PORT_ID = 'gemport_id'
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050078_STARTUP_RETRY_WAIT = 10
Mahir Gunyele9110a32020-02-20 14:56:50 -080079_PATH_SEPERATOR = "/"
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050080
81
82class BrcmOpenomciOnuHandler(object):
83
84 def __init__(self, adapter, device_id):
85 self.log = structlog.get_logger(device_id=device_id)
Matt Jeanneret08a8e862019-12-20 14:02:32 -050086 self.log.debug('starting-handler')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050087 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050088 self.core_proxy = adapter.core_proxy
89 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050090 self.parent_id = None
91 self.device_id = device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050092 self.proxy_address = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050093 self._enabled = False
Devmalya Paulffc89df2019-07-31 17:43:13 -040094 self.events = None
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050095 self._pm_metrics = None
96 self._pm_metrics_started = False
97 self._test_request = None
98 self._test_request_started = False
Girish Gowdradc98d812020-03-20 13:04:58 -070099 self._tp = dict() # tp_id -> technology profile definition in KV Store.
Matt Jeanneret5e331892019-12-07 21:31:45 -0500100 self._reconciling = False
101
102 # Persisted onu configuration needed in case of reconciliation.
103 self._onu_persisted_state = {
104 'onu_id': None,
105 'intf_id': None,
106 'serial_number': None,
107 'admin_state': None,
108 'oper_state': None,
109 'uni_config': list()
110 }
111
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500112 self._unis = dict() # Port # -> UniPort
113
114 self._pon = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500115 self._pon_port_number = 100
116 self.logical_device_id = None
117
118 self._heartbeat = HeartBeat.create(self, device_id)
119
120 # Set up OpenOMCI environment
121 self._onu_omci_device = None
122 self._dev_info_loaded = False
123 self._deferred = None
124
125 self._in_sync_subscription = None
Matt Jeanneretf4113222019-08-14 19:44:34 -0400126 self._port_state_subscription = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500127 self._connectivity_subscription = None
128 self._capabilities_subscription = None
129
130 self.mac_bridge_service_profile_entity_id = 0x201
131 self.gal_enet_profile_entity_id = 0x1
132
133 self._tp_service_specific_task = dict()
134 self._tech_profile_download_done = dict()
Girish Gowdradc98d812020-03-20 13:04:58 -0700135
136 # When the vlan filter is being removed for a given TP ID on a given UNI,
137 # mark that we are expecting a tp delete to happen for this UNI.
138 # Unless the TP delete is complete to not allow new vlan add tasks to this TP ID
139 self._pending_delete_tp = dict()
140
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400141 # Stores information related to queued vlan filter tasks
142 # 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 -0400143 self._queued_vlan_filter_task = dict()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500144
Girish Gowdradc98d812020-03-20 13:04:58 -0700145 self._set_vlan = dict() # uni_id, tp_id -> set_vlan_id
Matt Jeanneret5e331892019-12-07 21:31:45 -0500146
147 # Paths from kv store
148 ONU_PATH = 'service/voltha/openonu'
149
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500150 # Initialize KV store client
151 self.args = registry('main').get_args()
Matt Jeanneret5e331892019-12-07 21:31:45 -0500152 host, port = self.args.etcd.split(':', 1)
153 self.tp_kv_client = TwistedEtcdStore(host, port, TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
154 self.onu_kv_client = TwistedEtcdStore(host, port, ONU_PATH)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500155
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500156 @property
157 def enabled(self):
158 return self._enabled
159
160 @enabled.setter
161 def enabled(self, value):
162 if self._enabled != value:
163 self._enabled = value
164
165 @property
166 def omci_agent(self):
167 return self.adapter.omci_agent
168
169 @property
170 def omci_cc(self):
171 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
172
173 @property
174 def heartbeat(self):
175 return self._heartbeat
176
177 @property
178 def uni_ports(self):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500179 return list(self._unis.values())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500180
181 def uni_port(self, port_no_or_name):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500182 if isinstance(port_no_or_name, six.string_types):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500183 return next((uni for uni in self.uni_ports
184 if uni.name == port_no_or_name), None)
185
186 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
187 return next((uni for uni in self.uni_ports
Girish Gowdrae933cd32019-11-21 21:04:41 +0530188 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500189
190 @property
191 def pon_port(self):
192 return self._pon
193
Girish Gowdraa73ee452019-12-20 18:52:17 +0530194 @property
195 def onu_omci_device(self):
196 return self._onu_omci_device
197
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500198 def receive_message(self, msg):
199 if self.omci_cc is not None:
200 self.omci_cc.receive_message(msg)
201
Matt Jeanneretc083f462019-03-11 15:02:01 -0400202 def get_ofp_port_info(self, device, port_no):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500203 self.log.debug('get-ofp-port-info', port_no=port_no, device_id=device.id)
Matt Jeanneretc083f462019-03-11 15:02:01 -0400204 cap = OFPPF_1GB_FD | OFPPF_FIBER
205
Girish Gowdrae933cd32019-11-21 21:04:41 +0530206 hw_addr = mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
207 ((device.parent_port_no >> 8 & 0xff),
208 device.parent_port_no & 0xff,
209 (port_no >> 16) & 0xff,
210 (port_no >> 8) & 0xff,
211 port_no & 0xff))
Matt Jeanneretc083f462019-03-11 15:02:01 -0400212
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400213 uni_port = self.uni_port(int(port_no))
214 name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500215 self.log.debug('ofp-port-name', port_no=port_no, name=name, uni_port=uni_port)
Matt Jeanneretf4113222019-08-14 19:44:34 -0400216
217 ofstate = OFPPS_LINK_DOWN
218 if uni_port.operstatus is OperStatus.ACTIVE:
219 ofstate = OFPPS_LIVE
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400220
Matt Jeanneretc083f462019-03-11 15:02:01 -0400221 return PortCapability(
222 port=LogicalPort(
223 ofp_port=ofp_port(
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400224 name=name,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400225 hw_addr=hw_addr,
226 config=0,
Matt Jeanneretf4113222019-08-14 19:44:34 -0400227 state=ofstate,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400228 curr=cap,
229 advertised=cap,
230 peer=cap,
231 curr_speed=OFPPF_1GB_FD,
232 max_speed=OFPPF_1GB_FD
233 ),
234 device_id=device.id,
235 device_port_no=port_no
236 )
237 )
238
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500239 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500240 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500241 def activate(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700242 self.log.debug('activate-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500243
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500244 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500245 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500246 assert device.proxy_address.device_id
247
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500248 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500249 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500250 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500251 if self.enabled is not True:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700252 self.log.info('activating-new-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500253 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500254 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500255 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500256 device.reason = 'activating-onu'
257
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500258 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400259 # pm_metrics requires a logical device id. For now set to just device_id
260 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500261
Matt Jeanneret5e331892019-12-07 21:31:45 -0500262 self._onu_persisted_state['serial_number'] = device.serial_number
263 try:
264 self.log.debug('updating-onu-state', device_id=self.device_id,
265 onu_persisted_state=self._onu_persisted_state)
266 yield self.onu_kv_client.set(self.device_id, json.dumps(self._onu_persisted_state))
267 except Exception as e:
268 self.log.error('could-not-store-onu-state', device_id=self.device_id,
269 onu_persisted_state=self._onu_persisted_state, e=e)
270 # if we cannot write to storage we can proceed, for now.
271 # later onu indications from the olt will have another chance
272
Matt Jeannereta32441c2019-03-07 05:16:37 -0500273 yield self.core_proxy.device_update(device)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500274 self.log.debug('device-updated', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500275
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700276 yield self._init_pon_state()
Matteo Scandolod8d73172019-11-26 12:15:15 -0700277 self.log.debug('pon state initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500278
Matt Jeanneret5e331892019-12-07 21:31:45 -0500279 yield self._init_metrics()
280 self.log.debug('metrics initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500281
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500282 self.enabled = True
283 else:
284 self.log.info('onu-already-activated')
285
286 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400287 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500288 def reconcile(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700289 self.log.debug('reconcile-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500290
Matt Jeanneret5e331892019-12-07 21:31:45 -0500291 if self._reconciling:
292 self.log.debug('already-running-reconcile-device', device_id=device.id, serial_number=device.serial_number)
293 return
294
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500295 # first we verify that we got parent reference and proxy info
296 assert device.parent_id
297 assert device.proxy_address.device_id
298
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700299 self.proxy_address = device.proxy_address
300 self.parent_id = device.parent_id
301 self._pon_port_number = device.parent_port_no
302
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500303 if self.enabled is not True:
Matt Jeanneret5e331892019-12-07 21:31:45 -0500304 self._reconciling = True
305 self.log.info('reconciling-openonu-device')
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700306 self.logical_device_id = self.device_id
Matt Jeanneret5e331892019-12-07 21:31:45 -0500307
308 try:
309 query_data = yield self.onu_kv_client.get(device.id)
310 self._onu_persisted_state = json.loads(query_data)
311 self.log.debug('restored-onu-state', device_id=self.device_id,
312 onu_persisted_state=self._onu_persisted_state)
313 except Exception as e:
314 self.log.error('no-stored-onu-state', device_id=device.id, e=e)
315 # there is nothing we can do without data. flag the device as UNKNOWN and cannot reconcile
316 # likely it will take manual steps to delete/re-add this onu
317 yield self.core_proxy.device_reason_update(self.device_id, "cannot-reconcile")
318 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.UNKNOWN)
319 return
320
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700321 self._init_pon_state()
Matt Jeanneret5e331892019-12-07 21:31:45 -0500322 self.log.debug('pon state initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500323
Matt Jeanneret5e331892019-12-07 21:31:45 -0500324 self._init_metrics()
325 self.log.debug('metrics initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500326
Matt Jeanneret5e331892019-12-07 21:31:45 -0500327 self._subscribe_to_events()
328 # need to restart omci start machines and reload mib database. once db is loaded we can finish reconcile
329 self._onu_omci_device.start(device)
330 self._heartbeat.enabled = True
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500331
332 self.enabled = True
333 else:
334 self.log.info('onu-already-activated')
335
336 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700337 def _init_pon_state(self):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500338 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 -0500339
340 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500341 self._pon.add_peer(self.parent_id, self._pon_port_number)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700342 self.log.debug('adding-pon-port-to-agent',
343 type=self._pon.get_port().type,
344 admin_state=self._pon.get_port().admin_state,
345 oper_status=self._pon.get_port().oper_status,
346 )
Matt Jeanneret0c287892019-02-28 11:48:00 -0500347
Matt Jeanneret5e331892019-12-07 21:31:45 -0500348 if not self._reconciling:
349 yield self.core_proxy.port_created(self.device_id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500350
Matteo Scandolod8d73172019-11-26 12:15:15 -0700351 self.log.debug('added-pon-port-to-agent',
352 type=self._pon.get_port().type,
353 admin_state=self._pon.get_port().admin_state,
354 oper_status=self._pon.get_port().oper_status,
355 )
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500356
357 # Create and start the OpenOMCI ONU Device Entry for this ONU
358 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500359 self.core_proxy,
360 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500361 support_classes=self.adapter.broadcom_omci,
362 custom_me_map=self.adapter.custom_me_entities())
363 # Port startup
364 if self._pon is not None:
365 self._pon.enabled = True
366
Matt Jeanneret5e331892019-12-07 21:31:45 -0500367 @inlineCallbacks
368 def _init_metrics(self):
369 self.log.debug('init-metrics', device_id=self.device_id, device_logical_id=self.logical_device_id)
370
371 serial_number = self._onu_persisted_state.get('serial_number')
372
373 ############################################################################
374 # Setup Alarm handler
375 self.events = AdapterEvents(self.core_proxy, self.device_id, self.logical_device_id,
376 serial_number)
377 ############################################################################
378 # Setup PM configuration for this device
379 # Pass in ONU specific options
380 kwargs = {
381 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
382 'heartbeat': self.heartbeat,
383 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
384 }
385 self.log.debug('create-pm-metrics', device_id=self.device_id, serial_number=serial_number)
386 self._pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
387 self.logical_device_id, serial_number,
388 grouped=True, freq_override=False, **kwargs)
389 pm_config = self._pm_metrics.make_proto()
390 self._onu_omci_device.set_pm_config(self._pm_metrics.omci_pm.openomci_interval_pm)
391 self.log.debug("initial-pm-config", device_id=self.device_id, serial_number=serial_number)
392
393 if not self._reconciling:
394 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
395
396 # Note, ONU ID and UNI intf set in add_uni_port method
397 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
398 ani_ports=[self._pon])
399
400 # Code to Run OMCI Test Action
401 kwargs_omci_test_action = {
402 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
403 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
404 }
405 self._test_request = OmciTestRequest(self.core_proxy,
406 self.omci_agent, self.device_id,
407 AniG, serial_number,
408 self.logical_device_id,
409 exclusive=False,
410 **kwargs_omci_test_action)
411
412 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500413 def delete(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700414 self.log.info('delete-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneret42dad792020-02-01 09:28:27 -0500415
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
Matt Jeanneret42dad792020-02-01 09:28:27 -0500421 self._deferred.cancel()
422 self._test_request.stop_collector()
423 self._pm_metrics.stop_collector()
424 self.log.debug('removing-openomci-statemachine')
425 self.omci_agent.remove_device(device.id, cleanup=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500426
427 def _create_tconts(self, uni_id, us_scheduler):
428 alloc_id = us_scheduler['alloc_id']
429 q_sched_policy = us_scheduler['q_sched_policy']
430 self.log.debug('create-tcont', us_scheduler=us_scheduler)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800431 # TODO: revisit for multi tconts support
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800432 new_tconts = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500433 tcontdict = dict()
434 tcontdict['alloc-id'] = alloc_id
435 tcontdict['q_sched_policy'] = q_sched_policy
436 tcontdict['uni_id'] = uni_id
437
Matt Jeanneret3789d0d2020-01-19 09:03:42 -0500438 tcont = OnuTCont.create(self, tcont=tcontdict)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500439
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500440 success = self._pon.add_tcont(tcont)
441 if success:
442 new_tconts.append(tcont)
443 self.log.debug('pon-add-tcont', tcont=tcont)
444
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800445 return new_tconts
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500446
447 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
448 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
449 self.log.debug('create-gemport',
450 gem_ports=gem_ports, direction=direction)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530451 new_gem_ports = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500452 for gem_port in gem_ports:
453 gemdict = dict()
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800454 if gem_port[IS_MULTICAST] == 'True':
455 gemdict[GEM_PORT_ID] = gem_port['multicast_gem_id']
456 gemdict[IS_MULTICAST] = True
457 else:
458 gemdict[GEM_PORT_ID] = gem_port[GEM_PORT_ID]
459 gemdict[IS_MULTICAST] = False
460
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500461 gemdict['direction'] = direction
462 gemdict['alloc_id_ref'] = alloc_id_ref
463 gemdict['encryption'] = gem_port['aes_encryption']
464 gemdict['discard_config'] = dict()
465 gemdict['discard_config']['max_probability'] = \
466 gem_port['discard_config']['max_probability']
467 gemdict['discard_config']['max_threshold'] = \
468 gem_port['discard_config']['max_threshold']
469 gemdict['discard_config']['min_threshold'] = \
470 gem_port['discard_config']['min_threshold']
471 gemdict['discard_policy'] = gem_port['discard_policy']
472 gemdict['max_q_size'] = gem_port['max_q_size']
473 gemdict['pbit_map'] = gem_port['pbit_map']
474 gemdict['priority_q'] = gem_port['priority_q']
475 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
476 gemdict['weight'] = gem_port['weight']
477 gemdict['uni_id'] = uni_id
478
479 gem_port = OnuGemPort.create(self, gem_port=gemdict)
480
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500481 success = self._pon.add_gem_port(gem_port, True)
482 if success:
483 new_gem_ports.append(gem_port)
484 self.log.debug('pon-add-gemport', gem_port=gem_port)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500485
Girish Gowdrae933cd32019-11-21 21:04:41 +0530486 return new_gem_ports
487
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800488 def _execute_queued_vlan_filter_tasks(self, uni_id, tp_id):
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400489 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
490 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
491 # or initial-mib-download is not done. Once the tp_task is completed, we execute
492 # such queued vlan-filter tasks
493 try:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800494 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 -0400495 self.log.info("executing-queued-vlan-filter-task",
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800496 uni_id=uni_id, tp_id=tp_id)
Mahir Gunyela982ec32020-02-25 12:30:37 -0800497 for filter_info in self._queued_vlan_filter_task[uni_id][tp_id]:
498 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800499 uni_id=uni_id, uni_port=filter_info.get("uni_port"),
500 match_vlan=filter_info.get("match_vlan"),
501 _set_vlan_vid=filter_info.get("set_vlan_vid"),
502 _set_vlan_pcp=filter_info.get("set_vlan_pcp"),
503 tp_id=filter_info.get("tp_id"))
Girish Gowdraaf98a082020-03-05 16:40:51 -0800504 # Now remove the entry from the dictionary
Girish Gowdraaf98a082020-03-05 16:40:51 -0800505 self.log.debug("executed-queued-vlan-filter-task",
506 uni_id=uni_id, tp_id=tp_id)
Girish Gowdraa63eda82020-05-12 13:40:04 -0700507
508 # Now delete the key entry for the tp_id once we have handled the
509 # queued vlan filter tasks for that tp_id
510 del self._queued_vlan_filter_task[uni_id][tp_id]
511 # If the queued vlan filter tasks for all the tp_ids on a given
512 # uni_id is handled, then delete the uni_id key
513 if len(self._queued_vlan_filter_task[uni_id]) == 0:
514 del self._queued_vlan_filter_task[uni_id]
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400515 except Exception as e:
516 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
517
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500518 def _do_tech_profile_configuration(self, uni_id, tp):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500519 us_scheduler = tp['us_scheduler']
520 alloc_id = us_scheduler['alloc_id']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800521 new_tconts = self._create_tconts(uni_id, us_scheduler)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500522 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800523 new_upstream_gems = self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500524 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800525 new_downstream_gems = self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
526
527 new_gems = []
528 new_gems.extend(new_upstream_gems)
529 new_gems.extend(new_downstream_gems)
530
531 return new_tconts, new_gems
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500532
Matt Jeanneret5e331892019-12-07 21:31:45 -0500533 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500534 def load_and_configure_tech_profile(self, uni_id, tp_path):
535 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
Mahir Gunyele9110a32020-02-20 14:56:50 -0800536 tp_id = self.extract_tp_id_from_path(tp_path)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500537 if uni_id not in self._tp_service_specific_task:
538 self._tp_service_specific_task[uni_id] = dict()
539
540 if uni_id not in self._tech_profile_download_done:
541 self._tech_profile_download_done[uni_id] = dict()
542
Mahir Gunyele9110a32020-02-20 14:56:50 -0800543 if tp_id not in self._tech_profile_download_done[uni_id]:
544 self._tech_profile_download_done[uni_id][tp_id] = False
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500545
Mahir Gunyele9110a32020-02-20 14:56:50 -0800546 if not self._tech_profile_download_done[uni_id][tp_id]:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500547 try:
548 if tp_path in self._tp_service_specific_task[uni_id]:
549 self.log.info("tech-profile-config-already-in-progress",
Girish Gowdrae933cd32019-11-21 21:04:41 +0530550 tp_path=tp_path)
Matt Jeanneret5e331892019-12-07 21:31:45 -0500551 returnValue(None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500552
Matt Jeanneret5e331892019-12-07 21:31:45 -0500553 tpstored = yield self.tp_kv_client.get(tp_path)
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500554 tpstring = tpstored.decode('ascii')
555 tp = json.loads(tpstring)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800556 self._tp[tp_id] = tp
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500557 self.log.debug("tp-instance", tp=tp)
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800558 tconts, gem_ports = self._do_tech_profile_configuration(uni_id, tp)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700559
William Kurkian3a206332019-04-29 11:05:47 -0400560 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500561 def success(_results):
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800562 self.log.info("tech-profile-config-done-successfully", uni_id=uni_id, tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500563 if tp_path in self._tp_service_specific_task[uni_id]:
564 del self._tp_service_specific_task[uni_id][tp_path]
Mahir Gunyele9110a32020-02-20 14:56:50 -0800565 self._tech_profile_download_done[uni_id][tp_id] = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400566 # Now execute any vlan filter tasks that were queued for later
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800567 reactor.callInThread(self._execute_queued_vlan_filter_tasks, uni_id, tp_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500568 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-success')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530569
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800570 # Execute mcast task
571 for gem in gem_ports:
Girish Gowdradc98d812020-03-20 13:04:58 -0700572 self.log.debug("checking-multicast-service-for-gem ", gem=gem)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800573 if gem.mcast is True:
Girish Gowdradc98d812020-03-20 13:04:58 -0700574 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 -0800575 reactor.callInThread(self.start_multicast_service, uni_id, tp_path)
576 self.log.debug("started_multicast_service-successfully", tconts=tconts, gems=gem_ports)
577 break
578
William Kurkian3a206332019-04-29 11:05:47 -0400579 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500580 def failure(_reason):
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800581 self.log.warn('tech-profile-config-failure-retrying', uni_id=uni_id, tp_id=tp_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +0530582 _reason=_reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500583 if tp_path in self._tp_service_specific_task[uni_id]:
584 del self._tp_service_specific_task[uni_id][tp_path]
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800585 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500586 reactor.callLater(retry, self.load_and_configure_tech_profile,
587 uni_id, tp_path)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500588 yield self.core_proxy.device_reason_update(self.device_id,
589 'tech-profile-config-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500590
Mahir Gunyela982ec32020-02-25 12:30:37 -0800591 self.log.info('downloading-tech-profile-configuration', uni_id=uni_id, tp_id=tp_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530592 self.log.debug("tconts-gems-to-install", tconts=tconts, gem_ports=gem_ports)
593
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500594 self.log.debug("current-cached-tconts", tconts=list(self.pon_port.tconts.values()))
595 self.log.debug("current-cached-gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
596
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500597 self._tp_service_specific_task[uni_id][tp_path] = \
Mahir Gunyele9110a32020-02-20 14:56:50 -0800598 BrcmTpSetupTask(self.omci_agent, self, uni_id, tconts, gem_ports, tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500599 self._deferred = \
Girish Gowdrae933cd32019-11-21 21:04:41 +0530600 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500601 self._deferred.addCallbacks(success, failure)
602
603 except Exception as e:
604 self.log.exception("error-loading-tech-profile", e=e)
605 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700606 # There is an active tech-profile task ongoing on this UNI port. So, reschedule this task
607 # after a short interval
608 if uni_id in self._tp_service_specific_task and len(self._tp_service_specific_task[uni_id]):
609 self.log.debug("active-tp-tasks-in-progress-for-uni--scheduling-this-task-for-later",
610 uni_id=uni_id, tp_path=tp_path)
611 reactor.callLater(0.2, self.load_and_configure_tech_profile,
612 uni_id, tp_path)
613 return
614
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500615 self.log.info("tech-profile-config-already-done")
Girish Gowdradc98d812020-03-20 13:04:58 -0700616
Girish Gowdrae933cd32019-11-21 21:04:41 +0530617 # Could be a case where TP exists but new gem-ports are getting added dynamically
Matt Jeanneret5e331892019-12-07 21:31:45 -0500618 tpstored = yield self.tp_kv_client.get(tp_path)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530619 tpstring = tpstored.decode('ascii')
620 tp = json.loads(tpstring)
621 upstream_gems = []
622 downstream_gems = []
623 # Find out the new Gem ports that are getting added afresh.
624 for gp in tp['upstream_gem_port_attribute_list']:
625 if self.pon_port.gem_port(gp['gemport_id'], "upstream"):
626 # gem port already exists
627 continue
628 upstream_gems.append(gp)
629 for gp in tp['downstream_gem_port_attribute_list']:
630 if self.pon_port.gem_port(gp['gemport_id'], "downstream"):
631 # gem port already exists
632 continue
633 downstream_gems.append(gp)
634
635 us_scheduler = tp['us_scheduler']
636 alloc_id = us_scheduler['alloc_id']
637
638 if len(upstream_gems) > 0 or len(downstream_gems) > 0:
639 self.log.info("installing-new-gem-ports", upstream_gems=upstream_gems, downstream_gems=downstream_gems)
640 new_upstream_gems = self._create_gemports(uni_id, upstream_gems, alloc_id, "UPSTREAM")
641 new_downstream_gems = self._create_gemports(uni_id, downstream_gems, alloc_id, "DOWNSTREAM")
642 new_gems = []
643 new_gems.extend(new_upstream_gems)
644 new_gems.extend(new_downstream_gems)
645
646 def success(_results):
647 self.log.info("new-gem-ports-successfully-installed", result=_results)
648
649 def failure(_reason):
650 self.log.warn('new-gem-port-install-failed--retrying',
651 _reason=_reason)
652 # Remove gem ports from cache. We will re-add them during the retry
653 for gp in new_gems:
654 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
655
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800656 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500657 reactor.callLater(retry, self.load_and_configure_tech_profile,
658 uni_id, tp_path)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530659
660 self._tp_service_specific_task[uni_id][tp_path] = \
Mahir Gunyele9110a32020-02-20 14:56:50 -0800661 BrcmTpSetupTask(self.omci_agent, self, uni_id, [], new_gems, tp_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530662 self._deferred = \
663 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
664 self._deferred.addCallbacks(success, failure)
Girish Gowdradc98d812020-03-20 13:04:58 -0700665
Matt Jeanneret5e331892019-12-07 21:31:45 -0500666 @inlineCallbacks
Girish Gowdradc98d812020-03-20 13:04:58 -0700667 def start_multicast_service(self, uni_id, tp_path, retry_count=0):
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800668 self.log.debug("starting-multicast-service", uni_id=uni_id, tp_path=tp_path)
669 tp_id = self.extract_tp_id_from_path(tp_path)
670 if uni_id in self._set_vlan and tp_id in self._set_vlan[uni_id]:
671 try:
672 tp = self._tp[tp_id]
673 if tp is None:
Matt Jeanneret5e331892019-12-07 21:31:45 -0500674 tpstored = yield self.tp_kv_client.get(tp_path)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800675 tpstring = tpstored.decode('ascii')
676 tp = json.loads(tpstring)
677 if tp is None:
678 self.log.error("cannot-find-tp-to-start-multicast-service", uni_id=uni_id, tp_path=tp_path)
679 return
680 else:
681 self._tp[tp_id] = tp
682
683 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 -0700684
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800685 def success(_results):
686 self.log.debug('multicast-success', uni_id=uni_id)
687 self._multicast_task = None
688
689 def failure(_reason):
690 self.log.warn('multicast-failure', _reason=_reason)
Girish Gowdradc98d812020-03-20 13:04:58 -0700691 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800692 reactor.callLater(retry, self.start_multicast_service,
Girish Gowdradc98d812020-03-20 13:04:58 -0700693 uni_id, tp_path)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800694
695 self.log.debug('starting-multicast-task', mcast_vlan_id=self._set_vlan[uni_id][tp_id])
696 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
697 for i in range(len(downstream_gem_port_attribute_list)):
698 if IS_MULTICAST in downstream_gem_port_attribute_list[i] and \
699 downstream_gem_port_attribute_list[i][IS_MULTICAST] == 'True':
Girish Gowdradc98d812020-03-20 13:04:58 -0700700 dynamic_access_control_list_table = downstream_gem_port_attribute_list[i][
701 'dynamic_access_control_list'].split("-")
702 static_access_control_list_table = downstream_gem_port_attribute_list[i][
703 'static_access_control_list'].split("-")
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800704 multicast_gem_id = downstream_gem_port_attribute_list[i]['multicast_gem_id']
705 break
706
707 self._multicast_task = BrcmMcastTask(self.omci_agent, self, self.device_id, uni_id, tp_id,
Girish Gowdradc98d812020-03-20 13:04:58 -0700708 self._set_vlan[uni_id][tp_id], dynamic_access_control_list_table,
709 static_access_control_list_table, multicast_gem_id)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800710 self._deferred = self._onu_omci_device.task_runner.queue_task(self._multicast_task)
711 self._deferred.addCallbacks(success, failure)
712 except Exception as e:
713 self.log.exception("error-loading-multicast", e=e)
714 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700715 if retry_count < 30:
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800716 retry_count = +1
Girish Gowdradc98d812020-03-20 13:04:58 -0700717 self.log.debug("going-to-wait-for-flow-to-learn-mcast-vlan", uni_id=uni_id, tp_id=tp_id,
718 retry=retry_count)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800719 reactor.callLater(0.5, self.start_multicast_service, uni_id, tp_path, retry_count)
720 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700721 self.log.error("mcast-vlan-not-configured-yet-failing-mcast-service-conf", uni_id=uni_id, tp_id=tp_id,
722 retry=retry_count)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530723
724 def delete_tech_profile(self, uni_id, tp_path, alloc_id=None, gem_port_id=None):
725 try:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800726 tp_table_id = self.extract_tp_id_from_path(tp_path)
Naga Manjunathe433c712020-01-02 17:27:20 +0530727 if not uni_id in self._tech_profile_download_done:
728 self.log.warn("tp-key-is-not-present", uni_id=uni_id)
729 return
730
Mahir Gunyele9110a32020-02-20 14:56:50 -0800731 if not tp_table_id in self._tech_profile_download_done[uni_id]:
732 self.log.warn("tp-id-is-not-present", uni_id=uni_id, tp_id=tp_table_id)
Naga Manjunathe433c712020-01-02 17:27:20 +0530733 return
734
Mahir Gunyele9110a32020-02-20 14:56:50 -0800735 if self._tech_profile_download_done[uni_id][tp_table_id] is not True:
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800736 self.log.error("tp-download-is-not-done-in-order-to-process-tp-delete", uni_id=uni_id,
737 tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530738 return
739
740 if alloc_id is None and gem_port_id is None:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800741 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 +0530742 return
743
744 # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
745 # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
746 # due to additional tasks on different UNIs. So, it we cannot use the pon_port affter
747 # this initializer
748 tcont = None
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500749 self.log.debug("current-cached-tconts", tconts=list(self.pon_port.tconts.values()))
Girish Gowdrae933cd32019-11-21 21:04:41 +0530750 for tc in list(self.pon_port.tconts.values()):
751 if tc.alloc_id == alloc_id:
752 tcont = tc
753 self.pon_port.remove_tcont(tc.alloc_id, False)
754
755 gem_port = None
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500756 self.log.debug("current-cached-gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
Girish Gowdrae933cd32019-11-21 21:04:41 +0530757 for gp in list(self.pon_port.gem_ports.values()):
758 if gp.gem_id == gem_port_id:
759 gem_port = gp
760 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
761
Girish Gowdrae933cd32019-11-21 21:04:41 +0530762 @inlineCallbacks
763 def success(_results):
764 if gem_port_id:
765 self.log.info("gem-port-delete-done-successfully")
766 if alloc_id:
767 self.log.info("tcont-delete-done-successfully")
768 # The deletion of TCONT marks the complete deletion of tech-profile
769 try:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800770 del self._tech_profile_download_done[uni_id][tp_table_id]
Girish Gowdradc98d812020-03-20 13:04:58 -0700771 self.log.debug("tp-profile-download-flag-cleared", uni_id=uni_id, tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530772 del self._tp_service_specific_task[uni_id][tp_path]
Girish Gowdradc98d812020-03-20 13:04:58 -0700773 self.log.debug("tp-service-specific-task-cleared", uni_id=uni_id, tp_id=tp_table_id)
774 del self._pending_delete_tp[uni_id][tp_table_id]
775 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 +0530776 except Exception as ex:
777 self.log.error("del-tp-state-info", e=ex)
778
779 # TODO: There could be multiple TP on the UNI, and also the ONU.
780 # TODO: But the below reason updates for the whole device.
781 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-delete-success')
782
783 @inlineCallbacks
Girish Gowdraa73ee452019-12-20 18:52:17 +0530784 def failure(_reason):
Girish Gowdrae933cd32019-11-21 21:04:41 +0530785 self.log.warn('tech-profile-delete-failure-retrying',
786 _reason=_reason)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500787 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
788 reactor.callLater(retry, self.delete_tech_profile, uni_id, tp_path, alloc_id, gem_port_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500789 yield self.core_proxy.device_reason_update(self.device_id,
790 'tech-profile-config-delete-failure-retrying')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530791
792 self.log.info('deleting-tech-profile-configuration')
793
Girish Gowdraa73ee452019-12-20 18:52:17 +0530794 if tcont is None and gem_port is None:
795 if alloc_id is not None:
796 self.log.error("tcont-info-corresponding-to-alloc-id-not-found", alloc_id=alloc_id)
797 if gem_port_id is not None:
798 self.log.error("gem-port-info-corresponding-to-gem-port-id-not-found", gem_port_id=gem_port_id)
799 return
800
Girish Gowdrae933cd32019-11-21 21:04:41 +0530801 self._tp_service_specific_task[uni_id][tp_path] = \
802 BrcmTpDeleteTask(self.omci_agent, self, uni_id, tp_table_id,
803 tcont=tcont, gem_port=gem_port)
804 self._deferred = \
805 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
806 self._deferred.addCallbacks(success, failure)
807 except Exception as e:
808 self.log.exception("failed-to-delete-tp",
809 e=e, uni_id=uni_id, tp_path=tp_path,
810 alloc_id=alloc_id, gem_port_id=gem_port_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500811
Rohan Agrawalf0f8c292020-06-01 09:30:55 +0000812 def update_pm_config(self, device, pm_configs):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500813 # TODO: This has not been tested
Rohan Agrawalf0f8c292020-06-01 09:30:55 +0000814 self.log.info('update_pm_config', pm_configs=pm_configs)
815 self._pm_metrics.update(pm_configs)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500816
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800817 def remove_onu_flows(self, device, flows):
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500818 self.log.debug('remove-onu-flows')
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800819
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800820 # no point in removing omci flows if the device isnt reachable
821 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800822 device.admin_state != AdminState.ENABLED:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800823 self.log.warn("device-disabled-or-offline-skipping-remove-flow",
824 admin=device.admin_state, connect=device.connect_status)
825 return
826
827 for flow in flows:
828 # if incoming flow contains cookie, then remove from ONU
829 if flow.cookie:
830 self.log.debug("remove-flow", device_id=device.id, flow=flow)
831
832 def is_downstream(port):
833 return port == self._pon_port_number
834
835 def is_upstream(port):
836 return not is_downstream(port)
837
838 try:
839 _in_port = fd.get_in_port(flow)
840 assert _in_port is not None
841
842 _out_port = fd.get_out_port(flow) # may be None
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800843
844 if is_downstream(_in_port):
845 self.log.debug('downstream-flow-no-need-to-remove', in_port=_in_port, out_port=_out_port,
846 device_id=device.id)
847 # extended vlan tagging operation will handle it
848 continue
849 elif is_upstream(_in_port):
850 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
851 if fd.is_dhcp_flow(flow):
852 self.log.debug('The dhcp trap-to-host flow will be discarded', device_id=device.id)
853 return
854
Mahir Gunyel45610b42020-03-16 17:29:01 -0700855 _match_vlan_vid = None
856 for field in fd.get_ofb_fields(flow):
857 if field.type == fd.VLAN_VID:
858 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
859 _match_vlan_vid = RESERVED_TRANSPARENT_VLAN
860 else:
861 _match_vlan_vid = field.vlan_vid & 0xfff
862 self.log.debug('field-type-vlan-vid',
863 vlan=_match_vlan_vid)
864
865 _set_vlan_vid = None
866 _set_vlan_pcp = None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800867 # Retrieve the VLAN_VID that needs to be removed from the EVTO rule on the ONU.
868 for action in fd.get_actions(flow):
869 if action.type == fd.SET_FIELD:
870 _field = action.set_field.field.ofb_field
871 assert (action.set_field.field.oxm_class ==
872 OFPXMC_OPENFLOW_BASIC)
873 if _field.type == fd.VLAN_VID:
Mahir Gunyel45610b42020-03-16 17:29:01 -0700874 _set_vlan_vid = _field.vlan_vid & 0xfff
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800875 self.log.debug('vlan-vid-to-remove',
Mahir Gunyel45610b42020-03-16 17:29:01 -0700876 _vlan_vid=_set_vlan_vid, in_port=_in_port)
877 elif _field.type == fd.VLAN_PCP:
878 _set_vlan_pcp = _field.vlan_pcp
879 self.log.debug('set-field-type-vlan-pcp',
880 vlan_pcp=_set_vlan_pcp)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800881
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800882 uni_port = self.uni_port(_in_port)
883 uni_id = _in_port & 0xF
884 else:
885 raise Exception('port should be 1 or 2 by our convention')
886
887 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
888
889 tp_id = self.get_tp_id_in_flow(flow)
Girish Gowdradc98d812020-03-20 13:04:58 -0700890 # The vlan filter remove should be followed by a TP deleted for that TP ID.
891 # Use this information to re-schedule any vlan filter add tasks for the same TP ID again.
892 # First check if the TP download was done, before we access that TP delete is necessary
Mahir Gunyel45610b42020-03-16 17:29:01 -0700893 if uni_id in self._tech_profile_download_done and tp_id in self._tech_profile_download_done[
894 uni_id] and \
Girish Gowdradc98d812020-03-20 13:04:58 -0700895 self._tech_profile_download_done[uni_id][tp_id] is True:
896 if uni_id not in self._pending_delete_tp:
897 self._pending_delete_tp[uni_id] = dict()
898 self._pending_delete_tp[uni_id][tp_id] = True
899 else:
900 self._pending_delete_tp[uni_id][tp_id] = True
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800901 # Deleting flow from ONU.
Mahir Gunyel45610b42020-03-16 17:29:01 -0700902 self._remove_vlan_filter_task(device, uni_id, uni_port=uni_port,
903 _set_vlan_pcp=_set_vlan_pcp,
904 _set_vlan_vid=_set_vlan_vid,
905 match_vlan=_match_vlan_vid,
906 tp_id=tp_id)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800907 # TODO:Delete TD task.
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800908 except Exception as e:
909 self.log.exception('failed-to-remove-flow', e=e)
910
911 def add_onu_flows(self, device, flows):
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500912 self.log.debug('add-onu-flows')
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800913
914 # no point in pushing omci flows if the device isnt reachable
915 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800916 device.admin_state != AdminState.ENABLED:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800917 self.log.warn("device-disabled-or-offline-skipping-flow-update",
918 admin=device.admin_state, connect=device.connect_status)
919 return
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800920
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800921 def is_downstream(port):
922 return port == self._pon_port_number
923
924 def is_upstream(port):
925 return not is_downstream(port)
926
927 for flow in flows:
928 # if incoming flow contains cookie, then add to ONU
929 if flow.cookie:
930 _type = None
931 _port = None
932 _vlan_vid = None
933 _udp_dst = None
934 _udp_src = None
935 _ipv4_dst = None
936 _ipv4_src = None
937 _metadata = None
938 _output = None
939 _push_tpid = None
940 _field = None
941 _set_vlan_vid = None
Mahir Gunyel45610b42020-03-16 17:29:01 -0700942 _set_vlan_pcp = None
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800943 _tunnel_id = None
944 self.log.debug("add-flow", device_id=device.id, flow=flow)
945
946 try:
947 _in_port = fd.get_in_port(flow)
948 assert _in_port is not None
949
950 _out_port = fd.get_out_port(flow) # may be None
951 tp_id = self.get_tp_id_in_flow(flow)
952 if is_downstream(_in_port):
953 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
954 # NOTE: We don't care downstream flow because we will copy vlan_id to upstream flow
955 # uni_port = self.uni_port(_out_port)
956 # uni_id = _out_port & 0xF
957 continue
958 elif is_upstream(_in_port):
959 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
960 uni_port = self.uni_port(_in_port)
961 uni_id = _in_port & 0xF
962 else:
963 raise Exception('port should be 1 or 2 by our convention')
964
965 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
966
967 for field in fd.get_ofb_fields(flow):
968 if field.type == fd.ETH_TYPE:
969 _type = field.eth_type
970 self.log.debug('field-type-eth-type',
971 eth_type=_type)
972
973 elif field.type == fd.IP_PROTO:
974 _proto = field.ip_proto
975 self.log.debug('field-type-ip-proto',
976 ip_proto=_proto)
977
978 elif field.type == fd.IN_PORT:
979 _port = field.port
980 self.log.debug('field-type-in-port',
981 in_port=_port)
982 elif field.type == fd.TUNNEL_ID:
983 self.log.debug('field-type-tunnel-id')
984
985 elif field.type == fd.VLAN_VID:
Andrea Campanellacf916ea2020-02-14 10:03:58 +0100986 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
987 _vlan_vid = RESERVED_TRANSPARENT_VLAN
988 else:
989 _vlan_vid = field.vlan_vid & 0xfff
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800990 self.log.debug('field-type-vlan-vid',
991 vlan=_vlan_vid)
992
993 elif field.type == fd.VLAN_PCP:
994 _vlan_pcp = field.vlan_pcp
995 self.log.debug('field-type-vlan-pcp',
996 pcp=_vlan_pcp)
997
998 elif field.type == fd.UDP_DST:
999 _udp_dst = field.udp_dst
1000 self.log.debug('field-type-udp-dst',
1001 udp_dst=_udp_dst)
1002
1003 elif field.type == fd.UDP_SRC:
1004 _udp_src = field.udp_src
1005 self.log.debug('field-type-udp-src',
1006 udp_src=_udp_src)
1007
1008 elif field.type == fd.IPV4_DST:
1009 _ipv4_dst = field.ipv4_dst
1010 self.log.debug('field-type-ipv4-dst',
1011 ipv4_dst=_ipv4_dst)
1012
1013 elif field.type == fd.IPV4_SRC:
1014 _ipv4_src = field.ipv4_src
1015 self.log.debug('field-type-ipv4-src',
1016 ipv4_dst=_ipv4_src)
1017
1018 elif field.type == fd.METADATA:
1019 _metadata = field.table_metadata
1020 self.log.debug('field-type-metadata',
1021 metadata=_metadata)
1022
1023 else:
1024 raise NotImplementedError('field.type={}'.format(
1025 field.type))
1026
1027 for action in fd.get_actions(flow):
1028
1029 if action.type == fd.OUTPUT:
1030 _output = action.output.port
1031 self.log.debug('action-type-output',
1032 output=_output, in_port=_in_port)
1033
1034 elif action.type == fd.POP_VLAN:
1035 self.log.debug('action-type-pop-vlan',
1036 in_port=_in_port)
1037
1038 elif action.type == fd.PUSH_VLAN:
1039 _push_tpid = action.push.ethertype
1040 self.log.debug('action-type-push-vlan',
1041 push_tpid=_push_tpid, in_port=_in_port)
1042 if action.push.ethertype != 0x8100:
1043 self.log.error('unhandled-tpid',
1044 ethertype=action.push.ethertype)
1045
1046 elif action.type == fd.SET_FIELD:
1047 _field = action.set_field.field.ofb_field
1048 assert (action.set_field.field.oxm_class ==
1049 OFPXMC_OPENFLOW_BASIC)
1050 self.log.debug('action-type-set-field',
1051 field=_field, in_port=_in_port)
1052 if _field.type == fd.VLAN_VID:
1053 _set_vlan_vid = _field.vlan_vid & 0xfff
1054 self.log.debug('set-field-type-vlan-vid',
1055 vlan_vid=_set_vlan_vid)
1056 elif _field.type == fd.VLAN_PCP:
1057 _set_vlan_pcp = _field.vlan_pcp
1058 self.log.debug('set-field-type-vlan-pcp',
1059 vlan_pcp=_set_vlan_pcp)
1060 else:
1061 self.log.error('unsupported-action-set-field-type',
1062 field_type=_field.type)
1063 else:
1064 self.log.error('unsupported-action-type',
1065 action_type=action.type, in_port=_in_port)
1066
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001067 if self._set_vlan is not None:
1068 if uni_id not in self._set_vlan:
1069 self._set_vlan[uni_id] = dict()
1070 self._set_vlan[uni_id][tp_id] = _set_vlan_vid
1071 self.log.debug("set_vlan_id-for-tp", _set_vlan_vid=_set_vlan_vid, tp_id=tp_id)
1072
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001073 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
1074 # supported match and action criteria cannot be handled by omci and must be ignored.
1075 if (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid != RESERVED_TRANSPARENT_VLAN:
1076 self.log.warn('ignoring-flow-that-does-not-set-vlanid', set_vlan_vid=_set_vlan_vid)
1077 elif (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid == RESERVED_TRANSPARENT_VLAN:
1078 self.log.info('set-vlanid-any', uni_id=uni_id, uni_port=uni_port,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001079 _set_vlan_vid=_vlan_vid,
1080 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1081 tp_id=tp_id)
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001082 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1083 _set_vlan_vid=_vlan_vid,
1084 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1085 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001086 else:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001087 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, match_vlan=_vlan_vid,
1088 set_vlan_vid=_set_vlan_vid, _set_vlan_pcp=_set_vlan_pcp, ethType=_type)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001089 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1090 _set_vlan_vid=_set_vlan_vid,
1091 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1092 tp_id=tp_id)
1093
1094 except Exception as e:
1095 self.log.exception('failed-to-install-flow', e=e, flow=flow)
1096
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001097 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
1098 # flow decomposition that ultimately comes from onos
1099 def update_flow_table(self, device, flows):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001100 self.log.debug('update-flow-table', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001101
1102 #
1103 # We need to proxy through the OLT to get to the ONU
1104 # Configuration from here should be using OMCI
1105 #
1106 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
1107
1108 # no point in pushing omci flows if the device isnt reachable
1109 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdrae933cd32019-11-21 21:04:41 +05301110 device.admin_state != AdminState.ENABLED:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001111 self.log.warn("device-disabled-or-offline-skipping-flow-update",
1112 admin=device.admin_state, connect=device.connect_status)
1113 return
1114
1115 def is_downstream(port):
1116 return port == self._pon_port_number
1117
1118 def is_upstream(port):
1119 return not is_downstream(port)
1120
1121 for flow in flows:
1122 _type = None
1123 _port = None
1124 _vlan_vid = None
1125 _udp_dst = None
1126 _udp_src = None
1127 _ipv4_dst = None
1128 _ipv4_src = None
1129 _metadata = None
1130 _output = None
1131 _push_tpid = None
1132 _field = None
1133 _set_vlan_vid = None
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001134 _set_vlan_pcp = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -04001135 _tunnel_id = None
1136
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001137 try:
Girish Gowdraa73ee452019-12-20 18:52:17 +05301138 write_metadata = fd.get_write_metadata(flow)
1139 if write_metadata is None:
1140 self.log.error("do-not-process-flow-without-write-metadata")
1141 return
1142
1143 # extract tp id from flow
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001144 tp_id = self.get_tp_id_in_flow(flow)
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001145 self.log.debug("tp-id-in-flow", tp_id=tp_id)
Girish Gowdraa73ee452019-12-20 18:52:17 +05301146
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001147 _in_port = fd.get_in_port(flow)
1148 assert _in_port is not None
1149
1150 _out_port = fd.get_out_port(flow) # may be None
1151
1152 if is_downstream(_in_port):
1153 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
1154 uni_port = self.uni_port(_out_port)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301155 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001156 elif is_upstream(_in_port):
1157 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
1158 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001159 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001160 else:
1161 raise Exception('port should be 1 or 2 by our convention')
1162
1163 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
1164
1165 for field in fd.get_ofb_fields(flow):
1166 if field.type == fd.ETH_TYPE:
1167 _type = field.eth_type
1168 self.log.debug('field-type-eth-type',
1169 eth_type=_type)
1170
1171 elif field.type == fd.IP_PROTO:
1172 _proto = field.ip_proto
1173 self.log.debug('field-type-ip-proto',
1174 ip_proto=_proto)
1175
1176 elif field.type == fd.IN_PORT:
1177 _port = field.port
1178 self.log.debug('field-type-in-port',
1179 in_port=_port)
1180
1181 elif field.type == fd.VLAN_VID:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001182 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
1183 _vlan_vid = RESERVED_TRANSPARENT_VLAN
1184 else:
1185 _vlan_vid = field.vlan_vid & 0xfff
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001186 self.log.debug('field-type-vlan-vid',
1187 vlan=_vlan_vid)
1188
1189 elif field.type == fd.VLAN_PCP:
1190 _vlan_pcp = field.vlan_pcp
1191 self.log.debug('field-type-vlan-pcp',
1192 pcp=_vlan_pcp)
1193
1194 elif field.type == fd.UDP_DST:
1195 _udp_dst = field.udp_dst
1196 self.log.debug('field-type-udp-dst',
1197 udp_dst=_udp_dst)
1198
1199 elif field.type == fd.UDP_SRC:
1200 _udp_src = field.udp_src
1201 self.log.debug('field-type-udp-src',
1202 udp_src=_udp_src)
1203
1204 elif field.type == fd.IPV4_DST:
1205 _ipv4_dst = field.ipv4_dst
1206 self.log.debug('field-type-ipv4-dst',
1207 ipv4_dst=_ipv4_dst)
1208
1209 elif field.type == fd.IPV4_SRC:
1210 _ipv4_src = field.ipv4_src
1211 self.log.debug('field-type-ipv4-src',
1212 ipv4_dst=_ipv4_src)
1213
1214 elif field.type == fd.METADATA:
1215 _metadata = field.table_metadata
1216 self.log.debug('field-type-metadata',
1217 metadata=_metadata)
1218
Matt Jeanneretef06d0d2019-04-27 17:36:53 -04001219 elif field.type == fd.TUNNEL_ID:
1220 _tunnel_id = field.tunnel_id
1221 self.log.debug('field-type-tunnel-id',
1222 tunnel_id=_tunnel_id)
1223
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001224
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001225 else:
1226 raise NotImplementedError('field.type={}'.format(
1227 field.type))
1228
1229 for action in fd.get_actions(flow):
1230
1231 if action.type == fd.OUTPUT:
1232 _output = action.output.port
1233 self.log.debug('action-type-output',
1234 output=_output, in_port=_in_port)
1235
1236 elif action.type == fd.POP_VLAN:
1237 self.log.debug('action-type-pop-vlan',
1238 in_port=_in_port)
1239
1240 elif action.type == fd.PUSH_VLAN:
1241 _push_tpid = action.push.ethertype
1242 self.log.debug('action-type-push-vlan',
1243 push_tpid=_push_tpid, in_port=_in_port)
1244 if action.push.ethertype != 0x8100:
1245 self.log.error('unhandled-tpid',
1246 ethertype=action.push.ethertype)
1247
1248 elif action.type == fd.SET_FIELD:
1249 _field = action.set_field.field.ofb_field
1250 assert (action.set_field.field.oxm_class ==
1251 OFPXMC_OPENFLOW_BASIC)
1252 self.log.debug('action-type-set-field',
1253 field=_field, in_port=_in_port)
1254 if _field.type == fd.VLAN_VID:
1255 _set_vlan_vid = _field.vlan_vid & 0xfff
1256 self.log.debug('set-field-type-vlan-vid',
1257 vlan_vid=_set_vlan_vid)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001258 elif _field.type == fd.VLAN_PCP:
1259 _set_vlan_pcp = _field.vlan_pcp
1260 self.log.debug('set-field-type-vlan-pcp',
1261 vlan_pcp=_set_vlan_pcp)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001262 else:
1263 self.log.error('unsupported-action-set-field-type',
1264 field_type=_field.type)
1265 else:
1266 self.log.error('unsupported-action-type',
1267 action_type=action.type, in_port=_in_port)
1268
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001269 if self._set_vlan is not None:
1270 if uni_id not in self._set_vlan:
1271 self._set_vlan[uni_id] = dict()
1272 self._set_vlan[uni_id][tp_id] = _set_vlan_vid
1273 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 -04001274 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
1275 # supported match and action criteria cannot be handled by omci and must be ignored.
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001276 if (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid != RESERVED_TRANSPARENT_VLAN:
1277 self.log.warn('ignoring-flow-that-does-not-set-vlanid', set_vlan_vid=_set_vlan_vid)
1278 elif (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid == RESERVED_TRANSPARENT_VLAN:
1279 self.log.info('set-vlanid-any', uni_id=uni_id, uni_port=uni_port,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001280 _set_vlan_vid=_vlan_vid,
1281 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1282 tp_id=tp_id)
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001283 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1284 _set_vlan_vid=_vlan_vid,
1285 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1286 tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001287 else:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001288 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, match_vlan=_vlan_vid,
1289 set_vlan_vid=_set_vlan_vid, _set_vlan_pcp=_set_vlan_pcp, ethType=_type)
1290 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1291 _set_vlan_vid=_set_vlan_vid,
1292 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1293 tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001294 except Exception as e:
1295 self.log.exception('failed-to-install-flow', e=e, flow=flow)
1296
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001297 def _add_vlan_filter_task(self, device, uni_id, uni_port=None, match_vlan=0,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001298 _set_vlan_vid=None, _set_vlan_pcp=8, tp_id=0):
Girish Gowdradc98d812020-03-20 13:04:58 -07001299 if uni_id in self._pending_delete_tp and tp_id in self._pending_delete_tp[uni_id] and \
1300 self._pending_delete_tp[uni_id][tp_id] is True:
1301 self.log.debug("pending-del-tp--scheduling-add-vlan-filter-task-for-later")
1302 reactor.callLater(0.2, self._add_vlan_filter_task, device, uni_id, uni_port, match_vlan,
1303 _set_vlan_vid, _set_vlan_pcp, tp_id)
1304 return
1305
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001306 self.log.info('_adding_vlan_filter_task', uni_port=uni_port, uni_id=uni_id, tp_id=tp_id, match_vlan=match_vlan,
1307 vlan=_set_vlan_vid, vlan_pcp=_set_vlan_pcp)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001308 assert uni_port is not None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001309 if uni_id in self._tech_profile_download_done and tp_id in self._tech_profile_download_done[uni_id] and \
1310 self._tech_profile_download_done[uni_id][tp_id] is True:
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001311 @inlineCallbacks
1312 def success(_results):
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001313 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid, tp_id=tp_id,
1314 set_vlan_pcp=_set_vlan_pcp)
Matt Jeanneretd84c9072020-01-31 06:33:27 -05001315 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-pushed')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001316
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001317 @inlineCallbacks
1318 def failure(_reason):
Girish Gowdraa73ee452019-12-20 18:52:17 +05301319 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 -08001320 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1321 reactor.callLater(retry,
1322 self._add_vlan_filter_task, device, uni_id, uni_port=uni_port,
1323 match_vlan=match_vlan, _set_vlan_vid=_set_vlan_vid,
1324 _set_vlan_pcp=_set_vlan_pcp, tp_id=tp_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -05001325 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-failed-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001326
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001327 self.log.info('setting-vlan-tag', uni_port=uni_port, uni_id=uni_id, tp_id=tp_id, match_vlan=match_vlan,
1328 vlan=_set_vlan_vid, vlan_pcp=_set_vlan_pcp)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001329 vlan_filter_add_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001330 match_vlan, _set_vlan_pcp, add_tag=True,
1331 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001332 self._deferred = self._onu_omci_device.task_runner.queue_task(vlan_filter_add_task)
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001333 self._deferred.addCallbacks(success, failure)
1334 else:
1335 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001336 uni_id=uni_id, tp_id=tp_id)
1337 if uni_id not in self._queued_vlan_filter_task:
1338 self._queued_vlan_filter_task[uni_id] = dict()
Mahir Gunyela982ec32020-02-25 12:30:37 -08001339 if tp_id not in self._queued_vlan_filter_task[uni_id]:
1340 self._queued_vlan_filter_task[uni_id][tp_id] = []
1341 self._queued_vlan_filter_task[uni_id][tp_id].append({"device": device,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001342 "uni_id": uni_id,
1343 "uni_port": uni_port,
1344 "match_vlan": match_vlan,
1345 "set_vlan_vid": _set_vlan_vid,
1346 "set_vlan_pcp": _set_vlan_pcp,
1347 "tp_id": tp_id})
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001348
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001349 def get_tp_id_in_flow(self, flow):
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001350 flow_metadata = fd.get_metadata_from_write_metadata(flow)
1351 tp_id = fd.get_tp_id_from_metadata(flow_metadata)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001352 return tp_id
1353
1354 def _remove_vlan_filter_task(self, device, uni_id, uni_port=None, match_vlan=0,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001355 _set_vlan_vid=None, _set_vlan_pcp=8, tp_id=0):
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001356 assert uni_port is not None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001357
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001358 @inlineCallbacks
1359 def success(_results):
1360 self.log.info('vlan-untagging-success', _results=_results)
1361 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-deleted')
1362
1363 @inlineCallbacks
1364 def failure(_reason):
1365 self.log.warn('vlan-untagging-failure', _reason=_reason)
1366 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-deletion-failed-retrying')
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001367 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001368 reactor.callLater(retry,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001369 self._remove_vlan_filter_task, device, uni_id,
1370 add_tag=False, uni_port=uni_port)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001371
1372 self.log.info("remove_vlan_filter_task", tp_id=tp_id)
1373 vlan_remove_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001374 match_vlan, _set_vlan_pcp, add_tag=False,
1375 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001376 self._deferred = self._onu_omci_device.task_runner.queue_task(vlan_remove_task)
1377 self._deferred.addCallbacks(success, failure)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001378
Matt Jeanneret5e331892019-12-07 21:31:45 -05001379 @inlineCallbacks
Matt Jeannereta32441c2019-03-07 05:16:37 -05001380 def process_inter_adapter_message(self, request):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001381 self.log.debug('process-inter-adapter-message', type=request.header.type, from_topic=request.header.from_topic,
1382 to_topic=request.header.to_topic, to_device_id=request.header.to_device_id)
Matt Jeanneret2101f3d2020-03-12 10:13:06 -04001383
1384 if not self.enabled:
1385 self.log.warn('device-not-activated')
1386 reactor.callLater(0.5, self.process_inter_adapter_message, request)
1387 return
1388
Matt Jeannereta32441c2019-03-07 05:16:37 -05001389 try:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001390
1391 update_onu_state = False
1392
Matt Jeannereta32441c2019-03-07 05:16:37 -05001393 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
1394 omci_msg = InterAdapterOmciMessage()
1395 request.body.Unpack(omci_msg)
Matteo Scandolod8d73172019-11-26 12:15:15 -07001396 self.log.debug('inter-adapter-recv-omci', omci_msg=hexify(omci_msg.message))
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001397
Matt Jeannereta32441c2019-03-07 05:16:37 -05001398 self.receive_message(omci_msg.message)
1399
1400 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
1401 onu_indication = OnuIndication()
1402 request.body.Unpack(onu_indication)
Matteo Scandolod8d73172019-11-26 12:15:15 -07001403 self.log.debug('inter-adapter-recv-onu-ind', onu_id=onu_indication.onu_id,
1404 oper_state=onu_indication.oper_state, admin_state=onu_indication.admin_state,
1405 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001406
Matt Jeanneret5e331892019-12-07 21:31:45 -05001407 update_onu_state = True
1408 self._onu_persisted_state['onu_id'] = onu_indication.onu_id
1409 self._onu_persisted_state['intf_id'] = onu_indication.intf_id
1410 self._onu_persisted_state['admin_state'] = onu_indication.admin_state
Mahir Gunyel45610b42020-03-16 17:29:01 -07001411 self._onu_persisted_state['oper_state'] = onu_indication.oper_state
Matt Jeanneret5e331892019-12-07 21:31:45 -05001412
Matt Jeannereta32441c2019-03-07 05:16:37 -05001413 if onu_indication.oper_state == "up":
Matt Jeanneret5e331892019-12-07 21:31:45 -05001414 yield self.create_interface(onu_indication)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301415 elif onu_indication.oper_state == "down" or onu_indication.oper_state == "unreachable":
Matt Jeanneret5e331892019-12-07 21:31:45 -05001416 yield self.update_interface(onu_indication)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001417 else:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001418 self.log.error("unknown-onu-indication", onu_id=onu_indication.onu_id,
1419 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001420
Matt Jeanneret3bfebff2019-04-12 18:25:03 -04001421 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
1422 tech_msg = InterAdapterTechProfileDownloadMessage()
1423 request.body.Unpack(tech_msg)
1424 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
1425
Matt Jeanneret5e331892019-12-07 21:31:45 -05001426 update_onu_state = self._update_onu_persisted_state(tech_msg.uni_id, tp_path=tech_msg.path)
1427 yield self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
Matt Jeanneret3bfebff2019-04-12 18:25:03 -04001428
Girish Gowdrae933cd32019-11-21 21:04:41 +05301429 elif request.header.type == InterAdapterMessageType.DELETE_GEM_PORT_REQUEST:
1430 del_gem_msg = InterAdapterDeleteGemPortMessage()
1431 request.body.Unpack(del_gem_msg)
1432 self.log.debug('inter-adapter-recv-del-gem', gem_del_msg=del_gem_msg)
1433
1434 self.delete_tech_profile(uni_id=del_gem_msg.uni_id,
1435 gem_port_id=del_gem_msg.gem_port_id,
1436 tp_path=del_gem_msg.tp_path)
1437
1438 elif request.header.type == InterAdapterMessageType.DELETE_TCONT_REQUEST:
1439 del_tcont_msg = InterAdapterDeleteTcontMessage()
1440 request.body.Unpack(del_tcont_msg)
1441 self.log.debug('inter-adapter-recv-del-tcont', del_tcont_msg=del_tcont_msg)
1442
Matt Jeanneret5e331892019-12-07 21:31:45 -05001443 # Removal of the tcont/alloc id mapping represents the removal of the tech profile
1444 update_onu_state = self._update_onu_persisted_state(del_tcont_msg.uni_id, tp_path=None)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301445 self.delete_tech_profile(uni_id=del_tcont_msg.uni_id,
1446 alloc_id=del_tcont_msg.alloc_id,
1447 tp_path=del_tcont_msg.tp_path)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001448 else:
1449 self.log.error("inter-adapter-unhandled-type", request=request)
1450
Matt Jeanneret5e331892019-12-07 21:31:45 -05001451 if update_onu_state:
1452 try:
1453 self.log.debug('updating-onu-state', device_id=self.device_id,
1454 onu_persisted_state=self._onu_persisted_state)
1455 yield self.onu_kv_client.set(self.device_id, json.dumps(self._onu_persisted_state))
1456 except Exception as e:
1457 self.log.error('could-not-store-onu-state', device_id=self.device_id,
1458 onu_persisted_state=self._onu_persisted_state, e=e)
1459 # at this point omci is started and/or indications being processed
1460 # later indications may have a chance to write this state out again
1461
Matt Jeannereta32441c2019-03-07 05:16:37 -05001462 except Exception as e:
1463 self.log.exception("error-processing-inter-adapter-message", e=e)
1464
Matt Jeanneret5e331892019-12-07 21:31:45 -05001465 def _update_onu_persisted_state(self, uni_id, tp_path):
1466 # persist the uni and tech profile path for later reconciliation. update only if changed
1467 update_onu_state = False
1468 found = False
1469 for entry in self._onu_persisted_state.get('uni_config', list()):
1470 if entry.get('uni_id') == uni_id:
1471 found = True
1472 if entry.get('tp_path') != tp_path:
1473 update_onu_state = True
1474 entry['tp_path'] = tp_path
1475
1476 if not found:
1477 update_onu_state = True
1478 uni_tp = {
1479 'uni_id': uni_id,
1480 'tp_path': tp_path
1481 }
1482 self._onu_persisted_state['uni_config'].append(uni_tp)
1483
1484 return update_onu_state
1485
Matt Jeannereta32441c2019-03-07 05:16:37 -05001486 # Called each time there is an onu "up" indication from the olt handler
1487 @inlineCallbacks
1488 def create_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001489 self.log.info('create-interface', onu_id=onu_indication.onu_id,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001490 serial_number=onu_indication.serial_number)
Amit Ghosh028eb202020-02-17 13:34:00 +00001491
1492 # Ignore if onu_indication is received for an already running ONU
1493 if self._onu_omci_device is not None and self._onu_omci_device.active:
1494 self.log.warn('received-onu-indication-for-active-onu', onu_indication=onu_indication)
1495 return
1496
Matt Jeanneretc083f462019-03-11 15:02:01 -04001497 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
1498 connect_status=ConnectStatus.REACHABLE)
1499
Matt Jeannereta32441c2019-03-07 05:16:37 -05001500 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001501
1502 self.log.debug('starting-openomci-statemachine')
1503 self._subscribe_to_events()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001504 onu_device.reason = "starting-openomci"
Girish Gowdrae933cd32019-11-21 21:04:41 +05301505 reactor.callLater(1, self._onu_omci_device.start, onu_device)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001506 yield self.core_proxy.device_reason_update(self.device_id, onu_device.reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001507 self._heartbeat.enabled = True
1508
Matt Jeanneret42dad792020-02-01 09:28:27 -05001509 # Called each time there is an onu "down" indication from the olt handler
Matt Jeannereta32441c2019-03-07 05:16:37 -05001510 @inlineCallbacks
1511 def update_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001512 self.log.info('update-interface', onu_id=onu_indication.onu_id,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001513 serial_number=onu_indication.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001514
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -04001515 if onu_indication.oper_state == 'down' or onu_indication.oper_state == "unreachable":
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001516 self.log.debug('stopping-openomci-statemachine', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001517 reactor.callLater(0, self._onu_omci_device.stop)
1518
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001519 self._tp = dict()
1520
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001521 # Let TP download happen again
1522 for uni_id in self._tp_service_specific_task:
1523 self._tp_service_specific_task[uni_id].clear()
1524 for uni_id in self._tech_profile_download_done:
1525 self._tech_profile_download_done[uni_id].clear()
1526
Matt Jeanneretf4113222019-08-14 19:44:34 -04001527 yield self.disable_ports(lock_ports=False)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001528 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
1529 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.DISCOVERED,
1530 connect_status=ConnectStatus.UNREACHABLE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001531 else:
1532 self.log.debug('not-changing-openomci-statemachine')
1533
Matt Jeanneretf4113222019-08-14 19:44:34 -04001534 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001535 def disable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001536 self.log.info('disable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001537 try:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001538 yield self.disable_ports(lock_ports=True, device_disabled=True)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001539 yield self.core_proxy.device_reason_update(self.device_id, "omci-admin-lock")
1540 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001541 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001542 self.log.exception('exception-in-onu-disable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001543
William Kurkian3a206332019-04-29 11:05:47 -04001544 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001545 def reenable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001546 self.log.info('reenable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001547 try:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001548 yield self.core_proxy.device_state_update(device.id,
1549 oper_status=OperStatus.ACTIVE,
1550 connect_status=ConnectStatus.REACHABLE)
1551 yield self.core_proxy.device_reason_update(self.device_id, 'onu-reenabled')
1552 yield self.enable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001553 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001554 self.log.exception('exception-in-onu-reenable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001555
William Kurkian3a206332019-04-29 11:05:47 -04001556 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001557 def reboot(self):
1558 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -04001559 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001560 if device.connect_status != ConnectStatus.REACHABLE:
1561 self.log.error("device-unreachable")
1562 return
1563
William Kurkian3a206332019-04-29 11:05:47 -04001564 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001565 def success(_results):
1566 self.log.info('reboot-success', _results=_results)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001567 yield self.core_proxy.device_reason_update(self.device_id, 'rebooting')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001568
1569 def failure(_reason):
1570 self.log.info('reboot-failure', _reason=_reason)
1571
1572 self._deferred = self._onu_omci_device.reboot()
1573 self._deferred.addCallbacks(success, failure)
1574
William Kurkian3a206332019-04-29 11:05:47 -04001575 @inlineCallbacks
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001576 def disable_ports(self, lock_ports=True, device_disabled=False):
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001577 self.log.info('disable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001578
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001579 # TODO: for now only support the first UNI given no requirement for multiple uni yet. Also needed to reduce flow
1580 # load on the core
Matt Jeanneretf4113222019-08-14 19:44:34 -04001581 for port in self.uni_ports:
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001582 if port.mac_bridge_port_num == 1:
1583 port.operstatus = OperStatus.UNKNOWN
1584 self.log.info('disable-port', device_id=self.device_id, port=port)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001585 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number,
1586 port.operstatus)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001587
1588 if lock_ports is True:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001589 self.lock_ports(lock=True, device_disabled=device_disabled)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001590
William Kurkian3a206332019-04-29 11:05:47 -04001591 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001592 def enable_ports(self):
1593 self.log.info('enable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001594
Matt Jeanneretf4113222019-08-14 19:44:34 -04001595 self.lock_ports(lock=False)
1596
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001597 # TODO: for now only support the first UNI given no requirement for multiple uni yet. Also needed to reduce flow
1598 # load on the core
1599 # Given by default all unis are initially active according to omci alarming, we must mimic this.
Matt Jeanneretf4113222019-08-14 19:44:34 -04001600 for port in self.uni_ports:
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001601 if port.mac_bridge_port_num == 1:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001602 port.operstatus = OperStatus.ACTIVE
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001603 self.log.info('enable-port', device_id=self.device_id, port=port)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001604 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number,
1605 port.operstatus)
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001606
1607 # TODO: Normally we would want any uni ethernet link down or uni ethernet link up alarms to register in the core,
1608 # but practically olt provisioning cannot handle the churn of links up, down, then up again typical on startup.
1609 #
1610 # Basically the link state sequence:
1611 # 1) per omci default alarm state, all unis are initially up (no link down alarms received yet)
1612 # 2) a link state down alarm is received for all uni, given the lock command, and also because most unis have nothing plugged in
1613 # 3) a link state up alarm is received for the uni plugged in.
1614 #
1615 # Given the olt (BAL) has to provision all uni, de-provision all uni, and re-provision one uni in quick succession
1616 # and cannot (bug?), we have to skip this and leave uni ports as assumed active. Also all the link state activity
1617 # would have a ripple effect through the core to the controller as well. And is it really worth it?
1618 '''
Matt Jeanneretf4113222019-08-14 19:44:34 -04001619 @inlineCallbacks
1620 def port_state_handler(self, _topic, msg):
1621 self.log.info("port-state-change", _topic=_topic, msg=msg)
1622
1623 onu_id = msg['onu_id']
1624 port_no = msg['port_number']
1625 serial_number = msg['serial_number']
1626 port_status = msg['port_status']
1627 uni_port = self.uni_port(int(port_no))
1628
1629 self.log.debug("port-state-parsed-message", onu_id=onu_id, port_no=port_no, serial_number=serial_number,
1630 port_status=port_status)
1631
1632 if port_status is True:
1633 uni_port.operstatus = OperStatus.ACTIVE
1634 self.log.info('link-up', device_id=self.device_id, port=uni_port)
1635 else:
1636 uni_port.operstatus = OperStatus.UNKNOWN
1637 self.log.info('link-down', device_id=self.device_id, port=uni_port)
1638
1639 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 -05001640 '''
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001641
1642 # Called just before openomci state machine is started. These listen for events from selected state machines,
1643 # most importantly, mib in sync. Which ultimately leads to downloading the mib
1644 def _subscribe_to_events(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001645 self.log.debug('subscribe-to-events')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001646
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001647 bus = self._onu_omci_device.event_bus
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001648
1649 # OMCI MIB Database sync status
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001650 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1651 OnuDeviceEvents.MibDatabaseSyncEvent)
1652 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
1653
1654 # OMCI Capabilities
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001655 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1656 OnuDeviceEvents.OmciCapabilitiesEvent)
1657 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
1658
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001659 # TODO: these alarms seem to be unreliable depending on the environment
1660 # 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 -08001661 # topic = OnuDeviceEntry.event_bus_topic(self.device_id,
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001662 # OnuDeviceEvents.PortEvent)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001663 # self._port_state_subscription = bus.subscribe(topic, self.port_state_handler)
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001664
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001665 # Called when the mib is in sync
1666 def in_sync_handler(self, _topic, msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001667 self.log.debug('in-sync-handler', _topic=_topic, msg=msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001668 if self._in_sync_subscription is not None:
1669 try:
1670 in_sync = msg[IN_SYNC_KEY]
1671
1672 if in_sync:
1673 # Only call this once
1674 bus = self._onu_omci_device.event_bus
1675 bus.unsubscribe(self._in_sync_subscription)
1676 self._in_sync_subscription = None
1677
1678 # Start up device_info load
1679 self.log.debug('running-mib-sync')
1680 reactor.callLater(0, self._mib_in_sync)
1681
1682 except Exception as e:
1683 self.log.exception('in-sync', e=e)
1684
1685 def capabilties_handler(self, _topic, _msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001686 self.log.debug('capabilities-handler', _topic=_topic, msg=_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001687 if self._capabilities_subscription is not None:
1688 self.log.debug('capabilities-handler-done')
1689
1690 # 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 -04001691 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001692 def _mib_in_sync(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001693 self.log.debug('mib-in-sync')
Matt Jeanneretc083f462019-03-11 15:02:01 -04001694 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001695
Matt Jeanneret5e331892019-12-07 21:31:45 -05001696 # only notify core if this is a new device. otherwise do not have reconcile generating
1697 # a lot of needless message churn
1698 if not self._reconciling:
1699 yield self.core_proxy.device_reason_update(self.device_id, 'discovery-mibsync-complete')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001700
1701 if self._dev_info_loaded:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001702 self.log.debug('device-info-already-loaded')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001703 else:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001704 # new onu or adapter was restarted. fill up our local data
1705 yield self._load_device_data(device)
1706
1707 if self._check_mib_downloaded():
1708 self.log.debug('mib-already-downloaded')
1709 if not self._reconciling:
1710 yield self.core_proxy.device_state_update(device.id,
1711 oper_status=OperStatus.ACTIVE,
1712 connect_status=ConnectStatus.REACHABLE)
1713 yield self.enable_ports()
1714 else:
1715 self._download_mib(device)
1716
1717 if self._reconciling:
1718 yield self._restore_tech_profile()
1719 self._start_monitoring()
1720 self._reconciling = False
1721 self.log.debug('reconcile-finished')
1722
1723 def _download_mib(self, device):
1724 self.log.debug('downloading-initial-mib-configuration')
1725
1726 @inlineCallbacks
1727 def success(_results):
1728 self.log.debug('mib-download-success', _results=_results)
1729 yield self.core_proxy.device_state_update(device.id,
1730 oper_status=OperStatus.ACTIVE,
1731 connect_status=ConnectStatus.REACHABLE)
1732 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-downloaded')
1733 self._mib_download_task = None
1734 yield self.enable_ports()
1735 yield self.onu_active_event()
1736 self._start_monitoring()
1737
1738 @inlineCallbacks
1739 def failure(_reason):
1740 self.log.warn('mib-download-failure-retrying', _reason=_reason)
1741 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1742 reactor.callLater(retry, self._mib_in_sync)
1743 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-download-failure-retrying')
1744
1745 # start by locking all the unis till mib sync and initial mib is downloaded
1746 # this way we can capture the port down/up events when we are ready
1747 self.lock_ports(lock=True)
1748
1749 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1750 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1751 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1752 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1753 self._deferred.addCallbacks(success, failure)
1754
1755 def _start_monitoring(self):
1756 self.log.debug('starting-monitoring')
1757
1758 # Start collecting stats from the device after a brief pause
1759 if not self._pm_metrics_started:
1760 self._pm_metrics_started = True
1761 pmstart = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1762 reactor.callLater(pmstart, self._pm_metrics.start_collector)
1763
1764 # Start test requests after a brief pause
1765 if not self._test_request_started:
1766 self._test_request_started = True
1767 tststart = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1768 reactor.callLater(tststart, self._test_request.start_collector)
1769
1770 def _check_mib_downloaded(self):
1771 self.log.debug('checking-mib-downloaded')
1772 results = False
1773
1774 mac_bridges = self.onu_omci_device.query_mib(MacBridgeServiceProfile.class_id)
1775 self.log.debug('mac-bridges', mac_bridges=mac_bridges)
1776
1777 for k, v in mac_bridges.items():
1778 if not isinstance(v, dict):
1779 continue
1780 # found at least one mac bridge, good enough to say its done, break out
1781 self.log.debug('found-mac-bridge-mib-download-has-been-done', omci_key=k, omci_value=v)
1782 results = True
1783 break
1784
1785 return results
1786
1787 @inlineCallbacks
1788 def _load_device_data(self, device):
1789 self.log.debug('loading-device-data-from-mib', device_id=device.id)
1790
1791 omci_dev = self._onu_omci_device
1792 config = omci_dev.configuration
1793
1794 try:
1795 # sort the lists so we get consistent port ordering.
1796 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
1797 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
1798 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
1799 veip_list = sorted(config.veip_entities) if config.veip_entities else []
1800
1801 if ani_list is None or (pptp_list is None and veip_list is None):
1802 yield self.core_proxy.device_reason_update(self.device_id, 'onu-missing-required-elements')
1803 raise Exception("onu-missing-required-elements")
1804
1805 # Currently logging the ani, pptp, veip, and uni for information purposes.
1806 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
1807 # And in some ONU the UNI-G list is incomplete or incorrect...
1808 for entity_id in ani_list:
1809 ani_value = config.ani_g_entities[entity_id]
1810 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
1811
1812 for entity_id in uni_list:
1813 uni_value = config.uni_g_entities[entity_id]
1814 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
1815
1816 uni_entities = OrderedDict()
1817 for entity_id in pptp_list:
1818 pptp_value = config.pptp_entities[entity_id]
1819 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
1820 uni_entities[entity_id] = UniType.PPTP
1821
1822 for entity_id in veip_list:
1823 veip_value = config.veip_entities[entity_id]
1824 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
1825 uni_entities[entity_id] = UniType.VEIP
1826
1827 uni_id = 0
1828 for entity_id, uni_type in uni_entities.items():
1829 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
1830 uni_id += 1
1831
1832 if self._unis:
1833 self._dev_info_loaded = True
1834 else:
1835 yield self.core_proxy.device_reason_update(self.device_id, 'no-usable-unis')
1836 raise Exception("no-usable-unis")
1837
1838 except Exception as e:
1839 self.log.exception('device-info-load', e=e)
1840 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001841
Matt Jeanneretc083f462019-03-11 15:02:01 -04001842 @inlineCallbacks
1843 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matt Jeanneret5e331892019-12-07 21:31:45 -05001844 self.log.debug('add-uni-port', entity_id=entity_id, uni_id=uni_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001845
Matt Jeanneret5e331892019-12-07 21:31:45 -05001846 intf_id = self._onu_persisted_state.get('intf_id')
1847 onu_id = self._onu_persisted_state.get('onu_id')
1848 uni_no = self.mk_uni_port_num(intf_id, onu_id, uni_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001849
1850 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1851 uni_name = "uni-{}".format(uni_no)
1852
Girish Gowdrae933cd32019-11-21 21:04:41 +05301853 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001854
1855 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 -04001856 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001857
1858 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1859 uni_port.entity_id = entity_id
1860 uni_port.enabled = True
1861 uni_port.mac_bridge_port_num = mac_bridge_port_num
1862
1863 self.log.debug("created-uni-port", uni=uni_port)
1864
Matt Jeanneret5e331892019-12-07 21:31:45 -05001865 if not self._reconciling:
1866 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001867
1868 self._unis[uni_port.port_number] = uni_port
1869
Matt Jeanneret5e331892019-12-07 21:31:45 -05001870 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=onu_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301871 uni_ports=self.uni_ports,
1872 serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001873
Matt Jeanneret5e331892019-12-07 21:31:45 -05001874 @inlineCallbacks
1875 def _restore_tech_profile(self):
1876 self.log.debug("reconcile-restoring-tech-profile-tcont-gem-config")
1877
1878 # for every uni that has tech profile config reload all its tcont/alloc_id and gem from the tp path
1879 for entry in self._onu_persisted_state.get('uni_config', list()):
1880 uni_id = entry.get('uni_id')
1881 tp_path = entry.get('tp_path')
1882 if tp_path:
1883 tpstored = yield self.tp_kv_client.get(tp_path)
1884 tpstring = tpstored.decode('ascii')
1885 tp = json.loads(tpstring)
1886
1887 self.log.debug("restoring-tp-instance", tp=tp)
1888
1889 # re-run tech profile config that stores gem and tconts in the self._pon object
1890 # this does not actually re-run the omci, just rebuilds our local data store
1891 self._do_tech_profile_configuration(uni_id, tp)
1892
1893 tp_id = self.extract_tp_id_from_path(tp_path)
1894
1895 # rebuild cache dicts so tp updates and deletes dont get KeyErrors
1896 if uni_id not in self._tp_service_specific_task:
1897 self._tp_service_specific_task[uni_id] = dict()
1898
1899 if uni_id not in self._tech_profile_download_done:
1900 self._tech_profile_download_done[uni_id] = dict()
1901
1902 if tp_id not in self._tech_profile_download_done[uni_id]:
1903 self._tech_profile_download_done[uni_id][tp_id] = True
1904 else:
1905 self.log.debug("no-assigned-tp-instance", uni_id=uni_id)
1906
1907 # for every loaded tcont from tp check the mib database for its entity_id
1908 # needed for later tp deletes/adds
1909 tcont_idents = self.onu_omci_device.query_mib(Tcont.class_id)
1910 self.log.debug('tcont-idents', tcont_idents=tcont_idents)
1911
1912 for k, v in tcont_idents.items():
1913 if not isinstance(v, dict):
1914 continue
1915 alloc_check = v.get('attributes', {}).get('alloc_id', 0)
1916 tcont = self._pon.tconts.get(alloc_check)
1917 if tcont:
1918 tcont.entity_id = k
1919 self.log.debug('reassigning-tcont-entity-id', entity_id=tcont.entity_id,
1920 alloc_id=tcont.alloc_id)
1921
Matt Jeanneretc083f462019-03-11 15:02:01 -04001922 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1923 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
Amit Ghosh65400f12019-11-21 12:04:12 +00001924 MAX_PONS_PER_OLT = 256
1925 MAX_ONUS_PER_PON = 256
Matt Jeanneretc083f462019-03-11 15:02:01 -04001926 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001927
Matt Jeanneretc083f462019-03-11 15:02:01 -04001928 assert intf_id < MAX_PONS_PER_OLT
1929 assert onu_id < MAX_ONUS_PER_PON
1930 assert uni_id < MAX_UNIS_PER_ONU
Amit Ghosh65400f12019-11-21 12:04:12 +00001931 return intf_id << 12 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001932
1933 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001934 def onu_active_event(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001935 self.log.debug('onu-active-event')
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001936 try:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001937 # TODO: this is expensive for just getting the olt serial number. replace with direct api call
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001938 parent_device = yield self.core_proxy.get_device(self.parent_id)
1939 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001940 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001941
Matt Jeanneret5e331892019-12-07 21:31:45 -05001942 intf_id = self._onu_persisted_state.get('intf_id')
1943 onu_id = self._onu_persisted_state.get('onu_id')
1944 onu_serial = self._onu_persisted_state.get('serial_number')
1945
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001946 self.log.debug("onu-indication-context-data",
Matt Jeanneret5e331892019-12-07 21:31:45 -05001947 pon_id=intf_id,
1948 onu_id=onu_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301949 registration_id=self.device_id,
1950 device_id=self.device_id,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001951 onu_serial_number=onu_serial,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301952 olt_serial_number=olt_serial_number,
1953 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001954
Devmalya Paulffc89df2019-07-31 17:43:13 -04001955 self.log.debug("Trying-to-raise-onu-active-event")
1956 OnuActiveEvent(self.events, self.device_id,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001957 intf_id,
1958 onu_serial,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001959 str(self.device_id),
Girish Gowdrae933cd32019-11-21 21:04:41 +05301960 olt_serial_number, raised_ts,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001961 onu_id=onu_id).send(True)
Devmalya Paulffc89df2019-07-31 17:43:13 -04001962 except Exception as active_event_error:
1963 self.log.exception('onu-activated-event-error',
1964 errmsg=active_event_error.message)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001965
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001966 @inlineCallbacks
1967 def onu_disabled_event(self):
1968 self.log.debug('onu-disabled-event')
1969 try:
1970 device = yield self.core_proxy.get_device(self.device_id)
1971 parent_device = yield self.core_proxy.get_device(self.parent_id)
1972 olt_serial_number = parent_device.serial_number
1973 raised_ts = arrow.utcnow().timestamp
1974
1975 self.log.debug("onu-indication-context-data",
1976 pon_id=self._onu_indication.intf_id,
1977 onu_id=self._onu_indication.onu_id,
1978 registration_id=self.device_id,
1979 device_id=self.device_id,
1980 onu_serial_number=device.serial_number,
1981 olt_serial_number=olt_serial_number,
1982 raised_ts=raised_ts)
1983
1984 self.log.debug("Trying-to-raise-onu-disabled-event")
1985 OnuDisabledEvent(self.events, self.device_id,
Girish Gowdradc98d812020-03-20 13:04:58 -07001986 self._onu_indication.intf_id,
1987 device.serial_number,
1988 str(self.device_id),
1989 olt_serial_number, raised_ts,
1990 onu_id=self._onu_indication.onu_id).send(True)
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001991 except Exception as active_event_error:
1992 self.log.exception('onu-disabled-event-error',
1993 errmsg=active_event_error.message)
1994
1995 def lock_ports(self, lock=True, device_disabled=False):
Matt Jeanneretf4113222019-08-14 19:44:34 -04001996
1997 def success(response):
1998 self.log.debug('set-onu-ports-state', lock=lock, response=response)
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001999 if device_disabled:
2000 self.onu_disabled_event()
Matt Jeanneretf4113222019-08-14 19:44:34 -04002001
2002 def failure(response):
2003 self.log.error('cannot-set-onu-ports-state', lock=lock, response=response)
2004
2005 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=lock)
2006 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
2007 self._deferred.addCallbacks(success, failure)
Mahir Gunyele9110a32020-02-20 14:56:50 -08002008
2009 def extract_tp_id_from_path(self, tp_path):
2010 # tp_path is of the format <technology>/<table_id>/<uni_port_name>
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08002011 tp_id = int(tp_path.split(_PATH_SEPERATOR)[1])
2012 return tp_id
onkarkundargia1e2af22020-01-27 11:51:43 +05302013
2014 def start_omci_test_action(self, device, uuid):
2015 """
2016
2017 :param device:
2018 :return:
2019 """
2020 # Code to Run OMCI Test Action
2021 self.log.info('Omci-test-action-request-On', request=device.id)
2022 kwargs_omci_test_action = {
2023 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
2024 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
2025 }
2026 serial_number = device.serial_number
2027 if device.connect_status != ConnectStatus.REACHABLE or device.admin_state != AdminState.ENABLED:
2028 return (TestResponse(result=TestResponse.FAILURE))
2029 test_request = OmciTestRequest(self.core_proxy,
2030 self.omci_agent, self.device_id, AniG,
2031 serial_number,
2032 self.logical_device_id, exclusive=False,
2033 uuid=uuid,
2034 **kwargs_omci_test_action)
2035 test_request.perform_test_omci()
2036 return (TestResponse(result=TestResponse.SUCCESS))