blob: e92d5dfd512e2a7bc77399d2ca80efa4ea32893d [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
17import structlog
18import threading
19import grpc
20import collections
nick47b74372018-05-25 18:22:49 -040021import time
Shad Ansari2825d012018-02-22 23:57:46 +000022
Shad Ansari15928d12018-04-17 02:42:13 +000023from twisted.internet import reactor
Shad Ansari0346f0d2018-04-26 06:54:09 +000024from scapy.layers.l2 import Ether, Dot1Q
25import binascii
Shad Ansari22efe832018-05-19 05:37:03 +000026from transitions import Machine
Shad Ansari15928d12018-04-17 02:42:13 +000027
Shad Ansari2825d012018-02-22 23:57:46 +000028from voltha.protos.device_pb2 import Port, Device
29from voltha.protos.common_pb2 import OperStatus, AdminState, ConnectStatus
30from voltha.protos.logical_device_pb2 import LogicalDevice
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040031from voltha.protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, OFPPS_LINK_DOWN, \
Shad Ansari2825d012018-02-22 23:57:46 +000032 OFPPF_1GB_FD, OFPC_GROUP_STATS, OFPC_PORT_STATS, OFPC_TABLE_STATS, \
Shad Ansari2dda4f32018-05-17 07:16:07 +000033 OFPC_FLOW_STATS, ofp_switch_features, ofp_port
Shad Ansari2825d012018-02-22 23:57:46 +000034from voltha.protos.logical_device_pb2 import LogicalPort, LogicalDevice
35from voltha.core.logical_device_agent import mac_str_to_tuple
36from voltha.registry import registry
37from voltha.adapters.openolt.protos import openolt_pb2_grpc, openolt_pb2
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040038from voltha.protos.bbf_fiber_tcont_body_pb2 import TcontsConfigData
39from voltha.protos.bbf_fiber_gemport_body_pb2 import GemportsConfigData
40from voltha.protos.bbf_fiber_base_pb2 import VEnetConfig
Shad Ansari2825d012018-02-22 23:57:46 +000041import voltha.core.flow_decomposer as fd
42
Shad Ansari22920932018-05-17 00:33:34 +000043import openolt_platform as platform
Shad Ansari2dda4f32018-05-17 07:16:07 +000044from openolt_flow_mgr import OpenOltFlowMgr
Shad Ansari801f7372018-04-27 02:15:41 +000045
nick47b74372018-05-25 18:22:49 -040046MAX_HEARTBEAT_MISS = 3
47HEARTBEAT_PERIOD = 1
48GRPC_TIMEOUT = 5
Shad Ansari2825d012018-02-22 23:57:46 +000049
50"""
51OpenoltDevice represents an OLT.
52"""
53class OpenoltDevice(object):
54
Shad Ansari22efe832018-05-19 05:37:03 +000055 states = ['up', 'down']
56 transitions = [
57 { 'trigger': 'olt_up', 'source': 'down', 'dest': 'up', 'before': 'olt_indication_up' },
58 { 'trigger': 'olt_down', 'source': 'up', 'dest': 'down', 'before': 'olt_indication_down' }
59 ]
60
Shad Ansari2825d012018-02-22 23:57:46 +000061 def __init__(self, **kwargs):
62 super(OpenoltDevice, self).__init__()
63
64 self.adapter_agent = kwargs['adapter_agent']
Shad Ansari5dbc9c82018-05-10 03:29:31 +000065 self.device_num = kwargs['device_num']
Shad Ansari2825d012018-02-22 23:57:46 +000066 device = kwargs['device']
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040067 is_reconciliation = kwargs.get('reconciliation', False)
Shad Ansari2825d012018-02-22 23:57:46 +000068 self.device_id = device.id
69 self.host_and_port = device.host_and_port
70 self.log = structlog.get_logger(id=self.device_id, ip=self.host_and_port)
Shad Ansari2825d012018-02-22 23:57:46 +000071 self.nni_oper_state = dict() #intf_id -> oper_state
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -040072 self.proxy = registry('core').get_proxy('/')
Shad Ansari2825d012018-02-22 23:57:46 +000073
Nicolas Palpacuer253461f2018-06-01 12:01:45 -040074 # Device already set in the event of reconciliation
75 if not is_reconciliation:
76 # It is a new device
77 # Update device
78 device.root = True
79 device.serial_number = self.host_and_port # FIXME
80 device.connect_status = ConnectStatus.REACHABLE
81 device.oper_status = OperStatus.ACTIVATING
82 self.adapter_agent.update_device(device)
Shad Ansari2825d012018-02-22 23:57:46 +000083
Shad Ansari22efe832018-05-19 05:37:03 +000084 # Initialize the OLT state machine
85 self.machine = Machine(model=self, states=OpenoltDevice.states,
86 transitions=OpenoltDevice.transitions,
87 send_event=True, initial='down', ignore_invalid_triggers=True)
88 self.machine.add_transition(trigger='olt_ind_up', source='down', dest='up')
89 self.machine.add_transition(trigger='olt_ind_loss', source='up', dest='down')
90
Shad Ansari2825d012018-02-22 23:57:46 +000091 # Initialize gRPC
92 self.channel = grpc.insecure_channel(self.host_and_port)
Shad Ansari8f1b2532018-04-21 07:51:39 +000093 self.channel_ready_future = grpc.channel_ready_future(self.channel)
nick47b74372018-05-25 18:22:49 -040094 self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
95
96 self.flow_mgr = OpenOltFlowMgr(self.log, self.stub)
Shad Ansari2825d012018-02-22 23:57:46 +000097
nick369a5062018-05-29 17:11:06 -040098 # Indications thread plcaholder (started by heartbeat thread)
99 self.indications_thread = None
100 self.indications_thread_active = False
Shad Ansari2825d012018-02-22 23:57:46 +0000101
nick47b74372018-05-25 18:22:49 -0400102 # Start heartbeat thread
103 self.heartbeat_thread = threading.Thread(target=self.heartbeat)
104 self.heartbeat_thread.setDaemon(True)
105 self.heartbeat_thread_active = True
106 self.heartbeat_miss = 0
107 self.heartbeat_signature = None
108 self.heartbeat_thread.start()
109
Nicolas Palpacuer253461f2018-06-01 12:01:45 -0400110 self.log.debug('openolt-device-created', device_id=self.device_id)
111
112
Shad Ansari8f1b2532018-04-21 07:51:39 +0000113 def process_indications(self):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000114
nick47b74372018-05-25 18:22:49 -0400115 self.log.debug('starting-indications-thread')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000116
Shad Ansari15928d12018-04-17 02:42:13 +0000117 self.indications = self.stub.EnableIndication(openolt_pb2.Empty())
Shad Ansari2dda4f32018-05-17 07:16:07 +0000118
nick47b74372018-05-25 18:22:49 -0400119 while self.indications_thread_active:
120 try:
121 # get the next indication from olt
122 ind = next(self.indications)
123 except Exception as e:
124 self.log.warn('GRPC-connection-lost-stoping-indications-thread', error=e)
125 self.indications_thread_active = False
126 else:
127 self.log.debug("rx indication", indication=ind)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000128
nick47b74372018-05-25 18:22:49 -0400129 # indication handlers run in the main event loop
130 if ind.HasField('olt_ind'):
131 reactor.callFromThread(self.olt_indication, ind.olt_ind)
132 elif ind.HasField('intf_ind'):
133 reactor.callFromThread(self.intf_indication, ind.intf_ind)
134 elif ind.HasField('intf_oper_ind'):
135 reactor.callFromThread(self.intf_oper_indication, ind.intf_oper_ind)
136 elif ind.HasField('onu_disc_ind'):
137 reactor.callFromThread(self.onu_discovery_indication, ind.onu_disc_ind)
138 elif ind.HasField('onu_ind'):
139 reactor.callFromThread(self.onu_indication, ind.onu_ind)
140 elif ind.HasField('omci_ind'):
141 reactor.callFromThread(self.omci_indication, ind.omci_ind)
142 elif ind.HasField('pkt_ind'):
143 reactor.callFromThread(self.packet_indication, ind.pkt_ind)
144
145 self.log.debug('stopping-indications-thread', device_id=self.device_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000146
147 def olt_indication(self, olt_indication):
Shad Ansari22efe832018-05-19 05:37:03 +0000148 if olt_indication.oper_state == "up":
149 self.olt_up(ind=olt_indication)
150 elif olt_indication.oper_state == "down":
151 self.olt_down(ind=olt_indication)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000152
Shad Ansari22efe832018-05-19 05:37:03 +0000153 def olt_indication_up(self, event):
154 olt_indication = event.kwargs.get('ind', None)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400155 self.log.debug("olt indication", olt_ind=olt_indication)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000156
nick47b74372018-05-25 18:22:49 -0400157 device = self.adapter_agent.get_device(self.device_id)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000158
nick47b74372018-05-25 18:22:49 -0400159 # If logical device does not exist create it
160 if len(device.parent_id) == 0:
161
162 dpid = '00:00:' + self.ip_hex(self.host_and_port.split(":")[0])
163
164 # Create logical OF device
165 ld = LogicalDevice(
166 root_device_id=self.device_id,
167 switch_features=ofp_switch_features(
168 n_buffers=256, # TODO fake for now
169 n_tables=2, # TODO ditto
170 capabilities=( # TODO and ditto
171 OFPC_FLOW_STATS
172 | OFPC_TABLE_STATS
173 | OFPC_PORT_STATS
174 | OFPC_GROUP_STATS
175 )
Jonathan Hart7687e0a2018-05-16 10:54:47 -0700176 )
177 )
nick47b74372018-05-25 18:22:49 -0400178 ld_initialized = self.adapter_agent.create_logical_device(ld, dpid=dpid)
179 self.logical_device_id = ld_initialized.id
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000180
181 # Update phys OF device
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000182 device.parent_id = self.logical_device_id
183 device.oper_status = OperStatus.ACTIVE
184 self.adapter_agent.update_device(device)
185
Shad Ansari22efe832018-05-19 05:37:03 +0000186 def olt_indication_down(self, event):
187 olt_indication = event.kwargs.get('ind', None)
nick47b74372018-05-25 18:22:49 -0400188 new_admin_state = event.kwargs.get('admin_state', None)
189 new_oper_state = event.kwargs.get('oper_state', None)
190 new_connect_state = event.kwargs.get('connect_state', None)
191 self.log.debug("olt indication", olt_ind=olt_indication, admin_state=new_admin_state, oper_state=new_oper_state,
192 connect_state=new_connect_state)
193
nick369a5062018-05-29 17:11:06 -0400194 # Propagating to the children
195
196 # Children ports
197 child_devices = self.adapter_agent.get_child_devices(self.device_id)
198 for onu_device in child_devices:
199 uni_no = platform.mk_uni_port_num(onu_device.proxy_address.channel_id, onu_device.proxy_address.onu_id)
200 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI, serial_number=onu_device.serial_number)
201
202 self.onu_ports_down(onu_device, uni_no, uni_name, new_oper_state)
203 # Children devices
204 self.adapter_agent.update_child_devices_state(self.device_id, oper_status=new_oper_state,
205 connect_status=ConnectStatus.UNREACHABLE,
206 admin_state=new_admin_state)
207 # Device Ports
208 device_ports = self.adapter_agent.get_ports(self.device_id, Port.ETHERNET_NNI)
209 logical_ports_ids = [port.label for port in device_ports]
210 device_ports += self.adapter_agent.get_ports(self.device_id, Port.PON_OLT)
211
212 for port in device_ports:
213 if new_admin_state is not None:
214 port.admin_state = new_admin_state
215 if new_oper_state is not None:
216 port.oper_status = new_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(self.logical_device_id, logical_port_id)
222 logical_port.ofp_port.state = OFPPS_LINK_DOWN
223 self.adapter_agent.update_logical_port(self.logical_device_id, logical_port)
224
225 # Device
nick47b74372018-05-25 18:22:49 -0400226 device = self.adapter_agent.get_device(self.device_id)
nick369a5062018-05-29 17:11:06 -0400227 if new_admin_state is not None:
nick47b74372018-05-25 18:22:49 -0400228 device.admin_state = new_admin_state
229 if new_oper_state is not None:
230 device.oper_status = new_oper_state
231 if new_connect_state is not None:
232 device.connect_status = new_connect_state
233
234 self.adapter_agent.update_device(device)
Shad Ansari2825d012018-02-22 23:57:46 +0000235
236 def intf_indication(self, intf_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400237 self.log.debug("intf indication", intf_id=intf_indication.intf_id,
Shad Ansari2825d012018-02-22 23:57:46 +0000238 oper_state=intf_indication.oper_state)
239
240 if intf_indication.oper_state == "up":
241 oper_status = OperStatus.ACTIVE
242 else:
243 oper_status = OperStatus.DISCOVERED
244
nick47b74372018-05-25 18:22:49 -0400245 # add_port update the port if it exists
Shad Ansari2825d012018-02-22 23:57:46 +0000246 self.add_port(intf_indication.intf_id, Port.PON_OLT, oper_status)
247
248 def intf_oper_indication(self, intf_oper_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400249 self.log.debug("Received interface oper state change indication", intf_id=intf_oper_indication.intf_id,
250 type=intf_oper_indication.type, oper_state=intf_oper_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000251
252 if intf_oper_indication.oper_state == "up":
253 oper_state = OperStatus.ACTIVE
254 else:
255 oper_state = OperStatus.DISCOVERED
256
257 if intf_oper_indication.type == "nni":
258
Shad Ansari0346f0d2018-04-26 06:54:09 +0000259 # FIXME - creating logical port for 2nd interface throws exception!
Shad Ansari2825d012018-02-22 23:57:46 +0000260 if intf_oper_indication.intf_id != 0:
261 return
262
263 if intf_oper_indication.intf_id not in self.nni_oper_state:
264 self.nni_oper_state[intf_oper_indication.intf_id] = oper_state
265 port_no, label = self.add_port(intf_oper_indication.intf_id, Port.ETHERNET_NNI, oper_state)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400266 self.log.debug("int_oper_indication", port_no=port_no, label=label)
Shad Ansari4a232ca2018-05-05 05:24:17 +0000267 self.add_logical_port(port_no, intf_oper_indication.intf_id) # FIXME - add oper_state
Shad Ansari2825d012018-02-22 23:57:46 +0000268 elif intf_oper_indication.intf_id != self.nni_oper_state:
269 # FIXME - handle subsequent NNI oper state change
270 pass
271
272 elif intf_oper_indication.type == "pon":
273 # FIXME - handle PON oper state change
274 pass
275
276 def onu_discovery_indication(self, onu_disc_indication):
Shad Ansari803900a2018-05-02 06:26:00 +0000277 intf_id = onu_disc_indication.intf_id
278 serial_number=onu_disc_indication.serial_number
Shad Ansari2825d012018-02-22 23:57:46 +0000279
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400280 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari803900a2018-05-02 06:26:00 +0000281
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400282 self.log.debug("onu discovery indication", intf_id=intf_id, serial_number=serial_number_str)
283
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400284 onu_device = self.adapter_agent.get_child_device(self.device_id, serial_number=serial_number_str)
285
286 if onu_device is None:
Shad Ansari803900a2018-05-02 06:26:00 +0000287 onu_id = self.new_onu_id(intf_id)
Shad Ansari15928d12018-04-17 02:42:13 +0000288 try:
Shad Ansari803900a2018-05-02 06:26:00 +0000289 self.add_onu_device(intf_id,
Shad Ansari22920932018-05-17 00:33:34 +0000290 platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
Shad Ansari803900a2018-05-02 06:26:00 +0000291 onu_id, serial_number)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400292 self.log.info("activate-onu", intf_id=intf_id, onu_id=onu_id,
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400293 serial_number=serial_number_str)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400294 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,serial_number=serial_number)
295 self.stub.ActivateOnu(onu)
296 except Exception as e:
297 self.log.exception('onu-activation-failed', e=e)
298
Shad Ansari2825d012018-02-22 23:57:46 +0000299 else:
nick47b74372018-05-25 18:22:49 -0400300 if onu_device.connect_status != ConnectStatus.REACHABLE:
301 onu_device.connect_status = ConnectStatus.REACHABLE
302 self.adapter_agent.update_device(onu_device)
303
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400304 onu_id = onu_device.proxy_address.onu_id
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400305 if onu_device.oper_status == OperStatus.DISCOVERED or onu_device.oper_status == OperStatus.ACTIVATING:
306 self.log.debug("ignore onu discovery indication, the onu has been discovered and should be \
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400307 activating shorlty", intf_id=intf_id, onu_id=onu_id, state=onu_device.oper_status)
308 elif onu_device.oper_status== OperStatus.ACTIVE:
309 self.log.warn("onu discovery indication whereas onu is supposed to be active",
310 intf_id=intf_id, onu_id=onu_id, state=onu_device.oper_status)
nick47b74372018-05-25 18:22:49 -0400311 elif onu_device.oper_status == OperStatus.UNKNOWN:
312 self.log.info("onu-in-unknow-state-recovering-form-olt-reboot-activate-onu", intf_id=intf_id, onu_id=onu_id,
313 serial_number=serial_number_str)
314
315 onu_device.oper_status = OperStatus.DISCOVERED
316 self.adapter_agent.update_device(onu_device)
317
318 onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id, serial_number=serial_number)
319 self.stub.ActivateOnu(onu)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400320 else:
321 self.log.warn('unexpected state', onu_id=onu_id, onu_device_oper_state=onu_device.oper_status)
Shad Ansari2825d012018-02-22 23:57:46 +0000322
Shad Ansari2825d012018-02-22 23:57:46 +0000323 def onu_indication(self, onu_indication):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400324 self.log.debug("onu-indication", intf_id=onu_indication.intf_id,
325 onu_id=onu_indication.onu_id, serial_number=onu_indication.serial_number,
326 oper_state=onu_indication.oper_state, admin_state=onu_indication.admin_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000327
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400328 serial_number_str = self.stringify_serial_number(onu_indication.serial_number)
329
330 if serial_number_str == '000000000000':
331 self.log.debug('serial-number-was-not-provided-or-default-serial-number-provided-identifying-onu-by-onu_id')
332 #FIXME: if multiple PON ports onu_id is not a sufficient key
333 onu_device = self.adapter_agent.get_child_device(
334 self.device_id,
335 onu_id=onu_indication.onu_id)
336 else :
337 onu_device = self.adapter_agent.get_child_device(
338 self.device_id,
339 serial_number=serial_number_str)
340
341 self.log.debug('onu-device', olt_device_id=self.device_id, device=onu_device)
Shad Ansari803900a2018-05-02 06:26:00 +0000342
343 # FIXME - handle serial_number mismatch
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400344 # assert key is not None
345 # assert onu_device is not None
346 if onu_device is None:
347 self.log.warn('onu-device-is-none-invalid-message')
Shad Ansari803900a2018-05-02 06:26:00 +0000348 return
349
nick47b74372018-05-25 18:22:49 -0400350 if onu_device.connect_status != ConnectStatus.REACHABLE:
351 onu_device.connect_status = ConnectStatus.REACHABLE
352 self.adapter_agent.update_device(onu_device)
353
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400354 if platform.intf_id_from_pon_port_no(onu_device.parent_port_no) != onu_indication.intf_id:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400355 self.log.warn('ONU-is-on-a-different-intf-id-now',
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400356 previous_intf_id=platform.intf_id_from_pon_port_no(onu_device.parent_port_no),
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400357 current_intf_id=onu_indication.intf_id)
358 # FIXME - handle intf_id mismatch (ONU move?)
Shad Ansari2825d012018-02-22 23:57:46 +0000359
Shad Ansari2825d012018-02-22 23:57:46 +0000360
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400361 if onu_device.proxy_address.onu_id != onu_indication.onu_id:
362 # FIXME - handle onu id mismatch
363 self.log.warn('ONU-id-mismatch', expected_onu_id=onu_device.proxy_address.onu_id,
364 received_onu_id=onu_indication.onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000365
Shad Ansari22920932018-05-17 00:33:34 +0000366 uni_no = platform.mk_uni_port_num(onu_indication.intf_id, onu_indication.onu_id)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400367 uni_name = self.port_name(uni_no, Port.ETHERNET_UNI, serial_number=serial_number_str)
Shad Ansari2825d012018-02-22 23:57:46 +0000368
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400369 self.log.debug('port-number-ready', uni_no=uni_no, uni_name=uni_name)
Shad Ansari2825d012018-02-22 23:57:46 +0000370
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400371 #Admin state
372 if onu_indication.admin_state == 'down':
373 if onu_indication.oper_state != 'down':
374 self.log.error('ONU-admin-state-down-and-oper-status-not-down', oper_state=onu_indication.oper_state)
375 onu_indication.oper_state = 'down' # Forcing the oper state change code to execute
376
377 if onu_device.admin_state != AdminState.DISABLED:
378 onu_device.admin_state = AdminState.DISABLED
379 self.adapter_agent.update(onu_device)
380 self.log.debug('putting-onu-in-disabled-state', onu_serial_number=onu_device.serial_number)
381
382 #Port and logical port update is taken care of by oper state block
383
384 elif onu_indication.admin_state == 'up':
385 if onu_device.admin_state != AdminState.ENABLED:
386 onu_device.admin_state = AdminState.ENABLED
387 self.adapter_agent.update(onu_device)
388 self.log.debug('putting-onu-in-enabled-state', onu_serial_number=onu_device.serial_number)
389
390 else:
391 self.log.warn('Invalid-or-not-implemented-admin-state', received_admin_state=onu_indication.admin_state)
392
393 self.log.debug('admin-state-dealt-with')
394
395 #Operating state
396 if onu_indication.oper_state == 'down':
397 #Move to discovered state
398 self.log.debug('onu-oper-state-is-down')
399
400 if onu_device.oper_status != OperStatus.DISCOVERED:
401 onu_device.oper_status = OperStatus.DISCOVERED
402 self.adapter_agent.update_device(onu_device)
403 #Set port oper state to Discovered
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400404
nick47b74372018-05-25 18:22:49 -0400405 self.onu_ports_down(onu_device, uni_no, uni_name, OperStatus.DISCOVERED)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400406
407 elif onu_indication.oper_state == 'up':
408
409 if onu_device.oper_status != OperStatus.DISCOVERED:
410 self.log.debug("ignore onu indication", intf_id=onu_indication.intf_id,
nick47b74372018-05-25 18:22:49 -0400411 onu_id=onu_indication.onu_id, state=onu_device.oper_status,
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400412 msg_oper_state=onu_indication.oper_state)
413 return
414
415 #Device was in Discovered state, setting it to active
416
417
418
419 onu_adapter_agent = registry('adapter_loader').get_agent(onu_device.adapter)
420 if onu_adapter_agent is None:
421 self.log.error('onu_adapter_agent-could-not-be-retrieved', onu_device=onu_device)
422 return
423
424 #Prepare onu configuration
425
426 # onu initialization, base configuration (bridge setup ...)
427 def onu_initialization():
428
429 #FIXME: that's definitely cheating
430 if onu_device.adapter == 'broadcom_onu':
431 onu_adapter_agent.adapter.devices_handlers[onu_device.id].message_exchange()
432 self.log.debug('broadcom-message-exchange-started')
433
434 # tcont creation (onu)
435 tcont = TcontsConfigData()
436 tcont.alloc_id = platform.mk_alloc_id(onu_indication.onu_id)
437
438 # gem port creation
439 gem_port = GemportsConfigData()
440 gem_port.gemport_id = platform.mk_gemport_id(onu_indication.onu_id)
441
442 #ports creation/update
443 def port_config():
444
445 # "v_enet" creation (olt)
446
447 #add_port update port when it exists
448 self.adapter_agent.add_port(
449 self.device_id,
450 Port(
451 port_no=uni_no,
452 label=uni_name,
453 type=Port.ETHERNET_UNI,
454 admin_state=AdminState.ENABLED,
455 oper_status=OperStatus.ACTIVE))
456
457 # v_enet creation (onu)
458
459 venet = VEnetConfig(name=uni_name)
460 venet.interface.name = uni_name
461 onu_adapter_agent.create_interface(onu_device, venet)
462
463 # ONU device status update in the datastore
464 def onu_update_oper_status():
465 onu_device.oper_status = OperStatus.ACTIVE
466 onu_device.connect_status = ConnectStatus.REACHABLE
467 self.adapter_agent.update_device(onu_device)
468
469 # FIXME : the asynchronicity has to be taken care of properly
470 onu_initialization()
471 reactor.callLater(10, onu_adapter_agent.create_tcont, device=onu_device,
472 tcont_data=tcont, traffic_descriptor_data=None)
473 reactor.callLater(11, onu_adapter_agent.create_gemport, onu_device, gem_port)
474 reactor.callLater(12, port_config)
475 reactor.callLater(12, onu_update_oper_status)
476
477 else:
478 self.log.warn('Not-implemented-or-invalid-value-of-oper-state', oper_state=onu_indication.oper_state)
Shad Ansari2825d012018-02-22 23:57:46 +0000479
nick47b74372018-05-25 18:22:49 -0400480 def onu_ports_down(self, onu_device, uni_no, uni_name, oper_state):
481 # Set port oper state to Discovered
482 # add port will update port if it exists
483 self.adapter_agent.add_port(
484 self.device_id,
485 Port(
486 port_no=uni_no,
487 label=uni_name,
488 type=Port.ETHERNET_UNI,
489 admin_state=onu_device.admin_state,
490 oper_status=oper_state))
491
492 # Disable logical port
nick47b74372018-05-25 18:22:49 -0400493 onu_ports = self.proxy.get('devices/{}/ports'.format(onu_device.id))
494 onu_port_id = None
495 for onu_port in onu_ports:
496 if onu_port.port_no == uni_no:
497 onu_port_id = onu_port.label
498 if onu_port_id is None:
499 self.log.error('matching-onu-port-label-not-found', onu_id=onu_device.id, olt_id=self.device_id,
500 onu_ports=onu_ports)
501 return
502 try:
nick369a5062018-05-29 17:11:06 -0400503 onu_logical_port = self.adapter_agent.get_logical_port(logical_device_id=self.logical_device_id,
nick47b74372018-05-25 18:22:49 -0400504 port_id=onu_port_id)
505 onu_logical_port.ofp_port.state = OFPPS_LINK_DOWN
nick369a5062018-05-29 17:11:06 -0400506 self.adapter_agent.update_logical_port(logical_device_id=self.logical_device_id, port=onu_logical_port)
nick47b74372018-05-25 18:22:49 -0400507 self.log.debug('cascading-oper-state-to-port-and-logical-port')
508 except KeyError as e:
509 self.log.error('matching-onu-port-label-invalid', onu_id=onu_device.id, olt_id=self.device_id,
510 onu_ports=onu_ports, onu_port_id=onu_port_id, error=e)
511
Shad Ansari2825d012018-02-22 23:57:46 +0000512 def omci_indication(self, omci_indication):
513
514 self.log.debug("omci indication", intf_id=omci_indication.intf_id,
515 onu_id=omci_indication.onu_id)
516
Shad Ansari0efa6512018-04-28 06:42:54 +0000517 onu_device = self.adapter_agent.get_child_device(self.device_id,
518 onu_id=omci_indication.onu_id)
519
520 self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
521 omci_indication.pkt)
Shad Ansari2825d012018-02-22 23:57:46 +0000522
Shad Ansari42db7342018-04-25 21:39:46 +0000523 def packet_indication(self, pkt_indication):
524
525 self.log.debug("packet indication", intf_id=pkt_indication.intf_id,
526 gemport_id=pkt_indication.gemport_id,
527 flow_id=pkt_indication.flow_id)
528
Shad Ansari22920932018-05-17 00:33:34 +0000529 onu_id = platform.onu_id_from_gemport_id(pkt_indication.gemport_id)
530 logical_port_num = platform.mk_uni_port_num(pkt_indication.intf_id, onu_id)
Shad Ansari42db7342018-04-25 21:39:46 +0000531
532 pkt = Ether(pkt_indication.pkt)
Shad Ansari0efa6512018-04-28 06:42:54 +0000533 kw = dict(logical_device_id=self.logical_device_id,
534 logical_port_no=logical_port_num)
Shad Ansari42db7342018-04-25 21:39:46 +0000535 self.adapter_agent.send_packet_in(packet=str(pkt), **kw)
536
nick47b74372018-05-25 18:22:49 -0400537 def olt_reachable(self):
538 device = self.adapter_agent.get_device(self.device_id)
539 device.connect_status = ConnectStatus.REACHABLE
540 self.adapter_agent.update_device(device)
541 # Not changing its child devices state, we cannot guaranty that
542
543 def heartbeat(self):
544
nick369a5062018-05-29 17:11:06 -0400545 self.channel_ready_future.result() # blocks till gRPC connection is complete
546
nick47b74372018-05-25 18:22:49 -0400547 while self.heartbeat_thread_active:
548
549 try:
550 heartbeat = self.stub.HeartbeatCheck(openolt_pb2.Empty(), timeout=GRPC_TIMEOUT)
551 except Exception as e:
552 self.heartbeat_miss += 1
553 self.log.warn('heartbeat-miss', missed_heartbeat=self.heartbeat_miss, error=e)
554 if self.heartbeat_miss == MAX_HEARTBEAT_MISS:
555 self.log.error('lost-connectivity-to-olt')
556 #TODO : send alarm/notify monitoring system
557 # Using reactor to synchronize update
558 # flagging it as unreachable and in unknow state
nick369a5062018-05-29 17:11:06 -0400559 reactor.callFromThread(self.olt_down, oper_state=OperStatus.UNKNOWN,
nick47b74372018-05-25 18:22:49 -0400560 connect_state=ConnectStatus.UNREACHABLE)
561
562 else:
563 # heartbeat received
564 if self.heartbeat_signature is None:
565 # Initialize heartbeat signature
566 self.heartbeat_signature = heartbeat.heartbeat_signature
567 self.log.debug('heartbeat-signature', device_id=self.device_id,
568 heartbeat_signature=self.heartbeat_signature)
569 # Check if signature is different
570 if self.heartbeat_signature != heartbeat.heartbeat_signature:
571 # OLT has rebooted
572 self.log.warn('OLT-was-rebooted', device_id=self.device_id)
573 #TODO: notify monitoring system
574 self.heartbeat_signature = heartbeat.heartbeat_signature
575
576 else:
577 self.log.debug('valid-heartbeat-received')
578
579 if self.heartbeat_miss > MAX_HEARTBEAT_MISS:
580 self.log.info('OLT-connection-restored')
581 #TODO : suppress alarm/notify monitoring system
582 # flagging it as reachable again
nick369a5062018-05-29 17:11:06 -0400583 reactor.callFromThread(self.olt_reachable)
nick47b74372018-05-25 18:22:49 -0400584
585 if not self.indications_thread_active:
nick369a5062018-05-29 17:11:06 -0400586 self.log.info('(re)starting-indications-thread')
nick47b74372018-05-25 18:22:49 -0400587 # reset indications thread
588 self.indications_thread = threading.Thread(target=self.process_indications)
589 self.indications_thread.setDaemon(True)
590 self.indications_thread_active = True
591 self.indications_thread.start()
592
593 self.heartbeat_miss = 0
594
595 time.sleep(HEARTBEAT_PERIOD)
596
597 self.log.debug('stopping-heartbeat-thread', device_id=self.device_id)
598
599
600
Shad Ansari42db7342018-04-25 21:39:46 +0000601 def packet_out(self, egress_port, msg):
Shad Ansari0346f0d2018-04-26 06:54:09 +0000602 pkt = Ether(msg)
603 self.log.info('packet out', egress_port=egress_port,
604 packet=str(pkt).encode("HEX"))
605
606 if pkt.haslayer(Dot1Q):
607 outer_shim = pkt.getlayer(Dot1Q)
608 if isinstance(outer_shim.payload, Dot1Q):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400609 #If double tag, remove the outer tag
Shad Ansari0346f0d2018-04-26 06:54:09 +0000610 payload = (
611 Ether(src=pkt.src, dst=pkt.dst, type=outer_shim.type) /
612 outer_shim.payload
613 )
614 else:
615 payload = pkt
616 else:
617 payload = pkt
618
619 self.log.info('sending-packet-to-device', egress_port=egress_port,
620 packet=str(payload).encode("HEX"))
621
622 send_pkt = binascii.unhexlify(str(payload).encode("HEX"))
623
Nicolas Palpacuer36a93442018-05-23 17:38:57 -0400624 onu_pkt = openolt_pb2.OnuPacket(intf_id=platform.intf_id_from_pon_port_no(egress_port),
Shad Ansari22920932018-05-17 00:33:34 +0000625 onu_id=platform.onu_id_from_port_num(egress_port), pkt=send_pkt)
Shad Ansari0346f0d2018-04-26 06:54:09 +0000626
Jonathan Harta58dc382018-05-14 16:29:19 -0700627 self.stub.OnuPacketOut(onu_pkt)
Shad Ansari42db7342018-04-25 21:39:46 +0000628
Shad Ansari2825d012018-02-22 23:57:46 +0000629 def send_proxied_message(self, proxy_address, msg):
Shad Ansari0efa6512018-04-28 06:42:54 +0000630 omci = openolt_pb2.OmciMsg(intf_id=proxy_address.channel_id, # intf_id
631 onu_id=proxy_address.onu_id, pkt=str(msg))
Shad Ansari2825d012018-02-22 23:57:46 +0000632 self.stub.OmciMsgOut(omci)
633
634 def add_onu_device(self, intf_id, port_no, onu_id, serial_number):
Shad Ansari2825d012018-02-22 23:57:46 +0000635 self.log.info("Adding ONU", port_no=port_no, onu_id=onu_id,
Shad Ansari0efa6512018-04-28 06:42:54 +0000636 serial_number=serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000637
638 # NOTE - channel_id of onu is set to intf_id
Shad Ansari0efa6512018-04-28 06:42:54 +0000639 proxy_address = Device.ProxyAddress(device_id=self.device_id,
640 channel_id=intf_id, onu_id=onu_id, onu_session_id=onu_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000641
642 self.log.info("Adding ONU", proxy_address=proxy_address)
643
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400644 serial_number_str = self.stringify_serial_number(serial_number)
Shad Ansari2825d012018-02-22 23:57:46 +0000645
Shad Ansari0efa6512018-04-28 06:42:54 +0000646 self.adapter_agent.add_onu_device(parent_device_id=self.device_id,
647 parent_port_no=port_no, vendor_id=serial_number.vendor_id,
648 proxy_address=proxy_address, root=True,
649 serial_number=serial_number_str, admin_state=AdminState.ENABLED)
Shad Ansari2825d012018-02-22 23:57:46 +0000650
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000651 def port_name(self, port_no, port_type, intf_id=None, serial_number=None):
Shad Ansari2825d012018-02-22 23:57:46 +0000652 if port_type is Port.ETHERNET_NNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400653 return "nni-" + str(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000654 elif port_type is Port.PON_OLT:
Shad Ansari4a232ca2018-05-05 05:24:17 +0000655 return "pon" + str(intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000656 elif port_type is Port.ETHERNET_UNI:
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400657 if serial_number is not None:
658 return serial_number
659 else:
660 return "uni-{}".format(port_no)
Shad Ansari2825d012018-02-22 23:57:46 +0000661
Shad Ansari4a232ca2018-05-05 05:24:17 +0000662 def add_logical_port(self, port_no, intf_id):
Shad Ansari2825d012018-02-22 23:57:46 +0000663 self.log.info('adding-logical-port', port_no=port_no)
664
665 label = self.port_name(port_no, Port.ETHERNET_NNI)
666
667 cap = OFPPF_1GB_FD | OFPPF_FIBER
668 curr_speed = OFPPF_1GB_FD
669 max_speed = OFPPF_1GB_FD
670
Shad Ansari0efa6512018-04-28 06:42:54 +0000671 ofp = ofp_port(port_no=port_no,
672 hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % port_no),
673 name=label, config=0, state=OFPPS_LIVE, curr=cap,
674 advertised=cap, peer=cap, curr_speed=curr_speed,
675 max_speed=max_speed)
Shad Ansari2825d012018-02-22 23:57:46 +0000676
Shad Ansari0efa6512018-04-28 06:42:54 +0000677 logical_port = LogicalPort(id=label, ofp_port=ofp,
678 device_id=self.device_id, device_port_no=port_no,
679 root_port=True)
Shad Ansari2825d012018-02-22 23:57:46 +0000680
681 self.adapter_agent.add_logical_port(self.logical_device_id, logical_port)
682
683 def add_port(self, intf_id, port_type, oper_status):
Shad Ansari22920932018-05-17 00:33:34 +0000684 port_no = platform.intf_id_to_port_no(intf_id, port_type)
Shad Ansari2825d012018-02-22 23:57:46 +0000685
Shad Ansari4a232ca2018-05-05 05:24:17 +0000686 label = self.port_name(port_no, port_type, intf_id)
Shad Ansari2825d012018-02-22 23:57:46 +0000687
Shad Ansari0efa6512018-04-28 06:42:54 +0000688 self.log.info('adding-port', port_no=port_no, label=label,
689 port_type=port_type)
690
691 port = Port(port_no=port_no, label=label, type=port_type,
692 admin_state=AdminState.ENABLED, oper_status=oper_status)
693
Shad Ansari2825d012018-02-22 23:57:46 +0000694 self.adapter_agent.add_port(self.device_id, port)
Shad Ansari0efa6512018-04-28 06:42:54 +0000695
Shad Ansari2825d012018-02-22 23:57:46 +0000696 return port_no, label
697
Shad Ansari2825d012018-02-22 23:57:46 +0000698 def new_onu_id(self, intf_id):
699 onu_id = None
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400700 onu_devices = self.adapter_agent.get_child_devices(self.device_id)
Shad Ansari5dbc9c82018-05-10 03:29:31 +0000701 for i in range(1, 512):
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400702 id_not_taken = True
703 for child_device in onu_devices:
704 if child_device.proxy_address.onu_id == i:
705 id_not_taken = False
706 break
707 if id_not_taken:
Shad Ansari2825d012018-02-22 23:57:46 +0000708 onu_id = i
709 break
710 return onu_id
711
712 def stringify_vendor_specific(self, vendor_specific):
713 return ''.join(str(i) for i in [
Shad Ansari1fd9eb22018-05-15 05:13:49 +0000714 hex(ord(vendor_specific[0])>>4 & 0x0f)[2:],
715 hex(ord(vendor_specific[0]) & 0x0f)[2:],
716 hex(ord(vendor_specific[1])>>4 & 0x0f)[2:],
717 hex(ord(vendor_specific[1]) & 0x0f)[2:],
718 hex(ord(vendor_specific[2])>>4 & 0x0f)[2:],
719 hex(ord(vendor_specific[2]) & 0x0f)[2:],
720 hex(ord(vendor_specific[3])>>4 & 0x0f)[2:],
721 hex(ord(vendor_specific[3]) & 0x0f)[2:]])
Shad Ansari2825d012018-02-22 23:57:46 +0000722
Shad Ansari2825d012018-02-22 23:57:46 +0000723
724 def update_flow_table(self, flows):
725 device = self.adapter_agent.get_device(self.device_id)
Shad Ansaricd2e8ff2018-05-11 20:26:22 +0000726 self.log.debug('update flow table')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000727 in_port = None
Shad Ansari2825d012018-02-22 23:57:46 +0000728
729 for flow in flows:
Shad Ansari2825d012018-02-22 23:57:46 +0000730 is_down_stream = None
Shad Ansari2dda4f32018-05-17 07:16:07 +0000731 in_port = fd.get_in_port(flow)
732 assert in_port is not None
733 # Right now there is only one NNI port. Get the NNI PORT and compare
734 # with IN_PUT port number. Need to find better way.
735 ports = self.adapter_agent.get_ports(device.id, Port.ETHERNET_NNI)
Shad Ansari2825d012018-02-22 23:57:46 +0000736
Shad Ansari2dda4f32018-05-17 07:16:07 +0000737 for port in ports:
738 if (port.port_no == in_port):
739 self.log.info('downstream-flow')
740 is_down_stream = True
741 break
742 if is_down_stream is None:
743 is_down_stream = False
744 self.log.info('upstream-flow')
Shad Ansari2825d012018-02-22 23:57:46 +0000745
Shad Ansari2dda4f32018-05-17 07:16:07 +0000746 for flow in flows:
747 try:
748 self.flow_mgr.add_flow(flow, is_down_stream)
749 except Exception as e:
750 self.log.exception('failed-to-install-flow', e=e, flow=flow)
Shad Ansari89b09d52018-05-21 07:28:14 +0000751
752 # There has to be a better way to do this
753 def ip_hex(self, ip):
754 octets = ip.split(".")
755 hex_ip = []
756 for octet in octets:
757 octet_hex = hex(int(octet))
758 octet_hex = octet_hex.split('0x')[1]
759 octet_hex = octet_hex.rjust(2, '0')
760 hex_ip.append(octet_hex)
761 return ":".join(hex_ip)
Nicolas Palpacuer65de6a42018-05-22 17:28:29 -0400762
763 def stringify_serial_number(self, serial_number):
764 return ''.join([serial_number.vendor_id,
Shad Ansari5c596a52018-05-23 19:49:57 +0000765 self.stringify_vendor_specific(serial_number.vendor_specific)])
Jonathan Davis0f917a22018-05-30 14:39:45 -0400766
767 def disable(self):
768 self.log.info('sending-deactivate-olt-message', device_id=self.device_id)
769
770 # Send grpc call
771 #self.stub.DeactivateOlt(openolt_pb2.Empty())
772
773 # Soft deactivate. Turning down hardware should remove flows, but we are not doing that presently
774
775 # Bring OLT down
776 self.olt_down(oper_state=OperStatus.UNKNOWN, admin_state=AdminState.DISABLED,
777 connect_state = ConnectStatus.UNREACHABLE)
778
779
780 def delete(self):
781 self.log.info('delete-olt', device_id=self.device_id)
782
783 # Stop the grpc communication threads
784 self.log.info('stopping-grpc-threads', device_id=self.device_id)
785 self.indications_thread_active = False
786 self.heartbeat_thread_active = False
787
788 # Close the grpc channel
789 # self.log.info('unsubscribing-grpc-channel', device_id=self.device_id)
790 # self.channel.unsubscribe()
791
792 self.log.info('successfully-deleted-olt', device_id=self.device_id)
793
794 def reenable(self):
795 self.log.info('reenable-olt', device_id=self.device_id)
796
797 # Bring up OLT
798 self.olt_up()
799
800 # Enable all child devices
801 self.log.info('enabling-child-devices', device_id=self.device_id)
802 self.log.info('enabling-child-devices', olt_device_id=self.device_id)
803 self.adapter_agent.update_child_devices_state(parent_device_id=self.device_id,
804 admin_state=AdminState.ENABLED)
805
806 # Set all ports to enabled
807 self.log.info('enabling-all-ports', device_id=self.device_id)
808 self.adapter_agent.enable_all_ports(self.device_id)