Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [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 invokes the application |
| 18 | package main |
| 19 | |
| 20 | import ( |
| 21 | "fmt" |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 22 | "os" |
| 23 | "os/signal" |
Orhan Kupusoglu | 66b00d8 | 2020-03-13 12:06:33 +0300 | [diff] [blame] | 24 | "strconv" |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 25 | "syscall" |
| 26 | "time" |
| 27 | |
Orhan Kupusoglu | 66b00d8 | 2020-03-13 12:06:33 +0300 | [diff] [blame] | 28 | "github.com/opencord/openolt-scale-tester/core" |
| 29 | |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 30 | "github.com/opencord/openolt-scale-tester/config" |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame^] | 31 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 32 | ) |
| 33 | |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame^] | 34 | var logger log.CLogger |
| 35 | |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 36 | func init() { |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame^] | 37 | // Setup this package so that it's log level can be modified at run time |
| 38 | var err error |
| 39 | logger, err = log.RegisterPackage(log.JSON, log.DebugLevel, log.Fields{}) |
| 40 | if err != nil { |
| 41 | panic(err) |
| 42 | } |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | const ( |
| 46 | // DefaultLivelinessCheck to check the liveliness |
| 47 | DefaultLivelinessCheck = 30 * time.Second |
| 48 | // DefaultTimeout to close the connection |
| 49 | DefaultTimeout = 10 |
| 50 | ) |
| 51 | |
| 52 | type OpenOltScaleTester struct { |
| 53 | openOltManager *core.OpenOltManager |
| 54 | } |
| 55 | |
| 56 | func waitForExit() int { |
| 57 | signalChannel := make(chan os.Signal, 1) |
| 58 | signal.Notify(signalChannel, |
| 59 | syscall.SIGHUP, |
| 60 | syscall.SIGINT, |
| 61 | syscall.SIGTERM, |
| 62 | syscall.SIGQUIT) |
| 63 | |
| 64 | exitChannel := make(chan int) |
| 65 | |
| 66 | go func() { |
| 67 | s := <-signalChannel |
| 68 | switch s { |
| 69 | case syscall.SIGHUP, |
| 70 | syscall.SIGINT, |
| 71 | syscall.SIGTERM, |
| 72 | syscall.SIGQUIT: |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame^] | 73 | logger.Infow(nil, "closing-signal-received", log.Fields{"signal": s}) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 74 | exitChannel <- 0 |
| 75 | default: |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame^] | 76 | logger.Infow(nil, "unexpected-signal-received", log.Fields{"signal": s}) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 77 | exitChannel <- 1 |
| 78 | } |
| 79 | }() |
| 80 | |
| 81 | code := <-exitChannel |
| 82 | return code |
| 83 | } |
| 84 | |
| 85 | func printBanner() { |
| 86 | fmt.Println("TODO: Print banner here") |
| 87 | } |
| 88 | |
| 89 | func main() { |
| 90 | start := time.Now() |
| 91 | sc := OpenOltScaleTester{} |
| 92 | cf := config.NewOpenOltScaleTesterConfig() |
| 93 | cf.ParseCommandArguments() |
Orhan Kupusoglu | 66b00d8 | 2020-03-13 12:06:33 +0300 | [diff] [blame] | 94 | |
| 95 | cf.OpenOltAgentAddress = cf.OpenOltAgentIP + ":" + strconv.FormatUint(uint64(cf.OpenOltAgentPort), 10) |
| 96 | |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 97 | // Generate TP ID List from TP ID string parsed from command line for a given subscriber |
| 98 | cf.TpIDList = config.GetTpIDList(cf.TpIDsString) |
| 99 | sc.openOltManager = core.NewOpenOltManager(cf.OpenOltAgentAddress) |
| 100 | |
| 101 | printBanner() |
| 102 | |
| 103 | // Setup logging |
| 104 | |
| 105 | // Setup default logger - applies for packages that do not have specific logger set |
| 106 | if _, err := log.SetDefaultLogger(log.JSON, 0, log.Fields{"instanceId": 0}); err != nil { |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame^] | 107 | logger.With(log.Fields{"error": err}).Fatal(nil, "Cannot setup logging") |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Update all loggers (provisioned via init) with a common field |
| 111 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": 0}); err != nil { |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame^] | 112 | logger.With(log.Fields{"error": err}).Fatal(nil, "Cannot setup logging") |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 113 | } |
| 114 | |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame^] | 115 | log.SetPackageLogLevel("github.com/opencord/voltha-lib-go/v4/pkg/adapters/common", log.DebugLevel) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 116 | |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame^] | 117 | logger.Infow(nil, "config", log.Fields{"config": *cf}) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 118 | |
| 119 | go sc.openOltManager.Start(cf) |
| 120 | |
| 121 | code := waitForExit() |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame^] | 122 | logger.Infow(nil, "received-a-closing-signal", log.Fields{"code": code}) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 123 | |
| 124 | elapsed := time.Since(start) |
Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [diff] [blame^] | 125 | logger.Infow(nil, "run-time", log.Fields{"instanceId": 0, "time": elapsed / time.Second}) |
Girish Gowdra | 6450343 | 2020-01-07 10:59:10 +0530 | [diff] [blame] | 126 | } |