Brian O'Connor | 6a37ea9 | 2017-08-03 22:45:59 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Foundation |
David K. Bainbridge | c04fd55 | 2016-11-08 18:39:29 -0800 | [diff] [blame] | 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. |
| 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "bytes" |
| 18 | "encoding/json" |
| 19 | "net/http" |
| 20 | "strings" |
| 21 | "text/template" |
| 22 | ) |
| 23 | |
| 24 | type GenerationOptions struct { |
| 25 | SwitchCount int `json:"switchcount"` |
| 26 | HostCount int `json:"hostcount"` |
| 27 | } |
| 28 | |
| 29 | func (c *Config) configGenHandler(w http.ResponseWriter, r *http.Request) { |
| 30 | var options GenerationOptions |
| 31 | |
| 32 | deviceMap := make(map[string]*onosDevice) |
| 33 | |
| 34 | decoder := json.NewDecoder(r.Body) |
| 35 | defer r.Body.Close() |
| 36 | if err := decoder.Decode(&options); err != nil { |
| 37 | log.Errorf("Unable to decode provisioning request options: %s", err) |
| 38 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | var devices onosDevices |
| 43 | err := c.fetch("/onos/v1/devices", &devices) |
| 44 | if err != nil { |
| 45 | log.Errorf("Unable to retrieve device information from controller: %s", err) |
| 46 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | // If the request specified the number of switches, validate we have that |
| 51 | // exact number |
| 52 | if options.SwitchCount > 0 && len(devices.Devices) != options.SwitchCount { |
| 53 | log.Errorf("Expecting %d switch(es), found %d, no configuration generated", |
| 54 | options.SwitchCount, len(devices.Devices)) |
| 55 | http.Error(w, "Expected switch count mismatch", |
| 56 | http.StatusInternalServerError) |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | for _, device := range devices.Devices { |
| 61 | deviceMap[device.Id] = device |
| 62 | device.Mac = splitString(device.ChassisId, 2, ":") |
| 63 | } |
| 64 | |
| 65 | var hosts onosHosts |
| 66 | err = c.fetch("/onos/v1/hosts", &hosts) |
| 67 | if err != nil { |
| 68 | log.Errorf("Unable to retrieve host information from controller: %s", err) |
| 69 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 70 | return |
| 71 | |
| 72 | } |
| 73 | |
| 74 | // If the request specified the number of hosts, validate we have that |
| 75 | // exact number |
| 76 | if options.HostCount > 0 && len(hosts.Hosts) != options.HostCount { |
| 77 | log.Errorf("Expecting %d host(s), found %d, no configuration generaged", |
| 78 | options.HostCount, len(hosts.Hosts)) |
| 79 | http.Error(w, "Expected host count mismatch", |
| 80 | http.StatusInternalServerError) |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | // Use a simple heuristic to determine which switches are edge routers |
| 85 | // and which are not |
| 86 | markEdgeRouters(deviceMap, hosts) |
| 87 | |
| 88 | // Generate the configuration file |
| 89 | cfg := onosConfig{ |
| 90 | Devices: devices.Devices, |
| 91 | Hosts: hosts.Hosts, |
| 92 | } |
| 93 | |
| 94 | funcMap := template.FuncMap{ |
| 95 | // The name "inc" is what the function will be called in the template text. |
| 96 | "add": func(a, b int) int { |
| 97 | return a + b |
| 98 | }, |
| 99 | "gateway": func(ips []string) string { |
| 100 | if len(ips) > 0 { |
| 101 | parts := strings.Split(ips[0], ".") |
| 102 | ip := "" |
| 103 | for _, v := range parts[:len(parts)-1] { |
| 104 | ip = ip + v + "." |
| 105 | } |
| 106 | return ip + "254/24" |
| 107 | } else { |
| 108 | return "0.0.0.254/24" |
| 109 | } |
| 110 | }, |
| 111 | } |
| 112 | |
| 113 | tpl, err := template.New("netconfig.tpl").Funcs(funcMap).ParseFiles("netconfig.tpl") |
| 114 | if err != nil { |
| 115 | log.Errorf("Unable to parse template: %s", err) |
| 116 | http.Error(w, "Template parse error", http.StatusInternalServerError) |
| 117 | return |
| 118 | } |
| 119 | |
| 120 | // Write template to buffer, so if there is an error we can return an |
| 121 | // http error |
| 122 | buf := new(bytes.Buffer) |
| 123 | err = tpl.Execute(buf, cfg) |
| 124 | if err != nil { |
| 125 | log.Errorf("Unexpected error while processing template: %s", err) |
| 126 | http.Error(w, "Template processing error", http.StatusInternalServerError) |
| 127 | } |
| 128 | |
| 129 | w.Write(buf.Bytes()) |
| 130 | } |
| 131 | |
| 132 | // markEdgeRouters use hueristic to determine and mark switches that act |
| 133 | // as edge routers |
| 134 | func markEdgeRouters(dm map[string]*onosDevice, hosts onosHosts) { |
| 135 | // Walk the list of know compute nodes (hosts) and if the compute node |
| 136 | // is connected to a switch, then that switch is an edge router |
| 137 | for _, host := range hosts.Hosts { |
| 138 | if device, ok := dm[host.Location.ElementID]; ok { |
| 139 | (*device).IsEdgeRouter = true |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // splitString used to convert a string to a psudeo MAC address by |
| 145 | // splitting and separating it with a colon |
| 146 | func splitString(src string, n int, sep string) string { |
| 147 | r := "" |
| 148 | for i, c := range src { |
| 149 | if i > 0 && i%n == 0 { |
| 150 | r += sep |
| 151 | } |
| 152 | r += string(c) |
| 153 | } |
| 154 | return r |
| 155 | } |
| 156 | |
| 157 | // fetch fetch the specified data from ONOS |
| 158 | func (c *Config) fetch(path string, data interface{}) error { |
| 159 | resp, err := http.Get(c.connect + path) |
| 160 | if err != nil { |
| 161 | return err |
| 162 | } |
| 163 | |
| 164 | defer resp.Body.Close() |
| 165 | decoder := json.NewDecoder(resp.Body) |
| 166 | err = decoder.Decode(data) |
| 167 | return err |
| 168 | } |