Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | |
| 17 | package afrouter |
| 18 | |
| 19 | import ( |
| 20 | "encoding/json" |
| 21 | "fmt" |
| 22 | ) |
| 23 | |
| 24 | type backendType int |
| 25 | |
| 26 | const ( |
| 27 | BackendUndefined backendType = iota |
| 28 | BackendActiveActive |
| 29 | BackendSingleServer |
| 30 | ) |
| 31 | |
| 32 | var stringToBeType = map[string]backendType{"active_active": BackendActiveActive, "server": BackendSingleServer} |
| 33 | var beTypeToString = map[backendType]string{BackendActiveActive: "active_active", BackendSingleServer: "server"} |
| 34 | |
| 35 | func (t backendType) MarshalJSON() ([]byte, error) { |
| 36 | if t == BackendUndefined { |
| 37 | return json.Marshal(nil) |
| 38 | } |
| 39 | if str, have := beTypeToString[t]; have { |
| 40 | return json.Marshal(str) |
| 41 | } |
| 42 | return nil, fmt.Errorf("unknown %T '%d'", t, t) |
| 43 | } |
| 44 | |
| 45 | func (t *backendType) UnmarshalJSON(b []byte) error { |
| 46 | var str string |
| 47 | if err := json.Unmarshal(b, &str); err != nil { |
| 48 | return err |
| 49 | } |
| 50 | var have bool |
| 51 | if *t, have = stringToBeType[str]; !have { |
| 52 | return fmt.Errorf("invalid %T %s", *t, str) |
| 53 | } |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | type associationLocation int |
| 58 | |
| 59 | const ( |
| 60 | AssociationLocationUndefined associationLocation = iota |
| 61 | AssociationLocationHeader |
| 62 | AssociationLocationProtobuf |
| 63 | ) |
| 64 | |
| 65 | var stringToAlType = map[string]associationLocation{"header": AssociationLocationHeader, "protobuf": AssociationLocationProtobuf} |
| 66 | var alTypeToString = map[associationLocation]string{AssociationLocationHeader: "header", AssociationLocationProtobuf: "protobuf"} |
| 67 | |
| 68 | func (t associationLocation) MarshalJSON() ([]byte, error) { |
| 69 | if t == AssociationLocationUndefined { |
| 70 | return json.Marshal(nil) |
| 71 | } |
| 72 | if str, have := alTypeToString[t]; have { |
| 73 | return json.Marshal(str) |
| 74 | } |
| 75 | return nil, fmt.Errorf("unknown %T '%d'", t, t) |
| 76 | } |
| 77 | |
| 78 | func (t *associationLocation) UnmarshalJSON(b []byte) error { |
| 79 | var str string |
| 80 | if err := json.Unmarshal(b, &str); err != nil { |
| 81 | return err |
| 82 | } |
| 83 | var have bool |
| 84 | if *t, have = stringToAlType[str]; !have { |
| 85 | return fmt.Errorf("invalid %T %s", *t, str) |
| 86 | } |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | type associationStrategy int |
| 91 | |
| 92 | const ( |
| 93 | AssociationStrategyUndefined associationStrategy = iota |
| 94 | AssociationStrategySerialNo |
| 95 | ) |
| 96 | |
| 97 | var stringToAsType = map[string]associationStrategy{"serial_number": AssociationStrategySerialNo} |
| 98 | var asTypeToString = map[associationStrategy]string{AssociationStrategySerialNo: "serial_number"} |
| 99 | |
| 100 | func (t associationStrategy) MarshalJSON() ([]byte, error) { |
| 101 | if t == AssociationStrategyUndefined { |
| 102 | return json.Marshal(nil) |
| 103 | } |
| 104 | if str, have := asTypeToString[t]; have { |
| 105 | return json.Marshal(str) |
| 106 | } |
| 107 | return nil, fmt.Errorf("unknown %T '%d'", t, t) |
| 108 | } |
| 109 | |
| 110 | func (t *associationStrategy) UnmarshalJSON(b []byte) error { |
| 111 | var str string |
| 112 | if err := json.Unmarshal(b, &str); err != nil { |
| 113 | return err |
| 114 | } |
| 115 | var have bool |
| 116 | if *t, have = stringToAsType[str]; !have { |
| 117 | return fmt.Errorf("invalid %T %s", *t, str) |
| 118 | } |
| 119 | return nil |
| 120 | } |
| 121 | |
| 122 | type backendSequence int |
| 123 | |
| 124 | const ( |
| 125 | BackendSequenceRoundRobin backendSequence = iota |
| 126 | ) |
| 127 | |
| 128 | type routeType int |
| 129 | |
| 130 | const ( |
| 131 | RouteTypeUndefined routeType = iota |
| 132 | RouteTypeRpcAffinityMessage |
| 133 | RouteTypeRpcAffinityHeader |
| 134 | RouteTypeBinding |
| 135 | RouteTypeRoundRobin |
| 136 | RouteTypeSource |
| 137 | ) |
| 138 | |
| 139 | // String names for display in error messages. |
| 140 | var stringToRouteType = map[string]routeType{"rpc_affinity_message": RouteTypeRpcAffinityMessage, "rpc_affinity_header": RouteTypeRpcAffinityHeader, "binding": RouteTypeBinding, "round_robin": RouteTypeRoundRobin, "source": RouteTypeSource} |
| 141 | var routeTypeToString = map[routeType]string{RouteTypeRpcAffinityMessage: "rpc_affinity_message", RouteTypeRpcAffinityHeader: "rpc_affinity_header", RouteTypeBinding: "binding", RouteTypeRoundRobin: "round_robin", RouteTypeSource: "source"} |
| 142 | |
| 143 | func (t routeType) String() string { |
| 144 | if str, have := routeTypeToString[t]; have { |
| 145 | return str |
| 146 | } |
| 147 | return fmt.Sprintf("%T(%d)", t, t) |
| 148 | } |
| 149 | |
| 150 | func (t routeType) MarshalJSON() ([]byte, error) { |
| 151 | if t == RouteTypeUndefined { |
| 152 | return json.Marshal(nil) |
| 153 | } |
| 154 | if str, have := routeTypeToString[t]; have { |
| 155 | return json.Marshal(str) |
| 156 | } |
| 157 | return nil, fmt.Errorf("unknown %T '%d'", t, t) |
| 158 | } |
| 159 | |
| 160 | func (t *routeType) UnmarshalJSON(b []byte) error { |
| 161 | var str string |
| 162 | if err := json.Unmarshal(b, &str); err != nil { |
| 163 | return err |
| 164 | } |
| 165 | var have bool |
| 166 | if *t, have = stringToRouteType[str]; !have { |
| 167 | return fmt.Errorf("invalid %T %s", *t, str) |
| 168 | } |
| 169 | return nil |
| 170 | } |
| 171 | |
| 172 | type associationType int |
| 173 | |
| 174 | const ( |
| 175 | AssociationUndefined associationType = iota |
| 176 | AssociationRoundRobin |
| 177 | ) |
| 178 | |
| 179 | var stringToAssociationType = map[string]associationType{"round_robin": AssociationRoundRobin} |
| 180 | var associationTypeToString = map[associationType]string{AssociationRoundRobin: "round_robin"} |
| 181 | |
| 182 | func (t associationType) MarshalJSON() ([]byte, error) { |
| 183 | if t == AssociationUndefined { |
| 184 | return json.Marshal(nil) |
| 185 | } |
| 186 | if str, have := associationTypeToString[t]; have { |
| 187 | return json.Marshal(str) |
| 188 | } |
| 189 | return nil, fmt.Errorf("unknown %T '%d'", t, t) |
| 190 | } |
| 191 | |
| 192 | func (t *associationType) UnmarshalJSON(b []byte) error { |
| 193 | var str string |
| 194 | if err := json.Unmarshal(b, &str); err != nil { |
| 195 | return err |
| 196 | } |
| 197 | var have bool |
| 198 | if *t, have = stringToAssociationType[str]; !have { |
| 199 | return fmt.Errorf("invalid %T %s", *t, str) |
| 200 | } |
| 201 | return nil |
| 202 | } |