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 ( |
| 17 | "encoding/json" |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 18 | "errors" |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 19 | "fmt" |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 20 | "github.com/gorilla/mux" |
| 21 | "github.com/kelseyhightower/envconfig" |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 22 | "io/ioutil" |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 23 | "log" |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 24 | "net/http" |
| 25 | "os" |
| 26 | "strings" |
| 27 | "text/template" |
| 28 | ) |
| 29 | |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 30 | var unmarshalError = errors.New("Error Unmarshaling the JSON Data\n") |
| 31 | |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 32 | type Config struct { |
| 33 | Port string `default:"8181"` |
| 34 | IP string `default:"127.0.0.1"` |
gunjan5 | 0e64b59 | 2016-07-20 18:11:04 -0700 | [diff] [blame] | 35 | SwitchCount int `default:"0"` |
| 36 | HostCount int `default:"0"` |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 37 | Username string `default:"karaf"` |
| 38 | Password string `default:"karaf"` |
| 39 | LogLevel string `default:"warning" envconfig:"LOG_LEVEL"` |
| 40 | LogFormat string `default:"text" envconfig:"LOG_FORMAT"` |
| 41 | ConfigServerPort string `default:"1337"` |
| 42 | ConfigServerIP string `default:"127.0.0.1"` |
| 43 | } |
| 44 | |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 45 | type hosts struct { |
| 46 | Host []struct { |
| 47 | Mac string `json:"mac"` |
| 48 | IpAddresses []string `json:"ipAddresses"` |
| 49 | Location struct { |
| 50 | ElementID string `json:"elementId` |
| 51 | Port string `json:"port"` |
| 52 | } `json:"location"` |
| 53 | Comma string |
| 54 | Gateway string |
| 55 | } `json:"hosts"` |
| 56 | } |
| 57 | |
| 58 | type devices struct { |
| 59 | Device []struct { |
| 60 | Id string `json:"id"` |
| 61 | ChassisId string `json:"chassisId"` |
| 62 | Annotations struct { |
| 63 | ManagementAddress string `json:"managementAddress"` |
| 64 | } `json:"annotations"` |
| 65 | Comma string `default:","` |
| 66 | } `json:"devices"` |
| 67 | } |
| 68 | |
| 69 | type onosLinks struct { |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 70 | Links []struct { |
| 71 | Src struct { |
| 72 | Port string `json:"port"` |
| 73 | Device string `json:"device"` |
| 74 | } `json:"src"` |
| 75 | Dst struct { |
| 76 | Port string `json:"port"` |
| 77 | Device string `json:"device"` |
| 78 | } `json:"dst"` |
| 79 | } `json:"links"` |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | type linkStructJSON struct { |
| 83 | Val string |
| 84 | Comma string |
| 85 | } |
| 86 | |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 87 | type ConfigParam struct { |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 88 | SwitchCount int `json:"switchcount"` |
| 89 | HostCount int `json:"hostcount"` |
| 90 | ONOSIP string `json:"onosip"` |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | var c Config |
| 94 | |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 95 | func main() { |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 96 | |
| 97 | err := envconfig.Process("CONFIGGEN", &c) |
| 98 | if err != nil { |
| 99 | log.Fatalf("[ERROR] Unable to parse configuration options : %s", err) |
| 100 | } |
| 101 | |
| 102 | router := mux.NewRouter() |
| 103 | router.HandleFunc("/config/", ConfigGenHandler).Methods("POST") |
| 104 | http.Handle("/", router) |
| 105 | |
| 106 | fmt.Println("Config Generator server listening at: " + c.ConfigServerIP + ":" + c.ConfigServerPort) |
| 107 | |
| 108 | http.ListenAndServe(c.ConfigServerIP+":"+c.ConfigServerPort, nil) |
| 109 | |
| 110 | } |
| 111 | |
| 112 | func ConfigGenHandler(w http.ResponseWriter, r *http.Request) { |
| 113 | var para ConfigParam |
| 114 | |
| 115 | decoder := json.NewDecoder(r.Body) |
| 116 | defer r.Body.Close() |
| 117 | if err := decoder.Decode(¶); err != nil { |
| 118 | fmt.Errorf("Unable to decode request to provision : %s", err) |
| 119 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 120 | return |
| 121 | } |
| 122 | |
| 123 | c.HostCount = para.HostCount |
| 124 | c.SwitchCount = para.SwitchCount |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 125 | c.IP = para.ONOSIP |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 126 | |
| 127 | onos := "http://" + c.Username + ":" + c.Password + "@" + c.IP + ":" + c.Port |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 128 | |
| 129 | err := os.Remove("network-cfg.json") |
| 130 | if err != nil { |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 131 | log.Println("Warning: no file called network-cfg.json (ignore if this is the first run)") |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 132 | } |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 133 | err = generateDevicesJSON(onos) |
| 134 | if err != nil { |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 135 | w.WriteHeader(http.StatusInternalServerError) |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 136 | fmt.Fprintf(w, err.Error()) |
| 137 | return |
| 138 | } |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 139 | err = generateLinkJSON(onos) |
| 140 | if err != nil { |
| 141 | w.WriteHeader(http.StatusInternalServerError) |
| 142 | fmt.Fprintf(w, err.Error()) |
| 143 | return |
| 144 | } |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 145 | err = generateHostJSON(onos) |
| 146 | if err != nil { |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 147 | w.WriteHeader(http.StatusInternalServerError) |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 148 | fmt.Fprintf(w, err.Error()) |
| 149 | return |
| 150 | } |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 151 | |
| 152 | fmt.Println("Config file generated: network-cfg.json") |
| 153 | |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 154 | data, err := ioutil.ReadFile("network-cfg.json") |
| 155 | check(err) |
| 156 | |
| 157 | w.WriteHeader(http.StatusAccepted) |
| 158 | fmt.Fprintf(w, string(data)) |
| 159 | |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | func writeToFile(object interface{}, t string) { |
| 163 | f, err := os.OpenFile("network-cfg.json", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) |
| 164 | if err != nil { |
| 165 | panic(err) |
| 166 | } |
| 167 | |
| 168 | defer f.Close() |
| 169 | |
| 170 | tpl, err := template.ParseFiles(t) |
| 171 | check(err) |
| 172 | err = tpl.Execute(f, object) |
| 173 | check(err) |
| 174 | } |
| 175 | |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 176 | func generateDevicesJSON(onos string) error { |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 177 | ds, err := getData(onos + "/onos/v1/devices") |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 181 | |
| 182 | var d devices |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 183 | err = json.Unmarshal(ds, &d) |
| 184 | if err != nil { |
| 185 | return unmarshalError |
| 186 | } |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 187 | |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 188 | if len(d.Device) != c.SwitchCount { |
| 189 | _ = os.Remove("network-cfg.json") |
| 190 | log.Println("[INFO] Cleaning up unfinished config file") |
| 191 | e := fmt.Sprintf("[ERROR] Number of switches configured don't match actual switches connected to the controller. Configured: %d, connected: %d", c.SwitchCount, len(d.Device)) |
| 192 | log.Println(e) |
| 193 | return errors.New(e) |
| 194 | } |
| 195 | |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 196 | for k, _ := range d.Device { |
| 197 | d.Device[k].Comma = "," |
| 198 | if k >= len(d.Device)-1 { |
| 199 | d.Device[k].Comma = "" |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | writeToFile(d.Device, "devices.tpl") |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 204 | return nil |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 205 | |
| 206 | } |
| 207 | |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 208 | func generateHostJSON(onos string) error { |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 209 | hs, err := getData(onos + "/onos/v1/hosts") |
| 210 | if err != nil { |
| 211 | return err |
| 212 | } |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 213 | var h hosts |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 214 | err = json.Unmarshal(hs, &h) |
| 215 | if err != nil { |
| 216 | return unmarshalError |
| 217 | } |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 218 | |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 219 | if len(h.Host) != c.HostCount { |
| 220 | _ = os.Remove("network-cfg.json") |
| 221 | log.Println("[INFO] Cleaning up unfinished config file") |
| 222 | e := fmt.Sprintf("[ERROR] Number of hosts configured don't match actual hosts visible to the controller. Configured: %d, connected: %d", c.HostCount, len(h.Host)) |
| 223 | log.Println(e) |
| 224 | return errors.New(e) |
| 225 | } |
| 226 | |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 227 | for k, _ := range h.Host { |
| 228 | |
| 229 | h.Host[k].Comma = "," |
| 230 | if k >= len(h.Host)-1 { |
| 231 | h.Host[k].Comma = "" |
| 232 | } |
| 233 | |
| 234 | parts := strings.Split(h.Host[k].IpAddresses[0], ".") |
| 235 | ip := "" |
| 236 | for _, v := range parts[:len(parts)-1] { |
| 237 | ip = ip + v + "." |
| 238 | } |
| 239 | h.Host[k].Gateway = ip |
| 240 | } |
| 241 | |
| 242 | writeToFile(h.Host, "ports.tpl") |
| 243 | |
| 244 | writeToFile(h.Host, "hosts.tpl") |
gunjan5 | 4fbb81c | 2016-07-09 03:31:27 -0700 | [diff] [blame] | 245 | return nil |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 246 | |
| 247 | } |
| 248 | |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 249 | func generateLinkJSON(onos string) error { |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 250 | |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 251 | links, err := getData(onos + "/onos/v1/links") |
| 252 | if err != nil { |
| 253 | return err |
| 254 | } |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 255 | |
| 256 | var l onosLinks |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 257 | err = json.Unmarshal(links, &l) |
| 258 | if err != nil { |
| 259 | return unmarshalError |
| 260 | } |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 261 | |
| 262 | var in []linkStructJSON |
| 263 | |
| 264 | for k, v := range l.Links { |
| 265 | |
| 266 | comma := "," |
| 267 | val := fmt.Sprint(v.Src.Device + "/" + v.Src.Port + "-" + v.Dst.Device + "/" + v.Dst.Port) |
| 268 | if k >= len(l.Links)-1 { |
| 269 | comma = "" |
| 270 | } |
| 271 | |
| 272 | tmp := linkStructJSON{val, comma} |
| 273 | in = append(in, tmp) |
| 274 | |
| 275 | } |
| 276 | |
| 277 | writeToFile(in, "links.tpl") |
| 278 | |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 279 | return nil |
| 280 | |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 281 | } |
| 282 | |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 283 | func getData(url string) ([]byte, error) { |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 284 | |
| 285 | resp, err := http.Get(url) |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 286 | if err != nil { |
| 287 | return nil, errors.New("Error getting data from the URL\n") |
| 288 | } |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 289 | |
| 290 | defer resp.Body.Close() |
| 291 | |
| 292 | body, err := ioutil.ReadAll(resp.Body) |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 293 | if err != nil { |
| 294 | return nil, errors.New("Error reading data from response body\n") |
| 295 | } |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 296 | |
gunjan5 | 87fd406 | 2016-08-11 13:51:19 -0700 | [diff] [blame] | 297 | return body, nil |
gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame] | 298 | |
| 299 | } |
| 300 | |
| 301 | func check(e error) { |
| 302 | if e != nil { |
| 303 | panic(e) |
| 304 | } |
| 305 | } |