sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 1 | /* |
| 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 | */ |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 16 | |
| 17 | package afrouter |
| 18 | |
| 19 | // Command line parameters and parsing |
| 20 | import ( |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 21 | "encoding/json" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 22 | "errors" |
| 23 | "flag" |
| 24 | "fmt" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/common/log" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 26 | "io/ioutil" |
| 27 | "os" |
| 28 | "path" |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func ParseCmd() (*Configuration, error) { |
| 32 | config := &Configuration{} |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 33 | cmdParse := flag.NewFlagSet(path.Base(os.Args[0]), flag.ContinueOnError) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 34 | 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. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 37 | config.DisplayVersionOnly = cmdParse.Bool("version", false, "Print version information and exit") |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 38 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 39 | err := cmdParse.Parse(os.Args[1:]) |
sslobodr | 3d587c6 | 2019-01-24 12:33:39 -0500 | [diff] [blame] | 40 | if err != nil { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 41 | //return err |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 42 | return nil, errors.New("Error parsing the command line") |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 43 | } |
| 44 | //if(!cmdParse.Parsed()) { |
| 45 | //} |
| 46 | return config, nil |
| 47 | } |
| 48 | |
| 49 | // Configuration file loading and parsing |
| 50 | type Configuration struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 51 | ConfigFile *string |
| 52 | LogLevel *int |
| 53 | GrpcLog *bool |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 54 | DisplayVersionOnly *bool |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 55 | 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 |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | type RouterConfig struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 65 | Name string `json:"name"` |
| 66 | ProtoService string `json:"service"` |
| 67 | ProtoPackage string `json:"package"` |
| 68 | Routes []RouteConfig `json:"routes"` |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | type RouteConfig struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 72 | Name string `json:"name"` |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 73 | Type routeType `json:"type"` |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 74 | ProtoFile string `json:"proto_descriptor"` |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 75 | Association associationType `json:"association"` |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 76 | 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 |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | type BindingConfig struct { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 86 | Type string `json:"type"` |
| 87 | Field string `json:"field"` |
| 88 | Method string `json:"method"` |
| 89 | Association associationType `json:"association"` |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | type OverrideConfig struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 93 | Methods []string `json:"methods"` |
| 94 | Method string `json:"method"` |
| 95 | RouteField string `json:"routing_field"` |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Backend configuration |
| 99 | |
| 100 | type BackendClusterConfig struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 101 | Name string `json:"name"` |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 102 | Backends []BackendConfig `json:"backends"` |
| 103 | } |
| 104 | |
| 105 | type BackendConfig struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 106 | Name string `json:"name"` |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 107 | Type backendType `json:"type"` |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 108 | Association AssociationConfig `json:"association"` |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 109 | Connections []ConnectionConfig `json:"connections"` |
| 110 | } |
| 111 | |
| 112 | type AssociationConfig struct { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 113 | Strategy associationStrategy `json:"strategy"` |
| 114 | Location associationLocation `json:"location"` |
| 115 | Field string `json:"field"` |
| 116 | Key string `json:"key"` |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | type ConnectionConfig struct { |
| 120 | Name string `json:"name"` |
| 121 | Addr string `json:"addr"` |
| 122 | Port string `json:"port"` |
| 123 | } |
| 124 | |
| 125 | // Server configuration |
| 126 | |
| 127 | type ServerConfig struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 128 | Name string `json:"name"` |
| 129 | Port uint `json:"port"` |
| 130 | Addr string `json:"address"` |
| 131 | Type string `json:"type"` |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 132 | Routers []RouterPackage `json:"routers"` |
| 133 | routers map[string]*RouterConfig |
| 134 | } |
| 135 | |
| 136 | type RouterPackage struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 137 | Router string `json:"router"` |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 138 | Package string `json:"package"` |
| 139 | } |
| 140 | |
| 141 | // Port configuration |
| 142 | type PortConfig struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 143 | GrpcPort uint `json:"grpcPort"` |
| 144 | StreamingGrpcPort uint `json:"streamingGrpcPort"` |
| 145 | TlsGrpcPort uint `json:"tlsGrpcPort"` |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 146 | TlsStreamingGrpcPort uint `json:"tlsStreamingGrpcPort"` |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 147 | ControlPort uint `json:"controlPort"` |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Server Certificate configuration |
| 151 | type ServerCertConfig struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 152 | 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 |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Client Certificate configuration |
| 158 | type ClientCertConfig struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 159 | 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 |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Api configuration |
| 165 | type ApiConfig struct { |
| 166 | Addr string `json:"address"` |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 167 | Port uint `json:"port"` |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 170 | func (conf *Configuration) LoadConfig() error { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 171 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 172 | configF, err := os.Open(*conf.ConfigFile) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 173 | 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 | |
sslobodr | 8e2ccb5 | 2019-02-05 09:21:47 -0500 | [diff] [blame] | 187 | if err := json.Unmarshal(configBytes, conf); err != nil { |
| 188 | log.Errorf("Unmarshaling of the configuratino file failed: %v", err) |
| 189 | return err |
| 190 | } |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 191 | |
| 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 Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 198 | log.Debug("Resolving references in the config file") |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 199 | for k := range conf.Servers { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 200 | //s.routers =make(map[string]*RouterConfig) |
| 201 | conf.Servers[k].routers = make(map[string]*RouterConfig) |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 202 | for _, rPkg := range conf.Servers[k].Routers { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 203 | var found = false |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 204 | // Locate the router "r" in the top lever Routers array |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 205 | log.Debugf("Resolving router reference to router '%s' from server '%s'", rPkg.Router, conf.Servers[k].Name) |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 206 | for rk := range conf.Routers { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 207 | 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 Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 212 | if _, ok := conf.Servers[k].routers[rPkg.Package]; !ok { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 213 | 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 Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 216 | err := errors.New(fmt.Sprintf("Duplicate router '%s' defined for package '%s'", rPkg.Router, rPkg.Package)) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 217 | 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 Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 231 | for rk, rv := range conf.Routers { |
| 232 | for rtk, rtv := range rv.Routes { |
Kent Hagerman | 1e9061e | 2019-05-21 16:01:21 -0400 | [diff] [blame] | 233 | var found = false |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 234 | log.Debugf("Resolving backend reference to %s from router %s", rtv.BackendCluster, rv.Name) |
| 235 | for bek, bev := range conf.BackendClusters { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 236 | 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 Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 241 | err := errors.New(fmt.Sprintf("Duplicate backend defined, %s", conf.BackendClusters[bek].Name)) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 242 | log.Error(err) |
| 243 | return err |
| 244 | } |
| 245 | } |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 246 | if !found { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 247 | err := errors.New(fmt.Sprintf("Backend %s for router %s not found in config", |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 248 | rtv.BackendCluster, rv.Name)) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 249 | log.Error(err) |
| 250 | return err |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 255 | return nil |
| 256 | } |