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