blob: 27f46191131c93232b4e544febb42146570160ec [file] [log] [blame]
sslobodr392ebd52019-01-18 12:41:49 -05001/*
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 */
sslobodr392ebd52019-01-18 12:41:49 -050016
17package afrouter
18
19import (
20 "fmt"
sslobodr392ebd52019-01-18 12:41:49 -050021 "github.com/golang/protobuf/proto"
22 "github.com/opencord/voltha-go/common/log"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040023 "google.golang.org/grpc"
sslobodr392ebd52019-01-18 12:41:49 -050024)
25
26func Codec() grpc.Codec {
27 return CodecWithParent(&protoCodec{})
28}
29
sslobodrcd37bc52019-01-24 11:47:16 -050030func CodecWithParent(parent grpc.Codec) grpc.Codec {
31 return &transparentRoutingCodec{parent}
sslobodr392ebd52019-01-18 12:41:49 -050032}
33
sslobodrcd37bc52019-01-24 11:47:16 -050034type transparentRoutingCodec struct {
sslobodr392ebd52019-01-18 12:41:49 -050035 parentCodec grpc.Codec
36}
37
Kent Hagerman03b58992019-08-29 17:21:03 -040038// responseFrame is a frame being "returned" to whomever established the connection
39type responseFrame struct {
sslobodr392ebd52019-01-18 12:41:49 -050040 payload []byte
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040041 router Router
42 method string
Kent Hagerman1e9061e2019-05-21 16:01:21 -040043 backend *backend
sslobodr392ebd52019-01-18 12:41:49 -050044 metaKey string
45 metaVal string
46}
47
Kent Hagerman03b58992019-08-29 17:21:03 -040048// requestFrame is a frame coming in from whomever established the connection
49type requestFrame struct {
Kent Hagerman1e9061e2019-05-21 16:01:21 -040050 payload []byte
51 router Router
52 backend *backend
Scott Baker112b0d42019-08-22 08:32:26 -070053 connection *connection // optional, if the router preferred one connection over another
Kent Hagerman1e9061e2019-05-21 16:01:21 -040054 err error
55 methodInfo methodDetails
56 serialNo uint64
57 metaKey string
58 metaVal string
sslobodr392ebd52019-01-18 12:41:49 -050059}
60
sslobodrcd37bc52019-01-24 11:47:16 -050061func (cdc *transparentRoutingCodec) Marshal(v interface{}) ([]byte, error) {
sslobodr392ebd52019-01-18 12:41:49 -050062 switch t := v.(type) {
Kent Hagerman03b58992019-08-29 17:21:03 -040063 case *responseFrame:
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040064 return t.payload, nil
Kent Hagerman03b58992019-08-29 17:21:03 -040065 case *requestFrame:
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040066 return t.payload, nil
67 default:
68 return cdc.parentCodec.Marshal(v)
sslobodr392ebd52019-01-18 12:41:49 -050069 }
sslobodr392ebd52019-01-18 12:41:49 -050070}
71
sslobodrcd37bc52019-01-24 11:47:16 -050072func (cdc *transparentRoutingCodec) Unmarshal(data []byte, v interface{}) error {
sslobodr392ebd52019-01-18 12:41:49 -050073 switch t := v.(type) {
Kent Hagerman03b58992019-08-29 17:21:03 -040074 case *responseFrame:
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040075 t.payload = data
76 // This is where the affinity is established on a northbound response
77 t.router.ReplyHandler(v)
78 return nil
Kent Hagerman03b58992019-08-29 17:21:03 -040079 case *requestFrame:
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040080 t.payload = data
81 // This is were the afinity value is pulled from the payload
82 // and the backend selected.
Scott Baker112b0d42019-08-22 08:32:26 -070083 t.backend, t.connection = t.router.Route(v)
Kent Hagerman03b58992019-08-29 17:21:03 -040084 name := "<nil>"
85 if t.backend != nil {
86 name = t.backend.name
87 }
88 log.Debugf("Routing returned %s for method %s", name, t.methodInfo.method)
Scott Baker112b0d42019-08-22 08:32:26 -070089
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040090 return nil
91 default:
92 return cdc.parentCodec.Unmarshal(data, v)
sslobodr392ebd52019-01-18 12:41:49 -050093 }
94}
95
sslobodrcd37bc52019-01-24 11:47:16 -050096func (cdc *transparentRoutingCodec) String() string {
97 return fmt.Sprintf("%s", cdc.parentCodec.String())
sslobodr392ebd52019-01-18 12:41:49 -050098}
99
sslobodrcd37bc52019-01-24 11:47:16 -0500100// protoCodec is a Codec implementation with protobuf. It is the default Codec for gRPC.
sslobodr392ebd52019-01-18 12:41:49 -0500101type protoCodec struct{}
102
103func (protoCodec) Marshal(v interface{}) ([]byte, error) {
104 return proto.Marshal(v.(proto.Message))
105}
106
107func (protoCodec) Unmarshal(data []byte, v interface{}) error {
108 return proto.Unmarshal(data, v.(proto.Message))
109}
110
111func (protoCodec) String() string {
sslobodrcd37bc52019-01-24 11:47:16 -0500112 return "protoCodec"
sslobodr392ebd52019-01-18 12:41:49 -0500113}