blob: 9ade0d1e9cdf0ccdf55fe6c3830eb21af787df78 [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"
Girish Gowdra89c985b2020-10-14 15:02:09 -070020 "github.com/opencord/voltha-lib-go/v4/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"
Girish Gowdra89c985b2020-10-14 15:02:09 -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"
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 coreTopic string
khenaidoob6238b32020-04-07 12:07:36 -040034 endpointMgr kafka.EndpointManager
Scott Baker2c1c4822019-10-16 11:02:41 -070035}
36
Rohan Agrawal00e5efc2020-07-24 11:43:02 +000037func NewAdapterProxy(ctx context.Context, kafkaProxy kafka.InterContainerProxy, coreTopic string, backend *db.Backend) *AdapterProxy {
khenaidoob6238b32020-04-07 12:07:36 -040038 proxy := AdapterProxy{
39 kafkaICProxy: kafkaProxy,
khenaidoob6238b32020-04-07 12:07:36 -040040 coreTopic: coreTopic,
41 endpointMgr: kafka.NewEndpointManager(backend),
42 }
Rohan Agrawal00e5efc2020-07-24 11:43:02 +000043 logger.Debugw(ctx, "topics", log.Fields{"core": proxy.coreTopic})
Scott Baker2c1c4822019-10-16 11:02:41 -070044 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 Sharma94f16a92020-06-26 04:17:55 +000055 logger.Debugw(ctx, "sending-inter-adapter-message", log.Fields{"type": msgType, "from": fromAdapter,
Scott Baker2c1c4822019-10-16 11:02:41 -070056 "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 Sharma94f16a92020-06-26 04:17:55 +000062 logger.Warnw(ctx, "cannot-marshal-msg", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -070063 return err
64 }
65
Rohan Agrawal00e5efc2020-07-24 11:43:02 +000066 // 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 Baker2c1c4822019-10-16 11:02:41 -070072 //Build the inter adapter message
73 header := &ic.InterAdapterHeader{
74 Type: msgType,
75 FromTopic: fromAdapter,
Rohan Agrawal00e5efc2020-07-24 11:43:02 +000076 ToTopic: string(endpoint),
Scott Baker2c1c4822019-10-16 11:02:41 -070077 ToDeviceId: toDeviceId,
78 ProxyDeviceId: proxyDeviceId,
79 }
80 if messageId != "" {
81 header.Id = messageId
82 } else {
83 header.Id = uuid.New().String()
84 }
Scott Baker84a55ce2020-04-17 10:11:30 -070085 header.Timestamp = ptypes.TimestampNow()
Scott Baker2c1c4822019-10-16 11:02:41 -070086 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
khenaidoob6238b32020-04-07 12:07:36 -040096 topic := kafka.Topic{Name: string(endpoint)}
Scott Baker2c1c4822019-10-16 11:02:41 -070097 replyToTopic := kafka.Topic{Name: fromAdapter}
98 rpc := "process_inter_adapter_message"
99
Girish Kumar74240652020-07-10 11:54:28 +0000100 // 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 Baker2c1c4822019-10-16 11:02:41 -0700102 success, result := ap.kafkaICProxy.InvokeRPC(ctx, rpc, &topic, &replyToTopic, true, proxyDeviceId, args...)
Neha Sharma94f16a92020-06-26 04:17:55 +0000103 logger.Debugw(ctx, "inter-adapter-msg-response", log.Fields{"replyTopic": replyToTopic, "success": success})
104 return unPackResponse(ctx, rpc, "", success, result)
Scott Baker2c1c4822019-10-16 11:02:41 -0700105}