Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [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 | */ |
| 16 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "github.com/opencord/voltha-api-server/internal/pkg/afrouter" |
Scott Baker | f579f13 | 2019-10-24 14:31:41 -0700 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/v2/pkg/probe" |
Scott Baker | f579f13 | 2019-10-24 14:31:41 -0700 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v2/pkg/version" |
Scott Baker | b6de7a5 | 2019-11-04 09:13:37 -0800 | [diff] [blame] | 25 | _ "github.com/opencord/voltha-protos/v2" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 26 | "google.golang.org/grpc/grpclog" |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 27 | "io/ioutil" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 28 | "os" |
| 29 | ) |
| 30 | |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame] | 31 | // startup arouter, return exit status as an integer |
| 32 | func startup() int { |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 33 | |
| 34 | conf, err := afrouter.ParseCmd() |
| 35 | if err != nil { |
| 36 | fmt.Printf("Error: %v\n", err) |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame] | 37 | return 1 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // Setup logging |
Hardik Windlass | 9f949c9 | 2019-10-10 06:39:24 +0000 | [diff] [blame] | 41 | if _, err := log.SetDefaultLogger(log.JSON, *conf.LogLevel, log.Fields{"instanceId": conf.InstanceID}); err != nil { |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 42 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame] | 43 | return 1 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 46 | defer func() { |
| 47 | err := log.CleanUp() |
| 48 | if err != nil { |
| 49 | // Let's not use the logger to print the error message, since the |
| 50 | // logger could be in a bad state. |
| 51 | fmt.Fprintf(os.Stderr, "Failed to cleanup logger: %v", err) |
| 52 | } |
| 53 | }() |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 54 | |
| 55 | if *conf.DisplayVersionOnly { |
| 56 | fmt.Println("VOLTHA API Server (afrouter)") |
| 57 | fmt.Println(version.VersionInfo.String(" ")) |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame] | 58 | return 0 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Parse the config file |
| 62 | err = conf.LoadConfig() |
| 63 | if err != nil { |
| 64 | log.Error(err) |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame] | 65 | return 1 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 66 | } |
| 67 | log.With(log.Fields{"config": *conf}).Debug("Configuration loaded") |
| 68 | |
| 69 | // Enable grpc logging |
| 70 | if *conf.GrpcLog { |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 71 | grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stderr, ioutil.Discard, ioutil.Discard)) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 72 | } |
| 73 | |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame] | 74 | /* |
| 75 | * Create and start the liveness and readiness container management probes. This |
| 76 | * is done in the main function so just in case the main starts multiple other |
| 77 | * objects there can be a single probe end point for the process. |
| 78 | */ |
| 79 | p := &probe.Probe{} |
| 80 | go p.ListenAndServe(fmt.Sprintf("%s:%d", conf.Api.ProbeHost, conf.Api.ProbePort)) |
| 81 | |
| 82 | p.RegisterService( |
| 83 | "affinity-router-proxy", |
| 84 | ) |
| 85 | |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 86 | // Install the signal and error handlers. |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 87 | err = afrouter.InitExitHandler() |
| 88 | if err != nil { |
| 89 | log.Errorf("Failed to initialize exit handler, exiting: %v", err) |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame] | 90 | return 1 |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 91 | } |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 92 | |
| 93 | // Create the affinity router proxy... |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame] | 94 | if ap, err := afrouter.NewArouterProxy(conf, p); err != nil { |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 95 | log.Errorf("Failed to create the arouter proxy, exiting:%v", err) |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame] | 96 | return 1 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 97 | // and start it. |
| 98 | // This function never returns unless an error |
| 99 | // occurs or a signal is caught. |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame] | 100 | } else if *conf.DryRun { |
| 101 | // Do nothing |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 102 | } else if err := ap.ListenAndServe(); err != nil { |
| 103 | log.Errorf("Exiting on error %v", err) |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame] | 104 | return 1 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame] | 107 | return 0 |
| 108 | } |
| 109 | |
| 110 | func main() { |
| 111 | status := startup() |
| 112 | if status != 0 { |
| 113 | os.Exit(status) |
| 114 | } |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 115 | } |