blob: de8ae0b68b2a3d3a791991d91be13d7f54ed7711 [file] [log] [blame]
khenaidoob9203542018-09-17 22:56:37 -04001#
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"""
khenaidoo6fdf0ba2018-11-02 14:38:33 -040018This facade handles kafka-formatted messages from the Core, extracts the kafka
19formatting and forwards the request to the concrete handler.
khenaidoob9203542018-09-17 22:56:37 -040020"""
khenaidoo90847922018-12-03 14:47:51 -050021import structlog
khenaidoo6fdf0ba2018-11-02 14:38:33 -040022from twisted.internet.defer import inlineCallbacks
khenaidoob9203542018-09-17 22:56:37 -040023from zope.interface import implementer
khenaidoo43c82122018-11-22 18:38:28 -050024from twisted.internet import reactor
khenaidooca301322019-01-09 23:06:32 -050025
khenaidoo90847922018-12-03 14:47:51 -050026from afkak.consumer import OFFSET_LATEST, OFFSET_EARLIEST
khenaidoofdbad6e2018-11-06 22:26:38 -050027from python.adapters.interface import IAdapterInterface
khenaidoo79232702018-12-04 11:00:41 -050028from python.protos.inter_container_pb2 import IntType, InterAdapterMessage, StrType, Error, ErrorCode
khenaidoofdbad6e2018-11-06 22:26:38 -050029from python.protos.device_pb2 import Device
30from python.protos.openflow_13_pb2 import FlowChanges, FlowGroups, Flows, \
31 FlowGroupChanges, ofp_packet_out
khenaidoo43c82122018-11-22 18:38:28 -050032from python.adapters.kafka.kafka_inter_container_library import IKafkaMessagingProxy, \
khenaidooca301322019-01-09 23:06:32 -050033 get_messaging_proxy, KAFKA_OFFSET_LATEST, KAFKA_OFFSET_EARLIEST
khenaidoob9203542018-09-17 22:56:37 -040034
khenaidoo90847922018-12-03 14:47:51 -050035log = structlog.get_logger()
khenaidoob9203542018-09-17 22:56:37 -040036
37class MacAddressError(BaseException):
38 def __init__(self, error):
39 self.error = error
40
41
42class IDError(BaseException):
43 def __init__(self, error):
44 self.error = error
45
46
47@implementer(IAdapterInterface)
48class AdapterRequestFacade(object):
49 """
50 Gate-keeper between CORE and device adapters.
51
52 On one side it interacts with Core's internal model and update/dispatch
53 mechanisms.
54
55 On the other side, it interacts with the adapters standard interface as
56 defined in
57 """
58
59 def __init__(self, adapter):
60 self.adapter = adapter
61
62 @inlineCallbacks
63 def start(self):
khenaidoo90847922018-12-03 14:47:51 -050064 log.debug('starting')
khenaidoob9203542018-09-17 22:56:37 -040065
66 @inlineCallbacks
67 def stop(self):
khenaidoo90847922018-12-03 14:47:51 -050068 log.debug('stopping')
khenaidoob9203542018-09-17 22:56:37 -040069
khenaidoo90847922018-12-03 14:47:51 -050070 @inlineCallbacks
khenaidoo43c82122018-11-22 18:38:28 -050071 def createKafkaDeviceTopic(self, deviceId):
khenaidoo90847922018-12-03 14:47:51 -050072 log.debug("subscribing-to-topic", device_id=deviceId)
khenaidoo43c82122018-11-22 18:38:28 -050073 kafka_proxy = get_messaging_proxy()
74 device_topic = kafka_proxy.get_default_topic() + "_" + deviceId
khenaidoo90847922018-12-03 14:47:51 -050075 # yield kafka_proxy.create_topic(topic=device_topic)
khenaidooca301322019-01-09 23:06:32 -050076 yield kafka_proxy.subscribe(topic=device_topic, group_id=device_topic, target_cls=self, offset=KAFKA_OFFSET_EARLIEST)
khenaidoo90847922018-12-03 14:47:51 -050077 log.debug("subscribed-to-topic", topic=device_topic)
khenaidoo43c82122018-11-22 18:38:28 -050078
khenaidoob9203542018-09-17 22:56:37 -040079 def adopt_device(self, device):
80 d = Device()
81 if device:
82 device.Unpack(d)
khenaidoo43c82122018-11-22 18:38:28 -050083
khenaidoo90847922018-12-03 14:47:51 -050084 # Start the creation of a device specific topic to handle all
85 # subsequent requests from the Core. This adapter instance will
86 # handle all requests for that device.
87 reactor.callLater(0, self.createKafkaDeviceTopic, d.id)
88
khenaidoo43c82122018-11-22 18:38:28 -050089 result = self.adapter.adopt_device(d)
90 # return True, self.adapter.adopt_device(d)
91
khenaidoo43c82122018-11-22 18:38:28 -050092 return True, result
khenaidoob9203542018-09-17 22:56:37 -040093 else:
khenaidoofdbad6e2018-11-06 22:26:38 -050094 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
95 reason="device-invalid")
khenaidoob9203542018-09-17 22:56:37 -040096
97 def get_ofp_device_info(self, device):
98 d = Device()
99 if device:
100 device.Unpack(d)
khenaidoo92e62c52018-10-03 14:02:54 -0400101 return True, self.adapter.get_ofp_device_info(d)
khenaidoob9203542018-09-17 22:56:37 -0400102 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500103 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
104 reason="device-invalid")
khenaidoob9203542018-09-17 22:56:37 -0400105
106 def get_ofp_port_info(self, device, port_no):
107 d = Device()
108 if device:
109 device.Unpack(d)
110 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500111 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
112 reason="device-invalid")
khenaidoob9203542018-09-17 22:56:37 -0400113 p = IntType()
khenaidoofdbad6e2018-11-06 22:26:38 -0500114 if port_no:
115 port_no.Unpack(p)
116 else:
117 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
118 reason="port-no-invalid")
khenaidoob9203542018-09-17 22:56:37 -0400119
khenaidoo92e62c52018-10-03 14:02:54 -0400120 return True, self.adapter.get_ofp_port_info(d, p.val)
khenaidoob9203542018-09-17 22:56:37 -0400121
khenaidoob9203542018-09-17 22:56:37 -0400122 def reconcile_device(self, device):
123 return self.adapter.reconcile_device(device)
124
125 def abandon_device(self, device):
126 return self.adapter.abandon_device(device)
127
128 def disable_device(self, device):
khenaidoo92e62c52018-10-03 14:02:54 -0400129 d = Device()
130 if device:
131 device.Unpack(d)
132 return True, self.adapter.disable_device(d)
133 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500134 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
135 reason="device-invalid")
khenaidoob9203542018-09-17 22:56:37 -0400136
137 def reenable_device(self, device):
khenaidoo92e62c52018-10-03 14:02:54 -0400138 d = Device()
139 if device:
140 device.Unpack(d)
141 return True, self.adapter.reenable_device(d)
142 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500143 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
144 reason="device-invalid")
khenaidoob9203542018-09-17 22:56:37 -0400145
146 def reboot_device(self, device):
147 d = Device()
148 if device:
149 device.Unpack(d)
150 return (True, self.adapter.reboot_device(d))
151 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500152 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
153 reason="device-invalid")
khenaidoob9203542018-09-17 22:56:37 -0400154
155 def download_image(self, device, request):
156 return self.adapter.download_image(device, request)
157
158 def get_image_download_status(self, device, request):
159 return self.adapter.get_image_download_status(device, request)
160
161 def cancel_image_download(self, device, request):
162 return self.adapter.cancel_image_download(device, request)
163
164 def activate_image_update(self, device, request):
165 return self.adapter.activate_image_update(device, request)
166
167 def revert_image_update(self, device, request):
168 return self.adapter.revert_image_update(device, request)
169
170 def self_test(self, device):
171 return self.adapter.self_test_device(device)
172
173 def delete_device(self, device):
khenaidoo4d4802d2018-10-04 21:59:49 -0400174 d = Device()
175 if device:
176 device.Unpack(d)
khenaidoo43c82122018-11-22 18:38:28 -0500177 result = self.adapter.delete_device(d)
178 # return (True, self.adapter.delete_device(d))
179
180 # Before we return, delete the device specific topic as we will no
181 # longer receive requests from the Core for that device
182 kafka_proxy = get_messaging_proxy()
183 device_topic = kafka_proxy.get_default_topic() + "/" + d.id
184 kafka_proxy.unsubscribe(topic=device_topic)
185
186 return (True, result)
khenaidoo4d4802d2018-10-04 21:59:49 -0400187 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500188 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
189 reason="device-invalid")
khenaidoob9203542018-09-17 22:56:37 -0400190
191 def get_device_details(self, device):
192 return self.adapter.get_device_details(device)
193
194 def update_flows_bulk(self, device, flows, groups):
khenaidoo19d7b632018-10-30 10:49:50 -0400195 d = Device()
196 if device:
197 device.Unpack(d)
198 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500199 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
200 reason="device-invalid")
khenaidoo19d7b632018-10-30 10:49:50 -0400201 f = Flows()
202 if flows:
203 flows.Unpack(f)
204
205 g = FlowGroups()
206 if groups:
207 groups.Unpack(g)
208
209 return (True, self.adapter.update_flows_bulk(d, f, g))
khenaidoob9203542018-09-17 22:56:37 -0400210
211 def update_flows_incrementally(self, device, flow_changes, group_changes):
khenaidoo19d7b632018-10-30 10:49:50 -0400212 d = Device()
213 if device:
214 device.Unpack(d)
215 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500216 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
217 reason="device-invalid")
khenaidoo19d7b632018-10-30 10:49:50 -0400218 f = FlowChanges()
219 if flow_changes:
220 flow_changes.Unpack(f)
221
222 g = FlowGroupChanges()
223 if group_changes:
224 group_changes.Unpack(g)
225
226 return (True, self.adapter.update_flows_incrementally(d, f, g))
khenaidoob9203542018-09-17 22:56:37 -0400227
228 def suppress_alarm(self, filter):
229 return self.adapter.suppress_alarm(filter)
230
231 def unsuppress_alarm(self, filter):
232 return self.adapter.unsuppress_alarm(filter)
233
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400234 def process_inter_adapter_message(self, msg):
235 m = InterAdapterMessage()
236 if msg:
237 msg.Unpack(m)
238 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500239 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
240 reason="msg-invalid")
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400241
242 return (True, self.adapter.process_inter_adapter_message(m))
khenaidoofdbad6e2018-11-06 22:26:38 -0500243
244
245 def receive_packet_out(self, deviceId, outPort, packet):
246 d_id = StrType()
247 if deviceId:
248 deviceId.Unpack(d_id)
249 else:
250 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
251 reason="deviceid-invalid")
252
253 op = IntType
254 if outPort:
255 outPort.Unpack(op)
256 else:
257 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
258 reason="outport-invalid")
259
260 p = ofp_packet_out()
261 if packet:
262 packet.Unpack(p)
263 else:
264 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
265 reason="packet-invalid")
266
267 return (True, self.adapter.receive_packet_out(d_id, op, p))
268