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. |
| 14 | */ |
| 15 | |
| 16 | package nbi |
| 17 | |
| 18 | import ( |
| 19 | "encoding/json" |
| 20 | "net/http" |
| 21 | |
| 22 | "github.com/gorilla/mux" |
| 23 | app "voltha-go-controller/internal/pkg/application" |
| 24 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 25 | ) |
| 26 | |
| 27 | // TaskListHandle handle TaskList Requests |
| 28 | type TaskListHandle struct { |
| 29 | } |
| 30 | |
| 31 | // ServeHTTP to serve http request |
| 32 | func (dh *TaskListHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 33 | logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL}) |
| 34 | switch r.Method { |
| 35 | case "GET": |
| 36 | dh.GetTaskList(w, r) |
| 37 | default: |
| 38 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // GetTaskList to get task list |
| 43 | func (dh *TaskListHandle) GetTaskList(w http.ResponseWriter, r *http.Request) { |
| 44 | vars := mux.Vars(r) |
| 45 | id := vars["id"] |
| 46 | |
| 47 | va := app.GetApplication() |
| 48 | var deviceID string |
| 49 | taskListResp := map[string]map[int]*app.TaskInfo{} |
| 50 | |
| 51 | if len(id) > 0 { |
| 52 | //If Get for single Device |
| 53 | deviceID = id |
| 54 | voltDevice := va.GetDevice(deviceID) |
| 55 | if voltDevice != nil { |
| 56 | taskList := va.GetTaskList(deviceID) |
| 57 | taskListResp[deviceID] = taskList |
| 58 | } else { |
| 59 | logger.Errorw(ctx, "Invalid Device Id", log.Fields{"Device": voltDevice}) |
| 60 | return |
| 61 | } |
| 62 | } else { |
| 63 | //Else If GetAll |
| 64 | getDeviceTaskList := func(key, value interface{}) bool { |
| 65 | voltDevice := value.(*app.VoltDevice) |
| 66 | deviceID = voltDevice.Name |
| 67 | taskList := va.GetTaskList(deviceID) |
| 68 | taskListResp[deviceID] = taskList |
| 69 | return true |
| 70 | } |
| 71 | va.DevicesDisc.Range(getDeviceTaskList) |
| 72 | } |
| 73 | |
| 74 | taskListJSON, err := json.Marshal(taskListResp) |
| 75 | if err != nil { |
| 76 | logger.Errorw(ctx, "Error occurred while marshaling task list response", log.Fields{"Error": err}) |
| 77 | w.WriteHeader(http.StatusInternalServerError) |
| 78 | return |
| 79 | } |
| 80 | |
| 81 | w.Header().Add("Content-Type", "application/json") |
| 82 | _, err = w.Write(taskListJSON) |
| 83 | if err != nil { |
| 84 | logger.Errorw(ctx, "error in sending task list response", log.Fields{"Error": err}) |
| 85 | w.WriteHeader(http.StatusInternalServerError) |
| 86 | } |
| 87 | } |