blob: 90448bbafcccf3ee032de0bd2206f00dfe378055 [file] [log] [blame]
Matteo Scandolo8df63df2019-09-12 10:34:32 -07001/*
Joey Armstrong3881b732022-12-27 07:55:37 -05002 * Portions Copyright 2019-2023 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolo8df63df2019-09-12 10:34:32 -07003 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package config
19
20import (
Matteo Scandolo38305492019-10-11 11:36:00 -070021 "fmt"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070022 log "github.com/sirupsen/logrus"
Matteo Scandolo38305492019-10-11 11:36:00 -070023 "gopkg.in/yaml.v2"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070024 "io/ioutil"
25 "net"
26 "os"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070027 "time"
Matteo Scandolo8df63df2019-09-12 10:34:32 -070028)
29
30var (
Matteo Scandolo38305492019-10-11 11:36:00 -070031 Version string
32 BuildTime string
33 CommitHash string
34 GitStatus string
Matteo Scandolo8df63df2019-09-12 10:34:32 -070035)
36
37var GlobalOptions struct {
Matteo Scandolo38305492019-10-11 11:36:00 -070038 Config string `short:"c" long:"config" env:"BBSIMCTL_CONFIG" value-name:"FILE" default:"" description:"Location of client config file"`
39 Server string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of XOS"`
Matteo Scandolo8df63df2019-09-12 10:34:32 -070040 //Protoset string `long:"protoset" value-name:"FILENAME" description:"Load protobuf definitions from protoset instead of reflection api"`
Matteo Scandolo38305492019-10-11 11:36:00 -070041 Debug bool `short:"d" long:"debug" description:"Enable debug mode"`
Matteo Scandolo8df63df2019-09-12 10:34:32 -070042}
43
44type GrpcConfigSpec struct {
45 Timeout time.Duration `yaml:"timeout"`
46}
47
48type GlobalConfigSpec struct {
Matteo Scandolo38305492019-10-11 11:36:00 -070049 Server string `yaml:"server"`
50 Grpc GrpcConfigSpec
Matteo Scandolo8df63df2019-09-12 10:34:32 -070051}
52
53var GlobalConfig = GlobalConfigSpec{
54 Server: "localhost:50070",
55 Grpc: GrpcConfigSpec{
56 Timeout: time.Second * 10,
57 },
58}
59
hkouser24361d42020-12-14 19:21:47 +053060var DmiConfig = GlobalConfigSpec{
61 Server: "localhost:50075",
62 Grpc: GrpcConfigSpec{
63 Timeout: time.Second * 10,
64 },
65}
66
Matteo Scandolo8df63df2019-09-12 10:34:32 -070067func ProcessGlobalOptions() {
68 if len(GlobalOptions.Config) == 0 {
69 home, err := os.UserHomeDir()
70 if err != nil {
71 log.Printf("Unable to discover the users home directory: %s\n", err)
72 }
73 GlobalOptions.Config = fmt.Sprintf("%s/.bbsim/config", home)
74 }
75
76 info, err := os.Stat(GlobalOptions.Config)
77 if err == nil && !info.IsDir() {
78 configFile, err := ioutil.ReadFile(GlobalOptions.Config)
79 if err != nil {
80 log.Printf("configFile.Get err #%v ", err)
81 }
82 err = yaml.Unmarshal(configFile, &GlobalConfig)
83 if err != nil {
84 log.Fatalf("Unmarshal: %v", err)
85 }
86 }
87
88 // Override from environment
89 // in particualr, for passing env vars via `go test`
90 env_server, present := os.LookupEnv("BBSIMCTL_SERVER")
91 if present {
92 GlobalConfig.Server = env_server
93 }
94
95 // Override from command line
96 if GlobalOptions.Server != "" {
97 GlobalConfig.Server = GlobalOptions.Server
98 }
99
Matteo Scandolo8df63df2019-09-12 10:34:32 -0700100 // Generate error messages for required settings
101 if GlobalConfig.Server == "" {
102 log.Fatal("Server is not set. Please update config file or use the -s option")
103 }
104
105 //Try to resolve hostname if provided for the server
106 if host, port, err := net.SplitHostPort(GlobalConfig.Server); err == nil {
107 if addrs, err := net.LookupHost(host); err == nil {
108 GlobalConfig.Server = net.JoinHostPort(addrs[0], port)
109 }
110 }
Matteo Scandolo38305492019-10-11 11:36:00 -0700111}