blob: 5ab5b9cae294d49732d9077f78a3b5c6e371cb7d [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 Jeanneretf4113222019-08-14 19:44:34 -040046from voltha_protos.device_pb2 import Port
47from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, ofp_port, OFPPS_LIVE,OFPPS_LINK_DOWN,\
48 OFPPF_FIBER, OFPPF_1GB_FD
Matt Jeanneret3bfebff2019-04-12 18:25:03 -040049from voltha_protos.inter_container_pb2 import InterAdapterMessageType, \
Girish Gowdrae933cd32019-11-21 21:04:41 +053050 InterAdapterOmciMessage, PortCapability, InterAdapterTechProfileDownloadMessage, InterAdapterDeleteGemPortMessage, \
51 InterAdapterDeleteTcontMessage
Matt Jeannereta32441c2019-03-07 05:16:37 -050052from voltha_protos.openolt_pb2 import OnuIndication
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050053from pyvoltha.adapters.extensions.omci.onu_configuration import OMCCVersion
54from pyvoltha.adapters.extensions.omci.onu_device_entry import OnuDeviceEvents, \
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050055 OnuDeviceEntry, IN_SYNC_KEY
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050056from omci.brcm_mib_download_task import BrcmMibDownloadTask
Girish Gowdrae933cd32019-11-21 21:04:41 +053057from omci.brcm_tp_setup_task import BrcmTpSetupTask
58from omci.brcm_tp_delete_task import BrcmTpDeleteTask
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050059from omci.brcm_uni_lock_task import BrcmUniLockTask
60from omci.brcm_vlan_filter_task import BrcmVlanFilterTask
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050061from onu_gem_port import OnuGemPort
62from onu_tcont import OnuTCont
63from pon_port import PonPort
64from uni_port import UniPort, UniType
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050065from pyvoltha.common.tech_profile.tech_profile import TechProfile
onkarkundargiaae99712019-09-23 15:02:52 +053066from pyvoltha.adapters.extensions.omci.tasks.omci_test_request import OmciTestRequest
67from pyvoltha.adapters.extensions.omci.omci_entities import AniG
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050068from pyvoltha.adapters.extensions.omci.omci_defs import EntityOperations, ReasonCodes
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050069
70OP = EntityOperations
71RC = ReasonCodes
72
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050073_STARTUP_RETRY_WAIT = 20
74
75
76class BrcmOpenomciOnuHandler(object):
77
78 def __init__(self, adapter, device_id):
79 self.log = structlog.get_logger(device_id=device_id)
Matt Jeanneret08a8e862019-12-20 14:02:32 -050080 self.log.debug('starting-handler')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050081 self.adapter = adapter
Matt Jeannereta32441c2019-03-07 05:16:37 -050082 self.core_proxy = adapter.core_proxy
83 self.adapter_proxy = adapter.adapter_proxy
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050084 self.parent_adapter = None
85 self.parent_id = None
86 self.device_id = device_id
87 self.incoming_messages = DeferredQueue()
88 self.event_messages = DeferredQueue()
89 self.proxy_address = None
90 self.tx_id = 0
91 self._enabled = False
Devmalya Paulffc89df2019-07-31 17:43:13 -040092 self.events = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050093 self.pm_metrics = None
94 self._omcc_version = OMCCVersion.Unknown
95 self._total_tcont_count = 0 # From ANI-G ME
96 self._qos_flexibility = 0 # From ONT2_G ME
97
98 self._onu_indication = None
99 self._unis = dict() # Port # -> UniPort
100
101 self._pon = None
102 # TODO: probably shouldnt be hardcoded, determine from olt maybe?
103 self._pon_port_number = 100
104 self.logical_device_id = None
105
106 self._heartbeat = HeartBeat.create(self, device_id)
107
108 # Set up OpenOMCI environment
109 self._onu_omci_device = None
110 self._dev_info_loaded = False
111 self._deferred = None
112
113 self._in_sync_subscription = None
Matt Jeanneretf4113222019-08-14 19:44:34 -0400114 self._port_state_subscription = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500115 self._connectivity_subscription = None
116 self._capabilities_subscription = None
117
118 self.mac_bridge_service_profile_entity_id = 0x201
119 self.gal_enet_profile_entity_id = 0x1
120
121 self._tp_service_specific_task = dict()
122 self._tech_profile_download_done = dict()
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400123 # Stores information related to queued vlan filter tasks
124 # Dictionary with key being uni_id and value being device,uni port ,uni id and vlan id
125
126 self._queued_vlan_filter_task = dict()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500127
128 # Initialize KV store client
129 self.args = registry('main').get_args()
130 if self.args.backend == 'etcd':
131 host, port = self.args.etcd.split(':', 1)
132 self.kv_client = EtcdStore(host, port,
133 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
134 elif self.args.backend == 'consul':
135 host, port = self.args.consul.split(':', 1)
136 self.kv_client = ConsulStore(host, port,
137 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX)
138 else:
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500139 self.log.error('invalid-backend')
140 raise Exception("invalid-backend-for-kv-store")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500141
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500142 @property
143 def enabled(self):
144 return self._enabled
145
146 @enabled.setter
147 def enabled(self, value):
148 if self._enabled != value:
149 self._enabled = value
150
151 @property
152 def omci_agent(self):
153 return self.adapter.omci_agent
154
155 @property
156 def omci_cc(self):
157 return self._onu_omci_device.omci_cc if self._onu_omci_device is not None else None
158
159 @property
160 def heartbeat(self):
161 return self._heartbeat
162
163 @property
164 def uni_ports(self):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500165 return list(self._unis.values())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500166
167 def uni_port(self, port_no_or_name):
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500168 if isinstance(port_no_or_name, six.string_types):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500169 return next((uni for uni in self.uni_ports
170 if uni.name == port_no_or_name), None)
171
172 assert isinstance(port_no_or_name, int), 'Invalid parameter type'
173 return next((uni for uni in self.uni_ports
Girish Gowdrae933cd32019-11-21 21:04:41 +0530174 if uni.port_number == port_no_or_name), None)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500175
176 @property
177 def pon_port(self):
178 return self._pon
179
Girish Gowdraa73ee452019-12-20 18:52:17 +0530180 @property
181 def onu_omci_device(self):
182 return self._onu_omci_device
183
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500184 def receive_message(self, msg):
185 if self.omci_cc is not None:
186 self.omci_cc.receive_message(msg)
187
Matt Jeanneretc083f462019-03-11 15:02:01 -0400188 def get_ofp_port_info(self, device, port_no):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500189 self.log.debug('get-ofp-port-info', port_no=port_no, device_id=device.id)
Matt Jeanneretc083f462019-03-11 15:02:01 -0400190 cap = OFPPF_1GB_FD | OFPPF_FIBER
191
Girish Gowdrae933cd32019-11-21 21:04:41 +0530192 hw_addr = mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
193 ((device.parent_port_no >> 8 & 0xff),
194 device.parent_port_no & 0xff,
195 (port_no >> 16) & 0xff,
196 (port_no >> 8) & 0xff,
197 port_no & 0xff))
Matt Jeanneretc083f462019-03-11 15:02:01 -0400198
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400199 uni_port = self.uni_port(int(port_no))
200 name = device.serial_number + '-' + str(uni_port.mac_bridge_port_num)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500201 self.log.debug('ofp-port-name', port_no=port_no, name=name, uni_port=uni_port)
Matt Jeanneretf4113222019-08-14 19:44:34 -0400202
203 ofstate = OFPPS_LINK_DOWN
204 if uni_port.operstatus is OperStatus.ACTIVE:
205 ofstate = OFPPS_LIVE
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400206
Matt Jeanneretc083f462019-03-11 15:02:01 -0400207 return PortCapability(
208 port=LogicalPort(
209 ofp_port=ofp_port(
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400210 name=name,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400211 hw_addr=hw_addr,
212 config=0,
Matt Jeanneretf4113222019-08-14 19:44:34 -0400213 state=ofstate,
Matt Jeanneretc083f462019-03-11 15:02:01 -0400214 curr=cap,
215 advertised=cap,
216 peer=cap,
217 curr_speed=OFPPF_1GB_FD,
218 max_speed=OFPPF_1GB_FD
219 ),
220 device_id=device.id,
221 device_port_no=port_no
222 )
223 )
224
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500225 # Called once when the adapter creates the device/onu instance
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500226 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500227 def activate(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700228 self.log.debug('activate-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500229
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500230 assert device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500231 assert device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500232 assert device.proxy_address.device_id
233
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500234 self.proxy_address = device.proxy_address
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500235 self.parent_id = device.parent_id
Matt Jeanneret0c287892019-02-28 11:48:00 -0500236 self._pon_port_number = device.parent_port_no
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500237 if self.enabled is not True:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700238 self.log.info('activating-new-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500239 # populate what we know. rest comes later after mib sync
Matt Jeanneret0c287892019-02-28 11:48:00 -0500240 device.root = False
Matt Jeannereta32441c2019-03-07 05:16:37 -0500241 device.vendor = 'OpenONU'
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500242 device.reason = 'activating-onu'
243
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500244 # TODO NEW CORE: Need to either get logical device id from core or use regular device id
Matt Jeanneret3b7db442019-04-22 16:29:48 -0400245 # pm_metrics requires a logical device id. For now set to just device_id
246 self.logical_device_id = self.device_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500247
Matt Jeannereta32441c2019-03-07 05:16:37 -0500248 yield self.core_proxy.device_update(device)
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500249 self.log.debug('device-updated', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500250
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700251 yield self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500252
Matteo Scandolod8d73172019-11-26 12:15:15 -0700253 self.log.debug('pon state initialized', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500254 ############################################################################
Devmalya Paulffc89df2019-07-31 17:43:13 -0400255 # Setup Alarm handler
256 self.events = AdapterEvents(self.core_proxy, device.id, self.logical_device_id,
257 device.serial_number)
258 ############################################################################
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500259 # Setup PM configuration for this device
260 # Pass in ONU specific options
261 kwargs = {
262 OnuPmMetrics.DEFAULT_FREQUENCY_KEY: OnuPmMetrics.DEFAULT_ONU_COLLECTION_FREQUENCY,
263 'heartbeat': self.heartbeat,
264 OnuOmciPmMetrics.OMCI_DEV_KEY: self._onu_omci_device
265 }
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500266 self.log.debug('create-pm-metrics', device_id=device.id, serial_number=device.serial_number)
Devmalya Paulffc89df2019-07-31 17:43:13 -0400267 self.pm_metrics = OnuPmMetrics(self.events, self.core_proxy, self.device_id,
Yongjie Zhang8f891ad2019-07-03 15:32:38 -0400268 self.logical_device_id, device.serial_number,
269 grouped=True, freq_override=False, **kwargs)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500270 pm_config = self.pm_metrics.make_proto()
271 self._onu_omci_device.set_pm_config(self.pm_metrics.omci_pm.openomci_interval_pm)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530272 self.log.info("initial-pm-config", device_id=device.id, serial_number=device.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500273 yield self.core_proxy.device_pm_config_update(pm_config, init=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500274
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500275 # Note, ONU ID and UNI intf set in add_uni_port method
Devmalya Paulffc89df2019-07-31 17:43:13 -0400276 self._onu_omci_device.alarm_synchronizer.set_alarm_params(mgr=self.events,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500277 ani_ports=[self._pon])
aishwaryarana01a98d9fe2019-05-08 12:09:06 -0500278
Girish Gowdrae933cd32019-11-21 21:04:41 +0530279 # Start collecting stats from the device after a brief pause
aishwaryarana01a98d9fe2019-05-08 12:09:06 -0500280 reactor.callLater(10, self.pm_metrics.start_collector)
281
onkarkundargiaae99712019-09-23 15:02:52 +0530282 # Code to Run OMCI Test Action
283 kwargs_omci_test_action = {
284 OmciTestRequest.DEFAULT_FREQUENCY_KEY:
285 OmciTestRequest.DEFAULT_COLLECTION_FREQUENCY
286 }
287 serial_number = device.serial_number
288 test_request = OmciTestRequest(self.core_proxy,
289 self.omci_agent, self.device_id,
290 AniG, serial_number,
291 self.logical_device_id,
292 exclusive=False,
293 **kwargs_omci_test_action)
294 reactor.callLater(60, test_request.start_collector)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500295 self.enabled = True
296 else:
297 self.log.info('onu-already-activated')
298
299 # Called once when the adapter needs to re-create device. usually on vcore restart
William Kurkian3a206332019-04-29 11:05:47 -0400300 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500301 def reconcile(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700302 self.log.debug('reconcile-device', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500303
304 # first we verify that we got parent reference and proxy info
305 assert device.parent_id
306 assert device.proxy_address.device_id
307
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700308 self.proxy_address = device.proxy_address
309 self.parent_id = device.parent_id
310 self._pon_port_number = device.parent_port_no
311
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500312 if self.enabled is not True:
313 self.log.info('reconciling-broadcom-onu-device')
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700314 self.logical_device_id = self.device_id
315 self._init_pon_state()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500316
317 # need to restart state machines on vcore restart. there is no indication to do it for us.
318 self._onu_omci_device.start()
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700319 yield self.core_proxy.device_reason_update(self.device_id, "restarting-openomci")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500320
321 # TODO: this is probably a bit heavy handed
322 # Force a reboot for now. We need indications to reflow to reassign tconts and gems given vcore went away
323 # This may not be necessary when mib resync actually works
324 reactor.callLater(1, self.reboot)
325
326 self.enabled = True
327 else:
328 self.log.info('onu-already-activated')
329
330 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700331 def _init_pon_state(self):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500332 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 -0500333
334 self._pon = PonPort.create(self, self._pon_port_number)
Matt Jeanneret0c287892019-02-28 11:48:00 -0500335 self._pon.add_peer(self.parent_id, self._pon_port_number)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700336 self.log.debug('adding-pon-port-to-agent',
337 type=self._pon.get_port().type,
338 admin_state=self._pon.get_port().admin_state,
339 oper_status=self._pon.get_port().oper_status,
340 )
Matt Jeanneret0c287892019-02-28 11:48:00 -0500341
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700342 yield self.core_proxy.port_created(self.device_id, self._pon.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500343
Matteo Scandolod8d73172019-11-26 12:15:15 -0700344 self.log.debug('added-pon-port-to-agent',
345 type=self._pon.get_port().type,
346 admin_state=self._pon.get_port().admin_state,
347 oper_status=self._pon.get_port().oper_status,
348 )
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500349
350 # Create and start the OpenOMCI ONU Device Entry for this ONU
351 self._onu_omci_device = self.omci_agent.add_device(self.device_id,
Matt Jeannereta32441c2019-03-07 05:16:37 -0500352 self.core_proxy,
353 self.adapter_proxy,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500354 support_classes=self.adapter.broadcom_omci,
355 custom_me_map=self.adapter.custom_me_entities())
356 # Port startup
357 if self._pon is not None:
358 self._pon.enabled = True
359
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500360 def delete(self, device):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700361 self.log.info('delete-onu', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500362 if self.parent_adapter:
363 try:
364 self.parent_adapter.delete_child_device(self.parent_id, device)
365 except AttributeError:
366 self.log.debug('parent-device-delete-child-not-implemented')
367 else:
368 self.log.debug("parent-adapter-not-available")
369
370 def _create_tconts(self, uni_id, us_scheduler):
371 alloc_id = us_scheduler['alloc_id']
372 q_sched_policy = us_scheduler['q_sched_policy']
373 self.log.debug('create-tcont', us_scheduler=us_scheduler)
374
375 tcontdict = dict()
376 tcontdict['alloc-id'] = alloc_id
377 tcontdict['q_sched_policy'] = q_sched_policy
378 tcontdict['uni_id'] = uni_id
379
Matt Jeanneret3789d0d2020-01-19 09:03:42 -0500380 tcont = OnuTCont.create(self, tcont=tcontdict)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500381
382 self._pon.add_tcont(tcont)
383
384 self.log.debug('pon-add-tcont', tcont=tcont)
385
386 # Called when there is an olt up indication, providing the gem port id chosen by the olt handler
387 def _create_gemports(self, uni_id, gem_ports, alloc_id_ref, direction):
388 self.log.debug('create-gemport',
389 gem_ports=gem_ports, direction=direction)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530390 new_gem_ports = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500391 for gem_port in gem_ports:
392 gemdict = dict()
393 gemdict['gemport_id'] = gem_port['gemport_id']
394 gemdict['direction'] = direction
395 gemdict['alloc_id_ref'] = alloc_id_ref
396 gemdict['encryption'] = gem_port['aes_encryption']
397 gemdict['discard_config'] = dict()
398 gemdict['discard_config']['max_probability'] = \
399 gem_port['discard_config']['max_probability']
400 gemdict['discard_config']['max_threshold'] = \
401 gem_port['discard_config']['max_threshold']
402 gemdict['discard_config']['min_threshold'] = \
403 gem_port['discard_config']['min_threshold']
404 gemdict['discard_policy'] = gem_port['discard_policy']
405 gemdict['max_q_size'] = gem_port['max_q_size']
406 gemdict['pbit_map'] = gem_port['pbit_map']
407 gemdict['priority_q'] = gem_port['priority_q']
408 gemdict['scheduling_policy'] = gem_port['scheduling_policy']
409 gemdict['weight'] = gem_port['weight']
410 gemdict['uni_id'] = uni_id
411
412 gem_port = OnuGemPort.create(self, gem_port=gemdict)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530413 new_gem_ports.append(gem_port)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500414
415 self._pon.add_gem_port(gem_port)
416
417 self.log.debug('pon-add-gemport', gem_port=gem_port)
418
Girish Gowdrae933cd32019-11-21 21:04:41 +0530419 return new_gem_ports
420
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400421 def _execute_queued_vlan_filter_tasks(self, uni_id):
422 # During OLT Reboots, ONU Reboots, ONU Disable/Enable, it is seen that vlan_filter
423 # task is scheduled even before tp task. So we queue vlan-filter task if tp_task
424 # or initial-mib-download is not done. Once the tp_task is completed, we execute
425 # such queued vlan-filter tasks
426 try:
427 if uni_id in self._queued_vlan_filter_task:
428 self.log.info("executing-queued-vlan-filter-task",
429 uni_id=uni_id)
430 filter_info = self._queued_vlan_filter_task[uni_id]
431 reactor.callLater(0, self._add_vlan_filter_task, filter_info.get("device"),
Girish Gowdraa73ee452019-12-20 18:52:17 +0530432 uni_id, filter_info.get("uni_port"), filter_info.get("set_vlan_vid"),
433 filter_info.get("tp_id"))
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400434 # Now remove the entry from the dictionary
435 self._queued_vlan_filter_task[uni_id].clear()
436 self.log.debug("executed-queued-vlan-filter-task",
437 uni_id=uni_id)
438 except Exception as e:
439 self.log.error("vlan-filter-configuration-failed", uni_id=uni_id, error=e)
440
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500441 def _do_tech_profile_configuration(self, uni_id, tp):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500442 us_scheduler = tp['us_scheduler']
443 alloc_id = us_scheduler['alloc_id']
444 self._create_tconts(uni_id, us_scheduler)
445 upstream_gem_port_attribute_list = tp['upstream_gem_port_attribute_list']
446 self._create_gemports(uni_id, upstream_gem_port_attribute_list, alloc_id, "UPSTREAM")
447 downstream_gem_port_attribute_list = tp['downstream_gem_port_attribute_list']
448 self._create_gemports(uni_id, downstream_gem_port_attribute_list, alloc_id, "DOWNSTREAM")
449
450 def load_and_configure_tech_profile(self, uni_id, tp_path):
451 self.log.debug("loading-tech-profile-configuration", uni_id=uni_id, tp_path=tp_path)
452
453 if uni_id not in self._tp_service_specific_task:
454 self._tp_service_specific_task[uni_id] = dict()
455
456 if uni_id not in self._tech_profile_download_done:
457 self._tech_profile_download_done[uni_id] = dict()
458
459 if tp_path not in self._tech_profile_download_done[uni_id]:
460 self._tech_profile_download_done[uni_id][tp_path] = False
461
462 if not self._tech_profile_download_done[uni_id][tp_path]:
463 try:
464 if tp_path in self._tp_service_specific_task[uni_id]:
465 self.log.info("tech-profile-config-already-in-progress",
Girish Gowdrae933cd32019-11-21 21:04:41 +0530466 tp_path=tp_path)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500467 return
468
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -0500469 tpstored = self.kv_client[tp_path]
470 tpstring = tpstored.decode('ascii')
471 tp = json.loads(tpstring)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530472
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500473 self.log.debug("tp-instance", tp=tp)
474 self._do_tech_profile_configuration(uni_id, tp)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700475
William Kurkian3a206332019-04-29 11:05:47 -0400476 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500477 def success(_results):
478 self.log.info("tech-profile-config-done-successfully")
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700479 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-download-success')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500480 if tp_path in self._tp_service_specific_task[uni_id]:
481 del self._tp_service_specific_task[uni_id][tp_path]
482 self._tech_profile_download_done[uni_id][tp_path] = True
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400483 # Now execute any vlan filter tasks that were queued for later
484 self._execute_queued_vlan_filter_tasks(uni_id)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530485
William Kurkian3a206332019-04-29 11:05:47 -0400486 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500487 def failure(_reason):
488 self.log.warn('tech-profile-config-failure-retrying',
Girish Gowdrae933cd32019-11-21 21:04:41 +0530489 _reason=_reason)
490 yield self.core_proxy.device_reason_update(self.device_id,
491 'tech-profile-config-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500492 if tp_path in self._tp_service_specific_task[uni_id]:
493 del self._tp_service_specific_task[uni_id][tp_path]
494 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self.load_and_configure_tech_profile,
495 uni_id, tp_path)
496
497 self.log.info('downloading-tech-profile-configuration')
Girish Gowdrae933cd32019-11-21 21:04:41 +0530498 # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
499 # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
500 # due to additional tasks on different UNIs. So, it we cannot use the pon_port after
501 # this initializer
502 tconts = []
503 for tcont in list(self.pon_port.tconts.values()):
504 if tcont.uni_id is not None and tcont.uni_id != uni_id:
505 continue
506 tconts.append(tcont)
507
508 gem_ports = []
509 for gem_port in list(self.pon_port.gem_ports.values()):
510 if gem_port.uni_id is not None and gem_port.uni_id != uni_id:
511 continue
512 gem_ports.append(gem_port)
513
514 self.log.debug("tconts-gems-to-install", tconts=tconts, gem_ports=gem_ports)
515
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500516 self._tp_service_specific_task[uni_id][tp_path] = \
Girish Gowdrae933cd32019-11-21 21:04:41 +0530517 BrcmTpSetupTask(self.omci_agent, self, uni_id, tconts, gem_ports, int(tp_path.split("/")[1]))
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500518 self._deferred = \
Girish Gowdrae933cd32019-11-21 21:04:41 +0530519 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500520 self._deferred.addCallbacks(success, failure)
521
522 except Exception as e:
523 self.log.exception("error-loading-tech-profile", e=e)
524 else:
525 self.log.info("tech-profile-config-already-done")
Girish Gowdrae933cd32019-11-21 21:04:41 +0530526 # Could be a case where TP exists but new gem-ports are getting added dynamically
527 tpstored = self.kv_client[tp_path]
528 tpstring = tpstored.decode('ascii')
529 tp = json.loads(tpstring)
530 upstream_gems = []
531 downstream_gems = []
532 # Find out the new Gem ports that are getting added afresh.
533 for gp in tp['upstream_gem_port_attribute_list']:
534 if self.pon_port.gem_port(gp['gemport_id'], "upstream"):
535 # gem port already exists
536 continue
537 upstream_gems.append(gp)
538 for gp in tp['downstream_gem_port_attribute_list']:
539 if self.pon_port.gem_port(gp['gemport_id'], "downstream"):
540 # gem port already exists
541 continue
542 downstream_gems.append(gp)
543
544 us_scheduler = tp['us_scheduler']
545 alloc_id = us_scheduler['alloc_id']
546
547 if len(upstream_gems) > 0 or len(downstream_gems) > 0:
548 self.log.info("installing-new-gem-ports", upstream_gems=upstream_gems, downstream_gems=downstream_gems)
549 new_upstream_gems = self._create_gemports(uni_id, upstream_gems, alloc_id, "UPSTREAM")
550 new_downstream_gems = self._create_gemports(uni_id, downstream_gems, alloc_id, "DOWNSTREAM")
551 new_gems = []
552 new_gems.extend(new_upstream_gems)
553 new_gems.extend(new_downstream_gems)
554
555 def success(_results):
556 self.log.info("new-gem-ports-successfully-installed", result=_results)
557
558 def failure(_reason):
559 self.log.warn('new-gem-port-install-failed--retrying',
560 _reason=_reason)
561 # Remove gem ports from cache. We will re-add them during the retry
562 for gp in new_gems:
563 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
564
565 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self.load_and_configure_tech_profile,
566 uni_id, tp_path)
567
568 self._tp_service_specific_task[uni_id][tp_path] = \
569 BrcmTpSetupTask(self.omci_agent, self, uni_id, [], new_gems, int(tp_path.split("/")[1]))
570 self._deferred = \
571 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
572 self._deferred.addCallbacks(success, failure)
573
574 def delete_tech_profile(self, uni_id, tp_path, alloc_id=None, gem_port_id=None):
575 try:
Naga Manjunathe433c712020-01-02 17:27:20 +0530576 if not uni_id in self._tech_profile_download_done:
577 self.log.warn("tp-key-is-not-present", uni_id=uni_id)
578 return
579
580 if not tp_path in self._tech_profile_download_done[uni_id]:
581 self.log.warn("tp-path-is-not-present", tp_path=tp_path)
582 return
583
Girish Gowdrae933cd32019-11-21 21:04:41 +0530584 if self._tech_profile_download_done[uni_id][tp_path] is not True:
585 self.log.error("tp-download-is-not-done-in-order-to-process-tp-delete")
586 return
587
588 if alloc_id is None and gem_port_id is None:
589 self.log.error("alloc-id-and-gem-port-id-are-none")
590 return
591
592 # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
593 # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
594 # due to additional tasks on different UNIs. So, it we cannot use the pon_port affter
595 # this initializer
596 tcont = None
597 self.log.debug("tconts", tconts=list(self.pon_port.tconts.values()))
598 for tc in list(self.pon_port.tconts.values()):
599 if tc.alloc_id == alloc_id:
600 tcont = tc
601 self.pon_port.remove_tcont(tc.alloc_id, False)
602
603 gem_port = None
604 self.log.debug("gem-ports", gem_ports=list(self.pon_port.gem_ports.values()))
605 for gp in list(self.pon_port.gem_ports.values()):
606 if gp.gem_id == gem_port_id:
607 gem_port = gp
608 self.pon_port.remove_gem_id(gp.gem_id, gp.direction, False)
609
610 # tp_path is of the format <technology>/<table_id>/<uni_port_name>
611 # We need the TP Table ID
612 tp_table_id = int(tp_path.split("/")[1])
613
614 @inlineCallbacks
615 def success(_results):
616 if gem_port_id:
617 self.log.info("gem-port-delete-done-successfully")
618 if alloc_id:
619 self.log.info("tcont-delete-done-successfully")
620 # The deletion of TCONT marks the complete deletion of tech-profile
621 try:
622 del self._tech_profile_download_done[uni_id][tp_path]
623 del self._tp_service_specific_task[uni_id][tp_path]
624 except Exception as ex:
625 self.log.error("del-tp-state-info", e=ex)
626
627 # TODO: There could be multiple TP on the UNI, and also the ONU.
628 # TODO: But the below reason updates for the whole device.
629 yield self.core_proxy.device_reason_update(self.device_id, 'tech-profile-config-delete-success')
630
631 @inlineCallbacks
Girish Gowdraa73ee452019-12-20 18:52:17 +0530632 def failure(_reason):
Girish Gowdrae933cd32019-11-21 21:04:41 +0530633 self.log.warn('tech-profile-delete-failure-retrying',
634 _reason=_reason)
635 yield self.core_proxy.device_reason_update(self.device_id,
636 'tech-profile-config-delete-failure-retrying')
637 self._deferred = \
638 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
639 self._deferred.addCallbacks(success, failure)
640
641 self.log.info('deleting-tech-profile-configuration')
642
Girish Gowdraa73ee452019-12-20 18:52:17 +0530643 if tcont is None and gem_port is None:
644 if alloc_id is not None:
645 self.log.error("tcont-info-corresponding-to-alloc-id-not-found", alloc_id=alloc_id)
646 if gem_port_id is not None:
647 self.log.error("gem-port-info-corresponding-to-gem-port-id-not-found", gem_port_id=gem_port_id)
648 return
649
Girish Gowdrae933cd32019-11-21 21:04:41 +0530650 self._tp_service_specific_task[uni_id][tp_path] = \
651 BrcmTpDeleteTask(self.omci_agent, self, uni_id, tp_table_id,
652 tcont=tcont, gem_port=gem_port)
653 self._deferred = \
654 self._onu_omci_device.task_runner.queue_task(self._tp_service_specific_task[uni_id][tp_path])
655 self._deferred.addCallbacks(success, failure)
656 except Exception as e:
657 self.log.exception("failed-to-delete-tp",
658 e=e, uni_id=uni_id, tp_path=tp_path,
659 alloc_id=alloc_id, gem_port_id=gem_port_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500660
661 def update_pm_config(self, device, pm_config):
662 # TODO: This has not been tested
663 self.log.info('update_pm_config', pm_config=pm_config)
664 self.pm_metrics.update(pm_config)
665
666 # Calling this assumes the onu is active/ready and had at least an initial mib downloaded. This gets called from
667 # flow decomposition that ultimately comes from onos
668 def update_flow_table(self, device, flows):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700669 self.log.debug('update-flow-table', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500670
671 #
672 # We need to proxy through the OLT to get to the ONU
673 # Configuration from here should be using OMCI
674 #
675 # self.log.info('bulk-flow-update', device_id=device.id, flows=flows)
676
677 # no point in pushing omci flows if the device isnt reachable
678 if device.connect_status != ConnectStatus.REACHABLE or \
Girish Gowdrae933cd32019-11-21 21:04:41 +0530679 device.admin_state != AdminState.ENABLED:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500680 self.log.warn("device-disabled-or-offline-skipping-flow-update",
681 admin=device.admin_state, connect=device.connect_status)
682 return
683
684 def is_downstream(port):
685 return port == self._pon_port_number
686
687 def is_upstream(port):
688 return not is_downstream(port)
689
690 for flow in flows:
691 _type = None
692 _port = None
693 _vlan_vid = None
694 _udp_dst = None
695 _udp_src = None
696 _ipv4_dst = None
697 _ipv4_src = None
698 _metadata = None
699 _output = None
700 _push_tpid = None
701 _field = None
702 _set_vlan_vid = None
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400703 _tunnel_id = None
704
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500705 try:
Girish Gowdraa73ee452019-12-20 18:52:17 +0530706 write_metadata = fd.get_write_metadata(flow)
707 if write_metadata is None:
708 self.log.error("do-not-process-flow-without-write-metadata")
709 return
710
711 # extract tp id from flow
712 tp_id = (write_metadata >> 32) & 0xFFFF
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500713 self.log.debug("tp-id-in-flow", tp_id=tp_id)
Girish Gowdraa73ee452019-12-20 18:52:17 +0530714
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500715 _in_port = fd.get_in_port(flow)
716 assert _in_port is not None
717
718 _out_port = fd.get_out_port(flow) # may be None
719
720 if is_downstream(_in_port):
721 self.log.debug('downstream-flow', in_port=_in_port, out_port=_out_port)
722 uni_port = self.uni_port(_out_port)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530723 uni_id = _out_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500724 elif is_upstream(_in_port):
725 self.log.debug('upstream-flow', in_port=_in_port, out_port=_out_port)
726 uni_port = self.uni_port(_in_port)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400727 uni_id = _in_port & 0xF
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500728 else:
729 raise Exception('port should be 1 or 2 by our convention')
730
731 self.log.debug('flow-ports', in_port=_in_port, out_port=_out_port, uni_port=str(uni_port))
732
733 for field in fd.get_ofb_fields(flow):
734 if field.type == fd.ETH_TYPE:
735 _type = field.eth_type
736 self.log.debug('field-type-eth-type',
737 eth_type=_type)
738
739 elif field.type == fd.IP_PROTO:
740 _proto = field.ip_proto
741 self.log.debug('field-type-ip-proto',
742 ip_proto=_proto)
743
744 elif field.type == fd.IN_PORT:
745 _port = field.port
746 self.log.debug('field-type-in-port',
747 in_port=_port)
748
749 elif field.type == fd.VLAN_VID:
750 _vlan_vid = field.vlan_vid & 0xfff
751 self.log.debug('field-type-vlan-vid',
752 vlan=_vlan_vid)
753
754 elif field.type == fd.VLAN_PCP:
755 _vlan_pcp = field.vlan_pcp
756 self.log.debug('field-type-vlan-pcp',
757 pcp=_vlan_pcp)
758
759 elif field.type == fd.UDP_DST:
760 _udp_dst = field.udp_dst
761 self.log.debug('field-type-udp-dst',
762 udp_dst=_udp_dst)
763
764 elif field.type == fd.UDP_SRC:
765 _udp_src = field.udp_src
766 self.log.debug('field-type-udp-src',
767 udp_src=_udp_src)
768
769 elif field.type == fd.IPV4_DST:
770 _ipv4_dst = field.ipv4_dst
771 self.log.debug('field-type-ipv4-dst',
772 ipv4_dst=_ipv4_dst)
773
774 elif field.type == fd.IPV4_SRC:
775 _ipv4_src = field.ipv4_src
776 self.log.debug('field-type-ipv4-src',
777 ipv4_dst=_ipv4_src)
778
779 elif field.type == fd.METADATA:
780 _metadata = field.table_metadata
781 self.log.debug('field-type-metadata',
782 metadata=_metadata)
783
Matt Jeanneretef06d0d2019-04-27 17:36:53 -0400784 elif field.type == fd.TUNNEL_ID:
785 _tunnel_id = field.tunnel_id
786 self.log.debug('field-type-tunnel-id',
787 tunnel_id=_tunnel_id)
788
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500789 else:
790 raise NotImplementedError('field.type={}'.format(
791 field.type))
792
793 for action in fd.get_actions(flow):
794
795 if action.type == fd.OUTPUT:
796 _output = action.output.port
797 self.log.debug('action-type-output',
798 output=_output, in_port=_in_port)
799
800 elif action.type == fd.POP_VLAN:
801 self.log.debug('action-type-pop-vlan',
802 in_port=_in_port)
803
804 elif action.type == fd.PUSH_VLAN:
805 _push_tpid = action.push.ethertype
806 self.log.debug('action-type-push-vlan',
807 push_tpid=_push_tpid, in_port=_in_port)
808 if action.push.ethertype != 0x8100:
809 self.log.error('unhandled-tpid',
810 ethertype=action.push.ethertype)
811
812 elif action.type == fd.SET_FIELD:
813 _field = action.set_field.field.ofb_field
814 assert (action.set_field.field.oxm_class ==
815 OFPXMC_OPENFLOW_BASIC)
816 self.log.debug('action-type-set-field',
817 field=_field, in_port=_in_port)
818 if _field.type == fd.VLAN_VID:
819 _set_vlan_vid = _field.vlan_vid & 0xfff
820 self.log.debug('set-field-type-vlan-vid',
821 vlan_vid=_set_vlan_vid)
822 else:
823 self.log.error('unsupported-action-set-field-type',
824 field_type=_field.type)
825 else:
826 self.log.error('unsupported-action-type',
827 action_type=action.type, in_port=_in_port)
828
Matt Jeanneret810148b2019-09-29 12:44:01 -0400829 # OMCI set vlan task can only filter and set on vlan header attributes. Any other openflow
830 # supported match and action criteria cannot be handled by omci and must be ignored.
831 if _set_vlan_vid is None or _set_vlan_vid == 0:
832 self.log.warn('ignoring-flow-that-does-not-set-vlanid')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500833 else:
Girish Gowdraa73ee452019-12-20 18:52:17 +0530834 self.log.info('set-vlanid', uni_id=uni_id, uni_port=uni_port, set_vlan_vid=_set_vlan_vid, tp_id=tp_id)
835 self._add_vlan_filter_task(device, uni_id, uni_port, _set_vlan_vid, tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500836 except Exception as e:
837 self.log.exception('failed-to-install-flow', e=e, flow=flow)
838
Girish Gowdraa73ee452019-12-20 18:52:17 +0530839 def _add_vlan_filter_task(self, device, uni_id, uni_port, _set_vlan_vid, tp_id):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500840 assert uni_port is not None
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400841 if uni_id in self._tech_profile_download_done and self._tech_profile_download_done[uni_id] != {}:
842 @inlineCallbacks
843 def success(_results):
Girish Gowdraa73ee452019-12-20 18:52:17 +0530844 self.log.info('vlan-tagging-success', uni_port=uni_port, vlan=_set_vlan_vid, tp_id=tp_id)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700845 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-pushed')
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400846 self._vlan_filter_task = None
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500847
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400848 @inlineCallbacks
849 def failure(_reason):
Girish Gowdraa73ee452019-12-20 18:52:17 +0530850 self.log.warn('vlan-tagging-failure', uni_port=uni_port, vlan=_set_vlan_vid, tp_id=tp_id)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700851 yield self.core_proxy.device_reason_update(self.device_id, 'omci-flows-failed-retrying')
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400852 self._vlan_filter_task = reactor.callLater(_STARTUP_RETRY_WAIT,
Girish Gowdrae933cd32019-11-21 21:04:41 +0530853 self._add_vlan_filter_task, device, uni_port.port_number,
Girish Gowdraa73ee452019-12-20 18:52:17 +0530854 uni_port, _set_vlan_vid, tp_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500855
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400856 self.log.info('setting-vlan-tag')
Girish Gowdraa73ee452019-12-20 18:52:17 +0530857 self._vlan_filter_task = BrcmVlanFilterTask(self.omci_agent, self, uni_port, _set_vlan_vid, tp_id)
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400858 self._deferred = self._onu_omci_device.task_runner.queue_task(self._vlan_filter_task)
859 self._deferred.addCallbacks(success, failure)
860 else:
861 self.log.info('tp-service-specific-task-not-done-adding-request-to-local-cache',
862 uni_id=uni_id)
Matt Jeanneret810148b2019-09-29 12:44:01 -0400863 self._queued_vlan_filter_task[uni_id] = {"device": device,
Girish Gowdrae933cd32019-11-21 21:04:41 +0530864 "uni_id": uni_id,
Chaitrashree G S8fb96782019-08-19 00:10:49 -0400865 "uni_port": uni_port,
Girish Gowdraa73ee452019-12-20 18:52:17 +0530866 "set_vlan_vid": _set_vlan_vid,
867 "tp_id": tp_id}
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500868
869 def get_tx_id(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700870 self.log.debug('get-tx-id')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500871 self.tx_id += 1
872 return self.tx_id
873
Matt Jeannereta32441c2019-03-07 05:16:37 -0500874 def process_inter_adapter_message(self, request):
Matteo Scandolod8d73172019-11-26 12:15:15 -0700875 self.log.debug('process-inter-adapter-message', type=request.header.type, from_topic=request.header.from_topic,
876 to_topic=request.header.to_topic, to_device_id=request.header.to_device_id)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500877 try:
878 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
879 omci_msg = InterAdapterOmciMessage()
880 request.body.Unpack(omci_msg)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700881 self.log.debug('inter-adapter-recv-omci', omci_msg=hexify(omci_msg.message))
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500882
Matt Jeannereta32441c2019-03-07 05:16:37 -0500883 self.receive_message(omci_msg.message)
884
885 elif request.header.type == InterAdapterMessageType.ONU_IND_REQUEST:
886 onu_indication = OnuIndication()
887 request.body.Unpack(onu_indication)
Matteo Scandolod8d73172019-11-26 12:15:15 -0700888 self.log.debug('inter-adapter-recv-onu-ind', onu_id=onu_indication.onu_id,
889 oper_state=onu_indication.oper_state, admin_state=onu_indication.admin_state,
890 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500891
892 if onu_indication.oper_state == "up":
893 self.create_interface(onu_indication)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530894 elif onu_indication.oper_state == "down" or onu_indication.oper_state == "unreachable":
Matt Jeannereta32441c2019-03-07 05:16:37 -0500895 self.update_interface(onu_indication)
896 else:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700897 self.log.error("unknown-onu-indication", onu_id=onu_indication.onu_id,
898 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500899
Matt Jeanneret3bfebff2019-04-12 18:25:03 -0400900 elif request.header.type == InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST:
901 tech_msg = InterAdapterTechProfileDownloadMessage()
902 request.body.Unpack(tech_msg)
903 self.log.debug('inter-adapter-recv-tech-profile', tech_msg=tech_msg)
904
905 self.load_and_configure_tech_profile(tech_msg.uni_id, tech_msg.path)
906
Girish Gowdrae933cd32019-11-21 21:04:41 +0530907 elif request.header.type == InterAdapterMessageType.DELETE_GEM_PORT_REQUEST:
908 del_gem_msg = InterAdapterDeleteGemPortMessage()
909 request.body.Unpack(del_gem_msg)
910 self.log.debug('inter-adapter-recv-del-gem', gem_del_msg=del_gem_msg)
911
912 self.delete_tech_profile(uni_id=del_gem_msg.uni_id,
913 gem_port_id=del_gem_msg.gem_port_id,
914 tp_path=del_gem_msg.tp_path)
915
916 elif request.header.type == InterAdapterMessageType.DELETE_TCONT_REQUEST:
917 del_tcont_msg = InterAdapterDeleteTcontMessage()
918 request.body.Unpack(del_tcont_msg)
919 self.log.debug('inter-adapter-recv-del-tcont', del_tcont_msg=del_tcont_msg)
920
921 self.delete_tech_profile(uni_id=del_tcont_msg.uni_id,
922 alloc_id=del_tcont_msg.alloc_id,
923 tp_path=del_tcont_msg.tp_path)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500924 else:
925 self.log.error("inter-adapter-unhandled-type", request=request)
926
927 except Exception as e:
928 self.log.exception("error-processing-inter-adapter-message", e=e)
929
930 # Called each time there is an onu "up" indication from the olt handler
931 @inlineCallbacks
932 def create_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500933 self.log.info('create-interface', onu_id=onu_indication.onu_id,
Matteo Scandolod8d73172019-11-26 12:15:15 -0700934 serial_number=onu_indication.serial_number)
Matt Jeannereta32441c2019-03-07 05:16:37 -0500935 self._onu_indication = onu_indication
936
Matt Jeanneretc083f462019-03-11 15:02:01 -0400937 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.ACTIVATING,
938 connect_status=ConnectStatus.REACHABLE)
939
Matt Jeannereta32441c2019-03-07 05:16:37 -0500940 onu_device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500941
942 self.log.debug('starting-openomci-statemachine')
943 self._subscribe_to_events()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500944 onu_device.reason = "starting-openomci"
Girish Gowdrae933cd32019-11-21 21:04:41 +0530945 reactor.callLater(1, self._onu_omci_device.start, onu_device)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700946 yield self.core_proxy.device_reason_update(self.device_id, onu_device.reason)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500947 self._heartbeat.enabled = True
948
949 # Currently called each time there is an onu "down" indication from the olt handler
950 # TODO: possibly other reasons to "update" from the olt?
Matt Jeannereta32441c2019-03-07 05:16:37 -0500951 @inlineCallbacks
952 def update_interface(self, onu_indication):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500953 self.log.info('update-interface', onu_id=onu_indication.onu_id,
Matteo Scandolod8d73172019-11-26 12:15:15 -0700954 serial_number=onu_indication.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500955
Chaitrashree G Sd73fb9b2019-09-09 20:27:30 -0400956 if onu_indication.oper_state == 'down' or onu_indication.oper_state == "unreachable":
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500957 self.log.debug('stopping-openomci-statemachine')
958 reactor.callLater(0, self._onu_omci_device.stop)
959
960 # Let TP download happen again
961 for uni_id in self._tp_service_specific_task:
962 self._tp_service_specific_task[uni_id].clear()
963 for uni_id in self._tech_profile_download_done:
964 self._tech_profile_download_done[uni_id].clear()
965
Matt Jeanneretf4113222019-08-14 19:44:34 -0400966 yield self.disable_ports(lock_ports=False)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700967 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
968 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.DISCOVERED,
969 connect_status=ConnectStatus.UNREACHABLE)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500970 else:
971 self.log.debug('not-changing-openomci-statemachine')
972
973 # Not currently called by olt or anything else
William Kurkian3a206332019-04-29 11:05:47 -0400974 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500975 def remove_interface(self, data):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500976 self.log.info('remove-interface', data=data)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500977
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500978 self.log.debug('stopping-openomci-statemachine')
979 reactor.callLater(0, self._onu_omci_device.stop)
980
981 # Let TP download happen again
982 for uni_id in self._tp_service_specific_task:
983 self._tp_service_specific_task[uni_id].clear()
984 for uni_id in self._tech_profile_download_done:
985 self._tech_profile_download_done[uni_id].clear()
986
Matt Jeanneretf4113222019-08-14 19:44:34 -0400987 yield self.disable_ports(lock_ports=False)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -0700988 yield self.core_proxy.device_reason_update(self.device_id, "stopping-openomci")
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500989
Matt Jeanneretf4113222019-08-14 19:44:34 -0400990 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500991 def disable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -0500992 self.log.info('disable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500993 try:
Matt Jeanneretf4113222019-08-14 19:44:34 -0400994 yield self.disable_ports(lock_ports=True)
995 yield self.core_proxy.device_reason_update(self.device_id, "omci-admin-lock")
996 yield self.core_proxy.device_state_update(self.device_id, oper_status=OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500997
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500998 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -0700999 self.log.exception('exception-in-onu-disable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001000
William Kurkian3a206332019-04-29 11:05:47 -04001001 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001002 def reenable(self, device):
Matt Jeanneret08a8e862019-12-20 14:02:32 -05001003 self.log.info('reenable', device_id=device.id, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001004 try:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001005 yield self.core_proxy.device_state_update(device.id,
1006 oper_status=OperStatus.ACTIVE,
1007 connect_status=ConnectStatus.REACHABLE)
1008 yield self.core_proxy.device_reason_update(self.device_id, 'onu-reenabled')
1009 yield self.enable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001010 except Exception as e:
Matteo Scandolod8d73172019-11-26 12:15:15 -07001011 self.log.exception('exception-in-onu-reenable', exception=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001012
William Kurkian3a206332019-04-29 11:05:47 -04001013 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001014 def reboot(self):
1015 self.log.info('reboot-device')
William Kurkian3a206332019-04-29 11:05:47 -04001016 device = yield self.core_proxy.get_device(self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001017 if device.connect_status != ConnectStatus.REACHABLE:
1018 self.log.error("device-unreachable")
1019 return
1020
William Kurkian3a206332019-04-29 11:05:47 -04001021 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001022 def success(_results):
1023 self.log.info('reboot-success', _results=_results)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001024 yield self.core_proxy.device_reason_update(self.device_id, 'rebooting')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001025
1026 def failure(_reason):
1027 self.log.info('reboot-failure', _reason=_reason)
1028
1029 self._deferred = self._onu_omci_device.reboot()
1030 self._deferred.addCallbacks(success, failure)
1031
William Kurkian3a206332019-04-29 11:05:47 -04001032 @inlineCallbacks
Matt Jeanneretf4113222019-08-14 19:44:34 -04001033 def disable_ports(self, lock_ports=True):
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001034 self.log.info('disable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001035
Matt Jeanneretf4113222019-08-14 19:44:34 -04001036 bus = self._onu_omci_device.event_bus
1037 bus.unsubscribe(self._port_state_subscription)
1038 self._port_state_subscription = None
1039
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001040 # Disable all ports on that device
Matt Jeanneretf4113222019-08-14 19:44:34 -04001041 for port in self.uni_ports:
1042 port.operstatus = OperStatus.UNKNOWN
1043 self.log.info('disable-port', device_id=self.device_id, port=port)
1044
1045 if lock_ports is True:
Matt Jeanneretf4113222019-08-14 19:44:34 -04001046 self.lock_ports(lock=True)
1047
Matt Jeanneret80766692019-05-03 09:58:38 -04001048 yield self.core_proxy.ports_state_update(self.device_id, OperStatus.UNKNOWN)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001049
William Kurkian3a206332019-04-29 11:05:47 -04001050 @inlineCallbacks
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001051 def enable_ports(self):
1052 self.log.info('enable-ports', device_id=self.device_id)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001053
Matt Jeanneretf4113222019-08-14 19:44:34 -04001054 # Listen for UNI link state alarms and set the oper_state based on that rather than assuming all UNI are up
Matt Jeanneretf4113222019-08-14 19:44:34 -04001055 bus = self._onu_omci_device.event_bus
1056 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1057 OnuDeviceEvents.PortEvent)
1058 self._port_state_subscription = bus.subscribe(topic, self.port_state_handler)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001059 self.lock_ports(lock=False)
1060
1061 # TODO: Currently the only VEIP capable ONU i can test with does not send UNI link state alarms
1062 # or set operational-state per OMCI spec. So i have know way of "knowing" if the port is up.
1063 # Given this i assume its always up for now. Maybe a software upgrade will fix my onu...
1064 for port in self.uni_ports:
1065 if port.type.value == UniType.VEIP.value:
1066 port.operstatus = OperStatus.ACTIVE
1067 self.log.warn('force-showing-veip-link-up', device_id=self.device_id, port=port)
1068 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, port.port_number, port.operstatus)
1069
1070 @inlineCallbacks
1071 def port_state_handler(self, _topic, msg):
1072 self.log.info("port-state-change", _topic=_topic, msg=msg)
1073
1074 onu_id = msg['onu_id']
1075 port_no = msg['port_number']
1076 serial_number = msg['serial_number']
1077 port_status = msg['port_status']
1078 uni_port = self.uni_port(int(port_no))
1079
1080 self.log.debug("port-state-parsed-message", onu_id=onu_id, port_no=port_no, serial_number=serial_number,
1081 port_status=port_status)
1082
1083 if port_status is True:
1084 uni_port.operstatus = OperStatus.ACTIVE
1085 self.log.info('link-up', device_id=self.device_id, port=uni_port)
1086 else:
1087 uni_port.operstatus = OperStatus.UNKNOWN
1088 self.log.info('link-down', device_id=self.device_id, port=uni_port)
1089
1090 yield self.core_proxy.port_state_update(self.device_id, Port.ETHERNET_UNI, uni_port.port_number, uni_port.operstatus)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001091
1092 # Called just before openomci state machine is started. These listen for events from selected state machines,
1093 # most importantly, mib in sync. Which ultimately leads to downloading the mib
1094 def _subscribe_to_events(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001095 self.log.debug('subscribe-to-events')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001096
1097 # OMCI MIB Database sync status
1098 bus = self._onu_omci_device.event_bus
1099 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1100 OnuDeviceEvents.MibDatabaseSyncEvent)
1101 self._in_sync_subscription = bus.subscribe(topic, self.in_sync_handler)
1102
1103 # OMCI Capabilities
1104 bus = self._onu_omci_device.event_bus
1105 topic = OnuDeviceEntry.event_bus_topic(self.device_id,
1106 OnuDeviceEvents.OmciCapabilitiesEvent)
1107 self._capabilities_subscription = bus.subscribe(topic, self.capabilties_handler)
1108
1109 # Called when the mib is in sync
1110 def in_sync_handler(self, _topic, msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001111 self.log.debug('in-sync-handler', _topic=_topic, msg=msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001112 if self._in_sync_subscription is not None:
1113 try:
1114 in_sync = msg[IN_SYNC_KEY]
1115
1116 if in_sync:
1117 # Only call this once
1118 bus = self._onu_omci_device.event_bus
1119 bus.unsubscribe(self._in_sync_subscription)
1120 self._in_sync_subscription = None
1121
1122 # Start up device_info load
1123 self.log.debug('running-mib-sync')
1124 reactor.callLater(0, self._mib_in_sync)
1125
1126 except Exception as e:
1127 self.log.exception('in-sync', e=e)
1128
1129 def capabilties_handler(self, _topic, _msg):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001130 self.log.debug('capabilities-handler', _topic=_topic, msg=_msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001131 if self._capabilities_subscription is not None:
1132 self.log.debug('capabilities-handler-done')
1133
1134 # Mib is in sync, we can now query what we learned and actually start pushing ME (download) to the ONU.
1135 # Currently uses a basic mib download task that create a bridge with a single gem port and uni, only allowing EAP
1136 # Implement your own MibDownloadTask if you wish to setup something different by default
Matt Jeanneretc083f462019-03-11 15:02:01 -04001137 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001138 def _mib_in_sync(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001139 self.log.debug('mib-in-sync')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001140
1141 omci = self._onu_omci_device
1142 in_sync = omci.mib_db_in_sync
1143
Matt Jeanneretc083f462019-03-11 15:02:01 -04001144 device = yield self.core_proxy.get_device(self.device_id)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001145 yield self.core_proxy.device_reason_update(self.device_id, 'discovery-mibsync-complete')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001146
1147 if not self._dev_info_loaded:
1148 self.log.info('loading-device-data-from-mib', in_sync=in_sync, already_loaded=self._dev_info_loaded)
1149
1150 omci_dev = self._onu_omci_device
1151 config = omci_dev.configuration
1152
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001153 try:
1154
1155 # sort the lists so we get consistent port ordering.
1156 ani_list = sorted(config.ani_g_entities) if config.ani_g_entities else []
1157 uni_list = sorted(config.uni_g_entities) if config.uni_g_entities else []
1158 pptp_list = sorted(config.pptp_entities) if config.pptp_entities else []
1159 veip_list = sorted(config.veip_entities) if config.veip_entities else []
1160
1161 if ani_list is None or (pptp_list is None and veip_list is None):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001162 self.log.warn("no-ani-or-unis")
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001163 yield self.core_proxy.device_reason_update(self.device_id, 'onu-missing-required-elements')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001164 raise Exception("onu-missing-required-elements")
1165
1166 # Currently logging the ani, pptp, veip, and uni for information purposes.
1167 # Actually act on the veip/pptp as its ME is the most correct one to use in later tasks.
1168 # And in some ONU the UNI-G list is incomplete or incorrect...
1169 for entity_id in ani_list:
1170 ani_value = config.ani_g_entities[entity_id]
1171 self.log.debug("discovered-ani", entity_id=entity_id, value=ani_value)
1172 # TODO: currently only one OLT PON port/ANI, so this works out. With NGPON there will be 2..?
1173 self._total_tcont_count = ani_value.get('total-tcont-count')
1174 self.log.debug("set-total-tcont-count", tcont_count=self._total_tcont_count)
1175
1176 for entity_id in uni_list:
1177 uni_value = config.uni_g_entities[entity_id]
1178 self.log.debug("discovered-uni", entity_id=entity_id, value=uni_value)
1179
1180 uni_entities = OrderedDict()
1181 for entity_id in pptp_list:
1182 pptp_value = config.pptp_entities[entity_id]
1183 self.log.debug("discovered-pptp", entity_id=entity_id, value=pptp_value)
1184 uni_entities[entity_id] = UniType.PPTP
1185
1186 for entity_id in veip_list:
1187 veip_value = config.veip_entities[entity_id]
1188 self.log.debug("discovered-veip", entity_id=entity_id, value=veip_value)
1189 uni_entities[entity_id] = UniType.VEIP
1190
1191 uni_id = 0
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -05001192 for entity_id, uni_type in uni_entities.items():
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001193 try:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001194 yield self._add_uni_port(device, entity_id, uni_id, uni_type)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001195 uni_id += 1
1196 except AssertionError as e:
1197 self.log.warn("could not add UNI", entity_id=entity_id, uni_type=uni_type, e=e)
1198
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001199 self._qos_flexibility = config.qos_configuration_flexibility or 0
1200 self._omcc_version = config.omcc_version or OMCCVersion.Unknown
1201
1202 if self._unis:
1203 self._dev_info_loaded = True
1204 else:
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001205 yield self.core_proxy.device_reason_update(self.device_id, 'no-usable-unis')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001206 self.log.warn("no-usable-unis")
1207 raise Exception("no-usable-unis")
1208
1209 except Exception as e:
1210 self.log.exception('device-info-load', e=e)
1211 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1212
1213 else:
1214 self.log.info('device-info-already-loaded', in_sync=in_sync, already_loaded=self._dev_info_loaded)
1215
1216 if self._dev_info_loaded:
Matt Jeanneretad9a0f12019-05-09 14:05:49 -04001217 if device.admin_state == AdminState.PREPROVISIONED or device.admin_state == AdminState.ENABLED:
Matt Jeanneretc083f462019-03-11 15:02:01 -04001218
1219 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001220 def success(_results):
1221 self.log.info('mib-download-success', _results=_results)
Matt Jeanneretc083f462019-03-11 15:02:01 -04001222 yield self.core_proxy.device_state_update(device.id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301223 oper_status=OperStatus.ACTIVE,
1224 connect_status=ConnectStatus.REACHABLE)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001225 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-downloaded')
Matt Jeanneretf4113222019-08-14 19:44:34 -04001226 yield self.enable_ports()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001227 self._mib_download_task = None
Devmalya Paulffc89df2019-07-31 17:43:13 -04001228 yield self.onu_active_event()
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001229
Matt Jeanneretc083f462019-03-11 15:02:01 -04001230 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001231 def failure(_reason):
1232 self.log.warn('mib-download-failure-retrying', _reason=_reason)
Mahir Gunyel0e6882a2019-10-16 17:02:39 -07001233 yield self.core_proxy.device_reason_update(self.device_id, 'initial-mib-download-failure-retrying')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001234 self._deferred = reactor.callLater(_STARTUP_RETRY_WAIT, self._mib_in_sync)
1235
Matt Jeanneretf4113222019-08-14 19:44:34 -04001236 # start by locking all the unis till mib sync and initial mib is downloaded
1237 # this way we can capture the port down/up events when we are ready
1238 self.lock_ports(lock=True)
1239
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001240 # Download an initial mib that creates simple bridge that can pass EAP. On success (above) finally set
1241 # the device to active/reachable. This then opens up the handler to openflow pushes from outside
1242 self.log.info('downloading-initial-mib-configuration')
1243 self._mib_download_task = BrcmMibDownloadTask(self.omci_agent, self)
1244 self._deferred = self._onu_omci_device.task_runner.queue_task(self._mib_download_task)
1245 self._deferred.addCallbacks(success, failure)
1246 else:
1247 self.log.info('admin-down-disabling')
1248 self.disable(device)
1249 else:
1250 self.log.info('device-info-not-loaded-skipping-mib-download')
1251
Matt Jeanneretc083f462019-03-11 15:02:01 -04001252 @inlineCallbacks
1253 def _add_uni_port(self, device, entity_id, uni_id, uni_type=UniType.PPTP):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001254 self.log.debug('add-uni-port')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001255
Matt Jeanneretc083f462019-03-11 15:02:01 -04001256 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 -05001257
1258 # TODO: Some or parts of this likely need to move to UniPort. especially the format stuff
1259 uni_name = "uni-{}".format(uni_no)
1260
Girish Gowdrae933cd32019-11-21 21:04:41 +05301261 mac_bridge_port_num = uni_id + 1 # TODO +1 is only to test non-zero index
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001262
1263 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 -04001264 entity_id=entity_id, mac_bridge_port_num=mac_bridge_port_num, serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001265
1266 uni_port = UniPort.create(self, uni_name, uni_id, uni_no, uni_name, uni_type)
1267 uni_port.entity_id = entity_id
1268 uni_port.enabled = True
1269 uni_port.mac_bridge_port_num = mac_bridge_port_num
1270
1271 self.log.debug("created-uni-port", uni=uni_port)
1272
Matt Jeanneretc083f462019-03-11 15:02:01 -04001273 yield self.core_proxy.port_created(device.id, uni_port.get_port())
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001274
1275 self._unis[uni_port.port_number] = uni_port
1276
1277 self._onu_omci_device.alarm_synchronizer.set_alarm_params(onu_id=self._onu_indication.onu_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +05301278 uni_ports=self.uni_ports,
1279 serial_number=device.serial_number)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001280
Matt Jeanneretc083f462019-03-11 15:02:01 -04001281 # TODO NEW CORE: Figure out how to gain this knowledge from the olt. for now cheat terribly.
1282 def mk_uni_port_num(self, intf_id, onu_id, uni_id):
Amit Ghosh65400f12019-11-21 12:04:12 +00001283 MAX_PONS_PER_OLT = 256
1284 MAX_ONUS_PER_PON = 256
Matt Jeanneretc083f462019-03-11 15:02:01 -04001285 MAX_UNIS_PER_ONU = 16
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001286
Matt Jeanneretc083f462019-03-11 15:02:01 -04001287 assert intf_id < MAX_PONS_PER_OLT
1288 assert onu_id < MAX_ONUS_PER_PON
1289 assert uni_id < MAX_UNIS_PER_ONU
Amit Ghosh65400f12019-11-21 12:04:12 +00001290 return intf_id << 12 | onu_id << 4 | uni_id
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001291
1292 @inlineCallbacks
Devmalya Paulffc89df2019-07-31 17:43:13 -04001293 def onu_active_event(self):
Matteo Scandolod8d73172019-11-26 12:15:15 -07001294 self.log.debug('onu-active-event')
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001295 try:
1296 device = yield self.core_proxy.get_device(self.device_id)
1297 parent_device = yield self.core_proxy.get_device(self.parent_id)
1298 olt_serial_number = parent_device.serial_number
Devmalya Paulffc89df2019-07-31 17:43:13 -04001299 raised_ts = arrow.utcnow().timestamp
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001300
1301 self.log.debug("onu-indication-context-data",
Girish Gowdrae933cd32019-11-21 21:04:41 +05301302 pon_id=self._onu_indication.intf_id,
1303 onu_id=self._onu_indication.onu_id,
1304 registration_id=self.device_id,
1305 device_id=self.device_id,
1306 onu_serial_number=device.serial_number,
1307 olt_serial_number=olt_serial_number,
1308 raised_ts=raised_ts)
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001309
Devmalya Paulffc89df2019-07-31 17:43:13 -04001310 self.log.debug("Trying-to-raise-onu-active-event")
1311 OnuActiveEvent(self.events, self.device_id,
Devmalya Paul7e0be4a2019-05-08 05:18:04 -04001312 self._onu_indication.intf_id,
1313 device.serial_number,
1314 str(self.device_id),
Girish Gowdrae933cd32019-11-21 21:04:41 +05301315 olt_serial_number, raised_ts,
Devmalya Paulffc89df2019-07-31 17:43:13 -04001316 onu_id=self._onu_indication.onu_id).send(True)
1317 except Exception as active_event_error:
1318 self.log.exception('onu-activated-event-error',
1319 errmsg=active_event_error.message)
Matt Jeanneretf4113222019-08-14 19:44:34 -04001320
1321 def lock_ports(self, lock=True):
1322
1323 def success(response):
1324 self.log.debug('set-onu-ports-state', lock=lock, response=response)
1325
1326 def failure(response):
1327 self.log.error('cannot-set-onu-ports-state', lock=lock, response=response)
1328
1329 task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=lock)
1330 self._deferred = self._onu_omci_device.task_runner.queue_task(task)
1331 self._deferred.addCallbacks(success, failure)