David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Laboratory |
| 2 | // |
| 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. |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 17 | "flag" |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 18 | "fmt" |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 19 | "net/http" |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 20 | "os" |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 21 | |
| 22 | "github.com/Sirupsen/logrus" |
| 23 | |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 24 | "github.com/gorilla/mux" |
| 25 | "github.com/kelseyhightower/envconfig" |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 28 | const appName = "CONFIGGEN" |
| 29 | |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 30 | type Config struct { |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 31 | Port int `default:"1337" desc:"port on which to listen for requests"` |
| 32 | Listen string `default:"0.0.0.0" desc:"IP address on which to listen for requests"` |
| 33 | Controller string `default:"http://%s:%s@127.0.0.1:8181" desc:"connection string with which to connect to ONOS"` |
| 34 | Username string `default:"karaf" desc:"username with which to connect to ONOS"` |
| 35 | Password string `default:"karaf" desc:"password with which to connect to ONOS"` |
| 36 | LogLevel string `default:"warning" envconfig:"LOG_LEVEL" desc:"detail level for logging"` |
| 37 | LogFormat string `default:"text" envconfig:"LOG_FORMAT" desc:"log output format, text or json"` |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 38 | |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 39 | connect string |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 40 | } |
| 41 | |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 42 | var log = logrus.New() |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 43 | var appFlags = flag.NewFlagSet("", flag.ContinueOnError) |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 44 | |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 45 | func main() { |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 46 | |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 47 | config := Config{} |
| 48 | appFlags.Usage = func() { |
| 49 | envconfig.Usage(appName, &config) |
| 50 | } |
| 51 | if err := appFlags.Parse(os.Args[1:]); err != nil { |
| 52 | if err != flag.ErrHelp { |
| 53 | os.Exit(1) |
| 54 | } else { |
| 55 | return |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | err := envconfig.Process("CONFIGGEN", &config) |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 60 | if err != nil { |
| 61 | log.Fatalf("[ERROR] Unable to parse configuration options : %s", err) |
| 62 | } |
| 63 | |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 64 | switch config.LogFormat { |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 65 | case "json": |
| 66 | log.Formatter = &logrus.JSONFormatter{} |
| 67 | default: |
| 68 | log.Formatter = &logrus.TextFormatter{ |
| 69 | FullTimestamp: true, |
| 70 | ForceColors: true, |
| 71 | } |
| 72 | } |
| 73 | |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 74 | level, err := logrus.ParseLevel(config.LogLevel) |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 75 | if err != nil { |
| 76 | level = logrus.WarnLevel |
| 77 | } |
| 78 | log.Level = level |
| 79 | |
| 80 | log.Infof(`Configuration: |
| 81 | LISTEN: %s |
| 82 | PORT: %d |
| 83 | CONTROLLER: %s |
| 84 | USERNAME: %s |
| 85 | PASSWORD: %s |
| 86 | LOG_LEVEL: %s |
| 87 | LOG_FORMAT: %s`, |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 88 | config.Listen, config.Port, config.Controller, |
| 89 | config.Username, config.Password, |
| 90 | config.LogLevel, config.LogFormat) |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 91 | |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 92 | router := mux.NewRouter() |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 93 | router.HandleFunc("/config/", config.configGenHandler).Methods("POST") |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 94 | http.Handle("/", router) |
| 95 | |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 96 | config.connect = fmt.Sprintf(config.Controller, config.Username, config.Password) |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 97 | |
David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 98 | panic(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)) |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 99 | } |