blob: 1f0be7bca6c5f19a3883c730b995a6c0f2a19255 [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 */
sslobodr392ebd52019-01-18 12:41:49 -050016
17package afrouter
18
19// Command line parameters and parsing
20import (
sslobodr392ebd52019-01-18 12:41:49 -050021 "encoding/json"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040022 "errors"
23 "flag"
24 "fmt"
sslobodr392ebd52019-01-18 12:41:49 -050025 "github.com/opencord/voltha-go/common/log"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040026 "io/ioutil"
27 "os"
28 "path"
sslobodr392ebd52019-01-18 12:41:49 -050029)
30
31func ParseCmd() (*Configuration, error) {
32 config := &Configuration{}
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040033 cmdParse := flag.NewFlagSet(path.Base(os.Args[0]), flag.ContinueOnError)
sslobodr392ebd52019-01-18 12:41:49 -050034 config.ConfigFile = cmdParse.String("config", "arouter.json", "The configuration file for the affinity router")
35 config.LogLevel = cmdParse.Int("logLevel", 0, "The log level for the affinity router")
36 config.GrpcLog = cmdParse.Bool("grpclog", false, "Enable GRPC logging")
David K. Bainbridgef430cd52019-05-28 15:00:35 -070037 config.DisplayVersionOnly = cmdParse.Bool("version", false, "Print version information and exit")
sslobodr392ebd52019-01-18 12:41:49 -050038
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040039 err := cmdParse.Parse(os.Args[1:])
sslobodr3d587c62019-01-24 12:33:39 -050040 if err != nil {
sslobodr392ebd52019-01-18 12:41:49 -050041 //return err
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040042 return nil, errors.New("Error parsing the command line")
sslobodr392ebd52019-01-18 12:41:49 -050043 }
44 //if(!cmdParse.Parsed()) {
45 //}
46 return config, nil
47}
48
49// Configuration file loading and parsing
50type Configuration struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040051 ConfigFile *string
52 LogLevel *int
53 GrpcLog *bool
David K. Bainbridgef430cd52019-05-28 15:00:35 -070054 DisplayVersionOnly *bool
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040055 Servers []ServerConfig `json:"servers"`
56 Ports PortConfig `json:"ports"`
57 ServerCertificates ServerCertConfig `json:"serverCertificates"`
58 ClientCertificates ClientCertConfig `json:"clientCertificates"`
59 BackendClusters []BackendClusterConfig `json:"backend_clusters"`
60 Routers []RouterConfig `json:"routers"`
61 Api ApiConfig
sslobodr392ebd52019-01-18 12:41:49 -050062}
63
64type RouterConfig struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040065 Name string `json:"name"`
66 ProtoService string `json:"service"`
67 ProtoPackage string `json:"package"`
68 Routes []RouteConfig `json:"routes"`
sslobodr392ebd52019-01-18 12:41:49 -050069}
70
71type RouteConfig struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040072 Name string `json:"name"`
Kent Hagerman1e9061e2019-05-21 16:01:21 -040073 Type routeType `json:"type"`
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040074 ProtoFile string `json:"proto_descriptor"`
Kent Hagerman1e9061e2019-05-21 16:01:21 -040075 Association associationType `json:"association"`
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040076 RouteField string `json:"routing_field"`
77 Methods []string `json:"methods"` // The GRPC methods to route using the route field
78 NbBindingMethods []string `json:"nb_binding_methods"`
79 BackendCluster string `json:"backend_cluster"`
80 Binding BindingConfig `json:"binding"`
81 Overrides []OverrideConfig `json:"overrides"`
82 backendCluster *BackendClusterConfig
sslobodr392ebd52019-01-18 12:41:49 -050083}
84
85type BindingConfig struct {
Kent Hagerman1e9061e2019-05-21 16:01:21 -040086 Type string `json:"type"`
87 Field string `json:"field"`
88 Method string `json:"method"`
89 Association associationType `json:"association"`
sslobodr392ebd52019-01-18 12:41:49 -050090}
91
92type OverrideConfig struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040093 Methods []string `json:"methods"`
94 Method string `json:"method"`
95 RouteField string `json:"routing_field"`
sslobodr392ebd52019-01-18 12:41:49 -050096}
97
98// Backend configuration
99
100type BackendClusterConfig struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400101 Name string `json:"name"`
sslobodr392ebd52019-01-18 12:41:49 -0500102 Backends []BackendConfig `json:"backends"`
103}
104
105type BackendConfig struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400106 Name string `json:"name"`
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400107 Type backendType `json:"type"`
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400108 Association AssociationConfig `json:"association"`
sslobodr392ebd52019-01-18 12:41:49 -0500109 Connections []ConnectionConfig `json:"connections"`
110}
111
112type AssociationConfig struct {
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400113 Strategy associationStrategy `json:"strategy"`
114 Location associationLocation `json:"location"`
115 Field string `json:"field"`
116 Key string `json:"key"`
sslobodr392ebd52019-01-18 12:41:49 -0500117}
118
119type ConnectionConfig struct {
120 Name string `json:"name"`
121 Addr string `json:"addr"`
122 Port string `json:"port"`
123}
124
125// Server configuration
126
127type ServerConfig struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400128 Name string `json:"name"`
129 Port uint `json:"port"`
130 Addr string `json:"address"`
131 Type string `json:"type"`
sslobodr392ebd52019-01-18 12:41:49 -0500132 Routers []RouterPackage `json:"routers"`
133 routers map[string]*RouterConfig
134}
135
136type RouterPackage struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400137 Router string `json:"router"`
sslobodr392ebd52019-01-18 12:41:49 -0500138 Package string `json:"package"`
139}
140
141// Port configuration
142type PortConfig struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400143 GrpcPort uint `json:"grpcPort"`
144 StreamingGrpcPort uint `json:"streamingGrpcPort"`
145 TlsGrpcPort uint `json:"tlsGrpcPort"`
sslobodr392ebd52019-01-18 12:41:49 -0500146 TlsStreamingGrpcPort uint `json:"tlsStreamingGrpcPort"`
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400147 ControlPort uint `json:"controlPort"`
sslobodr392ebd52019-01-18 12:41:49 -0500148}
149
150// Server Certificate configuration
151type ServerCertConfig struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400152 GrpcCert string `json:"grpcCertificate"` // File path to the certificate file
153 GrpcKey string `json:"grpcKey"` // File path to the key file
154 GrpcCsr string `json:"grpcCsr"` // File path to the CSR file
sslobodr392ebd52019-01-18 12:41:49 -0500155}
156
157// Client Certificate configuration
158type ClientCertConfig struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400159 GrpcCert string `json:"grpcCertificate"` // File path to the certificate file
160 GrpcKey string `json:"grpcKey"` // File path to the key file
161 GrpcCsr string `json:"grpcCsr"` // File path to the CSR file
sslobodr392ebd52019-01-18 12:41:49 -0500162}
163
164// Api configuration
165type ApiConfig struct {
166 Addr string `json:"address"`
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400167 Port uint `json:"port"`
sslobodr392ebd52019-01-18 12:41:49 -0500168}
169
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400170func (conf *Configuration) LoadConfig() error {
sslobodr392ebd52019-01-18 12:41:49 -0500171
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400172 configF, err := os.Open(*conf.ConfigFile)
sslobodr392ebd52019-01-18 12:41:49 -0500173 log.Info("Loading configuration from: ", *conf.ConfigFile)
174 if err != nil {
175 log.Error(err)
176 return err
177 }
178
179 defer configF.Close()
180
181 configBytes, err := ioutil.ReadAll(configF)
182 if err != nil {
183 log.Error(err)
184 return err
185 }
186
sslobodr8e2ccb52019-02-05 09:21:47 -0500187 if err := json.Unmarshal(configBytes, conf); err != nil {
188 log.Errorf("Unmarshaling of the configuratino file failed: %v", err)
189 return err
190 }
sslobodr392ebd52019-01-18 12:41:49 -0500191
192 // Now resolve references to different config objects in the
193 // config file. Currently there are 2 possible references
194 // to resolve: referecnes to routers in the servers, and
195 // references to backend_cluster in the routers.
196
197 // Resolve router references for the servers
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400198 log.Debug("Resolving references in the config file")
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400199 for k := range conf.Servers {
sslobodr392ebd52019-01-18 12:41:49 -0500200 //s.routers =make(map[string]*RouterConfig)
201 conf.Servers[k].routers = make(map[string]*RouterConfig)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400202 for _, rPkg := range conf.Servers[k].Routers {
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400203 var found = false
sslobodr392ebd52019-01-18 12:41:49 -0500204 // Locate the router "r" in the top lever Routers array
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400205 log.Debugf("Resolving router reference to router '%s' from server '%s'", rPkg.Router, conf.Servers[k].Name)
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400206 for rk := range conf.Routers {
sslobodr392ebd52019-01-18 12:41:49 -0500207 if conf.Routers[rk].Name == rPkg.Router && !found {
208 log.Debugf("Reference to router '%s' found for package '%s'", rPkg.Router, rPkg.Package)
209 conf.Servers[k].routers[rPkg.Package] = &conf.Routers[rk]
210 found = true
211 } else if conf.Routers[rk].Name == rPkg.Router && found {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400212 if _, ok := conf.Servers[k].routers[rPkg.Package]; !ok {
sslobodr392ebd52019-01-18 12:41:49 -0500213 log.Debugf("Reference to router '%s' found for package '%s'", rPkg.Router, rPkg.Package)
214 conf.Servers[k].routers[rPkg.Package] = &conf.Routers[rk]
215 } else {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400216 err := errors.New(fmt.Sprintf("Duplicate router '%s' defined for package '%s'", rPkg.Router, rPkg.Package))
sslobodr392ebd52019-01-18 12:41:49 -0500217 log.Error(err)
218 return err
219 }
220 }
221 }
222 if !found {
223 err := errors.New(fmt.Sprintf("Router %s for server %s not found in config", conf.Servers[k].Name, rPkg.Router))
224 log.Error(err)
225 return err
226 }
227 }
228 }
229
230 // Resolve backend references for the routers
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400231 for rk, rv := range conf.Routers {
232 for rtk, rtv := range rv.Routes {
Kent Hagerman1e9061e2019-05-21 16:01:21 -0400233 var found = false
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400234 log.Debugf("Resolving backend reference to %s from router %s", rtv.BackendCluster, rv.Name)
235 for bek, bev := range conf.BackendClusters {
sslobodr392ebd52019-01-18 12:41:49 -0500236 log.Debugf("Checking cluster %s", conf.BackendClusters[bek].Name)
237 if rtv.BackendCluster == bev.Name && !found {
238 conf.Routers[rk].Routes[rtk].backendCluster = &conf.BackendClusters[bek]
239 found = true
240 } else if rtv.BackendCluster == bev.Name && found {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400241 err := errors.New(fmt.Sprintf("Duplicate backend defined, %s", conf.BackendClusters[bek].Name))
sslobodr392ebd52019-01-18 12:41:49 -0500242 log.Error(err)
243 return err
244 }
245 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400246 if !found {
sslobodr392ebd52019-01-18 12:41:49 -0500247 err := errors.New(fmt.Sprintf("Backend %s for router %s not found in config",
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400248 rtv.BackendCluster, rv.Name))
sslobodr392ebd52019-01-18 12:41:49 -0500249 log.Error(err)
250 return err
251 }
252 }
253 }
254
sslobodr392ebd52019-01-18 12:41:49 -0500255 return nil
256}