Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-present Open Networking Foundation |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | // Package envutils provides the env parsing utility functions |
| 17 | package envutils |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "os" |
| 22 | "strconv" |
| 23 | ) |
| 24 | |
| 25 | // common constants |
| 26 | const ( |
| 27 | // common environment variables |
| 28 | |
| 29 | KafkaAdapterHost = "KAFKA_ADAPTER_HOST" |
| 30 | KafkaAdapterPort = "KAFKA_ADAPTER_PORT" |
| 31 | KafkaClusterHost = "KAFKA_CLUSTER_HOST" |
| 32 | KafkaClusterPort = "KAFKA_CLUSTER_PORT" |
| 33 | KvStoreType = "KV_STORE_TYPE" |
| 34 | KvStoreTimeout = "KV_STORE_TIMEOUT" |
| 35 | KvStoreHost = "KV_STORE_HOST" |
| 36 | KvStorePort = "KV_STORE_PORT" |
| 37 | AdapterTopic = "ADAPTER_TOPIC" |
| 38 | CoreTopic = "CORE_TOPIC" |
| 39 | EventTopic = "EVENT_TOPIC" |
| 40 | LogLevel = "LOG_LEVEL" |
| 41 | OnuNumber = "ONU_NUMBER" |
| 42 | Banner = "BANNER" |
| 43 | DisplayVersionOnly = "DISPLAY_VERSION_ONLY" |
| 44 | ProbeHost = "PROBE_HOST" |
| 45 | ProbePort = "PROBE_PORT" |
| 46 | LiveProbeInterval = "LIVE_PROBE_INTERVAL" |
| 47 | NotLiveProbeInterval = "NOT_LIVE_PROBE_INTERVAL" |
| 48 | VolthaHost = "VOLTHA_HOST" |
| 49 | VolthaPort = "VOLTHA_PORT" |
| 50 | HostName = "HOST_NAME" |
| 51 | |
| 52 | // openolt adapter environment variables |
| 53 | |
| 54 | HeartbeatCheckInterval = "HEARTBEAT_CHECK_INTERVAL" |
| 55 | HeartbeatFailReportInterval = "HEARTBEAT_FAIL_REPORT_INTERVAL" |
| 56 | GrpcTimeoutInterval = "GRPC_TIMEOUT_INTERVAL" |
| 57 | |
| 58 | // rwcore environment variables |
| 59 | |
| 60 | RWCoreEndpoint = "RW_CORE_ENDPOINT" |
| 61 | GrpcHost = "GRPC_HOST" |
| 62 | GrpcPort = "GRPC_PORT" |
| 63 | AffinityRouterTopic = "AFFINITY_ROUTER_TOPIC" |
| 64 | InCompetingMode = "IN_COMPETING_MODE" |
| 65 | KVTxnKeyDelTime = "KV_TXN_KEY_DEL_TIME" |
| 66 | KVStoreDataPrefix = "KV_STORE_DATA_PREFIX" |
| 67 | LongRunningRequestTimeout = "LONG_RUNNING_REQ_TIMEOUT" |
| 68 | DefaultRequestTimeout = "DEFAULT_REQ_TIMEOUT" |
| 69 | DefaultCoreTimeout = "DEFAULT_CORE_TIMEOUT" |
| 70 | CoreBindingKey = "CORE_BINDING_KEY" |
| 71 | CorePairTopic = "CORE_PAIR_TOPIC" |
| 72 | MaxConnectionRetries = "MAX_CONNECTION_RETRIES" |
| 73 | ConnectionRetryInterval = "CONNECTION_RETRY_INTERVAL" |
| 74 | |
| 75 | // vgc environment variables |
| 76 | |
| 77 | DeviceListRefreshInterval = "DEVICE_LIST_REFRESH_INTERVAL" // in seconds |
| 78 | CPUProfile = "CPU_PROFILE" |
| 79 | MemProfile = "MEM_PROFILE" |
| 80 | VendorID = "VENDOR_ID" |
| 81 | DeviceSyncDuration = "DEVICE_SYNC_DURATION" |
| 82 | // openonu environment variables |
| 83 | |
| 84 | OmciPacketCapture = "SAVE_OMCI_PACKET_CAPTURE" |
| 85 | ) |
| 86 | |
| 87 | // ParseStringEnvVariable reads the environment variable and returns env as string |
| 88 | func ParseStringEnvVariable(envVarName string, defaultVal string) string { |
| 89 | envValue := os.Getenv(envVarName) |
| 90 | if envValue == "" { |
| 91 | fmt.Println("Environment variable " + envVarName + " undefined") |
| 92 | return defaultVal |
| 93 | } |
| 94 | return envValue |
| 95 | } |
| 96 | |
| 97 | // ParseIntEnvVariable reads the environment variable and returns env as int64 |
| 98 | func ParseIntEnvVariable(envVarName string, defaultVal int64) int64 { |
| 99 | envValue := os.Getenv(envVarName) |
| 100 | if envValue == "" { |
| 101 | fmt.Println("Environment variable "+envVarName+" undefined", envVarName) |
| 102 | return defaultVal |
| 103 | } |
| 104 | returnVal, err := strconv.Atoi(envValue) |
| 105 | if err != nil { |
| 106 | fmt.Println("Unable to convert string to integer environment variable") |
| 107 | return defaultVal |
| 108 | } |
| 109 | return int64(returnVal) |
| 110 | } |
| 111 | |
| 112 | // ParseBoolEnvVariable reads the environment variable and returns env as boolean |
| 113 | func ParseBoolEnvVariable(envVarName string, defaultVal bool) bool { |
| 114 | envValue := os.Getenv(envVarName) |
| 115 | if envValue == "" { |
| 116 | fmt.Println("Environment variable " + envVarName + " undefined") |
| 117 | return defaultVal |
| 118 | } |
| 119 | if envValue == "true" || envValue == "True" { |
| 120 | return true |
| 121 | } |
| 122 | return false |
| 123 | } |