blob: f23e67035692aa7553a69b29ac3c39e86a803b3e [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{
Daniele Moro11794a32020-03-05 18:04:58 -080053 ID: sub.OnuSerialNumber,
54 NasPortID: sub.NasPortID,
55 CircuitID: sub.CircuitID,
56 RemoteID: sub.RemoteID,
Jonathan Hartf86817b2018-08-17 10:35:54 -070057 }
58
Daniele Moro11794a32020-03-05 18:04:58 -080059 log.Debugf("Fetching UNI Tag list for subscriber %s", sub.OnuSerialNumber)
Matteo Scandolo60676562019-03-15 14:56:25 -070060
Daniele Moro11794a32020-03-05 18:04:58 -080061 unitaglist := sadisUnitaginfolist{}
62 for _, unitagid := range sub.UniTagListId {
63 utinfo := unitaginfo{}
64 err = c.getOneUniTagInfo(unitagid, &utinfo)
65 if err != nil {
66 log.Errorf("Cannot fetch UNI tag information%s for subscriber %s", strconv.Itoa(unitagid), sub.OnuSerialNumber)
67 http.Error(w, err.Error(), http.StatusInternalServerError)
68 return
69 }
70 if (unitaginfo{}) == utinfo {
71 // it's empty
72 log.WithFields(logrus.Fields{
73 "UniTagInfoId": unitagid,
74 "Subscriber": sub.OnuSerialNumber,
75 "sadisId": sadisRequestID,
76 }).Error("UNI Tag info not found in XOS")
77 http.Error(w, "UNI Tag info not found in XOS", http.StatusInternalServerError)
78 return
79 }
80 sadisUnitaginfo := sadisUnitaginfo{
81 UniTagMatch: utinfo.UniTagMatch,
82 PonCTag: utinfo.PonCTag,
83 PonSTag: utinfo.PonSTag,
84 UsPonCTagPriority: utinfo.UsPonCTagPriority,
85 UsPonSTagPriority: utinfo.UsPonSTagPriority,
86 DsPonCTagPriority: utinfo.DsPonCTagPriority,
87 DsPonSTagPriority: utinfo.DsPonSTagPriority,
88 TechnologyProfileID: utinfo.TechnologyProfileID,
89 ServiceName: utinfo.ServiceName,
90 EnableMacLearning: utinfo.EnableMacLearning,
91 ConfiguredMacAddress: utinfo.ConfiguredMacAddress,
92 IsDhcpRequired: utinfo.IsDhcpRequired,
93 IsIgmpRequired: utinfo.IsIgmpRequired,
94 }
95
96 log.Debugf("Fetching bandwidth profiles for subscriber %s and unitagid: %s", sub.OnuSerialNumber, strconv.Itoa(utinfo.ID))
97
98 dsBandwidthprofile := bandwidthprofile{}
99 err = c.getOneBandwidthProfileHandler(utinfo.DownstreamBandwidthProfile, &dsBandwidthprofile)
100 if err != nil {
101 log.Errorf("Cannot fetch downstream bandwidth profile %s for subscriber %s", strconv.Itoa(utinfo.DownstreamBandwidthProfile), sub.OnuSerialNumber)
102 http.Error(w, err.Error(), http.StatusInternalServerError)
103 return
104 }
105 if (bandwidthprofile{}) == dsBandwidthprofile {
106 // it's empty
107 log.WithFields(logrus.Fields{
108 "DownstreamBandwidthProfile": utinfo.DownstreamBandwidthProfile,
109 "Subscriber": sub.OnuSerialNumber,
110 "sadisId": sadisRequestID,
111 }).Error("Downstream bandwidth profile not found in XOS")
112 http.Error(w, "Downstream bandwidth profile not found in XOS", http.StatusInternalServerError)
113 return
114 }
115 sadisUnitaginfo.DownstreamBandwidthProfile = dsBandwidthprofile.Name
116
117 usBandwidthprofile := bandwidthprofile{}
118 err = c.getOneBandwidthProfileHandler(utinfo.UpstreamBandwidthProfile, &usBandwidthprofile)
119 if err != nil {
120 log.Errorf("Cannot fetch upstream bandwidth profile %s for subscriber %s", strconv.Itoa(utinfo.UpstreamBandwidthProfile), sub.OnuSerialNumber)
121 http.Error(w, err.Error(), http.StatusInternalServerError)
122 return
123 }
124 if (bandwidthprofile{}) == usBandwidthprofile {
125 // it's empty
126 log.WithFields(logrus.Fields{
127 "UpstreamBandwidthProfile": utinfo.UpstreamBandwidthProfile,
128 "Subscriber": sub.OnuSerialNumber,
129 "sadisId": sadisRequestID,
130 }).Error("Upstream bandwidth profile not found in XOS")
131 http.Error(w, "Upstream bandwidth profile not found in XOS", http.StatusInternalServerError)
132 return
133 }
134 sadisUnitaginfo.UpstreamBandwidthProfile = usBandwidthprofile.Name
135
Matteo Scandolo60676562019-03-15 14:56:25 -0700136 log.WithFields(logrus.Fields{
Matteo Scandolo60676562019-03-15 14:56:25 -0700137 "Subscriber": sub.OnuSerialNumber,
Daniele Moro11794a32020-03-05 18:04:58 -0800138 "UniTagInfo": utinfo.ID,
139 "UpstreamBandwidthProfile": usBandwidthprofile.Name,
140 "DownstreamBandwidthProfile": dsBandwidthprofile.Name,
141 "sadisId": sadisRequestID,
142 }).Debug("Bandwidth profiles for subscriber/unitaginfo")
143 unitaglist.SadisUniTagList = append(unitaglist.SadisUniTagList, &sadisUnitaginfo)
Matteo Scandolo60676562019-03-15 14:56:25 -0700144 }
Daniele Moro11794a32020-03-05 18:04:58 -0800145 sadisSubscriber.UniTagList = unitaglist.SadisUniTagList
Matteo Scandolo60676562019-03-15 14:56:25 -0700146
Daniele Moro11794a32020-03-05 18:04:58 -0800147 sadisjson, e := json.Marshal(&sadisSubscriber)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700148 if e != nil {
149 log.Errorf("Unable to marshal JSON: %s", e)
150 http.Error(w, e.Error(), http.StatusInternalServerError)
151 return
152 }
Daniele Moro11794a32020-03-05 18:04:58 -0800153 w.Write(sadisjson)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700154 return
155 }
156 }
157
158 log.Debug("Checking devices")
159
160 devices := oltDevices{}
161
162 err = c.fetch("/xosapi/v1/volt/oltdevices", &devices)
163 if err != nil {
164 log.Errorf("Unable to retrieve device information from XOS: %s", err)
165 http.Error(w, err.Error(), http.StatusInternalServerError)
166 return
167
168 }
169
170 for _, device := range devices.OltDevices {
Matteo Scandolo5be70a12018-10-16 10:57:57 -0700171 // NOTE if it's an OLT then sadisRequestID is the device serial number
172 devID := device.SerialNumber
Jonathan Hartf86817b2018-08-17 10:35:54 -0700173 if devID == sadisRequestID {
174 log.Infof("Found OLT device with ID %s", devID)
Andy Baviera0095922018-09-07 12:49:38 -0700175
176 ipaddr := device.Host
177 addr, err := net.ResolveIPAddr("ip", device.Host)
178 if err != nil {
179 log.Errorf("Resolution error: %s", err.Error())
180 http.Error(w, err.Error(), http.StatusInternalServerError)
181 return
182 } else {
183 ipaddr = addr.String()
184 }
Matteo Scandoloddaa32c2019-05-22 10:55:45 -0700185 log.Debugf("ID: %s, Uplink: %d, IPAddress: %s, NasID: %s", devID, toInt(device.Uplink), ipaddr, device.NasID)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700186 sadisDevice := sadisDevice{
Matteo Scandoloa79e6082018-08-21 14:16:47 -0700187 ID: devID,
188 Uplink: toInt(device.Uplink),
Jonathan Hartf86817b2018-08-17 10:35:54 -0700189 HardwareID: "de:ad:be:ef:ba:11", // TODO do we really need to configure this?
Andy Baviera0095922018-09-07 12:49:38 -0700190 IPAddress: ipaddr,
Matteo Scandolo3f330cd2018-08-31 07:29:35 -0700191 NasID: device.NasID,
Jonathan Hartf86817b2018-08-17 10:35:54 -0700192 }
193
194 json, e := json.Marshal(&sadisDevice)
195 if e != nil {
196 log.Errorf("Unable to marshal JSON: %s", e)
197 http.Error(w, e.Error(), http.StatusInternalServerError)
198 return
199 }
200 w.Write(json)
201 return
202 }
203 }
204
Matteo Scandolo9a2772a2018-11-19 14:56:26 -0800205 log.WithFields(logrus.Fields{
206 "sadisId": sadisRequestID,
207 }).Infof("Couldn't find object %s in XOS database", sadisRequestID)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700208
209 http.NotFound(w, r)
210}
211
Matteo Scandolo60676562019-03-15 14:56:25 -0700212func (c *Config) getBandwidthProfileHandler(w http.ResponseWriter, r *http.Request) {
213 vars := mux.Vars(r)
214 sadisRequestID := vars["id"]
215
216 log.WithFields(logrus.Fields{
217 "sadisId": sadisRequestID,
218 }).Infof("Fetching BandwidthProfiles from XOS database")
219 defer r.Body.Close()
220
221 bandwidthprofiles := bandwidthprofiles{}
222
223 err := c.fetch("/xosapi/v1/rcord/bandwidthprofiles", &bandwidthprofiles)
224
225 if err != nil {
226 log.Errorf("Unable to retrieve bandwidth profiles information from XOS: %s", err)
227 http.Error(w, err.Error(), http.StatusInternalServerError)
228 return
229 }
230
231 for _, profile := range bandwidthprofiles.Profiles {
232 profileID := profile.Name
233 if profileID == sadisRequestID {
234 sadisProfile := sadisBandwidthProfile{
235 ID: profile.Name,
236 Cir: profile.Cir,
237 Cbs: profile.Cbs,
238 Eir: profile.Eir,
239 Ebs: profile.Ebs,
240 Air: profile.Air,
241 }
242 json, e := json.Marshal(&sadisProfile)
243 if e != nil {
244 log.Errorf("Unable to marshal JSON: %s", e)
245 http.Error(w, e.Error(), http.StatusInternalServerError)
246 return
247 }
248 w.Write(json)
249 return
250 }
251 }
252
253 log.WithFields(logrus.Fields{
254 "sadisId": sadisRequestID,
255 }).Infof("Couldn't find object %s in XOS database", sadisRequestID)
256
257 http.NotFound(w, r)
258}
259
260func (c *Config) getOneBandwidthProfileHandler(id int, data interface{}) error {
261 err := c.fetch("/xosapi/v1/rcord/bandwidthprofiles/"+strconv.Itoa(id), &data)
262 if err != nil {
263 log.Errorf("Unable to retrieve bandwidth profile information from XOS: %s", err)
264 return err
265 }
266 return nil
267}
268
Daniele Moro11794a32020-03-05 18:04:58 -0800269func (c *Config) getOneUniTagInfo(id int, data interface{}) error {
270 err := c.fetch("/xosapi/v1/rcord/rcordunitaginformations/"+strconv.Itoa(id), &data)
271 if err != nil {
272 log.Errorf("Unable to retrieve UNI Tag information from XOS: %s", err)
273 return err
274 }
275 return nil
276}
277
Jonathan Hartf86817b2018-08-17 10:35:54 -0700278func toInt(value string) int {
279 r, _ := strconv.Atoi(value)
280 return r
281}
282
283func (c *Config) fetch(path string, data interface{}) error {
284 resp, err := http.Get(c.connect + path)
Matteo Scandolo60676562019-03-15 14:56:25 -0700285
Jonathan Hartf86817b2018-08-17 10:35:54 -0700286 if err != nil {
287 return err
288 }
289
290 defer resp.Body.Close()
291 decoder := json.NewDecoder(resp.Body)
292 err = decoder.Decode(data)
Matteo Scandolo60676562019-03-15 14:56:25 -0700293 log.WithFields(logrus.Fields{
294 "path": path,
295 "resp": data,
296 "status": resp.Status,
297 }).Debug("Received data from XOS")
Jonathan Hartf86817b2018-08-17 10:35:54 -0700298 return err
299}