blob: db1daa8f0d384036b61705ea7882821a50da15c5 [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
16import structlog
17from twisted.internet.defer import inlineCallbacks, returnValue
18from voltha.protos.common_pb2 import AdminState, OperStatus
19from voltha.protos.device_pb2 import Port
20from voltha.extensions.omci.tasks.task import Task
21
22BRDCM_DEFAULT_VLAN = 4091
23TASK_PRIORITY = Task.DEFAULT_PRIORITY + 10
24DEFAULT_TPID = 0x8100
25DEFAULT_GEM_PAYLOAD = 48
26
27
28class PonPort(object):
29 """Wraps northbound-port/ANI support for ONU"""
30 # TODO: possibly get from olt
31 MIN_GEM_ENTITY_ID = 0x408
32 MAX_GEM_ENTITY_ID = 0x4FF # TODO: This limits is internal to specific ONU. It should be more "discoverable"?
33
34 def __init__(self, handler, port_no):
35 self.log = structlog.get_logger(device_id=handler.device_id, port_no=port_no)
36 self.log.debug('function-entry')
37
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
44 self._next_entity_id = PonPort.MIN_GEM_ENTITY_ID
45
46 self._admin_state = AdminState.ENABLED
47 self._oper_status = OperStatus.ACTIVE
48
49 self._gem_ports = {} # gem-id -> GemPort
50 self._tconts = {} # alloc-id -> TCont
51
52 self.ieee_mapper_service_profile_entity_id = 0x8001
53 self.mac_bridge_port_ani_entity_id = 0x2102 # TODO: can we just use the entity id from the anis list?
54
55 def __str__(self):
56 return "PonPort - port_number: {}, next_entity_id: {}, num_gem_ports: {}, num_tconts: {}".format(
57 self._port_number, self._next_entity_id, len(self._gem_ports), len(self._tconts))
58
59 def __repr__(self):
60 return str(self)
61
62 @staticmethod
63 def create(handler, port_no):
64 log = structlog.get_logger(device_id=handler.device_id, port_no=port_no)
65 log.debug('function-entry')
66 port = PonPort(handler, port_no)
67
68 return port
69
70 def _start(self):
71 self.log.debug('function-entry')
72 self._cancel_deferred()
73
74 self._admin_state = AdminState.ENABLED
75 self._oper_status = OperStatus.ACTIVE
76 self._update_adapter_agent()
77
78 def _stop(self):
79 self.log.debug('function-entry')
80 self._cancel_deferred()
81
82 self._admin_state = AdminState.DISABLED
83 self._oper_status = OperStatus.UNKNOWN
84 self._update_adapter_agent()
85
86 # TODO: stop h/w sync
87
88 def _cancel_deferred(self):
89 self.log.debug('function-entry')
90 d1, self._deferred = self._deferred, None
91
92 for d in [d1]:
93 try:
94 if d is not None and not d.called:
95 d.cancel()
96 except:
97 pass
98
99 def delete(self):
100 self.log.debug('function-entry')
101 self.enabled = False
102 self._valid = False
103 self._handler = None
104
105 @property
106 def enabled(self):
107 self.log.debug('function-entry')
108 return self._enabled
109
110 @enabled.setter
111 def enabled(self, value):
112 self.log.debug('function-entry')
113 if self._enabled != value:
114 self._enabled = value
115
116 if value:
117 self._start()
118 else:
119 self._stop()
120
121 @property
122 def port_number(self):
123 self.log.debug('function-entry')
124 return self._port_number
125
126 @property
127 def next_gem_entity_id(self):
128 self.log.debug('function-entry')
129 entity_id = self._next_entity_id
130
131 self._next_entity_id = self._next_entity_id + 1
132 if self._next_entity_id > PonPort.MAX_GEM_ENTITY_ID:
133 self._next_entity_id = PonPort.MIN_GEM_ENTITY_ID
134
135 return entity_id
136
137 @property
138 def tconts(self):
139 self.log.debug('function-entry')
140 return self._tconts
141
142 @property
143 def gem_ports(self):
144 self.log.debug('function-entry')
145 return self._gem_ports
146
147 def get_port(self):
148 """
149 Get the VOLTHA PORT object for this port
150 :return: VOLTHA Port object
151 """
152 self.log.debug('function-entry')
153
154 if self._port is None:
155 self._port = Port(port_no=self.port_number,
156 label='PON port',
157 type=Port.PON_ONU,
158 admin_state=self._admin_state,
159 oper_status=self._oper_status,
160 peers=[])
161 return self._port
162
163 def _update_adapter_agent(self):
164 """
165 Update the port status and state in the core
166 """
167 self.log.debug('function-entry')
168 self.log.debug('update-adapter-agent', admin_state=self._admin_state,
169 oper_status=self._oper_status)
170
171 if self._port is not None:
172 self._port.admin_state = self._admin_state
173 self._port.oper_status = self._oper_status
174
175 # adapter_agent add_port also does an update of port status
176 try:
177 self._handler.adapter_agent.add_port(self._handler.device_id, self.get_port())
178 except Exception as e:
179 self.log.exception('update-port', e=e)
180
181 def add_tcont(self, tcont, reflow=False):
182 """
183 Creates/ a T-CONT with the given alloc-id
184
185 :param tcont: (TCont) Object that maintains the TCONT properties
186 :param reflow: (boolean) If true, force add (used during h/w resync)
187 :return: (deferred)
188 """
189 self.log.debug('function-entry', tcont=tcont.alloc_id)
190
191 if not self._valid:
192 return # Deleting
193
194 if not reflow and tcont.alloc_id in self._tconts:
195 return # already created
196
197 self.log.info('add-tcont', tcont=tcont.alloc_id, reflow=reflow)
198 self._tconts[tcont.alloc_id] = tcont
199
200 def update_tcont_td(self, alloc_id, new_td):
201 self.log.debug('function-entry')
202
203 tcont = self._tconts.get(alloc_id)
204
205 if tcont is None:
206 return # not-found
207
208 tcont.traffic_descriptor = new_td
209
210 # TODO: Not yet implemented
211 #TODO: How does this affect ONU tcont settings?
212 #try:
213 # results = yield tcont.add_to_hardware(self._handler.omci)
214 #except Exception as e:
215 # self.log.exception('tcont', tcont=tcont, e=e)
216 # # May occur with xPON provisioning, use hw-resync to recover
217 # results = 'resync needed'
218 # returnValue(results)
219
220 @inlineCallbacks
221 def remove_tcont(self, alloc_id):
222 self.log.debug('function-entry')
223
224 tcont = self._tconts.get(alloc_id)
225
226 if tcont is None:
227 returnValue('nop')
228
229 try:
230 del self._tconts[alloc_id]
231 results = yield tcont.remove_from_hardware(self._handler.openomci.omci_cc)
232 returnValue(results)
233
234 except Exception as e:
235 self.log.exception('delete', e=e)
236 raise
237
238 def gem_port(self, gem_id, direction):
239 self.log.debug('function-entry')
240 return self._gem_ports.get((gem_id, direction))
241
242 @property
243 def gem_ids(self):
244 """Get all GEM Port IDs used by this ONU"""
245 self.log.debug('function-entry')
246 return sorted([gem_id_and_direction[0] for gem_id_and_direction, gem in self._gem_ports.items()])
247
248 def add_gem_port(self, gem_port, reflow=False):
249 """
250 Add a GEM Port to this ONU
251
252 :param gem_port: (GemPort) GEM Port to add
253 :param reflow: (boolean) If true, force add (used during h/w resync)
254 :return: (deferred)
255 """
256 self.log.debug('function-entry', gem_port=gem_port.gem_id)
257
258 if not self._valid:
259 return # Deleting
260
261 if not reflow and (gem_port.gem_id, gem_port.direction) in self._gem_ports:
262 return # nop
263
264 # if this is actually a new gem port then issue the next entity_id
265 gem_port.entity_id = self.next_gem_entity_id
266 self.log.info('add-gem-port', gem_port=gem_port, reflow=reflow)
267 self._gem_ports[(gem_port.gem_id, gem_port.direction)] = gem_port
268
269 @inlineCallbacks
270 def remove_gem_id(self, gem_id, direction):
271 """
272 Remove a GEM Port from this ONU
273
274 :param gem_id: (GemPort) GEM Port to remove
275 :param direction: Direction of the gem port
276 :return: deferred
277 """
278 self.log.debug('function-entry', gem_id=gem_id)
279
280 gem_port = self._gem_ports.get((gem_id, direction))
281
282 if gem_port is None:
283 returnValue('nop')
284
285 try:
286 del self._gem_ports[(gem_id, direction)]
287 results = yield gem_port.remove_from_hardware(self._handler.openomci.omci_cc)
288 returnValue(results)
289
290 except Exception as ex:
291 self.log.exception('gem-port-delete', e=ex)
292 raise
293
294