blob: 9ade0d1e9cdf0ccdf55fe6c3830eb21af787df78 [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"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070020 "github.com/opencord/voltha-lib-go/v4/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"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070026 "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"
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 coreTopic string
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070034 endpointMgr kafka.EndpointManager
William Kurkianea869482019-04-09 15:16:11 -040035}
36
Matteo Scandolo46654682020-08-05 11:46:37 -070037func NewAdapterProxy(ctx context.Context, kafkaProxy kafka.InterContainerProxy, coreTopic string, backend *db.Backend) *AdapterProxy {
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070038 proxy := AdapterProxy{
39 kafkaICProxy: kafkaProxy,
Matteo Scandolo3ad5d2b2020-04-02 17:02:04 -070040 coreTopic: coreTopic,
41 endpointMgr: kafka.NewEndpointManager(backend),
42 }
Matteo Scandolo46654682020-08-05 11:46:37 -070043 logger.Debugw(ctx, "topics", log.Fields{"core": proxy.coreTopic})
William Kurkianea869482019-04-09 15:16:11 -040044 return &proxy
45}
46
47func (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 Sharma96b7bf22020-06-15 10:37:32 +000055 logger.Debugw(ctx, "sending-inter-adapter-message", log.Fields{"type": msgType, "from": fromAdapter,
William Kurkianea869482019-04-09 15:16:11 -040056 "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 Sharma96b7bf22020-06-15 10:37:32 +000062 logger.Warnw(ctx, "cannot-marshal-msg", log.Fields{"error": err})
William Kurkianea869482019-04-09 15:16:11 -040063 return err
64 }
65
Matteo Scandolo46654682020-08-05 11:46:37 -070066 // Set up the required rpc arguments
67 endpoint, err := ap.endpointMgr.GetEndpoint(ctx, toDeviceId, toAdapter)
68 if err != nil {
69 return err
70 }
71
William Kurkianea869482019-04-09 15:16:11 -040072 //Build the inter adapter message
73 header := &ic.InterAdapterHeader{
74 Type: msgType,
75 FromTopic: fromAdapter,
Matteo Scandolo46654682020-08-05 11:46:37 -070076 ToTopic: string(endpoint),
William Kurkianea869482019-04-09 15:16:11 -040077 ToDeviceId: toDeviceId,
78 ProxyDeviceId: proxyDeviceId,
79 }
80 if messageId != "" {
81 header.Id = messageId
82 } else {
83 header.Id = uuid.New().String()
84 }
Scott Bakered4a8e72020-04-17 11:10:20 -070085 header.Timestamp = ptypes.TimestampNow()
William Kurkianea869482019-04-09 15:16:11 -040086 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 Scandolo3ad5d2b2020-04-02 17:02:04 -070096 topic := kafka.Topic{Name: string(endpoint)}
Matt Jeanneretcab955f2019-04-10 15:45:57 -040097 replyToTopic := kafka.Topic{Name: fromAdapter}
98 rpc := "process_inter_adapter_message"
William Kurkianea869482019-04-09 15:16:11 -040099
Matteo Scandolo46654682020-08-05 11:46:37 -0700100 // 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)
William Kurkianea869482019-04-09 15:16:11 -0400102 success, result := ap.kafkaICProxy.InvokeRPC(ctx, rpc, &topic, &replyToTopic, true, proxyDeviceId, args...)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000103 logger.Debugw(ctx, "inter-adapter-msg-response", log.Fields{"replyTopic": replyToTopic, "success": success})
104 return unPackResponse(ctx, rpc, "", success, result)
William Kurkianea869482019-04-09 15:16:11 -0400105}