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 | |
| 48 | va := app.GetApplication() |
| 49 | var deviceID string |
| 50 | taskListResp := map[string]map[int]*app.TaskInfo{} |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame^] | 51 | logger.Infow(ctx, "Received get TaskList", log.Fields{"DeviceID": id}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 52 | |
| 53 | if len(id) > 0 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 54 | // If Get for single Device |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 55 | deviceID = id |
| 56 | voltDevice := va.GetDevice(deviceID) |
| 57 | if voltDevice != nil { |
| 58 | taskList := va.GetTaskList(deviceID) |
| 59 | taskListResp[deviceID] = taskList |
| 60 | } else { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame^] | 61 | logger.Warnw(ctx, "Invalid Device Id", log.Fields{"Device": id}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 62 | return |
| 63 | } |
| 64 | } else { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 65 | // Else If GetAll |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 66 | getDeviceTaskList := func(key, value interface{}) bool { |
| 67 | voltDevice := value.(*app.VoltDevice) |
| 68 | deviceID = voltDevice.Name |
| 69 | taskList := va.GetTaskList(deviceID) |
| 70 | taskListResp[deviceID] = taskList |
| 71 | return true |
| 72 | } |
| 73 | va.DevicesDisc.Range(getDeviceTaskList) |
| 74 | } |
| 75 | |
| 76 | taskListJSON, err := json.Marshal(taskListResp) |
| 77 | if err != nil { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame^] | 78 | 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] | 79 | w.WriteHeader(http.StatusInternalServerError) |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | w.Header().Add("Content-Type", "application/json") |
| 84 | _, err = w.Write(taskListJSON) |
| 85 | if err != nil { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame^] | 86 | 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] | 87 | w.WriteHeader(http.StatusInternalServerError) |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame^] | 88 | return |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 89 | } |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame^] | 90 | logger.Debugw(ctx, "Fetching TaskListResp for device id", log.Fields{"TaskListResp": taskListResp}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 91 | } |