blob: cbae56d78440000d9846b50648a59c9055454d7f [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"""
khenaidoob9203542018-09-17 22:56:37 -040021
khenaidoo6fdf0ba2018-11-02 14:38:33 -040022from twisted.internet.defer import inlineCallbacks
khenaidoob9203542018-09-17 22:56:37 -040023from zope.interface import implementer
24
khenaidoofdbad6e2018-11-06 22:26:38 -050025from python.adapters.interface import IAdapterInterface
26from python.protos.core_adapter_pb2 import IntType, InterAdapterMessage, StrType, Error, ErrorCode
27from python.protos.device_pb2 import Device
28from python.protos.openflow_13_pb2 import FlowChanges, FlowGroups, Flows, \
29 FlowGroupChanges, ofp_packet_out
khenaidoob9203542018-09-17 22:56:37 -040030
31
32class MacAddressError(BaseException):
33 def __init__(self, error):
34 self.error = error
35
36
37class IDError(BaseException):
38 def __init__(self, error):
39 self.error = error
40
41
42@implementer(IAdapterInterface)
43class AdapterRequestFacade(object):
44 """
45 Gate-keeper between CORE and device adapters.
46
47 On one side it interacts with Core's internal model and update/dispatch
48 mechanisms.
49
50 On the other side, it interacts with the adapters standard interface as
51 defined in
52 """
53
54 def __init__(self, adapter):
55 self.adapter = adapter
56
57 @inlineCallbacks
58 def start(self):
59 self.log.debug('starting')
60
61 @inlineCallbacks
62 def stop(self):
63 self.log.debug('stopping')
64
65 def adopt_device(self, device):
66 d = Device()
67 if device:
68 device.Unpack(d)
khenaidoo92e62c52018-10-03 14:02:54 -040069 return True, self.adapter.adopt_device(d)
khenaidoob9203542018-09-17 22:56:37 -040070 else:
khenaidoofdbad6e2018-11-06 22:26:38 -050071 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
72 reason="device-invalid")
khenaidoob9203542018-09-17 22:56:37 -040073
74 def get_ofp_device_info(self, device):
75 d = Device()
76 if device:
77 device.Unpack(d)
khenaidoo92e62c52018-10-03 14:02:54 -040078 return True, self.adapter.get_ofp_device_info(d)
khenaidoob9203542018-09-17 22:56:37 -040079 else:
khenaidoofdbad6e2018-11-06 22:26:38 -050080 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
81 reason="device-invalid")
khenaidoob9203542018-09-17 22:56:37 -040082
83 def get_ofp_port_info(self, device, port_no):
84 d = Device()
85 if device:
86 device.Unpack(d)
87 else:
khenaidoofdbad6e2018-11-06 22:26:38 -050088 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
89 reason="device-invalid")
khenaidoob9203542018-09-17 22:56:37 -040090 p = IntType()
khenaidoofdbad6e2018-11-06 22:26:38 -050091 if port_no:
92 port_no.Unpack(p)
93 else:
94 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
95 reason="port-no-invalid")
khenaidoob9203542018-09-17 22:56:37 -040096
khenaidoo92e62c52018-10-03 14:02:54 -040097 return True, self.adapter.get_ofp_port_info(d, p.val)
khenaidoob9203542018-09-17 22:56:37 -040098
khenaidoob9203542018-09-17 22:56:37 -040099 def reconcile_device(self, device):
100 return self.adapter.reconcile_device(device)
101
102 def abandon_device(self, device):
103 return self.adapter.abandon_device(device)
104
105 def disable_device(self, device):
khenaidoo92e62c52018-10-03 14:02:54 -0400106 d = Device()
107 if device:
108 device.Unpack(d)
109 return True, self.adapter.disable_device(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
114 def reenable_device(self, device):
khenaidoo92e62c52018-10-03 14:02:54 -0400115 d = Device()
116 if device:
117 device.Unpack(d)
118 return True, self.adapter.reenable_device(d)
119 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500120 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
121 reason="device-invalid")
khenaidoob9203542018-09-17 22:56:37 -0400122
123 def reboot_device(self, device):
124 d = Device()
125 if device:
126 device.Unpack(d)
127 return (True, self.adapter.reboot_device(d))
128 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500129 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
130 reason="device-invalid")
khenaidoob9203542018-09-17 22:56:37 -0400131
132 def download_image(self, device, request):
133 return self.adapter.download_image(device, request)
134
135 def get_image_download_status(self, device, request):
136 return self.adapter.get_image_download_status(device, request)
137
138 def cancel_image_download(self, device, request):
139 return self.adapter.cancel_image_download(device, request)
140
141 def activate_image_update(self, device, request):
142 return self.adapter.activate_image_update(device, request)
143
144 def revert_image_update(self, device, request):
145 return self.adapter.revert_image_update(device, request)
146
147 def self_test(self, device):
148 return self.adapter.self_test_device(device)
149
150 def delete_device(self, device):
khenaidoo4d4802d2018-10-04 21:59:49 -0400151 d = Device()
152 if device:
153 device.Unpack(d)
154 return (True, self.adapter.delete_device(d))
155 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500156 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
157 reason="device-invalid")
khenaidoob9203542018-09-17 22:56:37 -0400158
159 def get_device_details(self, device):
160 return self.adapter.get_device_details(device)
161
162 def update_flows_bulk(self, device, flows, groups):
khenaidoo19d7b632018-10-30 10:49:50 -0400163 d = Device()
164 if device:
165 device.Unpack(d)
166 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500167 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
168 reason="device-invalid")
khenaidoo19d7b632018-10-30 10:49:50 -0400169 f = Flows()
170 if flows:
171 flows.Unpack(f)
172
173 g = FlowGroups()
174 if groups:
175 groups.Unpack(g)
176
177 return (True, self.adapter.update_flows_bulk(d, f, g))
khenaidoob9203542018-09-17 22:56:37 -0400178
179 def update_flows_incrementally(self, device, flow_changes, group_changes):
khenaidoo19d7b632018-10-30 10:49:50 -0400180 d = Device()
181 if device:
182 device.Unpack(d)
183 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500184 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
185 reason="device-invalid")
khenaidoo19d7b632018-10-30 10:49:50 -0400186 f = FlowChanges()
187 if flow_changes:
188 flow_changes.Unpack(f)
189
190 g = FlowGroupChanges()
191 if group_changes:
192 group_changes.Unpack(g)
193
194 return (True, self.adapter.update_flows_incrementally(d, f, g))
khenaidoob9203542018-09-17 22:56:37 -0400195
196 def suppress_alarm(self, filter):
197 return self.adapter.suppress_alarm(filter)
198
199 def unsuppress_alarm(self, filter):
200 return self.adapter.unsuppress_alarm(filter)
201
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400202 def process_inter_adapter_message(self, msg):
203 m = InterAdapterMessage()
204 if msg:
205 msg.Unpack(m)
206 else:
khenaidoofdbad6e2018-11-06 22:26:38 -0500207 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
208 reason="msg-invalid")
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400209
210 return (True, self.adapter.process_inter_adapter_message(m))
khenaidoofdbad6e2018-11-06 22:26:38 -0500211
212
213 def receive_packet_out(self, deviceId, outPort, packet):
214 d_id = StrType()
215 if deviceId:
216 deviceId.Unpack(d_id)
217 else:
218 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
219 reason="deviceid-invalid")
220
221 op = IntType
222 if outPort:
223 outPort.Unpack(op)
224 else:
225 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
226 reason="outport-invalid")
227
228 p = ofp_packet_out()
229 if packet:
230 packet.Unpack(p)
231 else:
232 return False, Error(code=ErrorCode.INVALID_PARAMETERS,
233 reason="packet-invalid")
234
235 return (True, self.adapter.receive_packet_out(d_id, op, p))
236