sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [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 | // gRPC affinity router with active/active backends |
| 17 | |
| 18 | package afrouter |
| 19 | |
| 20 | import ( |
| 21 | "fmt" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 22 | "github.com/golang/protobuf/proto" |
| 23 | "github.com/opencord/voltha-go/common/log" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 24 | "google.golang.org/grpc" |
| 25 | "sync" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func Codec() grpc.Codec { |
| 29 | return CodecWithParent(&protoCodec{}) |
| 30 | } |
| 31 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 32 | func CodecWithParent(parent grpc.Codec) grpc.Codec { |
| 33 | return &transparentRoutingCodec{parent} |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 34 | } |
| 35 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 36 | type transparentRoutingCodec struct { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 37 | parentCodec grpc.Codec |
| 38 | } |
| 39 | |
| 40 | type sbFrame struct { |
| 41 | payload []byte |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 42 | router Router |
| 43 | method string |
| 44 | be *backend |
| 45 | lck sync.Mutex |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 46 | metaKey string |
| 47 | metaVal string |
| 48 | } |
| 49 | |
| 50 | type nbFrame struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 51 | payload []byte |
| 52 | router Router |
| 53 | be *backend |
| 54 | err error |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 55 | mthdSlice []string |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 56 | serNo chan uint64 |
| 57 | metaKey string |
| 58 | metaVal string |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 59 | } |
| 60 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 61 | func (cdc *transparentRoutingCodec) Marshal(v interface{}) ([]byte, error) { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 62 | switch t := v.(type) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 63 | case *sbFrame: |
| 64 | return t.payload, nil |
| 65 | case *nbFrame: |
| 66 | return t.payload, nil |
| 67 | default: |
| 68 | return cdc.parentCodec.Marshal(v) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | } |
| 72 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 73 | func (cdc *transparentRoutingCodec) Unmarshal(data []byte, v interface{}) error { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 74 | switch t := v.(type) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 75 | 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) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 92 | func (cdc *transparentRoutingCodec) String() string { |
| 93 | return fmt.Sprintf("%s", cdc.parentCodec.String()) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 94 | } |
| 95 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 96 | // protoCodec is a Codec implementation with protobuf. It is the default Codec for gRPC. |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 97 | type protoCodec struct{} |
| 98 | |
| 99 | func (protoCodec) Marshal(v interface{}) ([]byte, error) { |
| 100 | return proto.Marshal(v.(proto.Message)) |
| 101 | } |
| 102 | |
| 103 | func (protoCodec) Unmarshal(data []byte, v interface{}) error { |
| 104 | return proto.Unmarshal(data, v.(proto.Message)) |
| 105 | } |
| 106 | |
| 107 | func (protoCodec) String() string { |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 108 | return "protoCodec" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 109 | } |