blob: fd0d0c15d9b3f586ddcbbcd50d0b9a786303559c [file] [log] [blame]
Chip Boling8e042f62019-02-12 16:14:34 -06001# Copyright 2017-present Adtran, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import structlog
16from twisted.internet.defer import inlineCallbacks, returnValue
17from pyvoltha.protos.common_pb2 import AdminState, OperStatus
18from pyvoltha.protos.device_pb2 import Port
19
20
21class PonPort(object):
22 """Wraps northbound-port/ANI support for ONU"""
23 MIN_GEM_ENTITY_ID = 0x4900
24 MAX_GEM_ENTITY_ID = 0x4AFF
25
26 def __init__(self, handler, port_no):
27 self.log = structlog.get_logger(device_id=handler.device_id, port_no=port_no)
28
29 self._enabled = False
30 self._valid = True
31 self._handler = handler
32 self._deferred = None
33 self._port = None
34 self._port_number = port_no
35 self._entity_id = None # ANI entity ID
36 self._next_entity_id = PonPort.MIN_GEM_ENTITY_ID
37
38 self._admin_state = AdminState.ENABLED
39 self._oper_status = OperStatus.ACTIVE
40
41 self._gem_ports = {} # gem-id -> GemPort
42 self._tconts = {} # alloc-id -> TCont
43
44 # OMCI resources
45 # TODO: These could be dynamically chosen (can be most any value)
46 self.ieee_mapper_service_profile_entity_id = 0x100
47 self.mac_bridge_port_ani_entity_id = 0x100
48
49 def __str__(self):
50 return "PonPort" # TODO: Encode current state
51
52 @staticmethod
53 def create(handler, port_no):
54 port = PonPort(handler, port_no)
55 return port
56
57 def _start(self):
58 self._cancel_deferred()
59
60 self._admin_state = AdminState.ENABLED
61 self._oper_status = OperStatus.ACTIVE
62 self._update_adapter_agent()
63
64 def _stop(self):
65 self._cancel_deferred()
66
67 self._admin_state = AdminState.DISABLED
68 self._oper_status = OperStatus.UNKNOWN
69 self._update_adapter_agent()
70
71 # TODO: stop h/w sync
72
73 def _cancel_deferred(self):
74 d1, self._deferred = self._deferred, None
75
76 for d in [d1]:
77 try:
78 if d is not None and not d.called:
79 d.cancel()
80 except:
81 pass
82
83 def delete(self):
84 self.enabled = False
85 self._valid = False
86 self._handler = None
87
88 @property
89 def enabled(self):
90 return self._enabled
91
92 @enabled.setter
93 def enabled(self, value):
94 if self._enabled != value:
95 self._enabled = value
96
97 if value:
98 self._start()
99 else:
100 self._stop()
101
102 @property
103 def port_number(self):
104 return self._port_number
105
106 @property
107 def entity_id(self):
108 """
109 OMCI ANI_G entity ID for port
110 """
111 return self._entity_id
112
113 @entity_id.setter
114 def entity_id(self, value):
115 assert self._entity_id is None or self._entity_id == value, 'Cannot reset the Entity ID'
116 self._entity_id = value
117
118 @property
119 def next_gem_entity_id(self):
120 entity_id = self._next_entity_id
121
122 self._next_entity_id = self._next_entity_id + 1
123 if self._next_entity_id > PonPort.MAX_GEM_ENTITY_ID:
124 self._next_entity_id = PonPort.MIN_GEM_ENTITY_ID
125
126 return entity_id
127
128 @property
129 def tconts(self):
130 return self._tconts
131
132 @property
133 def gem_ports(self):
134 return self._gem_ports
135
136 def get_port(self):
137 """
138 Get the VOLTHA PORT object for this port
139 :return: VOLTHA Port object
140 """
141 if self._port is None:
142 device = self._handler.adapter_agent.get_device(self._handler.device_id)
143
144 self._port = Port(port_no=self.port_number,
145 label='PON port',
146 type=Port.PON_ONU,
147 admin_state=self._admin_state,
148 oper_status=self._oper_status,
149 peers=[Port.PeerPort(device_id=device.parent_id,
150 port_no=device.parent_port_no)])
151 return self._port
152
153 def _update_adapter_agent(self):
154 """
155 Update the port status and state in the core
156 """
157 self.log.debug('update-adapter-agent', admin_state=self._admin_state,
158 oper_status=self._oper_status)
159
160 if self._port is not None:
161 self._port.admin_state = self._admin_state
162 self._port.oper_status = self._oper_status
163
164 # adapter_agent add_port also does an update of port status
165 try:
166 self._handler.adapter_agent.add_port(self._handler.device_id, self.get_port())
167 except Exception as e:
168 self.log.exception('update-port', e=e)
169
170 def add_tcont(self, tcont, reflow=False):
171 """
172 Creates/ a T-CONT with the given alloc-id
173
174 :param tcont: (TCont) Object that maintains the TCONT properties
175 :param reflow: (boolean) If true, force add (used during h/w resync)
176 :return: (deferred)
177 """
178 if not self._valid:
179 return # Deleting
180
181 if not reflow and tcont.alloc_id in self._tconts:
182 return # already created
183
184 self.log.info('add', tcont=tcont, reflow=reflow)
185 self._tconts[tcont.alloc_id] = tcont
186
187 @inlineCallbacks
188 def remove_tcont(self, alloc_id):
189 tcont = self._tconts.get(alloc_id)
190
191 if tcont is None:
192 returnValue('nop')
193
194 try:
195 del self._tconts[alloc_id]
196 results = yield tcont.remove_from_hardware(self._handler.openomci.omci_cc)
197 returnValue(results)
198
199 except Exception as e:
200 self.log.exception('delete', e=e)
201 raise
202
203 def gem_port(self, gem_id):
204 return self._gem_ports.get(gem_id)
205
206 @property
207 def gem_ids(self):
208 """Get all GEM Port IDs used by this ONU"""
209 return sorted([gem_id for gem_id, gem in self._gem_ports.items()])
210
211 def add_gem_port(self, gem_port, reflow=False):
212 """
213 Add a GEM Port to this ONU
214
215 :param gem_port: (GemPort) GEM Port to add
216 :param reflow: (boolean) If true, force add (used during h/w resync)
217 :return: (deferred)
218 """
219 if not self._valid:
220 return # Deleting
221
222 if not reflow and gem_port.gem_id in self._gem_ports:
223 return # nop
224
225 self.log.info('add', gem_port=gem_port, reflow=reflow)
226 self._gem_ports[gem_port.gem_id] = gem_port
227
228 @inlineCallbacks
229 def remove_gem_id(self, gem_id):
230 """
231 Remove a GEM Port from this ONU
232
233 :param gem_port: (GemPort) GEM Port to remove
234 :return: deferred
235 """
236 gem_port = self._gem_ports.get(gem_id)
237
238 if gem_port is None:
239 returnValue('nop')
240
241 try:
242 del self._gem_ports[gem_id]
243 results = yield gem_port.remove_from_hardware(self._handler.openomci.omci_cc)
244 returnValue(results)
245
246 except Exception as ex:
247 self.log.exception('gem-port-delete', e=ex)
248 raise