blob: 761be2ea59ccf5e2c721734cdc49c156ff9eba90 [file] [log] [blame]
sslobodr392ebd52019-01-18 12:41:49 -05001/*
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 */
16// gRPC affinity router with active/active backends
17
18package afrouter
19
20// Command line parameters and parsing
21import (
22 "os"
23 "fmt"
24 "flag"
25 "path"
26 "errors"
27 "io/ioutil"
28 "encoding/json"
29 "github.com/opencord/voltha-go/common/log"
30)
31
32func ParseCmd() (*Configuration, error) {
33 config := &Configuration{}
34 cmdParse := flag.NewFlagSet(path.Base(os.Args[0]), flag.ContinueOnError);
35 config.ConfigFile = cmdParse.String("config", "arouter.json", "The configuration file for the affinity router")
36 config.LogLevel = cmdParse.Int("logLevel", 0, "The log level for the affinity router")
37 config.GrpcLog = cmdParse.Bool("grpclog", false, "Enable GRPC logging")
38
39 err := cmdParse.Parse(os.Args[1:]);
40 if(err != nil) {
41 //return err
42 return nil, errors.New("Error parsing the command line");
43 }
44 //if(!cmdParse.Parsed()) {
45 //}
46 return config, nil
47}
48
49// Configuration file loading and parsing
50type Configuration struct {
51 ConfigFile * string
52 LogLevel * int
53 GrpcLog * bool
54 Servers []ServerConfig `json:"servers"`
55 Ports PortConfig `json:"ports"`
56 ServerCertificates ServerCertConfig `json:"serverCertificates"`
57 ClientCertificates ClientCertConfig `json:"clientCertificates"`
58 BackendClusters []BackendClusterConfig `json:"backend_clusters"`
59 Routers []RouterConfig `json:"routers"`
60 Api ApiConfig
61}
62
63type RouterConfig struct {
64 Name string `json:"name"`
65 ProtoService string `json:"service"`
66 ProtoPackage string `json:"package"`
67 Routes []RouteConfig `json:"routes"`
68}
69
70type RouteConfig struct {
71 Name string `json:"name"`
72 Type string `json:"type"`
73 ProtoFile string `json:"proto_descriptor"`
74 Association string `json:"association"`
75 RouteField string `json:"routing_field"`
76 Methods []string `json:"methods"` // The GRPC methods to route using the route field
77 NbBindingMethods []string `json:"nb_binding_methods"`
78 BackendCluster string `json:"backend_cluster"`
79 Binding BindingConfig `json:"binding"`
80 Overrides []OverrideConfig `json:"overrides"`
81 backendCluster *BackendClusterConfig
82}
83
84type BindingConfig struct {
85 Type string `json:"type"`
86 Field string `json:"field"`
87 Method string `json:"method"`
88 Association string `json:"association"`
89
90}
91
92type OverrideConfig struct {
93 Methods []string `json:"methods"`
94 Method []string `json:"method"`
95 RouteField string `json:"routing_field"`
96}
97
98// Backend configuration
99
100type BackendClusterConfig struct {
101 Name string `json:"name"`
102 Backends []BackendConfig `json:"backends"`
103}
104
105type BackendConfig struct {
106 Name string `json:"name"`
107 Type string `json:"type"`
108 Association AssociationConfig `json:"association"`
109 Connections []ConnectionConfig `json:"connections"`
110}
111
112type AssociationConfig struct {
113 Strategy string `json:"strategy"`
114 Location string `json:"location"`
115 Field string `json:"field"`
116}
117
118type ConnectionConfig struct {
119 Name string `json:"name"`
120 Addr string `json:"addr"`
121 Port string `json:"port"`
122}
123
124// Server configuration
125
126type ServerConfig struct {
127 Name string `json:"name"`
128 Port uint `json:"port"`
129 Addr string `json:"address"`
130 Type string `json:"type"`
131 Routers []RouterPackage `json:"routers"`
132 routers map[string]*RouterConfig
133}
134
135type RouterPackage struct {
136 Router string `json:"router"`
137 Package string `json:"package"`
138}
139
140// Port configuration
141type PortConfig struct {
142 GrpcPort uint `json:"grpcPort"`
143 StreamingGrpcPort uint `json:"streamingGrpcPort"`
144 TlsGrpcPort uint `json:"tlsGrpcPort"`
145 TlsStreamingGrpcPort uint `json:"tlsStreamingGrpcPort"`
146 ControlPort uint `json:"controlPort"`
147}
148
149// Server Certificate configuration
150type ServerCertConfig struct {
151 GrpcCert string `json:"grpcCertificate"` // File path to the certificate file
152 GrpcKey string `json:"grpcKey"` // File path to the key file
153 GrpcCsr string `json:"grpcCsr"` // File path to the CSR file
154}
155
156// Client Certificate configuration
157type ClientCertConfig struct {
158 GrpcCert string `json:"grpcCertificate"` // File path to the certificate file
159 GrpcKey string `json:"grpcKey"` // File path to the key file
160 GrpcCsr string `json:"grpcCsr"` // File path to the CSR file
161}
162
163// Api configuration
164type ApiConfig struct {
165 Addr string `json:"address"`
166 Port uint `json:"port"`
167}
168
169func (conf * Configuration) LoadConfig() error {
170
171 configF, err := os.Open(*conf.ConfigFile);
172 log.Info("Loading configuration from: ", *conf.ConfigFile)
173 if err != nil {
174 log.Error(err)
175 return err
176 }
177
178 defer configF.Close()
179
180 configBytes, err := ioutil.ReadAll(configF)
181 if err != nil {
182 log.Error(err)
183 return err
184 }
185
186 json.Unmarshal(configBytes, conf)
187
188 // Now resolve references to different config objects in the
189 // config file. Currently there are 2 possible references
190 // to resolve: referecnes to routers in the servers, and
191 // references to backend_cluster in the routers.
192
193 // Resolve router references for the servers
194 log.Debug("Resolving references in the config file");
195 for k,_ := range(conf.Servers) {
196 //s.routers =make(map[string]*RouterConfig)
197 conf.Servers[k].routers = make(map[string]*RouterConfig)
198 for _,rPkg := range(conf.Servers[k].Routers) {
199 var found bool = false
200 // Locate the router "r" in the top lever Routers array
201 log.Debugf("Resolving router reference to router '%s' from server '%s'",rPkg.Router, conf.Servers[k].Name)
202 for rk, _ := range(conf.Routers) {
203 if conf.Routers[rk].Name == rPkg.Router && !found {
204 log.Debugf("Reference to router '%s' found for package '%s'", rPkg.Router, rPkg.Package)
205 conf.Servers[k].routers[rPkg.Package] = &conf.Routers[rk]
206 found = true
207 } else if conf.Routers[rk].Name == rPkg.Router && found {
208 if _,ok := conf.Servers[k].routers[rPkg.Package]; !ok {
209 log.Debugf("Reference to router '%s' found for package '%s'", rPkg.Router, rPkg.Package)
210 conf.Servers[k].routers[rPkg.Package] = &conf.Routers[rk]
211 } else {
212 err := errors.New(fmt.Sprintf("Duplicate router '%s' defined for package '%s'",rPkg.Package))
213 log.Error(err)
214 return err
215 }
216 }
217 }
218 if !found {
219 err := errors.New(fmt.Sprintf("Router %s for server %s not found in config", conf.Servers[k].Name, rPkg.Router))
220 log.Error(err)
221 return err
222 }
223 }
224 }
225
226 // Resolve backend references for the routers
227 for rk,rv := range(conf.Routers) {
228 for rtk,rtv := range(rv.Routes) {
229 var found bool = false
230 log.Debugf("Resolving backend reference to %s from router %s",rtv.BackendCluster, rv.Name)
231 for bek,bev := range(conf.BackendClusters) {
232 log.Debugf("Checking cluster %s", conf.BackendClusters[bek].Name)
233 if rtv.BackendCluster == bev.Name && !found {
234 conf.Routers[rk].Routes[rtk].backendCluster = &conf.BackendClusters[bek]
235 found = true
236 } else if rtv.BackendCluster == bev.Name && found {
237 err := errors.New(fmt.Sprintf("Duplicate backend defined, %s",conf.BackendClusters[bek].Name))
238 log.Error(err)
239 return err
240 }
241 }
242 if !found {
243 err := errors.New(fmt.Sprintf("Backend %s for router %s not found in config",
244 rtv.BackendCluster, rv.Name))
245 log.Error(err)
246 return err
247 }
248 }
249 }
250
251
252 return nil
253}