Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 1 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 2 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 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 | |
| 17 | """ |
| 18 | Agent to play gateway between CORE and an individual adapter. |
| 19 | """ |
| 20 | from uuid import uuid4 |
Stephane Barbarie | 52198b9 | 2017-03-02 13:44:46 -0500 | [diff] [blame] | 21 | import arrow |
| 22 | import re |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 23 | |
| 24 | import structlog |
Zsolt Haraszti | ef05ad2 | 2017-01-07 22:08:06 -0800 | [diff] [blame] | 25 | from google.protobuf.json_format import MessageToJson |
Zsolt Haraszti | 8925d1f | 2016-12-21 00:45:19 -0800 | [diff] [blame] | 26 | from scapy.packet import Packet |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 27 | from twisted.internet.defer import inlineCallbacks, returnValue |
| 28 | from zope.interface import implementer |
| 29 | |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 30 | from common.event_bus import EventBusClient |
Zsolt Haraszti | ef05ad2 | 2017-01-07 22:08:06 -0800 | [diff] [blame] | 31 | from common.frameio.frameio import hexify |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 32 | from voltha.adapters.interface import IAdapterAgent |
| 33 | from voltha.protos import third_party |
| 34 | from voltha.protos.device_pb2 import Device, Port |
Stephane Barbarie | cc6b2e6 | 2017-03-02 14:35:55 -0500 | [diff] [blame] | 35 | from voltha.protos.events_pb2 import KpiEvent, AlarmEvent, AlarmEventType, \ |
| 36 | AlarmEventSeverity, AlarmEventState, AlarmEventCategory |
Sergio Slobodrian | a2eb52b | 2017-03-07 12:24:46 -0500 | [diff] [blame] | 37 | from voltha.protos.device_pb2 import Device, Port, PmConfigs |
| 38 | from voltha.protos.events_pb2 import KpiEvent |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 39 | from voltha.protos.voltha_pb2 import DeviceGroup, LogicalDevice, \ |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 40 | LogicalPort, AdminState, OperStatus |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 41 | from voltha.registry import registry |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 42 | from voltha.core.flow_decomposer import OUTPUT |
Sergio Slobodrian | a2eb52b | 2017-03-07 12:24:46 -0500 | [diff] [blame] | 43 | import sys |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 44 | |
| 45 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 46 | @implementer(IAdapterAgent) |
| 47 | class AdapterAgent(object): |
| 48 | """ |
| 49 | Gate-keeper between CORE and device adapters. |
| 50 | |
| 51 | On one side it interacts with Core's internal model and update/dispatch |
| 52 | mechanisms. |
| 53 | |
| 54 | On the other side, it interacts with the adapters standard interface as |
| 55 | defined in |
| 56 | """ |
| 57 | |
| 58 | def __init__(self, adapter_name, adapter_cls): |
| 59 | self.adapter_name = adapter_name |
| 60 | self.adapter_cls = adapter_cls |
| 61 | self.core = registry('core') |
| 62 | self.adapter = None |
| 63 | self.adapter_node_proxy = None |
| 64 | self.root_proxy = self.core.get_proxy('/') |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 65 | self._rx_event_subscriptions = {} |
| 66 | self._tx_event_subscriptions = {} |
| 67 | self.event_bus = EventBusClient() |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 68 | self.packet_out_subscription = None |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 69 | self.log = structlog.get_logger(adapter_name=adapter_name) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 70 | |
| 71 | @inlineCallbacks |
| 72 | def start(self): |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 73 | self.log.debug('starting') |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 74 | config = self._get_adapter_config() # this may be None |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 75 | try: |
| 76 | adapter = self.adapter_cls(self, config) |
| 77 | yield adapter.start() |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 78 | self.adapter = adapter |
| 79 | self.adapter_node_proxy = self._update_adapter_node() |
| 80 | self._update_device_types() |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 81 | except Exception, e: |
| 82 | self.log.exception(e) |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 83 | self.log.info('started') |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 84 | returnValue(self) |
| 85 | |
| 86 | @inlineCallbacks |
| 87 | def stop(self): |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 88 | self.log.debug('stopping') |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 89 | if self.adapter is not None: |
| 90 | yield self.adapter.stop() |
| 91 | self.adapter = None |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 92 | self.log.info('stopped') |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 93 | |
| 94 | def _get_adapter_config(self): |
| 95 | """ |
| 96 | Opportunistically load persisted adapter configuration. |
| 97 | Return None if no configuration exists yet. |
| 98 | """ |
| 99 | proxy = self.core.get_proxy('/') |
| 100 | try: |
| 101 | config = proxy.get('/adapters/' + self.adapter_name) |
| 102 | return config |
| 103 | except KeyError: |
| 104 | return None |
| 105 | |
| 106 | def _update_adapter_node(self): |
| 107 | """ |
| 108 | Creates or updates the adapter node object based on self |
| 109 | description from the adapter. |
| 110 | """ |
| 111 | |
| 112 | adapter_desc = self.adapter.adapter_descriptor() |
| 113 | assert adapter_desc.id == self.adapter_name |
| 114 | path = self._make_up_to_date( |
| 115 | '/adapters', self.adapter_name, adapter_desc) |
| 116 | return self.core.get_proxy(path) |
| 117 | |
| 118 | def _update_device_types(self): |
| 119 | """ |
| 120 | Make sure device types are registered in Core |
| 121 | """ |
| 122 | device_types = self.adapter.device_types() |
| 123 | for device_type in device_types.items: |
| 124 | key = device_type.id |
| 125 | self._make_up_to_date('/device_types', key, device_type) |
| 126 | |
| 127 | def _make_up_to_date(self, container_path, key, data): |
| 128 | full_path = container_path + '/' + str(key) |
| 129 | root_proxy = self.core.get_proxy('/') |
| 130 | try: |
| 131 | root_proxy.get(full_path) |
| 132 | root_proxy.update(full_path, data) |
| 133 | except KeyError: |
| 134 | root_proxy.add(container_path, data) |
| 135 | return full_path |
| 136 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 137 | def _remove_node(self, container_path, key): |
| 138 | """ |
| 139 | Remove a node from the data model |
| 140 | :param container_path: path to node |
| 141 | :param key: node |
| 142 | :return: None |
| 143 | """ |
| 144 | full_path = container_path + '/' + str(key) |
| 145 | root_proxy = self.core.get_proxy('/') |
| 146 | try: |
| 147 | root_proxy.get(full_path) |
| 148 | root_proxy.remove(full_path) |
| 149 | except KeyError: |
| 150 | # Node does not exist |
| 151 | pass |
| 152 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 153 | # ~~~~~~~~~~~~~~~~~~~~~ Core-Facing Service ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 154 | |
| 155 | def adopt_device(self, device): |
| 156 | return self.adapter.adopt_device(device) |
| 157 | |
| 158 | def abandon_device(self, device): |
| 159 | return self.adapter.abandon_device(device) |
| 160 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 161 | def disable_device(self, device): |
| 162 | return self.adapter.disable_device(device) |
| 163 | |
| 164 | def reenable_device(self, device): |
| 165 | return self.adapter.reenable_device(device) |
| 166 | |
| 167 | def reboot_device(self, device): |
| 168 | return self.adapter.reboot_device(device) |
| 169 | |
| 170 | def delete_device(self, device): |
| 171 | return self.adapter.delete_device(device) |
| 172 | |
| 173 | def get_device_details(self, device): |
| 174 | return self.adapter.get_device_details(device) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 175 | |
Zsolt Haraszti | c5c5d10 | 2016-12-07 21:12:27 -0800 | [diff] [blame] | 176 | def update_flows_bulk(self, device, flows, groups): |
| 177 | return self.adapter.update_flows_bulk(device, flows, groups) |
| 178 | |
| 179 | def update_flows_incrementally(self, device, flow_changes, group_changes): |
| 180 | return self.update_flows_incrementally( |
| 181 | device, flow_changes, group_changes) |
| 182 | |
Sergio Slobodrian | a2eb52b | 2017-03-07 12:24:46 -0500 | [diff] [blame] | 183 | #def update_pm_collection(self, device, pm_collection_config): |
| 184 | # return self.adapter.update_pm_collection(device, pm_collection_config) |
| 185 | |
| 186 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 187 | # ~~~~~~~~~~~~~~~~~~~ Adapter-Facing Service ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 188 | |
| 189 | def get_device(self, device_id): |
| 190 | return self.root_proxy.get('/devices/{}'.format(device_id)) |
| 191 | |
| 192 | def add_device(self, device): |
| 193 | assert isinstance(device, Device) |
| 194 | self._make_up_to_date('/devices', device.id, device) |
| 195 | |
alshabib | be8ca2e | 2017-02-01 18:28:57 -0800 | [diff] [blame] | 196 | # Ultimately, assign devices to device grpups. |
| 197 | # see https://jira.opencord.org/browse/CORD-838 |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 198 | |
| 199 | dg = DeviceGroup(id='1') |
| 200 | self._make_up_to_date('/device_groups', dg.id, dg) |
| 201 | |
| 202 | # add device to device group |
alshabib | be8ca2e | 2017-02-01 18:28:57 -0800 | [diff] [blame] | 203 | # see https://jira.opencord.org/browse/CORD-838 |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 204 | |
| 205 | def update_device(self, device): |
| 206 | assert isinstance(device, Device) |
| 207 | |
| 208 | # we run the update through the device_agent so that the change |
| 209 | # does not loop back to the adapter unnecessarily |
| 210 | device_agent = self.core.get_device_agent(device.id) |
| 211 | device_agent.update_device(device) |
| 212 | |
Sergio Slobodrian | 2db4c10 | 2017-03-09 22:29:23 -0500 | [diff] [blame] | 213 | def update_device_pm_config(self, device_pm_config, init=False): |
| 214 | assert isinstance(device_pm_config, PmConfigs) |
| 215 | |
| 216 | # we run the update through the device_agent so that the change |
| 217 | # does not loop back to the adapter unnecessarily |
| 218 | device_agent = self.core.get_device_agent(device_pm_config.id) |
| 219 | device_agent.update_device_pm_config(device_pm_config,init) |
| 220 | |
| 221 | def update_adapter_pm_config(self, device, device_pm_config): |
| 222 | self.adapter.update_pm_config(device, device_pm_config) |
| 223 | |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame^] | 224 | def _add_peer_reference(self, device_id, port): |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 225 | # for referential integrity, add/augment references |
| 226 | port.device_id = device_id |
| 227 | me_as_peer = Port.PeerPort(device_id=device_id, port_no=port.port_no) |
| 228 | for peer in port.peers: |
| 229 | peer_port_path = '/devices/{}/ports/{}'.format( |
| 230 | peer.device_id, peer.port_no) |
| 231 | peer_port = self.root_proxy.get(peer_port_path) |
| 232 | if me_as_peer not in peer_port.peers: |
| 233 | new = peer_port.peers.add() |
| 234 | new.CopyFrom(me_as_peer) |
| 235 | self.root_proxy.update(peer_port_path, peer_port) |
| 236 | |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame^] | 237 | def _del_peer_reference(self, device_id, port): |
| 238 | me_as_peer = Port.PeerPort(device_id=device_id, port_no=port.port_no) |
| 239 | for peer in port.peers: |
| 240 | peer_port_path = '/devices/{}/ports/{}'.format( |
| 241 | peer.device_id, peer.port_no) |
| 242 | peer_port = self.root_proxy.get(peer_port_path) |
| 243 | if me_as_peer in peer_port.peers: |
| 244 | peer_port.peers.remove(me_as_peer) |
| 245 | self.root_proxy.update(peer_port_path, peer_port) |
| 246 | |
| 247 | def add_port(self, device_id, port): |
| 248 | assert isinstance(port, Port) |
| 249 | |
| 250 | # for referential integrity, add/augment references |
| 251 | self._add_peer_reference(device_id, port) |
| 252 | |
| 253 | # Add port |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 254 | self._make_up_to_date('/devices/{}/ports'.format(device_id), |
| 255 | port.port_no, port) |
| 256 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 257 | def disable_all_ports(self, device_id): |
| 258 | """ |
| 259 | Disable all ports on that device, i.e. change the admin status to |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame^] | 260 | disable and operational status to UNKNOWN. |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 261 | :param device_id: device id |
| 262 | :return: None |
| 263 | """ |
| 264 | |
| 265 | # get all device ports |
| 266 | ports = self.root_proxy.get('/devices/{}/ports'.format(device_id)) |
| 267 | for port in ports: |
| 268 | port.admin_state = AdminState.DISABLED |
| 269 | port.oper_status = OperStatus.UNKNOWN |
| 270 | self._make_up_to_date('/devices/{}/ports'.format(device_id), |
| 271 | port.port_no, port) |
| 272 | |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame^] | 273 | |
| 274 | def enable_all_ports(self, device_id): |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 275 | """ |
| 276 | Re-enable all ports on that device, i.e. change the admin status to |
| 277 | enabled and operational status to ACTIVE |
| 278 | :param device_id: device id |
| 279 | :return: None |
| 280 | """ |
| 281 | |
| 282 | # get all device ports |
| 283 | ports = self.root_proxy.get('/devices/{}/ports'.format(device_id)) |
| 284 | for port in ports: |
| 285 | port.admin_state = AdminState.ENABLED |
| 286 | port.oper_status = OperStatus.ACTIVE |
| 287 | self._make_up_to_date('/devices/{}/ports'.format(device_id), |
| 288 | port.port_no, port) |
| 289 | |
| 290 | def delete_all_peer_references(self, device_id): |
| 291 | """ |
| 292 | Remove all peer port references for that device |
| 293 | :param device_id: device_id of device |
| 294 | :return: None |
| 295 | """ |
| 296 | ports = self.root_proxy.get('/devices/{}/ports'.format(device_id)) |
| 297 | for port in ports: |
| 298 | port_path = '/devices/{}/ports/{}'.format(device_id, port.port_no) |
| 299 | for peer in port.peers: |
| 300 | port.peers.remove(peer) |
| 301 | self.root_proxy.update(port_path, port) |
| 302 | |
| 303 | def delete_port_reference_from_parent(self, device_id, port): |
| 304 | """ |
| 305 | Delete the port reference from the parent device |
| 306 | :param device_id: id of device containing the port |
| 307 | :param port: port to remove |
| 308 | :return: None |
| 309 | """ |
| 310 | assert isinstance(port, Port) |
| 311 | self.log.info('delete-port-reference', device_id=device_id, port=port) |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame^] | 312 | self._del_peer_reference(device_id, port) |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 313 | |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame^] | 314 | def add_port_reference_to_parent(self, device_id, port): |
| 315 | """ |
| 316 | Add the port reference to the parent device |
| 317 | :param device_id: id of device containing the port |
| 318 | :param port: port to add |
| 319 | :return: None |
| 320 | """ |
| 321 | assert isinstance(port, Port) |
| 322 | self.log.info('add-port-reference', device_id=device_id, port=port) |
| 323 | self._add_peer_reference(device_id, port) |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 324 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 325 | def _find_first_available_id(self): |
| 326 | logical_devices = self.root_proxy.get('/logical_devices') |
| 327 | existing_ids = set(ld.id for ld in logical_devices) |
| 328 | existing_datapath_ids = set(ld.datapath_id for ld in logical_devices) |
| 329 | i = 1 |
| 330 | while True: |
| 331 | if i not in existing_datapath_ids and str(i) not in existing_ids: |
| 332 | return i |
| 333 | i += 1 |
| 334 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 335 | def get_logical_device(self, logical_device_id): |
| 336 | return self.root_proxy.get('/logical_devices/{}'.format( |
| 337 | logical_device_id)) |
| 338 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 339 | def get_logical_port(self, logical_device_id, port_id): |
| 340 | return self.root_proxy.get('/logical_devices/{}/ports/{}'.format( |
| 341 | logical_device_id, port_id)) |
| 342 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 343 | def create_logical_device(self, logical_device): |
| 344 | assert isinstance(logical_device, LogicalDevice) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 345 | |
| 346 | if not logical_device.id: |
| 347 | id = self._find_first_available_id() |
| 348 | logical_device.id = str(id) |
| 349 | logical_device.datapath_id = id |
| 350 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 351 | self._make_up_to_date('/logical_devices', |
| 352 | logical_device.id, logical_device) |
| 353 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 354 | # Keep a reference to the packet out subscription as it will be |
| 355 | # referred during removal |
| 356 | self.packet_out_subscription = self.event_bus.subscribe( |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 357 | topic='packet-out:{}'.format(logical_device.id), |
| 358 | callback=lambda _, p: self.receive_packet_out(logical_device.id, p) |
| 359 | ) |
| 360 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 361 | return logical_device |
| 362 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 363 | |
| 364 | def delete_logical_device(self, logical_device): |
| 365 | """ |
| 366 | This will remove the logical device as well as all logical ports |
| 367 | associated with it |
| 368 | :param logical_device: The logical device to remove |
| 369 | :return: None |
| 370 | """ |
| 371 | assert isinstance(logical_device, LogicalDevice) |
| 372 | |
| 373 | # Remove packet out subscription |
| 374 | self.event_bus.unsubscribe(self.packet_out_subscription) |
| 375 | |
| 376 | # Remove node from the data model - this will trigger the logical |
| 377 | # device 'remove callbacks' as well as logical ports 'remove |
| 378 | # callbacks' if present |
| 379 | self._remove_node('/logical_devices', logical_device.id) |
| 380 | |
| 381 | |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 382 | def receive_packet_out(self, logical_device_id, ofp_packet_out): |
| 383 | |
| 384 | def get_port_out(opo): |
| 385 | for action in opo.actions: |
| 386 | if action.type == OUTPUT: |
| 387 | return action.output.port |
| 388 | |
| 389 | out_port = get_port_out(ofp_packet_out) |
| 390 | frame = ofp_packet_out.data |
| 391 | self.adapter.receive_packet_out(logical_device_id, out_port, frame) |
| 392 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 393 | def add_logical_port(self, logical_device_id, port): |
| 394 | assert isinstance(port, LogicalPort) |
| 395 | self._make_up_to_date( |
| 396 | '/logical_devices/{}/ports'.format(logical_device_id), |
| 397 | port.id, port) |
| 398 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 399 | def delete_logical_port(self, logical_device_id, port): |
| 400 | assert isinstance(port, LogicalPort) |
| 401 | self._remove_node('/logical_devices/{}/ports'.format( |
| 402 | logical_device_id), port.id) |
| 403 | |
| 404 | def update_logical_port(self, logical_device_id, port): |
| 405 | assert isinstance(port, LogicalPort) |
| 406 | self.log.debug('update-logical-port', |
| 407 | logical_device_id=logical_device_id, |
| 408 | port=port) |
| 409 | |
| 410 | self._make_up_to_date( |
| 411 | '/logical_devices/{}/ports'.format(logical_device_id), |
| 412 | port.id, port) |
| 413 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 414 | def child_device_detected(self, |
| 415 | parent_device_id, |
| 416 | parent_port_no, |
| 417 | child_device_type, |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 418 | proxy_address, |
| 419 | **kw): |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 420 | # we create new ONU device objects and insert them into the config |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 421 | # TODO should we auto-enable the freshly created device? Probably. |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 422 | device = Device( |
| 423 | id=uuid4().hex[:12], |
| 424 | type=child_device_type, |
| 425 | parent_id=parent_device_id, |
| 426 | parent_port_no=parent_port_no, |
| 427 | admin_state=AdminState.ENABLED, |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 428 | proxy_address=proxy_address, |
| 429 | **kw |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 430 | ) |
| 431 | self._make_up_to_date( |
| 432 | '/devices', device.id, device) |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 433 | |
| 434 | topic = self._gen_tx_proxy_address_topic(proxy_address) |
| 435 | self._tx_event_subscriptions[topic] = self.event_bus.subscribe( |
| 436 | topic, lambda t, m: self._send_proxied_message(proxy_address, m)) |
| 437 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 438 | def remove_all_logical_ports(self, logical_device_id): |
| 439 | """ Remove all logical ports from a given logical device""" |
| 440 | ports = self.root_proxy.get('/logical_devices/{}/ports') |
| 441 | for port in ports: |
| 442 | self._remove_node('/logical_devices/{}/ports', port.id) |
| 443 | |
| 444 | def delete_all_child_devices(self, parent_device_id): |
| 445 | """ Remove all ONUs from a given OLT """ |
| 446 | devices = self.root_proxy.get('/devices') |
| 447 | children_ids = set(d.id for d in devices if d.parent_id == parent_device_id) |
| 448 | self.log.debug('devices-to-delete', |
| 449 | parent_id=parent_device_id, |
| 450 | children_ids=children_ids) |
| 451 | for child_id in children_ids: |
| 452 | self._remove_node('/devices', child_id) |
| 453 | |
| 454 | def reenable_all_child_devices(self, parent_device_id): |
| 455 | """ Re-enable all ONUs from a given OLT """ |
| 456 | devices = self.root_proxy.get('/devices') |
| 457 | children_ids = set(d.id for d in devices if d.parent_id == parent_device_id) |
| 458 | self.log.debug('devices-to-reenable', |
| 459 | parent_id=parent_device_id, |
| 460 | children_ids=children_ids) |
| 461 | for child_id in children_ids: |
| 462 | device = self.get_device(child_id) |
| 463 | device.admin_state = AdminState.ENABLED |
| 464 | self._make_up_to_date( |
| 465 | '/devices', device.id, device) |
| 466 | |
| 467 | def disable_all_child_devices(self, parent_device_id): |
| 468 | """ Disable all ONUs from a given OLT """ |
| 469 | devices = self.root_proxy.get('/devices') |
| 470 | children_ids = set(d.id for d in devices if d.parent_id == parent_device_id) |
| 471 | self.log.debug('devices-to-disable', |
| 472 | parent_id=parent_device_id, |
| 473 | children_ids=children_ids) |
| 474 | for child_id in children_ids: |
| 475 | # Change the admin state pf the device to DISABLE |
| 476 | device = self.get_device(child_id) |
| 477 | device.admin_state = AdminState.DISABLED |
| 478 | self._make_up_to_date( |
| 479 | '/devices', device.id, device) |
| 480 | |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 481 | def _gen_rx_proxy_address_topic(self, proxy_address): |
| 482 | """Generate unique topic name specific to this proxy address for rx""" |
Zsolt Haraszti | ef05ad2 | 2017-01-07 22:08:06 -0800 | [diff] [blame] | 483 | topic = 'rx:' + MessageToJson(proxy_address) |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 484 | return topic |
| 485 | |
| 486 | def _gen_tx_proxy_address_topic(self, proxy_address): |
| 487 | """Generate unique topic name specific to this proxy address for tx""" |
Zsolt Haraszti | ef05ad2 | 2017-01-07 22:08:06 -0800 | [diff] [blame] | 488 | topic = 'tx:' + MessageToJson(proxy_address) |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 489 | return topic |
| 490 | |
| 491 | def register_for_proxied_messages(self, proxy_address): |
| 492 | topic = self._gen_rx_proxy_address_topic(proxy_address) |
| 493 | self._rx_event_subscriptions[topic] = self.event_bus.subscribe( |
Stephane Barbarie | cc6b2e6 | 2017-03-02 14:35:55 -0500 | [diff] [blame] | 494 | topic, |
| 495 | lambda t, m: self._receive_proxied_message(proxy_address, m)) |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 496 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 497 | def unregister_for_proxied_messages(self, proxy_address): |
| 498 | topic = self._gen_rx_proxy_address_topic(proxy_address) |
| 499 | self.event_bus.unsubscribe(self._rx_event_subscriptions[topic]) |
| 500 | del self._rx_event_subscriptions[topic] |
| 501 | |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 502 | def _receive_proxied_message(self, proxy_address, msg): |
| 503 | self.adapter.receive_proxied_message(proxy_address, msg) |
| 504 | |
| 505 | def send_proxied_message(self, proxy_address, msg): |
| 506 | topic = self._gen_tx_proxy_address_topic(proxy_address) |
| 507 | self.event_bus.publish(topic, msg) |
| 508 | |
| 509 | def _send_proxied_message(self, proxy_address, msg): |
| 510 | self.adapter.send_proxied_message(proxy_address, msg) |
| 511 | |
| 512 | def receive_proxied_message(self, proxy_address, msg): |
| 513 | topic = self._gen_rx_proxy_address_topic(proxy_address) |
| 514 | self.event_bus.publish(topic, msg) |
Zsolt Haraszti | 8925d1f | 2016-12-21 00:45:19 -0800 | [diff] [blame] | 515 | |
| 516 | # ~~~~~~~~~~~~~~~~~~ Handling packet-in and packet-out ~~~~~~~~~~~~~~~~~~~~ |
| 517 | |
| 518 | def send_packet_in(self, logical_device_id, logical_port_no, packet): |
| 519 | self.log.debug('send-packet-in', logical_device_id=logical_device_id, |
Zsolt Haraszti | ef05ad2 | 2017-01-07 22:08:06 -0800 | [diff] [blame] | 520 | logical_port_no=logical_port_no, packet=hexify(packet)) |
Zsolt Haraszti | 8925d1f | 2016-12-21 00:45:19 -0800 | [diff] [blame] | 521 | |
| 522 | if isinstance(packet, Packet): |
| 523 | packet = str(packet) |
| 524 | |
| 525 | topic = 'packet-in:' + logical_device_id |
| 526 | self.event_bus.publish(topic, (logical_port_no, packet)) |
Zsolt Haraszti | 749b095 | 2017-01-18 09:02:35 -0800 | [diff] [blame] | 527 | |
| 528 | # ~~~~~~~~~~~~~~~~~~~ Handling KPI metric submissions ~~~~~~~~~~~~~~~~~~~~~ |
Zsolt Haraszti | c5f740b | 2017-01-18 09:53:17 -0800 | [diff] [blame] | 529 | |
Zsolt Haraszti | 749b095 | 2017-01-18 09:02:35 -0800 | [diff] [blame] | 530 | def submit_kpis(self, kpi_event_msg): |
| 531 | try: |
| 532 | assert isinstance(kpi_event_msg, KpiEvent) |
| 533 | self.event_bus.publish('kpis', kpi_event_msg) |
| 534 | except Exception as e: |
| 535 | self.log.exception('failed-kpi-submission', |
| 536 | type=type(kpi_event_msg)) |
Stephane Barbarie | 52198b9 | 2017-03-02 13:44:46 -0500 | [diff] [blame] | 537 | |
| 538 | # ~~~~~~~~~~~~~~~~~~~ Handle alarm submissions ~~~~~~~~~~~~~~~~~~~~~ |
| 539 | |
Stephane Barbarie | cc6b2e6 | 2017-03-02 14:35:55 -0500 | [diff] [blame] | 540 | def create_alarm(self, id=None, resource_id=None, description=None, |
| 541 | raised_ts=0, changed_ts=0, |
| 542 | type=AlarmEventType.EQUIPMENT, |
Stephane Barbarie | bf3e10c | 2017-03-03 10:15:58 -0500 | [diff] [blame] | 543 | category=AlarmEventCategory.PON, |
Stephane Barbarie | cc6b2e6 | 2017-03-02 14:35:55 -0500 | [diff] [blame] | 544 | severity=AlarmEventSeverity.MINOR, |
| 545 | state=AlarmEventState.RAISED, |
Stephane Barbarie | 52198b9 | 2017-03-02 13:44:46 -0500 | [diff] [blame] | 546 | context=None): |
| 547 | |
| 548 | # Construct the ID if it is not provided |
| 549 | if id == None: |
| 550 | id = 'voltha.{}.{}'.format(self.adapter_name, resource_id) |
| 551 | |
| 552 | return AlarmEvent( |
| 553 | id=id, |
| 554 | resource_id=resource_id, |
| 555 | type=type, |
| 556 | category=category, |
| 557 | severity=severity, |
| 558 | state=state, |
| 559 | description=description, |
| 560 | reported_ts=arrow.utcnow().timestamp, |
| 561 | raised_ts=raised_ts, |
| 562 | changed_ts=changed_ts, |
| 563 | context=context |
| 564 | ) |
| 565 | |
| 566 | def submit_alarm(self, alarm_event_msg): |
| 567 | try: |
| 568 | assert isinstance(alarm_event_msg, AlarmEvent) |
| 569 | self.event_bus.publish('alarms', alarm_event_msg) |
| 570 | |
| 571 | except Exception as e: |
| 572 | self.log.exception('failed-alarm-submission', |
| 573 | type=type(alarm_event_msg)) |