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" |
Girish Gowdra | 89c985b | 2020-10-14 15:02:09 -0700 | [diff] [blame] | 20 | "github.com/opencord/voltha-lib-go/v4/pkg/db" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 21 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [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" |
Girish Gowdra | 89c985b | 2020-10-14 15:02:09 -0700 | [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" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | type AdapterProxy struct { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 32 | kafkaICProxy kafka.InterContainerProxy |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 33 | coreTopic string |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 34 | endpointMgr kafka.EndpointManager |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Rohan Agrawal | 00e5efc | 2020-07-24 11:43:02 +0000 | [diff] [blame] | 37 | func NewAdapterProxy(ctx context.Context, kafkaProxy kafka.InterContainerProxy, coreTopic string, backend *db.Backend) *AdapterProxy { |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 38 | proxy := AdapterProxy{ |
| 39 | kafkaICProxy: kafkaProxy, |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 40 | coreTopic: coreTopic, |
| 41 | endpointMgr: kafka.NewEndpointManager(backend), |
| 42 | } |
Rohan Agrawal | 00e5efc | 2020-07-24 11:43:02 +0000 | [diff] [blame] | 43 | logger.Debugw(ctx, "topics", log.Fields{"core": proxy.coreTopic}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 55 | logger.Debugw(ctx, "sending-inter-adapter-message", log.Fields{"type": msgType, "from": fromAdapter, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 62 | logger.Warnw(ctx, "cannot-marshal-msg", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 63 | return err |
| 64 | } |
| 65 | |
Rohan Agrawal | 00e5efc | 2020-07-24 11:43:02 +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 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 72 | //Build the inter adapter message |
| 73 | header := &ic.InterAdapterHeader{ |
| 74 | Type: msgType, |
| 75 | FromTopic: fromAdapter, |
Rohan Agrawal | 00e5efc | 2020-07-24 11:43:02 +0000 | [diff] [blame] | 76 | ToTopic: string(endpoint), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [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 | 84a55ce | 2020-04-17 10:11:30 -0700 | [diff] [blame] | 85 | header.Timestamp = ptypes.TimestampNow() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [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 | |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 96 | topic := kafka.Topic{Name: string(endpoint)} |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 97 | replyToTopic := kafka.Topic{Name: fromAdapter} |
| 98 | rpc := "process_inter_adapter_message" |
| 99 | |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +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) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 102 | success, result := ap.kafkaICProxy.InvokeRPC(ctx, rpc, &topic, &replyToTopic, true, proxyDeviceId, args...) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 103 | logger.Debugw(ctx, "inter-adapter-msg-response", log.Fields{"replyTopic": replyToTopic, "success": success}) |
| 104 | return unPackResponse(ctx, rpc, "", success, result) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 105 | } |