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" |
| 22 | "github.com/opencord/voltha-go/common/log" |
| 23 | "github.com/opencord/voltha-go/common/version" |
Scott Baker | b3a288a | 2019-10-02 14:57:29 -0700 | [diff] [blame] | 24 | _ "github.com/opencord/voltha-protos" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 25 | "google.golang.org/grpc/grpclog" |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 26 | "io/ioutil" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 27 | "os" |
| 28 | ) |
| 29 | |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame^] | 30 | // startup arouter, return exit status as an integer |
| 31 | func startup() int { |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 32 | |
| 33 | conf, err := afrouter.ParseCmd() |
| 34 | if err != nil { |
| 35 | fmt.Printf("Error: %v\n", err) |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame^] | 36 | return 1 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // Setup logging |
Hardik Windlass | 9f949c9 | 2019-10-10 06:39:24 +0000 | [diff] [blame] | 40 | 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] | 41 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame^] | 42 | return 1 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 45 | defer func() { |
| 46 | err := log.CleanUp() |
| 47 | if err != nil { |
| 48 | // Let's not use the logger to print the error message, since the |
| 49 | // logger could be in a bad state. |
| 50 | fmt.Fprintf(os.Stderr, "Failed to cleanup logger: %v", err) |
| 51 | } |
| 52 | }() |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 53 | |
| 54 | if *conf.DisplayVersionOnly { |
| 55 | fmt.Println("VOLTHA API Server (afrouter)") |
| 56 | fmt.Println(version.VersionInfo.String(" ")) |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame^] | 57 | return 0 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Parse the config file |
| 61 | err = conf.LoadConfig() |
| 62 | if err != nil { |
| 63 | log.Error(err) |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame^] | 64 | return 1 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 65 | } |
| 66 | log.With(log.Fields{"config": *conf}).Debug("Configuration loaded") |
| 67 | |
| 68 | // Enable grpc logging |
| 69 | if *conf.GrpcLog { |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 70 | grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stderr, ioutil.Discard, ioutil.Discard)) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Install the signal and error handlers. |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 74 | err = afrouter.InitExitHandler() |
| 75 | if err != nil { |
| 76 | log.Errorf("Failed to initialize exit handler, exiting: %v", err) |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame^] | 77 | return 1 |
Scott Baker | 4989fe9 | 2019-10-09 17:03:06 -0700 | [diff] [blame] | 78 | } |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 79 | |
| 80 | // Create the affinity router proxy... |
| 81 | if ap, err := afrouter.NewArouterProxy(conf); err != nil { |
| 82 | log.Errorf("Failed to create the arouter proxy, exiting:%v", err) |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame^] | 83 | return 1 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 84 | // and start it. |
| 85 | // This function never returns unless an error |
| 86 | // occurs or a signal is caught. |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame^] | 87 | } else if *conf.DryRun { |
| 88 | // Do nothing |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 89 | } else if err := ap.ListenAndServe(); err != nil { |
| 90 | log.Errorf("Exiting on error %v", err) |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame^] | 91 | return 1 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Scott Baker | edb0ae1 | 2019-10-22 08:55:12 -0700 | [diff] [blame^] | 94 | return 0 |
| 95 | } |
| 96 | |
| 97 | func main() { |
| 98 | status := startup() |
| 99 | if status != 0 { |
| 100 | os.Exit(status) |
| 101 | } |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 102 | } |