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" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func Codec() grpc.Codec { |
| 27 | return CodecWithParent(&protoCodec{}) |
| 28 | } |
| 29 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 30 | func CodecWithParent(parent grpc.Codec) grpc.Codec { |
| 31 | return &transparentRoutingCodec{parent} |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 32 | } |
| 33 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 34 | type transparentRoutingCodec struct { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 35 | parentCodec grpc.Codec |
| 36 | } |
| 37 | |
Kent Hagerman | 03b5899 | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 38 | // responseFrame is a frame being "returned" to whomever established the connection |
| 39 | type responseFrame struct { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 40 | payload []byte |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 41 | router Router |
| 42 | method string |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 43 | backend *backend |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 44 | metaKey string |
| 45 | metaVal string |
| 46 | } |
| 47 | |
Kent Hagerman | 03b5899 | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 48 | // requestFrame is a frame coming in from whomever established the connection |
| 49 | type requestFrame struct { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 50 | payload []byte |
| 51 | router Router |
| 52 | backend *backend |
| 53 | err error |
| 54 | methodInfo methodDetails |
| 55 | serialNo uint64 |
| 56 | metaKey string |
| 57 | metaVal string |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 58 | } |
| 59 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 60 | func (cdc *transparentRoutingCodec) Marshal(v interface{}) ([]byte, error) { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 61 | switch t := v.(type) { |
Kent Hagerman | 03b5899 | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 62 | case *responseFrame: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 63 | return t.payload, nil |
Kent Hagerman | 03b5899 | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 64 | case *requestFrame: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 65 | return t.payload, nil |
| 66 | default: |
| 67 | return cdc.parentCodec.Marshal(v) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 68 | } |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 69 | } |
| 70 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 71 | func (cdc *transparentRoutingCodec) Unmarshal(data []byte, v interface{}) error { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 72 | switch t := v.(type) { |
Kent Hagerman | 03b5899 | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 73 | case *responseFrame: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 74 | t.payload = data |
| 75 | // This is where the affinity is established on a northbound response |
| 76 | t.router.ReplyHandler(v) |
| 77 | return nil |
Kent Hagerman | 03b5899 | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 78 | case *requestFrame: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 79 | t.payload = data |
| 80 | // This is were the afinity value is pulled from the payload |
| 81 | // and the backend selected. |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 82 | t.backend = t.router.Route(v) |
Kent Hagerman | 03b5899 | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 83 | name := "<nil>" |
| 84 | if t.backend != nil { |
| 85 | name = t.backend.name |
| 86 | } |
| 87 | log.Debugf("Routing returned %s for method %s", name, t.methodInfo.method) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 88 | return nil |
| 89 | default: |
| 90 | return cdc.parentCodec.Unmarshal(data, v) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 94 | func (cdc *transparentRoutingCodec) String() string { |
| 95 | return fmt.Sprintf("%s", cdc.parentCodec.String()) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 96 | } |
| 97 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 98 | // 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] | 99 | type protoCodec struct{} |
| 100 | |
| 101 | func (protoCodec) Marshal(v interface{}) ([]byte, error) { |
| 102 | return proto.Marshal(v.(proto.Message)) |
| 103 | } |
| 104 | |
| 105 | func (protoCodec) Unmarshal(data []byte, v interface{}) error { |
| 106 | return proto.Unmarshal(data, v.(proto.Message)) |
| 107 | } |
| 108 | |
| 109 | func (protoCodec) String() string { |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 110 | return "protoCodec" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 111 | } |