Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +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. |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 14 | */ |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 15 | |
| 16 | package nbi |
| 17 | |
| 18 | import ( |
| 19 | "encoding/json" |
| 20 | "net/http" |
| 21 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 22 | app "voltha-go-controller/internal/pkg/application" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 23 | "voltha-go-controller/log" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 24 | |
| 25 | "github.com/gorilla/mux" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | // TaskListHandle handle TaskList Requests |
| 29 | type TaskListHandle struct { |
| 30 | } |
| 31 | |
| 32 | // ServeHTTP to serve http request |
| 33 | func (dh *TaskListHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 34 | logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL}) |
| 35 | switch r.Method { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 36 | case cGet: |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 37 | dh.GetTaskList(w, r) |
| 38 | default: |
| 39 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // GetTaskList to get task list |
| 44 | func (dh *TaskListHandle) GetTaskList(w http.ResponseWriter, r *http.Request) { |
| 45 | vars := mux.Vars(r) |
| 46 | id := vars["id"] |
| 47 | |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 48 | var voltAppIntr app.VoltAppInterface |
| 49 | voltApp := app.GetApplication() |
| 50 | voltAppIntr = voltApp |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 51 | var deviceID string |
| 52 | taskListResp := map[string]map[int]*app.TaskInfo{} |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 53 | logger.Infow(ctx, "Received get TaskList", log.Fields{"DeviceID": id}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 54 | |
| 55 | if len(id) > 0 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 56 | // If Get for single Device |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 57 | deviceID = id |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 58 | voltDevice := voltAppIntr.GetDevice(deviceID) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 59 | if voltDevice != nil { |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 60 | taskList := voltAppIntr.GetTaskList(deviceID) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 61 | taskListResp[deviceID] = taskList |
| 62 | } else { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 63 | logger.Warnw(ctx, "Invalid Device Id", log.Fields{"Device": id}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 64 | return |
| 65 | } |
| 66 | } else { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 67 | // Else If GetAll |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 68 | getDeviceTaskList := func(key, value interface{}) bool { |
| 69 | voltDevice := value.(*app.VoltDevice) |
| 70 | deviceID = voltDevice.Name |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 71 | taskList := voltAppIntr.GetTaskList(deviceID) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 72 | taskListResp[deviceID] = taskList |
| 73 | return true |
| 74 | } |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 75 | voltApp.DevicesDisc.Range(getDeviceTaskList) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | taskListJSON, err := json.Marshal(taskListResp) |
| 79 | if err != nil { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 80 | logger.Errorw(ctx, "Error occurred while marshaling task list response", log.Fields{"TaskList": taskListResp, "Error": err.Error()}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 81 | w.WriteHeader(http.StatusInternalServerError) |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | w.Header().Add("Content-Type", "application/json") |
| 86 | _, err = w.Write(taskListJSON) |
| 87 | if err != nil { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 88 | logger.Errorw(ctx, "error in sending task list response", log.Fields{"TaskList": taskListResp, "Error": err.Error()}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 89 | w.WriteHeader(http.StatusInternalServerError) |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 90 | return |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 91 | } |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 92 | logger.Debugw(ctx, "Fetching TaskListResp for device id", log.Fields{"TaskListResp": taskListResp}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 93 | } |