blob: fc310412f2fda47dc756cebecb5cf957bf4b26ab [file] [log] [blame]
yasin sapli5458a1c2021-06-14 22:24:38 +00001/*
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"
20 "github.com/opencord/voltha-lib-go/v5/pkg/db"
21 "google.golang.org/grpc/status"
22
23 "github.com/golang/protobuf/proto"
24 "github.com/golang/protobuf/ptypes"
25 "github.com/golang/protobuf/ptypes/any"
26 "github.com/google/uuid"
27 "github.com/opencord/voltha-lib-go/v5/pkg/kafka"
28 "github.com/opencord/voltha-lib-go/v5/pkg/log"
29 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
30)
31
32type AdapterProxy struct {
33 kafkaICProxy kafka.InterContainerProxy
34 coreTopic string
35 endpointMgr kafka.EndpointManager
36}
37
38func NewAdapterProxy(ctx context.Context, kafkaProxy kafka.InterContainerProxy, coreTopic string, backend *db.Backend) *AdapterProxy {
39 proxy := AdapterProxy{
40 kafkaICProxy: kafkaProxy,
41 coreTopic: coreTopic,
42 endpointMgr: kafka.NewEndpointManager(backend),
43 }
44 logger.Debugw(ctx, "topics", log.Fields{"core": proxy.coreTopic})
45 return &proxy
46}
47
48func (ap *AdapterProxy) SendInterAdapterMessage(ctx context.Context,
49 msg proto.Message,
50 msgType ic.InterAdapterMessageType_Types,
51 fromAdapter string,
52 toAdapter string,
53 toDeviceId string,
54 proxyDeviceId string,
55 messageId string) error {
56 logger.Debugw(ctx, "sending-inter-adapter-message", log.Fields{"type": msgType, "from": fromAdapter,
57 "to": toAdapter, "toDevice": toDeviceId, "proxyDevice": proxyDeviceId})
58
59 //Marshal the message
60 var marshalledMsg *any.Any
61 var err error
62 if marshalledMsg, err = ptypes.MarshalAny(msg); err != nil {
63 logger.Warnw(ctx, "cannot-marshal-msg", log.Fields{"error": err})
64 return err
65 }
66
67 // Set up the required rpc arguments
68 endpoint, err := ap.endpointMgr.GetEndpoint(ctx, toDeviceId, toAdapter)
69 if err != nil {
70 return err
71 }
72
73 //Build the inter adapter message
74 header := &ic.InterAdapterHeader{
75 Type: msgType,
76 FromTopic: fromAdapter,
77 ToTopic: string(endpoint),
78 ToDeviceId: toDeviceId,
79 ProxyDeviceId: proxyDeviceId,
80 }
81 if messageId != "" {
82 header.Id = messageId
83 } else {
84 header.Id = uuid.New().String()
85 }
86 header.Timestamp = ptypes.TimestampNow()
87 iaMsg := &ic.InterAdapterMessage{
88 Header: header,
89 Body: marshalledMsg,
90 }
91 args := make([]*kafka.KVArg, 1)
92 args[0] = &kafka.KVArg{
93 Key: "msg",
94 Value: iaMsg,
95 }
96
97 topic := kafka.Topic{Name: string(endpoint)}
98 replyToTopic := kafka.Topic{Name: fromAdapter}
99 rpc := "process_inter_adapter_message"
100
101 // Add a indication in context to differentiate this Inter Adapter message during Span processing in Kafka IC proxy
102 ctx = context.WithValue(ctx, "inter-adapter-msg-type", msgType)
103 success, result := ap.kafkaICProxy.InvokeRPC(ctx, rpc, &topic, &replyToTopic, true, proxyDeviceId, args...)
104 logger.Debugw(ctx, "inter-adapter-msg-response", log.Fields{"replyTopic": replyToTopic, "success": success})
105 return unPackResponse(ctx, rpc, "", success, result)
106}
107
108func (ap *AdapterProxy) TechProfileInstanceRequest(ctx context.Context,
109 tpPath string,
110 parentPonPort uint32,
111 onuID uint32,
112 uniID uint32,
113 fromAdapter string,
114 toAdapter string,
115 toDeviceId string,
116 proxyDeviceId string) (*ic.InterAdapterTechProfileDownloadMessage, error) {
117 logger.Debugw(ctx, "sending-tech-profile-instance-request-message", log.Fields{"from": fromAdapter,
118 "to": toAdapter, "toDevice": toDeviceId, "proxyDevice": proxyDeviceId})
119
120 // Set up the required rpc arguments
121 endpoint, err := ap.endpointMgr.GetEndpoint(ctx, toDeviceId, toAdapter)
122 if err != nil {
123 return nil, err
124 }
125
126 //Build the inter adapter message
127 tpReqMsg := &ic.InterAdapterTechProfileInstanceRequestMessage{
128 TpInstancePath: tpPath,
129 ParentDeviceId: toDeviceId,
130 ParentPonPort: parentPonPort,
131 OnuId: onuID,
132 UniId: uniID,
133 }
134
135 args := make([]*kafka.KVArg, 1)
136 args[0] = &kafka.KVArg{
137 Key: "msg",
138 Value: tpReqMsg,
139 }
140
141 topic := kafka.Topic{Name: string(endpoint)}
142 replyToTopic := kafka.Topic{Name: fromAdapter}
143 rpc := "process_tech_profile_instance_request"
144
145 ctx = context.WithValue(ctx, "inter-adapter-tp-req-msg", tpPath)
146 success, result := ap.kafkaICProxy.InvokeRPC(ctx, rpc, &topic, &replyToTopic, true, proxyDeviceId, args...)
147 logger.Debugw(ctx, "inter-adapter-msg-response", log.Fields{"replyTopic": replyToTopic, "success": success})
148 if success {
149 tpDwnldMsg := &ic.InterAdapterTechProfileDownloadMessage{}
150 if err := ptypes.UnmarshalAny(result, tpDwnldMsg); err != nil {
151 logger.Warnw(ctx, "cannot-unmarshal-response", log.Fields{"error": err})
152 return nil, err
153 }
154 return tpDwnldMsg, nil
155 } else {
156 unpackResult := &ic.Error{}
157 var err error
158 if err = ptypes.UnmarshalAny(result, unpackResult); err != nil {
159 logger.Warnw(ctx, "cannot-unmarshal-response", log.Fields{"error": err})
160 }
161 logger.Debugw(ctx, "TechProfileInstanceRequest-return", log.Fields{"tpPath": tpPath, "success": success, "error": err})
162
163 return nil, status.Error(ICProxyErrorCodeToGrpcErrorCode(ctx, unpackResult.Code), unpackResult.Reason)
164 }
165}