blob: 7ab4f1b5deba1c07fc983f10e9be349823cec4d3 [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
48 va := app.GetApplication()
49 var deviceID string
50 taskListResp := map[string]map[int]*app.TaskInfo{}
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +053051 logger.Infow(ctx, "Received get TaskList", log.Fields{"DeviceID": id})
Naveen Sampath04696f72022-06-13 15:19:14 +053052
53 if len(id) > 0 {
vinokuma926cb3e2023-03-29 11:41:06 +053054 // If Get for single Device
Naveen Sampath04696f72022-06-13 15:19:14 +053055 deviceID = id
56 voltDevice := va.GetDevice(deviceID)
57 if voltDevice != nil {
58 taskList := va.GetTaskList(deviceID)
59 taskListResp[deviceID] = taskList
60 } else {
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +053061 logger.Warnw(ctx, "Invalid Device Id", log.Fields{"Device": id})
Naveen Sampath04696f72022-06-13 15:19:14 +053062 return
63 }
64 } else {
vinokuma926cb3e2023-03-29 11:41:06 +053065 // Else If GetAll
Naveen Sampath04696f72022-06-13 15:19:14 +053066 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 Chhabra8c3f1662023-07-19 13:15:16 +053078 logger.Errorw(ctx, "Error occurred while marshaling task list response", log.Fields{"TaskList": taskListResp, "Error": err.Error()})
Naveen Sampath04696f72022-06-13 15:19:14 +053079 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 Chhabra8c3f1662023-07-19 13:15:16 +053086 logger.Errorw(ctx, "error in sending task list response", log.Fields{"TaskList": taskListResp, "Error": err.Error()})
Naveen Sampath04696f72022-06-13 15:19:14 +053087 w.WriteHeader(http.StatusInternalServerError)
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +053088 return
Naveen Sampath04696f72022-06-13 15:19:14 +053089 }
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +053090 logger.Debugw(ctx, "Fetching TaskListResp for device id", log.Fields{"TaskListResp": taskListResp})
Naveen Sampath04696f72022-06-13 15:19:14 +053091}