blob: 8aac57e2c2c700edac7a32888493f03944544147 [file] [log] [blame]
Brian O'Connor6a37ea92017-08-03 22:45:59 -07001// Copyright 2016 Open Networking Foundation
David K. Bainbridgedf9df632016-07-07 18:47:46 -07002//
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.
gunjan56e19d272016-07-07 14:23:26 -070014package main
15
16import (
David K. Bainbridge528b3182017-01-23 08:51:59 -080017 "flag"
gunjan56e19d272016-07-07 14:23:26 -070018 "fmt"
David K. Bainbridgec04fd552016-11-08 18:39:29 -080019 "net/http"
David K. Bainbridge528b3182017-01-23 08:51:59 -080020 "os"
David K. Bainbridgec04fd552016-11-08 18:39:29 -080021
22 "github.com/Sirupsen/logrus"
23
gunjan54fbb81c2016-07-09 03:31:27 -070024 "github.com/gorilla/mux"
25 "github.com/kelseyhightower/envconfig"
gunjan56e19d272016-07-07 14:23:26 -070026)
27
David K. Bainbridge528b3182017-01-23 08:51:59 -080028const appName = "CONFIGGEN"
29
gunjan54fbb81c2016-07-09 03:31:27 -070030type Config struct {
David K. Bainbridge528b3182017-01-23 08:51:59 -080031 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"`
gunjan54fbb81c2016-07-09 03:31:27 -070038
David K. Bainbridgec04fd552016-11-08 18:39:29 -080039 connect string
gunjan54fbb81c2016-07-09 03:31:27 -070040}
41
David K. Bainbridgec04fd552016-11-08 18:39:29 -080042var log = logrus.New()
David K. Bainbridge528b3182017-01-23 08:51:59 -080043var appFlags = flag.NewFlagSet("", flag.ContinueOnError)
gunjan54fbb81c2016-07-09 03:31:27 -070044
gunjan56e19d272016-07-07 14:23:26 -070045func main() {
gunjan54fbb81c2016-07-09 03:31:27 -070046
David K. Bainbridge528b3182017-01-23 08:51:59 -080047 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)
gunjan54fbb81c2016-07-09 03:31:27 -070060 if err != nil {
61 log.Fatalf("[ERROR] Unable to parse configuration options : %s", err)
62 }
63
David K. Bainbridge528b3182017-01-23 08:51:59 -080064 switch config.LogFormat {
David K. Bainbridgec04fd552016-11-08 18:39:29 -080065 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. Bainbridge528b3182017-01-23 08:51:59 -080074 level, err := logrus.ParseLevel(config.LogLevel)
David K. Bainbridgec04fd552016-11-08 18:39:29 -080075 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. Bainbridge528b3182017-01-23 08:51:59 -080088 config.Listen, config.Port, config.Controller,
89 config.Username, config.Password,
90 config.LogLevel, config.LogFormat)
David K. Bainbridgec04fd552016-11-08 18:39:29 -080091
gunjan54fbb81c2016-07-09 03:31:27 -070092 router := mux.NewRouter()
David K. Bainbridge528b3182017-01-23 08:51:59 -080093 router.HandleFunc("/config/", config.configGenHandler).Methods("POST")
gunjan54fbb81c2016-07-09 03:31:27 -070094 http.Handle("/", router)
95
David K. Bainbridge528b3182017-01-23 08:51:59 -080096 config.connect = fmt.Sprintf(config.Controller, config.Username, config.Password)
gunjan54fbb81c2016-07-09 03:31:27 -070097
David K. Bainbridge528b3182017-01-23 08:51:59 -080098 panic(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil))
gunjan56e19d272016-07-07 14:23:26 -070099}