blob: 5971b6834d4b959cc84726fee5c81653ec473ee5 [file] [log] [blame]
Shad Ansari2825d012018-02-22 23:57:46 +00001#
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#
16
Shad Ansari2825d012018-02-22 23:57:46 +000017import threading
Shad Ansari94250fc2018-07-04 06:52:11 +000018import binascii
Shad Ansari2825d012018-02-22 23:57:46 +000019import grpc
Shad Ansari2825d012018-02-22 23:57:46 +000020
Shad Ansari94250fc2018-07-04 06:52:11 +000021import structlog
Shad Ansari15928d12018-04-17 02:42:13 +000022from twisted.internet import reactor
Shad Ansari0346f0d2018-04-26 06:54:09 +000023from scapy.layers.l2 import Ether, Dot1Q
Shad Ansari94250fc2018-07-04 06:52:11 +000024from transitions import Machine, State
Shad Ansari15928d12018-04-17 02:42:13 +000025
Shad Ansari2825d012018-02-22 23:57:46 +000026from voltha.protos.device_pb2 import Port, Device
27from voltha.protos.common_pb2 import OperStatus, AdminState, ConnectStatus
28from voltha.protos.logical_device_pb2 import LogicalDevice
Shad Ansarif9d2d102018-06-13 02:15:26 +000029from voltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, \
30 OFPPS_LINK_DOWN, OFPPF_1GB_FD, OFPC_GROUP_STATS, OFPC_PORT_STATS, \
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -040031 OFPC_TABLE_STATS, OFPC_FLOW_STATS, ofp_switch_features, ofp_port, \
Jonathan Hart05845412018-07-19 09:55:43 -070032 ofp_port_stats, ofp_desc
Shad Ansarif9d2d102018-06-13 02:15:26 +000033from voltha.protos.logical_device_pb2 import LogicalPort
Shad Ansari2825d012018-02-22 23:57:46 +000034from voltha.core.logical_device_agent import mac_str_to_tuple
35from voltha.registry import registry
36from voltha.adapters.openolt.protos import openolt_pb2_grpc, openolt_pb2
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040037from voltha.protos.bbf_fiber_tcont_body_pb2 import TcontsConfigData
38from voltha.protos.bbf_fiber_gemport_body_pb2 import GemportsConfigData
39from voltha.protos.bbf_fiber_base_pb2 import VEnetConfig
Shad Ansari2825d012018-02-22 23:57:46 +000040import voltha.core.flow_decomposer as fd
41
Nicolas Palpacuere761c902018-07-05 16:30:52 -040042from voltha.adapters.openolt.openolt_statistics import OpenOltStatisticsMgr
Shad Ansari94250fc2018-07-04 06:52:11 +000043import voltha.adapters.openolt.openolt_platform as platform
44from voltha.adapters.openolt.openolt_flow_mgr import OpenOltFlowMgr, \
45 DEFAULT_MGMT_VLAN
46from voltha.adapters.openolt.openolt_alarms import OpenOltAlarmMgr
Shad Ansarif9d2d102018-06-13 02:15:26 +000047
48
Shad Ansari2825d012018-02-22 23:57:46 +000049class OpenoltDevice(object):
Shad Ansari94250fc2018-07-04 06:52:11 +000050 """
51 OpenoltDevice state machine:
Shad Ansari2825d012018-02-22 23:57:46 +000052
Shad Ansari94250fc2018-07-04 06:52:11 +000053 null ----> init ------> connected -----> up -----> down
54 ^ ^ | ^ | |
55 | | | | | |
56 | +-------------+ +---------+ |
57 | |
58 +-----------------------------------------+
59 """
60 # pylint: disable=too-many-instance-attributes
61 # pylint: disable=R0904
62 states = [
63 'state_null',
64 'state_init',
65 'state_connected',
66 'state_up',
67 'state_down']
68
Shad Ansari22efe832018-05-19 05:37:03 +000069 transitions = [
Shad Ansari94250fc2018-07-04 06:52:11 +000070 {'trigger': 'go_state_init',
71 'source': ['state_null', 'state_connected', 'state_down'],
72 'dest': 'state_init',
73 'before': 'do_state_init'},
74 {'trigger': 'go_state_connected',
75 'source': 'state_init',
76 'dest': 'state_connected',
77 'before': 'do_state_connected'},
78 {'trigger': 'go_state_up',
79 'source': ['state_connected', 'state_down'],
80 'dest': 'state_up',
81 'before': 'do_state_up'},
82 {'trigger': 'go_state_down',
83 'source': ['state_up'],
84 'dest': 'state_down',
85 'before': 'do_state_down'}]
Shad Ansari22efe832018-05-19 05:37:03 +000086
Shad Ansari2825d012018-02-22 23:57:46 +000087 def __init__(self, **kwargs):
88 super(OpenoltDevice, self).__init__()
89
90 self.adapter_agent = kwargs['adapter_agent']
Shad Ansari5dbc9c82018-05-10 03:29:31 +000091 self.device_num = kwargs['device_num']
Shad Ansari2825d012018-02-22 23:57:46 +000092 device = kwargs['device']
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040093 is_reconciliation = kwargs.get('reconciliation', False)
Shad Ansari2825d012018-02-22 23:57:46 +000094 self.device_id = device.id
95 self.host_and_port = device.host_and_port
Shad Ansarif9d2d102018-06-13 02:15:26 +000096 self.log = structlog.get_logger(id=self.device_id,
97 ip=self.host_and_port)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040098 self.proxy = registry('core').get_proxy('/')
Shad Ansari2825d012018-02-22 23:57:46 +000099
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400100 # Device already set in the event of reconciliation
101 if not is_reconciliation:
102 # It is a new device
103 # Update device
104 device.root = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000105 device.serial_number = self.host_and_port # FIXME
Shad Ansari94250fc2018-07-04 06:52:11 +0000106 device.connect_status = ConnectStatus.UNREACHABLE
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400107 device.oper_status = OperStatus.ACTIVATING
108 self.adapter_agent.update_device(device)
Shad Ansari2825d012018-02-22 23:57:46 +0000109
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400110 # If logical device does not exist create it
Shad Ansari94250fc2018-07-04 06:52:11 +0000111 if not device.parent_id:
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400112
113 dpid = '00:00:' + self.ip_hex(self.host_and_port.split(":")[0])
114
115 # Create logical OF device
116 ld = LogicalDevice(
117 root_device_id=self.device_id,
118 switch_features=ofp_switch_features(
119 n_buffers=256, # TODO fake for now
120 n_tables=2, # TODO ditto
121 capabilities=( # TODO and ditto
122 OFPC_FLOW_STATS
123 | OFPC_TABLE_STATS
124 | OFPC_PORT_STATS
125 | OFPC_GROUP_STATS
126 )
Jonathan Hart05845412018-07-19 09:55:43 -0700127 ),
128 desc=ofp_desc(
129 serial_num=device.serial_number
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400130 )
131 )
132 ld_init = self.adapter_agent.create_logical_device(ld,
133 dpid=dpid)
134 self.logical_device_id = ld_init.id
135 else:
136 # logical device already exists
137 self.logical_device_id = device.parent_id
138 if is_reconciliation:
139 self.adapter_agent.reconcile_logical_device(
140 self.logical_device_id)
141
Shad Ansari94250fc2018-07-04 06:52:11 +0000142 # Initialize the OLT state machine
143 self.machine = Machine(model=self, states=OpenoltDevice.states,
144 transitions=OpenoltDevice.transitions,
145 send_event=True, initial='state_null')
146 self.go_state_init()
147
148 def do_state_init(self, event):
Shad Ansari2825d012018-02-22 23:57:46 +0000149 # Initialize gRPC
150 self.channel = grpc.insecure_channel(self.host_and_port)
Shad Ansari8f1b2532018-04-21 07:51:39 +0000151 self.channel_ready_future = grpc.channel_ready_future(self.channel)
nick47b74372018-05-25 18:22:49 -0400152
Shad Ansari94250fc2018-07-04 06:52:11 +0000153 # Start indications thread
154 self.indications_thread_handle = threading.Thread(
155 target=self.indications_thread)
156 self.indications_thread_handle.daemon = True
157 self.indications_thread_handle.start()
158
159 '''
160 # FIXME - Move to oper_up state without connecting to OLT?
161 if is_reconciliation:
162 # Put state machine in state up
163 reactor.callFromThread(self.go_state_up, reconciliation=True)
164 '''
165
166 self.log.debug('openolt-device-created', device_id=self.device_id)
167
168 def do_state_connected(self, event):
169 device = self.adapter_agent.get_device(self.device_id)
170 device.connect_status = ConnectStatus.REACHABLE
171 self.adapter_agent.update_device(device)
172
173 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400174 self.flow_mgr = OpenOltFlowMgr(self.log, self.stub, self.device_id)
mzadig7cda5ff2018-07-10 16:37:28 -0400175 self.alarm_mgr = OpenOltAlarmMgr(self.log, self.adapter_agent, self.device_id,
176 self.logical_device_id)
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400177 self.stats_mgr = OpenOltStatisticsMgr(self, self.log)
Shad Ansari2825d012018-02-22 23:57:46 +0000178
Shad Ansari94250fc2018-07-04 06:52:11 +0000179 def do_state_up(self, event):
180 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000181
Shad Ansari94250fc2018-07-04 06:52:11 +0000182 # Update phys OF device
183 device.parent_id = self.logical_device_id
184 device.oper_status = OperStatus.ACTIVE
185 self.adapter_agent.update_device(device)
nick47b74372018-05-25 18:22:49 -0400186
Shad Ansari94250fc2018-07-04 06:52:11 +0000187 def do_state_down(self, event):
188 self.log.debug("do_state_down")
189 oper_state = OperStatus.UNKNOWN
190 connect_state = ConnectStatus.UNREACHABLE
Nicolas Palpacuerd35d9bb2018-06-20 17:06:31 -0400191
Shad Ansari94250fc2018-07-04 06:52:11 +0000192 # Propagating to the children
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400193
Shad Ansari94250fc2018-07-04 06:52:11 +0000194 # Children ports
195 child_devices = self.adapter_agent.get_child_devices(self.device_id)
196 for onu_device in child_devices:
197 uni_no = platform.mk_uni_port_num(
198 onu_device.proxy_address.channel_id,
199 onu_device.proxy_address.onu_id)
200 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
201 serial_number=onu_device.serial_number)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000202
Shad Ansari94250fc2018-07-04 06:52:11 +0000203 self.onu_ports_down(onu_device, uni_no, uni_name, oper_state)
204 # Children devices
205 self.adapter_agent.update_child_devices_state(
206 self.device_id, oper_status=oper_state,
207 connect_status=connect_state)
208 # Device Ports
209 device_ports = self.adapter_agent.get_ports(self.device_id,
210 Port.ETHERNET_NNI)
211 logical_ports_ids = [port.label for port in device_ports]
212 device_ports += self.adapter_agent.get_ports(self.device_id,
213 Port.PON_OLT)
214
215 for port in device_ports:
216 port.oper_status = oper_state
217 self.adapter_agent.add_port(self.device_id, port)
218
219 # Device logical port
220 for logical_port_id in logical_ports_ids:
221 logical_port = self.adapter_agent.get_logical_port(
222 self.logical_device_id, logical_port_id)
223 logical_port.ofp_port.state = OFPPS_LINK_DOWN
224 self.adapter_agent.update_logical_port(self.logical_device_id,
225 logical_port)
226
227 # Device
228 device = self.adapter_agent.get_device(self.device_id)
229 device.oper_status = oper_state
230 device.connect_status = connect_state
231
232 self.adapter_agent.update_device(device)
233
234 def indications_thread(self):
nick47b74372018-05-25 18:22:49 -0400235 self.log.debug('starting-indications-thread')
Shad Ansari94250fc2018-07-04 06:52:11 +0000236 self.log.debug('connecting to olt', device_id=self.device_id)
237 self.channel_ready_future.result() # blocking call
238 self.log.debug('connected to olt', device_id=self.device_id)
239 self.go_state_connected()
Shad Ansari2dda4f32018-05-17 07:16:07 +0000240
Shad Ansari15928d12018-04-17 02:42:13 +0000241 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
Shad Ansari2dda4f32018-05-17 07:16:07 +0000242
Shad Ansari94250fc2018-07-04 06:52:11 +0000243 while True:
nick47b74372018-05-25 18:22:49 -0400244 try:
245 # get the next indication from olt
246 ind = next(self.indications)
247 except Exception as e:
Shad Ansari94250fc2018-07-04 06:52:11 +0000248 self.log.warn('gRPC connection lost', error=e)
249 reactor.callFromThread(self.go_state_down)
250 reactor.callFromThread(self.go_state_init)
251 break
nick47b74372018-05-25 18:22:49 -0400252 else:
253 self.log.debug("rx indication", indication=ind)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000254
nick47b74372018-05-25 18:22:49 -0400255 # indication handlers run in the main event loop
256 if ind.HasField('olt_ind'):
257 reactor.callFromThread(self.olt_indication, ind.olt_ind)
258 elif ind.HasField('intf_ind'):
259 reactor.callFromThread(self.intf_indication, ind.intf_ind)
260 elif ind.HasField('intf_oper_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000261 reactor.callFromThread(self.intf_oper_indication,
262 ind.intf_oper_ind)
nick47b74372018-05-25 18:22:49 -0400263 elif ind.HasField('onu_disc_ind'):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000264 reactor.callFromThread(self.onu_discovery_indication,
265 ind.onu_disc_ind)
nick47b74372018-05-25 18:22:49 -0400266 elif ind.HasField('onu_ind'):
267 reactor.callFromThread(self.onu_indication, ind.onu_ind)
268 elif ind.HasField('omci_ind'):
269 reactor.callFromThread(self.omci_indication, ind.omci_ind)
270 elif ind.HasField('pkt_ind'):
271 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400272 elif ind.HasField('port_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400273 reactor.callFromThread(
Shad Ansari94250fc2018-07-04 06:52:11 +0000274 self.stats_mgr.port_statistics_indication,
275 ind.port_stats)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400276 elif ind.HasField('flow_stats'):
Nicolas Palpacuere761c902018-07-05 16:30:52 -0400277 reactor.callFromThread(
Shad Ansari94250fc2018-07-04 06:52:11 +0000278 self.stats_mgr.flow_statistics_indication,
279 ind.flow_stats)
Shad Ansari905b8402018-07-03 00:04:50 +0000280 elif ind.HasField('alarm_ind'):
Nicolas Palpacuer16138de2018-07-03 14:35:18 -0400281 reactor.callFromThread(self.alarm_mgr.process_alarms,
282 ind.alarm_ind)
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400283 else:
284 self.log.warn('unknown indication type')
nick47b74372018-05-25 18:22:49 -0400285
Shad Ansari2825d012018-02-22 23:57:46 +0000286 def olt_indication(self, olt_indication):
Shad Ansari22efe832018-05-19 05:37:03 +0000287 if olt_indication.oper_state == "up":
Shad Ansari94250fc2018-07-04 06:52:11 +0000288 self.go_state_up()
Shad Ansari22efe832018-05-19 05:37:03 +0000289 elif olt_indication.oper_state == "down":
Shad Ansari94250fc2018-07-04 06:52:11 +0000290 self.go_state_down()
Shad Ansari2825d012018-02-22 23:57:46 +0000291
292 def intf_indication(self, intf_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400293 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000294 oper_state=intf_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000295
296 if intf_indication.oper_state == "up":
297 oper_status = OperStatus.ACTIVE
298 else:
299 oper_status = OperStatus.DISCOVERED
300
nick47b74372018-05-25 18:22:49 -0400301 # add_port update the port if it exists
Shad Ansari2825d012018-02-22 23:57:46 +0000302 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
303
304 def intf_oper_indication(self, intf_oper_indication):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000305 self.log.debug("Received interface oper state change indication",
306 intf_id=intf_oper_indication.intf_id,
307 type=intf_oper_indication.type,
308 oper_state=intf_oper_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000309
310 if intf_oper_indication.oper_state == "up":
311 oper_state = OperStatus.ACTIVE
312 else:
313 oper_state = OperStatus.DISCOVERED
314
315 if intf_oper_indication.type == "nni":
316
Shad Ansari0346f0d2018-04-26 06:54:09 +0000317 # FIXME - creating logical port for 2nd interface throws exception!
Shad Ansari2825d012018-02-22 23:57:46 +0000318 if intf_oper_indication.intf_id != 0:
319 return
320
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400321 # add_(logical_)port update the port if it exists
Shad Ansarif9d2d102018-06-13 02:15:26 +0000322 port_no, label = self.add_port(intf_oper_indication.intf_id,
323 Port.ETHERNET_NNI, oper_state)
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400324 self.log.debug("int_oper_indication", port_no=port_no, label=label)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000325 self.add_logical_port(port_no, intf_oper_indication.intf_id,
326 oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000327
328 elif intf_oper_indication.type == "pon":
329 # FIXME - handle PON oper state change
330 pass
331
332 def onu_discovery_indication(self, onu_disc_indication):
Shad Ansari803900a2018-05-02 06:26:00 +0000333 intf_id = onu_disc_indication.intf_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000334 serial_number = onu_disc_indication.serial_number
Shad Ansari2825d012018-02-22 23:57:46 +0000335
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400336 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari803900a2018-05-02 06:26:00 +0000337
Shad Ansarif9d2d102018-06-13 02:15:26 +0000338 self.log.debug("onu discovery indication", intf_id=intf_id,
339 serial_number=serial_number_str)
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400340
Shad Ansarif9d2d102018-06-13 02:15:26 +0000341 onu_device = self.adapter_agent.get_child_device(
342 self.device_id,
343 serial_number=serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400344
345 if onu_device is None:
Shad Ansari803900a2018-05-02 06:26:00 +0000346 onu_id = self.new_onu_id(intf_id)
Shad Ansari15928d12018-04-17 02:42:13 +0000347 try:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000348 self.add_onu_device(
349 intf_id,
350 platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
351 onu_id, serial_number)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400352 self.log.info("activate-onu", intf_id=intf_id, onu_id=onu_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000353 serial_number=serial_number_str)
354 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
355 serial_number=serial_number)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400356 self.stub.ActivateOnu(onu)
357 except Exception as e:
358 self.log.exception('onu-activation-failed', e=e)
359
Shad Ansari2825d012018-02-22 23:57:46 +0000360 else:
nick47b74372018-05-25 18:22:49 -0400361 if onu_device.connect_status != ConnectStatus.REACHABLE:
362 onu_device.connect_status = ConnectStatus.REACHABLE
363 self.adapter_agent.update_device(onu_device)
364
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400365 onu_id = onu_device.proxy_address.onu_id
Shad Ansarif9d2d102018-06-13 02:15:26 +0000366 if onu_device.oper_status == OperStatus.DISCOVERED \
367 or onu_device.oper_status == OperStatus.ACTIVATING:
368 self.log.debug("ignore onu discovery indication, \
369 the onu has been discovered and should be \
370 activating shorlty", intf_id=intf_id,
371 onu_id=onu_id, state=onu_device.oper_status)
372 elif onu_device.oper_status == OperStatus.ACTIVE:
373 self.log.warn("onu discovery indication whereas onu is \
374 supposed to be active",
375 intf_id=intf_id, onu_id=onu_id,
376 state=onu_device.oper_status)
nick47b74372018-05-25 18:22:49 -0400377 elif onu_device.oper_status == OperStatus.UNKNOWN:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000378 self.log.info("onu in unknown state, recovering from olt \
379 reboot, activate onu", intf_id=intf_id,
380 onu_id=onu_id, serial_number=serial_number_str)
nick47b74372018-05-25 18:22:49 -0400381
382 onu_device.oper_status = OperStatus.DISCOVERED
383 self.adapter_agent.update_device(onu_device)
384
Shad Ansarif9d2d102018-06-13 02:15:26 +0000385 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
386 serial_number=serial_number)
nick47b74372018-05-25 18:22:49 -0400387 self.stub.ActivateOnu(onu)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400388 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000389 self.log.warn('unexpected state', onu_id=onu_id,
390 onu_device_oper_state=onu_device.oper_status)
Shad Ansari2825d012018-02-22 23:57:46 +0000391
Shad Ansari2825d012018-02-22 23:57:46 +0000392 def onu_indication(self, onu_indication):
Shad Ansaria0b37892018-06-12 21:34:30 +0000393 self.log.debug("onu indication", intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000394 onu_id=onu_indication.onu_id,
395 serial_number=onu_indication.serial_number,
396 oper_state=onu_indication.oper_state,
397 admin_state=onu_indication.admin_state)
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400398 try:
399 serial_number_str = self.stringify_serial_number(
400 onu_indication.serial_number)
Shad Ansari94250fc2018-07-04 06:52:11 +0000401 except Exception as e:
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400402 serial_number_str = None
Shad Ansari94250fc2018-07-04 06:52:11 +0000403
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400404 if serial_number_str is not None:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400405 onu_device = self.adapter_agent.get_child_device(
Shad Ansaria0b37892018-06-12 21:34:30 +0000406 self.device_id,
Nicolas Palpacuer28cc2662018-06-22 16:30:18 -0400407 serial_number=serial_number_str)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000408 else:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400409 onu_device = self.adapter_agent.get_child_device(
Shad Ansaria0b37892018-06-12 21:34:30 +0000410 self.device_id,
411 parent_port_no=platform.intf_id_to_port_no(
412 onu_indication.intf_id, Port.PON_OLT),
413 onu_id=onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400414
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400415 if onu_device is None:
Shad Ansaria0b37892018-06-12 21:34:30 +0000416 self.log.error('onu not found', intf_id=onu_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000417 onu_id=onu_indication.onu_id)
Shad Ansari803900a2018-05-02 06:26:00 +0000418 return
419
nick47b74372018-05-25 18:22:49 -0400420 if onu_device.connect_status != ConnectStatus.REACHABLE:
421 onu_device.connect_status = ConnectStatus.REACHABLE
422 self.adapter_agent.update_device(onu_device)
423
Shad Ansarif9d2d102018-06-13 02:15:26 +0000424 if platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
425 != onu_indication.intf_id:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400426 self.log.warn('ONU-is-on-a-different-intf-id-now',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000427 previous_intf_id=platform.intf_id_from_pon_port_no(
428 onu_device.parent_port_no),
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400429 current_intf_id=onu_indication.intf_id)
430 # FIXME - handle intf_id mismatch (ONU move?)
Shad Ansari2825d012018-02-22 23:57:46 +0000431
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400432 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
433 # FIXME - handle onu id mismatch
Shad Ansarif9d2d102018-06-13 02:15:26 +0000434 self.log.warn('ONU-id-mismatch',
435 expected_onu_id=onu_device.proxy_address.onu_id,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400436 received_onu_id=onu_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000437
Shad Ansarif9d2d102018-06-13 02:15:26 +0000438 uni_no = platform.mk_uni_port_num(onu_indication.intf_id,
439 onu_indication.onu_id)
440 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
441 serial_number=onu_device.serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000442
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400443 self.log.debug('port-number-ready', uni_no=uni_no, uni_name=uni_name)
Shad Ansari2825d012018-02-22 23:57:46 +0000444
Shad Ansarif9d2d102018-06-13 02:15:26 +0000445 # Admin state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400446 if onu_indication.admin_state == 'down':
447 if onu_indication.oper_state != 'down':
Shad Ansarif9d2d102018-06-13 02:15:26 +0000448 self.log.error('ONU-admin-state-down-and-oper-status-not-down',
449 oper_state=onu_indication.oper_state)
450 # Forcing the oper state change code to execute
451 onu_indication.oper_state = 'down'
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400452
453 if onu_device.admin_state != AdminState.DISABLED:
454 onu_device.admin_state = AdminState.DISABLED
455 self.adapter_agent.update(onu_device)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000456 self.log.debug('putting-onu-in-disabled-state',
457 onu_serial_number=onu_device.serial_number)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400458
Shad Ansarif9d2d102018-06-13 02:15:26 +0000459 # Port and logical port update is taken care of by oper state block
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400460
461 elif onu_indication.admin_state == 'up':
462 if onu_device.admin_state != AdminState.ENABLED:
463 onu_device.admin_state = AdminState.ENABLED
464 self.adapter_agent.update(onu_device)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000465 self.log.debug('putting-onu-in-enabled-state',
466 onu_serial_number=onu_device.serial_number)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400467
468 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000469 self.log.warn('Invalid-or-not-implemented-admin-state',
470 received_admin_state=onu_indication.admin_state)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400471
472 self.log.debug('admin-state-dealt-with')
473
Shad Ansarif9d2d102018-06-13 02:15:26 +0000474 # Operating state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400475 if onu_indication.oper_state == 'down':
Shad Ansarif9d2d102018-06-13 02:15:26 +0000476 # Move to discovered state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400477 self.log.debug('onu-oper-state-is-down')
478
479 if onu_device.oper_status != OperStatus.DISCOVERED:
480 onu_device.oper_status = OperStatus.DISCOVERED
481 self.adapter_agent.update_device(onu_device)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000482 # Set port oper state to Discovered
483 self.onu_ports_down(onu_device, uni_no, uni_name,
484 OperStatus.DISCOVERED)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400485
486 elif onu_indication.oper_state == 'up':
487
488 if onu_device.oper_status != OperStatus.DISCOVERED:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000489 self.log.debug("ignore onu indication",
490 intf_id=onu_indication.intf_id,
491 onu_id=onu_indication.onu_id,
492 state=onu_device.oper_status,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400493 msg_oper_state=onu_indication.oper_state)
494 return
495
Shad Ansarif9d2d102018-06-13 02:15:26 +0000496 # Device was in Discovered state, setting it to active
497 onu_adapter_agent = \
498 registry('adapter_loader').get_agent(onu_device.adapter)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400499 if onu_adapter_agent is None:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000500 self.log.error('onu_adapter_agent-could-not-be-retrieved',
501 onu_device=onu_device)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400502 return
503
Shad Ansarif9d2d102018-06-13 02:15:26 +0000504 # Prepare onu configuration
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400505
506 # onu initialization, base configuration (bridge setup ...)
507 def onu_initialization():
508
Shad Ansarif9d2d102018-06-13 02:15:26 +0000509 # FIXME: that's definitely cheating
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400510 if onu_device.adapter == 'broadcom_onu':
Shad Ansarif9d2d102018-06-13 02:15:26 +0000511 onu_adapter_agent.adapter.devices_handlers[onu_device.id] \
Nicolas Palpacuer4eed0a52018-06-28 14:18:31 -0400512 .message_exchange(cvid=DEFAULT_MGMT_VLAN)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400513 self.log.debug('broadcom-message-exchange-started')
514
515 # tcont creation (onu)
516 tcont = TcontsConfigData()
517 tcont.alloc_id = platform.mk_alloc_id(onu_indication.onu_id)
518
519 # gem port creation
520 gem_port = GemportsConfigData()
521 gem_port.gemport_id = platform.mk_gemport_id(onu_indication.onu_id)
522
Shad Ansarif9d2d102018-06-13 02:15:26 +0000523 # ports creation/update
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400524 def port_config():
525
526 # "v_enet" creation (olt)
527
Shad Ansarif9d2d102018-06-13 02:15:26 +0000528 # add_port update port when it exists
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400529 self.adapter_agent.add_port(
530 self.device_id,
531 Port(
532 port_no=uni_no,
533 label=uni_name,
534 type=Port.ETHERNET_UNI,
535 admin_state=AdminState.ENABLED,
536 oper_status=OperStatus.ACTIVE))
537
538 # v_enet creation (onu)
539
540 venet = VEnetConfig(name=uni_name)
541 venet.interface.name = uni_name
542 onu_adapter_agent.create_interface(onu_device, venet)
543
544 # ONU device status update in the datastore
545 def onu_update_oper_status():
546 onu_device.oper_status = OperStatus.ACTIVE
547 onu_device.connect_status = ConnectStatus.REACHABLE
548 self.adapter_agent.update_device(onu_device)
549
550 # FIXME : the asynchronicity has to be taken care of properly
551 onu_initialization()
Shad Ansarif9d2d102018-06-13 02:15:26 +0000552 reactor.callLater(10, onu_adapter_agent.create_tcont,
553 device=onu_device, tcont_data=tcont,
554 traffic_descriptor_data=None)
555 reactor.callLater(11, onu_adapter_agent.create_gemport, onu_device,
556 gem_port)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400557 reactor.callLater(12, port_config)
558 reactor.callLater(12, onu_update_oper_status)
559
560 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000561 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
562 oper_state=onu_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000563
nick47b74372018-05-25 18:22:49 -0400564 def onu_ports_down(self, onu_device, uni_no, uni_name, oper_state):
565 # Set port oper state to Discovered
566 # add port will update port if it exists
567 self.adapter_agent.add_port(
568 self.device_id,
569 Port(
570 port_no=uni_no,
571 label=uni_name,
572 type=Port.ETHERNET_UNI,
573 admin_state=onu_device.admin_state,
574 oper_status=oper_state))
575
576 # Disable logical port
nick47b74372018-05-25 18:22:49 -0400577 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
578 onu_port_id = None
579 for onu_port in onu_ports:
580 if onu_port.port_no == uni_no:
581 onu_port_id = onu_port.label
582 if onu_port_id is None:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000583 self.log.error('matching-onu-port-label-not-found',
584 onu_id=onu_device.id, olt_id=self.device_id,
nick47b74372018-05-25 18:22:49 -0400585 onu_ports=onu_ports)
586 return
587 try:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000588 onu_logical_port = self.adapter_agent.get_logical_port(
589 logical_device_id=self.logical_device_id, port_id=onu_port_id)
nick47b74372018-05-25 18:22:49 -0400590 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
Shad Ansarif9d2d102018-06-13 02:15:26 +0000591 self.adapter_agent.update_logical_port(
592 logical_device_id=self.logical_device_id,
593 port=onu_logical_port)
nick47b74372018-05-25 18:22:49 -0400594 self.log.debug('cascading-oper-state-to-port-and-logical-port')
595 except KeyError as e:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000596 self.log.error('matching-onu-port-label-invalid',
597 onu_id=onu_device.id, olt_id=self.device_id,
598 onu_ports=onu_ports, onu_port_id=onu_port_id,
599 error=e)
nick47b74372018-05-25 18:22:49 -0400600
Shad Ansari2825d012018-02-22 23:57:46 +0000601 def omci_indication(self, omci_indication):
602
603 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000604 onu_id=omci_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000605
Shad Ansarif9d2d102018-06-13 02:15:26 +0000606 onu_device = self.adapter_agent.get_child_device(
607 self.device_id, onu_id=omci_indication.onu_id)
Shad Ansari0efa6512018-04-28 06:42:54 +0000608
609 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000610 omci_indication.pkt)
Shad Ansari2825d012018-02-22 23:57:46 +0000611
Shad Ansari42db7342018-04-25 21:39:46 +0000612 def packet_indication(self, pkt_indication):
613
614 self.log.debug("packet indication", intf_id=pkt_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000615 gemport_id=pkt_indication.gemport_id,
616 flow_id=pkt_indication.flow_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000617
Shad Ansari22920932018-05-17 00:33:34 +0000618 onu_id = platform.onu_id_from_gemport_id(pkt_indication.gemport_id)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000619 logical_port_num = platform.mk_uni_port_num(pkt_indication.intf_id,
620 onu_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000621
622 pkt = Ether(pkt_indication.pkt)
Shad Ansari0efa6512018-04-28 06:42:54 +0000623 kw = dict(logical_device_id=self.logical_device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000624 logical_port_no=logical_port_num)
Shad Ansari42db7342018-04-25 21:39:46 +0000625 self.adapter_agent.send_packet_in(packet=str(pkt), **kw)
626
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400627
Shad Ansari42db7342018-04-25 21:39:46 +0000628 def packet_out(self, egress_port, msg):
Shad Ansari0346f0d2018-04-26 06:54:09 +0000629 pkt = Ether(msg)
630 self.log.info('packet out', egress_port=egress_port,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000631 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400632
633 # Find port type
634 egress_port_type = self.port_type(egress_port)
635
636 if egress_port_type == Port.ETHERNET_UNI:
637
638 if pkt.haslayer(Dot1Q):
639 outer_shim = pkt.getlayer(Dot1Q)
640 if isinstance(outer_shim.payload, Dot1Q):
641 # If double tag, remove the outer tag
642 payload = (
643 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
644 outer_shim.payload
645 )
646 else:
647 payload = pkt
Shad Ansari0346f0d2018-04-26 06:54:09 +0000648 else:
649 payload = pkt
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400650
651 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
652
Shad Ansarif9d2d102018-06-13 02:15:26 +0000653 self.log.info(
654 'sending-packet-to-ONU', egress_port=egress_port,
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400655 intf_id=platform.intf_id_from_uni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000656 onu_id=platform.onu_id_from_port_num(egress_port),
657 packet=str(payload).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400658
Shad Ansarif9d2d102018-06-13 02:15:26 +0000659 onu_pkt = openolt_pb2.OnuPacket(
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400660 intf_id=platform.intf_id_from_uni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000661 onu_id=platform.onu_id_from_port_num(egress_port),
662 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400663
664 self.stub.OnuPacketOut(onu_pkt)
665
666 elif egress_port_type == Port.ETHERNET_NNI:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000667 self.log.info('sending-packet-to-uplink', egress_port=egress_port,
668 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400669
670 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
671
Shad Ansarif9d2d102018-06-13 02:15:26 +0000672 uplink_pkt = openolt_pb2.UplinkPacket(
673 intf_id=platform.intf_id_from_nni_port_num(egress_port),
674 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400675
676 self.stub.UplinkPacketOut(uplink_pkt)
677
Shad Ansari0346f0d2018-04-26 06:54:09 +0000678 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000679 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
680 egress_port=egress_port,
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400681 port_type=egress_port_type)
Shad Ansari42db7342018-04-25 21:39:46 +0000682
Shad Ansari2825d012018-02-22 23:57:46 +0000683 def send_proxied_message(self, proxy_address, msg):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000684 omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id,
685 onu_id=proxy_address.onu_id, pkt=str(msg))
Shad Ansari2825d012018-02-22 23:57:46 +0000686 self.stub.OmciMsgOut(omci)
687
688 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Shad Ansari2825d012018-02-22 23:57:46 +0000689 self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000690 serial_number=serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000691
692 # NOTE - channel_id of onu is set to intf_id
Shad Ansari0efa6512018-04-28 06:42:54 +0000693 proxy_address = Device.ProxyAddress(device_id=self.device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000694 channel_id=intf_id, onu_id=onu_id,
695 onu_session_id=onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000696
697 self.log.info("Adding ONU", proxy_address=proxy_address)
698
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400699 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000700
Shad Ansarif9d2d102018-06-13 02:15:26 +0000701 self.adapter_agent.add_onu_device(
702 parent_device_id=self.device_id, parent_port_no=port_no,
703 vendor_id=serial_number.vendor_id, proxy_address=proxy_address,
704 root=True, serial_number=serial_number_str,
705 admin_state=AdminState.ENABLED)
Shad Ansari2825d012018-02-22 23:57:46 +0000706
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000707 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
Shad Ansari2825d012018-02-22 23:57:46 +0000708 if port_type is Port.ETHERNET_NNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400709 return "nni-" + str(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000710 elif port_type is Port.PON_OLT:
Shad Ansari4a232ca2018-05-05 05:24:17 +0000711 return "pon" + str(intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000712 elif port_type is Port.ETHERNET_UNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400713 if serial_number is not None:
714 return serial_number
715 else:
716 return "uni-{}".format(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000717
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400718 def port_type(self, port_no):
719 ports = self.adapter_agent.get_ports(self.device_id)
720 for port in ports:
721 if port.port_no == port_no:
722 return port.type
723 return None
724
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400725 def add_logical_port(self, port_no, intf_id, oper_state):
Shad Ansari2825d012018-02-22 23:57:46 +0000726 self.log.info('adding-logical-port', port_no=port_no)
727
728 label = self.port_name(port_no, Port.ETHERNET_NNI)
729
730 cap = OFPPF_1GB_FD | OFPPF_FIBER
731 curr_speed = OFPPF_1GB_FD
732 max_speed = OFPPF_1GB_FD
733
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400734 if oper_state == OperStatus.ACTIVE:
735 of_oper_state = OFPPS_LIVE
736 else:
737 of_oper_state = OFPPS_LINK_DOWN
738
Shad Ansarif9d2d102018-06-13 02:15:26 +0000739 ofp = ofp_port(
740 port_no=port_no,
741 hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % port_no),
742 name=label, config=0, state=of_oper_state, curr=cap,
743 advertised=cap, peer=cap, curr_speed=curr_speed,
744 max_speed=max_speed)
Shad Ansari2825d012018-02-22 23:57:46 +0000745
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400746 ofp_stats = ofp_port_stats(port_no=port_no)
747
Shad Ansarif9d2d102018-06-13 02:15:26 +0000748 logical_port = LogicalPort(
749 id=label, ofp_port=ofp, device_id=self.device_id,
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400750 device_port_no=port_no, root_port=True,
751 ofp_port_stats=ofp_stats)
Shad Ansari2825d012018-02-22 23:57:46 +0000752
Shad Ansarif9d2d102018-06-13 02:15:26 +0000753 self.adapter_agent.add_logical_port(self.logical_device_id,
754 logical_port)
Shad Ansari2825d012018-02-22 23:57:46 +0000755
756 def add_port(self, intf_id, port_type, oper_status):
Shad Ansari22920932018-05-17 00:33:34 +0000757 port_no = platform.intf_id_to_port_no(intf_id, port_type)
Shad Ansari2825d012018-02-22 23:57:46 +0000758
Shad Ansari4a232ca2018-05-05 05:24:17 +0000759 label = self.port_name(port_no, port_type, intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000760
Shad Ansari0efa6512018-04-28 06:42:54 +0000761 self.log.info('adding-port', port_no=port_no, label=label,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000762 port_type=port_type)
Shad Ansari0efa6512018-04-28 06:42:54 +0000763
764 port = Port(port_no=port_no, label=label, type=port_type,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000765 admin_state=AdminState.ENABLED, oper_status=oper_status)
Shad Ansari0efa6512018-04-28 06:42:54 +0000766
Shad Ansari2825d012018-02-22 23:57:46 +0000767 self.adapter_agent.add_port(self.device_id, port)
Shad Ansari0efa6512018-04-28 06:42:54 +0000768
Shad Ansari2825d012018-02-22 23:57:46 +0000769 return port_no, label
770
Shad Ansari2825d012018-02-22 23:57:46 +0000771 def new_onu_id(self, intf_id):
772 onu_id = None
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400773 onu_devices = self.adapter_agent.get_child_devices(self.device_id)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000774 for i in range(1, 512):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400775 id_not_taken = True
776 for child_device in onu_devices:
777 if child_device.proxy_address.onu_id == i:
778 id_not_taken = False
779 break
780 if id_not_taken:
Shad Ansari2825d012018-02-22 23:57:46 +0000781 onu_id = i
782 break
783 return onu_id
784
785 def stringify_vendor_specific(self, vendor_specific):
786 return ''.join(str(i) for i in [
Shad Ansarif9d2d102018-06-13 02:15:26 +0000787 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000788 hex(ord(vendor_specific[0]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000789 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000790 hex(ord(vendor_specific[1]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000791 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000792 hex(ord(vendor_specific[2]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000793 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000794 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
Shad Ansari2825d012018-02-22 23:57:46 +0000795
Shad Ansari2825d012018-02-22 23:57:46 +0000796 def update_flow_table(self, flows):
797 device = self.adapter_agent.get_device(self.device_id)
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400798 self.log.debug('update flow table', number_of_flows=len(flows))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000799 in_port = None
Shad Ansari2825d012018-02-22 23:57:46 +0000800
801 for flow in flows:
Shad Ansari2825d012018-02-22 23:57:46 +0000802 is_down_stream = None
Shad Ansari2dda4f32018-05-17 07:16:07 +0000803 in_port = fd.get_in_port(flow)
804 assert in_port is not None
Shad Ansarif9d2d102018-06-13 02:15:26 +0000805 # Right now there is only one NNI port. Get the NNI PORT and
806 # compare with IN_PUT port number. Need to find better way.
Shad Ansari2dda4f32018-05-17 07:16:07 +0000807 ports = self.adapter_agent.get_ports(device.id, Port.ETHERNET_NNI)
Shad Ansari2825d012018-02-22 23:57:46 +0000808
Shad Ansari2dda4f32018-05-17 07:16:07 +0000809 for port in ports:
810 if (port.port_no == in_port):
811 self.log.info('downstream-flow')
812 is_down_stream = True
813 break
814 if is_down_stream is None:
815 is_down_stream = False
816 self.log.info('upstream-flow')
Shad Ansari2825d012018-02-22 23:57:46 +0000817
Shad Ansari2dda4f32018-05-17 07:16:07 +0000818 for flow in flows:
819 try:
820 self.flow_mgr.add_flow(flow, is_down_stream)
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400821 except grpc.RpcError as grpc_e:
822 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
823 self.log.warn('flow already exists', e=grpc_e,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000824 flow=flow)
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400825 else:
826 self.log.error('failed to add flow', flow=flow,
827 e=grpc_e)
828 except Exception as e:
829 self.log.error('failed to add flow', flow=flow, e=e)
830
Shad Ansari89b09d52018-05-21 07:28:14 +0000831
832 # There has to be a better way to do this
833 def ip_hex(self, ip):
834 octets = ip.split(".")
835 hex_ip = []
836 for octet in octets:
837 octet_hex = hex(int(octet))
838 octet_hex = octet_hex.split('0x')[1]
839 octet_hex = octet_hex.rjust(2, '0')
840 hex_ip.append(octet_hex)
841 return ":".join(hex_ip)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400842
843 def stringify_serial_number(self, serial_number):
844 return ''.join([serial_number.vendor_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000845 self.stringify_vendor_specific(
846 serial_number.vendor_specific)])
Jonathan Davis0f917a22018-05-30 14:39:45 -0400847
848 def disable(self):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000849 self.log.info('sending-deactivate-olt-message',
850 device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400851
852 # Send grpc call
Shad Ansarif9d2d102018-06-13 02:15:26 +0000853 # self.stub.DeactivateOlt(openolt_pb2.Empty())
Jonathan Davis0f917a22018-05-30 14:39:45 -0400854
Shad Ansarif9d2d102018-06-13 02:15:26 +0000855 # Soft deactivate. Turning down hardware should remove flows,
856 # but we are not doing that presently
Jonathan Davis0f917a22018-05-30 14:39:45 -0400857
858 # Bring OLT down
Shad Ansari94250fc2018-07-04 06:52:11 +0000859 self.go_state_down()
Jonathan Davis0f917a22018-05-30 14:39:45 -0400860
861 def delete(self):
862 self.log.info('delete-olt', device_id=self.device_id)
863
864 # Stop the grpc communication threads
865 self.log.info('stopping-grpc-threads', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400866
867 # Close the grpc channel
868 # self.log.info('unsubscribing-grpc-channel', device_id=self.device_id)
869 # self.channel.unsubscribe()
870
871 self.log.info('successfully-deleted-olt', device_id=self.device_id)
872
873 def reenable(self):
874 self.log.info('reenable-olt', device_id=self.device_id)
875
876 # Bring up OLT
Shad Ansari94250fc2018-07-04 06:52:11 +0000877 self.go_state_up()
Jonathan Davis0f917a22018-05-30 14:39:45 -0400878
879 # Enable all child devices
880 self.log.info('enabling-child-devices', device_id=self.device_id)
881 self.log.info('enabling-child-devices', olt_device_id=self.device_id)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000882 self.adapter_agent.update_child_devices_state(
883 parent_device_id=self.device_id,
884 admin_state=AdminState.ENABLED)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400885
886 # Set all ports to enabled
887 self.log.info('enabling-all-ports', device_id=self.device_id)
Shad Ansaria0b37892018-06-12 21:34:30 +0000888 self.adapter_agent.enable_all_ports(self.device_id)