blob: ca44d0db5214a3660faea7582f929d283dfe13b6 [file] [log] [blame]
William Kurkianea869482019-04-09 15:16:11 -04001/*
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"
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070020 "github.com/opencord/voltha-lib-go/v3/pkg/db"
Esin Karamanccb714b2019-11-29 15:02:06 +000021
William Kurkianea869482019-04-09 15:16:11 -040022 "github.com/golang/protobuf/proto"
23 "github.com/golang/protobuf/ptypes"
24 "github.com/golang/protobuf/ptypes/any"
25 "github.com/google/uuid"
Esin Karamanccb714b2019-11-29 15:02:06 +000026 "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"
William Kurkianea869482019-04-09 15:16:11 -040029)
30
31type AdapterProxy struct {
npujarec5762e2020-01-01 14:08:48 +053032 kafkaICProxy kafka.InterContainerProxy
William Kurkianea869482019-04-09 15:16:11 -040033 adapterTopic string
34 coreTopic string
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070035 endpointMgr kafka.EndpointManager
William Kurkianea869482019-04-09 15:16:11 -040036}
37
Neha Sharma96b7bf22020-06-15 10:37:32 +000038func NewAdapterProxy(ctx context.Context, kafkaProxy kafka.InterContainerProxy, adapterTopic string, coreTopic string, backend *db.Backend) *AdapterProxy {
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070039 proxy := AdapterProxy{
40 kafkaICProxy: kafkaProxy,
41 adapterTopic: adapterTopic,
42 coreTopic: coreTopic,
43 endpointMgr: kafka.NewEndpointManager(backend),
44 }
Neha Sharma96b7bf22020-06-15 10:37:32 +000045 logger.Debugw(ctx, "topics", log.Fields{"core": proxy.coreTopic, "adapter": proxy.adapterTopic})
William Kurkianea869482019-04-09 15:16:11 -040046 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 {
Neha Sharma96b7bf22020-06-15 10:37:32 +000057 logger.Debugw(ctx, "sending-inter-adapter-message", log.Fields{"type": msgType, "from": fromAdapter,
William Kurkianea869482019-04-09 15:16:11 -040058 "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 {
Neha Sharma96b7bf22020-06-15 10:37:32 +000064 logger.Warnw(ctx, "cannot-marshal-msg", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -040065 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 Bakered4a8e72020-04-17 11:10:20 -070081 header.Timestamp = ptypes.TimestampNow()
William Kurkianea869482019-04-09 15:16:11 -040082 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
Neha Sharma96b7bf22020-06-15 10:37:32 +000093 endpoint, err := ap.endpointMgr.GetEndpoint(ctx, toDeviceId, toAdapter)
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070094 if err != nil {
95 return err
96 }
97 topic := kafka.Topic{Name: string(endpoint)}
Matt Jeanneretcab955f2019-04-10 15:45:57 -040098 replyToTopic := kafka.Topic{Name: fromAdapter}
99 rpc := "process_inter_adapter_message"
William Kurkianea869482019-04-09 15:16:11 -0400100
101 success, result := ap.kafkaICProxy.InvokeRPC(ctx, rpc, &topic, &replyToTopic, true, proxyDeviceId, args...)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000102 logger.Debugw(ctx, "inter-adapter-msg-response", log.Fields{"replyTopic": replyToTopic, "success": success})
103 return unPackResponse(ctx, rpc, "", success, result)
William Kurkianea869482019-04-09 15:16:11 -0400104}