blob: dc84d6a26aecd3b045dd4db8641bed4171feae49 [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
31from twisted.internet.defer import DeferredQueue, inlineCallbacks
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
35from pyvoltha.adapters.extensions.events.kpi.onu.onu_pm_metrics import OnuPmMetrics
36from pyvoltha.adapters.extensions.events.kpi.onu.onu_omci_pm import OnuOmciPmMetrics
37from pyvoltha.adapters.extensions.events.adapter_events import AdapterEvents
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050038
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050039import pyvoltha.common.openflow.utils as fd
40from pyvoltha.common.utils.registry import registry
Matteo Scandolod8d73172019-11-26 12:15:15 -070041from pyvoltha.adapters.common.frameio.frameio import hexify
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050042from pyvoltha.common.utils.nethelpers import mac_str_to_tuple
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050043from pyvoltha.common.config.config_backend import ConsulStore
44from pyvoltha.common.config.config_backend import EtcdStore
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050045from voltha_protos.logical_device_pb2 import LogicalPort
William Kurkian8235c1e2019-03-05 12:58:28 -050046from voltha_protos.common_pb2 import OperStatus, ConnectStatus, AdminState
Matt Jeanneretf4113222019-08-14 19:44:34 -040047from voltha_protos.device_pb2 import Port
Girish Gowdra4c11ddb2020-03-03 11:33:24 -080048from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, ofp_port, OFPPS_LIVE, OFPPS_LINK_DOWN, \
Matt Jeanneretf4113222019-08-14 19:44:34 -040049 OFPPF_FIBER, OFPPF_1GB_FD
Matt Jeanneret3bfebff2019-04-12 18:25:03 -040050from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
Girish Gowdrae933cd32019-11-21 21:04:41 +053051 InterAdapterOmciMessage, PortCapability, InterAdapterTechProfileDownloadMessage, InterAdapterDeleteGemPortMessage, \
52 InterAdapterDeleteTcontMessage
Matt Jeannereta32441c2019-03-07 05:16:37 -050053from voltha_protos.openolt_pb2 import OnuIndication
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050054from pyvoltha.adapters.extensions.omci.onu_configuration import OMCCVersion
55from 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
65from uni_port import UniPort, UniType
Andrea Campanellacf916ea2020-02-14 10:03:58 +010066from uni_port import RESERVED_TRANSPARENT_VLAN
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050067from pyvoltha.common.tech_profile.tech_profile import TechProfile
onkarkundargiaae99712019-09-23 15:02:52 +053068from pyvoltha.adapters.extensions.omci.tasks.omci_test_request import OmciTestRequest
69from pyvoltha.adapters.extensions.omci.omci_entities import AniG
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050070from pyvoltha.adapters.extensions.omci.omci_defs import EntityOperations, ReasonCodes
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050071
72OP = EntityOperations
73RC = ReasonCodes
74
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050075_STARTUP_RETRY_WAIT = 10
Mahir Gunyele9110a32020-02-20 14:56:50 -080076_PATH_SEPERATOR = "/"
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050077
78
79class BrcmOpenomciOnuHandler(object):
80
81 def __init__(self, adapter, device_id):
82 self.log = structlog.get_logger(device_id=device_id)
Matt Jeanneret08a8e862019-12-20 14:02:32 -050083 self.log.debug('starting-handler')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050084 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050085 self.core_proxy = adapter.core_proxy
86 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050087 self.parent_id = None
88 self.device_id = device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050089 self.proxy_address = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050090 self._enabled = False
Devmalya Paulffc89df2019-07-31 17:43:13 -040091 self.events = None
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050092 self._pm_metrics = None
93 self._pm_metrics_started = False
94 self._test_request = None
95 self._test_request_started = False
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050096 self._omcc_version = OMCCVersion.Unknown
97 self._total_tcont_count = 0 # From ANI-G ME
98 self._qos_flexibility = 0 # From ONT2_G ME
99
100 self._onu_indication = None
101 self._unis = dict() # Port # -> UniPort
102
103 self._pon = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500104 self._pon_port_number = 100
105 self.logical_device_id = None
106
107 self._heartbeat = HeartBeat.create(self, device_id)
108
109 # Set up OpenOMCI environment
110 self._onu_omci_device = None
111 self._dev_info_loaded = False
112 self._deferred = None
113
114 self._in_sync_subscription = None
Matt Jeanneretf4113222019-08-14 19:44:34 -0400115 self._port_state_subscription = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500116 self._connectivity_subscription = None
117 self._capabilities_subscription = None
118
119 self.mac_bridge_service_profile_entity_id = 0x201
120 self.gal_enet_profile_entity_id = 0x1
121
122 self._tp_service_specific_task = dict()
123 self._tech_profile_download_done = dict()
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400124 # Stores information related to queued vlan filter tasks
125 # Dictionary with key being uni_id and value being device,uni port ,uni id and vlan id
126
127 self._queued_vlan_filter_task = dict()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500128
129 # Initialize KV store client
130 self.args = registry('main').get_args()
131 if self.args.backend == 'etcd':
132 host, port = self.args.etcd.split(':', 1)
133 self.kv_client = EtcdStore(host, port,
134 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
135 elif self.args.backend == 'consul':
136 host, port = self.args.consul.split(':', 1)
137 self.kv_client = ConsulStore(host, port,
138 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
139 else:
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500140 self.log.error('invalid-backend')
141 raise Exception("invalid-backend-for-kv-store")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500142
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500143 @property
144 def enabled(self):
145 return self._enabled
146
147 @enabled.setter
148 def enabled(self, value):
149 if self._enabled != value:
150 self._enabled = value
151
152 @property
153 def omci_agent(self):
154 return self.adapter.omci_agent
155
156 @property
157 def omci_cc(self):
158 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
159
160 @property
161 def heartbeat(self):
162 return self._heartbeat
163
164 @property
165 def uni_ports(self):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500166 return list(self._unis.values())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500167
168 def uni_port(self, port_no_or_name):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500169 if isinstance(port_no_or_name, six.string_types):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500170 return next((uni for uni in self.uni_ports
171 if uni.name == port_no_or_name), None)
172
173 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
174 return next((uni for uni in self.uni_ports
Girish Gowdrae933cd32019-11-21 21:04:41 +0530175 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500176
177 @property
178 def pon_port(self):
179 return self._pon
180
Girish Gowdraa73ee452019-12-20 18:52:17 +0530181 @property
182 def onu_omci_device(self):
183 return self._onu_omci_device
184
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500185 def receive_message(self, msg):
186 if self.omci_cc is not None:
187 self.omci_cc.receive_message(msg)
188
Matt Jeanneretc083f462019-03-11 15:02:01 -0400189 def get_ofp_port_info(self, device, port_no):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500190 self.log.debug('get-ofp-port-info', port_no=port_no, device_id=device.id)
Matt Jeanneretc083f462019-03-11 15:02:01 -0400191 cap = OFPPF_1GB_FD | OFPPF_FIBER
192
Girish Gowdrae933cd32019-11-21 21:04:41 +0530193 hw_addr = mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
194 ((device.parent_port_no >> 8 & 0xff),
195 device.parent_port_no & 0xff,
196 (port_no >> 16) & 0xff,
197 (port_no >> 8) & 0xff,
198 port_no & 0xff))
Matt Jeanneretc083f462019-03-11 15:02:01 -0400199
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400200 uni_port = self.uni_port(int(port_no))
201 name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500202 self.log.debug('ofp-port-name', port_no=port_no, name=name, uni_port=uni_port)
Matt Jeanneretf4113222019-08-14 19:44:34 -0400203
204 ofstate = OFPPS_LINK_DOWN
205 if uni_port.operstatus is OperStatus.ACTIVE:
206 ofstate = OFPPS_LIVE
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400207
Matt Jeanneretc083f462019-03-11 15:02:01 -0400208 return PortCapability(
209 port=LogicalPort(
210 ofp_port=ofp_port(
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400211 name=name,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400212 hw_addr=hw_addr,
213 config=0,
Matt Jeanneretf4113222019-08-14 19:44:34 -0400214 state=ofstate,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400215 curr=cap,
216 advertised=cap,
217 peer=cap,
218 curr_speed=OFPPF_1GB_FD,
219 max_speed=OFPPF_1GB_FD
220 ),
221 device_id=device.id,
222 device_port_no=port_no
223 )
224 )
225
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500226 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500227 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500228 def activate(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700229 self.log.debug('activate-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500230
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500231 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500232 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500233 assert device.proxy_address.device_id
234
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500235 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500236 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500237 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500238 if self.enabled is not True:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700239 self.log.info('activating-new-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500240 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500241 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500242 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500243 device.reason = 'activating-onu'
244
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500245 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400246 # pm_metrics requires a logical device id. For now set to just device_id
247 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500248
Matt Jeannereta32441c2019-03-07 05:16:37 -0500249 yield self.core_proxy.device_update(device)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500250 self.log.debug('device-updated', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500251
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700252 yield self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500253
Matteo Scandolod8d73172019-11-26 12:15:15 -0700254 self.log.debug('pon state initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500255 ############################################################################
Devmalya Paulffc89df2019-07-31 17:43:13 -0400256 # Setup Alarm handler
257 self.events = AdapterEvents(self.core_proxy, device.id, self.logical_device_id,
258 device.serial_number)
259 ############################################################################
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500260 # Setup PM configuration for this device
261 # Pass in ONU specific options
262 kwargs = {
263 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
264 'heartbeat': self.heartbeat,
265 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
266 }
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500267 self.log.debug('create-pm-metrics', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500268 self._pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800269 self.logical_device_id, device.serial_number,
270 grouped=True, freq_override=False, **kwargs)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500271 pm_config = self._pm_metrics.make_proto()
272 self._onu_omci_device.set_pm_config(self._pm_metrics.omci_pm.openomci_interval_pm)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530273 self.log.info("initial-pm-config", device_id=device.id, serial_number=device.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500274 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500275
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500276 # Note, ONU ID and UNI intf set in add_uni_port method
Devmalya Paulffc89df2019-07-31 17:43:13 -0400277 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500278 ani_ports=[self._pon])
aishwaryarana01a98d9fe2019-05-08 12:09:06 -0500279
onkarkundargiaae99712019-09-23 15:02:52 +0530280 # Code to Run OMCI Test Action
281 kwargs_omci_test_action = {
282 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
283 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
284 }
285 serial_number = device.serial_number
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500286 self._test_request = OmciTestRequest(self.core_proxy,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800287 self.omci_agent, self.device_id,
288 AniG, serial_number,
289 self.logical_device_id,
290 exclusive=False,
291 **kwargs_omci_test_action)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500292
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500293 self.enabled = True
294 else:
295 self.log.info('onu-already-activated')
296
297 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400298 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500299 def reconcile(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700300 self.log.debug('reconcile-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500301
302 # first we verify that we got parent reference and proxy info
303 assert device.parent_id
304 assert device.proxy_address.device_id
305
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700306 self.proxy_address = device.proxy_address
307 self.parent_id = device.parent_id
308 self._pon_port_number = device.parent_port_no
309
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500310 if self.enabled is not True:
311 self.log.info('reconciling-broadcom-onu-device')
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700312 self.logical_device_id = self.device_id
313 self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500314
315 # need to restart state machines on vcore restart. there is no indication to do it for us.
316 self._onu_omci_device.start()
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700317 yield self.core_proxy.device_reason_update(self.device_id, "restarting-openomci")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500318
319 # TODO: this is probably a bit heavy handed
320 # Force a reboot for now. We need indications to reflow to reassign tconts and gems given vcore went away
321 # This may not be necessary when mib resync actually works
322 reactor.callLater(1, self.reboot)
323
324 self.enabled = True
325 else:
326 self.log.info('onu-already-activated')
327
328 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700329 def _init_pon_state(self):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500330 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 -0500331
332 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500333 self._pon.add_peer(self.parent_id, self._pon_port_number)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700334 self.log.debug('adding-pon-port-to-agent',
335 type=self._pon.get_port().type,
336 admin_state=self._pon.get_port().admin_state,
337 oper_status=self._pon.get_port().oper_status,
338 )
Matt Jeanneret0c287892019-02-28 11:48:00 -0500339
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700340 yield self.core_proxy.port_created(self.device_id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500341
Matteo Scandolod8d73172019-11-26 12:15:15 -0700342 self.log.debug('added-pon-port-to-agent',
343 type=self._pon.get_port().type,
344 admin_state=self._pon.get_port().admin_state,
345 oper_status=self._pon.get_port().oper_status,
346 )
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500347
348 # Create and start the OpenOMCI ONU Device Entry for this ONU
349 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500350 self.core_proxy,
351 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500352 support_classes=self.adapter.broadcom_omci,
353 custom_me_map=self.adapter.custom_me_entities())
354 # Port startup
355 if self._pon is not None:
356 self._pon.enabled = True
357
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500358 def delete(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700359 self.log.info('delete-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneret42dad792020-02-01 09:28:27 -0500360
361 self._deferred.cancel()
362 self._test_request.stop_collector()
363 self._pm_metrics.stop_collector()
364 self.log.debug('removing-openomci-statemachine')
365 self.omci_agent.remove_device(device.id, cleanup=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500366
367 def _create_tconts(self, uni_id, us_scheduler):
368 alloc_id = us_scheduler['alloc_id']
369 q_sched_policy = us_scheduler['q_sched_policy']
370 self.log.debug('create-tcont', us_scheduler=us_scheduler)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800371 # TODO: revisit for multi tconts support
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800372 new_tconts = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500373 tcontdict = dict()
374 tcontdict['alloc-id'] = alloc_id
375 tcontdict['q_sched_policy'] = q_sched_policy
376 tcontdict['uni_id'] = uni_id
377
Matt Jeanneret3789d0d2020-01-19 09:03:42 -0500378 tcont = OnuTCont.create(self, tcont=tcontdict)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500379
380 self._pon.add_tcont(tcont)
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800381 new_tconts.append(tcont)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500382 self.log.debug('pon-add-tcont', tcont=tcont)
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800383 return new_tconts
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500384
385 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
386 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
387 self.log.debug('create-gemport',
388 gem_ports=gem_ports, direction=direction)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530389 new_gem_ports = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500390 for gem_port in gem_ports:
391 gemdict = dict()
392 gemdict['gemport_id'] = gem_port['gemport_id']
393 gemdict['direction'] = direction
394 gemdict['alloc_id_ref'] = alloc_id_ref
395 gemdict['encryption'] = gem_port['aes_encryption']
396 gemdict['discard_config'] = dict()
397 gemdict['discard_config']['max_probability'] = \
398 gem_port['discard_config']['max_probability']
399 gemdict['discard_config']['max_threshold'] = \
400 gem_port['discard_config']['max_threshold']
401 gemdict['discard_config']['min_threshold'] = \
402 gem_port['discard_config']['min_threshold']
403 gemdict['discard_policy'] = gem_port['discard_policy']
404 gemdict['max_q_size'] = gem_port['max_q_size']
405 gemdict['pbit_map'] = gem_port['pbit_map']
406 gemdict['priority_q'] = gem_port['priority_q']
407 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
408 gemdict['weight'] = gem_port['weight']
409 gemdict['uni_id'] = uni_id
410
411 gem_port = OnuGemPort.create(self, gem_port=gemdict)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530412 new_gem_ports.append(gem_port)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500413
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800414 self._pon.add_gem_port(gem_port, True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500415
416 self.log.debug('pon-add-gemport', gem_port=gem_port)
417
Girish Gowdrae933cd32019-11-21 21:04:41 +0530418 return new_gem_ports
419
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800420 def _execute_queued_vlan_filter_tasks(self, uni_id, tp_id):
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400421 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
422 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
423 # or initial-mib-download is not done. Once the tp_task is completed, we execute
424 # such queued vlan-filter tasks
425 try:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800426 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 -0400427 self.log.info("executing-queued-vlan-filter-task",
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800428 uni_id=uni_id, tp_id=tp_id)
Mahir Gunyela982ec32020-02-25 12:30:37 -0800429 for filter_info in self._queued_vlan_filter_task[uni_id][tp_id]:
430 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800431 uni_id=uni_id, uni_port=filter_info.get("uni_port"),
432 match_vlan=filter_info.get("match_vlan"),
433 _set_vlan_vid=filter_info.get("set_vlan_vid"),
434 _set_vlan_pcp=filter_info.get("set_vlan_pcp"),
435 tp_id=filter_info.get("tp_id"))
Girish Gowdraaf98a082020-03-05 16:40:51 -0800436 # Now remove the entry from the dictionary
437 self._queued_vlan_filter_task[uni_id][tp_id].remove(filter_info)
438 self.log.debug("executed-queued-vlan-filter-task",
439 uni_id=uni_id, tp_id=tp_id)
440 # Now delete the key entries once we have handled the queued vlan filter tasks.
441 del self._queued_vlan_filter_task[uni_id]
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400442 except Exception as e:
443 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
444
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500445 def _do_tech_profile_configuration(self, uni_id, tp):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500446 us_scheduler = tp['us_scheduler']
447 alloc_id = us_scheduler['alloc_id']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800448 new_tconts = self._create_tconts(uni_id, us_scheduler)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500449 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800450 new_upstream_gems = self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500451 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800452 new_downstream_gems = self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
453
454 new_gems = []
455 new_gems.extend(new_upstream_gems)
456 new_gems.extend(new_downstream_gems)
457
458 return new_tconts, new_gems
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500459
460 def load_and_configure_tech_profile(self, uni_id, tp_path):
461 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
Mahir Gunyele9110a32020-02-20 14:56:50 -0800462 tp_id = self.extract_tp_id_from_path(tp_path)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500463 if uni_id not in self._tp_service_specific_task:
464 self._tp_service_specific_task[uni_id] = dict()
465
466 if uni_id not in self._tech_profile_download_done:
467 self._tech_profile_download_done[uni_id] = dict()
468
Mahir Gunyele9110a32020-02-20 14:56:50 -0800469 if tp_id not in self._tech_profile_download_done[uni_id]:
470 self._tech_profile_download_done[uni_id][tp_id] = False
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500471
Mahir Gunyele9110a32020-02-20 14:56:50 -0800472 if not self._tech_profile_download_done[uni_id][tp_id]:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500473 try:
474 if tp_path in self._tp_service_specific_task[uni_id]:
475 self.log.info("tech-profile-config-already-in-progress",
Girish Gowdrae933cd32019-11-21 21:04:41 +0530476 tp_path=tp_path)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500477 return
478
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500479 tpstored = self.kv_client[tp_path]
480 tpstring = tpstored.decode('ascii')
481 tp = json.loads(tpstring)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530482
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500483 self.log.debug("tp-instance", tp=tp)
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800484 tconts, gem_ports = self._do_tech_profile_configuration(uni_id, tp)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700485
William Kurkian3a206332019-04-29 11:05:47 -0400486 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500487 def success(_results):
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800488 self.log.info("tech-profile-config-done-successfully", uni_id=uni_id, tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500489 if tp_path in self._tp_service_specific_task[uni_id]:
490 del self._tp_service_specific_task[uni_id][tp_path]
Mahir Gunyele9110a32020-02-20 14:56:50 -0800491 self._tech_profile_download_done[uni_id][tp_id] = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400492 # Now execute any vlan filter tasks that were queued for later
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800493 reactor.callInThread(self._execute_queued_vlan_filter_tasks, uni_id, tp_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500494 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-success')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530495
William Kurkian3a206332019-04-29 11:05:47 -0400496 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500497 def failure(_reason):
Mahir Gunyel5afa9542020-02-23 22:54:04 -0800498 self.log.warn('tech-profile-config-failure-retrying', uni_id=uni_id, tp_id=tp_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +0530499 _reason=_reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500500 if tp_path in self._tp_service_specific_task[uni_id]:
501 del self._tp_service_specific_task[uni_id][tp_path]
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800502 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500503 reactor.callLater(retry, self.load_and_configure_tech_profile,
504 uni_id, tp_path)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500505 yield self.core_proxy.device_reason_update(self.device_id,
506 'tech-profile-config-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500507
Mahir Gunyela982ec32020-02-25 12:30:37 -0800508 self.log.info('downloading-tech-profile-configuration', uni_id=uni_id, tp_id=tp_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530509 self.log.debug("tconts-gems-to-install", tconts=tconts, gem_ports=gem_ports)
510
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500511 self._tp_service_specific_task[uni_id][tp_path] = \
Mahir Gunyele9110a32020-02-20 14:56:50 -0800512 BrcmTpSetupTask(self.omci_agent, self, uni_id, tconts, gem_ports, tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500513 self._deferred = \
Girish Gowdrae933cd32019-11-21 21:04:41 +0530514 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500515 self._deferred.addCallbacks(success, failure)
516
517 except Exception as e:
518 self.log.exception("error-loading-tech-profile", e=e)
519 else:
520 self.log.info("tech-profile-config-already-done")
Girish Gowdrae933cd32019-11-21 21:04:41 +0530521 # Could be a case where TP exists but new gem-ports are getting added dynamically
522 tpstored = self.kv_client[tp_path]
523 tpstring = tpstored.decode('ascii')
524 tp = json.loads(tpstring)
525 upstream_gems = []
526 downstream_gems = []
527 # Find out the new Gem ports that are getting added afresh.
528 for gp in tp['upstream_gem_port_attribute_list']:
529 if self.pon_port.gem_port(gp['gemport_id'], "upstream"):
530 # gem port already exists
531 continue
532 upstream_gems.append(gp)
533 for gp in tp['downstream_gem_port_attribute_list']:
534 if self.pon_port.gem_port(gp['gemport_id'], "downstream"):
535 # gem port already exists
536 continue
537 downstream_gems.append(gp)
538
539 us_scheduler = tp['us_scheduler']
540 alloc_id = us_scheduler['alloc_id']
541
542 if len(upstream_gems) > 0 or len(downstream_gems) > 0:
543 self.log.info("installing-new-gem-ports", upstream_gems=upstream_gems, downstream_gems=downstream_gems)
544 new_upstream_gems = self._create_gemports(uni_id, upstream_gems, alloc_id, "UPSTREAM")
545 new_downstream_gems = self._create_gemports(uni_id, downstream_gems, alloc_id, "DOWNSTREAM")
546 new_gems = []
547 new_gems.extend(new_upstream_gems)
548 new_gems.extend(new_downstream_gems)
549
550 def success(_results):
551 self.log.info("new-gem-ports-successfully-installed", result=_results)
552
553 def failure(_reason):
554 self.log.warn('new-gem-port-install-failed--retrying',
555 _reason=_reason)
556 # Remove gem ports from cache. We will re-add them during the retry
557 for gp in new_gems:
558 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
559
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800560 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500561 reactor.callLater(retry, self.load_and_configure_tech_profile,
562 uni_id, tp_path)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530563
564 self._tp_service_specific_task[uni_id][tp_path] = \
Mahir Gunyele9110a32020-02-20 14:56:50 -0800565 BrcmTpSetupTask(self.omci_agent, self, uni_id, [], new_gems, tp_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530566 self._deferred = \
567 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
568 self._deferred.addCallbacks(success, failure)
569
570 def delete_tech_profile(self, uni_id, tp_path, alloc_id=None, gem_port_id=None):
571 try:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800572 tp_table_id = self.extract_tp_id_from_path(tp_path)
Naga Manjunathe433c712020-01-02 17:27:20 +0530573 if not uni_id in self._tech_profile_download_done:
574 self.log.warn("tp-key-is-not-present", uni_id=uni_id)
575 return
576
Mahir Gunyele9110a32020-02-20 14:56:50 -0800577 if not tp_table_id in self._tech_profile_download_done[uni_id]:
578 self.log.warn("tp-id-is-not-present", uni_id=uni_id, tp_id=tp_table_id)
Naga Manjunathe433c712020-01-02 17:27:20 +0530579 return
580
Mahir Gunyele9110a32020-02-20 14:56:50 -0800581 if self._tech_profile_download_done[uni_id][tp_table_id] is not True:
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800582 self.log.error("tp-download-is-not-done-in-order-to-process-tp-delete", uni_id=uni_id,
583 tp_id=tp_table_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530584 return
585
586 if alloc_id is None and gem_port_id is None:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800587 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 +0530588 return
589
590 # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
591 # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
592 # due to additional tasks on different UNIs. So, it we cannot use the pon_port affter
593 # this initializer
594 tcont = None
595 self.log.debug("tconts", tconts=list(self.pon_port.tconts.values()))
596 for tc in list(self.pon_port.tconts.values()):
597 if tc.alloc_id == alloc_id:
598 tcont = tc
599 self.pon_port.remove_tcont(tc.alloc_id, False)
600
601 gem_port = None
602 self.log.debug("gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
603 for gp in list(self.pon_port.gem_ports.values()):
604 if gp.gem_id == gem_port_id:
605 gem_port = gp
606 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
607
Girish Gowdrae933cd32019-11-21 21:04:41 +0530608 @inlineCallbacks
609 def success(_results):
610 if gem_port_id:
611 self.log.info("gem-port-delete-done-successfully")
612 if alloc_id:
613 self.log.info("tcont-delete-done-successfully")
614 # The deletion of TCONT marks the complete deletion of tech-profile
615 try:
Mahir Gunyele9110a32020-02-20 14:56:50 -0800616 del self._tech_profile_download_done[uni_id][tp_table_id]
Girish Gowdrae933cd32019-11-21 21:04:41 +0530617 del self._tp_service_specific_task[uni_id][tp_path]
618 except Exception as ex:
619 self.log.error("del-tp-state-info", e=ex)
620
621 # TODO: There could be multiple TP on the UNI, and also the ONU.
622 # TODO: But the below reason updates for the whole device.
623 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-delete-success')
624
625 @inlineCallbacks
Girish Gowdraa73ee452019-12-20 18:52:17 +0530626 def failure(_reason):
Girish Gowdrae933cd32019-11-21 21:04:41 +0530627 self.log.warn('tech-profile-delete-failure-retrying',
628 _reason=_reason)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500629 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
630 reactor.callLater(retry, self.delete_tech_profile, uni_id, tp_path, alloc_id, gem_port_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -0500631 yield self.core_proxy.device_reason_update(self.device_id,
632 'tech-profile-config-delete-failure-retrying')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530633
634 self.log.info('deleting-tech-profile-configuration')
635
Girish Gowdraa73ee452019-12-20 18:52:17 +0530636 if tcont is None and gem_port is None:
637 if alloc_id is not None:
638 self.log.error("tcont-info-corresponding-to-alloc-id-not-found", alloc_id=alloc_id)
639 if gem_port_id is not None:
640 self.log.error("gem-port-info-corresponding-to-gem-port-id-not-found", gem_port_id=gem_port_id)
641 return
642
Girish Gowdrae933cd32019-11-21 21:04:41 +0530643 self._tp_service_specific_task[uni_id][tp_path] = \
644 BrcmTpDeleteTask(self.omci_agent, self, uni_id, tp_table_id,
645 tcont=tcont, gem_port=gem_port)
646 self._deferred = \
647 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
648 self._deferred.addCallbacks(success, failure)
649 except Exception as e:
650 self.log.exception("failed-to-delete-tp",
651 e=e, uni_id=uni_id, tp_path=tp_path,
652 alloc_id=alloc_id, gem_port_id=gem_port_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500653
654 def update_pm_config(self, device, pm_config):
655 # TODO: This has not been tested
656 self.log.info('update_pm_config', pm_config=pm_config)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500657 self._pm_metrics.update(pm_config)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500658
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800659 def remove_onu_flows(self, device, flows):
660 self.log.debug('remove_onu_flows', device_id=device.id)
661
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800662 # no point in removing omci flows if the device isnt reachable
663 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800664 device.admin_state != AdminState.ENABLED:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800665 self.log.warn("device-disabled-or-offline-skipping-remove-flow",
666 admin=device.admin_state, connect=device.connect_status)
667 return
668
669 for flow in flows:
670 # if incoming flow contains cookie, then remove from ONU
671 if flow.cookie:
672 self.log.debug("remove-flow", device_id=device.id, flow=flow)
673
674 def is_downstream(port):
675 return port == self._pon_port_number
676
677 def is_upstream(port):
678 return not is_downstream(port)
679
680 try:
681 _in_port = fd.get_in_port(flow)
682 assert _in_port is not None
683
684 _out_port = fd.get_out_port(flow) # may be None
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800685
686 if is_downstream(_in_port):
687 self.log.debug('downstream-flow-no-need-to-remove', in_port=_in_port, out_port=_out_port,
688 device_id=device.id)
689 # extended vlan tagging operation will handle it
690 continue
691 elif is_upstream(_in_port):
692 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
693 if fd.is_dhcp_flow(flow):
694 self.log.debug('The dhcp trap-to-host flow will be discarded', device_id=device.id)
695 return
696
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800697 _vlan_vid = 0
698 # Retrieve the VLAN_VID that needs to be removed from the EVTO rule on the ONU.
699 for action in fd.get_actions(flow):
700 if action.type == fd.SET_FIELD:
701 _field = action.set_field.field.ofb_field
702 assert (action.set_field.field.oxm_class ==
703 OFPXMC_OPENFLOW_BASIC)
704 if _field.type == fd.VLAN_VID:
705 _vlan_vid = _field.vlan_vid & 0xfff
706 self.log.debug('vlan-vid-to-remove',
707 _vlan_vid=_vlan_vid, in_port=_in_port)
708
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800709 uni_port = self.uni_port(_in_port)
710 uni_id = _in_port & 0xF
711 else:
712 raise Exception('port should be 1 or 2 by our convention')
713
714 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
715
716 tp_id = self.get_tp_id_in_flow(flow)
717 # Deleting flow from ONU.
718 self._remove_vlan_filter_task(device, uni_id, uni_port=uni_port, _set_vlan_vid=_vlan_vid,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800719 match_vlan=_vlan_vid, tp_id=tp_id)
720 # TODO:Delete TD task.
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800721 except Exception as e:
722 self.log.exception('failed-to-remove-flow', e=e)
723
724 def add_onu_flows(self, device, flows):
725 self.log.debug('function-entry', flows=flows)
726
727 #
728 # We need to proxy through the OLT to get to the ONU
729 # Configuration from here should be using OMCI
730 #
731 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
732
733 # no point in pushing omci flows if the device isnt reachable
734 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800735 device.admin_state != AdminState.ENABLED:
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800736 self.log.warn("device-disabled-or-offline-skipping-flow-update",
737 admin=device.admin_state, connect=device.connect_status)
738 return
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800739
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800740 def is_downstream(port):
741 return port == self._pon_port_number
742
743 def is_upstream(port):
744 return not is_downstream(port)
745
746 for flow in flows:
747 # if incoming flow contains cookie, then add to ONU
748 if flow.cookie:
749 _type = None
750 _port = None
751 _vlan_vid = None
752 _udp_dst = None
753 _udp_src = None
754 _ipv4_dst = None
755 _ipv4_src = None
756 _metadata = None
757 _output = None
758 _push_tpid = None
759 _field = None
760 _set_vlan_vid = None
761 _set_vlan_pcp = 0
762 _tunnel_id = None
763 self.log.debug("add-flow", device_id=device.id, flow=flow)
764
765 try:
766 _in_port = fd.get_in_port(flow)
767 assert _in_port is not None
768
769 _out_port = fd.get_out_port(flow) # may be None
770 tp_id = self.get_tp_id_in_flow(flow)
771 if is_downstream(_in_port):
772 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
773 # NOTE: We don't care downstream flow because we will copy vlan_id to upstream flow
774 # uni_port = self.uni_port(_out_port)
775 # uni_id = _out_port & 0xF
776 continue
777 elif is_upstream(_in_port):
778 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
779 uni_port = self.uni_port(_in_port)
780 uni_id = _in_port & 0xF
781 else:
782 raise Exception('port should be 1 or 2 by our convention')
783
784 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
785
786 for field in fd.get_ofb_fields(flow):
787 if field.type == fd.ETH_TYPE:
788 _type = field.eth_type
789 self.log.debug('field-type-eth-type',
790 eth_type=_type)
791
792 elif field.type == fd.IP_PROTO:
793 _proto = field.ip_proto
794 self.log.debug('field-type-ip-proto',
795 ip_proto=_proto)
796
797 elif field.type == fd.IN_PORT:
798 _port = field.port
799 self.log.debug('field-type-in-port',
800 in_port=_port)
801 elif field.type == fd.TUNNEL_ID:
802 self.log.debug('field-type-tunnel-id')
803
804 elif field.type == fd.VLAN_VID:
Andrea Campanellacf916ea2020-02-14 10:03:58 +0100805 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
806 _vlan_vid = RESERVED_TRANSPARENT_VLAN
807 else:
808 _vlan_vid = field.vlan_vid & 0xfff
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800809 self.log.debug('field-type-vlan-vid',
810 vlan=_vlan_vid)
811
812 elif field.type == fd.VLAN_PCP:
813 _vlan_pcp = field.vlan_pcp
814 self.log.debug('field-type-vlan-pcp',
815 pcp=_vlan_pcp)
816
817 elif field.type == fd.UDP_DST:
818 _udp_dst = field.udp_dst
819 self.log.debug('field-type-udp-dst',
820 udp_dst=_udp_dst)
821
822 elif field.type == fd.UDP_SRC:
823 _udp_src = field.udp_src
824 self.log.debug('field-type-udp-src',
825 udp_src=_udp_src)
826
827 elif field.type == fd.IPV4_DST:
828 _ipv4_dst = field.ipv4_dst
829 self.log.debug('field-type-ipv4-dst',
830 ipv4_dst=_ipv4_dst)
831
832 elif field.type == fd.IPV4_SRC:
833 _ipv4_src = field.ipv4_src
834 self.log.debug('field-type-ipv4-src',
835 ipv4_dst=_ipv4_src)
836
837 elif field.type == fd.METADATA:
838 _metadata = field.table_metadata
839 self.log.debug('field-type-metadata',
840 metadata=_metadata)
841
842 else:
843 raise NotImplementedError('field.type={}'.format(
844 field.type))
845
846 for action in fd.get_actions(flow):
847
848 if action.type == fd.OUTPUT:
849 _output = action.output.port
850 self.log.debug('action-type-output',
851 output=_output, in_port=_in_port)
852
853 elif action.type == fd.POP_VLAN:
854 self.log.debug('action-type-pop-vlan',
855 in_port=_in_port)
856
857 elif action.type == fd.PUSH_VLAN:
858 _push_tpid = action.push.ethertype
859 self.log.debug('action-type-push-vlan',
860 push_tpid=_push_tpid, in_port=_in_port)
861 if action.push.ethertype != 0x8100:
862 self.log.error('unhandled-tpid',
863 ethertype=action.push.ethertype)
864
865 elif action.type == fd.SET_FIELD:
866 _field = action.set_field.field.ofb_field
867 assert (action.set_field.field.oxm_class ==
868 OFPXMC_OPENFLOW_BASIC)
869 self.log.debug('action-type-set-field',
870 field=_field, in_port=_in_port)
871 if _field.type == fd.VLAN_VID:
872 _set_vlan_vid = _field.vlan_vid & 0xfff
873 self.log.debug('set-field-type-vlan-vid',
874 vlan_vid=_set_vlan_vid)
875 elif _field.type == fd.VLAN_PCP:
876 _set_vlan_pcp = _field.vlan_pcp
877 self.log.debug('set-field-type-vlan-pcp',
878 vlan_pcp=_set_vlan_pcp)
879 else:
880 self.log.error('unsupported-action-set-field-type',
881 field_type=_field.type)
882 else:
883 self.log.error('unsupported-action-type',
884 action_type=action.type, in_port=_in_port)
885
Andrea Campanellacf916ea2020-02-14 10:03:58 +0100886 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
887 # supported match and action criteria cannot be handled by omci and must be ignored.
888 if (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid != RESERVED_TRANSPARENT_VLAN:
889 self.log.warn('ignoring-flow-that-does-not-set-vlanid', set_vlan_vid=_set_vlan_vid)
890 elif (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid == RESERVED_TRANSPARENT_VLAN:
891 self.log.info('set-vlanid-any', uni_id=uni_id, uni_port=uni_port,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800892 _set_vlan_vid=_vlan_vid,
893 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
894 tp_id=tp_id)
Andrea Campanellacf916ea2020-02-14 10:03:58 +0100895 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
896 _set_vlan_vid=_vlan_vid,
897 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
898 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800899 else:
Andrea Campanellacf916ea2020-02-14 10:03:58 +0100900 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, match_vlan=_vlan_vid,
901 set_vlan_vid=_set_vlan_vid, _set_vlan_pcp=_set_vlan_pcp, ethType=_type)
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800902 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
903 _set_vlan_vid=_set_vlan_vid,
904 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
905 tp_id=tp_id)
906
907 except Exception as e:
908 self.log.exception('failed-to-install-flow', e=e, flow=flow)
909
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500910 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
911 # flow decomposition that ultimately comes from onos
912 def update_flow_table(self, device, flows):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700913 self.log.debug('update-flow-table', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500914
915 #
916 # We need to proxy through the OLT to get to the ONU
917 # Configuration from here should be using OMCI
918 #
919 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
920
921 # no point in pushing omci flows if the device isnt reachable
922 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdrae933cd32019-11-21 21:04:41 +0530923 device.admin_state != AdminState.ENABLED:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500924 self.log.warn("device-disabled-or-offline-skipping-flow-update",
925 admin=device.admin_state, connect=device.connect_status)
926 return
927
928 def is_downstream(port):
929 return port == self._pon_port_number
930
931 def is_upstream(port):
932 return not is_downstream(port)
933
934 for flow in flows:
935 _type = None
936 _port = None
937 _vlan_vid = None
938 _udp_dst = None
939 _udp_src = None
940 _ipv4_dst = None
941 _ipv4_src = None
942 _metadata = None
943 _output = None
944 _push_tpid = None
945 _field = None
946 _set_vlan_vid = None
Mahir Gunyeld680cb62020-02-18 10:28:12 -0800947 _set_vlan_pcp = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400948 _tunnel_id = None
949
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500950 try:
Girish Gowdraa73ee452019-12-20 18:52:17 +0530951 write_metadata = fd.get_write_metadata(flow)
952 if write_metadata is None:
953 self.log.error("do-not-process-flow-without-write-metadata")
954 return
955
956 # extract tp id from flow
Girish Gowdra4c11ddb2020-03-03 11:33:24 -0800957 tp_id = self.get_tp_id_in_flow(flow)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500958 self.log.debug("tp-id-in-flow", tp_id=tp_id)
Girish Gowdraa73ee452019-12-20 18:52:17 +0530959
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500960 _in_port = fd.get_in_port(flow)
961 assert _in_port is not None
962
963 _out_port = fd.get_out_port(flow) # may be None
964
965 if is_downstream(_in_port):
966 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
967 uni_port = self.uni_port(_out_port)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530968 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500969 elif is_upstream(_in_port):
970 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
971 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400972 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500973 else:
974 raise Exception('port should be 1 or 2 by our convention')
975
976 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
977
978 for field in fd.get_ofb_fields(flow):
979 if field.type == fd.ETH_TYPE:
980 _type = field.eth_type
981 self.log.debug('field-type-eth-type',
982 eth_type=_type)
983
984 elif field.type == fd.IP_PROTO:
985 _proto = field.ip_proto
986 self.log.debug('field-type-ip-proto',
987 ip_proto=_proto)
988
989 elif field.type == fd.IN_PORT:
990 _port = field.port
991 self.log.debug('field-type-in-port',
992 in_port=_port)
993
994 elif field.type == fd.VLAN_VID:
Andrea Campanellacf916ea2020-02-14 10:03:58 +0100995 if field.vlan_vid == RESERVED_TRANSPARENT_VLAN and field.vlan_vid_mask == RESERVED_TRANSPARENT_VLAN:
996 _vlan_vid = RESERVED_TRANSPARENT_VLAN
997 else:
998 _vlan_vid = field.vlan_vid & 0xfff
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500999 self.log.debug('field-type-vlan-vid',
1000 vlan=_vlan_vid)
1001
1002 elif field.type == fd.VLAN_PCP:
1003 _vlan_pcp = field.vlan_pcp
1004 self.log.debug('field-type-vlan-pcp',
1005 pcp=_vlan_pcp)
1006
1007 elif field.type == fd.UDP_DST:
1008 _udp_dst = field.udp_dst
1009 self.log.debug('field-type-udp-dst',
1010 udp_dst=_udp_dst)
1011
1012 elif field.type == fd.UDP_SRC:
1013 _udp_src = field.udp_src
1014 self.log.debug('field-type-udp-src',
1015 udp_src=_udp_src)
1016
1017 elif field.type == fd.IPV4_DST:
1018 _ipv4_dst = field.ipv4_dst
1019 self.log.debug('field-type-ipv4-dst',
1020 ipv4_dst=_ipv4_dst)
1021
1022 elif field.type == fd.IPV4_SRC:
1023 _ipv4_src = field.ipv4_src
1024 self.log.debug('field-type-ipv4-src',
1025 ipv4_dst=_ipv4_src)
1026
1027 elif field.type == fd.METADATA:
1028 _metadata = field.table_metadata
1029 self.log.debug('field-type-metadata',
1030 metadata=_metadata)
1031
Matt Jeanneretef06d0d2019-04-27 17:36:53 -04001032 elif field.type == fd.TUNNEL_ID:
1033 _tunnel_id = field.tunnel_id
1034 self.log.debug('field-type-tunnel-id',
1035 tunnel_id=_tunnel_id)
1036
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001037
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001038 else:
1039 raise NotImplementedError('field.type={}'.format(
1040 field.type))
1041
1042 for action in fd.get_actions(flow):
1043
1044 if action.type == fd.OUTPUT:
1045 _output = action.output.port
1046 self.log.debug('action-type-output',
1047 output=_output, in_port=_in_port)
1048
1049 elif action.type == fd.POP_VLAN:
1050 self.log.debug('action-type-pop-vlan',
1051 in_port=_in_port)
1052
1053 elif action.type == fd.PUSH_VLAN:
1054 _push_tpid = action.push.ethertype
1055 self.log.debug('action-type-push-vlan',
1056 push_tpid=_push_tpid, in_port=_in_port)
1057 if action.push.ethertype != 0x8100:
1058 self.log.error('unhandled-tpid',
1059 ethertype=action.push.ethertype)
1060
1061 elif action.type == fd.SET_FIELD:
1062 _field = action.set_field.field.ofb_field
1063 assert (action.set_field.field.oxm_class ==
1064 OFPXMC_OPENFLOW_BASIC)
1065 self.log.debug('action-type-set-field',
1066 field=_field, in_port=_in_port)
1067 if _field.type == fd.VLAN_VID:
1068 _set_vlan_vid = _field.vlan_vid & 0xfff
1069 self.log.debug('set-field-type-vlan-vid',
1070 vlan_vid=_set_vlan_vid)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001071 elif _field.type == fd.VLAN_PCP:
1072 _set_vlan_pcp = _field.vlan_pcp
1073 self.log.debug('set-field-type-vlan-pcp',
1074 vlan_pcp=_set_vlan_pcp)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001075 else:
1076 self.log.error('unsupported-action-set-field-type',
1077 field_type=_field.type)
1078 else:
1079 self.log.error('unsupported-action-type',
1080 action_type=action.type, in_port=_in_port)
1081
Matt Jeanneret810148b2019-09-29 12:44:01 -04001082 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
1083 # supported match and action criteria cannot be handled by omci and must be ignored.
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001084 if (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid != RESERVED_TRANSPARENT_VLAN:
1085 self.log.warn('ignoring-flow-that-does-not-set-vlanid', set_vlan_vid=_set_vlan_vid)
1086 elif (_set_vlan_vid is None or _set_vlan_vid == 0) and _vlan_vid == RESERVED_TRANSPARENT_VLAN:
1087 self.log.info('set-vlanid-any', uni_id=uni_id, uni_port=uni_port,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001088 _set_vlan_vid=_vlan_vid,
1089 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1090 tp_id=tp_id)
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001091 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1092 _set_vlan_vid=_vlan_vid,
1093 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1094 tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001095 else:
Andrea Campanellacf916ea2020-02-14 10:03:58 +01001096 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, match_vlan=_vlan_vid,
1097 set_vlan_vid=_set_vlan_vid, _set_vlan_pcp=_set_vlan_pcp, ethType=_type)
1098 self._add_vlan_filter_task(device, uni_id=uni_id, uni_port=uni_port,
1099 _set_vlan_vid=_set_vlan_vid,
1100 _set_vlan_pcp=_set_vlan_pcp, match_vlan=_vlan_vid,
1101 tp_id=tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001102 except Exception as e:
1103 self.log.exception('failed-to-install-flow', e=e, flow=flow)
1104
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001105 def _add_vlan_filter_task(self, device, uni_id, uni_port=None, match_vlan=0,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001106 _set_vlan_vid=None, _set_vlan_pcp=8, tp_id=0):
1107 self.log.info('_adding_vlan_filter_task', uni_port=uni_port, uni_id=uni_id, tp_id=tp_id, match_vlan=match_vlan,
1108 vlan=_set_vlan_vid, vlan_pcp=_set_vlan_pcp)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001109 assert uni_port is not None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001110 if uni_id in self._tech_profile_download_done and tp_id in self._tech_profile_download_done[uni_id] and \
1111 self._tech_profile_download_done[uni_id][tp_id] is True:
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001112 @inlineCallbacks
1113 def success(_results):
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001114 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid, tp_id=tp_id,
1115 set_vlan_pcp=_set_vlan_pcp)
Matt Jeanneretd84c9072020-01-31 06:33:27 -05001116 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-pushed')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001117
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001118 @inlineCallbacks
1119 def failure(_reason):
Girish Gowdraa73ee452019-12-20 18:52:17 +05301120 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 -08001121 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1122 reactor.callLater(retry,
1123 self._add_vlan_filter_task, device, uni_id, uni_port=uni_port,
1124 match_vlan=match_vlan, _set_vlan_vid=_set_vlan_vid,
1125 _set_vlan_pcp=_set_vlan_pcp, tp_id=tp_id)
Matt Jeanneretd84c9072020-01-31 06:33:27 -05001126 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-failed-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001127
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001128 self.log.info('setting-vlan-tag', uni_port=uni_port, uni_id=uni_id, tp_id=tp_id, match_vlan=match_vlan,
1129 vlan=_set_vlan_vid, vlan_pcp=_set_vlan_pcp)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001130 vlan_filter_add_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001131 match_vlan, _set_vlan_pcp, add_tag=True,
1132 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001133 self._deferred = self._onu_omci_device.task_runner.queue_task(vlan_filter_add_task)
Chaitrashree G S8fb96782019-08-19 00:10:49 -04001134 self._deferred.addCallbacks(success, failure)
1135 else:
1136 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001137 uni_id=uni_id, tp_id=tp_id)
1138 if uni_id not in self._queued_vlan_filter_task:
1139 self._queued_vlan_filter_task[uni_id] = dict()
Mahir Gunyela982ec32020-02-25 12:30:37 -08001140 if tp_id not in self._queued_vlan_filter_task[uni_id]:
1141 self._queued_vlan_filter_task[uni_id][tp_id] = []
1142 self._queued_vlan_filter_task[uni_id][tp_id].append({"device": device,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001143 "uni_id": uni_id,
1144 "uni_port": uni_port,
1145 "match_vlan": match_vlan,
1146 "set_vlan_vid": _set_vlan_vid,
1147 "set_vlan_pcp": _set_vlan_pcp,
1148 "tp_id": tp_id})
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001149
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001150 def get_tp_id_in_flow(self, flow):
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001151 flow_metadata = fd.get_metadata_from_write_metadata(flow)
1152 tp_id = fd.get_tp_id_from_metadata(flow_metadata)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001153 return tp_id
1154
1155 def _remove_vlan_filter_task(self, device, uni_id, uni_port=None, match_vlan=0,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001156 _set_vlan_vid=None, _set_vlan_pcp=8, tp_id=0):
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001157 assert uni_port is not None
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001158
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001159 @inlineCallbacks
1160 def success(_results):
1161 self.log.info('vlan-untagging-success', _results=_results)
1162 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-deleted')
1163
1164 @inlineCallbacks
1165 def failure(_reason):
1166 self.log.warn('vlan-untagging-failure', _reason=_reason)
1167 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-deletion-failed-retrying')
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001168 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001169 reactor.callLater(retry,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001170 self._remove_vlan_filter_task, device, uni_id,
1171 add_tag=False, uni_port=uni_port)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001172
1173 self.log.info("remove_vlan_filter_task", tp_id=tp_id)
1174 vlan_remove_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001175 match_vlan, _set_vlan_pcp, add_tag=False,
1176 tp_id=tp_id)
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001177 self._deferred = self._onu_omci_device.task_runner.queue_task(vlan_remove_task)
1178 self._deferred.addCallbacks(success, failure)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001179
Matt Jeannereta32441c2019-03-07 05:16:37 -05001180 def process_inter_adapter_message(self, request):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001181 self.log.debug('process-inter-adapter-message', type=request.header.type, from_topic=request.header.from_topic,
1182 to_topic=request.header.to_topic, to_device_id=request.header.to_device_id)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001183 try:
1184 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
1185 omci_msg = InterAdapterOmciMessage()
1186 request.body.Unpack(omci_msg)
Matteo Scandolod8d73172019-11-26 12:15:15 -07001187 self.log.debug('inter-adapter-recv-omci', omci_msg=hexify(omci_msg.message))
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001188
Matt Jeannereta32441c2019-03-07 05:16:37 -05001189 self.receive_message(omci_msg.message)
1190
1191 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
1192 onu_indication = OnuIndication()
1193 request.body.Unpack(onu_indication)
Matteo Scandolod8d73172019-11-26 12:15:15 -07001194 self.log.debug('inter-adapter-recv-onu-ind', onu_id=onu_indication.onu_id,
1195 oper_state=onu_indication.oper_state, admin_state=onu_indication.admin_state,
1196 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001197
1198 if onu_indication.oper_state == "up":
1199 self.create_interface(onu_indication)
Girish Gowdrae933cd32019-11-21 21:04:41 +05301200 elif onu_indication.oper_state == "down" or onu_indication.oper_state == "unreachable":
Matt Jeannereta32441c2019-03-07 05:16:37 -05001201 self.update_interface(onu_indication)
1202 else:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001203 self.log.error("unknown-onu-indication", onu_id=onu_indication.onu_id,
1204 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001205
Matt Jeanneret3bfebff2019-04-12 18:25:03 -04001206 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
1207 tech_msg = InterAdapterTechProfileDownloadMessage()
1208 request.body.Unpack(tech_msg)
1209 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
1210
1211 self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
1212
Girish Gowdrae933cd32019-11-21 21:04:41 +05301213 elif request.header.type == InterAdapterMessageType.DELETE_GEM_PORT_REQUEST:
1214 del_gem_msg = InterAdapterDeleteGemPortMessage()
1215 request.body.Unpack(del_gem_msg)
1216 self.log.debug('inter-adapter-recv-del-gem', gem_del_msg=del_gem_msg)
1217
1218 self.delete_tech_profile(uni_id=del_gem_msg.uni_id,
1219 gem_port_id=del_gem_msg.gem_port_id,
1220 tp_path=del_gem_msg.tp_path)
1221
1222 elif request.header.type == InterAdapterMessageType.DELETE_TCONT_REQUEST:
1223 del_tcont_msg = InterAdapterDeleteTcontMessage()
1224 request.body.Unpack(del_tcont_msg)
1225 self.log.debug('inter-adapter-recv-del-tcont', del_tcont_msg=del_tcont_msg)
1226
1227 self.delete_tech_profile(uni_id=del_tcont_msg.uni_id,
1228 alloc_id=del_tcont_msg.alloc_id,
1229 tp_path=del_tcont_msg.tp_path)
Matt Jeannereta32441c2019-03-07 05:16:37 -05001230 else:
1231 self.log.error("inter-adapter-unhandled-type", request=request)
1232
1233 except Exception as e:
1234 self.log.exception("error-processing-inter-adapter-message", e=e)
1235
1236 # Called each time there is an onu "up" indication from the olt handler
1237 @inlineCallbacks
1238 def create_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001239 self.log.info('create-interface', onu_id=onu_indication.onu_id,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001240 serial_number=onu_indication.serial_number)
Amit Ghosh028eb202020-02-17 13:34:00 +00001241
1242 # Ignore if onu_indication is received for an already running ONU
1243 if self._onu_omci_device is not None and self._onu_omci_device.active:
1244 self.log.warn('received-onu-indication-for-active-onu', onu_indication=onu_indication)
1245 return
1246
Matt Jeannereta32441c2019-03-07 05:16:37 -05001247 self._onu_indication = onu_indication
1248
Matt Jeanneretc083f462019-03-11 15:02:01 -04001249 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
1250 connect_status=ConnectStatus.REACHABLE)
1251
Matt Jeannereta32441c2019-03-07 05:16:37 -05001252 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001253
1254 self.log.debug('starting-openomci-statemachine')
1255 self._subscribe_to_events()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001256 onu_device.reason = "starting-openomci"
Girish Gowdrae933cd32019-11-21 21:04:41 +05301257 reactor.callLater(1, self._onu_omci_device.start, onu_device)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001258 yield self.core_proxy.device_reason_update(self.device_id, onu_device.reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001259 self._heartbeat.enabled = True
1260
Matt Jeanneret42dad792020-02-01 09:28:27 -05001261 # Called each time there is an onu "down" indication from the olt handler
Matt Jeannereta32441c2019-03-07 05:16:37 -05001262 @inlineCallbacks
1263 def update_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001264 self.log.info('update-interface', onu_id=onu_indication.onu_id,
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001265 serial_number=onu_indication.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001266
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -04001267 if onu_indication.oper_state == 'down' or onu_indication.oper_state == "unreachable":
Mahir Gunyeld680cb62020-02-18 10:28:12 -08001268 self.log.debug('stopping-openomci-statemachine', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001269 reactor.callLater(0, self._onu_omci_device.stop)
1270
1271 # Let TP download happen again
1272 for uni_id in self._tp_service_specific_task:
1273 self._tp_service_specific_task[uni_id].clear()
1274 for uni_id in self._tech_profile_download_done:
1275 self._tech_profile_download_done[uni_id].clear()
1276
Matt Jeanneretf4113222019-08-14 19:44:34 -04001277 yield self.disable_ports(lock_ports=False)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001278 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
1279 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.DISCOVERED,
1280 connect_status=ConnectStatus.UNREACHABLE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001281 else:
1282 self.log.debug('not-changing-openomci-statemachine')
1283
Matt Jeanneretf4113222019-08-14 19:44:34 -04001284 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001285 def disable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001286 self.log.info('disable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001287 try:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001288 yield self.disable_ports(lock_ports=True)
1289 yield self.core_proxy.device_reason_update(self.device_id, "omci-admin-lock")
1290 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001291
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001292 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001293 self.log.exception('exception-in-onu-disable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001294
William Kurkian3a206332019-04-29 11:05:47 -04001295 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001296 def reenable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001297 self.log.info('reenable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001298 try:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001299 yield self.core_proxy.device_state_update(device.id,
1300 oper_status=OperStatus.ACTIVE,
1301 connect_status=ConnectStatus.REACHABLE)
1302 yield self.core_proxy.device_reason_update(self.device_id, 'onu-reenabled')
1303 yield self.enable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001304 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001305 self.log.exception('exception-in-onu-reenable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001306
William Kurkian3a206332019-04-29 11:05:47 -04001307 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001308 def reboot(self):
1309 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -04001310 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001311 if device.connect_status != ConnectStatus.REACHABLE:
1312 self.log.error("device-unreachable")
1313 return
1314
William Kurkian3a206332019-04-29 11:05:47 -04001315 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001316 def success(_results):
1317 self.log.info('reboot-success', _results=_results)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001318 yield self.core_proxy.device_reason_update(self.device_id, 'rebooting')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001319
1320 def failure(_reason):
1321 self.log.info('reboot-failure', _reason=_reason)
1322
1323 self._deferred = self._onu_omci_device.reboot()
1324 self._deferred.addCallbacks(success, failure)
1325
William Kurkian3a206332019-04-29 11:05:47 -04001326 @inlineCallbacks
Matt Jeanneretf4113222019-08-14 19:44:34 -04001327 def disable_ports(self, lock_ports=True):
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001328 self.log.info('disable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001329
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001330 # TODO: for now only support the first UNI given no requirement for multiple uni yet. Also needed to reduce flow
1331 # load on the core
Matt Jeanneretf4113222019-08-14 19:44:34 -04001332 for port in self.uni_ports:
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001333 if port.mac_bridge_port_num == 1:
1334 port.operstatus = OperStatus.UNKNOWN
1335 self.log.info('disable-port', device_id=self.device_id, port=port)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001336 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number,
1337 port.operstatus)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001338
1339 if lock_ports is True:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001340 self.lock_ports(lock=True)
1341
William Kurkian3a206332019-04-29 11:05:47 -04001342 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001343 def enable_ports(self):
1344 self.log.info('enable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001345
Matt Jeanneretf4113222019-08-14 19:44:34 -04001346 self.lock_ports(lock=False)
1347
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001348 # TODO: for now only support the first UNI given no requirement for multiple uni yet. Also needed to reduce flow
1349 # load on the core
1350 # Given by default all unis are initially active according to omci alarming, we must mimic this.
Matt Jeanneretf4113222019-08-14 19:44:34 -04001351 for port in self.uni_ports:
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001352 if port.mac_bridge_port_num == 1:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001353 port.operstatus = OperStatus.ACTIVE
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001354 self.log.info('enable-port', device_id=self.device_id, port=port)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001355 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number,
1356 port.operstatus)
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001357
1358 # TODO: Normally we would want any uni ethernet link down or uni ethernet link up alarms to register in the core,
1359 # but practically olt provisioning cannot handle the churn of links up, down, then up again typical on startup.
1360 #
1361 # Basically the link state sequence:
1362 # 1) per omci default alarm state, all unis are initially up (no link down alarms received yet)
1363 # 2) a link state down alarm is received for all uni, given the lock command, and also because most unis have nothing plugged in
1364 # 3) a link state up alarm is received for the uni plugged in.
1365 #
1366 # Given the olt (BAL) has to provision all uni, de-provision all uni, and re-provision one uni in quick succession
1367 # and cannot (bug?), we have to skip this and leave uni ports as assumed active. Also all the link state activity
1368 # would have a ripple effect through the core to the controller as well. And is it really worth it?
1369 '''
Matt Jeanneretf4113222019-08-14 19:44:34 -04001370 @inlineCallbacks
1371 def port_state_handler(self, _topic, msg):
1372 self.log.info("port-state-change", _topic=_topic, msg=msg)
1373
1374 onu_id = msg['onu_id']
1375 port_no = msg['port_number']
1376 serial_number = msg['serial_number']
1377 port_status = msg['port_status']
1378 uni_port = self.uni_port(int(port_no))
1379
1380 self.log.debug("port-state-parsed-message", onu_id=onu_id, port_no=port_no, serial_number=serial_number,
1381 port_status=port_status)
1382
1383 if port_status is True:
1384 uni_port.operstatus = OperStatus.ACTIVE
1385 self.log.info('link-up', device_id=self.device_id, port=uni_port)
1386 else:
1387 uni_port.operstatus = OperStatus.UNKNOWN
1388 self.log.info('link-down', device_id=self.device_id, port=uni_port)
1389
1390 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 -05001391 '''
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001392
1393 # Called just before openomci state machine is started. These listen for events from selected state machines,
1394 # most importantly, mib in sync. Which ultimately leads to downloading the mib
1395 def _subscribe_to_events(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001396 self.log.debug('subscribe-to-events')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001397
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001398 bus = self._onu_omci_device.event_bus
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001399
1400 # OMCI MIB Database sync status
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001401 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1402 OnuDeviceEvents.MibDatabaseSyncEvent)
1403 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
1404
1405 # OMCI Capabilities
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001406 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1407 OnuDeviceEvents.OmciCapabilitiesEvent)
1408 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
1409
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001410 # TODO: these alarms seem to be unreliable depending on the environment
1411 # 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 -08001412 # topic = OnuDeviceEntry.event_bus_topic(self.device_id,
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001413 # OnuDeviceEvents.PortEvent)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001414 # self._port_state_subscription = bus.subscribe(topic, self.port_state_handler)
Matt Jeanneretfc6cdef2020-02-14 10:14:36 -05001415
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001416 # Called when the mib is in sync
1417 def in_sync_handler(self, _topic, msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001418 self.log.debug('in-sync-handler', _topic=_topic, msg=msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001419 if self._in_sync_subscription is not None:
1420 try:
1421 in_sync = msg[IN_SYNC_KEY]
1422
1423 if in_sync:
1424 # Only call this once
1425 bus = self._onu_omci_device.event_bus
1426 bus.unsubscribe(self._in_sync_subscription)
1427 self._in_sync_subscription = None
1428
1429 # Start up device_info load
1430 self.log.debug('running-mib-sync')
1431 reactor.callLater(0, self._mib_in_sync)
1432
1433 except Exception as e:
1434 self.log.exception('in-sync', e=e)
1435
1436 def capabilties_handler(self, _topic, _msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001437 self.log.debug('capabilities-handler', _topic=_topic, msg=_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001438 if self._capabilities_subscription is not None:
1439 self.log.debug('capabilities-handler-done')
1440
1441 # Mib is in sync, we can now query what we learned and actually start pushing ME (download) to the ONU.
1442 # Currently uses a basic mib download task that create a bridge with a single gem port and uni, only allowing EAP
1443 # Implement your own MibDownloadTask if you wish to setup something different by default
Matt Jeanneretc083f462019-03-11 15:02:01 -04001444 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001445 def _mib_in_sync(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001446 self.log.debug('mib-in-sync')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001447
1448 omci = self._onu_omci_device
1449 in_sync = omci.mib_db_in_sync
1450
Matt Jeanneretc083f462019-03-11 15:02:01 -04001451 device = yield self.core_proxy.get_device(self.device_id)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001452 yield self.core_proxy.device_reason_update(self.device_id, 'discovery-mibsync-complete')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001453
1454 if not self._dev_info_loaded:
1455 self.log.info('loading-device-data-from-mib', in_sync=in_sync, already_loaded=self._dev_info_loaded)
1456
1457 omci_dev = self._onu_omci_device
1458 config = omci_dev.configuration
1459
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001460 try:
1461
1462 # sort the lists so we get consistent port ordering.
1463 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
1464 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
1465 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
1466 veip_list = sorted(config.veip_entities) if config.veip_entities else []
1467
1468 if ani_list is None or (pptp_list is None and veip_list is None):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001469 self.log.warn("no-ani-or-unis")
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001470 yield self.core_proxy.device_reason_update(self.device_id, 'onu-missing-required-elements')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001471 raise Exception("onu-missing-required-elements")
1472
1473 # Currently logging the ani, pptp, veip, and uni for information purposes.
1474 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
1475 # And in some ONU the UNI-G list is incomplete or incorrect...
1476 for entity_id in ani_list:
1477 ani_value = config.ani_g_entities[entity_id]
1478 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
1479 # TODO: currently only one OLT PON port/ANI, so this works out. With NGPON there will be 2..?
1480 self._total_tcont_count = ani_value.get('total-tcont-count')
1481 self.log.debug("set-total-tcont-count", tcont_count=self._total_tcont_count)
1482
1483 for entity_id in uni_list:
1484 uni_value = config.uni_g_entities[entity_id]
1485 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
1486
1487 uni_entities = OrderedDict()
1488 for entity_id in pptp_list:
1489 pptp_value = config.pptp_entities[entity_id]
1490 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
1491 uni_entities[entity_id] = UniType.PPTP
1492
1493 for entity_id in veip_list:
1494 veip_value = config.veip_entities[entity_id]
1495 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
1496 uni_entities[entity_id] = UniType.VEIP
1497
1498 uni_id = 0
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -05001499 for entity_id, uni_type in uni_entities.items():
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001500 try:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001501 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001502 uni_id += 1
1503 except AssertionError as e:
1504 self.log.warn("could not add UNI", entity_id=entity_id, uni_type=uni_type, e=e)
1505
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001506 self._qos_flexibility = config.qos_configuration_flexibility or 0
1507 self._omcc_version = config.omcc_version or OMCCVersion.Unknown
1508
1509 if self._unis:
1510 self._dev_info_loaded = True
1511 else:
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001512 yield self.core_proxy.device_reason_update(self.device_id, 'no-usable-unis')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001513 self.log.warn("no-usable-unis")
1514 raise Exception("no-usable-unis")
1515
1516 except Exception as e:
1517 self.log.exception('device-info-load', e=e)
1518 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1519
1520 else:
1521 self.log.info('device-info-already-loaded', in_sync=in_sync, already_loaded=self._dev_info_loaded)
1522
1523 if self._dev_info_loaded:
Matt Jeanneretad9a0f12019-05-09 14:05:49 -04001524 if device.admin_state == AdminState.PREPROVISIONED or device.admin_state == AdminState.ENABLED:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001525
1526 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001527 def success(_results):
1528 self.log.info('mib-download-success', _results=_results)
Matt Jeanneretc083f462019-03-11 15:02:01 -04001529 yield self.core_proxy.device_state_update(device.id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301530 oper_status=OperStatus.ACTIVE,
1531 connect_status=ConnectStatus.REACHABLE)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001532 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-downloaded')
Matt Jeanneretf4113222019-08-14 19:44:34 -04001533 yield self.enable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001534 self._mib_download_task = None
Devmalya Paulffc89df2019-07-31 17:43:13 -04001535 yield self.onu_active_event()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001536
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -05001537 # Start collecting stats from the device after a brief pause
1538 if not self._pm_metrics_started:
1539 self._pm_metrics_started = True
1540 pmstart = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1541 reactor.callLater(pmstart, self._pm_metrics.start_collector)
1542
1543 # Start test requests after a brief pause
1544 if not self._test_request_started:
1545 self._test_request_started = True
1546 tststart = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1547 reactor.callLater(tststart, self._test_request.start_collector)
1548
Matt Jeanneretc083f462019-03-11 15:02:01 -04001549 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001550 def failure(_reason):
1551 self.log.warn('mib-download-failure-retrying', _reason=_reason)
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001552 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -05001553 reactor.callLater(retry, self._mib_in_sync)
Matt Jeanneretd84c9072020-01-31 06:33:27 -05001554 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001555
Matt Jeanneretf4113222019-08-14 19:44:34 -04001556 # start by locking all the unis till mib sync and initial mib is downloaded
1557 # this way we can capture the port down/up events when we are ready
1558 self.lock_ports(lock=True)
1559
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001560 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1561 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1562 self.log.info('downloading-initial-mib-configuration')
1563 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1564 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1565 self._deferred.addCallbacks(success, failure)
1566 else:
1567 self.log.info('admin-down-disabling')
1568 self.disable(device)
1569 else:
1570 self.log.info('device-info-not-loaded-skipping-mib-download')
1571
Matt Jeanneretc083f462019-03-11 15:02:01 -04001572 @inlineCallbacks
1573 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001574 self.log.debug('add-uni-port')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001575
Matt Jeanneretc083f462019-03-11 15:02:01 -04001576 uni_no = self.mk_uni_port_num(self._onu_indication.intf_id, self._onu_indication.onu_id, uni_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001577
1578 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1579 uni_name = "uni-{}".format(uni_no)
1580
Girish Gowdrae933cd32019-11-21 21:04:41 +05301581 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001582
1583 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 -04001584 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001585
1586 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1587 uni_port.entity_id = entity_id
1588 uni_port.enabled = True
1589 uni_port.mac_bridge_port_num = mac_bridge_port_num
1590
1591 self.log.debug("created-uni-port", uni=uni_port)
1592
Matt Jeanneretc083f462019-03-11 15:02:01 -04001593 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001594
1595 self._unis[uni_port.port_number] = uni_port
1596
1597 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=self._onu_indication.onu_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301598 uni_ports=self.uni_ports,
1599 serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001600
Matt Jeanneretc083f462019-03-11 15:02:01 -04001601 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1602 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
Amit Ghosh65400f12019-11-21 12:04:12 +00001603 MAX_PONS_PER_OLT = 256
1604 MAX_ONUS_PER_PON = 256
Matt Jeanneretc083f462019-03-11 15:02:01 -04001605 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001606
Matt Jeanneretc083f462019-03-11 15:02:01 -04001607 assert intf_id < MAX_PONS_PER_OLT
1608 assert onu_id < MAX_ONUS_PER_PON
1609 assert uni_id < MAX_UNIS_PER_ONU
Amit Ghosh65400f12019-11-21 12:04:12 +00001610 return intf_id << 12 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001611
1612 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001613 def onu_active_event(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001614 self.log.debug('onu-active-event')
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001615 try:
1616 device = yield self.core_proxy.get_device(self.device_id)
1617 parent_device = yield self.core_proxy.get_device(self.parent_id)
1618 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001619 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001620
1621 self.log.debug("onu-indication-context-data",
Girish Gowdrae933cd32019-11-21 21:04:41 +05301622 pon_id=self._onu_indication.intf_id,
1623 onu_id=self._onu_indication.onu_id,
1624 registration_id=self.device_id,
1625 device_id=self.device_id,
1626 onu_serial_number=device.serial_number,
1627 olt_serial_number=olt_serial_number,
1628 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001629
Devmalya Paulffc89df2019-07-31 17:43:13 -04001630 self.log.debug("Trying-to-raise-onu-active-event")
1631 OnuActiveEvent(self.events, self.device_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001632 self._onu_indication.intf_id,
1633 device.serial_number,
1634 str(self.device_id),
Girish Gowdrae933cd32019-11-21 21:04:41 +05301635 olt_serial_number, raised_ts,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001636 onu_id=self._onu_indication.onu_id).send(True)
1637 except Exception as active_event_error:
1638 self.log.exception('onu-activated-event-error',
1639 errmsg=active_event_error.message)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001640
1641 def lock_ports(self, lock=True):
1642
1643 def success(response):
1644 self.log.debug('set-onu-ports-state', lock=lock, response=response)
1645
1646 def failure(response):
1647 self.log.error('cannot-set-onu-ports-state', lock=lock, response=response)
1648
1649 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=lock)
1650 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
1651 self._deferred.addCallbacks(success, failure)
Mahir Gunyele9110a32020-02-20 14:56:50 -08001652
1653 def extract_tp_id_from_path(self, tp_path):
1654 # tp_path is of the format <technology>/<table_id>/<uni_port_name>
Girish Gowdra4c11ddb2020-03-03 11:33:24 -08001655 tp_id = int(tp_path.split(_PATH_SEPERATOR)[1])
1656 return tp_id