Fixing spaces
ONOS IP as HTTP POST param and updated README

Change-Id: I1fc81858cc70fff83c9501dda048ed0754e9b872
diff --git a/config-generator/README.md b/config-generator/README.md
index d694e71..7363057 100644
--- a/config-generator/README.md
+++ b/config-generator/README.md
@@ -1,7 +1,43 @@
-# config Gen
+# Config Gen
+-----------
+This service generates network configuration (`network-cfg.json`) for CORD POD
 
-- install Go
-- Make sure ONOS is reachable on localhost or modify the IP in the configGen.go
+### Prerequisites:
+- Install Go (only for local debugging)
+- Make sure ONOS is reachable either locally or via SSH tunnel (either way is fine)
 - Make sure devices, hosts are connected and showing up in ONOS
-- Run by `go run congifGen.go`
-- You should see `network-cfg.json` file in your current directory
+- Config-gen server listens on port 1337, so make sure it's available, if not change the ENV VAR `CONFIGGEN_ConfigServerPort` to the port you wish config-gen to use 
+
+
+### To run locally from source (without a container) for debugging:
+- Run `go run congifGen.go`
+- Use CURL to get the JSON output for `network-cfg.json` (CURL example below)
+
+
+### To run the container/uService:
+- Use the `Makefile` to build the container image with command `make image`
+- Run the container/service with `make run`
+- Example client would use the following CURL command to get the config:
+```bash
+curl -H "Content-Type: application/json" -d '{"switchcount" : 4, "hostcount": 4, "onosip" : "127.0.0.1" }' http://localhost:1337/config/
+```
+- Where, `switchcount`, and `hostcount` are the number of switches & hosts expected to be in the network respectively, and onosip is the ONOS IP (default is 127.0.0.1)
+
+
+### Note:
+- Every parameter is set by envconfig (https://github.com/kelseyhightower/envconfig)
+- For the parameters passed to the server through HTTP POST override the config set by envconfig
+- To modify any of the envvars, please set the ENV VAR with the prefix of `CONFIGGEN`
+- List of parameters set by envconfig:
+```bash
+	Port             string `default:"8181"`		//ONOS Port
+	IP               string `default:"127.0.0.1"`	//ONOS IP (Default)
+	SwitchCount      int    `default:"0"`
+	HostCount        int    `default:"0"`
+	Username         string `default:"karaf"`
+	Password         string `default:"karaf"`
+	LogLevel         string `default:"warning" envconfig:"LOG_LEVEL"`
+	LogFormat        string `default:"text" envconfig:"LOG_FORMAT"`
+	ConfigServerPort string `default:"1337"`		//Config-gen service port default
+	ConfigServerIP   string `default:"127.0.0.1"`	//Config-gen service IP
+```
\ No newline at end of file
diff --git a/config-generator/configGen.go b/config-generator/configGen.go
index bcc3ddd..b0460f0 100644
--- a/config-generator/configGen.go
+++ b/config-generator/configGen.go
@@ -27,6 +27,8 @@
 	"text/template"
 )
 
+var unmarshalError = errors.New("Error Unmarshaling the JSON Data\n")
+
 type Config struct {
 	Port             string `default:"8181"`
 	IP               string `default:"127.0.0.1"`
@@ -83,8 +85,9 @@
 }
 
 type ConfigParam struct {
-	SwitchCount int `json:"switchcount"`
-	HostCount   int `json:"hostcount"`
+	SwitchCount int    `json:"switchcount"`
+	HostCount   int    `json:"hostcount"`
+	ONOSIP      string `json:"onosip"`
 }
 
 var c Config
@@ -119,6 +122,7 @@
 
 	c.HostCount = para.HostCount
 	c.SwitchCount = para.SwitchCount
+	c.IP = para.ONOSIP
 
 	onos := "http://" + c.Username + ":" + c.Password + "@" + c.IP + ":" + c.Port
 
@@ -128,14 +132,19 @@
 	}
 	err = generateDevicesJSON(onos)
 	if err != nil {
-		w.WriteHeader(http.StatusExpectationFailed)
+		w.WriteHeader(http.StatusInternalServerError)
 		fmt.Fprintf(w, err.Error())
 		return
 	}
-	generateLinkJSON(onos)
+	err = generateLinkJSON(onos)
+	if err != nil {
+		w.WriteHeader(http.StatusInternalServerError)
+		fmt.Fprintf(w, err.Error())
+		return
+	}
 	err = generateHostJSON(onos)
 	if err != nil {
-		w.WriteHeader(http.StatusExpectationFailed)
+		w.WriteHeader(http.StatusInternalServerError)
 		fmt.Fprintf(w, err.Error())
 		return
 	}
