blob: 2e44b253a9b5637777e00a21678e1f34aa746d4c [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{}
51
52 if len(id) > 0 {
vinokuma926cb3e2023-03-29 11:41:06 +053053 // If Get for single Device
Naveen Sampath04696f72022-06-13 15:19:14 +053054 deviceID = id
55 voltDevice := va.GetDevice(deviceID)
56 if voltDevice != nil {
57 taskList := va.GetTaskList(deviceID)
58 taskListResp[deviceID] = taskList
59 } else {
60 logger.Errorw(ctx, "Invalid Device Id", log.Fields{"Device": voltDevice})
61 return
62 }
63 } else {
vinokuma926cb3e2023-03-29 11:41:06 +053064 // Else If GetAll
Naveen Sampath04696f72022-06-13 15:19:14 +053065 getDeviceTaskList := func(key, value interface{}) bool {
66 voltDevice := value.(*app.VoltDevice)
67 deviceID = voltDevice.Name
68 taskList := va.GetTaskList(deviceID)
69 taskListResp[deviceID] = taskList
70 return true
71 }
72 va.DevicesDisc.Range(getDeviceTaskList)
73 }
74
75 taskListJSON, err := json.Marshal(taskListResp)
76 if err != nil {
77 logger.Errorw(ctx, "Error occurred while marshaling task list response", log.Fields{"Error": err})
78 w.WriteHeader(http.StatusInternalServerError)
79 return
80 }
81
82 w.Header().Add("Content-Type", "application/json")
83 _, err = w.Write(taskListJSON)
84 if err != nil {
85 logger.Errorw(ctx, "error in sending task list response", log.Fields{"Error": err})
86 w.WriteHeader(http.StatusInternalServerError)
87 }
88}