blob: d030fa7e41bbc55c8a0f6e778faf0b59b28482ac [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"
Andy Baviera0095922018-09-07 12:49:38 -070019 "net"
Jonathan Hartf86817b2018-08-17 10:35:54 -070020 "net/http"
Matteo Scandoloa79e6082018-08-21 14:16:47 -070021 "strconv"
22
Jonathan Hartf86817b2018-08-17 10:35:54 -070023 "github.com/gorilla/mux"
24)
25
Jonathan Hartf86817b2018-08-17 10:35:54 -070026func (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 Scandoloa79e6082018-08-21 14:16:47 -070050 ID: sub.OnuSerialNumber,
51 CTag: sub.CTag,
52 STag: sub.STag,
Jonathan Hartf86817b2018-08-17 10:35:54 -070053 NasPortID: sub.NasPortID,
54 CircuitID: sub.CircuitID,
Matteo Scandoloa79e6082018-08-21 14:16:47 -070055 RemoteID: sub.RemoteID,
Jonathan Hartf86817b2018-08-17 10:35:54 -070056 }
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 Scandolo3f330cd2018-08-31 07:29:35 -070081 fmt.Println(devices)
82
Jonathan Hartf86817b2018-08-17 10:35:54 -070083 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 Baviera0095922018-09-07 12:49:38 -070087
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 Hartf86817b2018-08-17 10:35:54 -070098 sadisDevice := sadisDevice{
Matteo Scandoloa79e6082018-08-21 14:16:47 -070099 ID: devID,
100 Uplink: toInt(device.Uplink),
Jonathan Hartf86817b2018-08-17 10:35:54 -0700101 HardwareID: "de:ad:be:ef:ba:11", // TODO do we really need to configure this?
Andy Baviera0095922018-09-07 12:49:38 -0700102 IPAddress: ipaddr,
Matteo Scandolo3f330cd2018-08-31 07:29:35 -0700103 NasID: device.NasID,
Jonathan Hartf86817b2018-08-17 10:35:54 -0700104 }
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
122func toInt(value string) int {
123 r, _ := strconv.Atoi(value)
124 return r
125}
126
127func (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}