blob: fc286478277992a63dc67e8ac346220b1d82436e [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 Scandolo9a2772a2018-11-19 14:56:26 -080018 "github.com/sirupsen/logrus"
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
Matteo Scandolo9a2772a2018-11-19 14:56:26 -080030 log.WithFields(logrus.Fields{
31 "sadisId": sadisRequestID,
32 }).Infof("Looking for object %s in XOS database", sadisRequestID)
Jonathan Hartf86817b2018-08-17 10:35:54 -070033
34 defer r.Body.Close()
35
36 subscribers := subscribers{}
37
38 log.Debug("Checking subscribers")
39
40 err := c.fetch("/xosapi/v1/rcord/rcordsubscribers", &subscribers)
41 if err != nil {
42 log.Errorf("Unable to retrieve subscriber information from XOS: %s", err)
43 http.Error(w, err.Error(), http.StatusInternalServerError)
44 return
45
46 }
47
48 for _, sub := range subscribers.Subscribers {
49 if sub.OnuSerialNumber == sadisRequestID {
50 log.Infof("Found subscriber with ID %s", sub.OnuSerialNumber)
51 sadisSubscriber := sadisSubscriber{
Matteo Scandoloa79e6082018-08-21 14:16:47 -070052 ID: sub.OnuSerialNumber,
53 CTag: sub.CTag,
54 STag: sub.STag,
Jonathan Hartf86817b2018-08-17 10:35:54 -070055 NasPortID: sub.NasPortID,
56 CircuitID: sub.CircuitID,
Matteo Scandoloa79e6082018-08-21 14:16:47 -070057 RemoteID: sub.RemoteID,
Jonathan Hartf86817b2018-08-17 10:35:54 -070058 }
59
60 json, e := json.Marshal(&sadisSubscriber)
61 if e != nil {
62 log.Errorf("Unable to marshal JSON: %s", e)
63 http.Error(w, e.Error(), http.StatusInternalServerError)
64 return
65 }
66 w.Write(json)
67 return
68 }
69 }
70
71 log.Debug("Checking devices")
72
73 devices := oltDevices{}
74
75 err = c.fetch("/xosapi/v1/volt/oltdevices", &devices)
76 if err != nil {
77 log.Errorf("Unable to retrieve device information from XOS: %s", err)
78 http.Error(w, err.Error(), http.StatusInternalServerError)
79 return
80
81 }
82
83 for _, device := range devices.OltDevices {
Matteo Scandolo5be70a12018-10-16 10:57:57 -070084 // NOTE if it's an OLT then sadisRequestID is the device serial number
85 devID := device.SerialNumber
Jonathan Hartf86817b2018-08-17 10:35:54 -070086 if devID == sadisRequestID {
87 log.Infof("Found OLT device with ID %s", devID)
Andy Baviera0095922018-09-07 12:49:38 -070088
89 ipaddr := device.Host
90 addr, err := net.ResolveIPAddr("ip", device.Host)
91 if err != nil {
92 log.Errorf("Resolution error: %s", err.Error())
93 http.Error(w, err.Error(), http.StatusInternalServerError)
94 return
95 } else {
96 ipaddr = addr.String()
97 }
98 log.Debugf("ID: %s, Uplink: %s, IPAddress: %s, NasID: %s", devID, toInt(device.Uplink), ipaddr, device.NasID)
Jonathan Hartf86817b2018-08-17 10:35:54 -070099 sadisDevice := sadisDevice{
Matteo Scandoloa79e6082018-08-21 14:16:47 -0700100 ID: devID,
101 Uplink: toInt(device.Uplink),
Jonathan Hartf86817b2018-08-17 10:35:54 -0700102 HardwareID: "de:ad:be:ef:ba:11", // TODO do we really need to configure this?
Andy Baviera0095922018-09-07 12:49:38 -0700103 IPAddress: ipaddr,
Matteo Scandolo3f330cd2018-08-31 07:29:35 -0700104 NasID: device.NasID,
Jonathan Hartf86817b2018-08-17 10:35:54 -0700105 }
106
107 json, e := json.Marshal(&sadisDevice)
108 if e != nil {
109 log.Errorf("Unable to marshal JSON: %s", e)
110 http.Error(w, e.Error(), http.StatusInternalServerError)
111 return
112 }
113 w.Write(json)
114 return
115 }
116 }
117
Matteo Scandolo9a2772a2018-11-19 14:56:26 -0800118 log.WithFields(logrus.Fields{
119 "sadisId": sadisRequestID,
120 }).Infof("Couldn't find object %s in XOS database", sadisRequestID)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700121
122 http.NotFound(w, r)
123}
124
125func toInt(value string) int {
126 r, _ := strconv.Atoi(value)
127 return r
128}
129
130func (c *Config) fetch(path string, data interface{}) error {
131 resp, err := http.Get(c.connect + path)
132 if err != nil {
133 return err
134 }
135
136 defer resp.Body.Close()
137 decoder := json.NewDecoder(resp.Body)
138 err = decoder.Decode(data)
139 return err
140}