blob: 2eeb7a1283f9a56ed92f2a82fbc78b157bad860a [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
Matt Jeannerete6a70332018-07-20 16:11:25 -0400506 ## If we are using the old/current broadcom adapter otherwise use the openomci adapter
507 if onu_device.adapter == 'broadcom_onu':
508 self.log.debug('using-broadcom_onu')
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400509
Matt Jeannerete6a70332018-07-20 16:11:25 -0400510 # onu initialization, base configuration (bridge setup ...)
511 def onu_initialization():
512
Shad Ansarif9d2d102018-06-13 02:15:26 +0000513 onu_adapter_agent.adapter.devices_handlers[onu_device.id] \
Matt Jeannerete6a70332018-07-20 16:11:25 -0400514 .message_exchange(cvid=DEFAULT_MGMT_VLAN)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400515 self.log.debug('broadcom-message-exchange-started')
516
Matt Jeannerete6a70332018-07-20 16:11:25 -0400517 # tcont creation (onu)
518 tcont = TcontsConfigData()
519 tcont.alloc_id = platform.mk_alloc_id(onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400520
Matt Jeannerete6a70332018-07-20 16:11:25 -0400521 # gem port creation
522 gem_port = GemportsConfigData()
523 gem_port.gemport_id = platform.mk_gemport_id(onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400524
Matt Jeannerete6a70332018-07-20 16:11:25 -0400525 # ports creation/update
526 def port_config():
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400527
Matt Jeannerete6a70332018-07-20 16:11:25 -0400528 # "v_enet" creation (olt)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400529
Matt Jeannerete6a70332018-07-20 16:11:25 -0400530 # add_port update port when it exists
531 self.adapter_agent.add_port(
532 self.device_id,
533 Port(
534 port_no=uni_no,
535 label=uni_name,
536 type=Port.ETHERNET_UNI,
537 admin_state=AdminState.ENABLED,
538 oper_status=OperStatus.ACTIVE))
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400539
Matt Jeannerete6a70332018-07-20 16:11:25 -0400540 # v_enet creation (onu)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400541
Matt Jeannerete6a70332018-07-20 16:11:25 -0400542 venet = VEnetConfig(name=uni_name)
543 venet.interface.name = uni_name
544 onu_adapter_agent.create_interface(onu_device, venet)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400545
Matt Jeannerete6a70332018-07-20 16:11:25 -0400546 # ONU device status update in the datastore
547 def onu_update_oper_status():
548 onu_device.oper_status = OperStatus.ACTIVE
549 onu_device.connect_status = ConnectStatus.REACHABLE
550 self.adapter_agent.update_device(onu_device)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400551
Matt Jeannerete6a70332018-07-20 16:11:25 -0400552 # FIXME : the asynchronicity has to be taken care of properly
553 onu_initialization()
554 reactor.callLater(10, onu_adapter_agent.create_tcont,
555 device=onu_device, tcont_data=tcont,
556 traffic_descriptor_data=None)
557 reactor.callLater(11, onu_adapter_agent.create_gemport, onu_device,
558 gem_port)
559 reactor.callLater(12, port_config)
560 reactor.callLater(12, onu_update_oper_status)
561
562 elif onu_device.adapter == 'brcm_openomci_onu':
563 self.log.debug('using-brcm_openomci_onu')
564
565 # tcont creation (onu)
566 tcont = TcontsConfigData()
567 tcont.alloc_id = platform.mk_alloc_id(onu_indication.onu_id)
568
569 # gem port creation
570 gem_port = GemportsConfigData()
571 gem_port.gemport_id = platform.mk_gemport_id(onu_indication.onu_id)
572 gem_port.tcont_ref = str(tcont.alloc_id)
573
574 self.log.info('inject-tcont-gem-data-onu-handler', onu_indication=onu_indication,
575 tcont=tcont, gem_port=gem_port)
576
577 onu_adapter_agent.create_interface(onu_device, onu_indication)
578 onu_adapter_agent.create_tcont(onu_device, tcont, traffic_descriptor_data=None)
579 onu_adapter_agent.create_gemport(onu_device, gem_port)
580
581 else:
582 self.log.warn('unsupported-openolt-onu-adapter')
583
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400584
585 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000586 self.log.warn('Not-implemented-or-invalid-value-of-oper-state',
587 oper_state=onu_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000588
nick47b74372018-05-25 18:22:49 -0400589 def onu_ports_down(self, onu_device, uni_no, uni_name, oper_state):
590 # Set port oper state to Discovered
591 # add port will update port if it exists
592 self.adapter_agent.add_port(
593 self.device_id,
594 Port(
595 port_no=uni_no,
596 label=uni_name,
597 type=Port.ETHERNET_UNI,
598 admin_state=onu_device.admin_state,
599 oper_status=oper_state))
600
601 # Disable logical port
nick47b74372018-05-25 18:22:49 -0400602 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
603 onu_port_id = None
604 for onu_port in onu_ports:
605 if onu_port.port_no == uni_no:
606 onu_port_id = onu_port.label
607 if onu_port_id is None:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000608 self.log.error('matching-onu-port-label-not-found',
609 onu_id=onu_device.id, olt_id=self.device_id,
nick47b74372018-05-25 18:22:49 -0400610 onu_ports=onu_ports)
611 return
612 try:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000613 onu_logical_port = self.adapter_agent.get_logical_port(
614 logical_device_id=self.logical_device_id, port_id=onu_port_id)
nick47b74372018-05-25 18:22:49 -0400615 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
Shad Ansarif9d2d102018-06-13 02:15:26 +0000616 self.adapter_agent.update_logical_port(
617 logical_device_id=self.logical_device_id,
618 port=onu_logical_port)
nick47b74372018-05-25 18:22:49 -0400619 self.log.debug('cascading-oper-state-to-port-and-logical-port')
620 except KeyError as e:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000621 self.log.error('matching-onu-port-label-invalid',
622 onu_id=onu_device.id, olt_id=self.device_id,
623 onu_ports=onu_ports, onu_port_id=onu_port_id,
624 error=e)
nick47b74372018-05-25 18:22:49 -0400625
Shad Ansari2825d012018-02-22 23:57:46 +0000626 def omci_indication(self, omci_indication):
627
628 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000629 onu_id=omci_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000630
Shad Ansarif9d2d102018-06-13 02:15:26 +0000631 onu_device = self.adapter_agent.get_child_device(
632 self.device_id, onu_id=omci_indication.onu_id)
Shad Ansari0efa6512018-04-28 06:42:54 +0000633
634 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000635 omci_indication.pkt)
Shad Ansari2825d012018-02-22 23:57:46 +0000636
Shad Ansari42db7342018-04-25 21:39:46 +0000637 def packet_indication(self, pkt_indication):
638
639 self.log.debug("packet indication", intf_id=pkt_indication.intf_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000640 gemport_id=pkt_indication.gemport_id,
641 flow_id=pkt_indication.flow_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000642
Shad Ansari22920932018-05-17 00:33:34 +0000643 onu_id = platform.onu_id_from_gemport_id(pkt_indication.gemport_id)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000644 logical_port_num = platform.mk_uni_port_num(pkt_indication.intf_id,
645 onu_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000646
647 pkt = Ether(pkt_indication.pkt)
Shad Ansari0efa6512018-04-28 06:42:54 +0000648 kw = dict(logical_device_id=self.logical_device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000649 logical_port_no=logical_port_num)
Shad Ansari42db7342018-04-25 21:39:46 +0000650 self.adapter_agent.send_packet_in(packet=str(pkt), **kw)
651
Nicolas Palpacuer33f1a822018-06-13 12:36:36 -0400652
Shad Ansari42db7342018-04-25 21:39:46 +0000653 def packet_out(self, egress_port, msg):
Shad Ansari0346f0d2018-04-26 06:54:09 +0000654 pkt = Ether(msg)
655 self.log.info('packet out', egress_port=egress_port,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000656 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400657
658 # Find port type
659 egress_port_type = self.port_type(egress_port)
660
661 if egress_port_type == Port.ETHERNET_UNI:
662
663 if pkt.haslayer(Dot1Q):
664 outer_shim = pkt.getlayer(Dot1Q)
665 if isinstance(outer_shim.payload, Dot1Q):
666 # If double tag, remove the outer tag
667 payload = (
668 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
669 outer_shim.payload
670 )
671 else:
672 payload = pkt
Shad Ansari0346f0d2018-04-26 06:54:09 +0000673 else:
674 payload = pkt
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400675
676 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
677
Shad Ansarif9d2d102018-06-13 02:15:26 +0000678 self.log.info(
679 'sending-packet-to-ONU', egress_port=egress_port,
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400680 intf_id=platform.intf_id_from_uni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000681 onu_id=platform.onu_id_from_port_num(egress_port),
682 packet=str(payload).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400683
Shad Ansarif9d2d102018-06-13 02:15:26 +0000684 onu_pkt = openolt_pb2.OnuPacket(
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400685 intf_id=platform.intf_id_from_uni_port_num(egress_port),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000686 onu_id=platform.onu_id_from_port_num(egress_port),
687 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400688
689 self.stub.OnuPacketOut(onu_pkt)
690
691 elif egress_port_type == Port.ETHERNET_NNI:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000692 self.log.info('sending-packet-to-uplink', egress_port=egress_port,
693 packet=str(pkt).encode("HEX"))
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400694
695 send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
696
Shad Ansarif9d2d102018-06-13 02:15:26 +0000697 uplink_pkt = openolt_pb2.UplinkPacket(
698 intf_id=platform.intf_id_from_nni_port_num(egress_port),
699 pkt=send_pkt)
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400700
701 self.stub.UplinkPacketOut(uplink_pkt)
702
Shad Ansari0346f0d2018-04-26 06:54:09 +0000703 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000704 self.log.warn('Packet-out-to-this-interface-type-not-implemented',
705 egress_port=egress_port,
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400706 port_type=egress_port_type)
Shad Ansari42db7342018-04-25 21:39:46 +0000707
Shad Ansari2825d012018-02-22 23:57:46 +0000708 def send_proxied_message(self, proxy_address, msg):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000709 omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id,
710 onu_id=proxy_address.onu_id, pkt=str(msg))
Shad Ansari2825d012018-02-22 23:57:46 +0000711 self.stub.OmciMsgOut(omci)
712
713 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Shad Ansari2825d012018-02-22 23:57:46 +0000714 self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000715 serial_number=serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000716
717 # NOTE - channel_id of onu is set to intf_id
Shad Ansari0efa6512018-04-28 06:42:54 +0000718 proxy_address = Device.ProxyAddress(device_id=self.device_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000719 channel_id=intf_id, onu_id=onu_id,
720 onu_session_id=onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000721
722 self.log.info("Adding ONU", proxy_address=proxy_address)
723
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400724 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000725
Shad Ansarif9d2d102018-06-13 02:15:26 +0000726 self.adapter_agent.add_onu_device(
727 parent_device_id=self.device_id, parent_port_no=port_no,
728 vendor_id=serial_number.vendor_id, proxy_address=proxy_address,
729 root=True, serial_number=serial_number_str,
730 admin_state=AdminState.ENABLED)
Shad Ansari2825d012018-02-22 23:57:46 +0000731
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000732 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
Shad Ansari2825d012018-02-22 23:57:46 +0000733 if port_type is Port.ETHERNET_NNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400734 return "nni-" + str(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000735 elif port_type is Port.PON_OLT:
Shad Ansari4a232ca2018-05-05 05:24:17 +0000736 return "pon" + str(intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000737 elif port_type is Port.ETHERNET_UNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400738 if serial_number is not None:
739 return serial_number
740 else:
741 return "uni-{}".format(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000742
Nicolas Palpacuer7d902812018-06-07 16:17:09 -0400743 def port_type(self, port_no):
744 ports = self.adapter_agent.get_ports(self.device_id)
745 for port in ports:
746 if port.port_no == port_no:
747 return port.type
748 return None
749
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400750 def add_logical_port(self, port_no, intf_id, oper_state):
Shad Ansari2825d012018-02-22 23:57:46 +0000751 self.log.info('adding-logical-port', port_no=port_no)
752
753 label = self.port_name(port_no, Port.ETHERNET_NNI)
754
755 cap = OFPPF_1GB_FD | OFPPF_FIBER
756 curr_speed = OFPPF_1GB_FD
757 max_speed = OFPPF_1GB_FD
758
Nicolas Palpacuercf735ac2018-06-06 11:12:53 -0400759 if oper_state == OperStatus.ACTIVE:
760 of_oper_state = OFPPS_LIVE
761 else:
762 of_oper_state = OFPPS_LINK_DOWN
763
Shad Ansarif9d2d102018-06-13 02:15:26 +0000764 ofp = ofp_port(
765 port_no=port_no,
766 hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % port_no),
767 name=label, config=0, state=of_oper_state, curr=cap,
768 advertised=cap, peer=cap, curr_speed=curr_speed,
769 max_speed=max_speed)
Shad Ansari2825d012018-02-22 23:57:46 +0000770
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400771 ofp_stats = ofp_port_stats(port_no=port_no)
772
Shad Ansarif9d2d102018-06-13 02:15:26 +0000773 logical_port = LogicalPort(
774 id=label, ofp_port=ofp, device_id=self.device_id,
Nicolas Palpacuere7359fc2018-06-15 14:10:48 -0400775 device_port_no=port_no, root_port=True,
776 ofp_port_stats=ofp_stats)
Shad Ansari2825d012018-02-22 23:57:46 +0000777
Shad Ansarif9d2d102018-06-13 02:15:26 +0000778 self.adapter_agent.add_logical_port(self.logical_device_id,
779 logical_port)
Shad Ansari2825d012018-02-22 23:57:46 +0000780
781 def add_port(self, intf_id, port_type, oper_status):
Shad Ansari22920932018-05-17 00:33:34 +0000782 port_no = platform.intf_id_to_port_no(intf_id, port_type)
Shad Ansari2825d012018-02-22 23:57:46 +0000783
Shad Ansari4a232ca2018-05-05 05:24:17 +0000784 label = self.port_name(port_no, port_type, intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000785
Shad Ansari0efa6512018-04-28 06:42:54 +0000786 self.log.info('adding-port', port_no=port_no, label=label,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000787 port_type=port_type)
Shad Ansari0efa6512018-04-28 06:42:54 +0000788
789 port = Port(port_no=port_no, label=label, type=port_type,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000790 admin_state=AdminState.ENABLED, oper_status=oper_status)
Shad Ansari0efa6512018-04-28 06:42:54 +0000791
Shad Ansari2825d012018-02-22 23:57:46 +0000792 self.adapter_agent.add_port(self.device_id, port)
Shad Ansari0efa6512018-04-28 06:42:54 +0000793
Shad Ansari2825d012018-02-22 23:57:46 +0000794 return port_no, label
795
Shad Ansari2825d012018-02-22 23:57:46 +0000796 def new_onu_id(self, intf_id):
797 onu_id = None
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400798 onu_devices = self.adapter_agent.get_child_devices(self.device_id)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000799 for i in range(1, 512):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400800 id_not_taken = True
801 for child_device in onu_devices:
802 if child_device.proxy_address.onu_id == i:
803 id_not_taken = False
804 break
805 if id_not_taken:
Shad Ansari2825d012018-02-22 23:57:46 +0000806 onu_id = i
807 break
808 return onu_id
809
810 def stringify_vendor_specific(self, vendor_specific):
811 return ''.join(str(i) for i in [
Shad Ansarif9d2d102018-06-13 02:15:26 +0000812 hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000813 hex(ord(vendor_specific[0]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000814 hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000815 hex(ord(vendor_specific[1]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000816 hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000817 hex(ord(vendor_specific[2]) & 0x0f)[2:],
Shad Ansarif9d2d102018-06-13 02:15:26 +0000818 hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000819 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
Shad Ansari2825d012018-02-22 23:57:46 +0000820
Shad Ansari2825d012018-02-22 23:57:46 +0000821 def update_flow_table(self, flows):
822 device = self.adapter_agent.get_device(self.device_id)
Nicolas Palpacuer2eca1052018-06-26 15:26:15 -0400823 self.log.debug('update flow table', number_of_flows=len(flows))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000824 in_port = None
Shad Ansari2825d012018-02-22 23:57:46 +0000825
826 for flow in flows:
Shad Ansari2825d012018-02-22 23:57:46 +0000827 is_down_stream = None
Shad Ansari2dda4f32018-05-17 07:16:07 +0000828 in_port = fd.get_in_port(flow)
829 assert in_port is not None
Shad Ansarif9d2d102018-06-13 02:15:26 +0000830 # Right now there is only one NNI port. Get the NNI PORT and
831 # compare with IN_PUT port number. Need to find better way.
Shad Ansari2dda4f32018-05-17 07:16:07 +0000832 ports = self.adapter_agent.get_ports(device.id, Port.ETHERNET_NNI)
Shad Ansari2825d012018-02-22 23:57:46 +0000833
Shad Ansari2dda4f32018-05-17 07:16:07 +0000834 for port in ports:
835 if (port.port_no == in_port):
836 self.log.info('downstream-flow')
837 is_down_stream = True
838 break
839 if is_down_stream is None:
840 is_down_stream = False
841 self.log.info('upstream-flow')
Shad Ansari2825d012018-02-22 23:57:46 +0000842
Shad Ansari2dda4f32018-05-17 07:16:07 +0000843 for flow in flows:
844 try:
845 self.flow_mgr.add_flow(flow, is_down_stream)
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400846 except grpc.RpcError as grpc_e:
847 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
848 self.log.warn('flow already exists', e=grpc_e,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000849 flow=flow)
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400850 else:
851 self.log.error('failed to add flow', flow=flow,
852 e=grpc_e)
853 except Exception as e:
854 self.log.error('failed to add flow', flow=flow, e=e)
855
Shad Ansari89b09d52018-05-21 07:28:14 +0000856
857 # There has to be a better way to do this
858 def ip_hex(self, ip):
859 octets = ip.split(".")
860 hex_ip = []
861 for octet in octets:
862 octet_hex = hex(int(octet))
863 octet_hex = octet_hex.split('0x')[1]
864 octet_hex = octet_hex.rjust(2, '0')
865 hex_ip.append(octet_hex)
866 return ":".join(hex_ip)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400867
868 def stringify_serial_number(self, serial_number):
869 return ''.join([serial_number.vendor_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000870 self.stringify_vendor_specific(
871 serial_number.vendor_specific)])
Jonathan Davis0f917a22018-05-30 14:39:45 -0400872
873 def disable(self):
Shad Ansarif9d2d102018-06-13 02:15:26 +0000874 self.log.info('sending-deactivate-olt-message',
875 device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400876
877 # Send grpc call
Shad Ansarif9d2d102018-06-13 02:15:26 +0000878 # self.stub.DeactivateOlt(openolt_pb2.Empty())
Jonathan Davis0f917a22018-05-30 14:39:45 -0400879
Shad Ansarif9d2d102018-06-13 02:15:26 +0000880 # Soft deactivate. Turning down hardware should remove flows,
881 # but we are not doing that presently
Jonathan Davis0f917a22018-05-30 14:39:45 -0400882
883 # Bring OLT down
Shad Ansari94250fc2018-07-04 06:52:11 +0000884 self.go_state_down()
Jonathan Davis0f917a22018-05-30 14:39:45 -0400885
886 def delete(self):
887 self.log.info('delete-olt', device_id=self.device_id)
888
889 # Stop the grpc communication threads
890 self.log.info('stopping-grpc-threads', device_id=self.device_id)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400891
892 # Close the grpc channel
893 # self.log.info('unsubscribing-grpc-channel', device_id=self.device_id)
894 # self.channel.unsubscribe()
895
896 self.log.info('successfully-deleted-olt', device_id=self.device_id)
897
898 def reenable(self):
899 self.log.info('reenable-olt', device_id=self.device_id)
900
901 # Bring up OLT
Shad Ansari94250fc2018-07-04 06:52:11 +0000902 self.go_state_up()
Jonathan Davis0f917a22018-05-30 14:39:45 -0400903
904 # Enable all child devices
905 self.log.info('enabling-child-devices', device_id=self.device_id)
906 self.log.info('enabling-child-devices', olt_device_id=self.device_id)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000907 self.adapter_agent.update_child_devices_state(
908 parent_device_id=self.device_id,
909 admin_state=AdminState.ENABLED)
Jonathan Davis0f917a22018-05-30 14:39:45 -0400910
911 # Set all ports to enabled
912 self.log.info('enabling-all-ports', device_id=self.device_id)
Shad Ansaria0b37892018-06-12 21:34:30 +0000913 self.adapter_agent.enable_all_ports(self.device_id)
Jonathan Davisb45bb372018-07-19 15:05:15 -0400914
915 def disable_child_device(self, child_device):
916 self.log.debug('sending-disable-onu',
917 olt_device_id=self.device_id,
918 onu_device=child_device,
919 onu_serial_number=child_device.serial_number)
920 vendor_id = child_device.vendor_id.encode('hex')
921 vendor_specific = child_device.serial_number.replace(child_device.vendor_id,'').encode('hex')
922 serial_number = openolt_pb2.SerialNumber(vendor_id=vendor_id, vendor_specific=vendor_specific)
923 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
924 onu_id=child_device.proxy_address.onu_id,
925 serial_number=serial_number)
926 self.stub.DeactivateOnu(onu)
927
928 def delete_child_device(self, child_device):
929 self.log.debug('sending-deactivate-onu',
930 olt_device_id=self.device_id,
931 onu_device=child_device,
932 onu_serial_number=child_device.serial_number)
933 vendor_id = child_device.vendor_id.encode('hex')
934 vendor_specific = child_device.serial_number.replace(child_device.vendor_id,'').encode('hex')
935 serial_number = openolt_pb2.SerialNumber(vendor_id=vendor_id, vendor_specific=vendor_specific)
936 onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
937 onu_id=child_device.proxy_address.onu_id,
938 serial_number=serial_number)
939 self.stub.DeleteOnu(onu)