blob: 3bc1afba7f5f3822e0fd2f48c09d8e954e885709 [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 Jeanneretf1e9c5d2019-02-08 07:41:29 -050021import ast
Devmalya Paulffc89df2019-07-31 17:43:13 -040022import arrow
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050023import structlog
24
25from collections import OrderedDict
26
27from twisted.internet import reactor, task
28from twisted.internet.defer import DeferredQueue, inlineCallbacks, returnValue, TimeoutError
29
30from heartbeat import HeartBeat
Devmalya Paulffc89df2019-07-31 17:43:13 -040031from pyvoltha.adapters.extensions.events.device_events.onu.onu_active_event import OnuActiveEvent
32from pyvoltha.adapters.extensions.events.kpi.onu.onu_pm_metrics import OnuPmMetrics
33from pyvoltha.adapters.extensions.events.kpi.onu.onu_omci_pm import OnuOmciPmMetrics
34from pyvoltha.adapters.extensions.events.adapter_events import AdapterEvents
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050035
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050036import pyvoltha.common.openflow.utils as fd
37from pyvoltha.common.utils.registry import registry
38from pyvoltha.common.config.config_backend import ConsulStore
39from pyvoltha.common.config.config_backend import EtcdStore
William Kurkian8235c1e2019-03-05 12:58:28 -050040from voltha_protos.common_pb2 import OperStatus, ConnectStatus, AdminState
Matt Jeanneretc083f462019-03-11 15:02:01 -040041from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, ofp_port, OFPPS_LIVE, OFPPF_FIBER, OFPPF_1GB_FD
Matt Jeanneret3bfebff2019-04-12 18:25:03 -040042from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
43 InterAdapterOmciMessage, PortCapability, InterAdapterTechProfileDownloadMessage
Matt Jeannereta32441c2019-03-07 05:16:37 -050044from voltha_protos.openolt_pb2 import OnuIndication
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050045from pyvoltha.adapters.extensions.omci.onu_configuration import OMCCVersion
46from pyvoltha.adapters.extensions.omci.onu_device_entry import OnuDeviceEvents, \
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050047 OnuDeviceEntry, IN_SYNC_KEY
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050048from omci.brcm_mib_download_task import BrcmMibDownloadTask
49from omci.brcm_tp_service_specific_task import BrcmTpServiceSpecificTask
50from omci.brcm_uni_lock_task import BrcmUniLockTask
51from omci.brcm_vlan_filter_task import BrcmVlanFilterTask
52from onu_gem_port import *
53from onu_tcont import *
54from pon_port import *
55from uni_port import *
56from onu_traffic_descriptor import *
57from pyvoltha.common.tech_profile.tech_profile import TechProfile
onkarkundargiaae99712019-09-23 15:02:52 +053058from pyvoltha.adapters.extensions.omci.tasks.omci_test_request import OmciTestRequest
59from pyvoltha.adapters.extensions.omci.omci_entities import AniG
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050060
61OP = EntityOperations
62RC = ReasonCodes
63
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050064log = structlog.get_logger()
65
66_STARTUP_RETRY_WAIT = 20
67
68
69class BrcmOpenomciOnuHandler(object):
70
71 def __init__(self, adapter, device_id):
72 self.log = structlog.get_logger(device_id=device_id)
73 self.log.debug('function-entry')
74 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050075 self.core_proxy = adapter.core_proxy
76 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050077 self.parent_adapter = None
78 self.parent_id = None
79 self.device_id = device_id
80 self.incoming_messages = DeferredQueue()
81 self.event_messages = DeferredQueue()
82 self.proxy_address = None
83 self.tx_id = 0
84 self._enabled = False
Devmalya Paulffc89df2019-07-31 17:43:13 -040085 self.events = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050086 self.pm_metrics = None
87 self._omcc_version = OMCCVersion.Unknown
88 self._total_tcont_count = 0 # From ANI-G ME
89 self._qos_flexibility = 0 # From ONT2_G ME
90
91 self._onu_indication = None
92 self._unis = dict() # Port # -> UniPort
93
94 self._pon = None
95 # TODO: probably shouldnt be hardcoded, determine from olt maybe?
96 self._pon_port_number = 100
97 self.logical_device_id = None
98
99 self._heartbeat = HeartBeat.create(self, device_id)
100
101 # Set up OpenOMCI environment
102 self._onu_omci_device = None
103 self._dev_info_loaded = False
104 self._deferred = None
105
106 self._in_sync_subscription = None
107 self._connectivity_subscription = None
108 self._capabilities_subscription = None
109
110 self.mac_bridge_service_profile_entity_id = 0x201
111 self.gal_enet_profile_entity_id = 0x1
112
113 self._tp_service_specific_task = dict()
114 self._tech_profile_download_done = dict()
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400115 # Stores information related to queued vlan filter tasks
116 # Dictionary with key being uni_id and value being device,uni port ,uni id and vlan id
117
118 self._queued_vlan_filter_task = dict()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500119
120 # Initialize KV store client
121 self.args = registry('main').get_args()
122 if self.args.backend == 'etcd':
123 host, port = self.args.etcd.split(':', 1)
124 self.kv_client = EtcdStore(host, port,
125 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
126 elif self.args.backend == 'consul':
127 host, port = self.args.consul.split(':', 1)
128 self.kv_client = ConsulStore(host, port,
129 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
130 else:
131 self.log.error('Invalid-backend')
132 raise Exception("Invalid-backend-for-kv-store")
133
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500134 @property
135 def enabled(self):
136 return self._enabled
137
138 @enabled.setter
139 def enabled(self, value):
140 if self._enabled != value:
141 self._enabled = value
142
143 @property
144 def omci_agent(self):
145 return self.adapter.omci_agent
146
147 @property
148 def omci_cc(self):
149 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
150
151 @property
152 def heartbeat(self):
153 return self._heartbeat
154
155 @property
156 def uni_ports(self):
157 return self._unis.values()
158
159 def uni_port(self, port_no_or_name):
160 if isinstance(port_no_or_name, (str, unicode)):
161 return next((uni for uni in self.uni_ports
162 if uni.name == port_no_or_name), None)
163
164 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
165 return next((uni for uni in self.uni_ports
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400166 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500167
168 @property
169 def pon_port(self):
170 return self._pon
171
172 def receive_message(self, msg):
173 if self.omci_cc is not None:
174 self.omci_cc.receive_message(msg)
175
Matt Jeanneretc083f462019-03-11 15:02:01 -0400176 def get_ofp_port_info(self, device, port_no):
177 self.log.info('get_ofp_port_info', port_no=port_no, device_id=device.id)
178 cap = OFPPF_1GB_FD | OFPPF_FIBER
179
180 hw_addr=mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
181 ((device.parent_port_no >> 8 & 0xff),
182 device.parent_port_no & 0xff,
183 (port_no >> 16) & 0xff,
184 (port_no >> 8) & 0xff,
185 port_no & 0xff))
186
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400187 uni_port = self.uni_port(int(port_no))
188 name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
189 self.log.debug('ofp_port_name', port_no=port_no, name=name)
190
Matt Jeanneretc083f462019-03-11 15:02:01 -0400191 return PortCapability(
192 port=LogicalPort(
193 ofp_port=ofp_port(
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400194 name=name,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400195 hw_addr=hw_addr,
196 config=0,
197 state=OFPPS_LIVE,
198 curr=cap,
199 advertised=cap,
200 peer=cap,
201 curr_speed=OFPPF_1GB_FD,
202 max_speed=OFPPF_1GB_FD
203 ),
204 device_id=device.id,
205 device_port_no=port_no
206 )
207 )
208
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500209 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500210 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500211 def activate(self, device):
212 self.log.debug('function-entry', device=device)
213
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500214 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500215 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500216 assert device.proxy_address.device_id
217
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500218 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500219 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500220 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500221 if self.enabled is not True:
222 self.log.info('activating-new-onu')
223 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500224 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500225 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500226 device.reason = 'activating-onu'
227
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500228 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400229 # pm_metrics requires a logical device id. For now set to just device_id
230 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500231
Matt Jeannereta32441c2019-03-07 05:16:37 -0500232 yield self.core_proxy.device_update(device)
Mahir Gunyel0e1588a2019-06-27 06:12:47 -0700233 self.log.debug('device updated', device=device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500234
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700235 yield self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500236
Mahir Gunyel0e1588a2019-06-27 06:12:47 -0700237 self.log.debug('pon state initialized', device=device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500238 ############################################################################
Devmalya Paulffc89df2019-07-31 17:43:13 -0400239 # Setup Alarm handler
240 self.events = AdapterEvents(self.core_proxy, device.id, self.logical_device_id,
241 device.serial_number)
242 ############################################################################
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500243 # Setup PM configuration for this device
244 # Pass in ONU specific options
245 kwargs = {
246 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
247 'heartbeat': self.heartbeat,
248 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
249 }
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400250 self.log.debug('create-OnuPmMetrics', serial_number=device.serial_number)
Devmalya Paulffc89df2019-07-31 17:43:13 -0400251 self.pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400252 self.logical_device_id, device.serial_number,
253 grouped=True, freq_override=False, **kwargs)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500254 pm_config = self.pm_metrics.make_proto()
255 self._onu_omci_device.set_pm_config(self.pm_metrics.omci_pm.openomci_interval_pm)
256 self.log.info("initial-pm-config", pm_config=pm_config)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500257 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500258
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500259 # Note, ONU ID and UNI intf set in add_uni_port method
Devmalya Paulffc89df2019-07-31 17:43:13 -0400260 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500261 ani_ports=[self._pon])
aishwaryarana01a98d9fe2019-05-08 12:09:06 -0500262
263 #Start collecting stats from the device after a brief pause
264 reactor.callLater(10, self.pm_metrics.start_collector)
265
onkarkundargiaae99712019-09-23 15:02:52 +0530266 # Code to Run OMCI Test Action
267 kwargs_omci_test_action = {
268 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
269 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
270 }
271 serial_number = device.serial_number
272 test_request = OmciTestRequest(self.core_proxy,
273 self.omci_agent, self.device_id,
274 AniG, serial_number,
275 self.logical_device_id,
276 exclusive=False,
277 **kwargs_omci_test_action)
278 reactor.callLater(60, test_request.start_collector)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500279 self.enabled = True
280 else:
281 self.log.info('onu-already-activated')
282
283 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400284 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500285 def reconcile(self, device):
286 self.log.debug('function-entry', device=device)
287
288 # first we verify that we got parent reference and proxy info
289 assert device.parent_id
290 assert device.proxy_address.device_id
291
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700292 self.proxy_address = device.proxy_address
293 self.parent_id = device.parent_id
294 self._pon_port_number = device.parent_port_no
295
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500296 if self.enabled is not True:
297 self.log.info('reconciling-broadcom-onu-device')
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700298 self.logical_device_id = self.device_id
299 self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500300
301 # need to restart state machines on vcore restart. there is no indication to do it for us.
302 self._onu_omci_device.start()
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700303 yield self.core_proxy.device_reason_update(self.device_id, "restarting-openomci")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500304
305 # TODO: this is probably a bit heavy handed
306 # Force a reboot for now. We need indications to reflow to reassign tconts and gems given vcore went away
307 # This may not be necessary when mib resync actually works
308 reactor.callLater(1, self.reboot)
309
310 self.enabled = True
311 else:
312 self.log.info('onu-already-activated')
313
314 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700315 def _init_pon_state(self):
316 self.log.debug('function-entry', deviceId=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500317
318 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500319 self._pon.add_peer(self.parent_id, self._pon_port_number)
320 self.log.debug('adding-pon-port-to-agent', pon=self._pon.get_port())
321
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700322 yield self.core_proxy.port_created(self.device_id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500323
Matt Jeanneret0c287892019-02-28 11:48:00 -0500324 self.log.debug('added-pon-port-to-agent', pon=self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500325
326 # Create and start the OpenOMCI ONU Device Entry for this ONU
327 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500328 self.core_proxy,
329 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500330 support_classes=self.adapter.broadcom_omci,
331 custom_me_map=self.adapter.custom_me_entities())
332 # Port startup
333 if self._pon is not None:
334 self._pon.enabled = True
335
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500336 def delete(self, device):
337 self.log.info('delete-onu', device=device)
338 if self.parent_adapter:
339 try:
340 self.parent_adapter.delete_child_device(self.parent_id, device)
341 except AttributeError:
342 self.log.debug('parent-device-delete-child-not-implemented')
343 else:
344 self.log.debug("parent-adapter-not-available")
345
346 def _create_tconts(self, uni_id, us_scheduler):
347 alloc_id = us_scheduler['alloc_id']
348 q_sched_policy = us_scheduler['q_sched_policy']
349 self.log.debug('create-tcont', us_scheduler=us_scheduler)
350
351 tcontdict = dict()
352 tcontdict['alloc-id'] = alloc_id
353 tcontdict['q_sched_policy'] = q_sched_policy
354 tcontdict['uni_id'] = uni_id
355
356 # TODO: Not sure what to do with any of this...
357 tddata = dict()
358 tddata['name'] = 'not-sure-td-profile'
359 tddata['fixed-bandwidth'] = "not-sure-fixed"
360 tddata['assured-bandwidth'] = "not-sure-assured"
361 tddata['maximum-bandwidth'] = "not-sure-max"
362 tddata['additional-bw-eligibility-indicator'] = "not-sure-additional"
363
364 td = OnuTrafficDescriptor.create(tddata)
365 tcont = OnuTCont.create(self, tcont=tcontdict, td=td)
366
367 self._pon.add_tcont(tcont)
368
369 self.log.debug('pon-add-tcont', tcont=tcont)
370
371 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
372 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
373 self.log.debug('create-gemport',
374 gem_ports=gem_ports, direction=direction)
375
376 for gem_port in gem_ports:
377 gemdict = dict()
378 gemdict['gemport_id'] = gem_port['gemport_id']
379 gemdict['direction'] = direction
380 gemdict['alloc_id_ref'] = alloc_id_ref
381 gemdict['encryption'] = gem_port['aes_encryption']
382 gemdict['discard_config'] = dict()
383 gemdict['discard_config']['max_probability'] = \
384 gem_port['discard_config']['max_probability']
385 gemdict['discard_config']['max_threshold'] = \
386 gem_port['discard_config']['max_threshold']
387 gemdict['discard_config']['min_threshold'] = \
388 gem_port['discard_config']['min_threshold']
389 gemdict['discard_policy'] = gem_port['discard_policy']
390 gemdict['max_q_size'] = gem_port['max_q_size']
391 gemdict['pbit_map'] = gem_port['pbit_map']
392 gemdict['priority_q'] = gem_port['priority_q']
393 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
394 gemdict['weight'] = gem_port['weight']
395 gemdict['uni_id'] = uni_id
396
397 gem_port = OnuGemPort.create(self, gem_port=gemdict)
398
399 self._pon.add_gem_port(gem_port)
400
401 self.log.debug('pon-add-gemport', gem_port=gem_port)
402
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400403 def _execute_queued_vlan_filter_tasks(self, uni_id):
404 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
405 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
406 # or initial-mib-download is not done. Once the tp_task is completed, we execute
407 # such queued vlan-filter tasks
408 try:
409 if uni_id in self._queued_vlan_filter_task:
410 self.log.info("executing-queued-vlan-filter-task",
411 uni_id=uni_id)
412 filter_info = self._queued_vlan_filter_task[uni_id]
413 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
414 uni_id, filter_info.get("uni_port"), filter_info.get("set_vlan_vid"))
415 # Now remove the entry from the dictionary
416 self._queued_vlan_filter_task[uni_id].clear()
417 self.log.debug("executed-queued-vlan-filter-task",
418 uni_id=uni_id)
419 except Exception as e:
420 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
421
422
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500423 def _do_tech_profile_configuration(self, uni_id, tp):
424 num_of_tconts = tp['num_of_tconts']
425 us_scheduler = tp['us_scheduler']
426 alloc_id = us_scheduler['alloc_id']
427 self._create_tconts(uni_id, us_scheduler)
428 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
429 self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
430 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
431 self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
432
433 def load_and_configure_tech_profile(self, uni_id, tp_path):
434 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
435
436 if uni_id not in self._tp_service_specific_task:
437 self._tp_service_specific_task[uni_id] = dict()
438
439 if uni_id not in self._tech_profile_download_done:
440 self._tech_profile_download_done[uni_id] = dict()
441
442 if tp_path not in self._tech_profile_download_done[uni_id]:
443 self._tech_profile_download_done[uni_id][tp_path] = False
444
445 if not self._tech_profile_download_done[uni_id][tp_path]:
446 try:
447 if tp_path in self._tp_service_specific_task[uni_id]:
448 self.log.info("tech-profile-config-already-in-progress",
449 tp_path=tp_path)
450 return
451
452 tp = self.kv_client[tp_path]
453 tp = ast.literal_eval(tp)
454 self.log.debug("tp-instance", tp=tp)
455 self._do_tech_profile_configuration(uni_id, tp)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700456
William Kurkian3a206332019-04-29 11:05:47 -0400457 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500458 def success(_results):
459 self.log.info("tech-profile-config-done-successfully")
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700460 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-success')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500461 if tp_path in self._tp_service_specific_task[uni_id]:
462 del self._tp_service_specific_task[uni_id][tp_path]
463 self._tech_profile_download_done[uni_id][tp_path] = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400464 # Now execute any vlan filter tasks that were queued for later
465 self._execute_queued_vlan_filter_tasks(uni_id)
William Kurkian3a206332019-04-29 11:05:47 -0400466 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500467 def failure(_reason):
468 self.log.warn('tech-profile-config-failure-retrying',
469 _reason=_reason)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700470 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500471 if tp_path in self._tp_service_specific_task[uni_id]:
472 del self._tp_service_specific_task[uni_id][tp_path]
473 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self.load_and_configure_tech_profile,
474 uni_id, tp_path)
475
476 self.log.info('downloading-tech-profile-configuration')
477 self._tp_service_specific_task[uni_id][tp_path] = \
478 BrcmTpServiceSpecificTask(self.omci_agent, self, uni_id)
479 self._deferred = \
480 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
481 self._deferred.addCallbacks(success, failure)
482
483 except Exception as e:
484 self.log.exception("error-loading-tech-profile", e=e)
485 else:
486 self.log.info("tech-profile-config-already-done")
487
488 def update_pm_config(self, device, pm_config):
489 # TODO: This has not been tested
490 self.log.info('update_pm_config', pm_config=pm_config)
491 self.pm_metrics.update(pm_config)
492
493 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
494 # flow decomposition that ultimately comes from onos
495 def update_flow_table(self, device, flows):
496 self.log.debug('function-entry', device=device, flows=flows)
497
498 #
499 # We need to proxy through the OLT to get to the ONU
500 # Configuration from here should be using OMCI
501 #
502 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
503
504 # no point in pushing omci flows if the device isnt reachable
505 if device.connect_status != ConnectStatus.REACHABLE or \
506 device.admin_state != AdminState.ENABLED:
507 self.log.warn("device-disabled-or-offline-skipping-flow-update",
508 admin=device.admin_state, connect=device.connect_status)
509 return
510
511 def is_downstream(port):
512 return port == self._pon_port_number
513
514 def is_upstream(port):
515 return not is_downstream(port)
516
517 for flow in flows:
518 _type = None
519 _port = None
520 _vlan_vid = None
521 _udp_dst = None
522 _udp_src = None
523 _ipv4_dst = None
524 _ipv4_src = None
525 _metadata = None
526 _output = None
527 _push_tpid = None
528 _field = None
529 _set_vlan_vid = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400530 _tunnel_id = None
531
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500532 self.log.debug('bulk-flow-update', device_id=device.id, flow=flow)
533 try:
534 _in_port = fd.get_in_port(flow)
535 assert _in_port is not None
536
537 _out_port = fd.get_out_port(flow) # may be None
538
539 if is_downstream(_in_port):
540 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
541 uni_port = self.uni_port(_out_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400542 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500543 elif is_upstream(_in_port):
544 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
545 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400546 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500547 else:
548 raise Exception('port should be 1 or 2 by our convention')
549
550 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
551
552 for field in fd.get_ofb_fields(flow):
553 if field.type == fd.ETH_TYPE:
554 _type = field.eth_type
555 self.log.debug('field-type-eth-type',
556 eth_type=_type)
557
558 elif field.type == fd.IP_PROTO:
559 _proto = field.ip_proto
560 self.log.debug('field-type-ip-proto',
561 ip_proto=_proto)
562
563 elif field.type == fd.IN_PORT:
564 _port = field.port
565 self.log.debug('field-type-in-port',
566 in_port=_port)
567
568 elif field.type == fd.VLAN_VID:
569 _vlan_vid = field.vlan_vid & 0xfff
570 self.log.debug('field-type-vlan-vid',
571 vlan=_vlan_vid)
572
573 elif field.type == fd.VLAN_PCP:
574 _vlan_pcp = field.vlan_pcp
575 self.log.debug('field-type-vlan-pcp',
576 pcp=_vlan_pcp)
577
578 elif field.type == fd.UDP_DST:
579 _udp_dst = field.udp_dst
580 self.log.debug('field-type-udp-dst',
581 udp_dst=_udp_dst)
582
583 elif field.type == fd.UDP_SRC:
584 _udp_src = field.udp_src
585 self.log.debug('field-type-udp-src',
586 udp_src=_udp_src)
587
588 elif field.type == fd.IPV4_DST:
589 _ipv4_dst = field.ipv4_dst
590 self.log.debug('field-type-ipv4-dst',
591 ipv4_dst=_ipv4_dst)
592
593 elif field.type == fd.IPV4_SRC:
594 _ipv4_src = field.ipv4_src
595 self.log.debug('field-type-ipv4-src',
596 ipv4_dst=_ipv4_src)
597
598 elif field.type == fd.METADATA:
599 _metadata = field.table_metadata
600 self.log.debug('field-type-metadata',
601 metadata=_metadata)
602
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400603 elif field.type == fd.TUNNEL_ID:
604 _tunnel_id = field.tunnel_id
605 self.log.debug('field-type-tunnel-id',
606 tunnel_id=_tunnel_id)
607
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500608 else:
609 raise NotImplementedError('field.type={}'.format(
610 field.type))
611
612 for action in fd.get_actions(flow):
613
614 if action.type == fd.OUTPUT:
615 _output = action.output.port
616 self.log.debug('action-type-output',
617 output=_output, in_port=_in_port)
618
619 elif action.type == fd.POP_VLAN:
620 self.log.debug('action-type-pop-vlan',
621 in_port=_in_port)
622
623 elif action.type == fd.PUSH_VLAN:
624 _push_tpid = action.push.ethertype
625 self.log.debug('action-type-push-vlan',
626 push_tpid=_push_tpid, in_port=_in_port)
627 if action.push.ethertype != 0x8100:
628 self.log.error('unhandled-tpid',
629 ethertype=action.push.ethertype)
630
631 elif action.type == fd.SET_FIELD:
632 _field = action.set_field.field.ofb_field
633 assert (action.set_field.field.oxm_class ==
634 OFPXMC_OPENFLOW_BASIC)
635 self.log.debug('action-type-set-field',
636 field=_field, in_port=_in_port)
637 if _field.type == fd.VLAN_VID:
638 _set_vlan_vid = _field.vlan_vid & 0xfff
639 self.log.debug('set-field-type-vlan-vid',
640 vlan_vid=_set_vlan_vid)
641 else:
642 self.log.error('unsupported-action-set-field-type',
643 field_type=_field.type)
644 else:
645 self.log.error('unsupported-action-type',
646 action_type=action.type, in_port=_in_port)
647
Matt Jeanneret810148b2019-09-29 12:44:01 -0400648 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
649 # supported match and action criteria cannot be handled by omci and must be ignored.
650 if _set_vlan_vid is None or _set_vlan_vid == 0:
651 self.log.warn('ignoring-flow-that-does-not-set-vlanid')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500652 else:
Matt Jeanneret810148b2019-09-29 12:44:01 -0400653 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, set_vlan_vid=_set_vlan_vid)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400654 self._add_vlan_filter_task(device, uni_id, uni_port, _set_vlan_vid)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500655 except Exception as e:
656 self.log.exception('failed-to-install-flow', e=e, flow=flow)
657
658
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400659
660 def _add_vlan_filter_task(self, device,uni_id, uni_port, _set_vlan_vid):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500661 assert uni_port is not None
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400662 if uni_id in self._tech_profile_download_done and self._tech_profile_download_done[uni_id] != {}:
663 @inlineCallbacks
664 def success(_results):
665 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700666 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-pushed')
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400667 self._vlan_filter_task = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500668
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400669 @inlineCallbacks
670 def failure(_reason):
671 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700672 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-failed-retrying')
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400673 self._vlan_filter_task = reactor.callLater(_STARTUP_RETRY_WAIT,
674 self._add_vlan_filter_task, device,uni_port.port_number, uni_port, _set_vlan_vid)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500675
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400676 self.log.info('setting-vlan-tag')
Matt Jeanneret810148b2019-09-29 12:44:01 -0400677 self._vlan_filter_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400678 self._deferred = self._onu_omci_device.task_runner.queue_task(self._vlan_filter_task)
679 self._deferred.addCallbacks(success, failure)
680 else:
681 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
682 uni_id=uni_id)
Matt Jeanneret810148b2019-09-29 12:44:01 -0400683 self._queued_vlan_filter_task[uni_id] = {"device": device,
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400684 "uni_id":uni_id,
685 "uni_port": uni_port,
686 "set_vlan_vid": _set_vlan_vid}
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500687
688 def get_tx_id(self):
689 self.log.debug('function-entry')
690 self.tx_id += 1
691 return self.tx_id
692
Matt Jeannereta32441c2019-03-07 05:16:37 -0500693 def process_inter_adapter_message(self, request):
694 self.log.debug('process-inter-adapter-message', msg=request)
695 try:
696 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
697 omci_msg = InterAdapterOmciMessage()
698 request.body.Unpack(omci_msg)
699 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500700
Matt Jeannereta32441c2019-03-07 05:16:37 -0500701 self.receive_message(omci_msg.message)
702
703 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
704 onu_indication = OnuIndication()
705 request.body.Unpack(onu_indication)
706 self.log.debug('inter-adapter-recv-onu-ind', onu_indication=onu_indication)
707
708 if onu_indication.oper_state == "up":
709 self.create_interface(onu_indication)
710 elif onu_indication.oper_state == "down":
711 self.update_interface(onu_indication)
712 else:
713 self.log.error("unknown-onu-indication", onu_indication=onu_indication)
714
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400715 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
716 tech_msg = InterAdapterTechProfileDownloadMessage()
717 request.body.Unpack(tech_msg)
718 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
719
720 self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
721
Matt Jeannereta32441c2019-03-07 05:16:37 -0500722 else:
723 self.log.error("inter-adapter-unhandled-type", request=request)
724
725 except Exception as e:
726 self.log.exception("error-processing-inter-adapter-message", e=e)
727
728 # Called each time there is an onu "up" indication from the olt handler
729 @inlineCallbacks
730 def create_interface(self, onu_indication):
731 self.log.debug('function-entry', onu_indication=onu_indication)
732 self._onu_indication = onu_indication
733
Matt Jeanneretc083f462019-03-11 15:02:01 -0400734 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
735 connect_status=ConnectStatus.REACHABLE)
736
Matt Jeannereta32441c2019-03-07 05:16:37 -0500737 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500738
739 self.log.debug('starting-openomci-statemachine')
740 self._subscribe_to_events()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500741 onu_device.reason = "starting-openomci"
Mahir Gunyelfe6ac432019-09-04 10:17:14 -0700742 reactor.callLater(1, self._onu_omci_device.start,onu_device)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700743 yield self.core_proxy.device_reason_update(self.device_id, onu_device.reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500744 self._heartbeat.enabled = True
745
746 # Currently called each time there is an onu "down" indication from the olt handler
747 # TODO: possibly other reasons to "update" from the olt?
Matt Jeannereta32441c2019-03-07 05:16:37 -0500748 @inlineCallbacks
749 def update_interface(self, onu_indication):
750 self.log.debug('function-entry', onu_indication=onu_indication)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500751
Matt Jeannereta32441c2019-03-07 05:16:37 -0500752 if onu_indication.oper_state == 'down':
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500753 self.log.debug('stopping-openomci-statemachine')
754 reactor.callLater(0, self._onu_omci_device.stop)
755
756 # Let TP download happen again
757 for uni_id in self._tp_service_specific_task:
758 self._tp_service_specific_task[uni_id].clear()
759 for uni_id in self._tech_profile_download_done:
760 self._tech_profile_download_done[uni_id].clear()
761
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700762 self.disable_ports()
763 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
764 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.DISCOVERED,
765 connect_status=ConnectStatus.UNREACHABLE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500766 else:
767 self.log.debug('not-changing-openomci-statemachine')
768
769 # Not currently called by olt or anything else
William Kurkian3a206332019-04-29 11:05:47 -0400770 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500771 def remove_interface(self, data):
772 self.log.debug('function-entry', data=data)
773
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500774 self.log.debug('stopping-openomci-statemachine')
775 reactor.callLater(0, self._onu_omci_device.stop)
776
777 # Let TP download happen again
778 for uni_id in self._tp_service_specific_task:
779 self._tp_service_specific_task[uni_id].clear()
780 for uni_id in self._tech_profile_download_done:
781 self._tech_profile_download_done[uni_id].clear()
782
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700783 self.disable_ports()
784 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500785
786 # TODO: im sure there is more to do here
787
788 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400789 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500790 def remove_gemport(self, data):
791 self.log.debug('remove-gemport', data=data)
William Kurkian3a206332019-04-29 11:05:47 -0400792 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500793 if device.connect_status != ConnectStatus.REACHABLE:
794 self.log.error('device-unreachable')
795 return
796
797 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400798 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500799 def remove_tcont(self, tcont_data, traffic_descriptor_data):
800 self.log.debug('remove-tcont', tcont_data=tcont_data, traffic_descriptor_data=traffic_descriptor_data)
William Kurkian3a206332019-04-29 11:05:47 -0400801 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500802 if device.connect_status != ConnectStatus.REACHABLE:
803 self.log.error('device-unreachable')
804 return
805
806 # TODO: Create some omci task that encompases this what intended
807
808 # Not currently called. Would be called presumably from the olt handler
809 def create_multicast_gemport(self, data):
810 self.log.debug('function-entry', data=data)
811
812 # TODO: create objects and populate for later omci calls
813
814 def disable(self, device):
815 self.log.debug('function-entry', device=device)
816 try:
817 self.log.info('sending-uni-lock-towards-device', device=device)
818
Matt Jeanneret80766692019-05-03 09:58:38 -0400819 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500820 def stop_anyway(reason):
821 # proceed with disable regardless if we could reach the onu. for example onu is unplugged
822 self.log.debug('stopping-openomci-statemachine')
823 reactor.callLater(0, self._onu_omci_device.stop)
824
825 # Let TP download happen again
826 for uni_id in self._tp_service_specific_task:
827 self._tp_service_specific_task[uni_id].clear()
828 for uni_id in self._tech_profile_download_done:
829 self._tech_profile_download_done[uni_id].clear()
830
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700831 self.disable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500832 device.oper_status = OperStatus.UNKNOWN
833 device.reason = "omci-admin-lock"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400834 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500835
836 # lock all the unis
837 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=True)
838 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
839 self._deferred.addCallbacks(stop_anyway, stop_anyway)
840 except Exception as e:
841 log.exception('exception-in-onu-disable', exception=e)
842
William Kurkian3a206332019-04-29 11:05:47 -0400843 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500844 def reenable(self, device):
845 self.log.debug('function-entry', device=device)
846 try:
847 # Start up OpenOMCI state machines for this device
848 # this will ultimately resync mib and unlock unis on successful redownloading the mib
849 self.log.debug('restarting-openomci-statemachine')
850 self._subscribe_to_events()
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700851 yield self.core_proxy.device_reason_update(self.device_id, "restarting-openomci")
serkant.uluderya2cb65f72019-09-30 14:01:51 -0700852 reactor.callLater(1, self._onu_omci_device.start, device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500853 self._heartbeat.enabled = True
854 except Exception as e:
855 log.exception('exception-in-onu-reenable', exception=e)
856
William Kurkian3a206332019-04-29 11:05:47 -0400857 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500858 def reboot(self):
859 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -0400860 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500861 if device.connect_status != ConnectStatus.REACHABLE:
862 self.log.error("device-unreachable")
863 return
864
William Kurkian3a206332019-04-29 11:05:47 -0400865 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500866 def success(_results):
867 self.log.info('reboot-success', _results=_results)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700868 self.disable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500869 device.connect_status = ConnectStatus.UNREACHABLE
870 device.oper_status = OperStatus.DISCOVERED
871 device.reason = "rebooting"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400872 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500873
874 def failure(_reason):
875 self.log.info('reboot-failure', _reason=_reason)
876
877 self._deferred = self._onu_omci_device.reboot()
878 self._deferred.addCallbacks(success, failure)
879
William Kurkian3a206332019-04-29 11:05:47 -0400880 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700881 def disable_ports(self):
882 self.log.info('disable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500883
884 # Disable all ports on that device
Matt Jeanneret80766692019-05-03 09:58:38 -0400885 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500886
William Kurkian3a206332019-04-29 11:05:47 -0400887 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700888 def enable_ports(self):
889 self.log.info('enable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500890
Matt Jeanneret80766692019-05-03 09:58:38 -0400891 # Enable all ports on that device
892 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.ACTIVE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500893
894 # Called just before openomci state machine is started. These listen for events from selected state machines,
895 # most importantly, mib in sync. Which ultimately leads to downloading the mib
896 def _subscribe_to_events(self):
897 self.log.debug('function-entry')
898
899 # OMCI MIB Database sync status
900 bus = self._onu_omci_device.event_bus
901 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
902 OnuDeviceEvents.MibDatabaseSyncEvent)
903 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
904
905 # OMCI Capabilities
906 bus = self._onu_omci_device.event_bus
907 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
908 OnuDeviceEvents.OmciCapabilitiesEvent)
909 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
910
911 # Called when the mib is in sync
912 def in_sync_handler(self, _topic, msg):
913 self.log.debug('function-entry', _topic=_topic, msg=msg)
914 if self._in_sync_subscription is not None:
915 try:
916 in_sync = msg[IN_SYNC_KEY]
917
918 if in_sync:
919 # Only call this once
920 bus = self._onu_omci_device.event_bus
921 bus.unsubscribe(self._in_sync_subscription)
922 self._in_sync_subscription = None
923
924 # Start up device_info load
925 self.log.debug('running-mib-sync')
926 reactor.callLater(0, self._mib_in_sync)
927
928 except Exception as e:
929 self.log.exception('in-sync', e=e)
930
931 def capabilties_handler(self, _topic, _msg):
932 self.log.debug('function-entry', _topic=_topic, msg=_msg)
933 if self._capabilities_subscription is not None:
934 self.log.debug('capabilities-handler-done')
935
936 # Mib is in sync, we can now query what we learned and actually start pushing ME (download) to the ONU.
937 # Currently uses a basic mib download task that create a bridge with a single gem port and uni, only allowing EAP
938 # Implement your own MibDownloadTask if you wish to setup something different by default
Matt Jeanneretc083f462019-03-11 15:02:01 -0400939 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500940 def _mib_in_sync(self):
941 self.log.debug('function-entry')
942
943 omci = self._onu_omci_device
944 in_sync = omci.mib_db_in_sync
945
Matt Jeanneretc083f462019-03-11 15:02:01 -0400946 device = yield self.core_proxy.get_device(self.device_id)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700947 yield self.core_proxy.device_reason_update(self.device_id, 'discovery-mibsync-complete')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500948
949 if not self._dev_info_loaded:
950 self.log.info('loading-device-data-from-mib', in_sync=in_sync, already_loaded=self._dev_info_loaded)
951
952 omci_dev = self._onu_omci_device
953 config = omci_dev.configuration
954
955 # TODO: run this sooner somehow. shouldnt have to wait for mib sync to push an initial download
956 # In Sync, we can register logical ports now. Ideally this could occur on
957 # the first time we received a successful (no timeout) OMCI Rx response.
958 try:
959
960 # sort the lists so we get consistent port ordering.
961 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
962 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
963 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
964 veip_list = sorted(config.veip_entities) if config.veip_entities else []
965
966 if ani_list is None or (pptp_list is None and veip_list is None):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500967 self.log.warn("no-ani-or-unis")
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700968 yield self.core_proxy.device_reason_update(self.device_id, 'onu-missing-required-elements')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500969 raise Exception("onu-missing-required-elements")
970
971 # Currently logging the ani, pptp, veip, and uni for information purposes.
972 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
973 # And in some ONU the UNI-G list is incomplete or incorrect...
974 for entity_id in ani_list:
975 ani_value = config.ani_g_entities[entity_id]
976 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
977 # TODO: currently only one OLT PON port/ANI, so this works out. With NGPON there will be 2..?
978 self._total_tcont_count = ani_value.get('total-tcont-count')
979 self.log.debug("set-total-tcont-count", tcont_count=self._total_tcont_count)
980
981 for entity_id in uni_list:
982 uni_value = config.uni_g_entities[entity_id]
983 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
984
985 uni_entities = OrderedDict()
986 for entity_id in pptp_list:
987 pptp_value = config.pptp_entities[entity_id]
988 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
989 uni_entities[entity_id] = UniType.PPTP
990
991 for entity_id in veip_list:
992 veip_value = config.veip_entities[entity_id]
993 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
994 uni_entities[entity_id] = UniType.VEIP
995
996 uni_id = 0
997 for entity_id, uni_type in uni_entities.iteritems():
998 try:
Matt Jeanneretc083f462019-03-11 15:02:01 -0400999 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001000 uni_id += 1
1001 except AssertionError as e:
1002 self.log.warn("could not add UNI", entity_id=entity_id, uni_type=uni_type, e=e)
1003
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001004 self._qos_flexibility = config.qos_configuration_flexibility or 0
1005 self._omcc_version = config.omcc_version or OMCCVersion.Unknown
1006
1007 if self._unis:
1008 self._dev_info_loaded = True
1009 else:
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001010 yield self.core_proxy.device_reason_update(self.device_id, 'no-usable-unis')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001011 self.log.warn("no-usable-unis")
1012 raise Exception("no-usable-unis")
1013
1014 except Exception as e:
1015 self.log.exception('device-info-load', e=e)
1016 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1017
1018 else:
1019 self.log.info('device-info-already-loaded', in_sync=in_sync, already_loaded=self._dev_info_loaded)
1020
1021 if self._dev_info_loaded:
Matt Jeanneretad9a0f12019-05-09 14:05:49 -04001022 if device.admin_state == AdminState.PREPROVISIONED or device.admin_state == AdminState.ENABLED:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001023
1024 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001025 def success(_results):
1026 self.log.info('mib-download-success', _results=_results)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001027 yield self.enable_ports()
Matt Jeanneretc083f462019-03-11 15:02:01 -04001028 yield self.core_proxy.device_state_update(device.id,
1029 oper_status=OperStatus.ACTIVE, connect_status=ConnectStatus.REACHABLE)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001030 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-downloaded')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001031 self._mib_download_task = None
Devmalya Paulffc89df2019-07-31 17:43:13 -04001032 yield self.onu_active_event()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001033
Matt Jeanneretc083f462019-03-11 15:02:01 -04001034 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001035 def failure(_reason):
1036 self.log.warn('mib-download-failure-retrying', _reason=_reason)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001037 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001038 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1039
1040 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1041 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1042 self.log.info('downloading-initial-mib-configuration')
1043 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1044 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1045 self._deferred.addCallbacks(success, failure)
1046 else:
1047 self.log.info('admin-down-disabling')
1048 self.disable(device)
1049 else:
1050 self.log.info('device-info-not-loaded-skipping-mib-download')
1051
Matt Jeanneretc083f462019-03-11 15:02:01 -04001052 @inlineCallbacks
1053 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001054 self.log.debug('function-entry')
1055
Matt Jeanneretc083f462019-03-11 15:02:01 -04001056 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 -05001057
1058 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1059 uni_name = "uni-{}".format(uni_no)
1060
1061 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
1062
1063 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 -04001064 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001065
1066 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1067 uni_port.entity_id = entity_id
1068 uni_port.enabled = True
1069 uni_port.mac_bridge_port_num = mac_bridge_port_num
1070
1071 self.log.debug("created-uni-port", uni=uni_port)
1072
Matt Jeanneretc083f462019-03-11 15:02:01 -04001073 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001074
1075 self._unis[uni_port.port_number] = uni_port
1076
1077 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=self._onu_indication.onu_id,
Yongjie Zhang286099c2019-08-06 13:39:07 -04001078 uni_ports=self._unis.values(), serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001079
Matt Jeanneretc083f462019-03-11 15:02:01 -04001080 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1081 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
1082 MAX_PONS_PER_OLT = 16
Mahir Gunyel0e1588a2019-06-27 06:12:47 -07001083 MAX_ONUS_PER_PON = 128
Matt Jeanneretc083f462019-03-11 15:02:01 -04001084 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001085
Matt Jeanneretc083f462019-03-11 15:02:01 -04001086 assert intf_id < MAX_PONS_PER_OLT
1087 assert onu_id < MAX_ONUS_PER_PON
1088 assert uni_id < MAX_UNIS_PER_ONU
Matt Jeanneret3b7db442019-04-22 16:29:48 -04001089 return intf_id << 11 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001090
1091 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001092 def onu_active_event(self):
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001093 self.log.debug('function-entry')
1094 try:
1095 device = yield self.core_proxy.get_device(self.device_id)
1096 parent_device = yield self.core_proxy.get_device(self.parent_id)
1097 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001098 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001099
1100 self.log.debug("onu-indication-context-data",
1101 pon_id=self._onu_indication.intf_id,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001102 onu_id=self._onu_indication.onu_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001103 registration_id=self.device_id,
1104 device_id=self.device_id,
1105 onu_serial_number=device.serial_number,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001106 olt_serial_number=olt_serial_number,
1107 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001108
Devmalya Paulffc89df2019-07-31 17:43:13 -04001109 self.log.debug("Trying-to-raise-onu-active-event")
1110 OnuActiveEvent(self.events, self.device_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001111 self._onu_indication.intf_id,
1112 device.serial_number,
1113 str(self.device_id),
Devmalya Paulffc89df2019-07-31 17:43:13 -04001114 olt_serial_number,raised_ts,
1115 onu_id=self._onu_indication.onu_id).send(True)
1116 except Exception as active_event_error:
1117 self.log.exception('onu-activated-event-error',
1118 errmsg=active_event_error.message)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001119