blob: 791073ef01f0942337c666b9b12d5b0065e1329a [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 (
17 "strconv"
18 "encoding/json"
19 "net/http"
20 "github.com/gorilla/mux"
21)
22
23
24func (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{
48 ID: sub.OnuSerialNumber,
49 CTag: sub.CTag,
50 STag: sub.STag,
51 NasPortID: sub.NasPortID,
52 CircuitID: sub.CircuitID,
53 RemoteID: sub.CircuitID, // TODO get a proper value here
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{
84 ID: devID,
85 Uplink: toInt(device.Uplink),
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
105func toInt(value string) int {
106 r, _ := strconv.Atoi(value)
107 return r
108}
109
110func (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}