@@ -165,11 +174,16 @@
 }
 
 func generateDevicesJSON(onos string) error {
-	ds := getData(onos + "/onos/v1/devices")
+	ds, err := getData(onos + "/onos/v1/devices")
+	if err != nil {
+		return err
+	}
 
 	var d devices
-	err := json.Unmarshal(ds, &d)
-	check(err)
+	err = json.Unmarshal(ds, &d)
+	if err != nil {
+		return unmarshalError
+	}
 
 	if len(d.Device) != c.SwitchCount {
 		_ = os.Remove("network-cfg.json")
@@ -192,10 +206,15 @@
 }
 
 func generateHostJSON(onos string) error {
-	hs := getData(onos + "/onos/v1/hosts")
+	hs, err := getData(onos + "/onos/v1/hosts")
+	if err != nil {
+		return err
+	}
 	var h hosts
-	err := json.Unmarshal(hs, &h)
-	check(err)
+	err = json.Unmarshal(hs, &h)
+	if err != nil {
+		return unmarshalError
+	}
 
 	if len(h.Host) != c.HostCount {
 		_ = os.Remove("network-cfg.json")
@@ -227,13 +246,18 @@
 
 }
 
-func generateLinkJSON(onos string) {
+func generateLinkJSON(onos string) error {
 
-	links := getData(onos + "/onos/v1/links")
+	links, err := getData(onos + "/onos/v1/links")
+	if err != nil {
+		return err
+	}
 
 	var l onosLinks
-	err := json.Unmarshal(links, &l)
-	check(err)
+	err = json.Unmarshal(links, &l)
+	if err != nil {
+		return unmarshalError
+	}
 
 	var in []linkStructJSON
 
@@ -252,19 +276,25 @@
 
 	writeToFile(in, "links.tpl")
 
+	return nil
+
 }
 
-func getData(url string) []byte {
+func getData(url string) ([]byte, error) {
 
 	resp, err := http.Get(url)
-	check(err)
+	if err != nil {
+		return nil, errors.New("Error getting data from the URL\n")
+	}
 
 	defer resp.Body.Close()
 
 	body, err := ioutil.ReadAll(resp.Body)
-	check(err)
+	if err != nil {
+		return nil, errors.New("Error reading data from response body\n")
+	}
 
-	return body
+	return body, nil
 
 }
 
diff --git a/config-generator/network-cfg.json b/config-generator/network-cfg.json
index 0fd3906..c29a6cb 100644
--- a/config-generator/network-cfg.json
+++ b/config-generator/network-cfg.json
@@ -11,21 +11,21 @@
                 "adjacencySids" : []
             }
     },
-    "of:0000000000000011" : {
+    "of:0000000000000022" : {
             "segmentrouting" : {
-                "name" : "device-11",
+                "name" : "device-22",
                 "nodeSid" : 101,
-                "routerIp" : "10.6.0.6",
+                "routerIp" : "10.6.0.10",
                 "routerMac" : "cc:37:ab:00:00:01",
                 "isEdgeRouter" : true,
                 "adjacencySids" : []
             }
     },
-    "of:0000000000000022" : {
+    "of:0000000000000011" : {
             "segmentrouting" : {
-                "name" : "device-22",
+                "name" : "device-11",
                 "nodeSid" : 102,
-                "routerIp" : "10.6.0.10",
+                "routerIp" : "10.6.0.6",
                 "routerMac" : "cc:37:ab:00:00:02",
                 "isEdgeRouter" : true,
                 "adjacencySids" : []
@@ -51,10 +51,10 @@
     "of:0000000000000022/31-of:0000000000000011/2": {
          "basic": {}
      },
-    "of:0000000000000022/32-of:0000000000000012/2": {
+    "of:0000000000000021/31-of:0000000000000011/1": {
          "basic": {}
      },
-    "of:0000000000000021/31-of:0000000000000011/1": {
+    "of:0000000000000022/32-of:0000000000000012/2": {
          "basic": {}
      },
     "of:0000000000000012/1-of:0000000000000021/32": {
@@ -73,17 +73,17 @@
 
 "ports": {
 
-  "of:0000000000000022/3" : {
+  "of:0000000000000021/4" : {
     	"interfaces": [
     		{
-        		"ips" : [ "10.6.2.254/24" ]
+        		"ips" : [ "10.6.1.254/24" ]
     		}
     	]
 	},
-  "of:0000000000000022/4" : {
+  "of:0000000000000021/3" : {
     	"interfaces": [
     		{
-        		"ips" : [ "10.6.2.254/24" ]
+        		"ips" : [ "10.6.1.254/24" ]
     		}
     	]
 	},
@@ -105,16 +105,16 @@
 
  "hosts" : {
 
-	"3C:FD:FE:9E:95:99/-1" : {
+	"3C:FD:FE:9E:94:28/-1" : {
 		"basic" : {
-			"ips" : ["10.6.2.3"],
-			"location" : "of:0000000000000022/3"
+			"ips" : ["10.6.1.4"],
+			"location" : "of:0000000000000021/4"
 		}
 	},
-	"3C:FD:FE:9E:94:29/-1" : {
+	"3C:FD:FE:9E:95:98/-1" : {
 		"basic" : {
-			"ips" : ["10.6.2.4"],
-			"location" : "of:0000000000000022/4"
+			"ips" : ["10.6.1.2"],
+			"location" : "of:0000000000000021/3"
 		}
 	},
 	"3C:FD:FE:9E:98:68/-1" : {