Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 1 | /* |
| 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 Soni | 1db5e49 | 2023-03-27 14:14:07 +0530 | [diff] [blame] | 14 | */ |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 15 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 16 | package onosnbi |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 17 | |
| 18 | import ( |
| 19 | "encoding/json" |
| 20 | "net/http" |
| 21 | |
| 22 | app "voltha-go-controller/internal/pkg/application" |
| 23 | "voltha-go-controller/log" |
Akash Soni | 1db5e49 | 2023-03-27 14:14:07 +0530 | [diff] [blame] | 24 | |
| 25 | "github.com/gorilla/mux" |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | // DeviceHandle Handle DeviceIDList Requests |
| 29 | type DeviceHandle struct { |
| 30 | } |
| 31 | |
| 32 | // DevicePortHandle Handle Ports Requests |
| 33 | type DevicePortHandle struct { |
| 34 | } |
| 35 | |
| 36 | // ServeHTTP to serve HTTP requests |
| 37 | func (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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 40 | case cGet: |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 41 | dh.GetDeviceList(w, r) |
| 42 | default: |
| 43 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 44 | } |
| 45 | } |
Akash Soni | 1db5e49 | 2023-03-27 14:14:07 +0530 | [diff] [blame] | 46 | |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 47 | // GetDeviceList to get device id list |
| 48 | func (dh *DeviceHandle) GetDeviceList(w http.ResponseWriter, r *http.Request) { |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 49 | 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 |
| 77 | func (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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 80 | case cGet: |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 81 | dh.GetPortList(w, r) |
| 82 | default: |
| 83 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 84 | } |
| 85 | } |
| 86 | |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 87 | // ServeHTTPWithDeviceID to serve HTTP request for ports with deviceID |
| 88 | func (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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 91 | case cGet: |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 92 | 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 |
| 99 | func (dh *DevicePortHandle) GetPortListPerDevice(w http.ResponseWriter, r *http.Request) { |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 100 | vars := mux.Vars(r) |
| 101 | deviceID := vars["olt_of_id"] |
| 102 | |
Akash Soni | 1db5e49 | 2023-03-27 14:14:07 +0530 | [diff] [blame] | 103 | var devicePortListResp DevicePortEntry |
| 104 | devicePortListResp.Device = Device{} |
| 105 | devicePortListResp.Ports = []Port{} |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 106 | |
| 107 | getPortList := func(key, value interface{}) bool { |
| 108 | voltPort := value.(*app.VoltPort) |
| 109 | port := convertVoltPortToPort(voltPort) |
Akash Soni | 1db5e49 | 2023-03-27 14:14:07 +0530 | [diff] [blame] | 110 | devicePortListResp.Ports = append(devicePortListResp.Ports, port) |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 111 | return true |
| 112 | } |
| 113 | if len(deviceID) > 0 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 114 | logger.Infow(ctx, "Received Port get request for device", log.Fields{"deviceID": deviceID}) |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 115 | voltDevice := app.GetApplication().GetDevice(deviceID) |
| 116 | if voltDevice != nil { |
| 117 | logger.Infow(ctx, "Found device", log.Fields{"deviceID": deviceID}) |
Akash Soni | 1db5e49 | 2023-03-27 14:14:07 +0530 | [diff] [blame] | 118 | devicePortListResp.Device = convertVoltDeviceToDevice(voltDevice) |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 119 | voltDevice.Ports.Range(getPortList) |
| 120 | } |
| 121 | } |
Akash Soni | 1db5e49 | 2023-03-27 14:14:07 +0530 | [diff] [blame] | 122 | portListJSON, err := json.Marshal(devicePortListResp) |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 123 | 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 Soni | 1db5e49 | 2023-03-27 14:14:07 +0530 | [diff] [blame] | 136 | |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 137 | // GetPortList to get device id list |
| 138 | func (dh *DevicePortHandle) GetPortList(w http.ResponseWriter, r *http.Request) { |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 139 | 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 | } |