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