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" |
Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 18 | "github.com/sirupsen/logrus" |
Andy Bavier | a009592 | 2018-09-07 12:49:38 -0700 | [diff] [blame] | 19 | "net" |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 20 | "net/http" |
Matteo Scandolo | a79e608 | 2018-08-21 14:16:47 -0700 | [diff] [blame] | 21 | "strconv" |
| 22 | |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 23 | "github.com/gorilla/mux" |
| 24 | ) |
| 25 | |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 26 | func (c *Config) getSubscriberHandler(w http.ResponseWriter, r *http.Request) { |
| 27 | vars := mux.Vars(r) |
| 28 | sadisRequestID := vars["id"] |
| 29 | |
Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 30 | log.WithFields(logrus.Fields{ |
| 31 | "sadisId": sadisRequestID, |
| 32 | }).Infof("Looking for object %s in XOS database", sadisRequestID) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 33 | |
| 34 | defer r.Body.Close() |
| 35 | |
| 36 | subscribers := subscribers{} |
| 37 | |
| 38 | log.Debug("Checking subscribers") |
| 39 | |
| 40 | err := c.fetch("/xosapi/v1/rcord/rcordsubscribers", &subscribers) |
| 41 | if err != nil { |
| 42 | log.Errorf("Unable to retrieve subscriber information from XOS: %s", err) |
| 43 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 44 | return |
| 45 | |
| 46 | } |
| 47 | |
| 48 | for _, sub := range subscribers.Subscribers { |
| 49 | if sub.OnuSerialNumber == sadisRequestID { |
| 50 | log.Infof("Found subscriber with ID %s", sub.OnuSerialNumber) |
| 51 | sadisSubscriber := sadisSubscriber{ |
Matteo Scandolo | a79e608 | 2018-08-21 14:16:47 -0700 | [diff] [blame] | 52 | ID: sub.OnuSerialNumber, |
| 53 | CTag: sub.CTag, |
| 54 | STag: sub.STag, |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 55 | NasPortID: sub.NasPortID, |
| 56 | CircuitID: sub.CircuitID, |
Matteo Scandolo | a79e608 | 2018-08-21 14:16:47 -0700 | [diff] [blame] | 57 | RemoteID: sub.RemoteID, |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | json, e := json.Marshal(&sadisSubscriber) |
| 61 | if e != nil { |
| 62 | log.Errorf("Unable to marshal JSON: %s", e) |
| 63 | http.Error(w, e.Error(), http.StatusInternalServerError) |
| 64 | return |
| 65 | } |
| 66 | w.Write(json) |
| 67 | return |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | log.Debug("Checking devices") |
| 72 | |
| 73 | devices := oltDevices{} |
| 74 | |
| 75 | err = c.fetch("/xosapi/v1/volt/oltdevices", &devices) |
| 76 | if err != nil { |
| 77 | log.Errorf("Unable to retrieve device information from XOS: %s", err) |
| 78 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 79 | return |
| 80 | |
| 81 | } |
| 82 | |
| 83 | for _, device := range devices.OltDevices { |
Matteo Scandolo | 5be70a1 | 2018-10-16 10:57:57 -0700 | [diff] [blame] | 84 | // NOTE if it's an OLT then sadisRequestID is the device serial number |
| 85 | devID := device.SerialNumber |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 86 | if devID == sadisRequestID { |
| 87 | log.Infof("Found OLT device with ID %s", devID) |
Andy Bavier | a009592 | 2018-09-07 12:49:38 -0700 | [diff] [blame] | 88 | |
| 89 | ipaddr := device.Host |
| 90 | addr, err := net.ResolveIPAddr("ip", device.Host) |
| 91 | if err != nil { |
| 92 | log.Errorf("Resolution error: %s", err.Error()) |
| 93 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 94 | return |
| 95 | } else { |
| 96 | ipaddr = addr.String() |
| 97 | } |
| 98 | 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] | 99 | sadisDevice := sadisDevice{ |
Matteo Scandolo | a79e608 | 2018-08-21 14:16:47 -0700 | [diff] [blame] | 100 | ID: devID, |
| 101 | Uplink: toInt(device.Uplink), |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 102 | 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] | 103 | IPAddress: ipaddr, |
Matteo Scandolo | 3f330cd | 2018-08-31 07:29:35 -0700 | [diff] [blame] | 104 | NasID: device.NasID, |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | json, e := json.Marshal(&sadisDevice) |
| 108 | if e != nil { |
| 109 | log.Errorf("Unable to marshal JSON: %s", e) |
| 110 | http.Error(w, e.Error(), http.StatusInternalServerError) |
| 111 | return |
| 112 | } |
| 113 | w.Write(json) |
| 114 | return |
| 115 | } |
| 116 | } |
| 117 | |
Matteo Scandolo | 9a2772a | 2018-11-19 14:56:26 -0800 | [diff] [blame] | 118 | log.WithFields(logrus.Fields{ |
| 119 | "sadisId": sadisRequestID, |
| 120 | }).Infof("Couldn't find object %s in XOS database", sadisRequestID) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 121 | |
| 122 | http.NotFound(w, r) |
| 123 | } |
| 124 | |
| 125 | func toInt(value string) int { |
| 126 | r, _ := strconv.Atoi(value) |
| 127 | return r |
| 128 | } |
| 129 | |
| 130 | func (c *Config) fetch(path string, data interface{}) error { |
| 131 | resp, err := http.Get(c.connect + path) |
| 132 | if err != nil { |
| 133 | return err |
| 134 | } |
| 135 | |
| 136 | defer resp.Body.Close() |
| 137 | decoder := json.NewDecoder(resp.Body) |
| 138 | err = decoder.Decode(data) |
| 139 | return err |
| 140 | } |