blob: f644ec87eb289866473a9c72975cc8d36fc7d3df [file] [log] [blame]
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -05001#
2# Copyright 2018 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Matt Jeanneret2e3cb8d2019-11-16 09:22:41 -050016from __future__ import absolute_import
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050017import structlog
18from twisted.internet.defer import inlineCallbacks, returnValue
William Kurkian8235c1e2019-03-05 12:58:28 -050019from voltha_protos.common_pb2 import AdminState, OperStatus
20from voltha_protos.device_pb2 import Port
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050021from pyvoltha.adapters.extensions.omci.tasks.task import Task
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050022
23BRDCM_DEFAULT_VLAN = 4091
24TASK_PRIORITY = Task.DEFAULT_PRIORITY + 10
25DEFAULT_TPID = 0x8100
26DEFAULT_GEM_PAYLOAD = 48
27
28
29class PonPort(object):
30 """Wraps northbound-port/ANI support for ONU"""
31 # TODO: possibly get from olt
32 MIN_GEM_ENTITY_ID = 0x408
33 MAX_GEM_ENTITY_ID = 0x4FF # TODO: This limits is internal to specific ONU. It should be more "discoverable"?
34
35 def __init__(self, handler, port_no):
36 self.log = structlog.get_logger(device_id=handler.device_id, port_no=port_no)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050037
38 self._enabled = False
39 self._valid = True
40 self._handler = handler
41 self._deferred = None
42 self._port = None
43 self._port_number = port_no
Matt Jeanneret0c287892019-02-28 11:48:00 -050044 self._peers = []
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050045 self._next_entity_id = PonPort.MIN_GEM_ENTITY_ID
46
47 self._admin_state = AdminState.ENABLED
48 self._oper_status = OperStatus.ACTIVE
49
Girish Gowdrae933cd32019-11-21 21:04:41 +053050 self._gem_ports = {} # gem-id -> GemPort
51 self._tconts = {} # alloc-id -> TCont
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050052
53 self.ieee_mapper_service_profile_entity_id = 0x8001
54 self.mac_bridge_port_ani_entity_id = 0x2102 # TODO: can we just use the entity id from the anis list?
55
56 def __str__(self):
57 return "PonPort - port_number: {}, next_entity_id: {}, num_gem_ports: {}, num_tconts: {}".format(
58 self._port_number, self._next_entity_id, len(self._gem_ports), len(self._tconts))
59
60 def __repr__(self):
61 return str(self)
62
63 @staticmethod
64 def create(handler, port_no):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050065 port = PonPort(handler, port_no)
66
67 return port
68
69 def _start(self):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050070 self._cancel_deferred()
71
72 self._admin_state = AdminState.ENABLED
73 self._oper_status = OperStatus.ACTIVE
74 self._update_adapter_agent()
75
76 def _stop(self):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050077 self._cancel_deferred()
78
79 self._admin_state = AdminState.DISABLED
80 self._oper_status = OperStatus.UNKNOWN
81 self._update_adapter_agent()
82
83 # TODO: stop h/w sync
84
85 def _cancel_deferred(self):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050086 d1, self._deferred = self._deferred, None
87
88 for d in [d1]:
89 try:
90 if d is not None and not d.called:
91 d.cancel()
92 except:
93 pass
94
95 def delete(self):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050096 self.enabled = False
97 self._valid = False
98 self._handler = None
99
100 @property
101 def enabled(self):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500102 return self._enabled
103
104 @enabled.setter
105 def enabled(self, value):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500106 if self._enabled != value:
107 self._enabled = value
108
109 if value:
110 self._start()
111 else:
112 self._stop()
113
114 @property
115 def port_number(self):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500116 return self._port_number
117
118 @property
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500119 def tconts(self):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500120 return self._tconts
121
122 @property
123 def gem_ports(self):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500124 return self._gem_ports
125
126 def get_port(self):
127 """
128 Get the VOLTHA PORT object for this port
129 :return: VOLTHA Port object
130 """
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500131
Matt Jeanneret0c287892019-02-28 11:48:00 -0500132 self._port = Port(port_no=self.port_number,
133 label='PON port',
134 type=Port.PON_ONU,
135 admin_state=self._admin_state,
136 oper_status=self._oper_status,
137 peers=self._peers)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500138 return self._port
139
Matt Jeanneret0c287892019-02-28 11:48:00 -0500140 def add_peer(self, parent_device_id, parent_port_no):
141 self.log.debug('add-peer-port', parent_device_id=parent_device_id, parent_port_no=parent_port_no)
142 new_peer = Port.PeerPort(device_id=parent_device_id, port_no=parent_port_no)
143 self._peers.extend([new_peer])
144
Matt Jeanneret84e56f62019-02-26 10:48:09 -0500145 @inlineCallbacks
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500146 def _update_adapter_agent(self):
147 """
148 Update the port status and state in the core
149 """
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500150 self.log.debug('update-adapter-agent', admin_state=self._admin_state,
151 oper_status=self._oper_status)
152
153 if self._port is not None:
154 self._port.admin_state = self._admin_state
155 self._port.oper_status = self._oper_status
156
157 # adapter_agent add_port also does an update of port status
158 try:
Matt Jeannereta32441c2019-03-07 05:16:37 -0500159 yield self._handler.core_proxy.port_state_update(self._handler.device_id, self._port.type,
Girish Gowdrae933cd32019-11-21 21:04:41 +0530160 self._port.port_no, self._port.oper_status)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500161 except Exception as e:
162 self.log.exception('update-port', e=e)
163
164 def add_tcont(self, tcont, reflow=False):
165 """
166 Creates/ a T-CONT with the given alloc-id
167
168 :param tcont: (TCont) Object that maintains the TCONT properties
169 :param reflow: (boolean) If true, force add (used during h/w resync)
170 :return: (deferred)
171 """
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500172
173 if not self._valid:
Girish Gowdrae933cd32019-11-21 21:04:41 +0530174 return # Deleting
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500175
176 if not reflow and tcont.alloc_id in self._tconts:
Girish Gowdrae933cd32019-11-21 21:04:41 +0530177 return # already created
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500178
179 self.log.info('add-tcont', tcont=tcont.alloc_id, reflow=reflow)
180 self._tconts[tcont.alloc_id] = tcont
181
182 def update_tcont_td(self, alloc_id, new_td):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500183
184 tcont = self._tconts.get(alloc_id)
185
186 if tcont is None:
187 return # not-found
188
189 tcont.traffic_descriptor = new_td
190
191 # TODO: Not yet implemented
Girish Gowdrae933cd32019-11-21 21:04:41 +0530192 # TODO: How does this affect ONU tcont settings?
193 # try:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500194 # results = yield tcont.add_to_hardware(self._handler.omci)
Girish Gowdrae933cd32019-11-21 21:04:41 +0530195 # except Exception as e:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500196 # self.log.exception('tcont', tcont=tcont, e=e)
197 # # May occur with xPON provisioning, use hw-resync to recover
198 # results = 'resync needed'
199 # returnValue(results)
200
201 @inlineCallbacks
Girish Gowdrae933cd32019-11-21 21:04:41 +0530202 def remove_tcont(self, alloc_id, remove_from_hw=True):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500203
204 tcont = self._tconts.get(alloc_id)
205
206 if tcont is None:
207 returnValue('nop')
208
209 try:
210 del self._tconts[alloc_id]
Girish Gowdrae933cd32019-11-21 21:04:41 +0530211 if remove_from_hw:
212 results = yield tcont.remove_from_hardware(self._handler.openomci.omci_cc)
213 returnValue(results)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500214
215 except Exception as e:
216 self.log.exception('delete', e=e)
217 raise
218
219 def gem_port(self, gem_id, direction):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500220 return self._gem_ports.get((gem_id, direction))
221
222 @property
223 def gem_ids(self):
224 """Get all GEM Port IDs used by this ONU"""
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500225 return sorted([gem_id_and_direction[0] for gem_id_and_direction, gem in self._gem_ports.items()])
226
227 def add_gem_port(self, gem_port, reflow=False):
228 """
229 Add a GEM Port to this ONU
230
231 :param gem_port: (GemPort) GEM Port to add
232 :param reflow: (boolean) If true, force add (used during h/w resync)
233 :return: (deferred)
234 """
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500235
236 if not self._valid:
237 return # Deleting
238
239 if not reflow and (gem_port.gem_id, gem_port.direction) in self._gem_ports:
240 return # nop
241
Girish Gowdrae933cd32019-11-21 21:04:41 +0530242 # The gem_port entity id is set to be same as gem_id
243 gem_port.entity_id = gem_port.gem_id
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500244 self.log.info('add-gem-port', gem_port=gem_port, reflow=reflow)
245 self._gem_ports[(gem_port.gem_id, gem_port.direction)] = gem_port
246
247 @inlineCallbacks
Girish Gowdrae933cd32019-11-21 21:04:41 +0530248 def remove_gem_id(self, gem_id, direction, remove_from_hw=True):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500249 """
250 Remove a GEM Port from this ONU
251
252 :param gem_id: (GemPort) GEM Port to remove
253 :param direction: Direction of the gem port
Girish Gowdrae933cd32019-11-21 21:04:41 +0530254 :param remove_from_hw: Remove the GemPort from hardware (remove if True else not)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500255 :return: deferred
256 """
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500257
258 gem_port = self._gem_ports.get((gem_id, direction))
259
260 if gem_port is None:
261 returnValue('nop')
262
263 try:
264 del self._gem_ports[(gem_id, direction)]
Girish Gowdrae933cd32019-11-21 21:04:41 +0530265 if remove_from_hw:
266 results = yield gem_port.remove_from_hardware(self._handler.openomci.omci_cc)
267 returnValue(results)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500268
269 except Exception as ex:
270 self.log.exception('gem-port-delete', e=ex)
271 raise