blob: 6038af4ffcfd8263694304af24b3ebd8bde53d8d [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 #We commented out the line below because it is now being done in openolt-adapter,
234 #in onuDiscovery step. Line can be removed after tests.
235 #yield self.core_proxy.device_state_update(device.id, oper_status=OperStatus.DISCOVERED,
236 # connect_status=ConnectStatus.REACHABLE)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500237
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500238
Mahir Gunyel0e1588a2019-06-27 06:12:47 -0700239 self.log.debug('device updated', device=device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500240
Devmalya Paul7e0be4a2019-05-08 05:18:04 -0400241 yield self._init_pon_state(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500242
Mahir Gunyel0e1588a2019-06-27 06:12:47 -0700243 self.log.debug('pon state initialized', device=device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500244 ############################################################################
Devmalya Paulffc89df2019-07-31 17:43:13 -0400245 # Setup Alarm handler
246 self.events = AdapterEvents(self.core_proxy, device.id, self.logical_device_id,
247 device.serial_number)
248 ############################################################################
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500249 # Setup PM configuration for this device
250 # Pass in ONU specific options
251 kwargs = {
252 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
253 'heartbeat': self.heartbeat,
254 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
255 }
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400256 self.log.debug('create-OnuPmMetrics', serial_number=device.serial_number)
Devmalya Paulffc89df2019-07-31 17:43:13 -0400257 self.pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400258 self.logical_device_id, device.serial_number,
259 grouped=True, freq_override=False, **kwargs)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500260 pm_config = self.pm_metrics.make_proto()
261 self._onu_omci_device.set_pm_config(self.pm_metrics.omci_pm.openomci_interval_pm)
262 self.log.info("initial-pm-config", pm_config=pm_config)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500263 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500264
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500265 # Note, ONU ID and UNI intf set in add_uni_port method
Devmalya Paulffc89df2019-07-31 17:43:13 -0400266 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500267 ani_ports=[self._pon])
aishwaryarana01a98d9fe2019-05-08 12:09:06 -0500268
269 #Start collecting stats from the device after a brief pause
270 reactor.callLater(10, self.pm_metrics.start_collector)
271
onkarkundargiaae99712019-09-23 15:02:52 +0530272 # Code to Run OMCI Test Action
273 kwargs_omci_test_action = {
274 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
275 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
276 }
277 serial_number = device.serial_number
278 test_request = OmciTestRequest(self.core_proxy,
279 self.omci_agent, self.device_id,
280 AniG, serial_number,
281 self.logical_device_id,
282 exclusive=False,
283 **kwargs_omci_test_action)
284 reactor.callLater(60, test_request.start_collector)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500285 self.enabled = True
286 else:
287 self.log.info('onu-already-activated')
288
289 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400290 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500291 def reconcile(self, device):
292 self.log.debug('function-entry', device=device)
293
294 # first we verify that we got parent reference and proxy info
295 assert device.parent_id
296 assert device.proxy_address.device_id
297
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500298 if self.enabled is not True:
299 self.log.info('reconciling-broadcom-onu-device')
300
301 self._init_pon_state(device)
302
303 # need to restart state machines on vcore restart. there is no indication to do it for us.
304 self._onu_omci_device.start()
305 device.reason = "restarting-openomci"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400306 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500307
308 # TODO: this is probably a bit heavy handed
309 # Force a reboot for now. We need indications to reflow to reassign tconts and gems given vcore went away
310 # This may not be necessary when mib resync actually works
311 reactor.callLater(1, self.reboot)
312
313 self.enabled = True
314 else:
315 self.log.info('onu-already-activated')
316
317 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500318 def _init_pon_state(self, device):
319 self.log.debug('function-entry', device=device)
320
321 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500322 self._pon.add_peer(self.parent_id, self._pon_port_number)
323 self.log.debug('adding-pon-port-to-agent', pon=self._pon.get_port())
324
Matt Jeannereta32441c2019-03-07 05:16:37 -0500325 yield self.core_proxy.port_created(device.id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500326
Matt Jeanneret0c287892019-02-28 11:48:00 -0500327 self.log.debug('added-pon-port-to-agent', pon=self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500328
329 # Create and start the OpenOMCI ONU Device Entry for this ONU
330 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500331 self.core_proxy,
332 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500333 support_classes=self.adapter.broadcom_omci,
334 custom_me_map=self.adapter.custom_me_entities())
335 # Port startup
336 if self._pon is not None:
337 self._pon.enabled = True
338
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500339 def delete(self, device):
340 self.log.info('delete-onu', device=device)
341 if self.parent_adapter:
342 try:
343 self.parent_adapter.delete_child_device(self.parent_id, device)
344 except AttributeError:
345 self.log.debug('parent-device-delete-child-not-implemented')
346 else:
347 self.log.debug("parent-adapter-not-available")
348
349 def _create_tconts(self, uni_id, us_scheduler):
350 alloc_id = us_scheduler['alloc_id']
351 q_sched_policy = us_scheduler['q_sched_policy']
352 self.log.debug('create-tcont', us_scheduler=us_scheduler)
353
354 tcontdict = dict()
355 tcontdict['alloc-id'] = alloc_id
356 tcontdict['q_sched_policy'] = q_sched_policy
357 tcontdict['uni_id'] = uni_id
358
359 # TODO: Not sure what to do with any of this...
360 tddata = dict()
361 tddata['name'] = 'not-sure-td-profile'
362 tddata['fixed-bandwidth'] = "not-sure-fixed"
363 tddata['assured-bandwidth'] = "not-sure-assured"
364 tddata['maximum-bandwidth'] = "not-sure-max"
365 tddata['additional-bw-eligibility-indicator'] = "not-sure-additional"
366
367 td = OnuTrafficDescriptor.create(tddata)
368 tcont = OnuTCont.create(self, tcont=tcontdict, td=td)
369
370 self._pon.add_tcont(tcont)
371
372 self.log.debug('pon-add-tcont', tcont=tcont)
373
374 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
375 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
376 self.log.debug('create-gemport',
377 gem_ports=gem_ports, direction=direction)
378
379 for gem_port in gem_ports:
380 gemdict = dict()
381 gemdict['gemport_id'] = gem_port['gemport_id']
382 gemdict['direction'] = direction
383 gemdict['alloc_id_ref'] = alloc_id_ref
384 gemdict['encryption'] = gem_port['aes_encryption']
385 gemdict['discard_config'] = dict()
386 gemdict['discard_config']['max_probability'] = \
387 gem_port['discard_config']['max_probability']
388 gemdict['discard_config']['max_threshold'] = \
389 gem_port['discard_config']['max_threshold']
390 gemdict['discard_config']['min_threshold'] = \
391 gem_port['discard_config']['min_threshold']
392 gemdict['discard_policy'] = gem_port['discard_policy']
393 gemdict['max_q_size'] = gem_port['max_q_size']
394 gemdict['pbit_map'] = gem_port['pbit_map']
395 gemdict['priority_q'] = gem_port['priority_q']
396 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
397 gemdict['weight'] = gem_port['weight']
398 gemdict['uni_id'] = uni_id
399
400 gem_port = OnuGemPort.create(self, gem_port=gemdict)
401
402 self._pon.add_gem_port(gem_port)
403
404 self.log.debug('pon-add-gemport', gem_port=gem_port)
405
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400406 def _execute_queued_vlan_filter_tasks(self, uni_id):
407 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
408 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
409 # or initial-mib-download is not done. Once the tp_task is completed, we execute
410 # such queued vlan-filter tasks
411 try:
412 if uni_id in self._queued_vlan_filter_task:
413 self.log.info("executing-queued-vlan-filter-task",
414 uni_id=uni_id)
415 filter_info = self._queued_vlan_filter_task[uni_id]
416 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
417 uni_id, filter_info.get("uni_port"), filter_info.get("set_vlan_vid"))
418 # Now remove the entry from the dictionary
419 self._queued_vlan_filter_task[uni_id].clear()
420 self.log.debug("executed-queued-vlan-filter-task",
421 uni_id=uni_id)
422 except Exception as e:
423 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
424
425
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500426 def _do_tech_profile_configuration(self, uni_id, tp):
427 num_of_tconts = tp['num_of_tconts']
428 us_scheduler = tp['us_scheduler']
429 alloc_id = us_scheduler['alloc_id']
430 self._create_tconts(uni_id, us_scheduler)
431 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
432 self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
433 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
434 self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
435
436 def load_and_configure_tech_profile(self, uni_id, tp_path):
437 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
438
439 if uni_id not in self._tp_service_specific_task:
440 self._tp_service_specific_task[uni_id] = dict()
441
442 if uni_id not in self._tech_profile_download_done:
443 self._tech_profile_download_done[uni_id] = dict()
444
445 if tp_path not in self._tech_profile_download_done[uni_id]:
446 self._tech_profile_download_done[uni_id][tp_path] = False
447
448 if not self._tech_profile_download_done[uni_id][tp_path]:
449 try:
450 if tp_path in self._tp_service_specific_task[uni_id]:
451 self.log.info("tech-profile-config-already-in-progress",
452 tp_path=tp_path)
453 return
454
455 tp = self.kv_client[tp_path]
456 tp = ast.literal_eval(tp)
457 self.log.debug("tp-instance", tp=tp)
458 self._do_tech_profile_configuration(uni_id, tp)
William Kurkian3a206332019-04-29 11:05:47 -0400459
460 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500461 def success(_results):
462 self.log.info("tech-profile-config-done-successfully")
William Kurkian3a206332019-04-29 11:05:47 -0400463 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500464 device.reason = 'tech-profile-config-download-success'
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400465 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500466 if tp_path in self._tp_service_specific_task[uni_id]:
467 del self._tp_service_specific_task[uni_id][tp_path]
468 self._tech_profile_download_done[uni_id][tp_path] = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400469 # Now execute any vlan filter tasks that were queued for later
470 self._execute_queued_vlan_filter_tasks(uni_id)
William Kurkian3a206332019-04-29 11:05:47 -0400471 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500472 def failure(_reason):
473 self.log.warn('tech-profile-config-failure-retrying',
474 _reason=_reason)
William Kurkian3a206332019-04-29 11:05:47 -0400475 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500476 device.reason = 'tech-profile-config-download-failure-retrying'
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400477 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500478 if tp_path in self._tp_service_specific_task[uni_id]:
479 del self._tp_service_specific_task[uni_id][tp_path]
480 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self.load_and_configure_tech_profile,
481 uni_id, tp_path)
482
483 self.log.info('downloading-tech-profile-configuration')
484 self._tp_service_specific_task[uni_id][tp_path] = \
485 BrcmTpServiceSpecificTask(self.omci_agent, self, uni_id)
486 self._deferred = \
487 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
488 self._deferred.addCallbacks(success, failure)
489
490 except Exception as e:
491 self.log.exception("error-loading-tech-profile", e=e)
492 else:
493 self.log.info("tech-profile-config-already-done")
494
495 def update_pm_config(self, device, pm_config):
496 # TODO: This has not been tested
497 self.log.info('update_pm_config', pm_config=pm_config)
498 self.pm_metrics.update(pm_config)
499
500 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
501 # flow decomposition that ultimately comes from onos
502 def update_flow_table(self, device, flows):
503 self.log.debug('function-entry', device=device, flows=flows)
504
505 #
506 # We need to proxy through the OLT to get to the ONU
507 # Configuration from here should be using OMCI
508 #
509 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
510
511 # no point in pushing omci flows if the device isnt reachable
512 if device.connect_status != ConnectStatus.REACHABLE or \
513 device.admin_state != AdminState.ENABLED:
514 self.log.warn("device-disabled-or-offline-skipping-flow-update",
515 admin=device.admin_state, connect=device.connect_status)
516 return
517
518 def is_downstream(port):
519 return port == self._pon_port_number
520
521 def is_upstream(port):
522 return not is_downstream(port)
523
524 for flow in flows:
525 _type = None
526 _port = None
527 _vlan_vid = None
528 _udp_dst = None
529 _udp_src = None
530 _ipv4_dst = None
531 _ipv4_src = None
532 _metadata = None
533 _output = None
534 _push_tpid = None
535 _field = None
536 _set_vlan_vid = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400537 _tunnel_id = None
538
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500539 self.log.debug('bulk-flow-update', device_id=device.id, flow=flow)
540 try:
541 _in_port = fd.get_in_port(flow)
542 assert _in_port is not None
543
544 _out_port = fd.get_out_port(flow) # may be None
545
546 if is_downstream(_in_port):
547 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
548 uni_port = self.uni_port(_out_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400549 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500550 elif is_upstream(_in_port):
551 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
552 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400553 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500554 else:
555 raise Exception('port should be 1 or 2 by our convention')
556
557 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
558
559 for field in fd.get_ofb_fields(flow):
560 if field.type == fd.ETH_TYPE:
561 _type = field.eth_type
562 self.log.debug('field-type-eth-type',
563 eth_type=_type)
564
565 elif field.type == fd.IP_PROTO:
566 _proto = field.ip_proto
567 self.log.debug('field-type-ip-proto',
568 ip_proto=_proto)
569
570 elif field.type == fd.IN_PORT:
571 _port = field.port
572 self.log.debug('field-type-in-port',
573 in_port=_port)
574
575 elif field.type == fd.VLAN_VID:
576 _vlan_vid = field.vlan_vid & 0xfff
577 self.log.debug('field-type-vlan-vid',
578 vlan=_vlan_vid)
579
580 elif field.type == fd.VLAN_PCP:
581 _vlan_pcp = field.vlan_pcp
582 self.log.debug('field-type-vlan-pcp',
583 pcp=_vlan_pcp)
584
585 elif field.type == fd.UDP_DST:
586 _udp_dst = field.udp_dst
587 self.log.debug('field-type-udp-dst',
588 udp_dst=_udp_dst)
589
590 elif field.type == fd.UDP_SRC:
591 _udp_src = field.udp_src
592 self.log.debug('field-type-udp-src',
593 udp_src=_udp_src)
594
595 elif field.type == fd.IPV4_DST:
596 _ipv4_dst = field.ipv4_dst
597 self.log.debug('field-type-ipv4-dst',
598 ipv4_dst=_ipv4_dst)
599
600 elif field.type == fd.IPV4_SRC:
601 _ipv4_src = field.ipv4_src
602 self.log.debug('field-type-ipv4-src',
603 ipv4_dst=_ipv4_src)
604
605 elif field.type == fd.METADATA:
606 _metadata = field.table_metadata
607 self.log.debug('field-type-metadata',
608 metadata=_metadata)
609
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400610 elif field.type == fd.TUNNEL_ID:
611 _tunnel_id = field.tunnel_id
612 self.log.debug('field-type-tunnel-id',
613 tunnel_id=_tunnel_id)
614
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500615 else:
616 raise NotImplementedError('field.type={}'.format(
617 field.type))
618
619 for action in fd.get_actions(flow):
620
621 if action.type == fd.OUTPUT:
622 _output = action.output.port
623 self.log.debug('action-type-output',
624 output=_output, in_port=_in_port)
625
626 elif action.type == fd.POP_VLAN:
627 self.log.debug('action-type-pop-vlan',
628 in_port=_in_port)
629
630 elif action.type == fd.PUSH_VLAN:
631 _push_tpid = action.push.ethertype
632 self.log.debug('action-type-push-vlan',
633 push_tpid=_push_tpid, in_port=_in_port)
634 if action.push.ethertype != 0x8100:
635 self.log.error('unhandled-tpid',
636 ethertype=action.push.ethertype)
637
638 elif action.type == fd.SET_FIELD:
639 _field = action.set_field.field.ofb_field
640 assert (action.set_field.field.oxm_class ==
641 OFPXMC_OPENFLOW_BASIC)
642 self.log.debug('action-type-set-field',
643 field=_field, in_port=_in_port)
644 if _field.type == fd.VLAN_VID:
645 _set_vlan_vid = _field.vlan_vid & 0xfff
646 self.log.debug('set-field-type-vlan-vid',
647 vlan_vid=_set_vlan_vid)
648 else:
649 self.log.error('unsupported-action-set-field-type',
650 field_type=_field.type)
651 else:
652 self.log.error('unsupported-action-type',
653 action_type=action.type, in_port=_in_port)
654
655 # TODO: We only set vlan omci flows. Handle omci matching ethertypes at some point in another task
656 if _type is not None:
657 self.log.warn('ignoring-flow-with-ethType', ethType=_type)
658 elif _set_vlan_vid is None or _set_vlan_vid == 0:
659 self.log.warn('ignorning-flow-that-does-not-set-vlanid')
660 else:
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400661 self.log.warn('set-vlanid', uni_id=uni_id, uni_port=uni_port, set_vlan_vid=_set_vlan_vid)
662 self._add_vlan_filter_task(device, uni_id, uni_port, _set_vlan_vid)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500663 except Exception as e:
664 self.log.exception('failed-to-install-flow', e=e, flow=flow)
665
666
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400667
668 def _add_vlan_filter_task(self, device,uni_id, uni_port, _set_vlan_vid):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500669 assert uni_port is not None
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400670 if uni_id in self._tech_profile_download_done and self._tech_profile_download_done[uni_id] != {}:
671 @inlineCallbacks
672 def success(_results):
673 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid)
674 device.reason = 'omci-flows-pushed'
675 yield self.core_proxy.device_update(device)
676 self._vlan_filter_task = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500677
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400678 @inlineCallbacks
679 def failure(_reason):
680 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid)
681 device.reason = 'omci-flows-failed-retrying'
682 yield self.core_proxy.device_update(device)
683 self._vlan_filter_task = reactor.callLater(_STARTUP_RETRY_WAIT,
684 self._add_vlan_filter_task, device,uni_port.port_number, uni_port, _set_vlan_vid)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500685
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400686 self.log.info('setting-vlan-tag')
687 self._vlan_filter_task = BrcmVlanFilterTask(self.omci_agent, self.device_id, uni_port, _set_vlan_vid)
688 self._deferred = self._onu_omci_device.task_runner.queue_task(self._vlan_filter_task)
689 self._deferred.addCallbacks(success, failure)
690 else:
691 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
692 uni_id=uni_id)
693 self._queued_vlan_filter_task[uni_id] = {"device": device, \
694 "uni_id":uni_id,
695 "uni_port": uni_port,
696 "set_vlan_vid": _set_vlan_vid}
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500697
698 def get_tx_id(self):
699 self.log.debug('function-entry')
700 self.tx_id += 1
701 return self.tx_id
702
Matt Jeannereta32441c2019-03-07 05:16:37 -0500703 def process_inter_adapter_message(self, request):
704 self.log.debug('process-inter-adapter-message', msg=request)
705 try:
706 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
707 omci_msg = InterAdapterOmciMessage()
708 request.body.Unpack(omci_msg)
709 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500710
Matt Jeannereta32441c2019-03-07 05:16:37 -0500711 self.receive_message(omci_msg.message)
712
713 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
714 onu_indication = OnuIndication()
715 request.body.Unpack(onu_indication)
716 self.log.debug('inter-adapter-recv-onu-ind', onu_indication=onu_indication)
717
718 if onu_indication.oper_state == "up":
719 self.create_interface(onu_indication)
720 elif onu_indication.oper_state == "down":
721 self.update_interface(onu_indication)
722 else:
723 self.log.error("unknown-onu-indication", onu_indication=onu_indication)
724
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400725 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
726 tech_msg = InterAdapterTechProfileDownloadMessage()
727 request.body.Unpack(tech_msg)
728 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
729
730 self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
731
Matt Jeannereta32441c2019-03-07 05:16:37 -0500732 else:
733 self.log.error("inter-adapter-unhandled-type", request=request)
734
735 except Exception as e:
736 self.log.exception("error-processing-inter-adapter-message", e=e)
737
738 # Called each time there is an onu "up" indication from the olt handler
739 @inlineCallbacks
740 def create_interface(self, onu_indication):
741 self.log.debug('function-entry', onu_indication=onu_indication)
742 self._onu_indication = onu_indication
743
Matt Jeanneretc083f462019-03-11 15:02:01 -0400744 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
745 connect_status=ConnectStatus.REACHABLE)
746
Matt Jeannereta32441c2019-03-07 05:16:37 -0500747 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500748
749 self.log.debug('starting-openomci-statemachine')
750 self._subscribe_to_events()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500751 onu_device.reason = "starting-openomci"
Mahir Gunyelfe6ac432019-09-04 10:17:14 -0700752 reactor.callLater(1, self._onu_omci_device.start,onu_device)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500753 yield self.core_proxy.device_update(onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500754 self._heartbeat.enabled = True
755
756 # Currently called each time there is an onu "down" indication from the olt handler
757 # TODO: possibly other reasons to "update" from the olt?
Matt Jeannereta32441c2019-03-07 05:16:37 -0500758 @inlineCallbacks
759 def update_interface(self, onu_indication):
760 self.log.debug('function-entry', onu_indication=onu_indication)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500761
Matt Jeannereta32441c2019-03-07 05:16:37 -0500762 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500763
Matt Jeannereta32441c2019-03-07 05:16:37 -0500764 if onu_indication.oper_state == 'down':
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500765 self.log.debug('stopping-openomci-statemachine')
766 reactor.callLater(0, self._onu_omci_device.stop)
767
768 # Let TP download happen again
769 for uni_id in self._tp_service_specific_task:
770 self._tp_service_specific_task[uni_id].clear()
771 for uni_id in self._tech_profile_download_done:
772 self._tech_profile_download_done[uni_id].clear()
773
774 self.disable_ports(onu_device)
775 onu_device.reason = "stopping-openomci"
Chaitrashree G S01257fa2019-05-24 06:49:49 -0400776 yield self.core_proxy.device_update(onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500777 onu_device.connect_status = ConnectStatus.UNREACHABLE
778 onu_device.oper_status = OperStatus.DISCOVERED
Chaitrashree G S01257fa2019-05-24 06:49:49 -0400779 yield self.core_proxy.device_state_update(self.device_id, onu_device.oper_status,onu_device.connect_status)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500780 else:
781 self.log.debug('not-changing-openomci-statemachine')
782
783 # Not currently called by olt or anything else
William Kurkian3a206332019-04-29 11:05:47 -0400784 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500785 def remove_interface(self, data):
786 self.log.debug('function-entry', data=data)
787
William Kurkian3a206332019-04-29 11:05:47 -0400788 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500789
790 self.log.debug('stopping-openomci-statemachine')
791 reactor.callLater(0, self._onu_omci_device.stop)
792
793 # Let TP download happen again
794 for uni_id in self._tp_service_specific_task:
795 self._tp_service_specific_task[uni_id].clear()
796 for uni_id in self._tech_profile_download_done:
797 self._tech_profile_download_done[uni_id].clear()
798
799 self.disable_ports(onu_device)
800 onu_device.reason = "stopping-openomci"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400801 yield self.core_proxy.device_update(onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500802
803 # TODO: im sure there is more to do here
804
805 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400806 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500807 def remove_gemport(self, data):
808 self.log.debug('remove-gemport', data=data)
William Kurkian3a206332019-04-29 11:05:47 -0400809 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500810 if device.connect_status != ConnectStatus.REACHABLE:
811 self.log.error('device-unreachable')
812 return
813
814 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400815 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500816 def remove_tcont(self, tcont_data, traffic_descriptor_data):
817 self.log.debug('remove-tcont', tcont_data=tcont_data, traffic_descriptor_data=traffic_descriptor_data)
William Kurkian3a206332019-04-29 11:05:47 -0400818 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500819 if device.connect_status != ConnectStatus.REACHABLE:
820 self.log.error('device-unreachable')
821 return
822
823 # TODO: Create some omci task that encompases this what intended
824
825 # Not currently called. Would be called presumably from the olt handler
826 def create_multicast_gemport(self, data):
827 self.log.debug('function-entry', data=data)
828
829 # TODO: create objects and populate for later omci calls
830
831 def disable(self, device):
832 self.log.debug('function-entry', device=device)
833 try:
834 self.log.info('sending-uni-lock-towards-device', device=device)
835
Matt Jeanneret80766692019-05-03 09:58:38 -0400836 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500837 def stop_anyway(reason):
838 # proceed with disable regardless if we could reach the onu. for example onu is unplugged
839 self.log.debug('stopping-openomci-statemachine')
840 reactor.callLater(0, self._onu_omci_device.stop)
841
842 # Let TP download happen again
843 for uni_id in self._tp_service_specific_task:
844 self._tp_service_specific_task[uni_id].clear()
845 for uni_id in self._tech_profile_download_done:
846 self._tech_profile_download_done[uni_id].clear()
847
848 self.disable_ports(device)
849 device.oper_status = OperStatus.UNKNOWN
850 device.reason = "omci-admin-lock"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400851 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500852
853 # lock all the unis
854 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=True)
855 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
856 self._deferred.addCallbacks(stop_anyway, stop_anyway)
857 except Exception as e:
858 log.exception('exception-in-onu-disable', exception=e)
859
William Kurkian3a206332019-04-29 11:05:47 -0400860 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500861 def reenable(self, device):
862 self.log.debug('function-entry', device=device)
863 try:
864 # Start up OpenOMCI state machines for this device
865 # this will ultimately resync mib and unlock unis on successful redownloading the mib
866 self.log.debug('restarting-openomci-statemachine')
867 self._subscribe_to_events()
868 device.reason = "restarting-openomci"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400869 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500870 reactor.callLater(1, self._onu_omci_device.start)
871 self._heartbeat.enabled = True
872 except Exception as e:
873 log.exception('exception-in-onu-reenable', exception=e)
874
William Kurkian3a206332019-04-29 11:05:47 -0400875 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500876 def reboot(self):
877 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -0400878 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500879 if device.connect_status != ConnectStatus.REACHABLE:
880 self.log.error("device-unreachable")
881 return
882
William Kurkian3a206332019-04-29 11:05:47 -0400883 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500884 def success(_results):
885 self.log.info('reboot-success', _results=_results)
886 self.disable_ports(device)
887 device.connect_status = ConnectStatus.UNREACHABLE
888 device.oper_status = OperStatus.DISCOVERED
889 device.reason = "rebooting"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400890 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500891
892 def failure(_reason):
893 self.log.info('reboot-failure', _reason=_reason)
894
895 self._deferred = self._onu_omci_device.reboot()
896 self._deferred.addCallbacks(success, failure)
897
William Kurkian3a206332019-04-29 11:05:47 -0400898 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500899 def disable_ports(self, onu_device):
Matt Jeanneret80766692019-05-03 09:58:38 -0400900 self.log.info('disable-ports', device_id=self.device_id, onu_device=onu_device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500901
902 # Disable all ports on that device
Matt Jeanneret80766692019-05-03 09:58:38 -0400903 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500904
William Kurkian3a206332019-04-29 11:05:47 -0400905 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500906 def enable_ports(self, onu_device):
907 self.log.info('enable-ports', device_id=self.device_id, onu_device=onu_device)
908
Matt Jeanneret80766692019-05-03 09:58:38 -0400909 # Enable all ports on that device
910 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.ACTIVE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500911
912 # Called just before openomci state machine is started. These listen for events from selected state machines,
913 # most importantly, mib in sync. Which ultimately leads to downloading the mib
914 def _subscribe_to_events(self):
915 self.log.debug('function-entry')
916
917 # OMCI MIB Database sync status
918 bus = self._onu_omci_device.event_bus
919 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
920 OnuDeviceEvents.MibDatabaseSyncEvent)
921 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
922
923 # OMCI Capabilities
924 bus = self._onu_omci_device.event_bus
925 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
926 OnuDeviceEvents.OmciCapabilitiesEvent)
927 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
928
929 # Called when the mib is in sync
930 def in_sync_handler(self, _topic, msg):
931 self.log.debug('function-entry', _topic=_topic, msg=msg)
932 if self._in_sync_subscription is not None:
933 try:
934 in_sync = msg[IN_SYNC_KEY]
935
936 if in_sync:
937 # Only call this once
938 bus = self._onu_omci_device.event_bus
939 bus.unsubscribe(self._in_sync_subscription)
940 self._in_sync_subscription = None
941
942 # Start up device_info load
943 self.log.debug('running-mib-sync')
944 reactor.callLater(0, self._mib_in_sync)
945
946 except Exception as e:
947 self.log.exception('in-sync', e=e)
948
949 def capabilties_handler(self, _topic, _msg):
950 self.log.debug('function-entry', _topic=_topic, msg=_msg)
951 if self._capabilities_subscription is not None:
952 self.log.debug('capabilities-handler-done')
953
954 # Mib is in sync, we can now query what we learned and actually start pushing ME (download) to the ONU.
955 # Currently uses a basic mib download task that create a bridge with a single gem port and uni, only allowing EAP
956 # Implement your own MibDownloadTask if you wish to setup something different by default
Matt Jeanneretc083f462019-03-11 15:02:01 -0400957 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500958 def _mib_in_sync(self):
959 self.log.debug('function-entry')
960
961 omci = self._onu_omci_device
962 in_sync = omci.mib_db_in_sync
963
Matt Jeanneretc083f462019-03-11 15:02:01 -0400964 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500965 device.reason = 'discovery-mibsync-complete'
Matt Jeanneretc083f462019-03-11 15:02:01 -0400966 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500967
968 if not self._dev_info_loaded:
969 self.log.info('loading-device-data-from-mib', in_sync=in_sync, already_loaded=self._dev_info_loaded)
970
971 omci_dev = self._onu_omci_device
972 config = omci_dev.configuration
973
974 # TODO: run this sooner somehow. shouldnt have to wait for mib sync to push an initial download
975 # In Sync, we can register logical ports now. Ideally this could occur on
976 # the first time we received a successful (no timeout) OMCI Rx response.
977 try:
978
979 # sort the lists so we get consistent port ordering.
980 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
981 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
982 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
983 veip_list = sorted(config.veip_entities) if config.veip_entities else []
984
985 if ani_list is None or (pptp_list is None and veip_list is None):
986 device.reason = 'onu-missing-required-elements'
987 self.log.warn("no-ani-or-unis")
Matt Jeanneretc083f462019-03-11 15:02:01 -0400988 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500989 raise Exception("onu-missing-required-elements")
990
991 # Currently logging the ani, pptp, veip, and uni for information purposes.
992 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
993 # And in some ONU the UNI-G list is incomplete or incorrect...
994 for entity_id in ani_list:
995 ani_value = config.ani_g_entities[entity_id]
996 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
997 # TODO: currently only one OLT PON port/ANI, so this works out. With NGPON there will be 2..?
998 self._total_tcont_count = ani_value.get('total-tcont-count')
999 self.log.debug("set-total-tcont-count", tcont_count=self._total_tcont_count)
1000
1001 for entity_id in uni_list:
1002 uni_value = config.uni_g_entities[entity_id]
1003 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
1004
1005 uni_entities = OrderedDict()
1006 for entity_id in pptp_list:
1007 pptp_value = config.pptp_entities[entity_id]
1008 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
1009 uni_entities[entity_id] = UniType.PPTP
1010
1011 for entity_id in veip_list:
1012 veip_value = config.veip_entities[entity_id]
1013 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
1014 uni_entities[entity_id] = UniType.VEIP
1015
1016 uni_id = 0
1017 for entity_id, uni_type in uni_entities.iteritems():
1018 try:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001019 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001020 uni_id += 1
1021 except AssertionError as e:
1022 self.log.warn("could not add UNI", entity_id=entity_id, uni_type=uni_type, e=e)
1023
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001024 self._qos_flexibility = config.qos_configuration_flexibility or 0
1025 self._omcc_version = config.omcc_version or OMCCVersion.Unknown
1026
1027 if self._unis:
1028 self._dev_info_loaded = True
1029 else:
1030 device.reason = 'no-usable-unis'
Matt Jeanneretc083f462019-03-11 15:02:01 -04001031 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001032 self.log.warn("no-usable-unis")
1033 raise Exception("no-usable-unis")
1034
1035 except Exception as e:
1036 self.log.exception('device-info-load', e=e)
1037 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1038
1039 else:
1040 self.log.info('device-info-already-loaded', in_sync=in_sync, already_loaded=self._dev_info_loaded)
1041
1042 if self._dev_info_loaded:
Matt Jeanneretad9a0f12019-05-09 14:05:49 -04001043 if device.admin_state == AdminState.PREPROVISIONED or device.admin_state == AdminState.ENABLED:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001044
1045 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001046 def success(_results):
1047 self.log.info('mib-download-success', _results=_results)
Matt Jeanneretc083f462019-03-11 15:02:01 -04001048 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001049 device.reason = 'initial-mib-downloaded'
Chaitrashree G S01257fa2019-05-24 06:49:49 -04001050 yield self.enable_ports(device)
Matt Jeanneretc083f462019-03-11 15:02:01 -04001051 yield self.core_proxy.device_state_update(device.id,
1052 oper_status=OperStatus.ACTIVE, connect_status=ConnectStatus.REACHABLE)
1053 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001054 self._mib_download_task = None
Devmalya Paulffc89df2019-07-31 17:43:13 -04001055 yield self.onu_active_event()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001056
Matt Jeanneretc083f462019-03-11 15:02:01 -04001057 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001058 def failure(_reason):
1059 self.log.warn('mib-download-failure-retrying', _reason=_reason)
1060 device.reason = 'initial-mib-download-failure-retrying'
Matt Jeanneretc083f462019-03-11 15:02:01 -04001061 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001062 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1063
1064 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1065 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1066 self.log.info('downloading-initial-mib-configuration')
1067 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1068 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1069 self._deferred.addCallbacks(success, failure)
1070 else:
1071 self.log.info('admin-down-disabling')
1072 self.disable(device)
1073 else:
1074 self.log.info('device-info-not-loaded-skipping-mib-download')
1075
Matt Jeanneretc083f462019-03-11 15:02:01 -04001076 @inlineCallbacks
1077 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001078 self.log.debug('function-entry')
1079
Matt Jeanneretc083f462019-03-11 15:02:01 -04001080 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 -05001081
1082 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1083 uni_name = "uni-{}".format(uni_no)
1084
1085 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
1086
1087 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 -04001088 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001089
1090 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1091 uni_port.entity_id = entity_id
1092 uni_port.enabled = True
1093 uni_port.mac_bridge_port_num = mac_bridge_port_num
1094
1095 self.log.debug("created-uni-port", uni=uni_port)
1096
Matt Jeanneretc083f462019-03-11 15:02:01 -04001097 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001098
1099 self._unis[uni_port.port_number] = uni_port
1100
1101 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=self._onu_indication.onu_id,
Yongjie Zhang286099c2019-08-06 13:39:07 -04001102 uni_ports=self._unis.values(), serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001103
Matt Jeanneretc083f462019-03-11 15:02:01 -04001104 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1105 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
1106 MAX_PONS_PER_OLT = 16
Mahir Gunyel0e1588a2019-06-27 06:12:47 -07001107 MAX_ONUS_PER_PON = 128
Matt Jeanneretc083f462019-03-11 15:02:01 -04001108 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001109
Matt Jeanneretc083f462019-03-11 15:02:01 -04001110 assert intf_id < MAX_PONS_PER_OLT
1111 assert onu_id < MAX_ONUS_PER_PON
1112 assert uni_id < MAX_UNIS_PER_ONU
Matt Jeanneret3b7db442019-04-22 16:29:48 -04001113 return intf_id << 11 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001114
1115 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001116 def onu_active_event(self):
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001117 self.log.debug('function-entry')
1118 try:
1119 device = yield self.core_proxy.get_device(self.device_id)
1120 parent_device = yield self.core_proxy.get_device(self.parent_id)
1121 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001122 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001123
1124 self.log.debug("onu-indication-context-data",
1125 pon_id=self._onu_indication.intf_id,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001126 onu_id=self._onu_indication.onu_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001127 registration_id=self.device_id,
1128 device_id=self.device_id,
1129 onu_serial_number=device.serial_number,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001130 olt_serial_number=olt_serial_number,
1131 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001132
Devmalya Paulffc89df2019-07-31 17:43:13 -04001133 self.log.debug("Trying-to-raise-onu-active-event")
1134 OnuActiveEvent(self.events, self.device_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001135 self._onu_indication.intf_id,
1136 device.serial_number,
1137 str(self.device_id),
Devmalya Paulffc89df2019-07-31 17:43:13 -04001138 olt_serial_number,raised_ts,
1139 onu_id=self._onu_indication.onu_id).send(True)
1140 except Exception as active_event_error:
1141 self.log.exception('onu-activated-event-error',
1142 errmsg=active_event_error.message)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001143