blob: d7f9c1c1daf2eb4b01b3cb1a3972b88e4d993076 [file] [log] [blame]
William Kurkian6f436d02019-02-06 16:25:01 -05001#
2# Copyright 2018 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#
16import threading
17import binascii
18import grpc
19import socket
20import re
21import structlog
William Kurkianfefd4642019-02-07 15:30:03 -050022import time
William Kurkian6f436d02019-02-06 16:25:01 -050023from twisted.internet import reactor
William Kurkian92bd7122019-02-14 15:26:59 -050024from twisted.internet.defer import inlineCallbacks, returnValue
William Kurkian6f436d02019-02-06 16:25:01 -050025from scapy.layers.l2 import Ether, Dot1Q
26from transitions import Machine
27
William Kurkian8b1690c2019-03-04 16:53:22 -050028from voltha_protos import openolt_pb2_grpc, openolt_pb2
William Kurkian6f436d02019-02-06 16:25:01 -050029
William Kurkian44cd7bb2019-02-11 16:39:12 -050030from pyvoltha.adapters.extensions.alarms.onu.onu_discovery_alarm import OnuDiscoveryAlarm
William Kurkian6f436d02019-02-06 16:25:01 -050031
William Kurkian44cd7bb2019-02-11 16:39:12 -050032from pyvoltha.common.utils.nethelpers import mac_str_to_tuple
William Kurkian8b1690c2019-03-04 16:53:22 -050033from voltha_protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \
William Kurkian6f436d02019-02-06 16:25:01 -050034 OFPPS_LINK_DOWN, OFPPF_1GB_FD, \
35 OFPC_GROUP_STATS, OFPC_PORT_STATS, OFPC_TABLE_STATS, OFPC_FLOW_STATS, \
36 ofp_switch_features, ofp_port, ofp_port_stats, ofp_desc
William Kurkian44cd7bb2019-02-11 16:39:12 -050037from pyvoltha.common.utils.registry import registry
William Kurkian8b1690c2019-03-04 16:53:22 -050038from voltha_protos.common_pb2 import AdminState, OperStatus, ConnectStatus
William Kurkian8b1690c2019-03-04 16:53:22 -050039from voltha_protos.device_pb2 import Port, Device
Matt Jeannerete33a7092019-03-12 21:54:14 -040040from voltha_protos.inter_container_pb2 import SwitchCapability, PortCapability, \
41 InterAdapterMessageType, InterAdapterOmciMessage
William Kurkian8b1690c2019-03-04 16:53:22 -050042from voltha_protos.logical_device_pb2 import LogicalDevice, LogicalPort
William Kurkian6f436d02019-02-06 16:25:01 -050043
Matt Jeanneret9fd36df2019-02-14 19:14:36 -050044
William Kurkian6f436d02019-02-06 16:25:01 -050045class OpenoltDevice(object):
46 """
47 OpenoltDevice state machine:
48
49 null ----> init ------> connected -----> up -----> down
50 ^ ^ | ^ | |
51 | | | | | |
52 | +-------------+ +---------+ |
53 | |
54 +-----------------------------------------+
55 """
56 # pylint: disable=too-many-instance-attributes
57 # pylint: disable=R0904
58 states = [
59 'state_null',
60 'state_init',
61 'state_connected',
62 'state_up',
63 'state_down']
64
65 transitions = [
66 {'trigger': 'go_state_init',
67 'source': ['state_null', 'state_connected', 'state_down'],
68 'dest': 'state_init',
69 'before': 'do_state_init',
70 'after': 'post_init'},
71 {'trigger': 'go_state_connected',
72 'source': 'state_init',
73 'dest': 'state_connected',
74 'before': 'do_state_connected'},
75 {'trigger': 'go_state_up',
76 'source': ['state_connected', 'state_down'],
77 'dest': 'state_up',
78 'before': 'do_state_up'},
79 {'trigger': 'go_state_down',
80 'source': ['state_up'],
81 'dest': 'state_down',
82 'before': 'do_state_down',
83 'after': 'post_down'}]
84
85 def __init__(self, **kwargs):
86 super(OpenoltDevice, self).__init__()
87
Matt Jeanneretbad3d982019-03-11 16:06:10 -040088 self.core_proxy = kwargs['core_proxy']
Matt Jeanneret7906d232019-02-14 14:57:38 -050089 self.adapter_proxy = kwargs['adapter_proxy']
William Kurkian6f436d02019-02-06 16:25:01 -050090 self.device_num = kwargs['device_num']
serkant.uluderyadcfc74d2019-03-17 23:41:42 -070091 self.device = kwargs['device']
William Kurkian6f436d02019-02-06 16:25:01 -050092
93 self.platform_class = kwargs['support_classes']['platform']
94 self.resource_mgr_class = kwargs['support_classes']['resource_mgr']
95 self.flow_mgr_class = kwargs['support_classes']['flow_mgr']
96 self.alarm_mgr_class = kwargs['support_classes']['alarm_mgr']
97 self.stats_mgr_class = kwargs['support_classes']['stats_mgr']
98 self.bw_mgr_class = kwargs['support_classes']['bw_mgr']
Matt Jeanneret7906d232019-02-14 14:57:38 -050099
Matt Jeanneretb428b952019-03-07 05:14:17 -0500100 self.seen_discovery_indications = []
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500101 self.stub = None
William Kurkian27522582019-02-25 14:24:32 -0500102 self.connected = False
William Kurkian6f436d02019-02-06 16:25:01 -0500103 is_reconciliation = kwargs.get('reconciliation', False)
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700104 self.device_id = self.device.id
105 self.host_and_port = self.device.host_and_port
106 self.extra_args = self.device.extra_args
Matt Jeannerete33a7092019-03-12 21:54:14 -0400107 self.device_info = None
William Kurkian6f436d02019-02-06 16:25:01 -0500108 self.log = structlog.get_logger(id=self.device_id,
109 ip=self.host_and_port)
Matt Jeanneret6e315092019-02-20 10:42:57 -0500110
William Kurkian6f436d02019-02-06 16:25:01 -0500111 self.log.info('openolt-device-init')
112
113 # default device id and device serial number. If device_info provides better results, they will be updated
114 self.dpid = kwargs.get('dp_id')
115 self.serial_number = self.host_and_port # FIXME
116
117 # Device already set in the event of reconciliation
118 if not is_reconciliation:
119 self.log.info('updating-device')
120 # It is a new device
121 # Update device
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700122 self.device.root = True
123 self.device.connect_status = ConnectStatus.UNREACHABLE
124 self.device.oper_status = OperStatus.ACTIVATING
William Kurkian6f436d02019-02-06 16:25:01 -0500125
126 # If logical device does exist use it, else create one after connecting to device
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700127 if self.device.parent_id:
William Kurkian6f436d02019-02-06 16:25:01 -0500128 # logical device already exists
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700129 self.logical_device_id = self.device.parent_id
William Kurkian6f436d02019-02-06 16:25:01 -0500130 if is_reconciliation:
131 self.adapter_agent.reconcile_logical_device(
132 self.logical_device_id)
133
134 # Initialize the OLT state machine
135 self.machine = Machine(model=self, states=OpenoltDevice.states,
136 transitions=OpenoltDevice.transitions,
137 send_event=True, initial='state_null')
138 self.go_state_init()
139
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500140 @inlineCallbacks
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500141 def create_logical_device(self, device_info):
William Kurkian6f436d02019-02-06 16:25:01 -0500142 dpid = device_info.device_id
143 serial_number = device_info.device_serial_number
144
145 if dpid is None: dpid = self.dpid
146 if serial_number is None: serial_number = self.serial_number
147
148 if dpid == None or dpid == '':
149 uri = self.host_and_port.split(":")[0]
150 try:
151 socket.inet_pton(socket.AF_INET, uri)
152 dpid = '00:00:' + self.ip_hex(uri)
153 except socket.error:
154 # this is not an IP
155 dpid = self.stringToMacAddr(uri)
156
157 if serial_number == None or serial_number == '':
158 serial_number = self.host_and_port
159
160 self.log.info('creating-openolt-logical-device', dp_id=dpid, serial_number=serial_number)
161
162 mfr_desc = device_info.vendor
163 sw_desc = device_info.firmware_version
164 hw_desc = device_info.model
165 if device_info.hardware_version: hw_desc += '-' + device_info.hardware_version
William Kurkian92bd7122019-02-14 15:26:59 -0500166
William Kurkian6f436d02019-02-06 16:25:01 -0500167 # Create logical OF device
168 ld = LogicalDevice(
169 root_device_id=self.device_id,
170 switch_features=ofp_switch_features(
171 n_buffers=256, # TODO fake for now
172 n_tables=2, # TODO ditto
173 capabilities=( # TODO and ditto
174 OFPC_FLOW_STATS
175 | OFPC_TABLE_STATS
176 | OFPC_PORT_STATS
177 | OFPC_GROUP_STATS
178 )
179 ),
180 desc=ofp_desc(
181 serial_num=serial_number
182 )
183 )
184 ld_init = self.adapter_agent.create_logical_device(ld,
William Kurkian92bd7122019-02-14 15:26:59 -0500185 dpid=dpid)
186
William Kurkian6f436d02019-02-06 16:25:01 -0500187 self.logical_device_id = ld_init.id
188
William Kurkian27522582019-02-25 14:24:32 -0500189 ##Moved setting serial number outside of the logical_device function
190 #device = yield self.adapter_agent.get_device(self.device_id)
191 #device.serial_number = serial_number
192 #yield self.adapter_agent.update_device(device)
William Kurkian6f436d02019-02-06 16:25:01 -0500193
194 self.dpid = dpid
195 self.serial_number = serial_number
196
197 self.log.info('created-openolt-logical-device', logical_device_id=ld_init.id)
198
199 def stringToMacAddr(self, uri):
200 regex = re.compile('[^a-zA-Z]')
201 uri = regex.sub('', uri)
202
203 l = len(uri)
204 if l > 6:
205 uri = uri[0:6]
206 else:
207 uri = uri + uri[0:6 - l]
208
William Kurkian6f436d02019-02-06 16:25:01 -0500209 return ":".join([hex(ord(x))[-2:] for x in uri])
210
211 def do_state_init(self, event):
212 # Initialize gRPC
Matt Jeanneret7906d232019-02-14 14:57:38 -0500213 self.log.debug("grpc-host-port", self.host_and_port)
William Kurkian6f436d02019-02-06 16:25:01 -0500214 self.channel = grpc.insecure_channel(self.host_and_port)
215 self.channel_ready_future = grpc.channel_ready_future(self.channel)
216
217 self.log.info('openolt-device-created', device_id=self.device_id)
218
219 def post_init(self, event):
220 self.log.debug('post_init')
221
222 # We have reached init state, starting the indications thread
223
224 # Catch RuntimeError exception
225 try:
226 # Start indications thread
227 self.indications_thread_handle = threading.Thread(
228 target=self.indications_thread)
229 # Old getter/setter API for daemon; use it directly as a
230 # property instead. The Jinkins error will happon on the reason of
231 # Exception in thread Thread-1 (most likely raised # during
232 # interpreter shutdown)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500233 self.log.debug('starting indications thread')
William Kurkian6f436d02019-02-06 16:25:01 -0500234 self.indications_thread_handle.setDaemon(True)
235 self.indications_thread_handle.start()
236 except Exception as e:
237 self.log.exception('post_init failed', e=e)
238
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500239 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500240 def do_state_connected(self, event):
241 self.log.debug("do_state_connected")
William Kurkian27522582019-02-25 14:24:32 -0500242
William Kurkian6f436d02019-02-06 16:25:01 -0500243 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
244
William Kurkianfefd4642019-02-07 15:30:03 -0500245 delay = 1
246 while True:
247 try:
Matt Jeannerete33a7092019-03-12 21:54:14 -0400248 self.device_info = self.stub.GetDeviceInfo(openolt_pb2.Empty())
William Kurkianfefd4642019-02-07 15:30:03 -0500249 break
250 except Exception as e:
251 reraise = True
252 if delay > 120:
253 self.log.error("gRPC failure too many times")
254 else:
255 self.log.warn("gRPC failure, retry in %ds: %s"
256 % (delay, repr(e)))
257 time.sleep(delay)
258 delay += delay
259 reraise = False
260
261 if reraise:
262 raise
263
Matt Jeannerete33a7092019-03-12 21:54:14 -0400264 self.log.info('Device connected', device_info=self.device_info)
William Kurkian6f436d02019-02-06 16:25:01 -0500265
Matt Jeanneret7906d232019-02-14 14:57:38 -0500266 # self.create_logical_device(device_info)
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700267 self.logical_device_id = '0'
William Kurkian27522582019-02-25 14:24:32 -0500268
Matt Jeannerete33a7092019-03-12 21:54:14 -0400269 serial_number = self.device_info.device_serial_number
William Kurkian27522582019-02-25 14:24:32 -0500270 if serial_number is None:
271 serial_number = self.serial_number
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700272 self.device.serial_number = serial_number
William Kurkian27522582019-02-25 14:24:32 -0500273
274 self.serial_number = serial_number
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700275
276 self.device.root = True
277 self.device.vendor = self.device_info.vendor
278 self.device.model = self.device_info.model
279 self.device.hardware_version = self.device_info.hardware_version
280 self.device.firmware_version = self.device_info.firmware_version
William Kurkian27522582019-02-25 14:24:32 -0500281
282 # TODO: check for uptime and reboot if too long (VOL-1192)
283
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700284 self.device.connect_status = ConnectStatus.REACHABLE
285 self.device.mac_address = "AA:BB:CC:DD:EE:FF"
286 yield self.core_proxy.device_update(self.device)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500287
William Kurkian6f436d02019-02-06 16:25:01 -0500288 self.resource_mgr = self.resource_mgr_class(self.device_id,
289 self.host_and_port,
290 self.extra_args,
Matt Jeannerete33a7092019-03-12 21:54:14 -0400291 self.device_info)
William Kurkian6f436d02019-02-06 16:25:01 -0500292 self.platform = self.platform_class(self.log, self.resource_mgr)
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400293 self.flow_mgr = self.flow_mgr_class(self.core_proxy, self.log,
William Kurkian6f436d02019-02-06 16:25:01 -0500294 self.stub, self.device_id,
295 self.logical_device_id,
296 self.platform, self.resource_mgr)
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700297
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400298 self.alarm_mgr = self.alarm_mgr_class(self.log, self.core_proxy,
William Kurkian6f436d02019-02-06 16:25:01 -0500299 self.device_id,
300 self.logical_device_id,
serkant.uluderyadcfc74d2019-03-17 23:41:42 -0700301 self.platform,
302 self.serial_number)
Matt Jeanneret7906d232019-02-14 14:57:38 -0500303 self.stats_mgr = self.stats_mgr_class(self, self.log, self.platform)
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400304 self.bw_mgr = self.bw_mgr_class(self.log, self.core_proxy)
William Kurkian27522582019-02-25 14:24:32 -0500305
306 self.connected = True
Matt Jeanneret6e315092019-02-20 10:42:57 -0500307
308 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500309 def do_state_up(self, event):
310 self.log.debug("do_state_up")
311
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400312 yield self.core_proxy.device_state_update(self.device_id,
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500313 connect_status=ConnectStatus.REACHABLE,
314 oper_status=OperStatus.ACTIVE)
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500315 self.log.debug("done_state_up")
William Kurkian6f436d02019-02-06 16:25:01 -0500316
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500317 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500318 def do_state_down(self, event):
319 self.log.debug("do_state_down")
320 oper_state = OperStatus.UNKNOWN
321 connect_state = ConnectStatus.UNREACHABLE
322
323 # Propagating to the children
324
325 # Children ports
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500326 child_devices = yield self.adapter_agent.get_child_devices(self.device_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500327 for onu_device in child_devices:
328 onu_adapter_agent = \
329 registry('adapter_loader').get_agent(onu_device.adapter)
330 onu_adapter_agent.update_interface(onu_device,
331 {'oper_state': 'down'})
332 self.onu_ports_down(onu_device, oper_state)
333
334 # Children devices
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500335 yield self.adapter_agent.update_child_devices_state(
William Kurkian6f436d02019-02-06 16:25:01 -0500336 self.device_id, oper_status=oper_state,
337 connect_status=connect_state)
338 # Device Ports
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500339 device_ports = yield self.adapter_agent.get_ports(self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500340 Port.ETHERNET_NNI)
341 logical_ports_ids = [port.label for port in device_ports]
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500342 device_ports += yield self.adapter_agent.get_ports(self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500343 Port.PON_OLT)
344
345 for port in device_ports:
346 port.oper_status = oper_state
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500347 yield self.adapter_agent.add_port(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500348
349 # Device logical port
350 for logical_port_id in logical_ports_ids:
351 logical_port = self.adapter_agent.get_logical_port(
352 self.logical_device_id, logical_port_id)
353 logical_port.ofp_port.state = OFPPS_LINK_DOWN
354 self.adapter_agent.update_logical_port(self.logical_device_id,
355 logical_port)
356
357 # Device
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500358 device = yield self.adapter_agent.get_device(self.device_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500359 device.oper_status = oper_state
360 device.connect_status = connect_state
361
William Kurkianfefd4642019-02-07 15:30:03 -0500362 reactor.callLater(2, self.adapter_agent.device_update, device)
William Kurkian6f436d02019-02-06 16:25:01 -0500363
364 # def post_up(self, event):
365 # self.log.debug('post-up')
366 # self.flow_mgr.reseed_flows()
367
368 def post_down(self, event):
369 self.log.debug('post_down')
370 self.flow_mgr.reset_flows()
371
372 def indications_thread(self):
373 self.log.debug('starting-indications-thread')
374 self.log.debug('connecting to olt', device_id=self.device_id)
375 self.channel_ready_future.result() # blocking call
376 self.log.info('connected to olt', device_id=self.device_id)
377 self.go_state_connected()
378
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500379 # TODO: thread timing issue. stub isnt ready yet from above go_state_connected (which doesnt block)
William Kurkian27522582019-02-25 14:24:32 -0500380 # Don't continue until connected is done
381 while (not self.connected):
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500382 time.sleep(0.5)
383
William Kurkian6f436d02019-02-06 16:25:01 -0500384 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
385
386 while True:
387 try:
388 # get the next indication from olt
389 ind = next(self.indications)
390 except Exception as e:
391 self.log.warn('gRPC connection lost', error=e)
392 reactor.callFromThread(self.go_state_down)
393 reactor.callFromThread(self.go_state_init)
394 break
395 else:
396 self.log.debug("rx indication", indication=ind)
397
398 # indication handlers run in the main event loop
399 if ind.HasField('olt_ind'):
400 reactor.callFromThread(self.olt_indication, ind.olt_ind)
401 elif ind.HasField('intf_ind'):
402 reactor.callFromThread(self.intf_indication, ind.intf_ind)
403 elif ind.HasField('intf_oper_ind'):
404 reactor.callFromThread(self.intf_oper_indication,
405 ind.intf_oper_ind)
406 elif ind.HasField('onu_disc_ind'):
407 reactor.callFromThread(self.onu_discovery_indication,
408 ind.onu_disc_ind)
409 elif ind.HasField('onu_ind'):
410 reactor.callFromThread(self.onu_indication, ind.onu_ind)
411 elif ind.HasField('omci_ind'):
412 reactor.callFromThread(self.omci_indication, ind.omci_ind)
413 elif ind.HasField('pkt_ind'):
414 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
415 elif ind.HasField('port_stats'):
416 reactor.callFromThread(
417 self.stats_mgr.port_statistics_indication,
418 ind.port_stats)
419 elif ind.HasField('flow_stats'):
420 reactor.callFromThread(
421 self.stats_mgr.flow_statistics_indication,
422 ind.flow_stats)
423 elif ind.HasField('alarm_ind'):
424 reactor.callFromThread(self.alarm_mgr.process_alarms,
425 ind.alarm_ind)
426 else:
427 self.log.warn('unknown indication type')
428
429 def olt_indication(self, olt_indication):
430 if olt_indication.oper_state == "up":
431 self.go_state_up()
432 elif olt_indication.oper_state == "down":
433 self.go_state_down()
434
435 def intf_indication(self, intf_indication):
436 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
437 oper_state=intf_indication.oper_state)
438
439 if intf_indication.oper_state == "up":
440 oper_status = OperStatus.ACTIVE
441 else:
442 oper_status = OperStatus.DISCOVERED
443
444 # add_port update the port if it exists
445 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
446
447 def intf_oper_indication(self, intf_oper_indication):
448 self.log.debug("Received interface oper state change indication",
449 intf_id=intf_oper_indication.intf_id,
450 type=intf_oper_indication.type,
451 oper_state=intf_oper_indication.oper_state)
452
453 if intf_oper_indication.oper_state == "up":
454 oper_state = OperStatus.ACTIVE
455 else:
456 oper_state = OperStatus.DISCOVERED
457
458 if intf_oper_indication.type == "nni":
459
460 # add_(logical_)port update the port if it exists
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500461 self.add_port(intf_oper_indication.intf_id,
462 Port.ETHERNET_NNI, oper_state)
Matt Jeanneret6e315092019-02-20 10:42:57 -0500463
William Kurkian6f436d02019-02-06 16:25:01 -0500464 elif intf_oper_indication.type == "pon":
465 # FIXME - handle PON oper state change
466 pass
467
Matt Jeanneret6e315092019-02-20 10:42:57 -0500468 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500469 def onu_discovery_indication(self, onu_disc_indication):
470 intf_id = onu_disc_indication.intf_id
471 serial_number = onu_disc_indication.serial_number
472
473 serial_number_str = self.stringify_serial_number(serial_number)
474
475 self.log.debug("onu discovery indication", intf_id=intf_id,
476 serial_number=serial_number_str)
477
Matt Jeanneretb428b952019-03-07 05:14:17 -0500478 if serial_number_str in self.seen_discovery_indications:
479 self.log.debug("skipping-seen-onu-discovery-indication", intf_id=intf_id,
480 serial_number=serial_number_str)
481 return
482 else:
483 self.seen_discovery_indications.append(serial_number_str)
484
William Kurkian6f436d02019-02-06 16:25:01 -0500485 # Post ONU Discover alarm 20180809_0805
486 try:
487 OnuDiscoveryAlarm(self.alarm_mgr.alarms, pon_id=intf_id,
488 serial_number=serial_number_str).raise_alarm()
489 except Exception as disc_alarm_error:
490 self.log.exception("onu-discovery-alarm-error",
491 errmsg=disc_alarm_error.message)
492 # continue for now.
493
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400494 onu_device = yield self.core_proxy.get_child_device(
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500495 self.device_id,
496 serial_number=serial_number_str)
William Kurkian6f436d02019-02-06 16:25:01 -0500497
498 if onu_device is None:
499 try:
500 onu_id = self.resource_mgr.get_onu_id(intf_id)
501 if onu_id is None:
502 raise Exception("onu-id-unavailable")
503
504 self.add_onu_device(
505 intf_id,
506 self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
507 onu_id, serial_number)
508 self.activate_onu(intf_id, onu_id, serial_number,
509 serial_number_str)
510 except Exception as e:
511 self.log.exception('onu-activation-failed', e=e)
512
513 else:
514 if onu_device.connect_status != ConnectStatus.REACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400515 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.REACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500516
517 onu_id = onu_device.proxy_address.onu_id
518 if onu_device.oper_status == OperStatus.DISCOVERED \
519 or onu_device.oper_status == OperStatus.ACTIVATING:
520 self.log.debug("ignore onu discovery indication, \
521 the onu has been discovered and should be \
522 activating shorlty", intf_id=intf_id,
523 onu_id=onu_id, state=onu_device.oper_status)
524 elif onu_device.oper_status == OperStatus.ACTIVE:
525 self.log.warn("onu discovery indication whereas onu is \
526 supposed to be active",
527 intf_id=intf_id, onu_id=onu_id,
528 state=onu_device.oper_status)
529 elif onu_device.oper_status == OperStatus.UNKNOWN:
530 self.log.info("onu in unknown state, recovering from olt \
531 reboot probably, activate onu", intf_id=intf_id,
532 onu_id=onu_id, serial_number=serial_number_str)
533
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400534 yield self.core_proxy.device_state_update(onu_device.id, oper_status=OperStatus.DISCOVERED)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500535
William Kurkian6f436d02019-02-06 16:25:01 -0500536 try:
537 self.activate_onu(intf_id, onu_id, serial_number,
538 serial_number_str)
539 except Exception as e:
540 self.log.error('onu-activation-error',
541 serial_number=serial_number_str, error=e)
542 else:
543 self.log.warn('unexpected state', onu_id=onu_id,
544 onu_device_oper_state=onu_device.oper_status)
545
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500546 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500547 def onu_indication(self, onu_indication):
548 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
549 onu_id=onu_indication.onu_id,
550 serial_number=onu_indication.serial_number,
551 oper_state=onu_indication.oper_state,
552 admin_state=onu_indication.admin_state)
553 try:
554 serial_number_str = self.stringify_serial_number(
555 onu_indication.serial_number)
556 except Exception as e:
557 serial_number_str = None
558
559 if serial_number_str is not None:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400560 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500561 self.device_id,
562 serial_number=serial_number_str)
563 else:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400564 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500565 self.device_id,
566 parent_port_no=self.platform.intf_id_to_port_no(
567 onu_indication.intf_id, Port.PON_OLT),
568 onu_id=onu_indication.onu_id)
569
570 if onu_device is None:
571 self.log.error('onu not found', intf_id=onu_indication.intf_id,
572 onu_id=onu_indication.onu_id)
573 return
574
575 if self.platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
576 != onu_indication.intf_id:
577 self.log.warn('ONU-is-on-a-different-intf-id-now',
578 previous_intf_id=self.platform.intf_id_from_pon_port_no(
579 onu_device.parent_port_no),
580 current_intf_id=onu_indication.intf_id)
581 # FIXME - handle intf_id mismatch (ONU move?)
582
583 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
584 # FIXME - handle onu id mismatch
585 self.log.warn('ONU-id-mismatch, can happen if both voltha and '
586 'the olt rebooted',
587 expected_onu_id=onu_device.proxy_address.onu_id,
588 received_onu_id=onu_indication.onu_id)
589
590 # Admin state
591 if onu_indication.admin_state == 'down':
592 if onu_indication.oper_state != 'down':
593 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
594 oper_state=onu_indication.oper_state)
595 # Forcing the oper state change code to execute
596 onu_indication.oper_state = 'down'
597
598 # Port and logical port update is taken care of by oper state block
599
600 elif onu_indication.admin_state == 'up':
601 pass
602
603 else:
604 self.log.warn('Invalid-or-not-implemented-admin-state',
605 received_admin_state=onu_indication.admin_state)
606
607 self.log.debug('admin-state-dealt-with')
608
William Kurkian6f436d02019-02-06 16:25:01 -0500609 # Operating state
610 if onu_indication.oper_state == 'down':
611
612 if onu_device.connect_status != ConnectStatus.UNREACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400613 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.UNREACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500614
615 # Move to discovered state
616 self.log.debug('onu-oper-state-is-down')
617
618 if onu_device.oper_status != OperStatus.DISCOVERED:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400619 yield self.core_proxy.device_state_update(onu_device.id, oper_status=OperStatus.DISCOVERED)
William Kurkian6f436d02019-02-06 16:25:01 -0500620
Matt Jeanneretb428b952019-03-07 05:14:17 -0500621 self.log.debug('inter-adapter-send-onu-ind', onu_indication=onu_indication)
622
623 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
624 yield self.adapter_proxy.send_inter_adapter_message(
625 msg=onu_indication,
626 type=InterAdapterMessageType.ONU_IND_REQUEST,
627 from_adapter="openolt",
628 to_adapter=onu_device.type,
629 to_device_id=onu_device.id
630 )
William Kurkian6f436d02019-02-06 16:25:01 -0500631
632 elif onu_indication.oper_state == 'up':
633
634 if onu_device.connect_status != ConnectStatus.REACHABLE:
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400635 yield self.core_proxy.device_state_update(onu_device.id, connect_status=ConnectStatus.REACHABLE)
William Kurkian6f436d02019-02-06 16:25:01 -0500636
637 if onu_device.oper_status != OperStatus.DISCOVERED:
638 self.log.debug("ignore onu indication",
639 intf_id=onu_indication.intf_id,
640 onu_id=onu_indication.onu_id,
641 state=onu_device.oper_status,
642 msg_oper_state=onu_indication.oper_state)
643 return
644
Matt Jeanneretb428b952019-03-07 05:14:17 -0500645 self.log.debug('inter-adapter-send-onu-ind', onu_indication=onu_indication)
William Kurkian6f436d02019-02-06 16:25:01 -0500646
Matt Jeanneretb428b952019-03-07 05:14:17 -0500647 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
648 yield self.adapter_proxy.send_inter_adapter_message(
649 msg=onu_indication,
650 type=InterAdapterMessageType.ONU_IND_REQUEST,
651 from_adapter="openolt",
652 to_adapter=onu_device.type,
653 to_device_id=onu_device.id
654 )
William Kurkian6f436d02019-02-06 16:25:01 -0500655
656 else:
657 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
658 oper_state=onu_indication.oper_state)
659
660 def onu_ports_down(self, onu_device, oper_state):
661 # Set port oper state to Discovered
662 # add port will update port if it exists
663 # self.adapter_agent.add_port(
664 # self.device_id,
665 # Port(
666 # port_no=uni_no,
667 # label=uni_name,
668 # type=Port.ETHERNET_UNI,
669 # admin_state=onu_device.admin_state,
670 # oper_status=oper_state))
671 # TODO this should be downning ports in onu adatper
672
673 # Disable logical port
674 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
675 for onu_port in onu_ports:
676 self.log.debug('onu-ports-down', onu_port=onu_port)
677 onu_port_id = onu_port.label
678 try:
679 onu_logical_port = self.adapter_agent.get_logical_port(
680 logical_device_id=self.logical_device_id, port_id=onu_port_id)
681 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
682 self.adapter_agent.update_logical_port(
683 logical_device_id=self.logical_device_id,
684 port=onu_logical_port)
685 self.log.debug('cascading-oper-state-to-port-and-logical-port')
686 except KeyError as e:
687 self.log.error('matching-onu-port-label-invalid',
688 onu_id=onu_device.id, olt_id=self.device_id,
689 onu_ports=onu_ports, onu_port_id=onu_port_id,
690 error=e)
691
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500692 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500693 def omci_indication(self, omci_indication):
694
695 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
696 onu_id=omci_indication.onu_id)
697
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400698 onu_device = yield self.core_proxy.get_child_device(
William Kurkian6f436d02019-02-06 16:25:01 -0500699 self.device_id, onu_id=omci_indication.onu_id,
700 parent_port_no=self.platform.intf_id_to_port_no(
701 omci_indication.intf_id, Port.PON_OLT), )
702
Matt Jeanneretb428b952019-03-07 05:14:17 -0500703 omci_msg = InterAdapterOmciMessage(message=omci_indication.pkt)
704
705 self.log.debug('inter-adapter-send-omci', omci_msg=omci_msg)
706
707 # TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
708 yield self.adapter_proxy.send_inter_adapter_message(
709 msg=omci_msg,
710 type=InterAdapterMessageType.OMCI_REQUEST,
711 from_adapter="openolt",
712 to_adapter=onu_device.type,
713 to_device_id=onu_device.id
714 )
William Kurkian6f436d02019-02-06 16:25:01 -0500715
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500716 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500717 def packet_indication(self, pkt_indication):
718
719 self.log.debug("packet indication",
720 intf_type=pkt_indication.intf_type,
721 intf_id=pkt_indication.intf_id,
722 port_no=pkt_indication.port_no,
723 cookie=pkt_indication.cookie,
724 gemport_id=pkt_indication.gemport_id,
725 flow_id=pkt_indication.flow_id)
726
727 if pkt_indication.intf_type == "pon":
728 if pkt_indication.port_no:
729 logical_port_num = pkt_indication.port_no
730 else: # TODO Remove this else block after openolt device has been fully rolled out with cookie protobuf change
731 try:
732 onu_id_uni_id = self.resource_mgr.get_onu_uni_from_ponport_gemport(pkt_indication.intf_id,
733 pkt_indication.gemport_id)
734 onu_id = int(onu_id_uni_id[0])
735 uni_id = int(onu_id_uni_id[1])
736 self.log.debug("packet indication-kv", onu_id=onu_id, uni_id=uni_id)
737 if onu_id is None:
738 raise Exception("onu-id-none")
739 if uni_id is None:
740 raise Exception("uni-id-none")
741 logical_port_num = self.platform.mk_uni_port_num(pkt_indication.intf_id, onu_id, uni_id)
742 except Exception as e:
743 self.log.error("no-onu-reference-for-gem",
744 gemport_id=pkt_indication.gemport_id, e=e)
745 return
746
747
748 elif pkt_indication.intf_type == "nni":
749 logical_port_num = self.platform.intf_id_to_port_no(
750 pkt_indication.intf_id,
751 Port.ETHERNET_NNI)
752
753 pkt = Ether(pkt_indication.pkt)
754
755 self.log.debug("packet indication",
756 logical_device_id=self.logical_device_id,
757 logical_port_no=logical_port_num)
758
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500759 yield self.adapter_agent.send_packet_in(
William Kurkian6f436d02019-02-06 16:25:01 -0500760 logical_device_id=self.logical_device_id,
761 logical_port_no=logical_port_num,
762 packet=str(pkt))
763
764 def packet_out(self, egress_port, msg):
765 pkt = Ether(msg)
766 self.log.debug('packet out', egress_port=egress_port,
767 device_id=self.device_id,
768 logical_device_id=self.logical_device_id,
769 packet=str(pkt).encode("HEX"))
770
771 # Find port type
772 egress_port_type = self.platform.intf_id_to_port_type_name(egress_port)
773 if egress_port_type == Port.ETHERNET_UNI:
774
775 if pkt.haslayer(Dot1Q):
776 outer_shim = pkt.getlayer(Dot1Q)
777 if isinstance(outer_shim.payload, Dot1Q):
778 # If double tag, remove the outer tag
779 payload = (
780 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
781 outer_shim.payload
782 )
783 else:
784 payload = pkt
785 else:
786 payload = pkt
787
788 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
789
790 self.log.debug(
791 'sending-packet-to-ONU', egress_port=egress_port,
792 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
793 onu_id=self.platform.onu_id_from_port_num(egress_port),
794 uni_id=self.platform.uni_id_from_port_num(egress_port),
795 port_no=egress_port,
796 packet=str(payload).encode("HEX"))
797
798 onu_pkt = openolt_pb2.OnuPacket(
799 intf_id=self.platform.intf_id_from_uni_port_num(egress_port),
800 onu_id=self.platform.onu_id_from_port_num(egress_port),
801 port_no=egress_port,
802 pkt=send_pkt)
803
804 self.stub.OnuPacketOut(onu_pkt)
805
806 elif egress_port_type == Port.ETHERNET_NNI:
807 self.log.debug('sending-packet-to-uplink', egress_port=egress_port,
808 packet=str(pkt).encode("HEX"))
809
810 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
811
812 uplink_pkt = openolt_pb2.UplinkPacket(
813 intf_id=self.platform.intf_id_from_nni_port_num(egress_port),
814 pkt=send_pkt)
815
816 self.stub.UplinkPacketOut(uplink_pkt)
817
818 else:
819 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
820 egress_port=egress_port,
821 port_type=egress_port_type)
822
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500823 @inlineCallbacks
Matt Jeanneretb428b952019-03-07 05:14:17 -0500824 def process_inter_adapter_message(self, request):
825 self.log.debug('process-inter-adapter-message', msg=request)
826 try:
827 if request.header.type == InterAdapterMessageType.OMCI_REQUEST:
828 omci_msg = InterAdapterOmciMessage()
829 request.body.Unpack(omci_msg)
830 self.log.debug('inter-adapter-recv-omci', omci_msg=omci_msg)
831
832 onu_device_id = request.header.to_device_id
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400833 onu_device = yield self.core_proxy.get_device(onu_device_id)
Matt Jeanneretb428b952019-03-07 05:14:17 -0500834 self.send_proxied_message(onu_device, omci_msg.message)
835
836 else:
837 self.log.error("inter-adapter-unhandled-type", request=request)
838
839 except Exception as e:
840 self.log.exception("error-processing-inter-adapter-message", e=e)
841
842 def send_proxied_message(self, onu_device, msg):
843
William Kurkian6f436d02019-02-06 16:25:01 -0500844 if onu_device.connect_status != ConnectStatus.REACHABLE:
845 self.log.debug('ONU is not reachable, cannot send OMCI',
846 serial_number=onu_device.serial_number,
847 intf_id=onu_device.proxy_address.channel_id,
848 onu_id=onu_device.proxy_address.onu_id)
849 return
Matt Jeanneretb428b952019-03-07 05:14:17 -0500850
851 omci = openolt_pb2.OmciMsg(intf_id=onu_device.proxy_address.channel_id,
852 onu_id=onu_device.proxy_address.onu_id, pkt=str(msg))
William Kurkian6f436d02019-02-06 16:25:01 -0500853 self.stub.OmciMsgOut(omci)
854
Matt Jeanneretb428b952019-03-07 05:14:17 -0500855 self.log.debug("omci-message-sent", intf_id=onu_device.proxy_address.channel_id,
856 onu_id=onu_device.proxy_address.onu_id, pkt=str(msg))
857
Matt Jeanneret6e315092019-02-20 10:42:57 -0500858 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500859 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500860 self.log.info("adding-onu", port_no=port_no, onu_id=onu_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500861 serial_number=serial_number)
862
William Kurkian6f436d02019-02-06 16:25:01 -0500863 serial_number_str = self.stringify_serial_number(serial_number)
864
Matt Jeanneretb428b952019-03-07 05:14:17 -0500865 # TODO NEW CORE dont hardcode child device type. find some way of determining by vendor in serial number
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400866 yield self.core_proxy.child_device_detected(
Matt Jeanneret6e315092019-02-20 10:42:57 -0500867 parent_device_id=self.device_id,
868 parent_port_no=port_no,
869 child_device_type='brcm_openomci_onu',
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500870 channel_id=intf_id,
871 vendor_id=serial_number.vendor_id,
872 serial_number=serial_number_str,
873 onu_id=onu_id
William Kurkian6f436d02019-02-06 16:25:01 -0500874 )
875
Matt Jeanneret5ba87e32019-02-28 11:35:49 -0500876 self.log.debug("onu-added", onu_id=onu_id, port_no=port_no, serial_number=serial_number_str)
877
Matt Jeannerete33a7092019-03-12 21:54:14 -0400878 def get_ofp_device_info(self, device):
879 self.log.info('get_ofp_device_info', device_id=device.id)
880
881 mfr_desc = self.device_info.vendor
882 sw_desc = self.device_info.firmware_version
883 hw_desc = self.device_info.model
884 if self.device_info.hardware_version: hw_desc += '-' + self.device_info.hardware_version
885
886 return SwitchCapability(
887 desc=ofp_desc(
888 hw_desc=hw_desc,
889 sw_desc=sw_desc,
890 serial_num=device.serial_number
891 ),
892 switch_features=ofp_switch_features(
893 n_buffers=256, # Max packets buffered at once # TODO fake for now
894 n_tables=2, # Number of tables supported by datapath # TODO fake for now
895 capabilities=( #Bitmap of support "ofp_capabilities" # TODO fake for now
896 OFPC_FLOW_STATS
897 | OFPC_TABLE_STATS
898 | OFPC_PORT_STATS
899 | OFPC_GROUP_STATS
900 )
901 )
902 )
903
904 def get_ofp_port_info(self, device, port_no):
905 self.log.info('get_ofp_port_info', port_no=port_no, device_id=device.id)
906 cap = OFPPF_1GB_FD | OFPPF_FIBER
907 return PortCapability(
908 port=LogicalPort(
909 ofp_port=ofp_port(
910 hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
911 config=0,
912 state=OFPPS_LIVE,
913 curr=cap,
914 advertised=cap,
915 peer=cap,
916 curr_speed=OFPPF_1GB_FD,
917 max_speed=OFPPF_1GB_FD
918 ),
919 device_id=device.id,
920 device_port_no=port_no
921 )
922 )
923
William Kurkian6f436d02019-02-06 16:25:01 -0500924 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
925 if port_type is Port.ETHERNET_NNI:
926 return "nni-" + str(port_no)
927 elif port_type is Port.PON_OLT:
928 return "pon" + str(intf_id)
929 elif port_type is Port.ETHERNET_UNI:
930 assert False, 'local UNI management not supported'
931
932 def add_logical_port(self, port_no, intf_id, oper_state):
933 self.log.info('adding-logical-port', port_no=port_no)
934
935 label = self.port_name(port_no, Port.ETHERNET_NNI)
936
937 cap = OFPPF_1GB_FD | OFPPF_FIBER
938 curr_speed = OFPPF_1GB_FD
939 max_speed = OFPPF_1GB_FD
940
941 if oper_state == OperStatus.ACTIVE:
942 of_oper_state = OFPPS_LIVE
943 else:
944 of_oper_state = OFPPS_LINK_DOWN
945
946 ofp = ofp_port(
947 port_no=port_no,
948 hw_addr=mac_str_to_tuple(self._get_mac_form_port_no(port_no)),
949 name=label, config=0, state=of_oper_state, curr=cap,
950 advertised=cap, peer=cap, curr_speed=curr_speed,
951 max_speed=max_speed)
952
953 ofp_stats = ofp_port_stats(port_no=port_no)
954
955 logical_port = LogicalPort(
956 id=label, ofp_port=ofp, device_id=self.device_id,
957 device_port_no=port_no, root_port=True,
958 ofp_port_stats=ofp_stats)
959
960 self.adapter_agent.add_logical_port(self.logical_device_id,
961 logical_port)
962
963 def _get_mac_form_port_no(self, port_no):
964 mac = ''
965 for i in range(4):
966 mac = ':%02x' % ((port_no >> (i * 8)) & 0xff) + mac
967 return '00:00' + mac
968
William Kurkian92bd7122019-02-14 15:26:59 -0500969 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500970 def add_port(self, intf_id, port_type, oper_status):
971 port_no = self.platform.intf_id_to_port_no(intf_id, port_type)
972
973 label = self.port_name(port_no, port_type, intf_id)
974
975 self.log.debug('adding-port', port_no=port_no, label=label,
976 port_type=port_type)
977
978 port = Port(port_no=port_no, label=label, type=port_type,
979 admin_state=AdminState.ENABLED, oper_status=oper_status)
980
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400981 yield self.core_proxy.port_created(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -0500982
983 def delete_logical_port(self, child_device):
984 logical_ports = self.proxy.get('/logical_devices/{}/ports'.format(
985 self.logical_device_id))
986 for logical_port in logical_ports:
987 if logical_port.device_id == child_device.id:
988 self.log.debug('delete-logical-port',
989 onu_device_id=child_device.id,
990 logical_port=logical_port)
991 self.flow_mgr.clear_flows_and_scheduler_for_logical_port(
992 child_device, logical_port)
993 self.adapter_agent.delete_logical_port(
994 self.logical_device_id, logical_port)
995 return
996
Matt Jeanneretd2f155b2019-02-22 13:49:09 -0500997 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500998 def delete_port(self, child_serial_number):
999 ports = self.proxy.get('/devices/{}/ports'.format(
1000 self.device_id))
1001 for port in ports:
1002 if port.label == child_serial_number:
1003 self.log.debug('delete-port',
1004 onu_serial_number=child_serial_number,
1005 port=port)
Matt Jeanneretd2f155b2019-02-22 13:49:09 -05001006 yield self.adapter_agent.delete_port(self.device_id, port)
William Kurkian6f436d02019-02-06 16:25:01 -05001007 return
1008
1009 def update_flow_table(self, flows):
1010 self.log.debug('No updates here now, all is done in logical flows '
1011 'update')
1012
1013 def update_logical_flows(self, flows_to_add, flows_to_remove,
1014 device_rules_map):
1015 if not self.is_state_up():
1016 self.log.info('The OLT is not up, we cannot update flows',
1017 flows_to_add=[f.id for f in flows_to_add],
1018 flows_to_remove=[f.id for f in flows_to_remove])
1019 return
1020
1021 try:
1022 self.flow_mgr.update_children_flows(device_rules_map)
1023 except Exception as e:
1024 self.log.error('Error updating children flows', error=e)
1025
1026 self.log.debug('logical flows update', flows_to_add=flows_to_add,
1027 flows_to_remove=flows_to_remove)
1028
1029 for flow in flows_to_add:
1030
1031 try:
1032 self.flow_mgr.add_flow(flow)
1033 except Exception as e:
1034 self.log.error('failed to add flow', flow=flow, e=e)
1035
1036 for flow in flows_to_remove:
1037
1038 try:
1039 self.flow_mgr.remove_flow(flow)
1040 except Exception as e:
1041 self.log.error('failed to remove flow', flow=flow, e=e)
1042
1043 self.flow_mgr.repush_all_different_flows()
1044
1045 # There has to be a better way to do this
1046 def ip_hex(self, ip):
1047 octets = ip.split(".")
1048 hex_ip = []
1049 for octet in octets:
1050 octet_hex = hex(int(octet))
1051 octet_hex = octet_hex.split('0x')[1]
1052 octet_hex = octet_hex.rjust(2, '0')
1053 hex_ip.append(octet_hex)
1054 return ":".join(hex_ip)
1055
1056 def stringify_vendor_specific(self, vendor_specific):
1057 return ''.join(str(i) for i in [
1058 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
1059 hex(ord(vendor_specific[0]) & 0x0f)[2:],
1060 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
1061 hex(ord(vendor_specific[1]) & 0x0f)[2:],
1062 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
1063 hex(ord(vendor_specific[2]) & 0x0f)[2:],
1064 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
1065 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
1066
1067 def stringify_serial_number(self, serial_number):
1068 return ''.join([serial_number.vendor_id,
1069 self.stringify_vendor_specific(
1070 serial_number.vendor_specific)])
1071
1072 def destringify_serial_number(self, serial_number_str):
1073 serial_number = openolt_pb2.SerialNumber(
1074 vendor_id=serial_number_str[:4].encode('utf-8'),
1075 vendor_specific=binascii.unhexlify(serial_number_str[4:]))
1076 return serial_number
1077
1078 def disable(self):
1079 self.log.debug('sending-deactivate-olt-message',
1080 device_id=self.device_id)
1081
1082 try:
1083 # Send grpc call
1084 self.stub.DisableOlt(openolt_pb2.Empty())
1085 # The resulting indication will bring the OLT down
1086 # self.go_state_down()
1087 self.log.info('openolt device disabled')
1088 except Exception as e:
1089 self.log.error('Failure to disable openolt device', error=e)
1090
1091 def delete(self):
1092 self.log.info('deleting-olt', device_id=self.device_id,
1093 logical_device_id=self.logical_device_id)
1094
1095 # Clears up the data from the resource manager KV store
1096 # for the device
1097 del self.resource_mgr
1098
1099 try:
1100 # Rebooting to reset the state
1101 self.reboot()
1102 # Removing logical device
1103 ld = self.adapter_agent.get_logical_device(self.logical_device_id)
1104 self.adapter_agent.delete_logical_device(ld)
1105 except Exception as e:
1106 self.log.error('Failure to delete openolt device', error=e)
1107 raise e
1108 else:
1109 self.log.info('successfully-deleted-olt', device_id=self.device_id)
1110
1111 def reenable(self):
1112 self.log.debug('reenabling-olt', device_id=self.device_id)
1113
1114 try:
1115 self.stub.ReenableOlt(openolt_pb2.Empty())
1116
William Kurkian6f436d02019-02-06 16:25:01 -05001117 except Exception as e:
1118 self.log.error('Failure to reenable openolt device', error=e)
1119 else:
1120 self.log.info('openolt device reenabled')
1121
1122 def activate_onu(self, intf_id, onu_id, serial_number,
1123 serial_number_str):
1124 pir = self.bw_mgr.pir(serial_number_str)
1125 self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
1126 serial_number_str=serial_number_str,
1127 serial_number=serial_number, pir=pir)
1128 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
1129 serial_number=serial_number, pir=pir)
1130 self.stub.ActivateOnu(onu)
1131 self.log.info('onu-activated', serial_number=serial_number_str)
1132
Matt Jeanneretd2f155b2019-02-22 13:49:09 -05001133 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -05001134 def delete_child_device(self, child_device):
1135 self.log.debug('sending-deactivate-onu',
1136 olt_device_id=self.device_id,
1137 onu_device=child_device,
1138 onu_serial_number=child_device.serial_number)
1139 try:
Matt Jeanneretd2f155b2019-02-22 13:49:09 -05001140 yield self.adapter_agent.delete_child_device(self.device_id,
William Kurkian6f436d02019-02-06 16:25:01 -05001141 child_device.id,
1142 child_device)
1143 except Exception as e:
1144 self.log.error('adapter_agent error', error=e)
1145 try:
1146 self.delete_logical_port(child_device)
1147 except Exception as e:
1148 self.log.error('logical_port delete error', error=e)
1149 try:
1150 self.delete_port(child_device.serial_number)
1151 except Exception as e:
1152 self.log.error('port delete error', error=e)
1153 serial_number = self.destringify_serial_number(
1154 child_device.serial_number)
1155 # TODO FIXME - For each uni.
1156 # TODO FIXME - Flows are not deleted
1157 uni_id = 0 # FIXME
1158 self.flow_mgr.delete_tech_profile_instance(
Matt Jeanneret7906d232019-02-14 14:57:38 -05001159 child_device.proxy_address.channel_id,
1160 child_device.proxy_address.onu_id,
1161 uni_id
William Kurkian6f436d02019-02-06 16:25:01 -05001162 )
1163 pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
1164 child_device.proxy_address.onu_id,
1165 uni_id)
1166 # Free any PON resources that were reserved for the ONU
1167 self.resource_mgr.free_pon_resources_for_onu(pon_intf_id_onu_id)
1168
1169 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
1170 onu_id=child_device.proxy_address.onu_id,
1171 serial_number=serial_number)
1172 self.stub.DeleteOnu(onu)
1173
1174 def reboot(self):
1175 self.log.debug('rebooting openolt device', device_id=self.device_id)
1176 try:
1177 self.stub.Reboot(openolt_pb2.Empty())
1178 except Exception as e:
1179 self.log.error('something went wrong with the reboot', error=e)
1180 else:
1181 self.log.info('device rebooted')
1182
1183 def trigger_statistics_collection(self):
1184 try:
1185 self.stub.CollectStatistics(openolt_pb2.Empty())
1186 except Exception as e:
1187 self.log.error('Error while triggering statistics collection',
1188 error=e)
1189 else:
1190 self.log.info('statistics requested')
1191
1192 def simulate_alarm(self, alarm):
1193 self.alarm_mgr.simulate_alarm(alarm)