Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | package common |
| 17 | |
| 18 | import ( |
| 19 | "context" |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 20 | "github.com/opencord/voltha-lib-go/v3/pkg/db" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 21 | "time" |
| 22 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 23 | "github.com/golang/protobuf/proto" |
| 24 | "github.com/golang/protobuf/ptypes" |
| 25 | "github.com/golang/protobuf/ptypes/any" |
| 26 | "github.com/google/uuid" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 27 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 28 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 29 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | type AdapterProxy struct { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 33 | kafkaICProxy kafka.InterContainerProxy |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 34 | adapterTopic string |
| 35 | coreTopic string |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 36 | endpointMgr kafka.EndpointManager |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 37 | } |
| 38 | |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 39 | func NewAdapterProxy(kafkaProxy kafka.InterContainerProxy, adapterTopic string, coreTopic string, backend *db.Backend) *AdapterProxy { |
| 40 | proxy := AdapterProxy{ |
| 41 | kafkaICProxy: kafkaProxy, |
| 42 | adapterTopic: adapterTopic, |
| 43 | coreTopic: coreTopic, |
| 44 | endpointMgr: kafka.NewEndpointManager(backend), |
| 45 | } |
| 46 | logger.Debugw("topics", log.Fields{"core": proxy.coreTopic, "adapter": proxy.adapterTopic}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 47 | return &proxy |
| 48 | } |
| 49 | |
| 50 | func (ap *AdapterProxy) SendInterAdapterMessage(ctx context.Context, |
| 51 | msg proto.Message, |
| 52 | msgType ic.InterAdapterMessageType_Types, |
| 53 | fromAdapter string, |
| 54 | toAdapter string, |
| 55 | toDeviceId string, |
| 56 | proxyDeviceId string, |
| 57 | messageId string) error { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 58 | logger.Debugw("sending-inter-adapter-message", log.Fields{"type": msgType, "from": fromAdapter, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 59 | "to": toAdapter, "toDevice": toDeviceId, "proxyDevice": proxyDeviceId}) |
| 60 | |
| 61 | //Marshal the message |
| 62 | var marshalledMsg *any.Any |
| 63 | var err error |
| 64 | if marshalledMsg, err = ptypes.MarshalAny(msg); err != nil { |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 65 | logger.Warnw("cannot-marshal-msg", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 66 | return err |
| 67 | } |
| 68 | |
| 69 | //Build the inter adapter message |
| 70 | header := &ic.InterAdapterHeader{ |
| 71 | Type: msgType, |
| 72 | FromTopic: fromAdapter, |
| 73 | ToTopic: toAdapter, |
| 74 | ToDeviceId: toDeviceId, |
| 75 | ProxyDeviceId: proxyDeviceId, |
| 76 | } |
| 77 | if messageId != "" { |
| 78 | header.Id = messageId |
| 79 | } else { |
| 80 | header.Id = uuid.New().String() |
| 81 | } |
| 82 | header.Timestamp = time.Now().Unix() |
| 83 | iaMsg := &ic.InterAdapterMessage{ |
| 84 | Header: header, |
| 85 | Body: marshalledMsg, |
| 86 | } |
| 87 | args := make([]*kafka.KVArg, 1) |
| 88 | args[0] = &kafka.KVArg{ |
| 89 | Key: "msg", |
| 90 | Value: iaMsg, |
| 91 | } |
| 92 | |
| 93 | // Set up the required rpc arguments |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 94 | endpoint, err := ap.endpointMgr.GetEndpoint(toDeviceId, toAdapter) |
| 95 | if err != nil { |
| 96 | return err |
| 97 | } |
| 98 | topic := kafka.Topic{Name: string(endpoint)} |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 99 | replyToTopic := kafka.Topic{Name: fromAdapter} |
| 100 | rpc := "process_inter_adapter_message" |
| 101 | |
| 102 | success, result := ap.kafkaICProxy.InvokeRPC(ctx, rpc, &topic, &replyToTopic, true, proxyDeviceId, args...) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 103 | logger.Debugw("inter-adapter-msg-response", log.Fields{"replyTopic": replyToTopic, "success": success}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 104 | return unPackResponse(rpc, "", success, result) |
| 105 | } |