blob: cd5750fe2009d6fb53b5184dc93fd642b305f49f [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
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 */
16package common
17
18import (
19 "context"
khenaidoob6238b32020-04-07 12:07:36 -040020 "github.com/opencord/voltha-lib-go/v3/pkg/db"
serkant.uluderyab38671c2019-11-01 09:35:38 -070021
Scott Baker2c1c4822019-10-16 11:02:41 -070022 "github.com/golang/protobuf/proto"
23 "github.com/golang/protobuf/ptypes"
24 "github.com/golang/protobuf/ptypes/any"
25 "github.com/google/uuid"
serkant.uluderyab38671c2019-11-01 09:35:38 -070026 "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 Baker2c1c4822019-10-16 11:02:41 -070029)
30
31type AdapterProxy struct {
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080032 kafkaICProxy kafka.InterContainerProxy
Scott Baker2c1c4822019-10-16 11:02:41 -070033 adapterTopic string
34 coreTopic string
khenaidoob6238b32020-04-07 12:07:36 -040035 endpointMgr kafka.EndpointManager
Scott Baker2c1c4822019-10-16 11:02:41 -070036}
37
Scott Bakere6685952020-06-23 04:05:39 +000038func NewAdapterProxy(kafkaProxy kafka.InterContainerProxy, adapterTopic string, coreTopic string, backend *db.Backend) *AdapterProxy {
khenaidoob6238b32020-04-07 12:07:36 -040039 proxy := AdapterProxy{
40 kafkaICProxy: kafkaProxy,
41 adapterTopic: adapterTopic,
42 coreTopic: coreTopic,
43 endpointMgr: kafka.NewEndpointManager(backend),
44 }
Scott Bakere6685952020-06-23 04:05:39 +000045 logger.Debugw("topics", log.Fields{"core": proxy.coreTopic, "adapter": proxy.adapterTopic})
Scott Baker2c1c4822019-10-16 11:02:41 -070046 return &proxy
47}
48
49func (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 Bakere6685952020-06-23 04:05:39 +000057 logger.Debugw("sending-inter-adapter-message", log.Fields{"type": msgType, "from": fromAdapter,
Scott Baker2c1c4822019-10-16 11:02:41 -070058 "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 Bakere6685952020-06-23 04:05:39 +000064 logger.Warnw("cannot-marshal-msg", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -070065 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 Baker84a55ce2020-04-17 10:11:30 -070081 header.Timestamp = ptypes.TimestampNow()
Scott Baker2c1c4822019-10-16 11:02:41 -070082 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 Bakere6685952020-06-23 04:05:39 +000093 endpoint, err := ap.endpointMgr.GetEndpoint(toDeviceId, toAdapter)
khenaidoob6238b32020-04-07 12:07:36 -040094 if err != nil {
95 return err
96 }
97 topic := kafka.Topic{Name: string(endpoint)}
Scott Baker2c1c4822019-10-16 11:02:41 -070098 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 Bakere6685952020-06-23 04:05:39 +0000102 logger.Debugw("inter-adapter-msg-response", log.Fields{"replyTopic": replyToTopic, "success": success})
103 return unPackResponse(rpc, "", success, result)
Scott Baker2c1c4822019-10-16 11:02:41 -0700104}