khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [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" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame^] | 20 | "github.com/opencord/voltha-lib-go/v4/pkg/db" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 21 | |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 22 | "github.com/golang/protobuf/proto" |
| 23 | "github.com/golang/protobuf/ptypes" |
| 24 | "github.com/golang/protobuf/ptypes/any" |
| 25 | "github.com/google/uuid" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame^] | 26 | "github.com/opencord/voltha-lib-go/v4/pkg/kafka" |
| 27 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 28 | ic "github.com/opencord/voltha-protos/v4/go/inter_container" |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | type AdapterProxy struct { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 32 | kafkaICProxy kafka.InterContainerProxy |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 33 | coreTopic string |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 34 | endpointMgr kafka.EndpointManager |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 35 | } |
| 36 | |
Girish Kumar | def46fc | 2020-08-05 18:20:11 +0000 | [diff] [blame] | 37 | func NewAdapterProxy(ctx context.Context, kafkaProxy kafka.InterContainerProxy, coreTopic string, backend *db.Backend) *AdapterProxy { |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 38 | proxy := AdapterProxy{ |
| 39 | kafkaICProxy: kafkaProxy, |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 40 | coreTopic: coreTopic, |
| 41 | endpointMgr: kafka.NewEndpointManager(backend), |
| 42 | } |
Girish Kumar | def46fc | 2020-08-05 18:20:11 +0000 | [diff] [blame] | 43 | logger.Debugw(ctx, "topics", log.Fields{"core": proxy.coreTopic}) |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 44 | return &proxy |
| 45 | } |
| 46 | |
| 47 | func (ap *AdapterProxy) SendInterAdapterMessage(ctx context.Context, |
| 48 | msg proto.Message, |
| 49 | msgType ic.InterAdapterMessageType_Types, |
| 50 | fromAdapter string, |
| 51 | toAdapter string, |
| 52 | toDeviceId string, |
| 53 | proxyDeviceId string, |
| 54 | messageId string) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 55 | logger.Debugw(ctx, "sending-inter-adapter-message", log.Fields{"type": msgType, "from": fromAdapter, |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 56 | "to": toAdapter, "toDevice": toDeviceId, "proxyDevice": proxyDeviceId}) |
| 57 | |
| 58 | //Marshal the message |
| 59 | var marshalledMsg *any.Any |
| 60 | var err error |
| 61 | if marshalledMsg, err = ptypes.MarshalAny(msg); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 62 | logger.Warnw(ctx, "cannot-marshal-msg", log.Fields{"error": err}) |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 63 | return err |
| 64 | } |
| 65 | |
Girish Kumar | def46fc | 2020-08-05 18:20:11 +0000 | [diff] [blame] | 66 | // Set up the required rpc arguments |
| 67 | endpoint, err := ap.endpointMgr.GetEndpoint(ctx, toDeviceId, toAdapter) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 72 | //Build the inter adapter message |
| 73 | header := &ic.InterAdapterHeader{ |
| 74 | Type: msgType, |
| 75 | FromTopic: fromAdapter, |
Girish Kumar | def46fc | 2020-08-05 18:20:11 +0000 | [diff] [blame] | 76 | ToTopic: string(endpoint), |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 77 | ToDeviceId: toDeviceId, |
| 78 | ProxyDeviceId: proxyDeviceId, |
| 79 | } |
| 80 | if messageId != "" { |
| 81 | header.Id = messageId |
| 82 | } else { |
| 83 | header.Id = uuid.New().String() |
| 84 | } |
Scott Baker | 504b480 | 2020-04-17 10:12:20 -0700 | [diff] [blame] | 85 | header.Timestamp = ptypes.TimestampNow() |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 86 | iaMsg := &ic.InterAdapterMessage{ |
| 87 | Header: header, |
| 88 | Body: marshalledMsg, |
| 89 | } |
| 90 | args := make([]*kafka.KVArg, 1) |
| 91 | args[0] = &kafka.KVArg{ |
| 92 | Key: "msg", |
| 93 | Value: iaMsg, |
| 94 | } |
| 95 | |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 96 | topic := kafka.Topic{Name: string(endpoint)} |
cuilin2018 | 6b6a995 | 2019-04-03 22:37:11 -0700 | [diff] [blame] | 97 | replyToTopic := kafka.Topic{Name: fromAdapter} |
| 98 | rpc := "process_inter_adapter_message" |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 99 | |
Girish Kumar | def46fc | 2020-08-05 18:20:11 +0000 | [diff] [blame] | 100 | // Add a indication in context to differentiate this Inter Adapter message during Span processing in Kafka IC proxy |
| 101 | ctx = context.WithValue(ctx, "inter-adapter-msg-type", msgType) |
khenaidoo | bdcb8e0 | 2019-03-06 16:28:56 -0500 | [diff] [blame] | 102 | success, result := ap.kafkaICProxy.InvokeRPC(ctx, rpc, &topic, &replyToTopic, true, proxyDeviceId, args...) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 103 | logger.Debugw(ctx, "inter-adapter-msg-response", log.Fields{"replyTopic": replyToTopic, "success": success}) |
| 104 | return unPackResponse(ctx, rpc, "", success, result) |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 105 | } |