blob: 3f9e5fc39bb82bd98403a4e6027c27f7692d691e [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.
vinokuma926cb3e2023-03-29 11:41:06 +053014 */
Naveen Sampath04696f72022-06-13 15:19:14 +053015
16package nbi
17
18import (
19 "encoding/json"
20 "net/http"
21
Naveen Sampath04696f72022-06-13 15:19:14 +053022 app "voltha-go-controller/internal/pkg/application"
Tinoj Joseph1d108322022-07-13 10:07:39 +053023 "voltha-go-controller/log"
vinokuma926cb3e2023-03-29 11:41:06 +053024
25 "github.com/gorilla/mux"
Naveen Sampath04696f72022-06-13 15:19:14 +053026)
27
28// TaskListHandle handle TaskList Requests
29type TaskListHandle struct {
30}
31
32// ServeHTTP to serve http request
33func (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 {
vinokuma926cb3e2023-03-29 11:41:06 +053036 case cGet:
Naveen Sampath04696f72022-06-13 15:19:14 +053037 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
44func (dh *TaskListHandle) GetTaskList(w http.ResponseWriter, r *http.Request) {
45 vars := mux.Vars(r)
46 id := vars["id"]
47
Akash Soni6f369452023-09-19 11:18:28 +053048 var voltAppIntr app.VoltAppInterface
49 voltApp := app.GetApplication()
50 voltAppIntr = voltApp
Naveen Sampath04696f72022-06-13 15:19:14 +053051 var deviceID string
52 taskListResp := map[string]map[int]*app.TaskInfo{}
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +053053 logger.Infow(ctx, "Received get TaskList", log.Fields{"DeviceID": id})
Naveen Sampath04696f72022-06-13 15:19:14 +053054
55 if len(id) > 0 {
vinokuma926cb3e2023-03-29 11:41:06 +053056 // If Get for single Device
Naveen Sampath04696f72022-06-13 15:19:14 +053057 deviceID = id
Akash Soni6f369452023-09-19 11:18:28 +053058 voltDevice := voltAppIntr.GetDevice(deviceID)
Naveen Sampath04696f72022-06-13 15:19:14 +053059 if voltDevice != nil {
Akash Soni6f369452023-09-19 11:18:28 +053060 taskList := voltAppIntr.GetTaskList(deviceID)
Naveen Sampath04696f72022-06-13 15:19:14 +053061 taskListResp[deviceID] = taskList
62 } else {
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +053063 logger.Warnw(ctx, "Invalid Device Id", log.Fields{"Device": id})
Naveen Sampath04696f72022-06-13 15:19:14 +053064 return
65 }
66 } else {
vinokuma926cb3e2023-03-29 11:41:06 +053067 // Else If GetAll
Naveen Sampath04696f72022-06-13 15:19:14 +053068 getDeviceTaskList := func(key, value interface{}) bool {
69 voltDevice := value.(*app.VoltDevice)
70 deviceID = voltDevice.Name
Akash Soni6f369452023-09-19 11:18:28 +053071 taskList := voltAppIntr.GetTaskList(deviceID)
Naveen Sampath04696f72022-06-13 15:19:14 +053072 taskListResp[deviceID] = taskList
73 return true
74 }
Akash Soni6f369452023-09-19 11:18:28 +053075 voltApp.DevicesDisc.Range(getDeviceTaskList)
Naveen Sampath04696f72022-06-13 15:19:14 +053076 }
77
78 taskListJSON, err := json.Marshal(taskListResp)
79 if err != nil {
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +053080 logger.Errorw(ctx, "Error occurred while marshaling task list response", log.Fields{"TaskList": taskListResp, "Error": err.Error()})
Naveen Sampath04696f72022-06-13 15:19:14 +053081 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 Chhabra8c3f1662023-07-19 13:15:16 +053088 logger.Errorw(ctx, "error in sending task list response", log.Fields{"TaskList": taskListResp, "Error": err.Error()})
Naveen Sampath04696f72022-06-13 15:19:14 +053089 w.WriteHeader(http.StatusInternalServerError)
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +053090 return
Naveen Sampath04696f72022-06-13 15:19:14 +053091 }
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +053092 logger.Debugw(ctx, "Fetching TaskListResp for device id", log.Fields{"TaskListResp": taskListResp})
Naveen Sampath04696f72022-06-13 15:19:14 +053093}