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