blob: 72c5433bf7f9a2616a409baf5cec6f49919fd750 [file] [log] [blame]
Tinoj Joseph429b9d92022-11-16 18:51:05 +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.
Akash Soni1db5e492023-03-27 14:14:07 +053014 */
Tinoj Joseph429b9d92022-11-16 18:51:05 +053015
vinokuma926cb3e2023-03-29 11:41:06 +053016package onosnbi
Tinoj Joseph429b9d92022-11-16 18:51:05 +053017
18import (
19 "encoding/json"
20 "net/http"
21
22 app "voltha-go-controller/internal/pkg/application"
23 "voltha-go-controller/log"
Akash Soni1db5e492023-03-27 14:14:07 +053024
25 "github.com/gorilla/mux"
Tinoj Joseph429b9d92022-11-16 18:51:05 +053026)
27
28// DeviceHandle Handle DeviceIDList Requests
29type DeviceHandle struct {
30}
31
32// DevicePortHandle Handle Ports Requests
33type DevicePortHandle struct {
34}
35
36// ServeHTTP to serve HTTP requests
37func (dh *DeviceHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
38 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
39 switch r.Method {
vinokuma926cb3e2023-03-29 11:41:06 +053040 case cGet:
Tinoj Joseph429b9d92022-11-16 18:51:05 +053041 dh.GetDeviceList(w, r)
42 default:
43 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
44 }
45}
Akash Soni1db5e492023-03-27 14:14:07 +053046
Tinoj Joseph429b9d92022-11-16 18:51:05 +053047// GetDeviceList to get device id list
48func (dh *DeviceHandle) GetDeviceList(w http.ResponseWriter, r *http.Request) {
Tinoj Joseph429b9d92022-11-16 18:51:05 +053049 va := app.GetApplication()
50 var deviceListResp DeviceEntry
51 deviceListResp.Devices = []Device{}
52
53 getDeviceList := func(key, value interface{}) bool {
54 voltDevice := value.(*app.VoltDevice)
55 device := convertVoltDeviceToDevice(voltDevice)
56 deviceListResp.Devices = append(deviceListResp.Devices, device)
57 return true
58 }
59 va.DevicesDisc.Range(getDeviceList)
60
61 deviceListJSON, err := json.Marshal(deviceListResp)
62 if err != nil {
63 logger.Errorw(ctx, "Error occurred while marshaling device list response", log.Fields{"Error": err})
64 w.WriteHeader(http.StatusInternalServerError)
65 return
66 }
67
68 w.Header().Add("Content-Type", "application/json")
69 _, err = w.Write(deviceListJSON)
70 if err != nil {
71 logger.Errorw(ctx, "error in sending deviceList response", log.Fields{"Error": err})
72 w.WriteHeader(http.StatusInternalServerError)
73 }
74}
75
76// ServeHTTP to serve HTTP requests
77func (dh *DevicePortHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
78 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
79 switch r.Method {
vinokuma926cb3e2023-03-29 11:41:06 +053080 case cGet:
Tinoj Joseph429b9d92022-11-16 18:51:05 +053081 dh.GetPortList(w, r)
82 default:
83 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
84 }
85}
86
Tinoj Josephaf37ce82022-12-28 11:59:43 +053087// ServeHTTPWithDeviceID to serve HTTP request for ports with deviceID
88func (dh *DevicePortHandle) ServeHTTPWithDeviceID(w http.ResponseWriter, r *http.Request) {
89 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
90 switch r.Method {
vinokuma926cb3e2023-03-29 11:41:06 +053091 case cGet:
Tinoj Josephaf37ce82022-12-28 11:59:43 +053092 dh.GetPortListPerDevice(w, r)
93 default:
94 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
95 }
96}
97
98// GetPortListPerDevice to get port list for a given device
99func (dh *DevicePortHandle) GetPortListPerDevice(w http.ResponseWriter, r *http.Request) {
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530100 vars := mux.Vars(r)
101 deviceID := vars["olt_of_id"]
102
Akash Soni1db5e492023-03-27 14:14:07 +0530103 var devicePortListResp DevicePortEntry
104 devicePortListResp.Device = Device{}
105 devicePortListResp.Ports = []Port{}
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530106
107 getPortList := func(key, value interface{}) bool {
108 voltPort := value.(*app.VoltPort)
109 port := convertVoltPortToPort(voltPort)
Akash Soni1db5e492023-03-27 14:14:07 +0530110 devicePortListResp.Ports = append(devicePortListResp.Ports, port)
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530111 return true
112 }
113 if len(deviceID) > 0 {
vinokuma926cb3e2023-03-29 11:41:06 +0530114 logger.Infow(ctx, "Received Port get request for device", log.Fields{"deviceID": deviceID})
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530115 voltDevice := app.GetApplication().GetDevice(deviceID)
116 if voltDevice != nil {
117 logger.Infow(ctx, "Found device", log.Fields{"deviceID": deviceID})
Akash Soni1db5e492023-03-27 14:14:07 +0530118 devicePortListResp.Device = convertVoltDeviceToDevice(voltDevice)
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530119 voltDevice.Ports.Range(getPortList)
120 }
121 }
Akash Soni1db5e492023-03-27 14:14:07 +0530122 portListJSON, err := json.Marshal(devicePortListResp)
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530123 if err != nil {
124 logger.Errorw(ctx, "Error occurred while marshaling port list response", log.Fields{"Error": err})
125 w.WriteHeader(http.StatusInternalServerError)
126 return
127 }
128
129 w.Header().Add("Content-Type", "application/json")
130 _, err = w.Write(portListJSON)
131 if err != nil {
132 logger.Errorw(ctx, "error in sending portList response", log.Fields{"Error": err})
133 w.WriteHeader(http.StatusInternalServerError)
134 }
135}
Akash Soni1db5e492023-03-27 14:14:07 +0530136
Tinoj Joseph429b9d92022-11-16 18:51:05 +0530137// GetPortList to get device id list
138func (dh *DevicePortHandle) GetPortList(w http.ResponseWriter, r *http.Request) {
Tinoj Joseph429b9d92022-11-16 18:51:05 +0530139 va := app.GetApplication()
140 var portListResp PortEntry
141 portListResp.Ports = []Port{}
142
143 getPortList := func(key, value interface{}) bool {
144 voltPort := value.(*app.VoltPort)
145 port := convertVoltPortToPort(voltPort)
146 portListResp.Ports = append(portListResp.Ports, port)
147 return true
148 }
149
150 getDeviceList := func(key, value interface{}) bool {
151 voltDevice := value.(*app.VoltDevice)
152 voltDevice.Ports.Range(getPortList)
153 return true
154 }
155 va.DevicesDisc.Range(getDeviceList)
156
157 portListJSON, err := json.Marshal(portListResp)
158 if err != nil {
159 logger.Errorw(ctx, "Error occurred while marshaling port list response", log.Fields{"Error": err})
160 w.WriteHeader(http.StatusInternalServerError)
161 return
162 }
163
164 w.Header().Add("Content-Type", "application/json")
165 _, err = w.Write(portListJSON)
166 if err != nil {
167 logger.Errorw(ctx, "error in sending portList response", log.Fields{"Error": err})
168 w.WriteHeader(http.StatusInternalServerError)
169 }
170}