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