def child_device_detected(self, parent_device_id, parent_port_no, child_device_type, channel_id, **kw): id = ID() id.id = parent_device_id ppn = IntType() ppn.val = parent_port_no cdt = StrType() cdt.val = child_device_type channel = IntType() channel.val = channel_id to_topic = self.get_core_topic(parent_device_id) reply_topic = self.get_adapter_topic() # to_topic = createSubTopic(self.core_topic, parent_device_id) # reply_topic = createSubTopic(self.listening_topic, parent_device_id) args = self._to_proto(**kw) res = yield self.invoke(rpc="ChildDeviceDetected", to_topic=to_topic, reply_topic=reply_topic, parent_device_id=id, parent_port_no=ppn, child_device_type=cdt, channel_id=channel, **args) returnValue(res)
def device_reason_update(self, device_id, reason): dev_id = ID() dev_id.id = device_id rsn = StrType() rsn.val = reason to_topic = self.get_core_topic(device_id) reply_topic = self.get_adapter_topic() res = yield self.invoke(rpc="DeviceReasonUpdate", to_topic=to_topic, reply_topic=reply_topic, device_id=dev_id, device_reason=rsn) returnValue(res)
def adopt_device(self, device, **kwargs): d = Device() if device: device.Unpack(d) # Update the core reference for that device as it will be used # by the adapter to send async messages to the Core. if ARG_FROM_TOPIC in kwargs: t = StrType() kwargs[ARG_FROM_TOPIC].Unpack(t) # Update the core reference for that device self.core_proxy.update_device_core_reference(d.id, t.val) # # Start the creation of a device specific topic to handle all # # subsequent requests from the Core. This adapter instance will # # handle all requests for that device. # reactor.callLater(0, self.createKafkaDeviceTopic, d.id) result = self.adapter.adopt_device(d) # return True, self.adapter.adopt_device(d) return True, result else: return False, Error(code=ErrorCode.INVALID_PARAMETERS, reason="device-invalid")
def receive_packet_out(self, deviceId, outPort, packet, **kwargs): try: d_id = StrType() if deviceId: deviceId.Unpack(d_id) else: return False, Error(code=ErrorCode.INVALID_PARAMETERS, reason="deviceid-invalid") op = IntType() if outPort: outPort.Unpack(op) else: return False, Error(code=ErrorCode.INVALID_PARAMETERS, reason="outport-invalid") p = ofp_packet_out() if packet: packet.Unpack(p) else: return False, Error(code=ErrorCode.INVALID_PARAMETERS, reason="packet-invalid") return (True, self.adapter.receive_packet_out(d_id.val, op.val, p)) except Exception as e: log.exception("error-processing-receive_packet_out", e=e)
def receive_packet_out(self, deviceId, outPort, packet, **_kwargs): # pylint: disable=invalid-name try: d_id = StrType() if deviceId: deviceId.Unpack(d_id) else: return False, Error(code=ErrorCode.INVALID_PARAMETERS, reason="device-id-invalid") port_number = IntType() if outPort: outPort.Unpack(port_number) else: return False, Error(code=ErrorCode.INVALID_PARAMETERS, reason="outport-invalid") pkt = ofp_packet_out() if packet: packet.Unpack(pkt) else: return False, Error(code=ErrorCode.INVALID_PARAMETERS, reason="packet-invalid") return True, self.adapter.receive_packet_out( d_id.val, port_number.val, pkt) except Exception as e: log.exception("error-processing-receive_packet_out", e=e) return False, Error( code=ErrorCode.INVALID_PARAMETERS, reason="exception-during-processing: {}".format(str(e)))
def _to_proto(self, **kwargs): encoded = {} for k, v in kwargs.iteritems(): if isinstance(v, Message): encoded[k] = v elif type(v) == int: i_proto = IntType() i_proto.val = v encoded[k] = i_proto elif type(v) == str: s_proto = StrType() s_proto.val = v encoded[k] = s_proto elif type(v) == bool: b_proto = BoolType() b_proto.val = v encoded[k] = b_proto return encoded
def embed_span_as_arg(rpc, is_async, msg=None): """ Method to extract Open-tracing Span from Context and serialize it for transport over Kafka embedded as a additional argument. Additional argument is injected using key as "span" and value as Span marshalled into a byte slice The span name is automatically constructed using the RPC name with following convention (<rpc-name> represents name of invoked method): - RPC invoked in Sync manner (WaitForResponse=true) : kafka-rpc-<rpc-name> - RPC invoked in Async manner (WaitForResponse=false) : kafka-async-rpc-<rpc-name> - Inter Adapter RPC invoked in Sync manner (WaitForResponse=true) : kafka-inter-adapter-rpc-<rpc-name> - Inter Adapter RPC invoked in Async manner (WaitForResponse=false) : kafka-inter-adapter-async-rpc-<rpc-name> """ tracer = global_tracer() if tracer is None: return None, None try: span_name = 'kafka-' # In case of inter adapter message, use Msg Type for constructing RPC name if rpc == PROCESS_IA_MSG_RPC: span_name += 'inter-adapter-' msg_type = msg.header.type try: rpc = IA_MSG_ENUM.values_by_number[msg_type].name except Exception as _e: rpc = 'msg-type-{}'.format(msg_type) span_name += 'async-rpc-' if is_async else 'rpc-' span_name += rpc if is_async: span_to_inject = create_async_span(span_name) else: span_to_inject = create_child_span(span_name) if span_to_inject is None: return None, None span_to_inject.set_baggage_item('rpc-span-name', span_name) text_map = dict() tracer.inject(span_to_inject, Format.TEXT_MAP, text_map) text_map_json = json.dumps(text_map, indent=None, separators=(',', ':')) span_arg = StrType(val=text_map_json) return span_arg, span_to_inject except Exception as _e: return None, None
def _to_proto(**kwargs): encoded = {} for k, val in kwargs.items(): if isinstance(val, Message): encoded[k] = val elif isinstance(val, int): i_proto = IntType() i_proto.val = val encoded[k] = i_proto elif isinstance(val, str): s_proto = StrType() s_proto.val = val encoded[k] = s_proto elif isinstance(val, bool): b_proto = BoolType() b_proto.val = val encoded[k] = b_proto else: raise TypeError('Unsupported type: {} for key {}'.format( type(val), k)) return encoded
def disable_port(self, deviceId, port, **_kwargs): # pylint: disable=invalid-name if not deviceId: return False, Error(code=ErrorCode.INVALID_PARAMETER, reason="device-id") if not port: return False, Error(code=ErrorCode.INVALID_PARAMETER, reason="port-invalid") str_arg = StrType() deviceId.Unpack(str_arg) port_arg = Port() port.Unpack(port_arg) return True, self.adapter.disable_port(str_arg.val, port_arg.port_no)
def enrich_context_with_span(rpc_name, args): """ Method to extract the Span embedded in Kafka RPC request on the receiver side. If span is found embedded in the KV args (with key as "span"), it is de-serialized and injected into the Context to be carried forward by the RPC request processor thread. If no span is found embedded, even then a span is created with name as "kafka-rpc-<rpc-name>" to enrich the Context for RPC calls coming from components currently not sending the span (e.g. openonu adapter) """ tracer = global_tracer() try: for arg in args: if arg.key == SPAN_ARG and arg.value is not None: text_map_string = StrType() arg.value.Unpack(text_map_string) span_dict = json.loads(text_map_string.val) span_ctx = tracer.extract(Format.TEXT_MAP, span_dict) if span_ctx is not None: rx_rpc_name = span_ctx.baggage.get('rpc-span-name') return tracer.start_active_span(rx_rpc_name, child_of=span_ctx, finish_on_close=False) except Exception as e: log.info('exception-during-context-decode', err=str(e)) # If here, no active span found in request, start a new span instead span_name = 'kafka-' if rpc_name == PROCESS_IA_MSG_RPC: for arg in args: if arg.key == MESSAGE_KEY: ia_msg = InterAdapterMessage() arg.value.Unpack(ia_msg) span_name += 'inter-adapter-' msg_type = ia_msg.header.type try: rpc_name = IA_MSG_ENUM.values_by_number[msg_type].name except Exception as _e: rpc_name = 'msg-type-{}'.format(msg_type) break span_name += 'rpc-' + rpc_name return tracer.start_active_span(span_name, ignore_active_span=True, finish_on_close=False)
def process_inter_adapter_message(self, msg, **kwargs): ia_msg = InterAdapterMessage() if msg: msg.Unpack(ia_msg) else: return False, Error(code=ErrorCode.INVALID_PARAMETERS, reason="msg-invalid") from_topic = StrType() topic = kwargs.get('fromTopic') if topic: topic.Unpack(from_topic) log.debug('rx-message', message=ia_msg, from_topic=from_topic) return True, self.adapter.process_inter_adapter_message( ia_msg, from_topic=from_topic.val)
def get_ext_value(self, deviceId, device, value_flag, **_kwargs): # pylint: disable=invalid-name if deviceId: str_arg = StrType() deviceId.Unpack(str_arg) else: return False, Error(code=ErrorCode.INVALID_PARAMETER, reason="device-id") if device: dev = Device() device.Unpack(dev) else: return False, Error(code=ErrorCode.INVALID_PARAMETERS, reason="device-invalid") if value_flag: value = ValueSpecifier() value_flag.Unpack(value) else: return False, Error(code=ErrorCode.INVALID_PARAMETERS, reason="value-flag-invalid") return True, self.adapter.get_ext_value(str_arg.val, dev, value)
def child_device_lost(self, pDeviceId, pPortNo, onuID, **_kwargs): # pylint: disable=invalid-name if not pDeviceId: return False, Error(code=ErrorCode.INVALID_PARAMETER, reason="device-id") if not pPortNo: return False, Error(code=ErrorCode.INVALID_PARAMETER, reason="p-port-no") if not onuID: return False, Error(code=ErrorCode.INVALID_PARAMETER, reason="onu-id") str_arg = StrType() pDeviceId.Unpack(str_arg) port_no = IntType() pPortNo.Unpack(port_no) oid = IntType() onuID.Unpack(oid) return True, self.adapter.child_device_lost(str_arg.val, port_no.val, oid.val)
def _augment_args_with_from_topic(args, from_topic): arg = Argument(key=ARG_FROM_TOPIC) arg.value.Pack(StrType(val=from_topic)) args.extend([arg]) return args