blob: ac3b3e3163e1d81357a6468a02604c898803c046 [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 (
Scott Bakere7144bc2019-10-01 14:16:47 -070020 "flag"
21 "fmt"
Scott Bakere702d122019-10-22 11:54:12 -070022 "github.com/opencord/voltha-api-server/internal/pkg/afrouterd"
Scott Bakerf579f132019-10-24 14:31:41 -070023 "github.com/opencord/voltha-lib-go/v2/pkg/version"
Scott Bakere7144bc2019-10-01 14:16:47 -070024 "os"
25 "path"
Scott Bakere7144bc2019-10-01 14:16:47 -070026
Scott Bakerf579f132019-10-24 14:31:41 -070027 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Scott Bakere7144bc2019-10-01 14:16:47 -070028 pb "github.com/opencord/voltha-protos/go/afrouter"
Scott Bakere7144bc2019-10-01 14:16:47 -070029 "golang.org/x/net/context"
Scott Bakere7144bc2019-10-01 14:16:47 -070030)
31
Scott Bakere702d122019-10-22 11:54:12 -070032var (
33 instanceID = afrouterd.GetStrEnv("HOSTNAME", "arouterd001")
34 afrouterApiAddress = afrouterd.GetStrEnv("AFROUTER_API_ADDRESS", "localhost:55554")
35)
Scott Bakere7144bc2019-10-01 14:16:47 -070036
37type Configuration struct {
38 DisplayVersionOnly *bool
39}
40
Scott Bakere702d122019-10-22 11:54:12 -070041func startup() int {
Scott Bakere7144bc2019-10-01 14:16:47 -070042 config := &Configuration{}
43 cmdParse := flag.NewFlagSet(path.Base(os.Args[0]), flag.ContinueOnError)
44 config.DisplayVersionOnly = cmdParse.Bool("version", false, "Print version information and exit")
45
46 if err := cmdParse.Parse(os.Args[1:]); err != nil {
47 fmt.Printf("Error: %v\n", err)
Scott Bakere702d122019-10-22 11:54:12 -070048 return 1
Scott Bakere7144bc2019-10-01 14:16:47 -070049 }
50
51 if *config.DisplayVersionOnly {
52 fmt.Println("VOLTHA API Server (afrouterd)")
53 fmt.Println(version.VersionInfo.String(" "))
Scott Bakere702d122019-10-22 11:54:12 -070054 return 0
Scott Bakere7144bc2019-10-01 14:16:47 -070055 }
56
57 // Set up logging
Hardik Windlass9f949c92019-10-10 06:39:24 +000058 if _, err := log.SetDefaultLogger(log.JSON, 0, log.Fields{"instanceId": instanceID}); err != nil {
Scott Bakere7144bc2019-10-01 14:16:47 -070059 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
60 }
61
62 // Set up kubernetes api
Scott Bakere702d122019-10-22 11:54:12 -070063 clientset := afrouterd.K8sClientSet()
Scott Bakere7144bc2019-10-01 14:16:47 -070064
65 for {
66 // Connect to the affinity router
Scott Bakere702d122019-10-22 11:54:12 -070067 conn, err := afrouterd.Connect(context.Background(), afrouterApiAddress) // This is a sidecar container so communicating over localhost
Scott Bakere7144bc2019-10-01 14:16:47 -070068 if err != nil {
69 panic(err)
70 }
71
72 // monitor the connection status, end context if connection is lost
Scott Bakere702d122019-10-22 11:54:12 -070073 ctx := afrouterd.ConnectionActiveContext(conn)
Scott Bakere7144bc2019-10-01 14:16:47 -070074
75 // set up the client
76 client := pb.NewConfigurationClient(conn)
77
78 // start the discovery monitor and core monitor
79 // these two processes do the majority of the work
80
81 log.Info("Starting discovery monitoring")
Scott Bakere702d122019-10-22 11:54:12 -070082 doneCh, _ := afrouterd.StartDiscoveryMonitor(ctx, client)
Scott Bakere7144bc2019-10-01 14:16:47 -070083
84 log.Info("Starting core monitoring")
Scott Bakere702d122019-10-22 11:54:12 -070085 afrouterd.CoreMonitor(ctx, client, clientset)
Scott Bakere7144bc2019-10-01 14:16:47 -070086
87 //ensure the discovery monitor to quit
88 <-doneCh
89
90 conn.Close()
91 }
92}
Scott Bakere702d122019-10-22 11:54:12 -070093
94func main() {
95 status := startup()
96 if status != 0 {
97 os.Exit(status)
98 }
99}