Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 1 | // 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. |
| 14 | package main |
| 15 | |
| 16 | import ( |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 17 | "encoding/json" |
Andy Bavier | a009592 | 2018-09-07 12:49:38 -0700 | [diff] [blame] | 18 | "net" |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 19 | "net/http" |
Matteo Scandolo | a79e608 | 2018-08-21 14:16:47 -0700 | [diff] [blame] | 20 | "strconv" |
| 21 | |
Matteo Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame^] | 22 | "github.com/sirupsen/logrus" |
| 23 | |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 24 | "github.com/gorilla/mux" |
| 25 | ) |
| 26 | |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 27 | func (c *Config) getSubscriberHandler(w http.ResponseWriter, r *http.Request) { |
| 28 | vars := mux.Vars(r) |
| 29 | sadisRequestID := vars["id"] |
| 30 | |
Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 31 | log.WithFields(logrus.Fields{ |
| 32 | "sadisId": sadisRequestID, |
| 33 | }).Infof("Looking for object %s in XOS database", sadisRequestID) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 34 | |
| 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 Scandolo | a79e608 | 2018-08-21 14:16:47 -0700 | [diff] [blame] | 53 | ID: sub.OnuSerialNumber, |
| 54 | CTag: sub.CTag, |
| 55 | STag: sub.STag, |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 56 | NasPortID: sub.NasPortID, |
| 57 | CircuitID: sub.CircuitID, |
Matteo Scandolo | a79e608 | 2018-08-21 14:16:47 -0700 | [diff] [blame] | 58 | RemoteID: sub.RemoteID, |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Matteo Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame^] | 61 | 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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 108 | 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 Scandolo | 5be70a1 | 2018-10-16 10:57:57 -0700 | [diff] [blame] | 132 | // NOTE if it's an OLT then sadisRequestID is the device serial number |
| 133 | devID := device.SerialNumber |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 134 | if devID == sadisRequestID { |
| 135 | log.Infof("Found OLT device with ID %s", devID) |
Andy Bavier | a009592 | 2018-09-07 12:49:38 -0700 | [diff] [blame] | 136 | |
| 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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 147 | sadisDevice := sadisDevice{ |
Matteo Scandolo | a79e608 | 2018-08-21 14:16:47 -0700 | [diff] [blame] | 148 | ID: devID, |
| 149 | Uplink: toInt(device.Uplink), |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 150 | HardwareID: "de:ad:be:ef:ba:11", // TODO do we really need to configure this? |
Andy Bavier | a009592 | 2018-09-07 12:49:38 -0700 | [diff] [blame] | 151 | IPAddress: ipaddr, |
Matteo Scandolo | 3f330cd | 2018-08-31 07:29:35 -0700 | [diff] [blame] | 152 | NasID: device.NasID, |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 153 | } |
| 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 Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 166 | log.WithFields(logrus.Fields{ |
| 167 | "sadisId": sadisRequestID, |
| 168 | }).Infof("Couldn't find object %s in XOS database", sadisRequestID) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 169 | |
| 170 | http.NotFound(w, r) |
| 171 | } |
| 172 | |
Matteo Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame^] | 173 | func (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 | |
| 221 | func (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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 230 | func toInt(value string) int { |
| 231 | r, _ := strconv.Atoi(value) |
| 232 | return r |
| 233 | } |
| 234 | |
| 235 | func (c *Config) fetch(path string, data interface{}) error { |
| 236 | resp, err := http.Get(c.connect + path) |
Matteo Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame^] | 237 | |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 238 | 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 Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame^] | 245 | log.WithFields(logrus.Fields{ |
| 246 | "path": path, |
| 247 | "resp": data, |
| 248 | "status": resp.Status, |
| 249 | }).Debug("Received data from XOS") |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 250 | return err |
| 251 | } |