blob: 8d252a3421030914a470dd8d77ffbc5b0b843242 [file] [log] [blame]
khenaidoob9203542018-09-17 22:56:37 -04001#
khenaidoo6fdf0ba2018-11-02 14:38:33 -04002# Copyright 2018 the original author or authors.
khenaidoob9203542018-09-17 22:56:37 -04003#
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 -040018Agent to play gateway between CORE and an adapter.
khenaidoob9203542018-09-17 22:56:37 -040019"""
khenaidoob9203542018-09-17 22:56:37 -040020import structlog
khenaidoob9203542018-09-17 22:56:37 -040021from google.protobuf.message import Message
khenaidoo6fdf0ba2018-11-02 14:38:33 -040022from twisted.internet.defer import inlineCallbacks, returnValue
23
khenaidoofdbad6e2018-11-06 22:26:38 -050024from container_proxy import ContainerProxy
25from python.protos.common_pb2 import ID, ConnectStatus, OperStatus
khenaidoo79232702018-12-04 11:00:41 -050026from python.protos.inter_container_pb2 import StrType, BoolType, IntType, Packet
khenaidoofdbad6e2018-11-06 22:26:38 -050027from python.protos.device_pb2 import Device, Ports
28from python.protos.voltha_pb2 import CoreInstance
khenaidoob9203542018-09-17 22:56:37 -040029
30log = structlog.get_logger()
31
khenaidoob9203542018-09-17 22:56:37 -040032
khenaidoo43c82122018-11-22 18:38:28 -050033def createSubTopic(*args):
34 return '_'.join(args)
35
khenaidoo6fdf0ba2018-11-02 14:38:33 -040036class CoreProxy(ContainerProxy):
khenaidoob9203542018-09-17 22:56:37 -040037
khenaidoo54e0ddf2019-02-27 16:21:33 -050038 def __init__(self, kafka_proxy, default_core_topic, my_listening_topic):
39 super(CoreProxy, self).__init__(kafka_proxy, default_core_topic,
khenaidoo6fdf0ba2018-11-02 14:38:33 -040040 my_listening_topic)
khenaidoo54e0ddf2019-02-27 16:21:33 -050041 self.core_default_topic = default_core_topic
42 self.deviceId_to_core_map = dict()
43
44 def update_device_core_reference(self, device_id, core_topic):
45 log.debug("update_device_core_reference")
46 self.deviceId_to_core_map[device_id] = core_topic
47
48 def delete_device_core_reference(self, device_id, core_topic):
49 log.debug("delete_device_core_reference")
50 del self.deviceId_to_core_map[device_id]
51
52 def get_adapter_topic(self, **kwargs):
53 return self.listening_topic
54
55 def get_core_topic(self, device_id):
56 if device_id in self.deviceId_to_core_map:
57 return self.deviceId_to_core_map[device_id]
58 return self.core_default_topic
khenaidoob9203542018-09-17 22:56:37 -040059
khenaidoo6fdf0ba2018-11-02 14:38:33 -040060 @ContainerProxy.wrap_request(CoreInstance)
khenaidoob9203542018-09-17 22:56:37 -040061 @inlineCallbacks
khenaidoo91ecfd62018-11-04 17:13:42 -050062 def register(self, adapter, deviceTypes):
khenaidoob9203542018-09-17 22:56:37 -040063 log.debug("register")
64 try:
khenaidoo91ecfd62018-11-04 17:13:42 -050065 res = yield self.invoke(rpc="Register",
66 adapter=adapter,
67 deviceTypes=deviceTypes)
khenaidoob9203542018-09-17 22:56:37 -040068 log.info("registration-returned", res=res)
69 returnValue(res)
70 except Exception as e:
71 log.exception("registration-exception", e=e)
72 raise
73
khenaidoo6fdf0ba2018-11-02 14:38:33 -040074 @ContainerProxy.wrap_request(Device)
khenaidoob9203542018-09-17 22:56:37 -040075 @inlineCallbacks
76 def get_device(self, device_id):
77 log.debug("get-device")
78 id = ID()
79 id.id = device_id
khenaidoo43c82122018-11-22 18:38:28 -050080 # Once we have a device being managed, all communications between the
81 # the adapter and the core occurs over a topic associated with that
82 # device
khenaidoo54e0ddf2019-02-27 16:21:33 -050083 to_topic = self.get_core_topic(device_id)
84 reply_topic = self.get_adapter_topic()
85
86 # to_topic = createSubTopic(self.core_topic, device_id)
87 # reply_topic = createSubTopic(self.listening_topic, device_id)
khenaidoo43c82122018-11-22 18:38:28 -050088 res = yield self.invoke(rpc="GetDevice",
89 to_topic=to_topic,
90 reply_topic=reply_topic,
91 device_id=id)
khenaidoob9203542018-09-17 22:56:37 -040092 returnValue(res)
93
khenaidoo6fdf0ba2018-11-02 14:38:33 -040094 @ContainerProxy.wrap_request(Device)
khenaidoob9203542018-09-17 22:56:37 -040095 @inlineCallbacks
96 def get_child_device(self, parent_device_id, **kwargs):
97 raise NotImplementedError()
98
khenaidoo6fdf0ba2018-11-02 14:38:33 -040099 @ContainerProxy.wrap_request(Ports)
khenaidoo92e62c52018-10-03 14:02:54 -0400100 @inlineCallbacks
khenaidoob9203542018-09-17 22:56:37 -0400101 def get_ports(self, device_id, port_type):
khenaidoo92e62c52018-10-03 14:02:54 -0400102 id = ID()
103 id.id = device_id
104 p_type = IntType()
105 p_type.val = port_type
khenaidoo54e0ddf2019-02-27 16:21:33 -0500106 to_topic = self.get_core_topic(device_id)
107 reply_topic = self.get_adapter_topic()
108
109 # to_topic = createSubTopic(self.core_topic, device_id)
110 # reply_topic = createSubTopic(self.listening_topic, device_id)
khenaidoo92e62c52018-10-03 14:02:54 -0400111 res = yield self.invoke(rpc="GetPorts",
khenaidoo43c82122018-11-22 18:38:28 -0500112 to_topic=to_topic,
113 reply_topic=reply_topic,
khenaidoo92e62c52018-10-03 14:02:54 -0400114 device_id=id,
115 port_type=p_type)
116 returnValue(res)
khenaidoob9203542018-09-17 22:56:37 -0400117
118 def get_child_devices(self, parent_device_id):
119 raise NotImplementedError()
120
121 def get_child_device_with_proxy_address(self, proxy_address):
122 raise NotImplementedError()
123
124 def _to_proto(self, **kwargs):
125 encoded = {}
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400126 for k, v in kwargs.iteritems():
khenaidoob9203542018-09-17 22:56:37 -0400127 if isinstance(v, Message):
128 encoded[k] = v
129 elif type(v) == int:
130 i_proto = IntType()
131 i_proto.val = v
132 encoded[k] = i_proto
133 elif type(v) == str:
134 s_proto = StrType()
135 s_proto.val = v
136 encoded[k] = s_proto
137 elif type(v) == bool:
138 b_proto = BoolType()
139 b_proto.val = v
140 encoded[k] = b_proto
141 return encoded
142
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400143 @ContainerProxy.wrap_request(None)
khenaidoob9203542018-09-17 22:56:37 -0400144 @inlineCallbacks
145 def child_device_detected(self,
146 parent_device_id,
147 parent_port_no,
148 child_device_type,
149 channel_id,
150 **kw):
151 id = ID()
152 id.id = parent_device_id
153 ppn = IntType()
154 ppn.val = parent_port_no
155 cdt = StrType()
156 cdt.val = child_device_type
157 channel = IntType()
158 channel.val = channel_id
khenaidoo54e0ddf2019-02-27 16:21:33 -0500159 to_topic = self.get_core_topic(parent_device_id)
160 reply_topic = self.get_adapter_topic()
161
162 # to_topic = createSubTopic(self.core_topic, parent_device_id)
163 # reply_topic = createSubTopic(self.listening_topic, parent_device_id)
khenaidoob9203542018-09-17 22:56:37 -0400164 args = self._to_proto(**kw)
165 res = yield self.invoke(rpc="ChildDeviceDetected",
khenaidoo43c82122018-11-22 18:38:28 -0500166 to_topic=to_topic,
167 reply_topic=reply_topic,
khenaidoob9203542018-09-17 22:56:37 -0400168 parent_device_id=id,
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400169 parent_port_no=ppn,
170 child_device_type=cdt,
khenaidoob9203542018-09-17 22:56:37 -0400171 channel_id=channel,
172 **args)
173 returnValue(res)
174
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400175 @ContainerProxy.wrap_request(None)
khenaidoob9203542018-09-17 22:56:37 -0400176 @inlineCallbacks
177 def device_update(self, device):
178 log.debug("device_update")
khenaidoo54e0ddf2019-02-27 16:21:33 -0500179 to_topic = self.get_core_topic(device.id)
180 reply_topic = self.get_adapter_topic()
181
182 # to_topic = createSubTopic(self.core_topic, device.id)
183 # reply_topic = createSubTopic(self.listening_topic, device.id)
khenaidoo43c82122018-11-22 18:38:28 -0500184 res = yield self.invoke(rpc="DeviceUpdate",
185 to_topic=to_topic,
186 reply_topic=reply_topic,
187 device=device)
khenaidoob9203542018-09-17 22:56:37 -0400188 returnValue(res)
189
190 def child_device_removed(parent_device_id, child_device_id):
191 raise NotImplementedError()
192
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400193 @ContainerProxy.wrap_request(None)
khenaidoob9203542018-09-17 22:56:37 -0400194 @inlineCallbacks
195 def device_state_update(self, device_id,
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400196 oper_status=None,
197 connect_status=None):
khenaidoob9203542018-09-17 22:56:37 -0400198 id = ID()
199 id.id = device_id
200 o_status = IntType()
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400201 if oper_status or oper_status == OperStatus.UNKNOWN:
khenaidoob9203542018-09-17 22:56:37 -0400202 o_status.val = oper_status
203 else:
204 o_status.val = -1
205 c_status = IntType()
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400206 if connect_status or connect_status == ConnectStatus.UNKNOWN:
khenaidoob9203542018-09-17 22:56:37 -0400207 c_status.val = connect_status
208 else:
209 c_status.val = -1
khenaidoob9203542018-09-17 22:56:37 -0400210
khenaidoo54e0ddf2019-02-27 16:21:33 -0500211 to_topic = self.get_core_topic(device_id)
212 reply_topic = self.get_adapter_topic()
213
214 # to_topic = createSubTopic(self.core_topic, device_id)
215 # reply_topic = createSubTopic(self.listening_topic, device_id)
khenaidoob9203542018-09-17 22:56:37 -0400216 res = yield self.invoke(rpc="DeviceStateUpdate",
khenaidoo43c82122018-11-22 18:38:28 -0500217 to_topic=to_topic,
218 reply_topic=reply_topic,
khenaidoob9203542018-09-17 22:56:37 -0400219 device_id=id,
220 oper_status=o_status,
221 connect_status=c_status)
222 returnValue(res)
223
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400224 @ContainerProxy.wrap_request(None)
khenaidoo4d4802d2018-10-04 21:59:49 -0400225 @inlineCallbacks
226 def children_state_update(self, device_id,
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400227 oper_status=None,
228 connect_status=None):
khenaidoo4d4802d2018-10-04 21:59:49 -0400229 id = ID()
230 id.id = device_id
231 o_status = IntType()
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400232 if oper_status or oper_status == OperStatus.UNKNOWN:
khenaidoo4d4802d2018-10-04 21:59:49 -0400233 o_status.val = oper_status
234 else:
235 o_status.val = -1
236 c_status = IntType()
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400237 if connect_status or connect_status == ConnectStatus.UNKNOWN:
khenaidoo4d4802d2018-10-04 21:59:49 -0400238 c_status.val = connect_status
239 else:
240 c_status.val = -1
241
khenaidoo54e0ddf2019-02-27 16:21:33 -0500242 to_topic = self.get_core_topic(device_id)
243 reply_topic = self.get_adapter_topic()
244
245 # to_topic = createSubTopic(self.core_topic, device_id)
246 # reply_topic = createSubTopic(self.listening_topic, device_id)
khenaidoo4d4802d2018-10-04 21:59:49 -0400247 res = yield self.invoke(rpc="ChildrenStateUpdate",
khenaidoo43c82122018-11-22 18:38:28 -0500248 to_topic=to_topic,
249 reply_topic=reply_topic,
khenaidoo4d4802d2018-10-04 21:59:49 -0400250 device_id=id,
251 oper_status=o_status,
252 connect_status=c_status)
253 returnValue(res)
254
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400255 @ContainerProxy.wrap_request(None)
khenaidoob9203542018-09-17 22:56:37 -0400256 @inlineCallbacks
khenaidoo92e62c52018-10-03 14:02:54 -0400257 def port_state_update(self,
258 device_id,
259 port_type,
260 port_no,
261 oper_status):
262 id = ID()
263 id.id = device_id
264 pt = IntType()
265 pt.val = port_type
266 pNo = IntType()
267 pNo.val = port_no
268 o_status = IntType()
269 o_status.val = oper_status
270
khenaidoo54e0ddf2019-02-27 16:21:33 -0500271 to_topic = self.get_core_topic(device_id)
272 reply_topic = self.get_adapter_topic()
273
274 # to_topic = createSubTopic(self.core_topic, device_id)
275 # reply_topic = createSubTopic(self.listening_topic, device_id)
khenaidoo92e62c52018-10-03 14:02:54 -0400276 res = yield self.invoke(rpc="PortStateUpdate",
khenaidoo43c82122018-11-22 18:38:28 -0500277 to_topic=to_topic,
278 reply_topic=reply_topic,
khenaidoo92e62c52018-10-03 14:02:54 -0400279 device_id=id,
280 port_type=pt,
281 port_no=pNo,
282 oper_status=o_status)
283 returnValue(res)
284
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400285 @ContainerProxy.wrap_request(None)
khenaidoo92e62c52018-10-03 14:02:54 -0400286 @inlineCallbacks
khenaidoob9203542018-09-17 22:56:37 -0400287 def child_devices_state_update(self, parent_device_id,
288 oper_status=None,
khenaidoo92e62c52018-10-03 14:02:54 -0400289 connect_status=None):
khenaidoob9203542018-09-17 22:56:37 -0400290
291 id = ID()
292 id.id = parent_device_id
293 o_status = IntType()
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400294 if oper_status or oper_status == OperStatus.UNKNOWN:
khenaidoob9203542018-09-17 22:56:37 -0400295 o_status.val = oper_status
296 else:
297 o_status.val = -1
298 c_status = IntType()
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400299 if connect_status or connect_status == ConnectStatus.UNKNOWN:
khenaidoob9203542018-09-17 22:56:37 -0400300 c_status.val = connect_status
301 else:
302 c_status.val = -1
khenaidoob9203542018-09-17 22:56:37 -0400303
khenaidoo54e0ddf2019-02-27 16:21:33 -0500304 to_topic = self.get_core_topic(parent_device_id)
305 reply_topic = self.get_adapter_topic()
306
307 # to_topic = createSubTopic(self.core_topic, parent_device_id)
308 # reply_topic = createSubTopic(self.listening_topic, parent_device_id)
khenaidoob9203542018-09-17 22:56:37 -0400309 res = yield self.invoke(rpc="child_devices_state_update",
khenaidoo43c82122018-11-22 18:38:28 -0500310 to_topic=to_topic,
311 reply_topic=reply_topic,
khenaidoob9203542018-09-17 22:56:37 -0400312 parent_device_id=id,
313 oper_status=o_status,
khenaidoo92e62c52018-10-03 14:02:54 -0400314 connect_status=c_status)
khenaidoob9203542018-09-17 22:56:37 -0400315 returnValue(res)
316
khenaidoob9203542018-09-17 22:56:37 -0400317 def child_devices_removed(parent_device_id):
318 raise NotImplementedError()
319
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400320 @ContainerProxy.wrap_request(None)
khenaidoob9203542018-09-17 22:56:37 -0400321 @inlineCallbacks
322 def device_pm_config_update(self, device_pm_config, init=False):
323 log.debug("device_pm_config_update")
324 b = BoolType()
325 b.val = init
khenaidoo54e0ddf2019-02-27 16:21:33 -0500326 to_topic = self.get_core_topic(device_pm_config.id)
327 reply_topic = self.get_adapter_topic()
328
329 # to_topic = createSubTopic(self.core_topic, device_pm_config.id)
330 # reply_topic = createSubTopic(self.listening_topic, device_pm_config.id)
khenaidoob9203542018-09-17 22:56:37 -0400331 res = yield self.invoke(rpc="DevicePMConfigUpdate",
khenaidoo43c82122018-11-22 18:38:28 -0500332 to_topic=to_topic,
333 reply_topic=reply_topic,
khenaidoofdbad6e2018-11-06 22:26:38 -0500334 device_pm_config=device_pm_config,
335 init=b)
khenaidoob9203542018-09-17 22:56:37 -0400336 returnValue(res)
337
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400338 @ContainerProxy.wrap_request(None)
khenaidoob9203542018-09-17 22:56:37 -0400339 @inlineCallbacks
340 def port_created(self, device_id, port):
341 log.debug("port_created")
342 proto_id = ID()
343 proto_id.id = device_id
khenaidoo54e0ddf2019-02-27 16:21:33 -0500344 to_topic = self.get_core_topic(device_id)
345 reply_topic = self.get_adapter_topic()
346
347 # to_topic = createSubTopic(self.core_topic, device_id)
348 # reply_topic = createSubTopic(self.listening_topic, device_id)
khenaidoofdbad6e2018-11-06 22:26:38 -0500349 res = yield self.invoke(rpc="PortCreated",
khenaidoo43c82122018-11-22 18:38:28 -0500350 to_topic=to_topic,
351 reply_topic=reply_topic,
khenaidoofdbad6e2018-11-06 22:26:38 -0500352 device_id=proto_id,
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400353 port=port)
khenaidoob9203542018-09-17 22:56:37 -0400354 returnValue(res)
355
khenaidoob9203542018-09-17 22:56:37 -0400356 def port_removed(device_id, port):
357 raise NotImplementedError()
358
359 def ports_enabled(device_id):
360 raise NotImplementedError()
361
362 def ports_disabled(device_id):
363 raise NotImplementedError()
364
365 def ports_oper_status_update(device_id, oper_status):
366 raise NotImplementedError()
367
368 def image_download_update(img_dnld):
369 raise NotImplementedError()
370
371 def image_download_deleted(img_dnld):
372 raise NotImplementedError()
373
khenaidoo7f9bb1a2018-11-29 17:15:01 -0500374 @ContainerProxy.wrap_request(None)
375 @inlineCallbacks
khenaidoofdbad6e2018-11-06 22:26:38 -0500376 def send_packet_in(self, device_id, port, packet):
khenaidoo7f9bb1a2018-11-29 17:15:01 -0500377 log.debug("send_packet_in", device_id=device_id)
khenaidoofdbad6e2018-11-06 22:26:38 -0500378 proto_id = ID()
379 proto_id.id = device_id
380 p = IntType()
381 p.val = port
382 pac = Packet()
383 pac.payload = packet
khenaidoo54e0ddf2019-02-27 16:21:33 -0500384 to_topic = self.get_core_topic(device_id)
385 reply_topic = self.get_adapter_topic()
386 # to_topic = createSubTopic(self.core_topic, device_id)
387 # reply_topic = createSubTopic(self.listening_topic, device_id)
khenaidoofdbad6e2018-11-06 22:26:38 -0500388 res = yield self.invoke(rpc="PacketIn",
khenaidoo43c82122018-11-22 18:38:28 -0500389 to_topic=to_topic,
390 reply_topic=reply_topic,
khenaidoofdbad6e2018-11-06 22:26:38 -0500391 device_id=proto_id,
392 port=p,
393 packet=pac)
394 returnValue(res)