khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2017 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 | # |
| 16 | |
| 17 | """ |
| 18 | Agent to play gateway between CORE and an individual adapter. |
| 19 | """ |
| 20 | from uuid import uuid4 |
| 21 | |
| 22 | import arrow |
| 23 | import structlog |
| 24 | from google.protobuf.json_format import MessageToJson |
| 25 | from scapy.packet import Packet |
| 26 | from twisted.internet.defer import inlineCallbacks, returnValue |
| 27 | from zope.interface import implementer |
| 28 | |
| 29 | from adapters.common.event_bus import EventBusClient |
| 30 | from adapters.common.frameio.frameio import hexify |
| 31 | from adapters.common.utils.id_generation import create_cluster_logical_device_ids |
| 32 | from adapters.interface import IAdapterInterface |
| 33 | from adapters.protos.device_pb2 import Device |
| 34 | |
| 35 | from adapters.protos import third_party |
| 36 | from adapters.protos.device_pb2 import Device, Port, PmConfigs |
| 37 | from adapters.protos.events_pb2 import AlarmEvent, AlarmEventType, \ |
| 38 | AlarmEventSeverity, AlarmEventState, AlarmEventCategory |
| 39 | from adapters.protos.events_pb2 import KpiEvent |
| 40 | from adapters.protos.voltha_pb2 import DeviceGroup, LogicalDevice, \ |
| 41 | LogicalPort, AdminState, OperStatus, AlarmFilterRuleKey |
| 42 | from adapters.common.utils.registry import registry |
| 43 | from adapters.common.utils.id_generation import create_cluster_device_id |
| 44 | from adapters.protos.core_adapter_pb2 import IntType |
| 45 | import re |
| 46 | |
| 47 | |
| 48 | class MacAddressError(BaseException): |
| 49 | def __init__(self, error): |
| 50 | self.error = error |
| 51 | |
| 52 | |
| 53 | class IDError(BaseException): |
| 54 | def __init__(self, error): |
| 55 | self.error = error |
| 56 | |
| 57 | |
| 58 | @implementer(IAdapterInterface) |
| 59 | class AdapterRequestFacade(object): |
| 60 | """ |
| 61 | Gate-keeper between CORE and device adapters. |
| 62 | |
| 63 | On one side it interacts with Core's internal model and update/dispatch |
| 64 | mechanisms. |
| 65 | |
| 66 | On the other side, it interacts with the adapters standard interface as |
| 67 | defined in |
| 68 | """ |
| 69 | |
| 70 | def __init__(self, adapter): |
| 71 | self.adapter = adapter |
| 72 | |
| 73 | @inlineCallbacks |
| 74 | def start(self): |
| 75 | self.log.debug('starting') |
| 76 | |
| 77 | @inlineCallbacks |
| 78 | def stop(self): |
| 79 | self.log.debug('stopping') |
| 80 | |
| 81 | def adopt_device(self, device): |
| 82 | d = Device() |
| 83 | if device: |
| 84 | device.Unpack(d) |
| 85 | return (True, self.adapter.adopt_device(d)) |
| 86 | else: |
| 87 | return (False, d) |
| 88 | |
| 89 | def get_ofp_device_info(self, device): |
| 90 | d = Device() |
| 91 | if device: |
| 92 | device.Unpack(d) |
| 93 | return (True, self.adapter.get_ofp_device_info(d)) |
| 94 | else: |
| 95 | return (False, d) |
| 96 | |
| 97 | def get_ofp_port_info(self, device, port_no): |
| 98 | d = Device() |
| 99 | if device: |
| 100 | device.Unpack(d) |
| 101 | else: |
| 102 | return (False, d) |
| 103 | |
| 104 | p = IntType() |
| 105 | port_no.Unpack(p) |
| 106 | |
| 107 | return (True, self.adapter.get_ofp_port_info(d, p.val)) |
| 108 | |
| 109 | |
| 110 | def reconcile_device(self, device): |
| 111 | return self.adapter.reconcile_device(device) |
| 112 | |
| 113 | def abandon_device(self, device): |
| 114 | return self.adapter.abandon_device(device) |
| 115 | |
| 116 | def disable_device(self, device): |
| 117 | return self.adapter.disable_device(device) |
| 118 | |
| 119 | def reenable_device(self, device): |
| 120 | return self.adapter.reenable_device(device) |
| 121 | |
| 122 | def reboot_device(self, device): |
| 123 | d = Device() |
| 124 | if device: |
| 125 | device.Unpack(d) |
| 126 | return (True, self.adapter.reboot_device(d)) |
| 127 | else: |
| 128 | return (False, d) |
| 129 | |
| 130 | def download_image(self, device, request): |
| 131 | return self.adapter.download_image(device, request) |
| 132 | |
| 133 | def get_image_download_status(self, device, request): |
| 134 | return self.adapter.get_image_download_status(device, request) |
| 135 | |
| 136 | def cancel_image_download(self, device, request): |
| 137 | return self.adapter.cancel_image_download(device, request) |
| 138 | |
| 139 | def activate_image_update(self, device, request): |
| 140 | return self.adapter.activate_image_update(device, request) |
| 141 | |
| 142 | def revert_image_update(self, device, request): |
| 143 | return self.adapter.revert_image_update(device, request) |
| 144 | |
| 145 | def self_test(self, device): |
| 146 | return self.adapter.self_test_device(device) |
| 147 | |
| 148 | def delete_device(self, device): |
| 149 | # Remove all child devices |
| 150 | self.delete_all_child_devices(device.id) |
| 151 | |
| 152 | return self.adapter.delete_device(device) |
| 153 | |
| 154 | def get_device_details(self, device): |
| 155 | return self.adapter.get_device_details(device) |
| 156 | |
| 157 | def update_flows_bulk(self, device, flows, groups): |
| 158 | return self.adapter.update_flows_bulk(device, flows, groups) |
| 159 | |
| 160 | def update_flows_incrementally(self, device, flow_changes, group_changes): |
| 161 | return self.adapter.update_flows_incrementally(device, flow_changes, group_changes) |
| 162 | |
| 163 | def suppress_alarm(self, filter): |
| 164 | return self.adapter.suppress_alarm(filter) |
| 165 | |
| 166 | def unsuppress_alarm(self, filter): |
| 167 | return self.adapter.unsuppress_alarm(filter) |
| 168 | |