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 ( |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame] | 20 | "context" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 21 | "flag" |
| 22 | "fmt" |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-api-server/internal/pkg/afrouterd" |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v2/pkg/probe" |
Scott Baker | f579f13 | 2019-10-24 14:31:41 -0700 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v2/pkg/version" |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame] | 26 | "math" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 27 | "os" |
| 28 | "path" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 29 | |
Scott Baker | f579f13 | 2019-10-24 14:31:41 -0700 | [diff] [blame] | 30 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Scott Baker | b6de7a5 | 2019-11-04 09:13:37 -0800 | [diff] [blame] | 31 | pb "github.com/opencord/voltha-protos/v2/go/afrouter" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 34 | var ( |
| 35 | instanceID = afrouterd.GetStrEnv("HOSTNAME", "arouterd001") |
| 36 | afrouterApiAddress = afrouterd.GetStrEnv("AFROUTER_API_ADDRESS", "localhost:55554") |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame] | 37 | probeHost = afrouterd.GetStrEnv("PROBE_HOST", "") |
| 38 | probePort = afrouterd.GetIntEnv("PROBE_PORT", 0, math.MaxUint16, 8081) |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 39 | ) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 40 | |
| 41 | type Configuration struct { |
| 42 | DisplayVersionOnly *bool |
| 43 | } |
| 44 | |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 45 | func startup() int { |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 46 | 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 Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 52 | return 1 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | if *config.DisplayVersionOnly { |
| 56 | fmt.Println("VOLTHA API Server (afrouterd)") |
| 57 | fmt.Println(version.VersionInfo.String(" ")) |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 58 | return 0 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Set up logging |
Hardik Windlass | 9f949c9 | 2019-10-10 06:39:24 +0000 | [diff] [blame] | 62 | if _, err := log.SetDefaultLogger(log.JSON, 0, log.Fields{"instanceId": instanceID}); err != nil { |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 63 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 64 | } |
| 65 | |
| 66 | // Set up kubernetes api |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 67 | clientset := afrouterd.K8sClientSet() |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 68 | |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame] | 69 | 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 Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 77 | for { |
| 78 | // Connect to the affinity router |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 79 | conn, err := afrouterd.Connect(context.Background(), afrouterApiAddress) // This is a sidecar container so communicating over localhost |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 80 | if err != nil { |
| 81 | panic(err) |
| 82 | } |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame] | 83 | p.UpdateStatus("affinity-router", probe.ServiceStatusRunning) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 84 | |
| 85 | // monitor the connection status, end context if connection is lost |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame] | 86 | ctx := afrouterd.ConnectionActiveContext(conn, p) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 87 | |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame] | 88 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 89 | // 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") |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame] | 96 | doneCh, _ := afrouterd.StartDiscoveryMonitor(probeCtx, client) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 97 | |
| 98 | log.Info("Starting core monitoring") |
divyadesai | f117fc2 | 2019-11-04 06:32:01 +0000 | [diff] [blame] | 99 | afrouterd.CoreMonitor(probeCtx, client, clientset) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 100 | |
| 101 | //ensure the discovery monitor to quit |
| 102 | <-doneCh |
| 103 | |
| 104 | conn.Close() |
| 105 | } |
| 106 | } |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame] | 107 | |
| 108 | func main() { |
| 109 | status := startup() |
| 110 | if status != 0 { |
| 111 | os.Exit(status) |
| 112 | } |
| 113 | } |