blob: 63f14f68bfa13e6e6cab82eb3ead440ba6c309f9 [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 Scandoloa79e6082018-08-21 14:16:47 -070053 ID: sub.OnuSerialNumber,
54 CTag: sub.CTag,
55 STag: sub.STag,
Jonathan Hartf86817b2018-08-17 10:35:54 -070056 NasPortID: sub.NasPortID,
57 CircuitID: sub.CircuitID,
Matteo Scandoloa79e6082018-08-21 14:16:47 -070058 RemoteID: sub.RemoteID,
Jonathan Hartf86817b2018-08-17 10:35:54 -070059 }
60
Matteo Scandolo60676562019-03-15 14:56:25 -070061 log.Debugf("Fetching bandwidth profiles for subscriber %s", sub.OnuSerialNumber)
62
63 dsBandwidthprofile := bandwidthprofile{}
64 err = c.getOneBandwidthProfileHandler(sub.DownstreamBandwidthProfile, &dsBandwidthprofile)
65 if err != nil {
66 log.Errorf("Cannot fetch downstream bandwidth profile %s for subscriber %s", strconv.Itoa(sub.DownstreamBandwidthProfile), sub.OnuSerialNumber)
67 http.Error(w, err.Error(), http.StatusInternalServerError)
68 return
69 }
70 if (bandwidthprofile{}) == dsBandwidthprofile {
71 // it's empty
72 log.WithFields(logrus.Fields{
73 "DownstreamBandwidthProfile": sub.DownstreamBandwidthProfile,
74 "Subscriber": sub.OnuSerialNumber,
75 "sadisId": sadisRequestID,
76 }).Error("Downstream bandwidth profile not found in XOS")
77 http.Error(w, "Downstream bandwidth profile not found in XOS", http.StatusInternalServerError)
78 return
79 }
80 sadisSubscriber.DownstreamBandwidthProfile = dsBandwidthprofile.Name
81
82 usBandwidthprofile := bandwidthprofile{}
83 err = c.getOneBandwidthProfileHandler(sub.UpstreamBandwidthProfile, &usBandwidthprofile)
84 if err != nil {
85 log.Errorf("Cannot fetch upstream bandwidth profile %s for subscriber %s", strconv.Itoa(sub.UpstreamBandwidthProfile), sub.OnuSerialNumber)
86 http.Error(w, err.Error(), http.StatusInternalServerError)
87 return
88 }
89 if (bandwidthprofile{}) == usBandwidthprofile {
90 // it's empty
91 log.WithFields(logrus.Fields{
92 "UpstreamBandwidthProfile": usBandwidthprofile.Name,
93 "Subscriber": sub.OnuSerialNumber,
94 "sadisId": sadisRequestID,
95 }).Error("Upstream bandwidth profile not found in XOS")
96 http.Error(w, "Upstream bandwidth profile not found in XOS", http.StatusInternalServerError)
97 return
98 }
99 sadisSubscriber.UpstreamBandwidthProfile = usBandwidthprofile.Name
100
101 log.WithFields(logrus.Fields{
102 "UpstreamBandwidthProfile": usBandwidthprofile.Name,
103 "DownstreamBandwidthProfile": dsBandwidthprofile.Name,
104 "Subscriber": sub.OnuSerialNumber,
105 "sadisId": sadisRequestID,
106 }).Debug("Bandwidth profiles for subscriber")
107
Jonathan Hartf86817b2018-08-17 10:35:54 -0700108 json, e := json.Marshal(&sadisSubscriber)
109 if e != nil {
110 log.Errorf("Unable to marshal JSON: %s", e)
111 http.Error(w, e.Error(), http.StatusInternalServerError)
112 return
113 }
114 w.Write(json)
115 return
116 }
117 }
118
119 log.Debug("Checking devices")
120
121 devices := oltDevices{}
122
123 err = c.fetch("/xosapi/v1/volt/oltdevices", &devices)
124 if err != nil {
125 log.Errorf("Unable to retrieve device information from XOS: %s", err)
126 http.Error(w, err.Error(), http.StatusInternalServerError)
127 return
128
129 }
130
131 for _, device := range devices.OltDevices {
Matteo Scandolo5be70a12018-10-16 10:57:57 -0700132 // NOTE if it's an OLT then sadisRequestID is the device serial number
133 devID := device.SerialNumber
Jonathan Hartf86817b2018-08-17 10:35:54 -0700134 if devID == sadisRequestID {
135 log.Infof("Found OLT device with ID %s", devID)
Andy Baviera0095922018-09-07 12:49:38 -0700136
137 ipaddr := device.Host
138 addr, err := net.ResolveIPAddr("ip", device.Host)
139 if err != nil {
140 log.Errorf("Resolution error: %s", err.Error())
141 http.Error(w, err.Error(), http.StatusInternalServerError)
142 return
143 } else {
144 ipaddr = addr.String()
145 }
146 log.Debugf("ID: %s, Uplink: %s, IPAddress: %s, NasID: %s", devID, toInt(device.Uplink), ipaddr, device.NasID)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700147 sadisDevice := sadisDevice{
Matteo Scandoloa79e6082018-08-21 14:16:47 -0700148 ID: devID,
149 Uplink: toInt(device.Uplink),
Jonathan Hartf86817b2018-08-17 10:35:54 -0700150 HardwareID: "de:ad:be:ef:ba:11", // TODO do we really need to configure this?
Andy Baviera0095922018-09-07 12:49:38 -0700151 IPAddress: ipaddr,
Matteo Scandolo3f330cd2018-08-31 07:29:35 -0700152 NasID: device.NasID,
Jonathan Hartf86817b2018-08-17 10:35:54 -0700153 }
154
155 json, e := json.Marshal(&sadisDevice)
156 if e != nil {
157 log.Errorf("Unable to marshal JSON: %s", e)
158 http.Error(w, e.Error(), http.StatusInternalServerError)
159 return
160 }
161 w.Write(json)
162 return
163 }
164 }
165
Matteo Scandolo9a2772a2018-11-19 14:56:26 -0800166 log.WithFields(logrus.Fields{
167 "sadisId": sadisRequestID,
168 }).Infof("Couldn't find object %s in XOS database", sadisRequestID)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700169
170 http.NotFound(w, r)
171}
172
Matteo Scandolo60676562019-03-15 14:56:25 -0700173func (c *Config) getBandwidthProfileHandler(w http.ResponseWriter, r *http.Request) {
174 vars := mux.Vars(r)
175 sadisRequestID := vars["id"]
176
177 log.WithFields(logrus.Fields{
178 "sadisId": sadisRequestID,
179 }).Infof("Fetching BandwidthProfiles from XOS database")
180 defer r.Body.Close()
181
182 bandwidthprofiles := bandwidthprofiles{}
183
184 err := c.fetch("/xosapi/v1/rcord/bandwidthprofiles", &bandwidthprofiles)
185
186 if err != nil {
187 log.Errorf("Unable to retrieve bandwidth profiles information from XOS: %s", err)
188 http.Error(w, err.Error(), http.StatusInternalServerError)
189 return
190 }
191
192 for _, profile := range bandwidthprofiles.Profiles {
193 profileID := profile.Name
194 if profileID == sadisRequestID {
195 sadisProfile := sadisBandwidthProfile{
196 ID: profile.Name,
197 Cir: profile.Cir,
198 Cbs: profile.Cbs,
199 Eir: profile.Eir,
200 Ebs: profile.Ebs,
201 Air: profile.Air,
202 }
203 json, e := json.Marshal(&sadisProfile)
204 if e != nil {
205 log.Errorf("Unable to marshal JSON: %s", e)
206 http.Error(w, e.Error(), http.StatusInternalServerError)
207 return
208 }
209 w.Write(json)
210 return
211 }
212 }
213
214 log.WithFields(logrus.Fields{
215 "sadisId": sadisRequestID,
216 }).Infof("Couldn't find object %s in XOS database", sadisRequestID)
217
218 http.NotFound(w, r)
219}
220
221func (c *Config) getOneBandwidthProfileHandler(id int, data interface{}) error {
222 err := c.fetch("/xosapi/v1/rcord/bandwidthprofiles/"+strconv.Itoa(id), &data)
223 if err != nil {
224 log.Errorf("Unable to retrieve bandwidth profile information from XOS: %s", err)
225 return err
226 }
227 return nil
228}
229
Jonathan Hartf86817b2018-08-17 10:35:54 -0700230func toInt(value string) int {
231 r, _ := strconv.Atoi(value)
232 return r
233}
234
235func (c *Config) fetch(path string, data interface{}) error {
236 resp, err := http.Get(c.connect + path)
Matteo Scandolo60676562019-03-15 14:56:25 -0700237
Jonathan Hartf86817b2018-08-17 10:35:54 -0700238 if err != nil {
239 return err
240 }
241
242 defer resp.Body.Close()
243 decoder := json.NewDecoder(resp.Body)
244 err = decoder.Decode(data)
Matteo Scandolo60676562019-03-15 14:56:25 -0700245 log.WithFields(logrus.Fields{
246 "path": path,
247 "resp": data,
248 "status": resp.Status,
249 }).Debug("Received data from XOS")
Jonathan Hartf86817b2018-08-17 10:35:54 -0700250 return err
251}