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" |
| 18 | "net/http" |
Matteo Scandolo | a79e608 | 2018-08-21 14:16:47 -0700 | [diff] [blame^] | 19 | "strconv" |
| 20 | |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 21 | "github.com/gorilla/mux" |
| 22 | ) |
| 23 | |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 24 | func (c *Config) getSubscriberHandler(w http.ResponseWriter, r *http.Request) { |
| 25 | vars := mux.Vars(r) |
| 26 | sadisRequestID := vars["id"] |
| 27 | |
| 28 | log.Infof("Looking for object %s in XOS database", sadisRequestID) |
| 29 | |
| 30 | defer r.Body.Close() |
| 31 | |
| 32 | subscribers := subscribers{} |
| 33 | |
| 34 | log.Debug("Checking subscribers") |
| 35 | |
| 36 | err := c.fetch("/xosapi/v1/rcord/rcordsubscribers", &subscribers) |
| 37 | if err != nil { |
| 38 | log.Errorf("Unable to retrieve subscriber information from XOS: %s", err) |
| 39 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 40 | return |
| 41 | |
| 42 | } |
| 43 | |
| 44 | for _, sub := range subscribers.Subscribers { |
| 45 | if sub.OnuSerialNumber == sadisRequestID { |
| 46 | log.Infof("Found subscriber with ID %s", sub.OnuSerialNumber) |
| 47 | sadisSubscriber := sadisSubscriber{ |
Matteo Scandolo | a79e608 | 2018-08-21 14:16:47 -0700 | [diff] [blame^] | 48 | ID: sub.OnuSerialNumber, |
| 49 | CTag: sub.CTag, |
| 50 | STag: sub.STag, |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 51 | NasPortID: sub.NasPortID, |
| 52 | CircuitID: sub.CircuitID, |
Matteo Scandolo | a79e608 | 2018-08-21 14:16:47 -0700 | [diff] [blame^] | 53 | RemoteID: sub.RemoteID, |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | json, e := json.Marshal(&sadisSubscriber) |
| 57 | if e != nil { |
| 58 | log.Errorf("Unable to marshal JSON: %s", e) |
| 59 | http.Error(w, e.Error(), http.StatusInternalServerError) |
| 60 | return |
| 61 | } |
| 62 | w.Write(json) |
| 63 | return |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | log.Debug("Checking devices") |
| 68 | |
| 69 | devices := oltDevices{} |
| 70 | |
| 71 | err = c.fetch("/xosapi/v1/volt/oltdevices", &devices) |
| 72 | if err != nil { |
| 73 | log.Errorf("Unable to retrieve device information from XOS: %s", err) |
| 74 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 75 | return |
| 76 | |
| 77 | } |
| 78 | |
| 79 | for _, device := range devices.OltDevices { |
| 80 | devID := device.Host + ":" + strconv.Itoa(device.Port) |
| 81 | if devID == sadisRequestID { |
| 82 | log.Infof("Found OLT device with ID %s", devID) |
| 83 | sadisDevice := sadisDevice{ |
Matteo Scandolo | a79e608 | 2018-08-21 14:16:47 -0700 | [diff] [blame^] | 84 | ID: devID, |
| 85 | Uplink: toInt(device.Uplink), |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 86 | HardwareID: "de:ad:be:ef:ba:11", // TODO do we really need to configure this? |
| 87 | } |
| 88 | |
| 89 | json, e := json.Marshal(&sadisDevice) |
| 90 | if e != nil { |
| 91 | log.Errorf("Unable to marshal JSON: %s", e) |
| 92 | http.Error(w, e.Error(), http.StatusInternalServerError) |
| 93 | return |
| 94 | } |
| 95 | w.Write(json) |
| 96 | return |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | log.Infof("Couldn't find object %s in XOS database", sadisRequestID) |
| 101 | |
| 102 | http.NotFound(w, r) |
| 103 | } |
| 104 | |
| 105 | func toInt(value string) int { |
| 106 | r, _ := strconv.Atoi(value) |
| 107 | return r |
| 108 | } |
| 109 | |
| 110 | func (c *Config) fetch(path string, data interface{}) error { |
| 111 | resp, err := http.Get(c.connect + path) |
| 112 | if err != nil { |
| 113 | return err |
| 114 | } |
| 115 | |
| 116 | defer resp.Body.Close() |
| 117 | decoder := json.NewDecoder(resp.Body) |
| 118 | err = decoder.Decode(data) |
| 119 | return err |
| 120 | } |