blob: 9ee3198262ca098b1011c71082be142d3fb3e238 [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 enum import Enum
William Kurkian8235c1e2019-03-05 12:58:28 -050018from voltha_protos.common_pb2 import OperStatus, AdminState
19from voltha_protos.device_pb2 import Port
20from voltha_protos.openflow_13_pb2 import OFPPF_10GB_FD
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050021from pyvoltha.common.utils.nethelpers import mac_str_to_tuple
William Kurkian8235c1e2019-03-05 12:58:28 -050022from voltha_protos.logical_device_pb2 import LogicalPort
23from voltha_protos.openflow_13_pb2 import OFPPS_LIVE, OFPPF_FIBER, OFPPS_LINK_DOWN
24from voltha_protos.openflow_13_pb2 import ofp_port
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050025
26class UniType(Enum):
27 """
28 UNI Types Defined in G.988
29 """
30 PPTP = 'PhysicalPathTerminationPointEthernet'
31 VEIP = 'VirtualEthernetInterfacePoint'
32 # TODO: Add others as they become supported
33
34
35class UniPort(object):
36 """Wraps southbound-port(s) support for ONU"""
37
38 def __init__(self, handler, name, uni_id, port_no, ofp_port_no,
39 type=UniType.PPTP):
40 self.log = structlog.get_logger(device_id=handler.device_id,
41 port_no=port_no)
42 self._enabled = False
43 self._handler = handler
44 self._name = name
45 self._port = None
46 self._port_number = port_no
47 self._ofp_port_no = ofp_port_no
48 self._logical_port_number = None
49 self._entity_id = None
50 self._mac_bridge_port_num = 0
51 self._type = type
52 self._uni_id = uni_id
53
54 self._admin_state = AdminState.ENABLED
55 self._oper_status = OperStatus.ACTIVE
56
57 def __str__(self):
58 return "UniPort - name: {}, port_number: {}, entity_id: {}, mac_bridge_port_num: {}, type: {}, ofp_port: {}"\
59 .format(self.name, self.port_number, self.entity_id, self._mac_bridge_port_num, self.type, self._ofp_port_no)
60
61 def __repr__(self):
62 return str(self)
63
64 @staticmethod
65 def create(handler, name, uni_id, port_no, ofp_port_no, type):
66 port = UniPort(handler, name, uni_id, port_no, ofp_port_no, type)
67 return port
68
69 def _start(self):
70 self._cancel_deferred()
71 self._admin_state = AdminState.ENABLED
72 self._oper_status = OperStatus.ACTIVE
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050073
74 def _stop(self):
75 self._cancel_deferred()
76 self._admin_state = AdminState.DISABLED
77 self._oper_status = OperStatus.UNKNOWN
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050078
79 def delete(self):
80 self.enabled = False
81 self._handler = None
82
83 def _cancel_deferred(self):
84 pass
85
86 @property
87 def name(self):
88 return self._name
89
90 @property
91 def enabled(self):
92 return self._enabled
93
94 @enabled.setter
95 def enabled(self, value):
96 if self._enabled != value:
97 self._enabled = value
98
99 if value:
100 self._start()
101 else:
102 self._stop()
103
104 @property
105 def uni_id(self):
106 """
107 Physical prt index on ONU 0 - N
108 :return: (int) uni id
109 """
110 return self._uni_id
111
112
113 @property
114 def mac_bridge_port_num(self):
115 """
116 Port number used when creating MacBridgePortConfigurationDataFrame port number
117 :return: (int) port number
118 """
119 return self._mac_bridge_port_num
120
121 @mac_bridge_port_num.setter
122 def mac_bridge_port_num(self, value):
123 self._mac_bridge_port_num = value
124
125 @property
126 def port_number(self):
127 """
128 Physical device port number
129 :return: (int) port number
130 """
131 return self._port_number
132
133 @property
134 def entity_id(self):
135 """
136 OMCI UNI_G entity ID for port
137 """
138 return self._entity_id
139
140 @entity_id.setter
141 def entity_id(self, value):
142 assert self._entity_id is None, 'Cannot reset the Entity ID'
143 self._entity_id = value
144
145 @property
146 def logical_port_number(self):
147 """
148 Logical device port number (used as OpenFlow port for UNI)
149 :return: (int) port number
150 """
151 return self._logical_port_number
152
153 @property
154 def type(self):
155 """
156 UNI Type used in OMCI messaging
157 :return: (UniType) One of the enumerated types
158 """
159 return self._type
160
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500161 def get_port(self):
162 """
163 Get the VOLTHA PORT object for this port
164 :return: VOLTHA Port object
165 """
166 self._port = Port(port_no=self.port_number,
167 label=self.port_id_name(),
168 type=Port.ETHERNET_UNI,
169 admin_state=self._admin_state,
170 oper_status=self._oper_status)
171 return self._port
172
173 def port_id_name(self):
174 return 'uni-{}'.format(self._port_number)
175
176 def add_logical_port(self, openflow_port_no, multi_uni_naming,
177 capabilities=OFPPF_10GB_FD | OFPPF_FIBER,
178 speed=OFPPF_10GB_FD):
179
180 self.log.debug('function-entry')
181
182 if self._logical_port_number is not None:
183 # delete old logical port if it exists
184 try:
185 port = self._handler.adapter_agent.get_logical_port(self._handler.logical_device_id,
186 self.port_id_name())
187 self._handler.adapter_agent.delete_logical_port(self._handler.logical_device_id, port)
188
189 except Exception as e:
190 # assume this exception was because logical port does not already exist
191 pass
192
193 self._logical_port_number = None
194
195 port_no = openflow_port_no or self._ofp_port_no
196
197 if self._logical_port_number is None and port_no is not None:
198 self._logical_port_number = port_no
199
200 device = self._handler.adapter_agent.get_device(self._handler.device_id)
201
202 # leave the ports down until omci mib download has finished. otherwise flows push before time
203 openflow_port = ofp_port(
204 port_no=port_no,
205 hw_addr=mac_str_to_tuple('08:%02x:%02x:%02x:%02x:%02x' %
206 ((device.parent_port_no >> 8 & 0xff),
207 device.parent_port_no & 0xff,
208 (port_no >> 16) & 0xff,
209 (port_no >> 8) & 0xff,
210 port_no & 0xff)),
211 name=device.serial_number + ['', '-' + str(self._mac_bridge_port_num)][multi_uni_naming],
212 config=0,
213 state=OFPPS_LINK_DOWN,
214 curr=capabilities,
215 advertised=capabilities,
216 peer=capabilities,
217 curr_speed=speed,
218 max_speed=speed
219 )
220 self._handler.adapter_agent.add_logical_port(self._handler.logical_device_id,
221 LogicalPort(
222 id=self.port_id_name(),
223 ofp_port=openflow_port,
224 device_id=device.id,
225 device_port_no=self._port_number))
226
227 self.log.debug('logical-port', id=self.port_id_name(), device_port_no=self._port_number, openflow_port=openflow_port)