gunjan5 | 6e19d27 | 2016-07-07 14:23:26 -0700 | [diff] [blame^] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "io/ioutil" |
| 7 | "net/http" |
| 8 | "os" |
| 9 | "strings" |
| 10 | "text/template" |
| 11 | ) |
| 12 | |
| 13 | type hosts struct { |
| 14 | Host []struct { |
| 15 | Mac string `json:"mac"` |
| 16 | IpAddresses []string `json:"ipAddresses"` |
| 17 | Location struct { |
| 18 | ElementID string `json:"elementId` |
| 19 | Port string `json:"port"` |
| 20 | } `json:"location"` |
| 21 | Comma string |
| 22 | Gateway string |
| 23 | } `json:"hosts"` |
| 24 | } |
| 25 | |
| 26 | type devices struct { |
| 27 | Device []struct { |
| 28 | Id string `json:"id"` |
| 29 | ChassisId string `json:"chassisId"` |
| 30 | Annotations struct { |
| 31 | ManagementAddress string `json:"managementAddress"` |
| 32 | } `json:"annotations"` |
| 33 | Comma string `default:","` |
| 34 | } `json:"devices"` |
| 35 | } |
| 36 | |
| 37 | type onosLinks struct { |
| 38 | Links []link `json:"links"` |
| 39 | } |
| 40 | |
| 41 | type link struct { |
| 42 | Src devicePort `json:"src"` |
| 43 | Dst devicePort `json:"dst"` |
| 44 | } |
| 45 | |
| 46 | type devicePort struct { |
| 47 | Port string `json:"port"` |
| 48 | Device string `json:"device"` |
| 49 | } |
| 50 | |
| 51 | type linkStructJSON struct { |
| 52 | Val string |
| 53 | Comma string |
| 54 | } |
| 55 | |
| 56 | func main() { |
| 57 | onos := "http://karaf:karaf@127.0.0.1:8181" |
| 58 | |
| 59 | err := os.Remove("network-cfg.json") |
| 60 | if err != nil { |
| 61 | fmt.Println("Warning: no file called network-cfg.json (ignore if this is the first run)") |
| 62 | } |
| 63 | generateDevicesJSON(onos) |
| 64 | generateLinkJSON(onos) |
| 65 | generateHostJSON(onos) |
| 66 | |
| 67 | fmt.Println("Config file generated: network-cfg.json") |
| 68 | |
| 69 | } |
| 70 | |
| 71 | func writeToFile(object interface{}, t string) { |
| 72 | f, err := os.OpenFile("network-cfg.json", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) |
| 73 | if err != nil { |
| 74 | panic(err) |
| 75 | } |
| 76 | |
| 77 | defer f.Close() |
| 78 | |
| 79 | tpl, err := template.ParseFiles(t) |
| 80 | check(err) |
| 81 | err = tpl.Execute(f, object) |
| 82 | check(err) |
| 83 | } |
| 84 | |
| 85 | func generateDevicesJSON(onos string) { |
| 86 | ds := getData(onos + "/onos/v1/devices") |
| 87 | |
| 88 | var d devices |
| 89 | err := json.Unmarshal(ds, &d) |
| 90 | check(err) |
| 91 | |
| 92 | for k, _ := range d.Device { |
| 93 | d.Device[k].Comma = "," |
| 94 | if k >= len(d.Device)-1 { |
| 95 | d.Device[k].Comma = "" |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | writeToFile(d.Device, "devices.tpl") |
| 100 | |
| 101 | } |
| 102 | |
| 103 | func generateHostJSON(onos string) { |
| 104 | hs := getData(onos + "/onos/v1/hosts") |
| 105 | var h hosts |
| 106 | err := json.Unmarshal(hs, &h) |
| 107 | check(err) |
| 108 | |
| 109 | for k, _ := range h.Host { |
| 110 | |
| 111 | h.Host[k].Comma = "," |
| 112 | if k >= len(h.Host)-1 { |
| 113 | h.Host[k].Comma = "" |
| 114 | } |
| 115 | |
| 116 | parts := strings.Split(h.Host[k].IpAddresses[0], ".") |
| 117 | ip := "" |
| 118 | for _, v := range parts[:len(parts)-1] { |
| 119 | ip = ip + v + "." |
| 120 | } |
| 121 | h.Host[k].Gateway = ip |
| 122 | } |
| 123 | |
| 124 | writeToFile(h.Host, "ports.tpl") |
| 125 | |
| 126 | writeToFile(h.Host, "hosts.tpl") |
| 127 | |
| 128 | } |
| 129 | |
| 130 | func generateLinkJSON(onos string) { |
| 131 | |
| 132 | links := getData(onos + "/onos/v1/links") |
| 133 | |
| 134 | var l onosLinks |
| 135 | err := json.Unmarshal(links, &l) |
| 136 | check(err) |
| 137 | |
| 138 | var in []linkStructJSON |
| 139 | |
| 140 | for k, v := range l.Links { |
| 141 | |
| 142 | comma := "," |
| 143 | val := fmt.Sprint(v.Src.Device + "/" + v.Src.Port + "-" + v.Dst.Device + "/" + v.Dst.Port) |
| 144 | if k >= len(l.Links)-1 { |
| 145 | comma = "" |
| 146 | } |
| 147 | |
| 148 | tmp := linkStructJSON{val, comma} |
| 149 | in = append(in, tmp) |
| 150 | |
| 151 | } |
| 152 | |
| 153 | writeToFile(in, "links.tpl") |
| 154 | |
| 155 | } |
| 156 | |
| 157 | func getData(url string) []byte { |
| 158 | |
| 159 | resp, err := http.Get(url) |
| 160 | check(err) |
| 161 | |
| 162 | defer resp.Body.Close() |
| 163 | |
| 164 | body, err := ioutil.ReadAll(resp.Body) |
| 165 | check(err) |
| 166 | |
| 167 | return body |
| 168 | |
| 169 | } |
| 170 | |
| 171 | func check(e error) { |
| 172 | if e != nil { |
| 173 | panic(e) |
| 174 | } |
| 175 | } |