blob: 7147916403ee333795d42bc329183c7ebca17cec [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 */
16// gRPC affinity router with active/active backends
17
18package afrouter
19
20import (
21 "fmt"
sslobodr392ebd52019-01-18 12:41:49 -050022 "github.com/golang/protobuf/proto"
23 "github.com/opencord/voltha-go/common/log"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040024 "google.golang.org/grpc"
25 "sync"
sslobodr392ebd52019-01-18 12:41:49 -050026)
27
28func Codec() grpc.Codec {
29 return CodecWithParent(&protoCodec{})
30}
31
sslobodrcd37bc52019-01-24 11:47:16 -050032func CodecWithParent(parent grpc.Codec) grpc.Codec {
33 return &transparentRoutingCodec{parent}
sslobodr392ebd52019-01-18 12:41:49 -050034}
35
sslobodrcd37bc52019-01-24 11:47:16 -050036type transparentRoutingCodec struct {
sslobodr392ebd52019-01-18 12:41:49 -050037 parentCodec grpc.Codec
38}
39
40type sbFrame struct {
41 payload []byte
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040042 router Router
43 method string
44 be *backend
45 lck sync.Mutex
sslobodr392ebd52019-01-18 12:41:49 -050046 metaKey string
47 metaVal string
48}
49
50type nbFrame struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040051 payload []byte
52 router Router
53 be *backend
54 err error
sslobodr392ebd52019-01-18 12:41:49 -050055 mthdSlice []string
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040056 serNo chan 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 Hagerman0ab4cb22019-04-24 13:13:35 -040063 case *sbFrame:
64 return t.payload, nil
65 case *nbFrame:
66 return t.payload, nil
67 default:
68 return cdc.parentCodec.Marshal(v)
sslobodr392ebd52019-01-18 12:41:49 -050069 }
70
71}
72
sslobodrcd37bc52019-01-24 11:47:16 -050073func (cdc *transparentRoutingCodec) Unmarshal(data []byte, v interface{}) error {
sslobodr392ebd52019-01-18 12:41:49 -050074 switch t := v.(type) {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040075 case *sbFrame:
76 t.payload = data
77 // This is where the affinity is established on a northbound response
78 t.router.ReplyHandler(v)
79 return nil
80 case *nbFrame:
81 t.payload = data
82 // This is were the afinity value is pulled from the payload
83 // and the backend selected.
84 t.be = t.router.Route(v)
85 log.Debugf("Routing returned %v for method %s", t.be, t.mthdSlice[REQ_METHOD])
86 return nil
87 default:
88 return cdc.parentCodec.Unmarshal(data, v)
sslobodr392ebd52019-01-18 12:41:49 -050089 }
90}
91
sslobodrcd37bc52019-01-24 11:47:16 -050092func (cdc *transparentRoutingCodec) String() string {
93 return fmt.Sprintf("%s", cdc.parentCodec.String())
sslobodr392ebd52019-01-18 12:41:49 -050094}
95
sslobodrcd37bc52019-01-24 11:47:16 -050096// protoCodec is a Codec implementation with protobuf. It is the default Codec for gRPC.
sslobodr392ebd52019-01-18 12:41:49 -050097type protoCodec struct{}
98
99func (protoCodec) Marshal(v interface{}) ([]byte, error) {
100 return proto.Marshal(v.(proto.Message))
101}
102
103func (protoCodec) Unmarshal(data []byte, v interface{}) error {
104 return proto.Unmarshal(data, v.(proto.Message))
105}
106
107func (protoCodec) String() string {
sslobodrcd37bc52019-01-24 11:47:16 -0500108 return "protoCodec"
sslobodr392ebd52019-01-18 12:41:49 -0500109}