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 |
Scott Baker | 112b0d4 | 2019-08-22 08:32:26 -0700 | [diff] [blame^] | 53 | connection *connection // optional, if the router preferred one connection over another |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 54 | err error |
| 55 | methodInfo methodDetails |
| 56 | serialNo 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 | 03b5899 | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 63 | case *responseFrame: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 64 | return t.payload, nil |
Kent Hagerman | 03b5899 | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 65 | case *requestFrame: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 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 | } |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 70 | } |
| 71 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 72 | func (cdc *transparentRoutingCodec) Unmarshal(data []byte, v interface{}) error { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 73 | switch t := v.(type) { |
Kent Hagerman | 03b5899 | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 74 | case *responseFrame: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 75 | t.payload = data |
| 76 | // This is where the affinity is established on a northbound response |
| 77 | t.router.ReplyHandler(v) |
| 78 | return nil |
Kent Hagerman | 03b5899 | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 79 | case *requestFrame: |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 80 | t.payload = data |
| 81 | // This is were the afinity value is pulled from the payload |
| 82 | // and the backend selected. |
Scott Baker | 112b0d4 | 2019-08-22 08:32:26 -0700 | [diff] [blame^] | 83 | t.backend, t.connection = t.router.Route(v) |
Kent Hagerman | 03b5899 | 2019-08-29 17:21:03 -0400 | [diff] [blame] | 84 | 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 Baker | 112b0d4 | 2019-08-22 08:32:26 -0700 | [diff] [blame^] | 89 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 90 | return nil |
| 91 | default: |
| 92 | return cdc.parentCodec.Unmarshal(data, v) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 96 | func (cdc *transparentRoutingCodec) String() string { |
| 97 | return fmt.Sprintf("%s", cdc.parentCodec.String()) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 98 | } |
| 99 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 100 | // 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] | 101 | type protoCodec struct{} |
| 102 | |
| 103 | func (protoCodec) Marshal(v interface{}) ([]byte, error) { |
| 104 | return proto.Marshal(v.(proto.Message)) |
| 105 | } |
| 106 | |
| 107 | func (protoCodec) Unmarshal(data []byte, v interface{}) error { |
| 108 | return proto.Unmarshal(data, v.(proto.Message)) |
| 109 | } |
| 110 | |
| 111 | func (protoCodec) String() string { |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 112 | return "protoCodec" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 113 | } |