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 ( |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 20 | "flag" |
| 21 | "fmt" |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame^] | 22 | "github.com/opencord/voltha-api-server/internal/pkg/afrouterd" |
| 23 | "github.com/opencord/voltha-go/common/version" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 24 | "os" |
| 25 | "path" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 26 | |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 27 | "github.com/opencord/voltha-go/common/log" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 28 | pb "github.com/opencord/voltha-protos/go/afrouter" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 29 | "golang.org/x/net/context" |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame^] | 32 | var ( |
| 33 | instanceID = afrouterd.GetStrEnv("HOSTNAME", "arouterd001") |
| 34 | afrouterApiAddress = afrouterd.GetStrEnv("AFROUTER_API_ADDRESS", "localhost:55554") |
| 35 | ) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 36 | |
| 37 | type Configuration struct { |
| 38 | DisplayVersionOnly *bool |
| 39 | } |
| 40 | |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame^] | 41 | func startup() int { |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 42 | 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 Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame^] | 48 | return 1 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | if *config.DisplayVersionOnly { |
| 52 | fmt.Println("VOLTHA API Server (afrouterd)") |
| 53 | fmt.Println(version.VersionInfo.String(" ")) |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame^] | 54 | return 0 |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Set up logging |
Hardik Windlass | 9f949c9 | 2019-10-10 06:39:24 +0000 | [diff] [blame] | 58 | 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] | 59 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 60 | } |
| 61 | |
| 62 | // Set up kubernetes api |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame^] | 63 | clientset := afrouterd.K8sClientSet() |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 64 | |
| 65 | for { |
| 66 | // Connect to the affinity router |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame^] | 67 | 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] | 68 | if err != nil { |
| 69 | panic(err) |
| 70 | } |
| 71 | |
| 72 | // monitor the connection status, end context if connection is lost |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame^] | 73 | ctx := afrouterd.ConnectionActiveContext(conn) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 74 | |
| 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 Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame^] | 82 | doneCh, _ := afrouterd.StartDiscoveryMonitor(ctx, client) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 83 | |
| 84 | log.Info("Starting core monitoring") |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame^] | 85 | afrouterd.CoreMonitor(ctx, client, clientset) |
Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 86 | |
| 87 | //ensure the discovery monitor to quit |
| 88 | <-doneCh |
| 89 | |
| 90 | conn.Close() |
| 91 | } |
| 92 | } |
Scott Baker | e702d12 | 2019-10-22 11:54:12 -0700 | [diff] [blame^] | 93 | |
| 94 | func main() { |
| 95 | status := startup() |
| 96 | if status != 0 { |
| 97 | os.Exit(status) |
| 98 | } |
| 99 | } |