blob: b82b6fc476eac1ef407c13bef06ef4e2017e95ad [file] [log] [blame]
Jonathan Hartf86817b2018-08-17 10:35:54 -07001// 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.
14package main
15
16import (
Jonathan Hartf86817b2018-08-17 10:35:54 -070017 "encoding/json"
Matteo Scandolo3f330cd2018-08-31 07:29:35 -070018 "fmt"
Jonathan Hartf86817b2018-08-17 10:35:54 -070019 "net/http"
Matteo Scandoloa79e6082018-08-21 14:16:47 -070020 "strconv"
21
Jonathan Hartf86817b2018-08-17 10:35:54 -070022 "github.com/gorilla/mux"
23)
24
Jonathan Hartf86817b2018-08-17 10:35:54 -070025func (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 Scandoloa79e6082018-08-21 14:16:47 -070049 ID: sub.OnuSerialNumber,
50 CTag: sub.CTag,
51 STag: sub.STag,
Jonathan Hartf86817b2018-08-17 10:35:54 -070052 NasPortID: sub.NasPortID,
53 CircuitID: sub.CircuitID,
Matteo Scandoloa79e6082018-08-21 14:16:47 -070054 RemoteID: sub.RemoteID,
Jonathan Hartf86817b2018-08-17 10:35:54 -070055 }
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
Matteo Scandolo3f330cd2018-08-31 07:29:35 -070080 fmt.Println(devices)
81
Jonathan Hartf86817b2018-08-17 10:35:54 -070082 for _, device := range devices.OltDevices {
83 devID := device.Host + ":" + strconv.Itoa(device.Port)
84 if devID == sadisRequestID {
85 log.Infof("Found OLT device with ID %s", devID)
Matteo Scandolo3f330cd2018-08-31 07:29:35 -070086 log.Debugf("ID: %s, Uplink: %s, IPAddress: %s, NasID: %s", devID, toInt(device.Uplink), device.Host, device.NasID)
Jonathan Hartf86817b2018-08-17 10:35:54 -070087 sadisDevice := sadisDevice{
Matteo Scandoloa79e6082018-08-21 14:16:47 -070088 ID: devID,
89 Uplink: toInt(device.Uplink),
Jonathan Hartf86817b2018-08-17 10:35:54 -070090 HardwareID: "de:ad:be:ef:ba:11", // TODO do we really need to configure this?
Matteo Scandolo3f330cd2018-08-31 07:29:35 -070091 IPAddress: device.Host,
92 NasID: device.NasID,
Jonathan Hartf86817b2018-08-17 10:35:54 -070093 }
94
95 json, e := json.Marshal(&sadisDevice)
96 if e != nil {
97 log.Errorf("Unable to marshal JSON: %s", e)
98 http.Error(w, e.Error(), http.StatusInternalServerError)
99 return
100 }
101 w.Write(json)
102 return
103 }
104 }
105
106 log.Infof("Couldn't find object %s in XOS database", sadisRequestID)
107
108 http.NotFound(w, r)
109}
110
111func toInt(value string) int {
112 r, _ := strconv.Atoi(value)
113 return r
114}
115
116func (c *Config) fetch(path string, data interface{}) error {
117 resp, err := http.Get(c.connect + path)
118 if err != nil {
119 return err
120 }
121
122 defer resp.Body.Close()
123 decoder := json.NewDecoder(resp.Body)
124 err = decoder.Decode(data)
125 return err
126}