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