Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package common |
| 17 | |
| 18 | import ( |
| 19 | "context" |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 20 | "github.com/opencord/voltha-lib-go/v5/pkg/db" |
| 21 | "google.golang.org/grpc/status" |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 22 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 23 | "github.com/golang/protobuf/proto" |
| 24 | "github.com/golang/protobuf/ptypes" |
| 25 | "github.com/golang/protobuf/ptypes/any" |
| 26 | "github.com/google/uuid" |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 27 | "github.com/opencord/voltha-lib-go/v5/pkg/kafka" |
| 28 | "github.com/opencord/voltha-lib-go/v5/pkg/log" |
Girish Gowdra | 89c985b | 2020-10-14 15:02:09 -0700 | [diff] [blame] | 29 | ic "github.com/opencord/voltha-protos/v4/go/inter_container" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | type AdapterProxy struct { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 33 | kafkaICProxy kafka.InterContainerProxy |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 34 | coreTopic string |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 35 | endpointMgr kafka.EndpointManager |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Rohan Agrawal | 00e5efc | 2020-07-24 11:43:02 +0000 | [diff] [blame] | 38 | func NewAdapterProxy(ctx context.Context, kafkaProxy kafka.InterContainerProxy, coreTopic string, backend *db.Backend) *AdapterProxy { |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 39 | proxy := AdapterProxy{ |
| 40 | kafkaICProxy: kafkaProxy, |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 41 | coreTopic: coreTopic, |
| 42 | endpointMgr: kafka.NewEndpointManager(backend), |
| 43 | } |
Rohan Agrawal | 00e5efc | 2020-07-24 11:43:02 +0000 | [diff] [blame] | 44 | logger.Debugw(ctx, "topics", log.Fields{"core": proxy.coreTopic}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 45 | return &proxy |
| 46 | } |
| 47 | |
| 48 | func (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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 56 | logger.Debugw(ctx, "sending-inter-adapter-message", log.Fields{"type": msgType, "from": fromAdapter, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 63 | logger.Warnw(ctx, "cannot-marshal-msg", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 64 | return err |
| 65 | } |
| 66 | |
Rohan Agrawal | 00e5efc | 2020-07-24 11:43:02 +0000 | [diff] [blame] | 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 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 73 | //Build the inter adapter message |
| 74 | header := &ic.InterAdapterHeader{ |
| 75 | Type: msgType, |
| 76 | FromTopic: fromAdapter, |
Rohan Agrawal | 00e5efc | 2020-07-24 11:43:02 +0000 | [diff] [blame] | 77 | ToTopic: string(endpoint), |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 78 | ToDeviceId: toDeviceId, |
| 79 | ProxyDeviceId: proxyDeviceId, |
| 80 | } |
| 81 | if messageId != "" { |
| 82 | header.Id = messageId |
| 83 | } else { |
| 84 | header.Id = uuid.New().String() |
| 85 | } |
Scott Baker | 84a55ce | 2020-04-17 10:11:30 -0700 | [diff] [blame] | 86 | header.Timestamp = ptypes.TimestampNow() |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 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 | |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 97 | topic := kafka.Topic{Name: string(endpoint)} |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 98 | replyToTopic := kafka.Topic{Name: fromAdapter} |
| 99 | rpc := "process_inter_adapter_message" |
| 100 | |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 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) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 103 | success, result := ap.kafkaICProxy.InvokeRPC(ctx, rpc, &topic, &replyToTopic, true, proxyDeviceId, args...) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 104 | logger.Debugw(ctx, "inter-adapter-msg-response", log.Fields{"replyTopic": replyToTopic, "success": success}) |
| 105 | return unPackResponse(ctx, rpc, "", success, result) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 106 | } |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 107 | |
| 108 | func (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 | } |