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 | */ |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 16 | |
| 17 | package afrouter |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 21 | "github.com/golang/protobuf/proto" |
| 22 | "github.com/opencord/voltha-go/common/log" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 23 | "google.golang.org/grpc" |
| 24 | "sync" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func Codec() grpc.Codec { |
| 28 | return CodecWithParent(&protoCodec{}) |
| 29 | } |
| 30 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 31 | func CodecWithParent(parent grpc.Codec) grpc.Codec { |
| 32 | return &transparentRoutingCodec{parent} |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 33 | } |
| 34 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 35 | type transparentRoutingCodec struct { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 36 | parentCodec grpc.Codec |
| 37 | } |
| 38 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 39 | // sbFrame is a frame being "returned" to whomever established the connection |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 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 |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 44 | backend *backend |
| 45 | mutex sync.Mutex |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 46 | metaKey string |
| 47 | metaVal string |
| 48 | } |
| 49 | |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 50 | // nbFrame is a frame coming in from whomever established the connection |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 51 | type nbFrame struct { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 52 | payload []byte |
| 53 | router Router |
| 54 | backend *backend |
| 55 | err error |
| 56 | methodInfo methodDetails |
| 57 | serialNo uint64 |
| 58 | metaKey string |
| 59 | metaVal string |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 60 | } |
| 61 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 62 | func (cdc *transparentRoutingCodec) Marshal(v interface{}) ([]byte, error) { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 63 | switch t := v.(type) { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 64 | case *sbFrame: |
| 65 | return t.payload, nil |
| 66 | case *nbFrame: |
| 67 | return t.payload, nil |
| 68 | default: |
| 69 | return cdc.parentCodec.Marshal(v) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 70 | } |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 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. |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 84 | t.backend = t.router.Route(v) |
| 85 | log.Debugf("Routing returned %v for method %s", t.backend, t.methodInfo.method) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 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 | } |