blob: 074f39e1a71ecb2eb6ac2b3ec47f3a31dbec65df [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
48from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, ofp_port, OFPPS_LIVE,OFPPS_LINK_DOWN,\
49 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
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050066from pyvoltha.common.tech_profile.tech_profile import TechProfile
onkarkundargiaae99712019-09-23 15:02:52 +053067from pyvoltha.adapters.extensions.omci.tasks.omci_test_request import OmciTestRequest
68from pyvoltha.adapters.extensions.omci.omci_entities import AniG
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050069from pyvoltha.adapters.extensions.omci.omci_defs import EntityOperations, ReasonCodes
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050070
71OP = EntityOperations
72RC = ReasonCodes
73
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050074_STARTUP_RETRY_WAIT = 10
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050075
76
77class BrcmOpenomciOnuHandler(object):
78
79 def __init__(self, adapter, device_id):
80 self.log = structlog.get_logger(device_id=device_id)
Matt Jeanneret08a8e862019-12-20 14:02:32 -050081 self.log.debug('starting-handler')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050082 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050083 self.core_proxy = adapter.core_proxy
84 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050085 self.parent_adapter = None
86 self.parent_id = None
87 self.device_id = device_id
88 self.incoming_messages = DeferredQueue()
89 self.event_messages = DeferredQueue()
90 self.proxy_address = None
91 self.tx_id = 0
92 self._enabled = False
Devmalya Paulffc89df2019-07-31 17:43:13 -040093 self.events = None
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -050094 self._pm_metrics = None
95 self._pm_metrics_started = False
96 self._test_request = None
97 self._test_request_started = False
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050098 self._omcc_version = OMCCVersion.Unknown
99 self._total_tcont_count = 0 # From ANI-G ME
100 self._qos_flexibility = 0 # From ONT2_G ME
101
102 self._onu_indication = None
103 self._unis = dict() # Port # -> UniPort
104
105 self._pon = None
106 # TODO: probably shouldnt be hardcoded, determine from olt maybe?
107 self._pon_port_number = 100
108 self.logical_device_id = None
109
110 self._heartbeat = HeartBeat.create(self, device_id)
111
112 # Set up OpenOMCI environment
113 self._onu_omci_device = None
114 self._dev_info_loaded = False
115 self._deferred = None
116
117 self._in_sync_subscription = None
Matt Jeanneretf4113222019-08-14 19:44:34 -0400118 self._port_state_subscription = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500119 self._connectivity_subscription = None
120 self._capabilities_subscription = None
121
122 self.mac_bridge_service_profile_entity_id = 0x201
123 self.gal_enet_profile_entity_id = 0x1
124
125 self._tp_service_specific_task = dict()
126 self._tech_profile_download_done = dict()
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400127 # Stores information related to queued vlan filter tasks
128 # Dictionary with key being uni_id and value being device,uni port ,uni id and vlan id
129
130 self._queued_vlan_filter_task = dict()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500131
132 # Initialize KV store client
133 self.args = registry('main').get_args()
134 if self.args.backend == 'etcd':
135 host, port = self.args.etcd.split(':', 1)
136 self.kv_client = EtcdStore(host, port,
137 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
138 elif self.args.backend == 'consul':
139 host, port = self.args.consul.split(':', 1)
140 self.kv_client = ConsulStore(host, port,
141 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
142 else:
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500143 self.log.error('invalid-backend')
144 raise Exception("invalid-backend-for-kv-store")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500145
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500146 @property
147 def enabled(self):
148 return self._enabled
149
150 @enabled.setter
151 def enabled(self, value):
152 if self._enabled != value:
153 self._enabled = value
154
155 @property
156 def omci_agent(self):
157 return self.adapter.omci_agent
158
159 @property
160 def omci_cc(self):
161 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
162
163 @property
164 def heartbeat(self):
165 return self._heartbeat
166
167 @property
168 def uni_ports(self):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500169 return list(self._unis.values())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500170
171 def uni_port(self, port_no_or_name):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500172 if isinstance(port_no_or_name, six.string_types):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500173 return next((uni for uni in self.uni_ports
174 if uni.name == port_no_or_name), None)
175
176 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
177 return next((uni for uni in self.uni_ports
Girish Gowdrae933cd32019-11-21 21:04:41 +0530178 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500179
180 @property
181 def pon_port(self):
182 return self._pon
183
Girish Gowdraa73ee452019-12-20 18:52:17 +0530184 @property
185 def onu_omci_device(self):
186 return self._onu_omci_device
187
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500188 def receive_message(self, msg):
189 if self.omci_cc is not None:
190 self.omci_cc.receive_message(msg)
191
Matt Jeanneretc083f462019-03-11 15:02:01 -0400192 def get_ofp_port_info(self, device, port_no):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500193 self.log.debug('get-ofp-port-info', port_no=port_no, device_id=device.id)
Matt Jeanneretc083f462019-03-11 15:02:01 -0400194 cap = OFPPF_1GB_FD | OFPPF_FIBER
195
Girish Gowdrae933cd32019-11-21 21:04:41 +0530196 hw_addr = mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
197 ((device.parent_port_no >> 8 & 0xff),
198 device.parent_port_no & 0xff,
199 (port_no >> 16) & 0xff,
200 (port_no >> 8) & 0xff,
201 port_no & 0xff))
Matt Jeanneretc083f462019-03-11 15:02:01 -0400202
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400203 uni_port = self.uni_port(int(port_no))
204 name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500205 self.log.debug('ofp-port-name', port_no=port_no, name=name, uni_port=uni_port)
Matt Jeanneretf4113222019-08-14 19:44:34 -0400206
207 ofstate = OFPPS_LINK_DOWN
208 if uni_port.operstatus is OperStatus.ACTIVE:
209 ofstate = OFPPS_LIVE
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400210
Matt Jeanneretc083f462019-03-11 15:02:01 -0400211 return PortCapability(
212 port=LogicalPort(
213 ofp_port=ofp_port(
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400214 name=name,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400215 hw_addr=hw_addr,
216 config=0,
Matt Jeanneretf4113222019-08-14 19:44:34 -0400217 state=ofstate,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400218 curr=cap,
219 advertised=cap,
220 peer=cap,
221 curr_speed=OFPPF_1GB_FD,
222 max_speed=OFPPF_1GB_FD
223 ),
224 device_id=device.id,
225 device_port_no=port_no
226 )
227 )
228
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500229 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500230 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500231 def activate(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700232 self.log.debug('activate-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500233
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500234 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500235 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500236 assert device.proxy_address.device_id
237
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500238 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500239 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500240 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500241 if self.enabled is not True:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700242 self.log.info('activating-new-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500243 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500244 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500245 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500246 device.reason = 'activating-onu'
247
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500248 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400249 # pm_metrics requires a logical device id. For now set to just device_id
250 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500251
Matt Jeannereta32441c2019-03-07 05:16:37 -0500252 yield self.core_proxy.device_update(device)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500253 self.log.debug('device-updated', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500254
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700255 yield self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500256
Matteo Scandolod8d73172019-11-26 12:15:15 -0700257 self.log.debug('pon state initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500258 ############################################################################
Devmalya Paulffc89df2019-07-31 17:43:13 -0400259 # Setup Alarm handler
260 self.events = AdapterEvents(self.core_proxy, device.id, self.logical_device_id,
261 device.serial_number)
262 ############################################################################
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500263 # Setup PM configuration for this device
264 # Pass in ONU specific options
265 kwargs = {
266 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
267 'heartbeat': self.heartbeat,
268 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
269 }
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500270 self.log.debug('create-pm-metrics', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500271 self._pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400272 self.logical_device_id, device.serial_number,
273 grouped=True, freq_override=False, **kwargs)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500274 pm_config = self._pm_metrics.make_proto()
275 self._onu_omci_device.set_pm_config(self._pm_metrics.omci_pm.openomci_interval_pm)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530276 self.log.info("initial-pm-config", device_id=device.id, serial_number=device.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500277 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500278
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500279 # Note, ONU ID and UNI intf set in add_uni_port method
Devmalya Paulffc89df2019-07-31 17:43:13 -0400280 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500281 ani_ports=[self._pon])
aishwaryarana01a98d9fe2019-05-08 12:09:06 -0500282
onkarkundargiaae99712019-09-23 15:02:52 +0530283 # Code to Run OMCI Test Action
284 kwargs_omci_test_action = {
285 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
286 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
287 }
288 serial_number = device.serial_number
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500289 self._test_request = OmciTestRequest(self.core_proxy,
onkarkundargiaae99712019-09-23 15:02:52 +0530290 self.omci_agent, self.device_id,
291 AniG, serial_number,
292 self.logical_device_id,
293 exclusive=False,
294 **kwargs_omci_test_action)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500295
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500296 self.enabled = True
297 else:
298 self.log.info('onu-already-activated')
299
300 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400301 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500302 def reconcile(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700303 self.log.debug('reconcile-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500304
305 # first we verify that we got parent reference and proxy info
306 assert device.parent_id
307 assert device.proxy_address.device_id
308
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700309 self.proxy_address = device.proxy_address
310 self.parent_id = device.parent_id
311 self._pon_port_number = device.parent_port_no
312
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500313 if self.enabled is not True:
314 self.log.info('reconciling-broadcom-onu-device')
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700315 self.logical_device_id = self.device_id
316 self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500317
318 # need to restart state machines on vcore restart. there is no indication to do it for us.
319 self._onu_omci_device.start()
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700320 yield self.core_proxy.device_reason_update(self.device_id, "restarting-openomci")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500321
322 # TODO: this is probably a bit heavy handed
323 # Force a reboot for now. We need indications to reflow to reassign tconts and gems given vcore went away
324 # This may not be necessary when mib resync actually works
325 reactor.callLater(1, self.reboot)
326
327 self.enabled = True
328 else:
329 self.log.info('onu-already-activated')
330
331 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700332 def _init_pon_state(self):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500333 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 -0500334
335 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500336 self._pon.add_peer(self.parent_id, self._pon_port_number)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700337 self.log.debug('adding-pon-port-to-agent',
338 type=self._pon.get_port().type,
339 admin_state=self._pon.get_port().admin_state,
340 oper_status=self._pon.get_port().oper_status,
341 )
Matt Jeanneret0c287892019-02-28 11:48:00 -0500342
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700343 yield self.core_proxy.port_created(self.device_id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500344
Matteo Scandolod8d73172019-11-26 12:15:15 -0700345 self.log.debug('added-pon-port-to-agent',
346 type=self._pon.get_port().type,
347 admin_state=self._pon.get_port().admin_state,
348 oper_status=self._pon.get_port().oper_status,
349 )
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500350
351 # Create and start the OpenOMCI ONU Device Entry for this ONU
352 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500353 self.core_proxy,
354 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500355 support_classes=self.adapter.broadcom_omci,
356 custom_me_map=self.adapter.custom_me_entities())
357 # Port startup
358 if self._pon is not None:
359 self._pon.enabled = True
360
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500361 def delete(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700362 self.log.info('delete-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500363 if self.parent_adapter:
364 try:
365 self.parent_adapter.delete_child_device(self.parent_id, device)
366 except AttributeError:
367 self.log.debug('parent-device-delete-child-not-implemented')
368 else:
369 self.log.debug("parent-adapter-not-available")
370
371 def _create_tconts(self, uni_id, us_scheduler):
372 alloc_id = us_scheduler['alloc_id']
373 q_sched_policy = us_scheduler['q_sched_policy']
374 self.log.debug('create-tcont', us_scheduler=us_scheduler)
375
376 tcontdict = dict()
377 tcontdict['alloc-id'] = alloc_id
378 tcontdict['q_sched_policy'] = q_sched_policy
379 tcontdict['uni_id'] = uni_id
380
Matt Jeanneret3789d0d2020-01-19 09:03:42 -0500381 tcont = OnuTCont.create(self, tcont=tcontdict)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500382
383 self._pon.add_tcont(tcont)
384
385 self.log.debug('pon-add-tcont', tcont=tcont)
386
387 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
388 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
389 self.log.debug('create-gemport',
390 gem_ports=gem_ports, direction=direction)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530391 new_gem_ports = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500392 for gem_port in gem_ports:
393 gemdict = dict()
394 gemdict['gemport_id'] = gem_port['gemport_id']
395 gemdict['direction'] = direction
396 gemdict['alloc_id_ref'] = alloc_id_ref
397 gemdict['encryption'] = gem_port['aes_encryption']
398 gemdict['discard_config'] = dict()
399 gemdict['discard_config']['max_probability'] = \
400 gem_port['discard_config']['max_probability']
401 gemdict['discard_config']['max_threshold'] = \
402 gem_port['discard_config']['max_threshold']
403 gemdict['discard_config']['min_threshold'] = \
404 gem_port['discard_config']['min_threshold']
405 gemdict['discard_policy'] = gem_port['discard_policy']
406 gemdict['max_q_size'] = gem_port['max_q_size']
407 gemdict['pbit_map'] = gem_port['pbit_map']
408 gemdict['priority_q'] = gem_port['priority_q']
409 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
410 gemdict['weight'] = gem_port['weight']
411 gemdict['uni_id'] = uni_id
412
413 gem_port = OnuGemPort.create(self, gem_port=gemdict)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530414 new_gem_ports.append(gem_port)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500415
416 self._pon.add_gem_port(gem_port)
417
418 self.log.debug('pon-add-gemport', gem_port=gem_port)
419
Girish Gowdrae933cd32019-11-21 21:04:41 +0530420 return new_gem_ports
421
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400422 def _execute_queued_vlan_filter_tasks(self, uni_id):
423 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
424 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
425 # or initial-mib-download is not done. Once the tp_task is completed, we execute
426 # such queued vlan-filter tasks
427 try:
428 if uni_id in self._queued_vlan_filter_task:
429 self.log.info("executing-queued-vlan-filter-task",
430 uni_id=uni_id)
431 filter_info = self._queued_vlan_filter_task[uni_id]
432 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
Girish Gowdraa73ee452019-12-20 18:52:17 +0530433 uni_id, filter_info.get("uni_port"), filter_info.get("set_vlan_vid"),
434 filter_info.get("tp_id"))
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400435 # Now remove the entry from the dictionary
436 self._queued_vlan_filter_task[uni_id].clear()
437 self.log.debug("executed-queued-vlan-filter-task",
438 uni_id=uni_id)
439 except Exception as e:
440 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
441
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500442 def _do_tech_profile_configuration(self, uni_id, tp):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500443 us_scheduler = tp['us_scheduler']
444 alloc_id = us_scheduler['alloc_id']
445 self._create_tconts(uni_id, us_scheduler)
446 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
447 self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
448 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
449 self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
450
451 def load_and_configure_tech_profile(self, uni_id, tp_path):
452 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
453
454 if uni_id not in self._tp_service_specific_task:
455 self._tp_service_specific_task[uni_id] = dict()
456
457 if uni_id not in self._tech_profile_download_done:
458 self._tech_profile_download_done[uni_id] = dict()
459
460 if tp_path not in self._tech_profile_download_done[uni_id]:
461 self._tech_profile_download_done[uni_id][tp_path] = False
462
463 if not self._tech_profile_download_done[uni_id][tp_path]:
464 try:
465 if tp_path in self._tp_service_specific_task[uni_id]:
466 self.log.info("tech-profile-config-already-in-progress",
Girish Gowdrae933cd32019-11-21 21:04:41 +0530467 tp_path=tp_path)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500468 return
469
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500470 tpstored = self.kv_client[tp_path]
471 tpstring = tpstored.decode('ascii')
472 tp = json.loads(tpstring)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530473
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500474 self.log.debug("tp-instance", tp=tp)
475 self._do_tech_profile_configuration(uni_id, tp)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700476
William Kurkian3a206332019-04-29 11:05:47 -0400477 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500478 def success(_results):
479 self.log.info("tech-profile-config-done-successfully")
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700480 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-success')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500481 if tp_path in self._tp_service_specific_task[uni_id]:
482 del self._tp_service_specific_task[uni_id][tp_path]
483 self._tech_profile_download_done[uni_id][tp_path] = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400484 # Now execute any vlan filter tasks that were queued for later
485 self._execute_queued_vlan_filter_tasks(uni_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530486
William Kurkian3a206332019-04-29 11:05:47 -0400487 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500488 def failure(_reason):
489 self.log.warn('tech-profile-config-failure-retrying',
Girish Gowdrae933cd32019-11-21 21:04:41 +0530490 _reason=_reason)
491 yield self.core_proxy.device_reason_update(self.device_id,
492 'tech-profile-config-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500493 if tp_path in self._tp_service_specific_task[uni_id]:
494 del self._tp_service_specific_task[uni_id][tp_path]
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500495 retry = _STARTUP_RETRY_WAIT * (random.randint(1,5))
496 reactor.callLater(retry, self.load_and_configure_tech_profile,
497 uni_id, tp_path)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500498
499 self.log.info('downloading-tech-profile-configuration')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530500 # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
501 # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
502 # due to additional tasks on different UNIs. So, it we cannot use the pon_port after
503 # this initializer
504 tconts = []
505 for tcont in list(self.pon_port.tconts.values()):
506 if tcont.uni_id is not None and tcont.uni_id != uni_id:
507 continue
508 tconts.append(tcont)
509
510 gem_ports = []
511 for gem_port in list(self.pon_port.gem_ports.values()):
512 if gem_port.uni_id is not None and gem_port.uni_id != uni_id:
513 continue
514 gem_ports.append(gem_port)
515
516 self.log.debug("tconts-gems-to-install", tconts=tconts, gem_ports=gem_ports)
517
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500518 self._tp_service_specific_task[uni_id][tp_path] = \
Girish Gowdrae933cd32019-11-21 21:04:41 +0530519 BrcmTpSetupTask(self.omci_agent, self, uni_id, tconts, gem_ports, int(tp_path.split("/")[1]))
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500520 self._deferred = \
Girish Gowdrae933cd32019-11-21 21:04:41 +0530521 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500522 self._deferred.addCallbacks(success, failure)
523
524 except Exception as e:
525 self.log.exception("error-loading-tech-profile", e=e)
526 else:
527 self.log.info("tech-profile-config-already-done")
Girish Gowdrae933cd32019-11-21 21:04:41 +0530528 # Could be a case where TP exists but new gem-ports are getting added dynamically
529 tpstored = self.kv_client[tp_path]
530 tpstring = tpstored.decode('ascii')
531 tp = json.loads(tpstring)
532 upstream_gems = []
533 downstream_gems = []
534 # Find out the new Gem ports that are getting added afresh.
535 for gp in tp['upstream_gem_port_attribute_list']:
536 if self.pon_port.gem_port(gp['gemport_id'], "upstream"):
537 # gem port already exists
538 continue
539 upstream_gems.append(gp)
540 for gp in tp['downstream_gem_port_attribute_list']:
541 if self.pon_port.gem_port(gp['gemport_id'], "downstream"):
542 # gem port already exists
543 continue
544 downstream_gems.append(gp)
545
546 us_scheduler = tp['us_scheduler']
547 alloc_id = us_scheduler['alloc_id']
548
549 if len(upstream_gems) > 0 or len(downstream_gems) > 0:
550 self.log.info("installing-new-gem-ports", upstream_gems=upstream_gems, downstream_gems=downstream_gems)
551 new_upstream_gems = self._create_gemports(uni_id, upstream_gems, alloc_id, "UPSTREAM")
552 new_downstream_gems = self._create_gemports(uni_id, downstream_gems, alloc_id, "DOWNSTREAM")
553 new_gems = []
554 new_gems.extend(new_upstream_gems)
555 new_gems.extend(new_downstream_gems)
556
557 def success(_results):
558 self.log.info("new-gem-ports-successfully-installed", result=_results)
559
560 def failure(_reason):
561 self.log.warn('new-gem-port-install-failed--retrying',
562 _reason=_reason)
563 # Remove gem ports from cache. We will re-add them during the retry
564 for gp in new_gems:
565 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
566
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500567 retry = _STARTUP_RETRY_WAIT * (random.randint(1,5))
568 reactor.callLater(retry, self.load_and_configure_tech_profile,
569 uni_id, tp_path)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530570
571 self._tp_service_specific_task[uni_id][tp_path] = \
572 BrcmTpSetupTask(self.omci_agent, self, uni_id, [], new_gems, int(tp_path.split("/")[1]))
573 self._deferred = \
574 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
575 self._deferred.addCallbacks(success, failure)
576
577 def delete_tech_profile(self, uni_id, tp_path, alloc_id=None, gem_port_id=None):
578 try:
Naga Manjunathe433c712020-01-02 17:27:20 +0530579 if not uni_id in self._tech_profile_download_done:
580 self.log.warn("tp-key-is-not-present", uni_id=uni_id)
581 return
582
583 if not tp_path in self._tech_profile_download_done[uni_id]:
584 self.log.warn("tp-path-is-not-present", tp_path=tp_path)
585 return
586
Girish Gowdrae933cd32019-11-21 21:04:41 +0530587 if self._tech_profile_download_done[uni_id][tp_path] is not True:
588 self.log.error("tp-download-is-not-done-in-order-to-process-tp-delete")
589 return
590
591 if alloc_id is None and gem_port_id is None:
592 self.log.error("alloc-id-and-gem-port-id-are-none")
593 return
594
595 # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
596 # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
597 # due to additional tasks on different UNIs. So, it we cannot use the pon_port affter
598 # this initializer
599 tcont = None
600 self.log.debug("tconts", tconts=list(self.pon_port.tconts.values()))
601 for tc in list(self.pon_port.tconts.values()):
602 if tc.alloc_id == alloc_id:
603 tcont = tc
604 self.pon_port.remove_tcont(tc.alloc_id, False)
605
606 gem_port = None
607 self.log.debug("gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
608 for gp in list(self.pon_port.gem_ports.values()):
609 if gp.gem_id == gem_port_id:
610 gem_port = gp
611 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
612
613 # tp_path is of the format <technology>/<table_id>/<uni_port_name>
614 # We need the TP Table ID
615 tp_table_id = int(tp_path.split("/")[1])
616
617 @inlineCallbacks
618 def success(_results):
619 if gem_port_id:
620 self.log.info("gem-port-delete-done-successfully")
621 if alloc_id:
622 self.log.info("tcont-delete-done-successfully")
623 # The deletion of TCONT marks the complete deletion of tech-profile
624 try:
625 del self._tech_profile_download_done[uni_id][tp_path]
626 del self._tp_service_specific_task[uni_id][tp_path]
627 except Exception as ex:
628 self.log.error("del-tp-state-info", e=ex)
629
630 # TODO: There could be multiple TP on the UNI, and also the ONU.
631 # TODO: But the below reason updates for the whole device.
632 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-delete-success')
633
634 @inlineCallbacks
Girish Gowdraa73ee452019-12-20 18:52:17 +0530635 def failure(_reason):
Girish Gowdrae933cd32019-11-21 21:04:41 +0530636 self.log.warn('tech-profile-delete-failure-retrying',
637 _reason=_reason)
638 yield self.core_proxy.device_reason_update(self.device_id,
639 'tech-profile-config-delete-failure-retrying')
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500640 retry = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
641 reactor.callLater(retry, self.delete_tech_profile, uni_id, tp_path, alloc_id, gem_port_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530642
643 self.log.info('deleting-tech-profile-configuration')
644
Girish Gowdraa73ee452019-12-20 18:52:17 +0530645 if tcont is None and gem_port is None:
646 if alloc_id is not None:
647 self.log.error("tcont-info-corresponding-to-alloc-id-not-found", alloc_id=alloc_id)
648 if gem_port_id is not None:
649 self.log.error("gem-port-info-corresponding-to-gem-port-id-not-found", gem_port_id=gem_port_id)
650 return
651
Girish Gowdrae933cd32019-11-21 21:04:41 +0530652 self._tp_service_specific_task[uni_id][tp_path] = \
653 BrcmTpDeleteTask(self.omci_agent, self, uni_id, tp_table_id,
654 tcont=tcont, gem_port=gem_port)
655 self._deferred = \
656 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
657 self._deferred.addCallbacks(success, failure)
658 except Exception as e:
659 self.log.exception("failed-to-delete-tp",
660 e=e, uni_id=uni_id, tp_path=tp_path,
661 alloc_id=alloc_id, gem_port_id=gem_port_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500662
663 def update_pm_config(self, device, pm_config):
664 # TODO: This has not been tested
665 self.log.info('update_pm_config', pm_config=pm_config)
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500666 self._pm_metrics.update(pm_config)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500667
668 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
669 # flow decomposition that ultimately comes from onos
670 def update_flow_table(self, device, flows):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700671 self.log.debug('update-flow-table', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500672
673 #
674 # We need to proxy through the OLT to get to the ONU
675 # Configuration from here should be using OMCI
676 #
677 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
678
679 # no point in pushing omci flows if the device isnt reachable
680 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdrae933cd32019-11-21 21:04:41 +0530681 device.admin_state != AdminState.ENABLED:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500682 self.log.warn("device-disabled-or-offline-skipping-flow-update",
683 admin=device.admin_state, connect=device.connect_status)
684 return
685
686 def is_downstream(port):
687 return port == self._pon_port_number
688
689 def is_upstream(port):
690 return not is_downstream(port)
691
692 for flow in flows:
693 _type = None
694 _port = None
695 _vlan_vid = None
696 _udp_dst = None
697 _udp_src = None
698 _ipv4_dst = None
699 _ipv4_src = None
700 _metadata = None
701 _output = None
702 _push_tpid = None
703 _field = None
704 _set_vlan_vid = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400705 _tunnel_id = None
706
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500707 try:
Girish Gowdraa73ee452019-12-20 18:52:17 +0530708 write_metadata = fd.get_write_metadata(flow)
709 if write_metadata is None:
710 self.log.error("do-not-process-flow-without-write-metadata")
711 return
712
713 # extract tp id from flow
714 tp_id = (write_metadata >> 32) & 0xFFFF
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500715 self.log.debug("tp-id-in-flow", tp_id=tp_id)
Girish Gowdraa73ee452019-12-20 18:52:17 +0530716
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500717 _in_port = fd.get_in_port(flow)
718 assert _in_port is not None
719
720 _out_port = fd.get_out_port(flow) # may be None
721
722 if is_downstream(_in_port):
723 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
724 uni_port = self.uni_port(_out_port)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530725 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500726 elif is_upstream(_in_port):
727 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
728 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400729 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500730 else:
731 raise Exception('port should be 1 or 2 by our convention')
732
733 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
734
735 for field in fd.get_ofb_fields(flow):
736 if field.type == fd.ETH_TYPE:
737 _type = field.eth_type
738 self.log.debug('field-type-eth-type',
739 eth_type=_type)
740
741 elif field.type == fd.IP_PROTO:
742 _proto = field.ip_proto
743 self.log.debug('field-type-ip-proto',
744 ip_proto=_proto)
745
746 elif field.type == fd.IN_PORT:
747 _port = field.port
748 self.log.debug('field-type-in-port',
749 in_port=_port)
750
751 elif field.type == fd.VLAN_VID:
752 _vlan_vid = field.vlan_vid & 0xfff
753 self.log.debug('field-type-vlan-vid',
754 vlan=_vlan_vid)
755
756 elif field.type == fd.VLAN_PCP:
757 _vlan_pcp = field.vlan_pcp
758 self.log.debug('field-type-vlan-pcp',
759 pcp=_vlan_pcp)
760
761 elif field.type == fd.UDP_DST:
762 _udp_dst = field.udp_dst
763 self.log.debug('field-type-udp-dst',
764 udp_dst=_udp_dst)
765
766 elif field.type == fd.UDP_SRC:
767 _udp_src = field.udp_src
768 self.log.debug('field-type-udp-src',
769 udp_src=_udp_src)
770
771 elif field.type == fd.IPV4_DST:
772 _ipv4_dst = field.ipv4_dst
773 self.log.debug('field-type-ipv4-dst',
774 ipv4_dst=_ipv4_dst)
775
776 elif field.type == fd.IPV4_SRC:
777 _ipv4_src = field.ipv4_src
778 self.log.debug('field-type-ipv4-src',
779 ipv4_dst=_ipv4_src)
780
781 elif field.type == fd.METADATA:
782 _metadata = field.table_metadata
783 self.log.debug('field-type-metadata',
784 metadata=_metadata)
785
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400786 elif field.type == fd.TUNNEL_ID:
787 _tunnel_id = field.tunnel_id
788 self.log.debug('field-type-tunnel-id',
789 tunnel_id=_tunnel_id)
790
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500791 else:
792 raise NotImplementedError('field.type={}'.format(
793 field.type))
794
795 for action in fd.get_actions(flow):
796
797 if action.type == fd.OUTPUT:
798 _output = action.output.port
799 self.log.debug('action-type-output',
800 output=_output, in_port=_in_port)
801
802 elif action.type == fd.POP_VLAN:
803 self.log.debug('action-type-pop-vlan',
804 in_port=_in_port)
805
806 elif action.type == fd.PUSH_VLAN:
807 _push_tpid = action.push.ethertype
808 self.log.debug('action-type-push-vlan',
809 push_tpid=_push_tpid, in_port=_in_port)
810 if action.push.ethertype != 0x8100:
811 self.log.error('unhandled-tpid',
812 ethertype=action.push.ethertype)
813
814 elif action.type == fd.SET_FIELD:
815 _field = action.set_field.field.ofb_field
816 assert (action.set_field.field.oxm_class ==
817 OFPXMC_OPENFLOW_BASIC)
818 self.log.debug('action-type-set-field',
819 field=_field, in_port=_in_port)
820 if _field.type == fd.VLAN_VID:
821 _set_vlan_vid = _field.vlan_vid & 0xfff
822 self.log.debug('set-field-type-vlan-vid',
823 vlan_vid=_set_vlan_vid)
824 else:
825 self.log.error('unsupported-action-set-field-type',
826 field_type=_field.type)
827 else:
828 self.log.error('unsupported-action-type',
829 action_type=action.type, in_port=_in_port)
830
Matt Jeanneret810148b2019-09-29 12:44:01 -0400831 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
832 # supported match and action criteria cannot be handled by omci and must be ignored.
833 if _set_vlan_vid is None or _set_vlan_vid == 0:
834 self.log.warn('ignoring-flow-that-does-not-set-vlanid')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500835 else:
Girish Gowdraa73ee452019-12-20 18:52:17 +0530836 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, set_vlan_vid=_set_vlan_vid, tp_id=tp_id)
837 self._add_vlan_filter_task(device, uni_id, uni_port, _set_vlan_vid, tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500838 except Exception as e:
839 self.log.exception('failed-to-install-flow', e=e, flow=flow)
840
Girish Gowdraa73ee452019-12-20 18:52:17 +0530841 def _add_vlan_filter_task(self, device, uni_id, uni_port, _set_vlan_vid, tp_id):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500842 assert uni_port is not None
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400843 if uni_id in self._tech_profile_download_done and self._tech_profile_download_done[uni_id] != {}:
844 @inlineCallbacks
845 def success(_results):
Girish Gowdraa73ee452019-12-20 18:52:17 +0530846 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid, tp_id=tp_id)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700847 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-pushed')
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400848 self._vlan_filter_task = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500849
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400850 @inlineCallbacks
851 def failure(_reason):
Girish Gowdraa73ee452019-12-20 18:52:17 +0530852 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid, tp_id=tp_id)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700853 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-failed-retrying')
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -0500854 retry = _STARTUP_RETRY_WAIT * (random.randint(1,5))
855 reactor.callLater(retry,
856 self._add_vlan_filter_task, device, uni_port.port_number,
857 uni_port, _set_vlan_vid, tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500858
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400859 self.log.info('setting-vlan-tag')
Girish Gowdraa73ee452019-12-20 18:52:17 +0530860 self._vlan_filter_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid, tp_id)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400861 self._deferred = self._onu_omci_device.task_runner.queue_task(self._vlan_filter_task)
862 self._deferred.addCallbacks(success, failure)
863 else:
864 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
865 uni_id=uni_id)
Matt Jeanneret810148b2019-09-29 12:44:01 -0400866 self._queued_vlan_filter_task[uni_id] = {"device": device,
Girish Gowdrae933cd32019-11-21 21:04:41 +0530867 "uni_id": uni_id,
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400868 "uni_port": uni_port,
Girish Gowdraa73ee452019-12-20 18:52:17 +0530869 "set_vlan_vid": _set_vlan_vid,
870 "tp_id": tp_id}
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500871
872 def get_tx_id(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700873 self.log.debug('get-tx-id')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500874 self.tx_id += 1
875 return self.tx_id
876
Matt Jeannereta32441c2019-03-07 05:16:37 -0500877 def process_inter_adapter_message(self, request):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700878 self.log.debug('process-inter-adapter-message', type=request.header.type, from_topic=request.header.from_topic,
879 to_topic=request.header.to_topic, to_device_id=request.header.to_device_id)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500880 try:
881 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
882 omci_msg = InterAdapterOmciMessage()
883 request.body.Unpack(omci_msg)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700884 self.log.debug('inter-adapter-recv-omci', omci_msg=hexify(omci_msg.message))
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500885
Matt Jeannereta32441c2019-03-07 05:16:37 -0500886 self.receive_message(omci_msg.message)
887
888 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
889 onu_indication = OnuIndication()
890 request.body.Unpack(onu_indication)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700891 self.log.debug('inter-adapter-recv-onu-ind', onu_id=onu_indication.onu_id,
892 oper_state=onu_indication.oper_state, admin_state=onu_indication.admin_state,
893 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500894
895 if onu_indication.oper_state == "up":
896 self.create_interface(onu_indication)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530897 elif onu_indication.oper_state == "down" or onu_indication.oper_state == "unreachable":
Matt Jeannereta32441c2019-03-07 05:16:37 -0500898 self.update_interface(onu_indication)
899 else:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700900 self.log.error("unknown-onu-indication", onu_id=onu_indication.onu_id,
901 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500902
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400903 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
904 tech_msg = InterAdapterTechProfileDownloadMessage()
905 request.body.Unpack(tech_msg)
906 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
907
908 self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
909
Girish Gowdrae933cd32019-11-21 21:04:41 +0530910 elif request.header.type == InterAdapterMessageType.DELETE_GEM_PORT_REQUEST:
911 del_gem_msg = InterAdapterDeleteGemPortMessage()
912 request.body.Unpack(del_gem_msg)
913 self.log.debug('inter-adapter-recv-del-gem', gem_del_msg=del_gem_msg)
914
915 self.delete_tech_profile(uni_id=del_gem_msg.uni_id,
916 gem_port_id=del_gem_msg.gem_port_id,
917 tp_path=del_gem_msg.tp_path)
918
919 elif request.header.type == InterAdapterMessageType.DELETE_TCONT_REQUEST:
920 del_tcont_msg = InterAdapterDeleteTcontMessage()
921 request.body.Unpack(del_tcont_msg)
922 self.log.debug('inter-adapter-recv-del-tcont', del_tcont_msg=del_tcont_msg)
923
924 self.delete_tech_profile(uni_id=del_tcont_msg.uni_id,
925 alloc_id=del_tcont_msg.alloc_id,
926 tp_path=del_tcont_msg.tp_path)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500927 else:
928 self.log.error("inter-adapter-unhandled-type", request=request)
929
930 except Exception as e:
931 self.log.exception("error-processing-inter-adapter-message", e=e)
932
933 # Called each time there is an onu "up" indication from the olt handler
934 @inlineCallbacks
935 def create_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500936 self.log.info('create-interface', onu_id=onu_indication.onu_id,
Matteo Scandolod8d73172019-11-26 12:15:15 -0700937 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500938 self._onu_indication = onu_indication
939
Matt Jeanneretc083f462019-03-11 15:02:01 -0400940 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
941 connect_status=ConnectStatus.REACHABLE)
942
Matt Jeannereta32441c2019-03-07 05:16:37 -0500943 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500944
945 self.log.debug('starting-openomci-statemachine')
946 self._subscribe_to_events()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500947 onu_device.reason = "starting-openomci"
Girish Gowdrae933cd32019-11-21 21:04:41 +0530948 reactor.callLater(1, self._onu_omci_device.start, onu_device)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700949 yield self.core_proxy.device_reason_update(self.device_id, onu_device.reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500950 self._heartbeat.enabled = True
951
952 # Currently called each time there is an onu "down" indication from the olt handler
953 # TODO: possibly other reasons to "update" from the olt?
Matt Jeannereta32441c2019-03-07 05:16:37 -0500954 @inlineCallbacks
955 def update_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500956 self.log.info('update-interface', onu_id=onu_indication.onu_id,
Matteo Scandolod8d73172019-11-26 12:15:15 -0700957 serial_number=onu_indication.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500958
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -0400959 if onu_indication.oper_state == 'down' or onu_indication.oper_state == "unreachable":
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500960 self.log.debug('stopping-openomci-statemachine')
961 reactor.callLater(0, self._onu_omci_device.stop)
962
963 # Let TP download happen again
964 for uni_id in self._tp_service_specific_task:
965 self._tp_service_specific_task[uni_id].clear()
966 for uni_id in self._tech_profile_download_done:
967 self._tech_profile_download_done[uni_id].clear()
968
Matt Jeanneretf4113222019-08-14 19:44:34 -0400969 yield self.disable_ports(lock_ports=False)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700970 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
971 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.DISCOVERED,
972 connect_status=ConnectStatus.UNREACHABLE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500973 else:
974 self.log.debug('not-changing-openomci-statemachine')
975
976 # Not currently called by olt or anything else
William Kurkian3a206332019-04-29 11:05:47 -0400977 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500978 def remove_interface(self, data):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500979 self.log.info('remove-interface', data=data)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500980
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500981 self.log.debug('stopping-openomci-statemachine')
982 reactor.callLater(0, self._onu_omci_device.stop)
983
984 # Let TP download happen again
985 for uni_id in self._tp_service_specific_task:
986 self._tp_service_specific_task[uni_id].clear()
987 for uni_id in self._tech_profile_download_done:
988 self._tech_profile_download_done[uni_id].clear()
989
Matt Jeanneretf4113222019-08-14 19:44:34 -0400990 yield self.disable_ports(lock_ports=False)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700991 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500992
Matt Jeanneretf4113222019-08-14 19:44:34 -0400993 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500994 def disable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500995 self.log.info('disable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500996 try:
Matt Jeanneretf4113222019-08-14 19:44:34 -0400997 yield self.disable_ports(lock_ports=True)
998 yield self.core_proxy.device_reason_update(self.device_id, "omci-admin-lock")
999 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001000
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001001 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001002 self.log.exception('exception-in-onu-disable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001003
William Kurkian3a206332019-04-29 11:05:47 -04001004 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001005 def reenable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001006 self.log.info('reenable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001007 try:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001008 yield self.core_proxy.device_state_update(device.id,
1009 oper_status=OperStatus.ACTIVE,
1010 connect_status=ConnectStatus.REACHABLE)
1011 yield self.core_proxy.device_reason_update(self.device_id, 'onu-reenabled')
1012 yield self.enable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001013 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001014 self.log.exception('exception-in-onu-reenable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001015
William Kurkian3a206332019-04-29 11:05:47 -04001016 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001017 def reboot(self):
1018 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -04001019 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001020 if device.connect_status != ConnectStatus.REACHABLE:
1021 self.log.error("device-unreachable")
1022 return
1023
William Kurkian3a206332019-04-29 11:05:47 -04001024 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001025 def success(_results):
1026 self.log.info('reboot-success', _results=_results)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001027 yield self.core_proxy.device_reason_update(self.device_id, 'rebooting')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001028
1029 def failure(_reason):
1030 self.log.info('reboot-failure', _reason=_reason)
1031
1032 self._deferred = self._onu_omci_device.reboot()
1033 self._deferred.addCallbacks(success, failure)
1034
William Kurkian3a206332019-04-29 11:05:47 -04001035 @inlineCallbacks
Matt Jeanneretf4113222019-08-14 19:44:34 -04001036 def disable_ports(self, lock_ports=True):
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001037 self.log.info('disable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001038
Matt Jeanneretf4113222019-08-14 19:44:34 -04001039 bus = self._onu_omci_device.event_bus
1040 bus.unsubscribe(self._port_state_subscription)
1041 self._port_state_subscription = None
1042
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001043 # Disable all ports on that device
Matt Jeanneretf4113222019-08-14 19:44:34 -04001044 for port in self.uni_ports:
1045 port.operstatus = OperStatus.UNKNOWN
1046 self.log.info('disable-port', device_id=self.device_id, port=port)
1047
1048 if lock_ports is True:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001049 self.lock_ports(lock=True)
1050
Matt Jeanneret80766692019-05-03 09:58:38 -04001051 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001052
William Kurkian3a206332019-04-29 11:05:47 -04001053 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001054 def enable_ports(self):
1055 self.log.info('enable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001056
Matt Jeanneretf4113222019-08-14 19:44:34 -04001057 # Listen for UNI link state alarms and set the oper_state based on that rather than assuming all UNI are up
Matt Jeanneretf4113222019-08-14 19:44:34 -04001058 bus = self._onu_omci_device.event_bus
1059 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1060 OnuDeviceEvents.PortEvent)
1061 self._port_state_subscription = bus.subscribe(topic, self.port_state_handler)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001062 self.lock_ports(lock=False)
1063
1064 # TODO: Currently the only VEIP capable ONU i can test with does not send UNI link state alarms
1065 # or set operational-state per OMCI spec. So i have know way of "knowing" if the port is up.
1066 # Given this i assume its always up for now. Maybe a software upgrade will fix my onu...
1067 for port in self.uni_ports:
1068 if port.type.value == UniType.VEIP.value:
1069 port.operstatus = OperStatus.ACTIVE
1070 self.log.warn('force-showing-veip-link-up', device_id=self.device_id, port=port)
1071 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number, port.operstatus)
1072
1073 @inlineCallbacks
1074 def port_state_handler(self, _topic, msg):
1075 self.log.info("port-state-change", _topic=_topic, msg=msg)
1076
1077 onu_id = msg['onu_id']
1078 port_no = msg['port_number']
1079 serial_number = msg['serial_number']
1080 port_status = msg['port_status']
1081 uni_port = self.uni_port(int(port_no))
1082
1083 self.log.debug("port-state-parsed-message", onu_id=onu_id, port_no=port_no, serial_number=serial_number,
1084 port_status=port_status)
1085
1086 if port_status is True:
1087 uni_port.operstatus = OperStatus.ACTIVE
1088 self.log.info('link-up', device_id=self.device_id, port=uni_port)
1089 else:
1090 uni_port.operstatus = OperStatus.UNKNOWN
1091 self.log.info('link-down', device_id=self.device_id, port=uni_port)
1092
1093 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, uni_port.port_number, uni_port.operstatus)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001094
1095 # Called just before openomci state machine is started. These listen for events from selected state machines,
1096 # most importantly, mib in sync. Which ultimately leads to downloading the mib
1097 def _subscribe_to_events(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001098 self.log.debug('subscribe-to-events')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001099
1100 # OMCI MIB Database sync status
1101 bus = self._onu_omci_device.event_bus
1102 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1103 OnuDeviceEvents.MibDatabaseSyncEvent)
1104 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
1105
1106 # OMCI Capabilities
1107 bus = self._onu_omci_device.event_bus
1108 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1109 OnuDeviceEvents.OmciCapabilitiesEvent)
1110 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
1111
1112 # Called when the mib is in sync
1113 def in_sync_handler(self, _topic, msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001114 self.log.debug('in-sync-handler', _topic=_topic, msg=msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001115 if self._in_sync_subscription is not None:
1116 try:
1117 in_sync = msg[IN_SYNC_KEY]
1118
1119 if in_sync:
1120 # Only call this once
1121 bus = self._onu_omci_device.event_bus
1122 bus.unsubscribe(self._in_sync_subscription)
1123 self._in_sync_subscription = None
1124
1125 # Start up device_info load
1126 self.log.debug('running-mib-sync')
1127 reactor.callLater(0, self._mib_in_sync)
1128
1129 except Exception as e:
1130 self.log.exception('in-sync', e=e)
1131
1132 def capabilties_handler(self, _topic, _msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001133 self.log.debug('capabilities-handler', _topic=_topic, msg=_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001134 if self._capabilities_subscription is not None:
1135 self.log.debug('capabilities-handler-done')
1136
1137 # Mib is in sync, we can now query what we learned and actually start pushing ME (download) to the ONU.
1138 # Currently uses a basic mib download task that create a bridge with a single gem port and uni, only allowing EAP
1139 # Implement your own MibDownloadTask if you wish to setup something different by default
Matt Jeanneretc083f462019-03-11 15:02:01 -04001140 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001141 def _mib_in_sync(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001142 self.log.debug('mib-in-sync')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001143
1144 omci = self._onu_omci_device
1145 in_sync = omci.mib_db_in_sync
1146
Matt Jeanneretc083f462019-03-11 15:02:01 -04001147 device = yield self.core_proxy.get_device(self.device_id)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001148 yield self.core_proxy.device_reason_update(self.device_id, 'discovery-mibsync-complete')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001149
1150 if not self._dev_info_loaded:
1151 self.log.info('loading-device-data-from-mib', in_sync=in_sync, already_loaded=self._dev_info_loaded)
1152
1153 omci_dev = self._onu_omci_device
1154 config = omci_dev.configuration
1155
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001156 try:
1157
1158 # sort the lists so we get consistent port ordering.
1159 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
1160 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
1161 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
1162 veip_list = sorted(config.veip_entities) if config.veip_entities else []
1163
1164 if ani_list is None or (pptp_list is None and veip_list is None):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001165 self.log.warn("no-ani-or-unis")
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001166 yield self.core_proxy.device_reason_update(self.device_id, 'onu-missing-required-elements')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001167 raise Exception("onu-missing-required-elements")
1168
1169 # Currently logging the ani, pptp, veip, and uni for information purposes.
1170 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
1171 # And in some ONU the UNI-G list is incomplete or incorrect...
1172 for entity_id in ani_list:
1173 ani_value = config.ani_g_entities[entity_id]
1174 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
1175 # TODO: currently only one OLT PON port/ANI, so this works out. With NGPON there will be 2..?
1176 self._total_tcont_count = ani_value.get('total-tcont-count')
1177 self.log.debug("set-total-tcont-count", tcont_count=self._total_tcont_count)
1178
1179 for entity_id in uni_list:
1180 uni_value = config.uni_g_entities[entity_id]
1181 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
1182
1183 uni_entities = OrderedDict()
1184 for entity_id in pptp_list:
1185 pptp_value = config.pptp_entities[entity_id]
1186 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
1187 uni_entities[entity_id] = UniType.PPTP
1188
1189 for entity_id in veip_list:
1190 veip_value = config.veip_entities[entity_id]
1191 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
1192 uni_entities[entity_id] = UniType.VEIP
1193
1194 uni_id = 0
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -05001195 for entity_id, uni_type in uni_entities.items():
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001196 try:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001197 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001198 uni_id += 1
1199 except AssertionError as e:
1200 self.log.warn("could not add UNI", entity_id=entity_id, uni_type=uni_type, e=e)
1201
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001202 self._qos_flexibility = config.qos_configuration_flexibility or 0
1203 self._omcc_version = config.omcc_version or OMCCVersion.Unknown
1204
1205 if self._unis:
1206 self._dev_info_loaded = True
1207 else:
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001208 yield self.core_proxy.device_reason_update(self.device_id, 'no-usable-unis')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001209 self.log.warn("no-usable-unis")
1210 raise Exception("no-usable-unis")
1211
1212 except Exception as e:
1213 self.log.exception('device-info-load', e=e)
1214 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1215
1216 else:
1217 self.log.info('device-info-already-loaded', in_sync=in_sync, already_loaded=self._dev_info_loaded)
1218
1219 if self._dev_info_loaded:
Matt Jeanneretad9a0f12019-05-09 14:05:49 -04001220 if device.admin_state == AdminState.PREPROVISIONED or device.admin_state == AdminState.ENABLED:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001221
1222 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001223 def success(_results):
1224 self.log.info('mib-download-success', _results=_results)
Matt Jeanneretc083f462019-03-11 15:02:01 -04001225 yield self.core_proxy.device_state_update(device.id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301226 oper_status=OperStatus.ACTIVE,
1227 connect_status=ConnectStatus.REACHABLE)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001228 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-downloaded')
Matt Jeanneretf4113222019-08-14 19:44:34 -04001229 yield self.enable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001230 self._mib_download_task = None
Devmalya Paulffc89df2019-07-31 17:43:13 -04001231 yield self.onu_active_event()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001232
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -05001233 # Start collecting stats from the device after a brief pause
1234 if not self._pm_metrics_started:
1235 self._pm_metrics_started = True
1236 pmstart = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1237 reactor.callLater(pmstart, self._pm_metrics.start_collector)
1238
1239 # Start test requests after a brief pause
1240 if not self._test_request_started:
1241 self._test_request_started = True
1242 tststart = _STARTUP_RETRY_WAIT * (random.randint(1, 5))
1243 reactor.callLater(tststart, self._test_request.start_collector)
1244
Matt Jeanneretc083f462019-03-11 15:02:01 -04001245 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001246 def failure(_reason):
1247 self.log.warn('mib-download-failure-retrying', _reason=_reason)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001248 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-download-failure-retrying')
Matt Jeanneret04ebe8f2020-01-26 01:05:23 -05001249 retry = _STARTUP_RETRY_WAIT * (random.randint(1,5))
1250 reactor.callLater(retry, self._mib_in_sync)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001251
Matt Jeanneretf4113222019-08-14 19:44:34 -04001252 # start by locking all the unis till mib sync and initial mib is downloaded
1253 # this way we can capture the port down/up events when we are ready
1254 self.lock_ports(lock=True)
1255
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001256 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1257 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1258 self.log.info('downloading-initial-mib-configuration')
1259 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1260 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1261 self._deferred.addCallbacks(success, failure)
1262 else:
1263 self.log.info('admin-down-disabling')
1264 self.disable(device)
1265 else:
1266 self.log.info('device-info-not-loaded-skipping-mib-download')
1267
Matt Jeanneretc083f462019-03-11 15:02:01 -04001268 @inlineCallbacks
1269 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001270 self.log.debug('add-uni-port')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001271
Matt Jeanneretc083f462019-03-11 15:02:01 -04001272 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 -05001273
1274 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1275 uni_name = "uni-{}".format(uni_no)
1276
Girish Gowdrae933cd32019-11-21 21:04:41 +05301277 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001278
1279 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 -04001280 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001281
1282 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1283 uni_port.entity_id = entity_id
1284 uni_port.enabled = True
1285 uni_port.mac_bridge_port_num = mac_bridge_port_num
1286
1287 self.log.debug("created-uni-port", uni=uni_port)
1288
Matt Jeanneretc083f462019-03-11 15:02:01 -04001289 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001290
1291 self._unis[uni_port.port_number] = uni_port
1292
1293 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=self._onu_indication.onu_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301294 uni_ports=self.uni_ports,
1295 serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001296
Matt Jeanneretc083f462019-03-11 15:02:01 -04001297 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1298 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
Amit Ghosh65400f12019-11-21 12:04:12 +00001299 MAX_PONS_PER_OLT = 256
1300 MAX_ONUS_PER_PON = 256
Matt Jeanneretc083f462019-03-11 15:02:01 -04001301 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001302
Matt Jeanneretc083f462019-03-11 15:02:01 -04001303 assert intf_id < MAX_PONS_PER_OLT
1304 assert onu_id < MAX_ONUS_PER_PON
1305 assert uni_id < MAX_UNIS_PER_ONU
Amit Ghosh65400f12019-11-21 12:04:12 +00001306 return intf_id << 12 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001307
1308 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001309 def onu_active_event(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001310 self.log.debug('onu-active-event')
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001311 try:
1312 device = yield self.core_proxy.get_device(self.device_id)
1313 parent_device = yield self.core_proxy.get_device(self.parent_id)
1314 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001315 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001316
1317 self.log.debug("onu-indication-context-data",
Girish Gowdrae933cd32019-11-21 21:04:41 +05301318 pon_id=self._onu_indication.intf_id,
1319 onu_id=self._onu_indication.onu_id,
1320 registration_id=self.device_id,
1321 device_id=self.device_id,
1322 onu_serial_number=device.serial_number,
1323 olt_serial_number=olt_serial_number,
1324 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001325
Devmalya Paulffc89df2019-07-31 17:43:13 -04001326 self.log.debug("Trying-to-raise-onu-active-event")
1327 OnuActiveEvent(self.events, self.device_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001328 self._onu_indication.intf_id,
1329 device.serial_number,
1330 str(self.device_id),
Girish Gowdrae933cd32019-11-21 21:04:41 +05301331 olt_serial_number, raised_ts,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001332 onu_id=self._onu_indication.onu_id).send(True)
1333 except Exception as active_event_error:
1334 self.log.exception('onu-activated-event-error',
1335 errmsg=active_event_error.message)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001336
1337 def lock_ports(self, lock=True):
1338
1339 def success(response):
1340 self.log.debug('set-onu-ports-state', lock=lock, response=response)
1341
1342 def failure(response):
1343 self.log.error('cannot-set-onu-ports-state', lock=lock, response=response)
1344
1345 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=lock)
1346 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
1347 self._deferred.addCallbacks(success, failure)