blob: 1847191c75112dfe36a1c4bda6053be1af4e0f2f [file] [log] [blame]
Kent Hagerman1e9061e2019-05-21 16:01:21 -04001/*
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
17package afrouter
18
19import (
20 "encoding/json"
21 "fmt"
22)
23
24type backendType int
25
26const (
27 BackendUndefined backendType = iota
28 BackendActiveActive
29 BackendSingleServer
30)
31
32var stringToBeType = map[string]backendType{"active_active": BackendActiveActive, "server": BackendSingleServer}
33var beTypeToString = map[backendType]string{BackendActiveActive: "active_active", BackendSingleServer: "server"}
34
35func (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
45func (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
57type associationLocation int
58
59const (
60 AssociationLocationUndefined associationLocation = iota
61 AssociationLocationHeader
62 AssociationLocationProtobuf
63)
64
65var stringToAlType = map[string]associationLocation{"header": AssociationLocationHeader, "protobuf": AssociationLocationProtobuf}
66var alTypeToString = map[associationLocation]string{AssociationLocationHeader: "header", AssociationLocationProtobuf: "protobuf"}
67
68func (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
78func (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
90type associationStrategy int
91
92const (
93 AssociationStrategyUndefined associationStrategy = iota
94 AssociationStrategySerialNo
95)
96
97var stringToAsType = map[string]associationStrategy{"serial_number": AssociationStrategySerialNo}
98var asTypeToString = map[associationStrategy]string{AssociationStrategySerialNo: "serial_number"}
99
100func (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
110func (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
122type backendSequence int
123
124const (
125 BackendSequenceRoundRobin backendSequence = iota
126)
127
128type routeType int
129
130const (
131 RouteTypeUndefined routeType = iota
132 RouteTypeRpcAffinityMessage
133 RouteTypeRpcAffinityHeader
134 RouteTypeBinding
135 RouteTypeRoundRobin
Scott Baker112b0d42019-08-22 08:32:26 -0700136 RouteTypeSource
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400137)
138
139// String names for display in error messages.
Scott Baker112b0d42019-08-22 08:32:26 -0700140var stringToRouteType = map[string]routeType{"rpc_affinity_message": RouteTypeRpcAffinityMessage, "rpc_affinity_header": RouteTypeRpcAffinityHeader, "binding": RouteTypeBinding, "round_robin": RouteTypeRoundRobin, "source": RouteTypeSource}
141var routeTypeToString = map[routeType]string{RouteTypeRpcAffinityMessage: "rpc_affinity_message", RouteTypeRpcAffinityHeader: "rpc_affinity_header", RouteTypeBinding: "binding", RouteTypeRoundRobin: "round_robin", RouteTypeSource: "source"}
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400142
143func (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
150func (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
160func (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
172type associationType int
173
174const (
175 AssociationUndefined associationType = iota
176 AssociationRoundRobin
177)
178
179var stringToAssociationType = map[string]associationType{"round_robin": AssociationRoundRobin}
180var associationTypeToString = map[associationType]string{AssociationRoundRobin: "round_robin"}
181
182func (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
192func (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}