blob: 73f8782a67c11cd4389256f6856ea38dfa189eb1 [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"
Scott Bakerf579f132019-10-24 14:31:41 -070022 "github.com/opencord/voltha-lib-go/v2/pkg/log"
divyadesaif117fc22019-11-04 06:32:01 +000023 "github.com/opencord/voltha-lib-go/v2/pkg/probe"
Scott Bakerf579f132019-10-24 14:31:41 -070024 "github.com/opencord/voltha-lib-go/v2/pkg/version"
Scott Bakerb6de7a52019-11-04 09:13:37 -080025 _ "github.com/opencord/voltha-protos/v2"
Scott Bakere7144bc2019-10-01 14:16:47 -070026 "google.golang.org/grpc/grpclog"
Scott Baker4989fe92019-10-09 17:03:06 -070027 "io/ioutil"
Scott Bakere7144bc2019-10-01 14:16:47 -070028 "os"
29)
30
Scott Bakeredb0ae12019-10-22 08:55:12 -070031// startup arouter, return exit status as an integer
32func startup() int {
Scott Bakere7144bc2019-10-01 14:16:47 -070033
34 conf, err := afrouter.ParseCmd()
35 if err != nil {
36 fmt.Printf("Error: %v\n", err)
Scott Bakeredb0ae12019-10-22 08:55:12 -070037 return 1
Scott Bakere7144bc2019-10-01 14:16:47 -070038 }
39
40 // Setup logging
Hardik Windlass9f949c92019-10-10 06:39:24 +000041 if _, err := log.SetDefaultLogger(log.JSON, *conf.LogLevel, log.Fields{"instanceId": conf.InstanceID}); err != nil {
Scott Bakere7144bc2019-10-01 14:16:47 -070042 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
Scott Bakeredb0ae12019-10-22 08:55:12 -070043 return 1
Scott Bakere7144bc2019-10-01 14:16:47 -070044 }
45
Scott Baker4989fe92019-10-09 17:03:06 -070046 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 Bakere7144bc2019-10-01 14:16:47 -070054
55 if *conf.DisplayVersionOnly {
56 fmt.Println("VOLTHA API Server (afrouter)")
57 fmt.Println(version.VersionInfo.String(" "))
Scott Bakeredb0ae12019-10-22 08:55:12 -070058 return 0
Scott Bakere7144bc2019-10-01 14:16:47 -070059 }
60
61 // Parse the config file
62 err = conf.LoadConfig()
63 if err != nil {
64 log.Error(err)
Scott Bakeredb0ae12019-10-22 08:55:12 -070065 return 1
Scott Bakere7144bc2019-10-01 14:16:47 -070066 }
67 log.With(log.Fields{"config": *conf}).Debug("Configuration loaded")
68
69 // Enable grpc logging
70 if *conf.GrpcLog {
Scott Baker4989fe92019-10-09 17:03:06 -070071 grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stderr, ioutil.Discard, ioutil.Discard))
Scott Bakere7144bc2019-10-01 14:16:47 -070072 }
73
divyadesaif117fc22019-11-04 06:32:01 +000074 /*
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 Bakere7144bc2019-10-01 14:16:47 -070086 // Install the signal and error handlers.
Scott Baker4989fe92019-10-09 17:03:06 -070087 err = afrouter.InitExitHandler()
88 if err != nil {
89 log.Errorf("Failed to initialize exit handler, exiting: %v", err)
Scott Bakeredb0ae12019-10-22 08:55:12 -070090 return 1
Scott Baker4989fe92019-10-09 17:03:06 -070091 }
Scott Bakere7144bc2019-10-01 14:16:47 -070092
93 // Create the affinity router proxy...
divyadesaif117fc22019-11-04 06:32:01 +000094 if ap, err := afrouter.NewArouterProxy(conf, p); err != nil {
Scott Bakere7144bc2019-10-01 14:16:47 -070095 log.Errorf("Failed to create the arouter proxy, exiting:%v", err)
Scott Bakeredb0ae12019-10-22 08:55:12 -070096 return 1
Scott Bakere7144bc2019-10-01 14:16:47 -070097 // and start it.
98 // This function never returns unless an error
99 // occurs or a signal is caught.
Scott Bakeredb0ae12019-10-22 08:55:12 -0700100 } else if *conf.DryRun {
101 // Do nothing
Scott Bakere7144bc2019-10-01 14:16:47 -0700102 } else if err := ap.ListenAndServe(); err != nil {
103 log.Errorf("Exiting on error %v", err)
Scott Bakeredb0ae12019-10-22 08:55:12 -0700104 return 1
Scott Bakere7144bc2019-10-01 14:16:47 -0700105 }
106
Scott Bakeredb0ae12019-10-22 08:55:12 -0700107 return 0
108}
109
110func main() {
111 status := startup()
112 if status != 0 {
113 os.Exit(status)
114 }
Scott Bakere7144bc2019-10-01 14:16:47 -0700115}