blob: 5452b9774dafcd41cd9c035a022376ee2db89b6e [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 (
divyadesaif117fc22019-11-04 06:32:01 +000020 "context"
Scott Bakere7144bc2019-10-01 14:16:47 -070021 "flag"
22 "fmt"
Scott Bakere702d122019-10-22 11:54:12 -070023 "github.com/opencord/voltha-api-server/internal/pkg/afrouterd"
divyadesaif117fc22019-11-04 06:32:01 +000024 "github.com/opencord/voltha-lib-go/v2/pkg/probe"
Scott Bakerf579f132019-10-24 14:31:41 -070025 "github.com/opencord/voltha-lib-go/v2/pkg/version"
divyadesaif117fc22019-11-04 06:32:01 +000026 "math"
Scott Bakere7144bc2019-10-01 14:16:47 -070027 "os"
28 "path"
Scott Bakere7144bc2019-10-01 14:16:47 -070029
Scott Bakerf579f132019-10-24 14:31:41 -070030 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Scott Bakerb6de7a52019-11-04 09:13:37 -080031 pb "github.com/opencord/voltha-protos/v2/go/afrouter"
Scott Bakere7144bc2019-10-01 14:16:47 -070032)
33
Scott Bakere702d122019-10-22 11:54:12 -070034var (
35 instanceID = afrouterd.GetStrEnv("HOSTNAME", "arouterd001")
36 afrouterApiAddress = afrouterd.GetStrEnv("AFROUTER_API_ADDRESS", "localhost:55554")
divyadesaif117fc22019-11-04 06:32:01 +000037 probeHost = afrouterd.GetStrEnv("PROBE_HOST", "")
38 probePort = afrouterd.GetIntEnv("PROBE_PORT", 0, math.MaxUint16, 8081)
Scott Bakere702d122019-10-22 11:54:12 -070039)
Scott Bakere7144bc2019-10-01 14:16:47 -070040
41type Configuration struct {
42 DisplayVersionOnly *bool
43}
44
Scott Bakere702d122019-10-22 11:54:12 -070045func startup() int {
Scott Bakere7144bc2019-10-01 14:16:47 -070046 config := &Configuration{}
47 cmdParse := flag.NewFlagSet(path.Base(os.Args[0]), flag.ContinueOnError)
48 config.DisplayVersionOnly = cmdParse.Bool("version", false, "Print version information and exit")
49
50 if err := cmdParse.Parse(os.Args[1:]); err != nil {
51 fmt.Printf("Error: %v\n", err)
Scott Bakere702d122019-10-22 11:54:12 -070052 return 1
Scott Bakere7144bc2019-10-01 14:16:47 -070053 }
54
55 if *config.DisplayVersionOnly {
56 fmt.Println("VOLTHA API Server (afrouterd)")
57 fmt.Println(version.VersionInfo.String(" "))
Scott Bakere702d122019-10-22 11:54:12 -070058 return 0
Scott Bakere7144bc2019-10-01 14:16:47 -070059 }
60
61 // Set up logging
Hardik Windlass9f949c92019-10-10 06:39:24 +000062 if _, err := log.SetDefaultLogger(log.JSON, 0, log.Fields{"instanceId": instanceID}); err != nil {
Scott Bakere7144bc2019-10-01 14:16:47 -070063 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
64 }
65
66 // Set up kubernetes api
Scott Bakere702d122019-10-22 11:54:12 -070067 clientset := afrouterd.K8sClientSet()
Scott Bakere7144bc2019-10-01 14:16:47 -070068
divyadesaif117fc22019-11-04 06:32:01 +000069 p := &probe.Probe{}
70 go p.ListenAndServe(fmt.Sprintf("%s:%d", probeHost, probePort))
71
72 p.RegisterService(
73 "affinity-router",
74 "message-bus",
75 )
76
Scott Bakere7144bc2019-10-01 14:16:47 -070077 for {
78 // Connect to the affinity router
Scott Bakere702d122019-10-22 11:54:12 -070079 conn, err := afrouterd.Connect(context.Background(), afrouterApiAddress) // This is a sidecar container so communicating over localhost
Scott Bakere7144bc2019-10-01 14:16:47 -070080 if err != nil {
81 panic(err)
82 }
divyadesaif117fc22019-11-04 06:32:01 +000083 p.UpdateStatus("affinity-router", probe.ServiceStatusRunning)
Scott Bakere7144bc2019-10-01 14:16:47 -070084
85 // monitor the connection status, end context if connection is lost
divyadesaif117fc22019-11-04 06:32:01 +000086 ctx := afrouterd.ConnectionActiveContext(conn, p)
Scott Bakere7144bc2019-10-01 14:16:47 -070087
divyadesaif117fc22019-11-04 06:32:01 +000088 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p)
Scott Bakere7144bc2019-10-01 14:16:47 -070089 // set up the client
90 client := pb.NewConfigurationClient(conn)
91
92 // start the discovery monitor and core monitor
93 // these two processes do the majority of the work
94
95 log.Info("Starting discovery monitoring")
divyadesaif117fc22019-11-04 06:32:01 +000096 doneCh, _ := afrouterd.StartDiscoveryMonitor(probeCtx, client)
Scott Bakere7144bc2019-10-01 14:16:47 -070097
98 log.Info("Starting core monitoring")
divyadesaif117fc22019-11-04 06:32:01 +000099 afrouterd.CoreMonitor(probeCtx, client, clientset)
Scott Bakere7144bc2019-10-01 14:16:47 -0700100
101 //ensure the discovery monitor to quit
102 <-doneCh
103
104 conn.Close()
105 }
106}
Scott Bakere702d122019-10-22 11:54:12 -0700107
108func main() {
109 status := startup()
110 if status != 0 {
111 os.Exit(status)
112 }
113}