blob: 0950ca7728fc85a1cb98d37decdd4039a6862bc0 [file] [log] [blame]
Jonathan Hartf86817b2018-08-17 10:35:54 -07001// Copyright 2018 Open Networking Foundation
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.
14package main
15
16import (
Jonathan Hartf86817b2018-08-17 10:35:54 -070017 "encoding/json"
Andy Baviera0095922018-09-07 12:49:38 -070018 "net"
Jonathan Hartf86817b2018-08-17 10:35:54 -070019 "net/http"
Matteo Scandoloa79e6082018-08-21 14:16:47 -070020 "strconv"
21
Matteo Scandolo60676562019-03-15 14:56:25 -070022 "github.com/sirupsen/logrus"
23
Jonathan Hartf86817b2018-08-17 10:35:54 -070024 "github.com/gorilla/mux"
25)
26
Jonathan Hartf86817b2018-08-17 10:35:54 -070027func (c *Config) getSubscriberHandler(w http.ResponseWriter, r *http.Request) {
28 vars := mux.Vars(r)
29 sadisRequestID := vars["id"]
30
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080031 log.WithFields(logrus.Fields{
32 "sadisId": sadisRequestID,
33 }).Infof("Looking for object %s in XOS database", sadisRequestID)
Jonathan Hartf86817b2018-08-17 10:35:54 -070034
35 defer r.Body.Close()
36
37 subscribers := subscribers{}
38
39 log.Debug("Checking subscribers")
40
41 err := c.fetch("/xosapi/v1/rcord/rcordsubscribers", &subscribers)
42 if err != nil {
43 log.Errorf("Unable to retrieve subscriber information from XOS: %s", err)
44 http.Error(w, err.Error(), http.StatusInternalServerError)
45 return
46
47 }
48
49 for _, sub := range subscribers.Subscribers {
50 if sub.OnuSerialNumber == sadisRequestID {
51 log.Infof("Found subscriber with ID %s", sub.OnuSerialNumber)
52 sadisSubscriber := sadisSubscriber{
Matteo Scandolo04428fd2019-04-17 14:22:30 -070053 ID: sub.OnuSerialNumber,
54 CTag: sub.CTag,
55 STag: sub.STag,
56 NasPortID: sub.NasPortID,
57 CircuitID: sub.CircuitID,
58 RemoteID: sub.RemoteID,
59 TechnologyProfileID: 64,
Jonathan Hartf86817b2018-08-17 10:35:54 -070060 }
61
Matteo Scandolo60676562019-03-15 14:56:25 -070062 log.Debugf("Fetching bandwidth profiles for subscriber %s", sub.OnuSerialNumber)
63
64 dsBandwidthprofile := bandwidthprofile{}
65 err = c.getOneBandwidthProfileHandler(sub.DownstreamBandwidthProfile, &dsBandwidthprofile)
66 if err != nil {
67 log.Errorf("Cannot fetch downstream bandwidth profile %s for subscriber %s", strconv.Itoa(sub.DownstreamBandwidthProfile), sub.OnuSerialNumber)
68 http.Error(w, err.Error(), http.StatusInternalServerError)
69 return
70 }
71 if (bandwidthprofile{}) == dsBandwidthprofile {
72 // it's empty
73 log.WithFields(logrus.Fields{
74 "DownstreamBandwidthProfile": sub.DownstreamBandwidthProfile,
75 "Subscriber": sub.OnuSerialNumber,
76 "sadisId": sadisRequestID,
77 }).Error("Downstream bandwidth profile not found in XOS")
78 http.Error(w, "Downstream bandwidth profile not found in XOS", http.StatusInternalServerError)
79 return
80 }
81 sadisSubscriber.DownstreamBandwidthProfile = dsBandwidthprofile.Name
82
83 usBandwidthprofile := bandwidthprofile{}
84 err = c.getOneBandwidthProfileHandler(sub.UpstreamBandwidthProfile, &usBandwidthprofile)
85 if err != nil {
86 log.Errorf("Cannot fetch upstream bandwidth profile %s for subscriber %s", strconv.Itoa(sub.UpstreamBandwidthProfile), sub.OnuSerialNumber)
87 http.Error(w, err.Error(), http.StatusInternalServerError)
88 return
89 }
90 if (bandwidthprofile{}) == usBandwidthprofile {
91 // it's empty
92 log.WithFields(logrus.Fields{
93 "UpstreamBandwidthProfile": usBandwidthprofile.Name,
94 "Subscriber": sub.OnuSerialNumber,
95 "sadisId": sadisRequestID,
96 }).Error("Upstream bandwidth profile not found in XOS")
97 http.Error(w, "Upstream bandwidth profile not found in XOS", http.StatusInternalServerError)
98 return
99 }
100 sadisSubscriber.UpstreamBandwidthProfile = usBandwidthprofile.Name
101
102 log.WithFields(logrus.Fields{
103 "UpstreamBandwidthProfile": usBandwidthprofile.Name,
104 "DownstreamBandwidthProfile": dsBandwidthprofile.Name,
105 "Subscriber": sub.OnuSerialNumber,
106 "sadisId": sadisRequestID,
107 }).Debug("Bandwidth profiles for subscriber")
108
Jonathan Hartf86817b2018-08-17 10:35:54 -0700109 json, e := json.Marshal(&sadisSubscriber)
110 if e != nil {
111 log.Errorf("Unable to marshal JSON: %s", e)
112 http.Error(w, e.Error(), http.StatusInternalServerError)
113 return
114 }
115 w.Write(json)
116 return
117 }
118 }
119
120 log.Debug("Checking devices")
121
122 devices := oltDevices{}
123
124 err = c.fetch("/xosapi/v1/volt/oltdevices", &devices)
125 if err != nil {
126 log.Errorf("Unable to retrieve device information from XOS: %s", err)
127 http.Error(w, err.Error(), http.StatusInternalServerError)
128 return
129
130 }
131
132 for _, device := range devices.OltDevices {
Matteo Scandolo5be70a12018-10-16 10:57:57 -0700133 // NOTE if it's an OLT then sadisRequestID is the device serial number
134 devID := device.SerialNumber
Jonathan Hartf86817b2018-08-17 10:35:54 -0700135 if devID == sadisRequestID {
136 log.Infof("Found OLT device with ID %s", devID)
Andy Baviera0095922018-09-07 12:49:38 -0700137
138 ipaddr := device.Host
139 addr, err := net.ResolveIPAddr("ip", device.Host)
140 if err != nil {
141 log.Errorf("Resolution error: %s", err.Error())
142 http.Error(w, err.Error(), http.StatusInternalServerError)
143 return
144 } else {
145 ipaddr = addr.String()
146 }
147 log.Debugf("ID: %s, Uplink: %s, IPAddress: %s, NasID: %s", devID, toInt(device.Uplink), ipaddr, device.NasID)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700148 sadisDevice := sadisDevice{
Matteo Scandoloa79e6082018-08-21 14:16:47 -0700149 ID: devID,
150 Uplink: toInt(device.Uplink),
Jonathan Hartf86817b2018-08-17 10:35:54 -0700151 HardwareID: "de:ad:be:ef:ba:11", // TODO do we really need to configure this?
Andy Baviera0095922018-09-07 12:49:38 -0700152 IPAddress: ipaddr,
Matteo Scandolo3f330cd2018-08-31 07:29:35 -0700153 NasID: device.NasID,
Jonathan Hartf86817b2018-08-17 10:35:54 -0700154 }
155
156 json, e := json.Marshal(&sadisDevice)
157 if e != nil {
158 log.Errorf("Unable to marshal JSON: %s", e)
159 http.Error(w, e.Error(), http.StatusInternalServerError)
160 return
161 }
162 w.Write(json)
163 return
164 }
165 }
166
Matteo Scandolo9a2772a2018-11-19 14:56:26 -0800167 log.WithFields(logrus.Fields{
168 "sadisId": sadisRequestID,
169 }).Infof("Couldn't find object %s in XOS database", sadisRequestID)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700170
171 http.NotFound(w, r)
172}
173
Matteo Scandolo60676562019-03-15 14:56:25 -0700174func (c *Config) getBandwidthProfileHandler(w http.ResponseWriter, r *http.Request) {
175 vars := mux.Vars(r)
176 sadisRequestID := vars["id"]
177
178 log.WithFields(logrus.Fields{
179 "sadisId": sadisRequestID,
180 }).Infof("Fetching BandwidthProfiles from XOS database")
181 defer r.Body.Close()
182
183 bandwidthprofiles := bandwidthprofiles{}
184
185 err := c.fetch("/xosapi/v1/rcord/bandwidthprofiles", &bandwidthprofiles)
186
187 if err != nil {
188 log.Errorf("Unable to retrieve bandwidth profiles information from XOS: %s", err)
189 http.Error(w, err.Error(), http.StatusInternalServerError)
190 return
191 }
192
193 for _, profile := range bandwidthprofiles.Profiles {
194 profileID := profile.Name
195 if profileID == sadisRequestID {
196 sadisProfile := sadisBandwidthProfile{
197 ID: profile.Name,
198 Cir: profile.Cir,
199 Cbs: profile.Cbs,
200 Eir: profile.Eir,
201 Ebs: profile.Ebs,
202 Air: profile.Air,
203 }
204 json, e := json.Marshal(&sadisProfile)
205 if e != nil {
206 log.Errorf("Unable to marshal JSON: %s", e)
207 http.Error(w, e.Error(), http.StatusInternalServerError)
208 return
209 }
210 w.Write(json)
211 return
212 }
213 }
214
215 log.WithFields(logrus.Fields{
216 "sadisId": sadisRequestID,
217 }).Infof("Couldn't find object %s in XOS database", sadisRequestID)
218
219 http.NotFound(w, r)
220}
221
222func (c *Config) getOneBandwidthProfileHandler(id int, data interface{}) error {
223 err := c.fetch("/xosapi/v1/rcord/bandwidthprofiles/"+strconv.Itoa(id), &data)
224 if err != nil {
225 log.Errorf("Unable to retrieve bandwidth profile information from XOS: %s", err)
226 return err
227 }
228 return nil
229}
230
Jonathan Hartf86817b2018-08-17 10:35:54 -0700231func toInt(value string) int {
232 r, _ := strconv.Atoi(value)
233 return r
234}
235
236func (c *Config) fetch(path string, data interface{}) error {
237 resp, err := http.Get(c.connect + path)
Matteo Scandolo60676562019-03-15 14:56:25 -0700238
Jonathan Hartf86817b2018-08-17 10:35:54 -0700239 if err != nil {
240 return err
241 }
242
243 defer resp.Body.Close()
244 decoder := json.NewDecoder(resp.Body)
245 err = decoder.Decode(data)
Matteo Scandolo60676562019-03-15 14:56:25 -0700246 log.WithFields(logrus.Fields{
247 "path": path,
248 "resp": data,
249 "status": resp.Status,
250 }).Debug("Received data from XOS")
Jonathan Hartf86817b2018-08-17 10:35:54 -0700251 return err
252}