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