blob: 6b1cec0c394927baf9bf9be82b42d82f35b3ee12 [file] [log] [blame]
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001#
2# Copyright 2017 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17"""
18Broadcom OpenOMCI OLT/ONU adapter handler.
19"""
20
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050021from __future__ import absolute_import
22import six
Devmalya Paulffc89df2019-07-31 17:43:13 -040023import arrow
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050024import structlog
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050025import json
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050026
27from collections import OrderedDict
28
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050029from twisted.internet import reactor
30from twisted.internet.defer import DeferredQueue, inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050031
32from heartbeat import HeartBeat
Devmalya Paulffc89df2019-07-31 17:43:13 -040033from pyvoltha.adapters.extensions.events.device_events.onu.onu_active_event import OnuActiveEvent
34from pyvoltha.adapters.extensions.events.kpi.onu.onu_pm_metrics import OnuPmMetrics
35from pyvoltha.adapters.extensions.events.kpi.onu.onu_omci_pm import OnuOmciPmMetrics
36from pyvoltha.adapters.extensions.events.adapter_events import AdapterEvents
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050037
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050038import pyvoltha.common.openflow.utils as fd
39from pyvoltha.common.utils.registry import registry
Matteo Scandolod8d73172019-11-26 12:15:15 -070040from pyvoltha.adapters.common.frameio.frameio import hexify
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050041from pyvoltha.common.utils.nethelpers import mac_str_to_tuple
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050042from pyvoltha.common.config.config_backend import ConsulStore
43from pyvoltha.common.config.config_backend import EtcdStore
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050044from voltha_protos.logical_device_pb2 import LogicalPort
William Kurkian8235c1e2019-03-05 12:58:28 -050045from voltha_protos.common_pb2 import OperStatus, ConnectStatus, AdminState
Matt Jeanneretc083f462019-03-11 15:02:01 -040046from 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 -040047from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
48 InterAdapterOmciMessage, PortCapability, InterAdapterTechProfileDownloadMessage
Matt Jeannereta32441c2019-03-07 05:16:37 -050049from voltha_protos.openolt_pb2 import OnuIndication
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050050from pyvoltha.adapters.extensions.omci.onu_configuration import OMCCVersion
51from pyvoltha.adapters.extensions.omci.onu_device_entry import OnuDeviceEvents, \
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050052 OnuDeviceEntry, IN_SYNC_KEY
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050053from omci.brcm_mib_download_task import BrcmMibDownloadTask
54from omci.brcm_tp_service_specific_task import BrcmTpServiceSpecificTask
55from omci.brcm_uni_lock_task import BrcmUniLockTask
56from omci.brcm_vlan_filter_task import BrcmVlanFilterTask
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050057from onu_gem_port import OnuGemPort
58from onu_tcont import OnuTCont
59from pon_port import PonPort
60from uni_port import UniPort, UniType
61from onu_traffic_descriptor import OnuTrafficDescriptor
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050062from pyvoltha.common.tech_profile.tech_profile import TechProfile
onkarkundargiaae99712019-09-23 15:02:52 +053063from pyvoltha.adapters.extensions.omci.tasks.omci_test_request import OmciTestRequest
64from pyvoltha.adapters.extensions.omci.omci_entities import AniG
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050065from pyvoltha.adapters.extensions.omci.omci_defs import EntityOperations, ReasonCodes
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050066
67OP = EntityOperations
68RC = ReasonCodes
69
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050070_STARTUP_RETRY_WAIT = 20
71
72
73class BrcmOpenomciOnuHandler(object):
74
75 def __init__(self, adapter, device_id):
76 self.log = structlog.get_logger(device_id=device_id)
Matteo Scandolod8d73172019-11-26 12:15:15 -070077 self.log.debug('BrcmOpenomciOnuHandler')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050078 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050079 self.core_proxy = adapter.core_proxy
80 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050081 self.parent_adapter = None
82 self.parent_id = None
83 self.device_id = device_id
84 self.incoming_messages = DeferredQueue()
85 self.event_messages = DeferredQueue()
86 self.proxy_address = None
87 self.tx_id = 0
88 self._enabled = False
Devmalya Paulffc89df2019-07-31 17:43:13 -040089 self.events = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050090 self.pm_metrics = None
91 self._omcc_version = OMCCVersion.Unknown
92 self._total_tcont_count = 0 # From ANI-G ME
93 self._qos_flexibility = 0 # From ONT2_G ME
94
95 self._onu_indication = None
96 self._unis = dict() # Port # -> UniPort
97
98 self._pon = None
99 # TODO: probably shouldnt be hardcoded, determine from olt maybe?
100 self._pon_port_number = 100
101 self.logical_device_id = None
102
103 self._heartbeat = HeartBeat.create(self, device_id)
104
105 # Set up OpenOMCI environment
106 self._onu_omci_device = None
107 self._dev_info_loaded = False
108 self._deferred = None
109
110 self._in_sync_subscription = None
111 self._connectivity_subscription = None
112 self._capabilities_subscription = None
113
114 self.mac_bridge_service_profile_entity_id = 0x201
115 self.gal_enet_profile_entity_id = 0x1
116
117 self._tp_service_specific_task = dict()
118 self._tech_profile_download_done = dict()
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400119 # Stores information related to queued vlan filter tasks
120 # Dictionary with key being uni_id and value being device,uni port ,uni id and vlan id
121
122 self._queued_vlan_filter_task = dict()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500123
124 # Initialize KV store client
125 self.args = registry('main').get_args()
126 if self.args.backend == 'etcd':
127 host, port = self.args.etcd.split(':', 1)
128 self.kv_client = EtcdStore(host, port,
129 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
130 elif self.args.backend == 'consul':
131 host, port = self.args.consul.split(':', 1)
132 self.kv_client = ConsulStore(host, port,
133 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
134 else:
135 self.log.error('Invalid-backend')
136 raise Exception("Invalid-backend-for-kv-store")
137
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500138 @property
139 def enabled(self):
140 return self._enabled
141
142 @enabled.setter
143 def enabled(self, value):
144 if self._enabled != value:
145 self._enabled = value
146
147 @property
148 def omci_agent(self):
149 return self.adapter.omci_agent
150
151 @property
152 def omci_cc(self):
153 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
154
155 @property
156 def heartbeat(self):
157 return self._heartbeat
158
159 @property
160 def uni_ports(self):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500161 return list(self._unis.values())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500162
163 def uni_port(self, port_no_or_name):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500164 if isinstance(port_no_or_name, six.string_types):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500165 return next((uni for uni in self.uni_ports
166 if uni.name == port_no_or_name), None)
167
168 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
169 return next((uni for uni in self.uni_ports
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400170 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500171
172 @property
173 def pon_port(self):
174 return self._pon
175
176 def receive_message(self, msg):
177 if self.omci_cc is not None:
178 self.omci_cc.receive_message(msg)
179
Matt Jeanneretc083f462019-03-11 15:02:01 -0400180 def get_ofp_port_info(self, device, port_no):
181 self.log.info('get_ofp_port_info', port_no=port_no, device_id=device.id)
182 cap = OFPPF_1GB_FD | OFPPF_FIBER
183
184 hw_addr=mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
185 ((device.parent_port_no >> 8 & 0xff),
186 device.parent_port_no & 0xff,
187 (port_no >> 16) & 0xff,
188 (port_no >> 8) & 0xff,
189 port_no & 0xff))
190
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400191 uni_port = self.uni_port(int(port_no))
192 name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
193 self.log.debug('ofp_port_name', port_no=port_no, name=name)
194
Matt Jeanneretc083f462019-03-11 15:02:01 -0400195 return PortCapability(
196 port=LogicalPort(
197 ofp_port=ofp_port(
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400198 name=name,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400199 hw_addr=hw_addr,
200 config=0,
201 state=OFPPS_LIVE,
202 curr=cap,
203 advertised=cap,
204 peer=cap,
205 curr_speed=OFPPF_1GB_FD,
206 max_speed=OFPPF_1GB_FD
207 ),
208 device_id=device.id,
209 device_port_no=port_no
210 )
211 )
212
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500213 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500214 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500215 def activate(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700216 self.log.debug('activate-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500217
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500218 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500219 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500220 assert device.proxy_address.device_id
221
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500222 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500223 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500224 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500225 if self.enabled is not True:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700226 self.log.info('activating-new-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500227 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500228 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500229 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500230 device.reason = 'activating-onu'
231
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500232 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400233 # pm_metrics requires a logical device id. For now set to just device_id
234 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500235
Matt Jeannereta32441c2019-03-07 05:16:37 -0500236 yield self.core_proxy.device_update(device)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700237 self.log.debug('device updated', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500238
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700239 yield self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500240
Matteo Scandolod8d73172019-11-26 12:15:15 -0700241 self.log.debug('pon state initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500242 ############################################################################
Devmalya Paulffc89df2019-07-31 17:43:13 -0400243 # Setup Alarm handler
244 self.events = AdapterEvents(self.core_proxy, device.id, self.logical_device_id,
245 device.serial_number)
246 ############################################################################
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500247 # Setup PM configuration for this device
248 # Pass in ONU specific options
249 kwargs = {
250 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
251 'heartbeat': self.heartbeat,
252 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
253 }
Matteo Scandolod8d73172019-11-26 12:15:15 -0700254 self.log.debug('create-OnuPmMetrics', device_id=device.id, serial_number=device.serial_number)
Devmalya Paulffc89df2019-07-31 17:43:13 -0400255 self.pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400256 self.logical_device_id, device.serial_number,
257 grouped=True, freq_override=False, **kwargs)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500258 pm_config = self.pm_metrics.make_proto()
259 self._onu_omci_device.set_pm_config(self.pm_metrics.omci_pm.openomci_interval_pm)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700260 self.log.info("initial-pm-config",device_id=device.id, serial_number=device.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500261 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500262
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500263 # Note, ONU ID and UNI intf set in add_uni_port method
Devmalya Paulffc89df2019-07-31 17:43:13 -0400264 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500265 ani_ports=[self._pon])
aishwaryarana01a98d9fe2019-05-08 12:09:06 -0500266
267 #Start collecting stats from the device after a brief pause
268 reactor.callLater(10, self.pm_metrics.start_collector)
269
onkarkundargiaae99712019-09-23 15:02:52 +0530270 # Code to Run OMCI Test Action
271 kwargs_omci_test_action = {
272 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
273 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
274 }
275 serial_number = device.serial_number
276 test_request = OmciTestRequest(self.core_proxy,
277 self.omci_agent, self.device_id,
278 AniG, serial_number,
279 self.logical_device_id,
280 exclusive=False,
281 **kwargs_omci_test_action)
282 reactor.callLater(60, test_request.start_collector)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500283 self.enabled = True
284 else:
285 self.log.info('onu-already-activated')
286
287 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400288 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500289 def reconcile(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700290 self.log.debug('reconcile-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500291
292 # first we verify that we got parent reference and proxy info
293 assert device.parent_id
294 assert device.proxy_address.device_id
295
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700296 self.proxy_address = device.proxy_address
297 self.parent_id = device.parent_id
298 self._pon_port_number = device.parent_port_no
299
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500300 if self.enabled is not True:
301 self.log.info('reconciling-broadcom-onu-device')
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700302 self.logical_device_id = self.device_id
303 self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500304
305 # need to restart state machines on vcore restart. there is no indication to do it for us.
306 self._onu_omci_device.start()
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700307 yield self.core_proxy.device_reason_update(self.device_id, "restarting-openomci")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500308
309 # TODO: this is probably a bit heavy handed
310 # Force a reboot for now. We need indications to reflow to reassign tconts and gems given vcore went away
311 # This may not be necessary when mib resync actually works
312 reactor.callLater(1, self.reboot)
313
314 self.enabled = True
315 else:
316 self.log.info('onu-already-activated')
317
318 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700319 def _init_pon_state(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700320 self.log.debug('_init-pon-state', device_id=self.device_id, device_logical_id=self.logical_device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500321
322 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500323 self._pon.add_peer(self.parent_id, self._pon_port_number)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700324 self.log.debug('adding-pon-port-to-agent',
325 type=self._pon.get_port().type,
326 admin_state=self._pon.get_port().admin_state,
327 oper_status=self._pon.get_port().oper_status,
328 )
Matt Jeanneret0c287892019-02-28 11:48:00 -0500329
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700330 yield self.core_proxy.port_created(self.device_id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500331
Matteo Scandolod8d73172019-11-26 12:15:15 -0700332 self.log.debug('added-pon-port-to-agent',
333 type=self._pon.get_port().type,
334 admin_state=self._pon.get_port().admin_state,
335 oper_status=self._pon.get_port().oper_status,
336 )
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500337
338 # Create and start the OpenOMCI ONU Device Entry for this ONU
339 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500340 self.core_proxy,
341 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500342 support_classes=self.adapter.broadcom_omci,
343 custom_me_map=self.adapter.custom_me_entities())
344 # Port startup
345 if self._pon is not None:
346 self._pon.enabled = True
347
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500348 def delete(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700349 self.log.info('delete-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500350 if self.parent_adapter:
351 try:
352 self.parent_adapter.delete_child_device(self.parent_id, device)
353 except AttributeError:
354 self.log.debug('parent-device-delete-child-not-implemented')
355 else:
356 self.log.debug("parent-adapter-not-available")
357
358 def _create_tconts(self, uni_id, us_scheduler):
359 alloc_id = us_scheduler['alloc_id']
360 q_sched_policy = us_scheduler['q_sched_policy']
361 self.log.debug('create-tcont', us_scheduler=us_scheduler)
362
363 tcontdict = dict()
364 tcontdict['alloc-id'] = alloc_id
365 tcontdict['q_sched_policy'] = q_sched_policy
366 tcontdict['uni_id'] = uni_id
367
368 # TODO: Not sure what to do with any of this...
369 tddata = dict()
370 tddata['name'] = 'not-sure-td-profile'
371 tddata['fixed-bandwidth'] = "not-sure-fixed"
372 tddata['assured-bandwidth'] = "not-sure-assured"
373 tddata['maximum-bandwidth'] = "not-sure-max"
374 tddata['additional-bw-eligibility-indicator'] = "not-sure-additional"
375
376 td = OnuTrafficDescriptor.create(tddata)
377 tcont = OnuTCont.create(self, tcont=tcontdict, td=td)
378
379 self._pon.add_tcont(tcont)
380
381 self.log.debug('pon-add-tcont', tcont=tcont)
382
383 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
384 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
385 self.log.debug('create-gemport',
386 gem_ports=gem_ports, direction=direction)
387
388 for gem_port in gem_ports:
389 gemdict = dict()
390 gemdict['gemport_id'] = gem_port['gemport_id']
391 gemdict['direction'] = direction
392 gemdict['alloc_id_ref'] = alloc_id_ref
393 gemdict['encryption'] = gem_port['aes_encryption']
394 gemdict['discard_config'] = dict()
395 gemdict['discard_config']['max_probability'] = \
396 gem_port['discard_config']['max_probability']
397 gemdict['discard_config']['max_threshold'] = \
398 gem_port['discard_config']['max_threshold']
399 gemdict['discard_config']['min_threshold'] = \
400 gem_port['discard_config']['min_threshold']
401 gemdict['discard_policy'] = gem_port['discard_policy']
402 gemdict['max_q_size'] = gem_port['max_q_size']
403 gemdict['pbit_map'] = gem_port['pbit_map']
404 gemdict['priority_q'] = gem_port['priority_q']
405 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
406 gemdict['weight'] = gem_port['weight']
407 gemdict['uni_id'] = uni_id
408
409 gem_port = OnuGemPort.create(self, gem_port=gemdict)
410
411 self._pon.add_gem_port(gem_port)
412
413 self.log.debug('pon-add-gemport', gem_port=gem_port)
414
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400415 def _execute_queued_vlan_filter_tasks(self, uni_id):
416 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
417 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
418 # or initial-mib-download is not done. Once the tp_task is completed, we execute
419 # such queued vlan-filter tasks
420 try:
421 if uni_id in self._queued_vlan_filter_task:
422 self.log.info("executing-queued-vlan-filter-task",
423 uni_id=uni_id)
424 filter_info = self._queued_vlan_filter_task[uni_id]
425 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
426 uni_id, filter_info.get("uni_port"), filter_info.get("set_vlan_vid"))
427 # Now remove the entry from the dictionary
428 self._queued_vlan_filter_task[uni_id].clear()
429 self.log.debug("executed-queued-vlan-filter-task",
430 uni_id=uni_id)
431 except Exception as e:
432 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
433
434
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500435 def _do_tech_profile_configuration(self, uni_id, tp):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500436 us_scheduler = tp['us_scheduler']
437 alloc_id = us_scheduler['alloc_id']
438 self._create_tconts(uni_id, us_scheduler)
439 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
440 self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
441 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
442 self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
443
444 def load_and_configure_tech_profile(self, uni_id, tp_path):
445 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
446
447 if uni_id not in self._tp_service_specific_task:
448 self._tp_service_specific_task[uni_id] = dict()
449
450 if uni_id not in self._tech_profile_download_done:
451 self._tech_profile_download_done[uni_id] = dict()
452
453 if tp_path not in self._tech_profile_download_done[uni_id]:
454 self._tech_profile_download_done[uni_id][tp_path] = False
455
456 if not self._tech_profile_download_done[uni_id][tp_path]:
457 try:
458 if tp_path in self._tp_service_specific_task[uni_id]:
459 self.log.info("tech-profile-config-already-in-progress",
460 tp_path=tp_path)
461 return
462
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500463 tpstored = self.kv_client[tp_path]
464 tpstring = tpstored.decode('ascii')
465 tp = json.loads(tpstring)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500466 self.log.debug("tp-instance", tp=tp)
467 self._do_tech_profile_configuration(uni_id, tp)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700468
William Kurkian3a206332019-04-29 11:05:47 -0400469 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500470 def success(_results):
471 self.log.info("tech-profile-config-done-successfully")
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700472 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-success')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500473 if tp_path in self._tp_service_specific_task[uni_id]:
474 del self._tp_service_specific_task[uni_id][tp_path]
475 self._tech_profile_download_done[uni_id][tp_path] = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400476 # Now execute any vlan filter tasks that were queued for later
477 self._execute_queued_vlan_filter_tasks(uni_id)
William Kurkian3a206332019-04-29 11:05:47 -0400478 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500479 def failure(_reason):
480 self.log.warn('tech-profile-config-failure-retrying',
481 _reason=_reason)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700482 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500483 if tp_path in self._tp_service_specific_task[uni_id]:
484 del self._tp_service_specific_task[uni_id][tp_path]
485 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self.load_and_configure_tech_profile,
486 uni_id, tp_path)
487
488 self.log.info('downloading-tech-profile-configuration')
489 self._tp_service_specific_task[uni_id][tp_path] = \
490 BrcmTpServiceSpecificTask(self.omci_agent, self, uni_id)
491 self._deferred = \
492 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
493 self._deferred.addCallbacks(success, failure)
494
495 except Exception as e:
496 self.log.exception("error-loading-tech-profile", e=e)
497 else:
498 self.log.info("tech-profile-config-already-done")
499
500 def update_pm_config(self, device, pm_config):
501 # TODO: This has not been tested
502 self.log.info('update_pm_config', pm_config=pm_config)
503 self.pm_metrics.update(pm_config)
504
505 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
506 # flow decomposition that ultimately comes from onos
507 def update_flow_table(self, device, flows):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700508 self.log.debug('update-flow-table', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500509
510 #
511 # We need to proxy through the OLT to get to the ONU
512 # Configuration from here should be using OMCI
513 #
514 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
515
516 # no point in pushing omci flows if the device isnt reachable
517 if device.connect_status != ConnectStatus.REACHABLE or \
518 device.admin_state != AdminState.ENABLED:
519 self.log.warn("device-disabled-or-offline-skipping-flow-update",
520 admin=device.admin_state, connect=device.connect_status)
521 return
522
523 def is_downstream(port):
524 return port == self._pon_port_number
525
526 def is_upstream(port):
527 return not is_downstream(port)
528
529 for flow in flows:
530 _type = None
531 _port = None
532 _vlan_vid = None
533 _udp_dst = None
534 _udp_src = None
535 _ipv4_dst = None
536 _ipv4_src = None
537 _metadata = None
538 _output = None
539 _push_tpid = None
540 _field = None
541 _set_vlan_vid = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400542 _tunnel_id = None
543
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500544 self.log.debug('bulk-flow-update', device_id=device.id, flow=flow)
545 try:
546 _in_port = fd.get_in_port(flow)
547 assert _in_port is not None
548
549 _out_port = fd.get_out_port(flow) # may be None
550
551 if is_downstream(_in_port):
552 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
553 uni_port = self.uni_port(_out_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400554 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500555 elif is_upstream(_in_port):
556 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
557 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400558 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500559 else:
560 raise Exception('port should be 1 or 2 by our convention')
561
562 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
563
564 for field in fd.get_ofb_fields(flow):
565 if field.type == fd.ETH_TYPE:
566 _type = field.eth_type
567 self.log.debug('field-type-eth-type',
568 eth_type=_type)
569
570 elif field.type == fd.IP_PROTO:
571 _proto = field.ip_proto
572 self.log.debug('field-type-ip-proto',
573 ip_proto=_proto)
574
575 elif field.type == fd.IN_PORT:
576 _port = field.port
577 self.log.debug('field-type-in-port',
578 in_port=_port)
579
580 elif field.type == fd.VLAN_VID:
581 _vlan_vid = field.vlan_vid & 0xfff
582 self.log.debug('field-type-vlan-vid',
583 vlan=_vlan_vid)
584
585 elif field.type == fd.VLAN_PCP:
586 _vlan_pcp = field.vlan_pcp
587 self.log.debug('field-type-vlan-pcp',
588 pcp=_vlan_pcp)
589
590 elif field.type == fd.UDP_DST:
591 _udp_dst = field.udp_dst
592 self.log.debug('field-type-udp-dst',
593 udp_dst=_udp_dst)
594
595 elif field.type == fd.UDP_SRC:
596 _udp_src = field.udp_src
597 self.log.debug('field-type-udp-src',
598 udp_src=_udp_src)
599
600 elif field.type == fd.IPV4_DST:
601 _ipv4_dst = field.ipv4_dst
602 self.log.debug('field-type-ipv4-dst',
603 ipv4_dst=_ipv4_dst)
604
605 elif field.type == fd.IPV4_SRC:
606 _ipv4_src = field.ipv4_src
607 self.log.debug('field-type-ipv4-src',
608 ipv4_dst=_ipv4_src)
609
610 elif field.type == fd.METADATA:
611 _metadata = field.table_metadata
612 self.log.debug('field-type-metadata',
613 metadata=_metadata)
614
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400615 elif field.type == fd.TUNNEL_ID:
616 _tunnel_id = field.tunnel_id
617 self.log.debug('field-type-tunnel-id',
618 tunnel_id=_tunnel_id)
619
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500620 else:
621 raise NotImplementedError('field.type={}'.format(
622 field.type))
623
624 for action in fd.get_actions(flow):
625
626 if action.type == fd.OUTPUT:
627 _output = action.output.port
628 self.log.debug('action-type-output',
629 output=_output, in_port=_in_port)
630
631 elif action.type == fd.POP_VLAN:
632 self.log.debug('action-type-pop-vlan',
633 in_port=_in_port)
634
635 elif action.type == fd.PUSH_VLAN:
636 _push_tpid = action.push.ethertype
637 self.log.debug('action-type-push-vlan',
638 push_tpid=_push_tpid, in_port=_in_port)
639 if action.push.ethertype != 0x8100:
640 self.log.error('unhandled-tpid',
641 ethertype=action.push.ethertype)
642
643 elif action.type == fd.SET_FIELD:
644 _field = action.set_field.field.ofb_field
645 assert (action.set_field.field.oxm_class ==
646 OFPXMC_OPENFLOW_BASIC)
647 self.log.debug('action-type-set-field',
648 field=_field, in_port=_in_port)
649 if _field.type == fd.VLAN_VID:
650 _set_vlan_vid = _field.vlan_vid & 0xfff
651 self.log.debug('set-field-type-vlan-vid',
652 vlan_vid=_set_vlan_vid)
653 else:
654 self.log.error('unsupported-action-set-field-type',
655 field_type=_field.type)
656 else:
657 self.log.error('unsupported-action-type',
658 action_type=action.type, in_port=_in_port)
659
Matt Jeanneret810148b2019-09-29 12:44:01 -0400660 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
661 # supported match and action criteria cannot be handled by omci and must be ignored.
662 if _set_vlan_vid is None or _set_vlan_vid == 0:
663 self.log.warn('ignoring-flow-that-does-not-set-vlanid')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500664 else:
Matt Jeanneret810148b2019-09-29 12:44:01 -0400665 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 -0400666 self._add_vlan_filter_task(device, uni_id, uni_port, _set_vlan_vid)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500667 except Exception as e:
668 self.log.exception('failed-to-install-flow', e=e, flow=flow)
669
670
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400671
672 def _add_vlan_filter_task(self, device,uni_id, uni_port, _set_vlan_vid):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500673 assert uni_port is not None
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400674 if uni_id in self._tech_profile_download_done and self._tech_profile_download_done[uni_id] != {}:
675 @inlineCallbacks
676 def success(_results):
677 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700678 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-pushed')
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400679 self._vlan_filter_task = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500680
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400681 @inlineCallbacks
682 def failure(_reason):
683 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700684 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-failed-retrying')
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400685 self._vlan_filter_task = reactor.callLater(_STARTUP_RETRY_WAIT,
686 self._add_vlan_filter_task, device,uni_port.port_number, uni_port, _set_vlan_vid)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500687
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400688 self.log.info('setting-vlan-tag')
Matt Jeanneret810148b2019-09-29 12:44:01 -0400689 self._vlan_filter_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400690 self._deferred = self._onu_omci_device.task_runner.queue_task(self._vlan_filter_task)
691 self._deferred.addCallbacks(success, failure)
692 else:
693 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
694 uni_id=uni_id)
Matt Jeanneret810148b2019-09-29 12:44:01 -0400695 self._queued_vlan_filter_task[uni_id] = {"device": device,
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400696 "uni_id":uni_id,
697 "uni_port": uni_port,
698 "set_vlan_vid": _set_vlan_vid}
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500699
700 def get_tx_id(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700701 self.log.debug('get-tx-id')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500702 self.tx_id += 1
703 return self.tx_id
704
Matt Jeannereta32441c2019-03-07 05:16:37 -0500705 def process_inter_adapter_message(self, request):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700706 self.log.debug('process-inter-adapter-message', type=request.header.type, from_topic=request.header.from_topic,
707 to_topic=request.header.to_topic, to_device_id=request.header.to_device_id)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500708 try:
709 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
710 omci_msg = InterAdapterOmciMessage()
711 request.body.Unpack(omci_msg)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700712 self.log.debug('inter-adapter-recv-omci', omci_msg=hexify(omci_msg.message))
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500713
Matt Jeannereta32441c2019-03-07 05:16:37 -0500714 self.receive_message(omci_msg.message)
715
716 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
717 onu_indication = OnuIndication()
718 request.body.Unpack(onu_indication)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700719 self.log.debug('inter-adapter-recv-onu-ind', onu_id=onu_indication.onu_id,
720 oper_state=onu_indication.oper_state, admin_state=onu_indication.admin_state,
721 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500722
723 if onu_indication.oper_state == "up":
724 self.create_interface(onu_indication)
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -0400725 elif onu_indication.oper_state == "down" or onu_indication.oper_state=="unreachable":
Matt Jeannereta32441c2019-03-07 05:16:37 -0500726 self.update_interface(onu_indication)
727 else:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700728 self.log.error("unknown-onu-indication", onu_id=onu_indication.onu_id,
729 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500730
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400731 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
732 tech_msg = InterAdapterTechProfileDownloadMessage()
733 request.body.Unpack(tech_msg)
734 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
735
736 self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
737
Matt Jeannereta32441c2019-03-07 05:16:37 -0500738 else:
739 self.log.error("inter-adapter-unhandled-type", request=request)
740
741 except Exception as e:
742 self.log.exception("error-processing-inter-adapter-message", e=e)
743
744 # Called each time there is an onu "up" indication from the olt handler
745 @inlineCallbacks
746 def create_interface(self, onu_indication):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700747 self.log.debug('create-interface', onu_id=onu_indication.onu_id,
748 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500749 self._onu_indication = onu_indication
750
Matt Jeanneretc083f462019-03-11 15:02:01 -0400751 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
752 connect_status=ConnectStatus.REACHABLE)
753
Matt Jeannereta32441c2019-03-07 05:16:37 -0500754 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500755
756 self.log.debug('starting-openomci-statemachine')
757 self._subscribe_to_events()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500758 onu_device.reason = "starting-openomci"
Mahir Gunyelfe6ac432019-09-04 10:17:14 -0700759 reactor.callLater(1, self._onu_omci_device.start,onu_device)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700760 yield self.core_proxy.device_reason_update(self.device_id, onu_device.reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500761 self._heartbeat.enabled = True
762
763 # Currently called each time there is an onu "down" indication from the olt handler
764 # TODO: possibly other reasons to "update" from the olt?
Matt Jeannereta32441c2019-03-07 05:16:37 -0500765 @inlineCallbacks
766 def update_interface(self, onu_indication):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700767 self.log.debug('update-interface', onu_id=onu_indication.onu_id,
768 serial_number=onu_indication.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500769
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -0400770 if onu_indication.oper_state == 'down' or onu_indication.oper_state == "unreachable":
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500771 self.log.debug('stopping-openomci-statemachine')
772 reactor.callLater(0, self._onu_omci_device.stop)
773
774 # Let TP download happen again
775 for uni_id in self._tp_service_specific_task:
776 self._tp_service_specific_task[uni_id].clear()
777 for uni_id in self._tech_profile_download_done:
778 self._tech_profile_download_done[uni_id].clear()
779
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700780 self.disable_ports()
781 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
782 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.DISCOVERED,
783 connect_status=ConnectStatus.UNREACHABLE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500784 else:
785 self.log.debug('not-changing-openomci-statemachine')
786
787 # Not currently called by olt or anything else
William Kurkian3a206332019-04-29 11:05:47 -0400788 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500789 def remove_interface(self, data):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700790 self.log.debug('remove-interface', data=data)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500791
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500792 self.log.debug('stopping-openomci-statemachine')
793 reactor.callLater(0, self._onu_omci_device.stop)
794
795 # Let TP download happen again
796 for uni_id in self._tp_service_specific_task:
797 self._tp_service_specific_task[uni_id].clear()
798 for uni_id in self._tech_profile_download_done:
799 self._tech_profile_download_done[uni_id].clear()
800
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700801 self.disable_ports()
802 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500803
804 # TODO: im sure there is more to do here
805
806 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400807 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500808 def remove_gemport(self, data):
809 self.log.debug('remove-gemport', data=data)
William Kurkian3a206332019-04-29 11:05:47 -0400810 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500811 if device.connect_status != ConnectStatus.REACHABLE:
812 self.log.error('device-unreachable')
813 return
814
815 # Not currently called. Would be called presumably from the olt handler
William Kurkian3a206332019-04-29 11:05:47 -0400816 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500817 def remove_tcont(self, tcont_data, traffic_descriptor_data):
818 self.log.debug('remove-tcont', tcont_data=tcont_data, traffic_descriptor_data=traffic_descriptor_data)
William Kurkian3a206332019-04-29 11:05:47 -0400819 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500820 if device.connect_status != ConnectStatus.REACHABLE:
821 self.log.error('device-unreachable')
822 return
823
824 # TODO: Create some omci task that encompases this what intended
825
826 # Not currently called. Would be called presumably from the olt handler
827 def create_multicast_gemport(self, data):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700828 self.log.debug('create-multicast-gem-port', data=data)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500829
830 # TODO: create objects and populate for later omci calls
831
832 def disable(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700833 self.log.debug('disable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500834 try:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700835 self.log.info('sending-uni-lock-towards-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500836
Matt Jeanneret80766692019-05-03 09:58:38 -0400837 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500838 def stop_anyway(reason):
839 # proceed with disable regardless if we could reach the onu. for example onu is unplugged
840 self.log.debug('stopping-openomci-statemachine')
841 reactor.callLater(0, self._onu_omci_device.stop)
842
843 # Let TP download happen again
844 for uni_id in self._tp_service_specific_task:
845 self._tp_service_specific_task[uni_id].clear()
846 for uni_id in self._tech_profile_download_done:
847 self._tech_profile_download_done[uni_id].clear()
848
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700849 self.disable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500850 device.oper_status = OperStatus.UNKNOWN
851 device.reason = "omci-admin-lock"
Matt Jeannereta8fd85f2019-05-01 12:16:45 -0400852 yield self.core_proxy.device_update(device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500853
854 # lock all the unis
855 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=True)
856 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
857 self._deferred.addCallbacks(stop_anyway, stop_anyway)
858 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700859 self.log.exception('exception-in-onu-disable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500860
William Kurkian3a206332019-04-29 11:05:47 -0400861 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500862 def reenable(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700863 self.log.debug('reenable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500864 try:
865 # Start up OpenOMCI state machines for this device
866 # this will ultimately resync mib and unlock unis on successful redownloading the mib
867 self.log.debug('restarting-openomci-statemachine')
868 self._subscribe_to_events()
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700869 yield self.core_proxy.device_reason_update(self.device_id, "restarting-openomci")
serkant.uluderya2cb65f72019-09-30 14:01:51 -0700870 reactor.callLater(1, self._onu_omci_device.start, device)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500871 self._heartbeat.enabled = True
872 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700873 self.log.exception('exception-in-onu-reenable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500874
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)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700886 self.disable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500887 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
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700899 def disable_ports(self):
900 self.log.info('disable-ports', device_id=self.device_id)
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
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700906 def enable_ports(self):
907 self.log.info('enable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500908
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):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700915 self.log.debug('subscribe-to-events')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500916
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):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700931 self.log.debug('in-sync-handler', _topic=_topic, msg=msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500932 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):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700950 self.log.debug('capabilities-handler', _topic=_topic, msg=_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500951 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):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700959 self.log.debug('mib-in-sync')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500960
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)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700965 yield self.core_proxy.device_reason_update(self.device_id, 'discovery-mibsync-complete')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500966
967 if not self._dev_info_loaded:
968 self.log.info('loading-device-data-from-mib', in_sync=in_sync, already_loaded=self._dev_info_loaded)
969
970 omci_dev = self._onu_omci_device
971 config = omci_dev.configuration
972
973 # TODO: run this sooner somehow. shouldnt have to wait for mib sync to push an initial download
974 # In Sync, we can register logical ports now. Ideally this could occur on
975 # the first time we received a successful (no timeout) OMCI Rx response.
976 try:
977
978 # sort the lists so we get consistent port ordering.
979 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
980 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
981 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
982 veip_list = sorted(config.veip_entities) if config.veip_entities else []
983
984 if ani_list is None or (pptp_list is None and veip_list is None):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500985 self.log.warn("no-ani-or-unis")
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700986 yield self.core_proxy.device_reason_update(self.device_id, 'onu-missing-required-elements')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500987 raise Exception("onu-missing-required-elements")
988
989 # Currently logging the ani, pptp, veip, and uni for information purposes.
990 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
991 # And in some ONU the UNI-G list is incomplete or incorrect...
992 for entity_id in ani_list:
993 ani_value = config.ani_g_entities[entity_id]
994 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
995 # TODO: currently only one OLT PON port/ANI, so this works out. With NGPON there will be 2..?
996 self._total_tcont_count = ani_value.get('total-tcont-count')
997 self.log.debug("set-total-tcont-count", tcont_count=self._total_tcont_count)
998
999 for entity_id in uni_list:
1000 uni_value = config.uni_g_entities[entity_id]
1001 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
1002
1003 uni_entities = OrderedDict()
1004 for entity_id in pptp_list:
1005 pptp_value = config.pptp_entities[entity_id]
1006 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
1007 uni_entities[entity_id] = UniType.PPTP
1008
1009 for entity_id in veip_list:
1010 veip_value = config.veip_entities[entity_id]
1011 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
1012 uni_entities[entity_id] = UniType.VEIP
1013
1014 uni_id = 0
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -05001015 for entity_id, uni_type in uni_entities.items():
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001016 try:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001017 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001018 uni_id += 1
1019 except AssertionError as e:
1020 self.log.warn("could not add UNI", entity_id=entity_id, uni_type=uni_type, e=e)
1021
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001022 self._qos_flexibility = config.qos_configuration_flexibility or 0
1023 self._omcc_version = config.omcc_version or OMCCVersion.Unknown
1024
1025 if self._unis:
1026 self._dev_info_loaded = True
1027 else:
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001028 yield self.core_proxy.device_reason_update(self.device_id, 'no-usable-unis')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001029 self.log.warn("no-usable-unis")
1030 raise Exception("no-usable-unis")
1031
1032 except Exception as e:
1033 self.log.exception('device-info-load', e=e)
1034 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1035
1036 else:
1037 self.log.info('device-info-already-loaded', in_sync=in_sync, already_loaded=self._dev_info_loaded)
1038
1039 if self._dev_info_loaded:
Matt Jeanneretad9a0f12019-05-09 14:05:49 -04001040 if device.admin_state == AdminState.PREPROVISIONED or device.admin_state == AdminState.ENABLED:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001041
1042 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001043 def success(_results):
1044 self.log.info('mib-download-success', _results=_results)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001045 yield self.enable_ports()
Matt Jeanneretc083f462019-03-11 15:02:01 -04001046 yield self.core_proxy.device_state_update(device.id,
1047 oper_status=OperStatus.ACTIVE, connect_status=ConnectStatus.REACHABLE)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001048 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-downloaded')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001049 self._mib_download_task = None
Devmalya Paulffc89df2019-07-31 17:43:13 -04001050 yield self.onu_active_event()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001051
Matt Jeanneretc083f462019-03-11 15:02:01 -04001052 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001053 def failure(_reason):
1054 self.log.warn('mib-download-failure-retrying', _reason=_reason)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001055 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001056 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1057
1058 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1059 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1060 self.log.info('downloading-initial-mib-configuration')
1061 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1062 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1063 self._deferred.addCallbacks(success, failure)
1064 else:
1065 self.log.info('admin-down-disabling')
1066 self.disable(device)
1067 else:
1068 self.log.info('device-info-not-loaded-skipping-mib-download')
1069
Matt Jeanneretc083f462019-03-11 15:02:01 -04001070 @inlineCallbacks
1071 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001072 self.log.debug('add-uni-port')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001073
Matt Jeanneretc083f462019-03-11 15:02:01 -04001074 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 -05001075
1076 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1077 uni_name = "uni-{}".format(uni_no)
1078
1079 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
1080
1081 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 -04001082 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001083
1084 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1085 uni_port.entity_id = entity_id
1086 uni_port.enabled = True
1087 uni_port.mac_bridge_port_num = mac_bridge_port_num
1088
1089 self.log.debug("created-uni-port", uni=uni_port)
1090
Matt Jeanneretc083f462019-03-11 15:02:01 -04001091 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001092
1093 self._unis[uni_port.port_number] = uni_port
1094
1095 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=self._onu_indication.onu_id,
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -05001096 uni_ports=self.uni_ports, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001097
Matt Jeanneretc083f462019-03-11 15:02:01 -04001098 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1099 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
Amit Ghosh65400f12019-11-21 12:04:12 +00001100 MAX_PONS_PER_OLT = 256
1101 MAX_ONUS_PER_PON = 256
Matt Jeanneretc083f462019-03-11 15:02:01 -04001102 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001103
Matt Jeanneretc083f462019-03-11 15:02:01 -04001104 assert intf_id < MAX_PONS_PER_OLT
1105 assert onu_id < MAX_ONUS_PER_PON
1106 assert uni_id < MAX_UNIS_PER_ONU
Amit Ghosh65400f12019-11-21 12:04:12 +00001107 return intf_id << 12 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001108
1109 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001110 def onu_active_event(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001111 self.log.debug('onu-active-event')
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001112 try:
1113 device = yield self.core_proxy.get_device(self.device_id)
1114 parent_device = yield self.core_proxy.get_device(self.parent_id)
1115 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001116 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001117
1118 self.log.debug("onu-indication-context-data",
1119 pon_id=self._onu_indication.intf_id,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001120 onu_id=self._onu_indication.onu_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001121 registration_id=self.device_id,
1122 device_id=self.device_id,
1123 onu_serial_number=device.serial_number,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001124 olt_serial_number=olt_serial_number,
1125 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001126
Devmalya Paulffc89df2019-07-31 17:43:13 -04001127 self.log.debug("Trying-to-raise-onu-active-event")
1128 OnuActiveEvent(self.events, self.device_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001129 self._onu_indication.intf_id,
1130 device.serial_number,
1131 str(self.device_id),
Devmalya Paulffc89df2019-07-31 17:43:13 -04001132 olt_serial_number,raised_ts,
1133 onu_id=self._onu_indication.onu_id).send(True)
1134 except Exception as active_event_error:
1135 self.log.exception('onu-activated-event-error',
1136 errmsg=active_event_error.message)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001137