blob: 89a4a99bdd556cb22228f26a80857f66970f597d [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
2* Copyright 2022-present Open Networking Foundation
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.
14*/
15
16package nbi
17
18import (
19 "encoding/json"
20 "net/http"
21
22 app "voltha-go-controller/internal/pkg/application"
Tinoj Joseph1d108322022-07-13 10:07:39 +053023 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053024)
25
26// DeviceIDListHandle Handle DeviceIDList Requests
27type DeviceIDListHandle struct {
28}
29
30// ServeHTTP to serve HTTP requests
31func (dh *DeviceIDListHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
32 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
33 switch r.Method {
34 case "GET":
35 dh.GetDeviceIDList(w, r)
36 default:
37 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
38 }
39}
40
41// GetDeviceIDList to get device id list
42func (dh *DeviceIDListHandle) GetDeviceIDList(w http.ResponseWriter, r *http.Request) {
43
44 va := app.GetApplication()
45 var deviceID string
46 var deviceIDListResp []string
47
48 getDeviceIDList := func(key, value interface{}) bool {
49 voltDevice := value.(*app.VoltDevice)
50 deviceID = voltDevice.Name
51 deviceIDListResp = append(deviceIDListResp, deviceID)
52 return true
53 }
54 va.DevicesDisc.Range(getDeviceIDList)
55
56 deviceIDListJSON, err := json.Marshal(deviceIDListResp)
57 if err != nil {
58 logger.Errorw(ctx, "Error occurred while marshaling device id list response", log.Fields{"Error": err})
59 w.WriteHeader(http.StatusInternalServerError)
60 return
61 }
62
63 w.Header().Add("Content-Type", "application/json")
64 _, err = w.Write(deviceIDListJSON)
65 if err != nil {
66 logger.Errorw(ctx, "error in sending deviceIDList response", log.Fields{"Error": err})
67 w.WriteHeader(http.StatusInternalServerError)
68 }
69
70}