blob: 941536275985ce70cd7ba4cab5d4126c824816b6 [file] [log] [blame]
Scott Bakere7144bc2019-10-01 14:16:47 -07001/*
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
17package main
18
19import (
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 Bakerb3a288a2019-10-02 14:57:29 -070024 _ "github.com/opencord/voltha-protos"
Scott Bakere7144bc2019-10-01 14:16:47 -070025 "google.golang.org/grpc/grpclog"
Scott Baker4989fe92019-10-09 17:03:06 -070026 "io/ioutil"
Scott Bakere7144bc2019-10-01 14:16:47 -070027 "os"
28)
29
Scott Bakeredb0ae12019-10-22 08:55:12 -070030// startup arouter, return exit status as an integer
31func startup() int {
Scott Bakere7144bc2019-10-01 14:16:47 -070032
33 conf, err := afrouter.ParseCmd()
34 if err != nil {
35 fmt.Printf("Error: %v\n", err)
Scott Bakeredb0ae12019-10-22 08:55:12 -070036 return 1
Scott Bakere7144bc2019-10-01 14:16:47 -070037 }
38
39 // Setup logging
Hardik Windlass9f949c92019-10-10 06:39:24 +000040 if _, err := log.SetDefaultLogger(log.JSON, *conf.LogLevel, log.Fields{"instanceId": conf.InstanceID}); err != nil {
Scott Bakere7144bc2019-10-01 14:16:47 -070041 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
Scott Bakeredb0ae12019-10-22 08:55:12 -070042 return 1
Scott Bakere7144bc2019-10-01 14:16:47 -070043 }
44
Scott Baker4989fe92019-10-09 17:03:06 -070045 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 Bakere7144bc2019-10-01 14:16:47 -070053
54 if *conf.DisplayVersionOnly {
55 fmt.Println("VOLTHA API Server (afrouter)")
56 fmt.Println(version.VersionInfo.String(" "))
Scott Bakeredb0ae12019-10-22 08:55:12 -070057 return 0
Scott Bakere7144bc2019-10-01 14:16:47 -070058 }
59
60 // Parse the config file
61 err = conf.LoadConfig()
62 if err != nil {
63 log.Error(err)
Scott Bakeredb0ae12019-10-22 08:55:12 -070064 return 1
Scott Bakere7144bc2019-10-01 14:16:47 -070065 }
66 log.With(log.Fields{"config": *conf}).Debug("Configuration loaded")
67
68 // Enable grpc logging
69 if *conf.GrpcLog {
Scott Baker4989fe92019-10-09 17:03:06 -070070 grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stderr, ioutil.Discard, ioutil.Discard))
Scott Bakere7144bc2019-10-01 14:16:47 -070071 }
72
73 // Install the signal and error handlers.
Scott Baker4989fe92019-10-09 17:03:06 -070074 err = afrouter.InitExitHandler()
75 if err != nil {
76 log.Errorf("Failed to initialize exit handler, exiting: %v", err)
Scott Bakeredb0ae12019-10-22 08:55:12 -070077 return 1
Scott Baker4989fe92019-10-09 17:03:06 -070078 }
Scott Bakere7144bc2019-10-01 14:16:47 -070079
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 Bakeredb0ae12019-10-22 08:55:12 -070083 return 1
Scott Bakere7144bc2019-10-01 14:16:47 -070084 // and start it.
85 // This function never returns unless an error
86 // occurs or a signal is caught.
Scott Bakeredb0ae12019-10-22 08:55:12 -070087 } else if *conf.DryRun {
88 // Do nothing
Scott Bakere7144bc2019-10-01 14:16:47 -070089 } else if err := ap.ListenAndServe(); err != nil {
90 log.Errorf("Exiting on error %v", err)
Scott Bakeredb0ae12019-10-22 08:55:12 -070091 return 1
Scott Bakere7144bc2019-10-01 14:16:47 -070092 }
93
Scott Bakeredb0ae12019-10-22 08:55:12 -070094 return 0
95}
96
97func main() {
98 status := startup()
99 if status != 0 {
100 os.Exit(status)
101 }
Scott Bakere7144bc2019-10-01 14:16:47 -0700102}