blob: c85f239bf4b19b05ece3d3f49fed37bad78185e9 [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"
Andy Baviera0095922018-09-07 12:49:38 -070018 "net"
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
80 for _, device := range devices.OltDevices {
Matteo Scandolo5be70a12018-10-16 10:57:57 -070081 // NOTE if it's an OLT then sadisRequestID is the device serial number
82 devID := device.SerialNumber
Jonathan Hartf86817b2018-08-17 10:35:54 -070083 if devID == sadisRequestID {
84 log.Infof("Found OLT device with ID %s", devID)
Andy Baviera0095922018-09-07 12:49:38 -070085
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 Hartf86817b2018-08-17 10:35:54 -070096 sadisDevice := sadisDevice{
Matteo Scandoloa79e6082018-08-21 14:16:47 -070097 ID: devID,
98 Uplink: toInt(device.Uplink),
Jonathan Hartf86817b2018-08-17 10:35:54 -070099 HardwareID: "de:ad:be:ef:ba:11", // TODO do we really need to configure this?
Andy Baviera0095922018-09-07 12:49:38 -0700100 IPAddress: ipaddr,
Matteo Scandolo3f330cd2018-08-31 07:29:35 -0700101 NasID: device.NasID,
Jonathan Hartf86817b2018-08-17 10:35:54 -0700102 }
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
120func toInt(value string) int {
121 r, _ := strconv.Atoi(value)
122 return r
123}
124
125func (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}