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 | 04428fd | 2019-04-17 14:22:30 -0700 | [diff] [blame^] | 53 | 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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Matteo Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame] | 62 | 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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 109 | 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 Scandolo | 5be70a1 | 2018-10-16 10:57:57 -0700 | [diff] [blame] | 133 | // NOTE if it's an OLT then sadisRequestID is the device serial number |
| 134 | devID := device.SerialNumber |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 135 | if devID == sadisRequestID { |
| 136 | log.Infof("Found OLT device with ID %s", devID) |
Andy Bavier | a009592 | 2018-09-07 12:49:38 -0700 | [diff] [blame] | 137 | |
| 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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 148 | sadisDevice := sadisDevice{ |
Matteo Scandolo | a79e608 | 2018-08-21 14:16:47 -0700 | [diff] [blame] | 149 | ID: devID, |
| 150 | Uplink: toInt(device.Uplink), |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 151 | 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] | 152 | IPAddress: ipaddr, |
Matteo Scandolo | 3f330cd | 2018-08-31 07:29:35 -0700 | [diff] [blame] | 153 | NasID: device.NasID, |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 154 | } |
| 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 Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 167 | log.WithFields(logrus.Fields{ |
| 168 | "sadisId": sadisRequestID, |
| 169 | }).Infof("Couldn't find object %s in XOS database", sadisRequestID) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 170 | |
| 171 | http.NotFound(w, r) |
| 172 | } |
| 173 | |
Matteo Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame] | 174 | func (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 | |
| 222 | func (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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 231 | func toInt(value string) int { |
| 232 | r, _ := strconv.Atoi(value) |
| 233 | return r |
| 234 | } |
| 235 | |
| 236 | func (c *Config) fetch(path string, data interface{}) error { |
| 237 | resp, err := http.Get(c.connect + path) |
Matteo Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame] | 238 | |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 239 | 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 Scandolo | 6067656 | 2019-03-15 14:56:25 -0700 | [diff] [blame] | 246 | log.WithFields(logrus.Fields{ |
| 247 | "path": path, |
| 248 | "resp": data, |
| 249 | "status": resp.Status, |
| 250 | }).Debug("Received data from XOS") |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 251 | return err |
| 252 | } |