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