blob: 9e3f6c1a19fde3e3be8637ed7d1ff6679f5928a9 [file] [log] [blame]
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001#
2# Copyright 2017 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17"""
18Broadcom OpenOMCI OLT/ONU adapter handler.
19"""
20
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050021from __future__ import absolute_import
22import six
Devmalya Paulffc89df2019-07-31 17:43:13 -040023import arrow
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050024import structlog
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050025import json
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050026import random
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050027
28from collections import OrderedDict
29
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050030from twisted.internet import reactor
Matt Jeanneret5e331892019-12-07 21:31:45 -050031from twisted.internet.defer import inlineCallbacks, returnValue
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050032
33from heartbeat import HeartBeat
Devmalya Paulffc89df2019-07-31 17:43:13 -040034from pyvoltha.adapters.extensions.events.device_events.onu.onu_active_event import OnuActiveEvent
Devmalya Paule2e5f2b2020-03-08 18:50:33 -040035from pyvoltha.adapters.extensions.events.device_events.onu.onu_disabled_event import OnuDisabledEvent
Devmalya Paul1e1b1722020-05-07 02:51:15 -040036from pyvoltha.adapters.extensions.events.device_events.onu.onu_deleted_event import OnuDeletedEvent
Devmalya Paulffc89df2019-07-31 17:43:13 -040037from pyvoltha.adapters.extensions.events.kpi.onu.onu_pm_metrics import OnuPmMetrics
38from pyvoltha.adapters.extensions.events.kpi.onu.onu_omci_pm import OnuOmciPmMetrics
39from pyvoltha.adapters.extensions.events.adapter_events import AdapterEvents
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050040
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050041import pyvoltha.common.openflow.utils as fd
42from pyvoltha.common.utils.registry import registry
Matteo Scandolod8d73172019-11-26 12:15:15 -070043from pyvoltha.adapters.common.frameio.frameio import hexify
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050044from pyvoltha.common.utils.nethelpers import mac_str_to_tuple
Matt Jeanneret5e331892019-12-07 21:31:45 -050045from pyvoltha.adapters.common.kvstore.twisted_etcd_store import TwistedEtcdStore
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050046from voltha_protos.logical_device_pb2 import LogicalPort
William Kurkian8235c1e2019-03-05 12:58:28 -050047from voltha_protos.common_pb2 import OperStatus, ConnectStatus, AdminState
Matt Jeanneretf4113222019-08-14 19:44:34 -040048from voltha_protos.device_pb2 import Port
Girish Gowdra4c11ddb2020-03-03 11:33:24 -080049from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, ofp_port, OFPPS_LIVE, OFPPS_LINK_DOWN, \
Matt Jeanneretf4113222019-08-14 19:44:34 -040050 OFPPF_FIBER, OFPPF_1GB_FD
Matt Jeanneret3bfebff2019-04-12 18:25:03 -040051from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
Girish Gowdrae933cd32019-11-21 21:04:41 +053052 InterAdapterOmciMessage, PortCapability, InterAdapterTechProfileDownloadMessage, InterAdapterDeleteGemPortMessage, \
53 InterAdapterDeleteTcontMessage
Matt Jeannereta32441c2019-03-07 05:16:37 -050054from voltha_protos.openolt_pb2 import OnuIndication
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050055from pyvoltha.adapters.extensions.omci.onu_device_entry import OnuDeviceEvents, \
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050056 OnuDeviceEntry, IN_SYNC_KEY
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050057from omci.brcm_mib_download_task import BrcmMibDownloadTask
Girish Gowdrae933cd32019-11-21 21:04:41 +053058from omci.brcm_tp_setup_task import BrcmTpSetupTask
59from omci.brcm_tp_delete_task import BrcmTpDeleteTask
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050060from omci.brcm_uni_lock_task import BrcmUniLockTask
61from omci.brcm_vlan_filter_task import BrcmVlanFilterTask
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050062from onu_gem_port import OnuGemPort
63from onu_tcont import OnuTCont
64from pon_port import PonPort
Mahir Gunyel5de33fe2020-03-03 22:38:44 -080065from omci.brcm_mcast_task import BrcmMcastTask
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050066from uni_port import UniPort, UniType
Andrea Campanellacf916ea2020-02-14 10:03:58 +010067from uni_port import RESERVED_TRANSPARENT_VLAN
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050068from pyvoltha.common.tech_profile.tech_profile import TechProfile
onkarkundargiaae99712019-09-23 15:02:52 +053069from pyvoltha.adapters.extensions.omci.tasks.omci_test_request import OmciTestRequest
Matt Jeanneret5e331892019-12-07 21:31:45 -050070from pyvoltha.adapters.extensions.omci.omci_entities import AniG, Tcont, MacBridgeServiceProfile
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050071from pyvoltha.adapters.extensions.omci.omci_defs import EntityOperations, ReasonCodes
onkarkundargia1e2af22020-01-27 11:51:43 +053072from voltha_protos.voltha_pb2 import TestResponse
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050073
74OP = EntityOperations
75RC = ReasonCodes
76
Girish Gowdradc98d812020-03-20 13:04:58 -070077IS_MULTICAST = 'is_multicast'
Mahir Gunyel5de33fe2020-03-03 22:38:44 -080078GEM_PORT_ID = 'gemport_id'
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050079_STARTUP_RETRY_WAIT = 10
Mahir Gunyele9110a32020-02-20 14:56:50 -080080_PATH_SEPERATOR = "/"
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050081
82
83class BrcmOpenomciOnuHandler(object):
84
85 def __init__(self, adapter, device_id):
86 self.log = structlog.get_logger(device_id=device_id)
Matt Jeanneret08a8e862019-12-20 14:02:32 -050087 self.log.debug('starting-handler')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050088 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050089 self.core_proxy = adapter.core_proxy
90 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050091 self.parent_id = None
92 self.device_id = device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050093 self.proxy_address = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050094 self._enabled = False
Devmalya Paulffc89df2019-07-31 17:43:13 -040095 self.events = None
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050096 self._pm_metrics = None
97 self._pm_metrics_started = False
98 self._test_request = None
99 self._test_request_started = False
Girish Gowdradc98d812020-03-20 13:04:58 -0700100 self._tp = dict() # tp_id -> technology profile definition in KV Store.
Matt Jeanneret5e331892019-12-07 21:31:45 -0500101 self._reconciling = False
102
103 # Persisted onu configuration needed in case of reconciliation.
104 self._onu_persisted_state = {
105 'onu_id': None,
106 'intf_id': None,
107 'serial_number': None,
108 'admin_state': None,
109 'oper_state': None,
110 'uni_config': list()
111 }
112
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500113 self._unis = dict() # Port # -> UniPort
114
115 self._pon = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500116 self._pon_port_number = 100
117 self.logical_device_id = None
118
119 self._heartbeat = HeartBeat.create(self, device_id)
120
121 # Set up OpenOMCI environment
122 self._onu_omci_device = None
123 self._dev_info_loaded = False
124 self._deferred = None
125
126 self._in_sync_subscription = None
Matt Jeanneretf4113222019-08-14 19:44:34 -0400127 self._port_state_subscription = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500128 self._connectivity_subscription = None
129 self._capabilities_subscription = None
130
131 self.mac_bridge_service_profile_entity_id = 0x201
132 self.gal_enet_profile_entity_id = 0x1
133
134 self._tp_service_specific_task = dict()
135 self._tech_profile_download_done = dict()
Girish Gowdradc98d812020-03-20 13:04:58 -0700136
137 # When the vlan filter is being removed for a given TP ID on a given UNI,
138 # mark that we are expecting a tp delete to happen for this UNI.
139 # Unless the TP delete is complete to not allow new vlan add tasks to this TP ID
140 self._pending_delete_tp = dict()
141
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400142 # Stores information related to queued vlan filter tasks
143 # Dictionary with key being uni_id and value being device,uni port ,uni id and vlan id
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400144 self._queued_vlan_filter_task = dict()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500145
Girish Gowdradc98d812020-03-20 13:04:58 -0700146 self._set_vlan = dict() # uni_id, tp_id -> set_vlan_id
Matt Jeanneret5e331892019-12-07 21:31:45 -0500147
148 # Paths from kv store
149 ONU_PATH = 'service/voltha/openonu'
150
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500151 # Initialize KV store client
152 self.args = registry('main').get_args()
Matt Jeanneret5e331892019-12-07 21:31:45 -0500153 host, port = self.args.etcd.split(':', 1)
154 self.tp_kv_client = TwistedEtcdStore(host, port, TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
155 self.onu_kv_client = TwistedEtcdStore(host, port, ONU_PATH)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500156
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500157 @property
158 def enabled(self):
159 return self._enabled
160
161 @enabled.setter
162 def enabled(self, value):
163 if self._enabled != value:
164 self._enabled = value
165
166 @property
167 def omci_agent(self):
168 return self.adapter.omci_agent
169
170 @property
171 def omci_cc(self):
172 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
173
174 @property
175 def heartbeat(self):
176 return self._heartbeat
177
178 @property
179 def uni_ports(self):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500180 return list(self._unis.values())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500181
182 def uni_port(self, port_no_or_name):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500183 if isinstance(port_no_or_name, six.string_types):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500184 return next((uni for uni in self.uni_ports
185 if uni.name == port_no_or_name), None)
186
187 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
188 return next((uni for uni in self.uni_ports
Girish Gowdrae933cd32019-11-21 21:04:41 +0530189 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500190
191 @property
192 def pon_port(self):
193 return self._pon
194
Girish Gowdraa73ee452019-12-20 18:52:17 +0530195 @property
196 def onu_omci_device(self):
197 return self._onu_omci_device
198
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500199 def receive_message(self, msg):
200 if self.omci_cc is not None:
201 self.omci_cc.receive_message(msg)
202
Matt Jeanneretc083f462019-03-11 15:02:01 -0400203 def get_ofp_port_info(self, device, port_no):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500204 self.log.debug('get-ofp-port-info', port_no=port_no, device_id=device.id)
Matt Jeanneretc083f462019-03-11 15:02:01 -0400205 cap = OFPPF_1GB_FD | OFPPF_FIBER
206
Girish Gowdrae933cd32019-11-21 21:04:41 +0530207 hw_addr = mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
208 ((device.parent_port_no >> 8 & 0xff),
209 device.parent_port_no & 0xff,
210 (port_no >> 16) & 0xff,
211 (port_no >> 8) & 0xff,
212 port_no & 0xff))
Matt Jeanneretc083f462019-03-11 15:02:01 -0400213
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400214 uni_port = self.uni_port(int(port_no))
215 name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500216 self.log.debug('ofp-port-name', port_no=port_no, name=name, uni_port=uni_port)
Matt Jeanneretf4113222019-08-14 19:44:34 -0400217
218 ofstate = OFPPS_LINK_DOWN
219 if uni_port.operstatus is OperStatus.ACTIVE:
220 ofstate = OFPPS_LIVE
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400221
Matt Jeanneretc083f462019-03-11 15:02:01 -0400222 return PortCapability(
223 port=LogicalPort(
224 ofp_port=ofp_port(
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400225 name=name,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400226 hw_addr=hw_addr,
227 config=0,
Matt Jeanneretf4113222019-08-14 19:44:34 -0400228 state=ofstate,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400229 curr=cap,
230 advertised=cap,
231 peer=cap,
232 curr_speed=OFPPF_1GB_FD,
233 max_speed=OFPPF_1GB_FD
234 ),
235 device_id=device.id,
236 device_port_no=port_no
237 )
238 )
239
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500240 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500241 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500242 def activate(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700243 self.log.debug('activate-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500244
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500245 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500246 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500247 assert device.proxy_address.device_id
248
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500249 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500250 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500251 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500252 if self.enabled is not True:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700253 self.log.info('activating-new-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500254 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500255 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500256 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500257 device.reason = 'activating-onu'
258
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500259 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400260 # pm_metrics requires a logical device id. For now set to just device_id
261 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500262
Matt Jeanneret5e331892019-12-07 21:31:45 -0500263 self._onu_persisted_state['serial_number'] = device.serial_number
264 try:
265 self.log.debug('updating-onu-state', device_id=self.device_id,
266 onu_persisted_state=self._onu_persisted_state)
267 yield self.onu_kv_client.set(self.device_id, json.dumps(self._onu_persisted_state))
268 except Exception as e:
269 self.log.error('could-not-store-onu-state', device_id=self.device_id,
270 onu_persisted_state=self._onu_persisted_state, e=e)
271 # if we cannot write to storage we can proceed, for now.
272 # later onu indications from the olt will have another chance
273
Matt Jeannereta32441c2019-03-07 05:16:37 -0500274 yield self.core_proxy.device_update(device)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500275 self.log.debug('device-updated', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500276
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700277 yield self._init_pon_state()
Matteo Scandolod8d73172019-11-26 12:15:15 -0700278 self.log.debug('pon state initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500279
Matt Jeanneret5e331892019-12-07 21:31:45 -0500280 yield self._init_metrics()
281 self.log.debug('metrics initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500282
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500283 self.enabled = True
284 else:
285 self.log.info('onu-already-activated')
286
287 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400288 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500289 def reconcile(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700290 self.log.debug('reconcile-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500291
Matt Jeanneret5e331892019-12-07 21:31:45 -0500292 if self._reconciling:
293 self.log.debug('already-running-reconcile-device', device_id=device.id, serial_number=device.serial_number)
294 return
295
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500296 # first we verify that we got parent reference and proxy info
297 assert device.parent_id
298 assert device.proxy_address.device_id
299
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700300 self.proxy_address = device.proxy_address
301 self.parent_id = device.parent_id
302 self._pon_port_number = device.parent_port_no
303
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500304 if self.enabled is not True:
Matt Jeanneret5e331892019-12-07 21:31:45 -0500305 self._reconciling = True
306 self.log.info('reconciling-openonu-device')
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700307 self.logical_device_id = self.device_id
Matt Jeanneret5e331892019-12-07 21:31:45 -0500308
309 try:
310 query_data = yield self.onu_kv_client.get(device.id)
311 self._onu_persisted_state = json.loads(query_data)
312 self.log.debug('restored-onu-state', device_id=self.device_id,
313 onu_persisted_state=self._onu_persisted_state)
314 except Exception as e:
315 self.log.error('no-stored-onu-state', device_id=device.id, e=e)
316 # there is nothing we can do without data. flag the device as UNKNOWN and cannot reconcile
317 # likely it will take manual steps to delete/re-add this onu
318 yield self.core_proxy.device_reason_update(self.device_id, "cannot-reconcile")
319 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.UNKNOWN)
320 return
321
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700322 self._init_pon_state()
Matt Jeanneret5e331892019-12-07 21:31:45 -0500323 self.log.debug('pon state initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500324
Matt Jeanneret5e331892019-12-07 21:31:45 -0500325 self._init_metrics()
326 self.log.debug('metrics initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500327
Matt Jeanneret5e331892019-12-07 21:31:45 -0500328 self._subscribe_to_events()
329 # need to restart omci start machines and reload mib database. once db is loaded we can finish reconcile
330 self._onu_omci_device.start(device)
331 self._heartbeat.enabled = True
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500332
333 self.enabled = True
334 else:
335 self.log.info('onu-already-activated')
336
337 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700338 def _init_pon_state(self):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500339 self.log.debug('init-pon-state', device_id=self.device_id, device_logical_id=self.logical_device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500340
341 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500342 self._pon.add_peer(self.parent_id, self._pon_port_number)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700343 self.log.debug('adding-pon-port-to-agent',
344 type=self._pon.get_port().type,
345 admin_state=self._pon.get_port().admin_state,
346 oper_status=self._pon.get_port().oper_status,
347 )
Matt Jeanneret0c287892019-02-28 11:48:00 -0500348
Matt Jeanneret5e331892019-12-07 21:31:45 -0500349 if not self._reconciling:
350 yield self.core_proxy.port_created(self.device_id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500351
Matteo Scandolod8d73172019-11-26 12:15:15 -0700352 self.log.debug('added-pon-port-to-agent',
353 type=self._pon.get_port().type,
354 admin_state=self._pon.get_port().admin_state,
355 oper_status=self._pon.get_port().oper_status,
356 )
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500357
358 # Create and start the OpenOMCI ONU Device Entry for this ONU
359 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500360 self.core_proxy,
361 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500362 support_classes=self.adapter.broadcom_omci,
363 custom_me_map=self.adapter.custom_me_entities())
364 # Port startup
365 if self._pon is not None:
366 self._pon.enabled = True
367
Matt Jeanneret5e331892019-12-07 21:31:45 -0500368 @inlineCallbacks
369 def _init_metrics(self):
370 self.log.debug('init-metrics', device_id=self.device_id, device_logical_id=self.logical_device_id)
371
372 serial_number = self._onu_persisted_state.get('serial_number')
373
374 ############################################################################
375 # Setup Alarm handler
376 self.events = AdapterEvents(self.core_proxy, self.device_id, self.logical_device_id,
377 serial_number)
378 ############################################################################
379 # Setup PM configuration for this device
380 # Pass in ONU specific options
381 kwargs = {
382 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
383 'heartbeat': self.heartbeat,
384 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
385 }
386 self.log.debug('create-pm-metrics', device_id=self.device_id, serial_number=serial_number)
387 self._pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
388 self.logical_device_id, serial_number,
389 grouped=True, freq_override=False, **kwargs)
390 pm_config = self._pm_metrics.make_proto()
391 self._onu_omci_device.set_pm_config(self._pm_metrics.omci_pm.openomci_interval_pm)
392 self.log.debug("initial-pm-config", device_id=self.device_id, serial_number=serial_number)
393
394 if not self._reconciling:
395 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
396
397 # Note, ONU ID and UNI intf set in add_uni_port method
398 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
399 ani_ports=[self._pon])
400
401 # Code to Run OMCI Test Action
402 kwargs_omci_test_action = {
403 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
404 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
405 }
406 self._test_request = OmciTestRequest(self.core_proxy,
407 self.omci_agent, self.device_id,
408 AniG, serial_number,
409 self.logical_device_id,
410 exclusive=False,
411 **kwargs_omci_test_action)
412
413 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500414 def delete(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700415 self.log.info('delete-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneret5e331892019-12-07 21:31:45 -0500416 try:
417 yield self.onu_kv_client.delete(device.id)
418 except Exception as e:
419 self.log.error('could-not-delete-onu-state', device_id=device.id, e=e)
420
Devmalya Paul1e1b1722020-05-07 02:51:15 -0400421 try:
422 self._deferred.cancel()
423 self._test_request.stop_collector()
424 self._pm_metrics.stop_collector()
425 self.log.debug('removing-openomci-statemachine')
426 self.omci_agent.remove_device(device.id, cleanup=True)
427 yield self.onu_deleted_event()
428 except Exception as e:
429 self.log.error('could-not-delete-onu', device_id=device.id, e=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500430
431 def _create_tconts(self, uni_id, us_scheduler):
432 alloc_id = us_scheduler['alloc_id']
433 q_sched_policy = us_scheduler['q_sched_policy']
434 self.log.debug('create-tcont', us_scheduler=us_scheduler)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800435 # TODO: revisit for multi tconts support
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800436 new_tconts = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500437 tcontdict = dict()
438 tcontdict['alloc-id'] = alloc_id
439 tcontdict['q_sched_policy'] = q_sched_policy
440 tcontdict['uni_id'] = uni_id
441
Matt Jeanneret3789d0d2020-01-19 09:03:42 -0500442 tcont = OnuTCont.create(self, tcont=tcontdict)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500443
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500444 success = self._pon.add_tcont(tcont)
445 if success:
446 new_tconts.append(tcont)
447 self.log.debug('pon-add-tcont', tcont=tcont)
448
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800449 return new_tconts
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500450
451 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
452 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
453 self.log.debug('create-gemport',
454 gem_ports=gem_ports, direction=direction)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530455 new_gem_ports = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500456 for gem_port in gem_ports:
457 gemdict = dict()
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800458 if gem_port[IS_MULTICAST] == 'True':
459 gemdict[GEM_PORT_ID] = gem_port['multicast_gem_id']
460 gemdict[IS_MULTICAST] = True
461 else:
462 gemdict[GEM_PORT_ID] = gem_port[GEM_PORT_ID]
463 gemdict[IS_MULTICAST] = False
464
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500465 gemdict['direction'] = direction
466 gemdict['alloc_id_ref'] = alloc_id_ref
467 gemdict['encryption'] = gem_port['aes_encryption']
468 gemdict['discard_config'] = dict()
469 gemdict['discard_config']['max_probability'] = \
470 gem_port['discard_config']['max_probability']
471 gemdict['discard_config']['max_threshold'] = \
472 gem_port['discard_config']['max_threshold']
473 gemdict['discard_config']['min_threshold'] = \
474 gem_port['discard_config']['min_threshold']
475 gemdict['discard_policy'] = gem_port['discard_policy']
476 gemdict['max_q_size'] = gem_port['max_q_size']
477 gemdict['pbit_map'] = gem_port['pbit_map']
478 gemdict['priority_q'] = gem_port['priority_q']
479 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
480 gemdict['weight'] = gem_port['weight']
481 gemdict['uni_id'] = uni_id
482
483 gem_port = OnuGemPort.create(self, gem_port=gemdict)
484
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500485 success = self._pon.add_gem_port(gem_port, True)
486 if success:
487 new_gem_ports.append(gem_port)
488 self.log.debug('pon-add-gemport', gem_port=gem_port)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500489
Girish Gowdrae933cd32019-11-21 21:04:41 +0530490 return new_gem_ports
491
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800492 def _execute_queued_vlan_filter_tasks(self, uni_id, tp_id):
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400493 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
494 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
495 # or initial-mib-download is not done. Once the tp_task is completed, we execute
496 # such queued vlan-filter tasks
497 try:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800498 if uni_id in self._queued_vlan_filter_task and tp_id in self._queued_vlan_filter_task[uni_id]:
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400499 self.log.info("executing-queued-vlan-filter-task",
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800500 uni_id=uni_id, tp_id=tp_id)
Mahir Gunyela982ec32020-02-25 12:30:37 -0800501 for filter_info in self._queued_vlan_filter_task[uni_id][tp_id]:
502 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800503 uni_id=uni_id, uni_port=filter_info.get("uni_port"),
504 match_vlan=filter_info.get("match_vlan"),
505 _set_vlan_vid=filter_info.get("set_vlan_vid"),
506 _set_vlan_pcp=filter_info.get("set_vlan_pcp"),
507 tp_id=filter_info.get("tp_id"))
Girish Gowdraaf98a082020-03-05 16:40:51 -0800508 # Now remove the entry from the dictionary
Girish Gowdraaf98a082020-03-05 16:40:51 -0800509 self.log.debug("executed-queued-vlan-filter-task",
510 uni_id=uni_id, tp_id=tp_id)
Girish Gowdraa63eda82020-05-12 13:40:04 -0700511
512 # Now delete the key entry for the tp_id once we have handled the
513 # queued vlan filter tasks for that tp_id
514 del self._queued_vlan_filter_task[uni_id][tp_id]
515 # If the queued vlan filter tasks for all the tp_ids on a given
516 # uni_id is handled, then delete the uni_id key
517 if len(self._queued_vlan_filter_task[uni_id]) == 0:
518 del self._queued_vlan_filter_task[uni_id]
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400519 except Exception as e:
520 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
521
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500522 def _do_tech_profile_configuration(self, uni_id, tp):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500523 us_scheduler = tp['us_scheduler']
524 alloc_id = us_scheduler['alloc_id']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800525 new_tconts = self._create_tconts(uni_id, us_scheduler)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500526 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800527 new_upstream_gems = self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500528 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800529 new_downstream_gems = self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
530
531 new_gems = []
532 new_gems.extend(new_upstream_gems)
533 new_gems.extend(new_downstream_gems)
534
535 return new_tconts, new_gems
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500536
Matt Jeanneret5e331892019-12-07 21:31:45 -0500537 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500538 def load_and_configure_tech_profile(self, uni_id, tp_path):
539 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
Mahir Gunyele9110a32020-02-20 14:56:50 -0800540 tp_id = self.extract_tp_id_from_path(tp_path)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500541 if uni_id not in self._tp_service_specific_task:
542 self._tp_service_specific_task[uni_id] = dict()
543
544 if uni_id not in self._tech_profile_download_done:
545 self._tech_profile_download_done[uni_id] = dict()
546
Mahir Gunyele9110a32020-02-20 14:56:50 -0800547 if tp_id not in self._tech_profile_download_done[uni_id]:
548 self._tech_profile_download_done[uni_id][tp_id] = False
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500549
Mahir Gunyele9110a32020-02-20 14:56:50 -0800550 if not self._tech_profile_download_done[uni_id][tp_id]:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500551 try:
552 if tp_path in self._tp_service_specific_task[uni_id]:
553 self.log.info("tech-profile-config-already-in-progress",
Girish Gowdrae933cd32019-11-21 21:04:41 +0530554 tp_path=tp_path)
Matt Jeanneret5e331892019-12-07 21:31:45 -0500555 returnValue(None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500556
Matt Jeanneret5e331892019-12-07 21:31:45 -0500557 tpstored = yield self.tp_kv_client.get(tp_path)
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500558 tpstring = tpstored.decode('ascii')
559 tp = json.loads(tpstring)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800560 self._tp[tp_id] = tp
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500561 self.log.debug("tp-instance", tp=tp)
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800562 tconts, gem_ports = self._do_tech_profile_configuration(uni_id, tp)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700563
William Kurkian3a206332019-04-29 11:05:47 -0400564 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500565 def success(_results):
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800566 self.log.info("tech-profile-config-done-successfully", uni_id=uni_id, tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500567 if tp_path in self._tp_service_specific_task[uni_id]:
568 del self._tp_service_specific_task[uni_id][tp_path]
Mahir Gunyele9110a32020-02-20 14:56:50 -0800569 self._tech_profile_download_done[uni_id][tp_id] = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400570 # Now execute any vlan filter tasks that were queued for later
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800571 reactor.callInThread(self._execute_queued_vlan_filter_tasks, uni_id, tp_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500572 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-success')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530573
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800574 # Execute mcast task
575 for gem in gem_ports:
Girish Gowdradc98d812020-03-20 13:04:58 -0700576 self.log.debug("checking-multicast-service-for-gem ", gem=gem)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800577 if gem.mcast is True:
Girish Gowdradc98d812020-03-20 13:04:58 -0700578 self.log.info("found-multicast-service-for-gem ", gem=gem, uni_id=uni_id, tp_id=tp_id)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800579 reactor.callInThread(self.start_multicast_service, uni_id, tp_path)
580 self.log.debug("started_multicast_service-successfully", tconts=tconts, gems=gem_ports)
581 break
582
William Kurkian3a206332019-04-29 11:05:47 -0400583 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500584 def failure(_reason):
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800585 self.log.warn('tech-profile-config-failure-retrying', uni_id=uni_id, tp_id=tp_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +0530586 _reason=_reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500587 if tp_path in self._tp_service_specific_task[uni_id]:
588 del self._tp_service_specific_task[uni_id][tp_path]
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800589 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500590 reactor.callLater(retry, self.load_and_configure_tech_profile,
591 uni_id, tp_path)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500592 yield self.core_proxy.device_reason_update(self.device_id,
593 'tech-profile-config-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500594
Mahir Gunyela982ec32020-02-25 12:30:37 -0800595 self.log.info('downloading-tech-profile-configuration', uni_id=uni_id, tp_id=tp_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530596 self.log.debug("tconts-gems-to-install", tconts=tconts, gem_ports=gem_ports)
597
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500598 self.log.debug("current-cached-tconts", tconts=list(self.pon_port.tconts.values()))
599 self.log.debug("current-cached-gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
600
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500601 self._tp_service_specific_task[uni_id][tp_path] = \
Mahir Gunyele9110a32020-02-20 14:56:50 -0800602 BrcmTpSetupTask(self.omci_agent, self, uni_id, tconts, gem_ports, tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500603 self._deferred = \
Girish Gowdrae933cd32019-11-21 21:04:41 +0530604 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500605 self._deferred.addCallbacks(success, failure)
606
607 except Exception as e:
608 self.log.exception("error-loading-tech-profile", e=e)
609 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700610 # There is an active tech-profile task ongoing on this UNI port. So, reschedule this task
611 # after a short interval
612 if uni_id in self._tp_service_specific_task and len(self._tp_service_specific_task[uni_id]):
613 self.log.debug("active-tp-tasks-in-progress-for-uni--scheduling-this-task-for-later",
614 uni_id=uni_id, tp_path=tp_path)
615 reactor.callLater(0.2, self.load_and_configure_tech_profile,
616 uni_id, tp_path)
617 return
618
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500619 self.log.info("tech-profile-config-already-done")
Girish Gowdradc98d812020-03-20 13:04:58 -0700620
Girish Gowdrae933cd32019-11-21 21:04:41 +0530621 # Could be a case where TP exists but new gem-ports are getting added dynamically
Matt Jeanneret5e331892019-12-07 21:31:45 -0500622 tpstored = yield self.tp_kv_client.get(tp_path)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530623 tpstring = tpstored.decode('ascii')
624 tp = json.loads(tpstring)
625 upstream_gems = []
626 downstream_gems = []
627 # Find out the new Gem ports that are getting added afresh.
628 for gp in tp['upstream_gem_port_attribute_list']:
629 if self.pon_port.gem_port(gp['gemport_id'], "upstream"):
630 # gem port already exists
631 continue
632 upstream_gems.append(gp)
633 for gp in tp['downstream_gem_port_attribute_list']:
634 if self.pon_port.gem_port(gp['gemport_id'], "downstream"):
635 # gem port already exists
636 continue
637 downstream_gems.append(gp)
638
639 us_scheduler = tp['us_scheduler']
640 alloc_id = us_scheduler['alloc_id']
641
642 if len(upstream_gems) > 0 or len(downstream_gems) > 0:
643 self.log.info("installing-new-gem-ports", upstream_gems=upstream_gems, downstream_gems=downstream_gems)
644 new_upstream_gems = self._create_gemports(uni_id, upstream_gems, alloc_id, "UPSTREAM")
645 new_downstream_gems = self._create_gemports(uni_id, downstream_gems, alloc_id, "DOWNSTREAM")
646 new_gems = []
647 new_gems.extend(new_upstream_gems)
648 new_gems.extend(new_downstream_gems)
649
650 def success(_results):
651 self.log.info("new-gem-ports-successfully-installed", result=_results)
652
653 def failure(_reason):
654 self.log.warn('new-gem-port-install-failed--retrying',
655 _reason=_reason)
656 # Remove gem ports from cache. We will re-add them during the retry
657 for gp in new_gems:
658 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
659
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800660 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500661 reactor.callLater(retry, self.load_and_configure_tech_profile,
662 uni_id, tp_path)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530663
664 self._tp_service_specific_task[uni_id][tp_path] = \
Mahir Gunyele9110a32020-02-20 14:56:50 -0800665 BrcmTpSetupTask(self.omci_agent, self, uni_id, [], new_gems, tp_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530666 self._deferred = \
667 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
668 self._deferred.addCallbacks(success, failure)
Girish Gowdradc98d812020-03-20 13:04:58 -0700669
Matt Jeanneret5e331892019-12-07 21:31:45 -0500670 @inlineCallbacks
Girish Gowdradc98d812020-03-20 13:04:58 -0700671 def start_multicast_service(self, uni_id, tp_path, retry_count=0):
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800672 self.log.debug("starting-multicast-service", uni_id=uni_id, tp_path=tp_path)
673 tp_id = self.extract_tp_id_from_path(tp_path)
674 if uni_id in self._set_vlan and tp_id in self._set_vlan[uni_id]:
675 try:
676 tp = self._tp[tp_id]
677 if tp is None:
Matt Jeanneret5e331892019-12-07 21:31:45 -0500678 tpstored = yield self.tp_kv_client.get(tp_path)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800679 tpstring = tpstored.decode('ascii')
680 tp = json.loads(tpstring)
681 if tp is None:
682 self.log.error("cannot-find-tp-to-start-multicast-service", uni_id=uni_id, tp_path=tp_path)
683 return
684 else:
685 self._tp[tp_id] = tp
686
687 self.log.debug("mcast-vlan-learned-before", self._set_vlan[uni_id][tp_id], uni_id=uni_id, tp_id=tp_id)
Girish Gowdradc98d812020-03-20 13:04:58 -0700688
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800689 def success(_results):
690 self.log.debug('multicast-success', uni_id=uni_id)
691 self._multicast_task = None
692
693 def failure(_reason):
694 self.log.warn('multicast-failure', _reason=_reason)
Girish Gowdradc98d812020-03-20 13:04:58 -0700695 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800696 reactor.callLater(retry, self.start_multicast_service,
Girish Gowdradc98d812020-03-20 13:04:58 -0700697 uni_id, tp_path)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800698
699 self.log.debug('starting-multicast-task', mcast_vlan_id=self._set_vlan[uni_id][tp_id])
700 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
701 for i in range(len(downstream_gem_port_attribute_list)):
702 if IS_MULTICAST in downstream_gem_port_attribute_list[i] and \
703 downstream_gem_port_attribute_list[i][IS_MULTICAST] == 'True':
Girish Gowdradc98d812020-03-20 13:04:58 -0700704 dynamic_access_control_list_table = downstream_gem_port_attribute_list[i][
705 'dynamic_access_control_list'].split("-")
706 static_access_control_list_table = downstream_gem_port_attribute_list[i][
707 'static_access_control_list'].split("-")
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800708 multicast_gem_id = downstream_gem_port_attribute_list[i]['multicast_gem_id']
709 break
710
711 self._multicast_task = BrcmMcastTask(self.omci_agent, self, self.device_id, uni_id, tp_id,
Girish Gowdradc98d812020-03-20 13:04:58 -0700712 self._set_vlan[uni_id][tp_id], dynamic_access_control_list_table,
713 static_access_control_list_table, multicast_gem_id)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800714 self._deferred = self._onu_omci_device.task_runner.queue_task(self._multicast_task)
715 self._deferred.addCallbacks(success, failure)
716 except Exception as e:
717 self.log.exception("error-loading-multicast", e=e)
718 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700719 if retry_count < 30:
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800720 retry_count = +1
Girish Gowdradc98d812020-03-20 13:04:58 -0700721 self.log.debug("going-to-wait-for-flow-to-learn-mcast-vlan", uni_id=uni_id, tp_id=tp_id,
722 retry=retry_count)
Mahir Gunyel5de33fe2020-03-03 22:38:44 -0800723 reactor.callLater(0.5, self.start_multicast_service, uni_id, tp_path, retry_count)
724 else:
Girish Gowdradc98d812020-03-20 13:04:58 -0700725 self.log.error("mcast-vlan-not-configured-yet-failing-mcast-service-conf", uni_id=uni_id, tp_id=tp_id,
726 retry=retry_count)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530727
728 def delete_tech_profile(self, uni_id, tp_path, alloc_id=None, gem_port_id=None):
729 try:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800730 tp_table_id = self.extract_tp_id_from_path(tp_path)
Naga Manjunathe433c712020-01-02 17:27:20 +0530731 if not uni_id in self._tech_profile_download_done:
732 self.log.warn("tp-key-is-not-present", uni_id=uni_id)
733 return
734
Mahir Gunyele9110a32020-02-20 14:56:50 -0800735 if not tp_table_id in self._tech_profile_download_done[uni_id]:
736 self.log.warn("tp-id-is-not-present", uni_id=uni_id, tp_id=tp_table_id)
Naga Manjunathe433c712020-01-02 17:27:20 +0530737 return
738
Mahir Gunyele9110a32020-02-20 14:56:50 -0800739 if self._tech_profile_download_done[uni_id][tp_table_id] is not True:
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800740 self.log.error("tp-download-is-not-done-in-order-to-process-tp-delete", uni_id=uni_id,
741 tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530742 return
743
744 if alloc_id is None and gem_port_id is None:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800745 self.log.error("alloc-id-and-gem-port-id-are-none", uni_id=uni_id, tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530746 return
747
748 # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
749 # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
750 # due to additional tasks on different UNIs. So, it we cannot use the pon_port affter
751 # this initializer
752 tcont = None
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500753 self.log.debug("current-cached-tconts", tconts=list(self.pon_port.tconts.values()))
Girish Gowdrae933cd32019-11-21 21:04:41 +0530754 for tc in list(self.pon_port.tconts.values()):
755 if tc.alloc_id == alloc_id:
756 tcont = tc
757 self.pon_port.remove_tcont(tc.alloc_id, False)
758
759 gem_port = None
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500760 self.log.debug("current-cached-gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
Girish Gowdrae933cd32019-11-21 21:04:41 +0530761 for gp in list(self.pon_port.gem_ports.values()):
762 if gp.gem_id == gem_port_id:
763 gem_port = gp
764 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
765
Girish Gowdrae933cd32019-11-21 21:04:41 +0530766 @inlineCallbacks
767 def success(_results):
768 if gem_port_id:
769 self.log.info("gem-port-delete-done-successfully")
770 if alloc_id:
771 self.log.info("tcont-delete-done-successfully")
772 # The deletion of TCONT marks the complete deletion of tech-profile
773 try:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800774 del self._tech_profile_download_done[uni_id][tp_table_id]
Girish Gowdradc98d812020-03-20 13:04:58 -0700775 self.log.debug("tp-profile-download-flag-cleared", uni_id=uni_id, tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530776 del self._tp_service_specific_task[uni_id][tp_path]
Girish Gowdradc98d812020-03-20 13:04:58 -0700777 self.log.debug("tp-service-specific-task-cleared", uni_id=uni_id, tp_id=tp_table_id)
778 del self._pending_delete_tp[uni_id][tp_table_id]
779 self.log.debug("pending-delete-tp-task-flag-cleared", uni_id=uni_id, tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530780 except Exception as ex:
781 self.log.error("del-tp-state-info", e=ex)
782
783 # TODO: There could be multiple TP on the UNI, and also the ONU.
784 # TODO: But the below reason updates for the whole device.
785 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-delete-success')
786
787 @inlineCallbacks
Girish Gowdraa73ee452019-12-20 18:52:17 +0530788 def failure(_reason):
Girish Gowdrae933cd32019-11-21 21:04:41 +0530789 self.log.warn('tech-profile-delete-failure-retrying',
790 _reason=_reason)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500791 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
792 reactor.callLater(retry, self.delete_tech_profile, uni_id, tp_path, alloc_id, gem_port_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500793 yield self.core_proxy.device_reason_update(self.device_id,
794 'tech-profile-config-delete-failure-retrying')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530795
796 self.log.info('deleting-tech-profile-configuration')
797
Girish Gowdraa73ee452019-12-20 18:52:17 +0530798 if tcont is None and gem_port is None:
799 if alloc_id is not None:
800 self.log.error("tcont-info-corresponding-to-alloc-id-not-found", alloc_id=alloc_id)
801 if gem_port_id is not None:
802 self.log.error("gem-port-info-corresponding-to-gem-port-id-not-found", gem_port_id=gem_port_id)
803 return
804
Girish Gowdrae933cd32019-11-21 21:04:41 +0530805 self._tp_service_specific_task[uni_id][tp_path] = \
806 BrcmTpDeleteTask(self.omci_agent, self, uni_id, tp_table_id,
807 tcont=tcont, gem_port=gem_port)
808 self._deferred = \
809 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
810 self._deferred.addCallbacks(success, failure)
811 except Exception as e:
812 self.log.exception("failed-to-delete-tp",
813 e=e, uni_id=uni_id, tp_path=tp_path,
814 alloc_id=alloc_id, gem_port_id=gem_port_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500815
Rohan Agrawalf0f8c292020-06-01 09:30:55 +0000816 def update_pm_config(self, device, pm_configs):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500817 # TODO: This has not been tested
Rohan Agrawalf0f8c292020-06-01 09:30:55 +0000818 self.log.info('update_pm_config', pm_configs=pm_configs)
819 self._pm_metrics.update(pm_configs)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500820
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800821 def remove_onu_flows(self, device, flows):
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500822 self.log.debug('remove-onu-flows')
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800823
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800824 # no point in removing omci flows if the device isnt reachable
825 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800826 device.admin_state != AdminState.ENABLED:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800827 self.log.warn("device-disabled-or-offline-skipping-remove-flow",
828 admin=device.admin_state, connect=device.connect_status)
829 return
830
831 for flow in flows:
832 # if incoming flow contains cookie, then remove from ONU
833 if flow.cookie:
834 self.log.debug("remove-flow", device_id=device.id, flow=flow)
835
836 def is_downstream(port):
837 return port == self._pon_port_number
838
839 def is_upstream(port):
840 return not is_downstream(port)
841
842 try:
843 _in_port = fd.get_in_port(flow)
844 assert _in_port is not None
845
846 _out_port = fd.get_out_port(flow) # may be None
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800847
848 if is_downstream(_in_port):
849 self.log.debug('downstream-flow-no-need-to-remove', in_port=_in_port, out_port=_out_port,
850 device_id=device.id)
851 # extended vlan tagging operation will handle it
852 continue
853 elif is_upstream(_in_port):
854 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
855 if fd.is_dhcp_flow(flow):
856 self.log.debug('The dhcp trap-to-host flow will be discarded', device_id=device.id)
857 return
858
Mahir Gunyel45610b42020-03-16 17:29:01 -0700859 _match_vlan_vid = None
860 for field in fd.get_ofb_fields(flow):
861 if field.type == fd.VLAN_VID:
862 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
863 _match_vlan_vid = RESERVED_TRANSPARENT_VLAN
864 else:
865 _match_vlan_vid = field.vlan_vid & 0xfff
866 self.log.debug('field-type-vlan-vid',
867 vlan=_match_vlan_vid)
868
869 _set_vlan_vid = None
870 _set_vlan_pcp = None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800871 # Retrieve the VLAN_VID that needs to be removed from the EVTO rule on the ONU.
872 for action in fd.get_actions(flow):
873 if action.type == fd.SET_FIELD:
874 _field = action.set_field.field.ofb_field
875 assert (action.set_field.field.oxm_class ==
876 OFPXMC_OPENFLOW_BASIC)
877 if _field.type == fd.VLAN_VID:
Mahir Gunyel45610b42020-03-16 17:29:01 -0700878 _set_vlan_vid = _field.vlan_vid & 0xfff
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800879 self.log.debug('vlan-vid-to-remove',
Mahir Gunyel45610b42020-03-16 17:29:01 -0700880 _vlan_vid=_set_vlan_vid, in_port=_in_port)
881 elif _field.type == fd.VLAN_PCP:
882 _set_vlan_pcp = _field.vlan_pcp
883 self.log.debug('set-field-type-vlan-pcp',
884 vlan_pcp=_set_vlan_pcp)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800885
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800886 uni_port = self.uni_port(_in_port)
887 uni_id = _in_port & 0xF
888 else:
889 raise Exception('port should be 1 or 2 by our convention')
890
891 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
892
893 tp_id = self.get_tp_id_in_flow(flow)
Girish Gowdradc98d812020-03-20 13:04:58 -0700894 # The vlan filter remove should be followed by a TP deleted for that TP ID.
895 # Use this information to re-schedule any vlan filter add tasks for the same TP ID again.
896 # First check if the TP download was done, before we access that TP delete is necessary
Mahir Gunyel45610b42020-03-16 17:29:01 -0700897 if uni_id in self._tech_profile_download_done and tp_id in self._tech_profile_download_done[
898 uni_id] and \
Girish Gowdradc98d812020-03-20 13:04:58 -0700899 self._tech_profile_download_done[uni_id][tp_id] is True:
900 if uni_id not in self._pending_delete_tp:
901 self._pending_delete_tp[uni_id] = dict()
902 self._pending_delete_tp[uni_id][tp_id] = True
903 else:
904 self._pending_delete_tp[uni_id][tp_id] = True
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800905 # Deleting flow from ONU.
Mahir Gunyel45610b42020-03-16 17:29:01 -0700906 self._remove_vlan_filter_task(device, uni_id, uni_port=uni_port,
907 _set_vlan_pcp=_set_vlan_pcp,
908 _set_vlan_vid=_set_vlan_vid,
909 match_vlan=_match_vlan_vid,
910 tp_id=tp_id)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800911 # TODO:Delete TD task.
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800912 except Exception as e:
913 self.log.exception('failed-to-remove-flow', e=e)
914
915 def add_onu_flows(self, device, flows):
Matt Jeanneret2ca384f2020-03-06 13:49:31 -0500916 self.log.debug('add-onu-flows')
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800917
918 # no point in pushing omci flows if the device isnt reachable
919 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800920 device.admin_state != AdminState.ENABLED:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800921 self.log.warn("device-disabled-or-offline-skipping-flow-update",
922 admin=device.admin_state, connect=device.connect_status)
923 return
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800924
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800925 def is_downstream(port):
926 return port == self._pon_port_number
927
928 def is_upstream(port):
929 return not is_downstream(port)
930
931 for flow in flows:
932 # if incoming flow contains cookie, then add to ONU
933 if flow.cookie:
934 _type = None
935 _port = None
936 _vlan_vid = None
937 _udp_dst = None
938 _udp_src = None
939 _ipv4_dst = None
940 _ipv4_src = None
941 _metadata = None
942 _output = None
943 _push_tpid = None
944 _field = None
945 _set_vlan_vid = None
Mahir Gunyel45610b42020-03-16 17:29:01 -0700946 _set_vlan_pcp = None
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800947 _tunnel_id = None
948 self.log.debug("add-flow", device_id=device.id, flow=flow)
949
950 try:
951 _in_port = fd.get_in_port(flow)
952 assert _in_port is not None
953
954 _out_port = fd.get_out_port(flow) # may be None
955 tp_id = self.get_tp_id_in_flow(flow)
956 if is_downstream(_in_port):
957 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
958 # NOTE: We don't care downstream flow because we will copy vlan_id to upstream flow
959 # uni_port = self.uni_port(_out_port)
960 # uni_id = _out_port & 0xF
961 continue
962 elif is_upstream(_in_port):
963 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
964 uni_port = self.uni_port(_in_port)
965 uni_id = _in_port & 0xF
966 else:
967 raise Exception('port should be 1 or 2 by our convention')
968
969 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
970
971 for field in fd.get_ofb_fields(flow):
972 if field.type == fd.ETH_TYPE:
973 _type = field.eth_type
974 self.log.debug('field-type-eth-type',
975 eth_type=_type)
976
977 elif field.type == fd.IP_PROTO:
978 _proto = field.ip_proto
979 self.log.debug('field-type-ip-proto',
980 ip_proto=_proto)
981
982 elif field.type == fd.IN_PORT:
983 _port = field.port
984 self.log.debug('field-type-in-port',
985 in_port=_port)
986 elif field.type == fd.TUNNEL_ID:
987 self.log.debug('field-type-tunnel-id')
988
989 elif field.type == fd.VLAN_VID:
Andrea Campanellacf916ea2020-02-14 10:03:58 +0100990 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
991 _vlan_vid = RESERVED_TRANSPARENT_VLAN
992 else:
993 _vlan_vid = field.vlan_vid & 0xfff
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800994 self.log.debug('field-type-vlan-vid',
995 vlan=_vlan_vid)
996
997 elif field.type == fd.VLAN_PCP:
998 _vlan_pcp = field.vlan_pcp
999 self.log.debug('field-type-vlan-pcp',
1000 pcp=_vlan_pcp)
1001
1002 elif field.type == fd.UDP_DST:
1003 _udp_dst = field.udp_dst
1004 self.log.debug('field-type-udp-dst',
1005 udp_dst=_udp_dst)
1006
1007 elif field.type == fd.UDP_SRC:
1008 _udp_src = field.udp_src
1009 self.log.debug('field-type-udp-src',
1010 udp_src=_udp_src)
1011
1012 elif field.type == fd.IPV4_DST:
1013 _ipv4_dst = field.ipv4_dst
1014 self.log.debug('field-type-ipv4-dst',
1015 ipv4_dst=_ipv4_dst)
1016
1017 elif field.type == fd.IPV4_SRC:
1018 _ipv4_src = field.ipv4_src
1019 self.log.debug('field-type-ipv4-src',
1020 ipv4_dst=_ipv4_src)
1021
1022 elif field.type == fd.METADATA:
1023 _metadata = field.table_metadata
1024 self.log.debug('field-type-metadata',
1025 metadata=_metadata)
1026
1027 else:
1028 raise NotImplementedError('field.type={}'.format(
1029 field.type))
1030
1031 for action in fd.get_actions(flow):
1032
1033 if action.type == fd.OUTPUT:
1034 _output = action.output.port
1035 self.log.debug('action-type-output',
1036 output=_output, in_port=_in_port)
1037
1038 elif action.type == fd.POP_VLAN:
1039 self.log.debug('action-type-pop-vlan',
1040 in_port=_in_port)
1041
1042 elif action.type == fd.PUSH_VLAN:
1043 _push_tpid = action.push.ethertype
1044 self.log.debug('action-type-push-vlan',
1045 push_tpid=_push_tpid, in_port=_in_port)
1046 if action.push.ethertype != 0x8100:
1047 self.log.error('unhandled-tpid',
1048 ethertype=action.push.ethertype)
1049
1050 elif action.type == fd.SET_FIELD:
1051 _field = action.set_field.field.ofb_field
1052 assert (action.set_field.field.oxm_class ==
1053 OFPXMC_OPENFLOW_BASIC)
1054 self.log.debug('action-type-set-field',
1055 field=_field, in_port=_in_port)
1056 if _field.type == fd.VLAN_VID:
1057 _set_vlan_vid = _field.vlan_vid & 0xfff
1058 self.log.debug('set-field-type-vlan-vid',
1059 vlan_vid=_set_vlan_vid)
1060 elif _field.type == fd.VLAN_PCP:
1061 _set_vlan_pcp = _field.vlan_pcp
1062 self.log.debug('set-field-type-vlan-pcp',
1063 vlan_pcp=_set_vlan_pcp)
1064 else:
1065 self.log.error('unsupported-action-set-field-type',
1066 field_type=_field.type)
1067 else:
1068 self.log.error('unsupported-action-type',
1069 action_type=action.type, in_port=_in_port)
1070
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001071 if self._set_vlan is not None:
1072 if uni_id not in self._set_vlan:
1073 self._set_vlan[uni_id] = dict()
1074 self._set_vlan[uni_id][tp_id] = _set_vlan_vid
1075 self.log.debug("set_vlan_id-for-tp", _set_vlan_vid=_set_vlan_vid, tp_id=tp_id)
1076
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001077 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
1078 # supported match and action criteria cannot be handled by omci and must be ignored.
1079 if (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid != RESERVED_TRANSPARENT_VLAN:
1080 self.log.warn('ignoring-flow-that-does-not-set-vlanid', set_vlan_vid=_set_vlan_vid)
1081 elif (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid == RESERVED_TRANSPARENT_VLAN:
1082 self.log.info('set-vlanid-any', uni_id=uni_id, uni_port=uni_port,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001083 _set_vlan_vid=_vlan_vid,
1084 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1085 tp_id=tp_id)
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001086 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1087 _set_vlan_vid=_vlan_vid,
1088 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1089 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001090 else:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001091 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, match_vlan=_vlan_vid,
1092 set_vlan_vid=_set_vlan_vid, _set_vlan_pcp=_set_vlan_pcp, ethType=_type)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001093 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1094 _set_vlan_vid=_set_vlan_vid,
1095 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1096 tp_id=tp_id)
1097
1098 except Exception as e:
1099 self.log.exception('failed-to-install-flow', e=e, flow=flow)
1100
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001101 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
1102 # flow decomposition that ultimately comes from onos
1103 def update_flow_table(self, device, flows):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001104 self.log.debug('update-flow-table', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001105
1106 #
1107 # We need to proxy through the OLT to get to the ONU
1108 # Configuration from here should be using OMCI
1109 #
1110 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
1111
1112 # no point in pushing omci flows if the device isnt reachable
1113 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdrae933cd32019-11-21 21:04:41 +05301114 device.admin_state != AdminState.ENABLED:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001115 self.log.warn("device-disabled-or-offline-skipping-flow-update",
1116 admin=device.admin_state, connect=device.connect_status)
1117 return
1118
1119 def is_downstream(port):
1120 return port == self._pon_port_number
1121
1122 def is_upstream(port):
1123 return not is_downstream(port)
1124
1125 for flow in flows:
1126 _type = None
1127 _port = None
1128 _vlan_vid = None
1129 _udp_dst = None
1130 _udp_src = None
1131 _ipv4_dst = None
1132 _ipv4_src = None
1133 _metadata = None
1134 _output = None
1135 _push_tpid = None
1136 _field = None
1137 _set_vlan_vid = None
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001138 _set_vlan_pcp = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -04001139 _tunnel_id = None
1140
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001141 try:
Girish Gowdraa73ee452019-12-20 18:52:17 +05301142 write_metadata = fd.get_write_metadata(flow)
1143 if write_metadata is None:
1144 self.log.error("do-not-process-flow-without-write-metadata")
1145 return
1146
1147 # extract tp id from flow
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001148 tp_id = self.get_tp_id_in_flow(flow)
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001149 self.log.debug("tp-id-in-flow", tp_id=tp_id)
Girish Gowdraa73ee452019-12-20 18:52:17 +05301150
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001151 _in_port = fd.get_in_port(flow)
1152 assert _in_port is not None
1153
1154 _out_port = fd.get_out_port(flow) # may be None
1155
1156 if is_downstream(_in_port):
1157 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
1158 uni_port = self.uni_port(_out_port)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301159 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001160 elif is_upstream(_in_port):
1161 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
1162 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001163 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001164 else:
1165 raise Exception('port should be 1 or 2 by our convention')
1166
1167 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
1168
1169 for field in fd.get_ofb_fields(flow):
1170 if field.type == fd.ETH_TYPE:
1171 _type = field.eth_type
1172 self.log.debug('field-type-eth-type',
1173 eth_type=_type)
1174
1175 elif field.type == fd.IP_PROTO:
1176 _proto = field.ip_proto
1177 self.log.debug('field-type-ip-proto',
1178 ip_proto=_proto)
1179
1180 elif field.type == fd.IN_PORT:
1181 _port = field.port
1182 self.log.debug('field-type-in-port',
1183 in_port=_port)
1184
1185 elif field.type == fd.VLAN_VID:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001186 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
1187 _vlan_vid = RESERVED_TRANSPARENT_VLAN
1188 else:
1189 _vlan_vid = field.vlan_vid & 0xfff
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001190 self.log.debug('field-type-vlan-vid',
1191 vlan=_vlan_vid)
1192
1193 elif field.type == fd.VLAN_PCP:
1194 _vlan_pcp = field.vlan_pcp
1195 self.log.debug('field-type-vlan-pcp',
1196 pcp=_vlan_pcp)
1197
1198 elif field.type == fd.UDP_DST:
1199 _udp_dst = field.udp_dst
1200 self.log.debug('field-type-udp-dst',
1201 udp_dst=_udp_dst)
1202
1203 elif field.type == fd.UDP_SRC:
1204 _udp_src = field.udp_src
1205 self.log.debug('field-type-udp-src',
1206 udp_src=_udp_src)
1207
1208 elif field.type == fd.IPV4_DST:
1209 _ipv4_dst = field.ipv4_dst
1210 self.log.debug('field-type-ipv4-dst',
1211 ipv4_dst=_ipv4_dst)
1212
1213 elif field.type == fd.IPV4_SRC:
1214 _ipv4_src = field.ipv4_src
1215 self.log.debug('field-type-ipv4-src',
1216 ipv4_dst=_ipv4_src)
1217
1218 elif field.type == fd.METADATA:
1219 _metadata = field.table_metadata
1220 self.log.debug('field-type-metadata',
1221 metadata=_metadata)
1222
Matt Jeanneretef06d0d2019-04-27 17:36:53 -04001223 elif field.type == fd.TUNNEL_ID:
1224 _tunnel_id = field.tunnel_id
1225 self.log.debug('field-type-tunnel-id',
1226 tunnel_id=_tunnel_id)
1227
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001228
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001229 else:
1230 raise NotImplementedError('field.type={}'.format(
1231 field.type))
1232
1233 for action in fd.get_actions(flow):
1234
1235 if action.type == fd.OUTPUT:
1236 _output = action.output.port
1237 self.log.debug('action-type-output',
1238 output=_output, in_port=_in_port)
1239
1240 elif action.type == fd.POP_VLAN:
1241 self.log.debug('action-type-pop-vlan',
1242 in_port=_in_port)
1243
1244 elif action.type == fd.PUSH_VLAN:
1245 _push_tpid = action.push.ethertype
1246 self.log.debug('action-type-push-vlan',
1247 push_tpid=_push_tpid, in_port=_in_port)
1248 if action.push.ethertype != 0x8100:
1249 self.log.error('unhandled-tpid',
1250 ethertype=action.push.ethertype)
1251
1252 elif action.type == fd.SET_FIELD:
1253 _field = action.set_field.field.ofb_field
1254 assert (action.set_field.field.oxm_class ==
1255 OFPXMC_OPENFLOW_BASIC)
1256 self.log.debug('action-type-set-field',
1257 field=_field, in_port=_in_port)
1258 if _field.type == fd.VLAN_VID:
1259 _set_vlan_vid = _field.vlan_vid & 0xfff
1260 self.log.debug('set-field-type-vlan-vid',
1261 vlan_vid=_set_vlan_vid)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001262 elif _field.type == fd.VLAN_PCP:
1263 _set_vlan_pcp = _field.vlan_pcp
1264 self.log.debug('set-field-type-vlan-pcp',
1265 vlan_pcp=_set_vlan_pcp)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001266 else:
1267 self.log.error('unsupported-action-set-field-type',
1268 field_type=_field.type)
1269 else:
1270 self.log.error('unsupported-action-type',
1271 action_type=action.type, in_port=_in_port)
1272
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001273 if self._set_vlan is not None:
1274 if uni_id not in self._set_vlan:
1275 self._set_vlan[uni_id] = dict()
1276 self._set_vlan[uni_id][tp_id] = _set_vlan_vid
1277 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 -04001278 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
1279 # supported match and action criteria cannot be handled by omci and must be ignored.
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001280 if (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid != RESERVED_TRANSPARENT_VLAN:
1281 self.log.warn('ignoring-flow-that-does-not-set-vlanid', set_vlan_vid=_set_vlan_vid)
1282 elif (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid == RESERVED_TRANSPARENT_VLAN:
1283 self.log.info('set-vlanid-any', uni_id=uni_id, uni_port=uni_port,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001284 _set_vlan_vid=_vlan_vid,
1285 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1286 tp_id=tp_id)
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001287 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1288 _set_vlan_vid=_vlan_vid,
1289 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1290 tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001291 else:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001292 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, match_vlan=_vlan_vid,
1293 set_vlan_vid=_set_vlan_vid, _set_vlan_pcp=_set_vlan_pcp, ethType=_type)
1294 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1295 _set_vlan_vid=_set_vlan_vid,
1296 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1297 tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001298 except Exception as e:
1299 self.log.exception('failed-to-install-flow', e=e, flow=flow)
1300
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001301 def _add_vlan_filter_task(self, device, uni_id, uni_port=None, match_vlan=0,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001302 _set_vlan_vid=None, _set_vlan_pcp=8, tp_id=0):
Girish Gowdradc98d812020-03-20 13:04:58 -07001303 if uni_id in self._pending_delete_tp and tp_id in self._pending_delete_tp[uni_id] and \
1304 self._pending_delete_tp[uni_id][tp_id] is True:
1305 self.log.debug("pending-del-tp--scheduling-add-vlan-filter-task-for-later")
1306 reactor.callLater(0.2, self._add_vlan_filter_task, device, uni_id, uni_port, match_vlan,
1307 _set_vlan_vid, _set_vlan_pcp, tp_id)
1308 return
1309
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001310 self.log.info('_adding_vlan_filter_task', uni_port=uni_port, uni_id=uni_id, tp_id=tp_id, match_vlan=match_vlan,
1311 vlan=_set_vlan_vid, vlan_pcp=_set_vlan_pcp)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001312 assert uni_port is not None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001313 if uni_id in self._tech_profile_download_done and tp_id in self._tech_profile_download_done[uni_id] and \
1314 self._tech_profile_download_done[uni_id][tp_id] is True:
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001315 @inlineCallbacks
1316 def success(_results):
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001317 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid, tp_id=tp_id,
1318 set_vlan_pcp=_set_vlan_pcp)
Matt Jeanneretd84c9072020-01-31 06:33:27 -05001319 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-pushed')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001320
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001321 @inlineCallbacks
1322 def failure(_reason):
Girish Gowdraa73ee452019-12-20 18:52:17 +05301323 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 -08001324 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1325 reactor.callLater(retry,
1326 self._add_vlan_filter_task, device, uni_id, uni_port=uni_port,
1327 match_vlan=match_vlan, _set_vlan_vid=_set_vlan_vid,
1328 _set_vlan_pcp=_set_vlan_pcp, tp_id=tp_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -05001329 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-failed-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001330
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001331 self.log.info('setting-vlan-tag', uni_port=uni_port, uni_id=uni_id, tp_id=tp_id, match_vlan=match_vlan,
1332 vlan=_set_vlan_vid, vlan_pcp=_set_vlan_pcp)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001333 vlan_filter_add_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001334 match_vlan, _set_vlan_pcp, add_tag=True,
1335 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001336 self._deferred = self._onu_omci_device.task_runner.queue_task(vlan_filter_add_task)
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001337 self._deferred.addCallbacks(success, failure)
1338 else:
1339 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001340 uni_id=uni_id, tp_id=tp_id)
1341 if uni_id not in self._queued_vlan_filter_task:
1342 self._queued_vlan_filter_task[uni_id] = dict()
Mahir Gunyela982ec32020-02-25 12:30:37 -08001343 if tp_id not in self._queued_vlan_filter_task[uni_id]:
1344 self._queued_vlan_filter_task[uni_id][tp_id] = []
1345 self._queued_vlan_filter_task[uni_id][tp_id].append({"device": device,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001346 "uni_id": uni_id,
1347 "uni_port": uni_port,
1348 "match_vlan": match_vlan,
1349 "set_vlan_vid": _set_vlan_vid,
1350 "set_vlan_pcp": _set_vlan_pcp,
1351 "tp_id": tp_id})
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001352
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001353 def get_tp_id_in_flow(self, flow):
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001354 flow_metadata = fd.get_metadata_from_write_metadata(flow)
1355 tp_id = fd.get_tp_id_from_metadata(flow_metadata)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001356 return tp_id
1357
1358 def _remove_vlan_filter_task(self, device, uni_id, uni_port=None, match_vlan=0,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001359 _set_vlan_vid=None, _set_vlan_pcp=8, tp_id=0):
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001360 assert uni_port is not None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001361
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001362 @inlineCallbacks
1363 def success(_results):
1364 self.log.info('vlan-untagging-success', _results=_results)
1365 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-deleted')
1366
1367 @inlineCallbacks
1368 def failure(_reason):
1369 self.log.warn('vlan-untagging-failure', _reason=_reason)
1370 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-deletion-failed-retrying')
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001371 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001372 reactor.callLater(retry,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001373 self._remove_vlan_filter_task, device, uni_id,
1374 add_tag=False, uni_port=uni_port)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001375
1376 self.log.info("remove_vlan_filter_task", tp_id=tp_id)
1377 vlan_remove_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001378 match_vlan, _set_vlan_pcp, add_tag=False,
1379 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001380 self._deferred = self._onu_omci_device.task_runner.queue_task(vlan_remove_task)
1381 self._deferred.addCallbacks(success, failure)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001382
Matt Jeanneret5e331892019-12-07 21:31:45 -05001383 @inlineCallbacks
Matt Jeannereta32441c2019-03-07 05:16:37 -05001384 def process_inter_adapter_message(self, request):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001385 self.log.debug('process-inter-adapter-message', type=request.header.type, from_topic=request.header.from_topic,
1386 to_topic=request.header.to_topic, to_device_id=request.header.to_device_id)
Matt Jeanneret2101f3d2020-03-12 10:13:06 -04001387
1388 if not self.enabled:
1389 self.log.warn('device-not-activated')
1390 reactor.callLater(0.5, self.process_inter_adapter_message, request)
1391 return
1392
Matt Jeannereta32441c2019-03-07 05:16:37 -05001393 try:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001394
1395 update_onu_state = False
1396
Matt Jeannereta32441c2019-03-07 05:16:37 -05001397 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
1398 omci_msg = InterAdapterOmciMessage()
1399 request.body.Unpack(omci_msg)
Matteo Scandolod8d73172019-11-26 12:15:15 -07001400 self.log.debug('inter-adapter-recv-omci', omci_msg=hexify(omci_msg.message))
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001401
Matt Jeannereta32441c2019-03-07 05:16:37 -05001402 self.receive_message(omci_msg.message)
1403
1404 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
1405 onu_indication = OnuIndication()
1406 request.body.Unpack(onu_indication)
Matteo Scandolod8d73172019-11-26 12:15:15 -07001407 self.log.debug('inter-adapter-recv-onu-ind', onu_id=onu_indication.onu_id,
1408 oper_state=onu_indication.oper_state, admin_state=onu_indication.admin_state,
1409 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001410
Matt Jeanneret5e331892019-12-07 21:31:45 -05001411 update_onu_state = True
1412 self._onu_persisted_state['onu_id'] = onu_indication.onu_id
1413 self._onu_persisted_state['intf_id'] = onu_indication.intf_id
1414 self._onu_persisted_state['admin_state'] = onu_indication.admin_state
Mahir Gunyel45610b42020-03-16 17:29:01 -07001415 self._onu_persisted_state['oper_state'] = onu_indication.oper_state
Matt Jeanneret5e331892019-12-07 21:31:45 -05001416
Matt Jeannereta32441c2019-03-07 05:16:37 -05001417 if onu_indication.oper_state == "up":
Matt Jeanneret5e331892019-12-07 21:31:45 -05001418 yield self.create_interface(onu_indication)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301419 elif onu_indication.oper_state == "down" or onu_indication.oper_state == "unreachable":
Matt Jeanneret5e331892019-12-07 21:31:45 -05001420 yield self.update_interface(onu_indication)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001421 else:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001422 self.log.error("unknown-onu-indication", onu_id=onu_indication.onu_id,
1423 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001424
Matt Jeanneret3bfebff2019-04-12 18:25:03 -04001425 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
1426 tech_msg = InterAdapterTechProfileDownloadMessage()
1427 request.body.Unpack(tech_msg)
1428 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
1429
Matt Jeanneret5e331892019-12-07 21:31:45 -05001430 update_onu_state = self._update_onu_persisted_state(tech_msg.uni_id, tp_path=tech_msg.path)
1431 yield self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
Matt Jeanneret3bfebff2019-04-12 18:25:03 -04001432
Girish Gowdrae933cd32019-11-21 21:04:41 +05301433 elif request.header.type == InterAdapterMessageType.DELETE_GEM_PORT_REQUEST:
1434 del_gem_msg = InterAdapterDeleteGemPortMessage()
1435 request.body.Unpack(del_gem_msg)
1436 self.log.debug('inter-adapter-recv-del-gem', gem_del_msg=del_gem_msg)
1437
1438 self.delete_tech_profile(uni_id=del_gem_msg.uni_id,
1439 gem_port_id=del_gem_msg.gem_port_id,
1440 tp_path=del_gem_msg.tp_path)
1441
1442 elif request.header.type == InterAdapterMessageType.DELETE_TCONT_REQUEST:
1443 del_tcont_msg = InterAdapterDeleteTcontMessage()
1444 request.body.Unpack(del_tcont_msg)
1445 self.log.debug('inter-adapter-recv-del-tcont', del_tcont_msg=del_tcont_msg)
1446
Matt Jeanneret5e331892019-12-07 21:31:45 -05001447 # Removal of the tcont/alloc id mapping represents the removal of the tech profile
1448 update_onu_state = self._update_onu_persisted_state(del_tcont_msg.uni_id, tp_path=None)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301449 self.delete_tech_profile(uni_id=del_tcont_msg.uni_id,
1450 alloc_id=del_tcont_msg.alloc_id,
1451 tp_path=del_tcont_msg.tp_path)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001452 else:
1453 self.log.error("inter-adapter-unhandled-type", request=request)
1454
Matt Jeanneret5e331892019-12-07 21:31:45 -05001455 if update_onu_state:
1456 try:
1457 self.log.debug('updating-onu-state', device_id=self.device_id,
1458 onu_persisted_state=self._onu_persisted_state)
1459 yield self.onu_kv_client.set(self.device_id, json.dumps(self._onu_persisted_state))
1460 except Exception as e:
1461 self.log.error('could-not-store-onu-state', device_id=self.device_id,
1462 onu_persisted_state=self._onu_persisted_state, e=e)
1463 # at this point omci is started and/or indications being processed
1464 # later indications may have a chance to write this state out again
1465
Matt Jeannereta32441c2019-03-07 05:16:37 -05001466 except Exception as e:
1467 self.log.exception("error-processing-inter-adapter-message", e=e)
1468
Matt Jeanneret5e331892019-12-07 21:31:45 -05001469 def _update_onu_persisted_state(self, uni_id, tp_path):
1470 # persist the uni and tech profile path for later reconciliation. update only if changed
1471 update_onu_state = False
1472 found = False
1473 for entry in self._onu_persisted_state.get('uni_config', list()):
1474 if entry.get('uni_id') == uni_id:
1475 found = True
1476 if entry.get('tp_path') != tp_path:
1477 update_onu_state = True
1478 entry['tp_path'] = tp_path
1479
1480 if not found:
1481 update_onu_state = True
1482 uni_tp = {
1483 'uni_id': uni_id,
1484 'tp_path': tp_path
1485 }
1486 self._onu_persisted_state['uni_config'].append(uni_tp)
1487
1488 return update_onu_state
1489
Matt Jeannereta32441c2019-03-07 05:16:37 -05001490 # Called each time there is an onu "up" indication from the olt handler
1491 @inlineCallbacks
1492 def create_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001493 self.log.info('create-interface', onu_id=onu_indication.onu_id,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001494 serial_number=onu_indication.serial_number)
Amit Ghosh028eb202020-02-17 13:34:00 +00001495
1496 # Ignore if onu_indication is received for an already running ONU
1497 if self._onu_omci_device is not None and self._onu_omci_device.active:
1498 self.log.warn('received-onu-indication-for-active-onu', onu_indication=onu_indication)
1499 return
1500
Matt Jeanneretc083f462019-03-11 15:02:01 -04001501 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
1502 connect_status=ConnectStatus.REACHABLE)
1503
Matt Jeannereta32441c2019-03-07 05:16:37 -05001504 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001505
1506 self.log.debug('starting-openomci-statemachine')
1507 self._subscribe_to_events()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001508 onu_device.reason = "starting-openomci"
Girish Gowdrae933cd32019-11-21 21:04:41 +05301509 reactor.callLater(1, self._onu_omci_device.start, onu_device)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001510 yield self.core_proxy.device_reason_update(self.device_id, onu_device.reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001511 self._heartbeat.enabled = True
1512
Matt Jeanneret42dad792020-02-01 09:28:27 -05001513 # Called each time there is an onu "down" indication from the olt handler
Matt Jeannereta32441c2019-03-07 05:16:37 -05001514 @inlineCallbacks
1515 def update_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001516 self.log.info('update-interface', onu_id=onu_indication.onu_id,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001517 serial_number=onu_indication.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001518
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -04001519 if onu_indication.oper_state == 'down' or onu_indication.oper_state == "unreachable":
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001520 self.log.debug('stopping-openomci-statemachine', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001521 reactor.callLater(0, self._onu_omci_device.stop)
1522
Mahir Gunyel5de33fe2020-03-03 22:38:44 -08001523 self._tp = dict()
1524
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001525 # Let TP download happen again
1526 for uni_id in self._tp_service_specific_task:
1527 self._tp_service_specific_task[uni_id].clear()
1528 for uni_id in self._tech_profile_download_done:
1529 self._tech_profile_download_done[uni_id].clear()
1530
Matt Jeanneretf4113222019-08-14 19:44:34 -04001531 yield self.disable_ports(lock_ports=False)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001532 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
1533 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.DISCOVERED,
1534 connect_status=ConnectStatus.UNREACHABLE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001535 else:
1536 self.log.debug('not-changing-openomci-statemachine')
1537
Matt Jeanneretf4113222019-08-14 19:44:34 -04001538 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001539 def disable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001540 self.log.info('disable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001541 try:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001542 yield self.disable_ports(lock_ports=True, device_disabled=True)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001543 yield self.core_proxy.device_reason_update(self.device_id, "omci-admin-lock")
1544 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001545 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001546 self.log.exception('exception-in-onu-disable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001547
William Kurkian3a206332019-04-29 11:05:47 -04001548 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001549 def reenable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001550 self.log.info('reenable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001551 try:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001552 yield self.core_proxy.device_state_update(device.id,
1553 oper_status=OperStatus.ACTIVE,
1554 connect_status=ConnectStatus.REACHABLE)
1555 yield self.core_proxy.device_reason_update(self.device_id, 'onu-reenabled')
1556 yield self.enable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001557 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001558 self.log.exception('exception-in-onu-reenable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001559
William Kurkian3a206332019-04-29 11:05:47 -04001560 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001561 def reboot(self):
1562 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -04001563 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001564 if device.connect_status != ConnectStatus.REACHABLE:
1565 self.log.error("device-unreachable")
1566 return
1567
William Kurkian3a206332019-04-29 11:05:47 -04001568 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001569 def success(_results):
1570 self.log.info('reboot-success', _results=_results)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001571 yield self.core_proxy.device_reason_update(self.device_id, 'rebooting')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001572
1573 def failure(_reason):
1574 self.log.info('reboot-failure', _reason=_reason)
1575
1576 self._deferred = self._onu_omci_device.reboot()
1577 self._deferred.addCallbacks(success, failure)
1578
William Kurkian3a206332019-04-29 11:05:47 -04001579 @inlineCallbacks
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001580 def disable_ports(self, lock_ports=True, device_disabled=False):
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001581 self.log.info('disable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001582
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001583 # TODO: for now only support the first UNI given no requirement for multiple uni yet. Also needed to reduce flow
1584 # load on the core
Matt Jeanneretf4113222019-08-14 19:44:34 -04001585 for port in self.uni_ports:
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001586 if port.mac_bridge_port_num == 1:
1587 port.operstatus = OperStatus.UNKNOWN
1588 self.log.info('disable-port', device_id=self.device_id, port=port)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001589 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number,
1590 port.operstatus)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001591
1592 if lock_ports is True:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001593 self.lock_ports(lock=True, device_disabled=device_disabled)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001594
William Kurkian3a206332019-04-29 11:05:47 -04001595 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001596 def enable_ports(self):
1597 self.log.info('enable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001598
Matt Jeanneretf4113222019-08-14 19:44:34 -04001599 self.lock_ports(lock=False)
1600
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001601 # TODO: for now only support the first UNI given no requirement for multiple uni yet. Also needed to reduce flow
1602 # load on the core
1603 # Given by default all unis are initially active according to omci alarming, we must mimic this.
Matt Jeanneretf4113222019-08-14 19:44:34 -04001604 for port in self.uni_ports:
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001605 if port.mac_bridge_port_num == 1:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001606 port.operstatus = OperStatus.ACTIVE
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001607 self.log.info('enable-port', device_id=self.device_id, port=port)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001608 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number,
1609 port.operstatus)
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001610
1611 # TODO: Normally we would want any uni ethernet link down or uni ethernet link up alarms to register in the core,
1612 # but practically olt provisioning cannot handle the churn of links up, down, then up again typical on startup.
1613 #
1614 # Basically the link state sequence:
1615 # 1) per omci default alarm state, all unis are initially up (no link down alarms received yet)
1616 # 2) a link state down alarm is received for all uni, given the lock command, and also because most unis have nothing plugged in
1617 # 3) a link state up alarm is received for the uni plugged in.
1618 #
1619 # Given the olt (BAL) has to provision all uni, de-provision all uni, and re-provision one uni in quick succession
1620 # and cannot (bug?), we have to skip this and leave uni ports as assumed active. Also all the link state activity
1621 # would have a ripple effect through the core to the controller as well. And is it really worth it?
1622 '''
Matt Jeanneretf4113222019-08-14 19:44:34 -04001623 @inlineCallbacks
1624 def port_state_handler(self, _topic, msg):
1625 self.log.info("port-state-change", _topic=_topic, msg=msg)
1626
1627 onu_id = msg['onu_id']
1628 port_no = msg['port_number']
1629 serial_number = msg['serial_number']
1630 port_status = msg['port_status']
1631 uni_port = self.uni_port(int(port_no))
1632
1633 self.log.debug("port-state-parsed-message", onu_id=onu_id, port_no=port_no, serial_number=serial_number,
1634 port_status=port_status)
1635
1636 if port_status is True:
1637 uni_port.operstatus = OperStatus.ACTIVE
1638 self.log.info('link-up', device_id=self.device_id, port=uni_port)
1639 else:
1640 uni_port.operstatus = OperStatus.UNKNOWN
1641 self.log.info('link-down', device_id=self.device_id, port=uni_port)
1642
1643 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 -05001644 '''
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001645
1646 # Called just before openomci state machine is started. These listen for events from selected state machines,
1647 # most importantly, mib in sync. Which ultimately leads to downloading the mib
1648 def _subscribe_to_events(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001649 self.log.debug('subscribe-to-events')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001650
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001651 bus = self._onu_omci_device.event_bus
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001652
1653 # OMCI MIB Database sync status
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001654 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1655 OnuDeviceEvents.MibDatabaseSyncEvent)
1656 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
1657
1658 # OMCI Capabilities
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001659 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1660 OnuDeviceEvents.OmciCapabilitiesEvent)
1661 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
1662
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001663 # TODO: these alarms seem to be unreliable depending on the environment
1664 # 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 -08001665 # topic = OnuDeviceEntry.event_bus_topic(self.device_id,
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001666 # OnuDeviceEvents.PortEvent)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001667 # self._port_state_subscription = bus.subscribe(topic, self.port_state_handler)
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001668
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001669 # Called when the mib is in sync
1670 def in_sync_handler(self, _topic, msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001671 self.log.debug('in-sync-handler', _topic=_topic, msg=msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001672 if self._in_sync_subscription is not None:
1673 try:
1674 in_sync = msg[IN_SYNC_KEY]
1675
1676 if in_sync:
1677 # Only call this once
1678 bus = self._onu_omci_device.event_bus
1679 bus.unsubscribe(self._in_sync_subscription)
1680 self._in_sync_subscription = None
1681
1682 # Start up device_info load
1683 self.log.debug('running-mib-sync')
1684 reactor.callLater(0, self._mib_in_sync)
1685
1686 except Exception as e:
1687 self.log.exception('in-sync', e=e)
1688
1689 def capabilties_handler(self, _topic, _msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001690 self.log.debug('capabilities-handler', _topic=_topic, msg=_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001691 if self._capabilities_subscription is not None:
1692 self.log.debug('capabilities-handler-done')
1693
1694 # 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 -04001695 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001696 def _mib_in_sync(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001697 self.log.debug('mib-in-sync')
Matt Jeanneretc083f462019-03-11 15:02:01 -04001698 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001699
Matt Jeanneret5e331892019-12-07 21:31:45 -05001700 # only notify core if this is a new device. otherwise do not have reconcile generating
1701 # a lot of needless message churn
1702 if not self._reconciling:
1703 yield self.core_proxy.device_reason_update(self.device_id, 'discovery-mibsync-complete')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001704
1705 if self._dev_info_loaded:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001706 self.log.debug('device-info-already-loaded')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001707 else:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001708 # new onu or adapter was restarted. fill up our local data
1709 yield self._load_device_data(device)
1710
1711 if self._check_mib_downloaded():
1712 self.log.debug('mib-already-downloaded')
1713 if not self._reconciling:
1714 yield self.core_proxy.device_state_update(device.id,
1715 oper_status=OperStatus.ACTIVE,
1716 connect_status=ConnectStatus.REACHABLE)
1717 yield self.enable_ports()
1718 else:
1719 self._download_mib(device)
1720
1721 if self._reconciling:
1722 yield self._restore_tech_profile()
1723 self._start_monitoring()
1724 self._reconciling = False
1725 self.log.debug('reconcile-finished')
1726
1727 def _download_mib(self, device):
1728 self.log.debug('downloading-initial-mib-configuration')
1729
1730 @inlineCallbacks
1731 def success(_results):
1732 self.log.debug('mib-download-success', _results=_results)
1733 yield self.core_proxy.device_state_update(device.id,
1734 oper_status=OperStatus.ACTIVE,
1735 connect_status=ConnectStatus.REACHABLE)
1736 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-downloaded')
1737 self._mib_download_task = None
1738 yield self.enable_ports()
1739 yield self.onu_active_event()
1740 self._start_monitoring()
1741
1742 @inlineCallbacks
1743 def failure(_reason):
1744 self.log.warn('mib-download-failure-retrying', _reason=_reason)
1745 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1746 reactor.callLater(retry, self._mib_in_sync)
1747 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-download-failure-retrying')
1748
1749 # start by locking all the unis till mib sync and initial mib is downloaded
1750 # this way we can capture the port down/up events when we are ready
1751 self.lock_ports(lock=True)
1752
1753 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1754 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1755 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1756 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1757 self._deferred.addCallbacks(success, failure)
1758
1759 def _start_monitoring(self):
1760 self.log.debug('starting-monitoring')
1761
1762 # Start collecting stats from the device after a brief pause
1763 if not self._pm_metrics_started:
1764 self._pm_metrics_started = True
1765 pmstart = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1766 reactor.callLater(pmstart, self._pm_metrics.start_collector)
1767
1768 # Start test requests after a brief pause
1769 if not self._test_request_started:
1770 self._test_request_started = True
1771 tststart = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1772 reactor.callLater(tststart, self._test_request.start_collector)
1773
1774 def _check_mib_downloaded(self):
1775 self.log.debug('checking-mib-downloaded')
1776 results = False
1777
1778 mac_bridges = self.onu_omci_device.query_mib(MacBridgeServiceProfile.class_id)
1779 self.log.debug('mac-bridges', mac_bridges=mac_bridges)
1780
1781 for k, v in mac_bridges.items():
1782 if not isinstance(v, dict):
1783 continue
1784 # found at least one mac bridge, good enough to say its done, break out
1785 self.log.debug('found-mac-bridge-mib-download-has-been-done', omci_key=k, omci_value=v)
1786 results = True
1787 break
1788
1789 return results
1790
1791 @inlineCallbacks
1792 def _load_device_data(self, device):
1793 self.log.debug('loading-device-data-from-mib', device_id=device.id)
1794
1795 omci_dev = self._onu_omci_device
1796 config = omci_dev.configuration
1797
1798 try:
1799 # sort the lists so we get consistent port ordering.
1800 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
1801 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
1802 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
1803 veip_list = sorted(config.veip_entities) if config.veip_entities else []
1804
1805 if ani_list is None or (pptp_list is None and veip_list is None):
1806 yield self.core_proxy.device_reason_update(self.device_id, 'onu-missing-required-elements')
1807 raise Exception("onu-missing-required-elements")
1808
1809 # Currently logging the ani, pptp, veip, and uni for information purposes.
1810 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
1811 # And in some ONU the UNI-G list is incomplete or incorrect...
1812 for entity_id in ani_list:
1813 ani_value = config.ani_g_entities[entity_id]
1814 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
1815
1816 for entity_id in uni_list:
1817 uni_value = config.uni_g_entities[entity_id]
1818 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
1819
1820 uni_entities = OrderedDict()
1821 for entity_id in pptp_list:
1822 pptp_value = config.pptp_entities[entity_id]
1823 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
1824 uni_entities[entity_id] = UniType.PPTP
1825
1826 for entity_id in veip_list:
1827 veip_value = config.veip_entities[entity_id]
1828 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
1829 uni_entities[entity_id] = UniType.VEIP
1830
1831 uni_id = 0
1832 for entity_id, uni_type in uni_entities.items():
1833 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
1834 uni_id += 1
1835
1836 if self._unis:
1837 self._dev_info_loaded = True
1838 else:
1839 yield self.core_proxy.device_reason_update(self.device_id, 'no-usable-unis')
1840 raise Exception("no-usable-unis")
1841
1842 except Exception as e:
1843 self.log.exception('device-info-load', e=e)
1844 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001845
Matt Jeanneretc083f462019-03-11 15:02:01 -04001846 @inlineCallbacks
1847 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matt Jeanneret5e331892019-12-07 21:31:45 -05001848 self.log.debug('add-uni-port', entity_id=entity_id, uni_id=uni_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001849
Matt Jeanneret5e331892019-12-07 21:31:45 -05001850 intf_id = self._onu_persisted_state.get('intf_id')
1851 onu_id = self._onu_persisted_state.get('onu_id')
1852 uni_no = self.mk_uni_port_num(intf_id, onu_id, uni_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001853
1854 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1855 uni_name = "uni-{}".format(uni_no)
1856
Girish Gowdrae933cd32019-11-21 21:04:41 +05301857 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001858
1859 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 -04001860 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001861
1862 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1863 uni_port.entity_id = entity_id
1864 uni_port.enabled = True
1865 uni_port.mac_bridge_port_num = mac_bridge_port_num
1866
1867 self.log.debug("created-uni-port", uni=uni_port)
1868
Matt Jeanneret5e331892019-12-07 21:31:45 -05001869 if not self._reconciling:
1870 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001871
1872 self._unis[uni_port.port_number] = uni_port
1873
Matt Jeanneret5e331892019-12-07 21:31:45 -05001874 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=onu_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301875 uni_ports=self.uni_ports,
1876 serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001877
Matt Jeanneret5e331892019-12-07 21:31:45 -05001878 @inlineCallbacks
1879 def _restore_tech_profile(self):
1880 self.log.debug("reconcile-restoring-tech-profile-tcont-gem-config")
1881
1882 # for every uni that has tech profile config reload all its tcont/alloc_id and gem from the tp path
1883 for entry in self._onu_persisted_state.get('uni_config', list()):
1884 uni_id = entry.get('uni_id')
1885 tp_path = entry.get('tp_path')
1886 if tp_path:
1887 tpstored = yield self.tp_kv_client.get(tp_path)
1888 tpstring = tpstored.decode('ascii')
1889 tp = json.loads(tpstring)
1890
1891 self.log.debug("restoring-tp-instance", tp=tp)
1892
1893 # re-run tech profile config that stores gem and tconts in the self._pon object
1894 # this does not actually re-run the omci, just rebuilds our local data store
1895 self._do_tech_profile_configuration(uni_id, tp)
1896
1897 tp_id = self.extract_tp_id_from_path(tp_path)
1898
1899 # rebuild cache dicts so tp updates and deletes dont get KeyErrors
1900 if uni_id not in self._tp_service_specific_task:
1901 self._tp_service_specific_task[uni_id] = dict()
1902
1903 if uni_id not in self._tech_profile_download_done:
1904 self._tech_profile_download_done[uni_id] = dict()
1905
1906 if tp_id not in self._tech_profile_download_done[uni_id]:
1907 self._tech_profile_download_done[uni_id][tp_id] = True
1908 else:
1909 self.log.debug("no-assigned-tp-instance", uni_id=uni_id)
1910
1911 # for every loaded tcont from tp check the mib database for its entity_id
1912 # needed for later tp deletes/adds
1913 tcont_idents = self.onu_omci_device.query_mib(Tcont.class_id)
1914 self.log.debug('tcont-idents', tcont_idents=tcont_idents)
1915
1916 for k, v in tcont_idents.items():
1917 if not isinstance(v, dict):
1918 continue
1919 alloc_check = v.get('attributes', {}).get('alloc_id', 0)
1920 tcont = self._pon.tconts.get(alloc_check)
1921 if tcont:
1922 tcont.entity_id = k
1923 self.log.debug('reassigning-tcont-entity-id', entity_id=tcont.entity_id,
1924 alloc_id=tcont.alloc_id)
1925
Matt Jeanneretc083f462019-03-11 15:02:01 -04001926 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1927 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
Amit Ghosh65400f12019-11-21 12:04:12 +00001928 MAX_PONS_PER_OLT = 256
1929 MAX_ONUS_PER_PON = 256
Matt Jeanneretc083f462019-03-11 15:02:01 -04001930 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001931
Matt Jeanneretc083f462019-03-11 15:02:01 -04001932 assert intf_id < MAX_PONS_PER_OLT
1933 assert onu_id < MAX_ONUS_PER_PON
1934 assert uni_id < MAX_UNIS_PER_ONU
Amit Ghosh65400f12019-11-21 12:04:12 +00001935 return intf_id << 12 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001936
1937 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001938 def onu_active_event(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001939 self.log.debug('onu-active-event')
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001940 try:
Matt Jeanneret5e331892019-12-07 21:31:45 -05001941 # TODO: this is expensive for just getting the olt serial number. replace with direct api call
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001942 parent_device = yield self.core_proxy.get_device(self.parent_id)
1943 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001944 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001945
Matt Jeanneret5e331892019-12-07 21:31:45 -05001946 intf_id = self._onu_persisted_state.get('intf_id')
1947 onu_id = self._onu_persisted_state.get('onu_id')
1948 onu_serial = self._onu_persisted_state.get('serial_number')
1949
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001950 self.log.debug("onu-indication-context-data",
Matt Jeanneret5e331892019-12-07 21:31:45 -05001951 pon_id=intf_id,
1952 onu_id=onu_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301953 registration_id=self.device_id,
1954 device_id=self.device_id,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001955 onu_serial_number=onu_serial,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301956 olt_serial_number=olt_serial_number,
1957 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001958
Devmalya Paulffc89df2019-07-31 17:43:13 -04001959 self.log.debug("Trying-to-raise-onu-active-event")
1960 OnuActiveEvent(self.events, self.device_id,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001961 intf_id,
1962 onu_serial,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001963 str(self.device_id),
Girish Gowdrae933cd32019-11-21 21:04:41 +05301964 olt_serial_number, raised_ts,
Matt Jeanneret5e331892019-12-07 21:31:45 -05001965 onu_id=onu_id).send(True)
Devmalya Paulffc89df2019-07-31 17:43:13 -04001966 except Exception as active_event_error:
1967 self.log.exception('onu-activated-event-error',
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001968 errmsg=active_event_error)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001969
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001970 @inlineCallbacks
1971 def onu_disabled_event(self):
1972 self.log.debug('onu-disabled-event')
1973 try:
1974 device = yield self.core_proxy.get_device(self.device_id)
1975 parent_device = yield self.core_proxy.get_device(self.parent_id)
1976 olt_serial_number = parent_device.serial_number
1977 raised_ts = arrow.utcnow().timestamp
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001978 intf_id = self._onu_persisted_state.get('intf_id')
1979 onu_id = self._onu_persisted_state.get('onu_id')
1980 onu_serial = self._onu_persisted_state.get('serial_number')
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001981
1982 self.log.debug("onu-indication-context-data",
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001983 pon_id=intf_id,
1984 onu_id=onu_id,
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001985 registration_id=self.device_id,
1986 device_id=self.device_id,
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001987 onu_serial_number=onu_serial,
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001988 olt_serial_number=olt_serial_number,
1989 raised_ts=raised_ts)
1990
1991 self.log.debug("Trying-to-raise-onu-disabled-event")
1992 OnuDisabledEvent(self.events, self.device_id,
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001993 intf_id,
Girish Gowdradc98d812020-03-20 13:04:58 -07001994 device.serial_number,
1995 str(self.device_id),
1996 olt_serial_number, raised_ts,
Devmalya Paul1e1b1722020-05-07 02:51:15 -04001997 onu_id=onu_id).send(True)
1998 except Exception as disable_event_error:
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04001999 self.log.exception('onu-disabled-event-error',
Devmalya Paul1e1b1722020-05-07 02:51:15 -04002000 errmsg=disable_event_error)
2001
2002 @inlineCallbacks
2003 def onu_deleted_event(self):
2004 self.log.debug('onu-deleted-event')
2005 try:
2006 device = yield self.core_proxy.get_device(self.device_id)
2007 parent_device = yield self.core_proxy.get_device(self.parent_id)
2008 olt_serial_number = parent_device.serial_number
2009 raised_ts = arrow.utcnow().timestamp
2010 intf_id = self._onu_persisted_state.get('intf_id')
2011 onu_id = self._onu_persisted_state.get('onu_id')
2012 serial_number = self._onu_persisted_state.get('serial_number')
2013
2014 self.log.debug("onu-deleted-event-context-data",
2015 pon_id=intf_id,
2016 onu_id=onu_id,
2017 registration_id=self.device_id,
2018 device_id=self.device_id,
2019 onu_serial_number=serial_number,
2020 olt_serial_number=olt_serial_number,
2021 raised_ts=raised_ts)
2022
2023 OnuDeletedEvent(self.events, self.device_id,
2024 intf_id,
2025 serial_number,
2026 str(self.device_id),
2027 olt_serial_number, raised_ts,
2028 onu_id=onu_id).send(True)
2029 except Exception as deleted_event_error:
2030 self.log.exception('onu-deleted-event-error',
2031 errmsg=deleted_event_error)
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04002032
2033 def lock_ports(self, lock=True, device_disabled=False):
Matt Jeanneretf4113222019-08-14 19:44:34 -04002034
2035 def success(response):
2036 self.log.debug('set-onu-ports-state', lock=lock, response=response)
Devmalya Paule2e5f2b2020-03-08 18:50:33 -04002037 if device_disabled:
2038 self.onu_disabled_event()
Matt Jeanneretf4113222019-08-14 19:44:34 -04002039
2040 def failure(response):
2041 self.log.error('cannot-set-onu-ports-state', lock=lock, response=response)
2042
2043 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=lock)
2044 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
2045 self._deferred.addCallbacks(success, failure)
Mahir Gunyele9110a32020-02-20 14:56:50 -08002046
2047 def extract_tp_id_from_path(self, tp_path):
2048 # tp_path is of the format <technology>/<table_id>/<uni_port_name>
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08002049 tp_id = int(tp_path.split(_PATH_SEPERATOR)[1])
2050 return tp_id
onkarkundargia1e2af22020-01-27 11:51:43 +05302051
2052 def start_omci_test_action(self, device, uuid):
2053 """
2054
2055 :param device:
2056 :return:
2057 """
2058 # Code to Run OMCI Test Action
2059 self.log.info('Omci-test-action-request-On', request=device.id)
2060 kwargs_omci_test_action = {
2061 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
2062 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
2063 }
2064 serial_number = device.serial_number
2065 if device.connect_status != ConnectStatus.REACHABLE or device.admin_state != AdminState.ENABLED:
2066 return (TestResponse(result=TestResponse.FAILURE))
2067 test_request = OmciTestRequest(self.core_proxy,
2068 self.omci_agent, self.device_id, AniG,
2069 serial_number,
2070 self.logical_device_id, exclusive=False,
2071 uuid=uuid,
2072 **kwargs_omci_test_action)
2073 test_request.perform_test_omci()
2074 return (TestResponse(result=TestResponse.SUCCESS))