blob: c273ae2bc400b3edc16be8f714006071bea16081 [file] [log] [blame]
Akash Soni87a19072023-02-28 00:46:59 +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.
14 */
15
vinokuma926cb3e2023-03-29 11:41:06 +053016package onosnbi
Akash Soni87a19072023-02-28 00:46:59 +053017
18import (
19 "bytes"
20 "context"
21 "encoding/json"
22 "net/http"
23
24 app "voltha-go-controller/internal/pkg/application"
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053025 errorCodes "voltha-go-controller/internal/pkg/errorcodes"
Akash Soni87a19072023-02-28 00:46:59 +053026 "voltha-go-controller/log"
27
28 "github.com/gorilla/mux"
29)
30
vinokuma926cb3e2023-03-29 11:41:06 +053031const (
32 cPost = "POST"
33 cGet = "GET"
34 cDelete = "DELETE"
35)
36
Akash Soni87a19072023-02-28 00:46:59 +053037// DeviceConfigHandle handles DeviceConfig Requests
38type DeviceConfigHandle struct {
39}
40
41// ServeHTTP to serve HTTP requests
42func (oh *DeviceConfigHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
43 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
44 switch r.Method {
vinokuma926cb3e2023-03-29 11:41:06 +053045 case cPost:
Akash Soni87a19072023-02-28 00:46:59 +053046 oh.AddDeviceConfig(context.Background(), w, r)
vinokuma926cb3e2023-03-29 11:41:06 +053047 case cGet:
Akash Soni87a19072023-02-28 00:46:59 +053048 oh.FetchDeviceConfig(context.Background(), w, r)
49 default:
50 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053051 err := errorCodes.ErrOperationNotSupported
52 http.Error(w, err.Error(), http.StatusBadRequest)
Akash Soni87a19072023-02-28 00:46:59 +053053 }
54}
55
56func (oh *DeviceConfigHandle) AddDeviceConfig(cntx context.Context, w http.ResponseWriter, r *http.Request) {
57 logger.Info(cntx, "Inside AddDeviceConfig method")
58 // Get the payload to process the request
59 d := new(bytes.Buffer)
60 if _, err := d.ReadFrom(r.Body); err != nil {
Hitesh Chhabraaf80b832023-07-21 15:27:20 +053061 logger.Errorw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()})
62 http.Error(w, err.Error(), http.StatusConflict)
Akash Soni87a19072023-02-28 00:46:59 +053063 return
64 }
65 // Unmarshal the request into device configuration structure
66 req := &app.DeviceConfig{}
67 if err := json.Unmarshal(d.Bytes(), req); err != nil {
Hitesh Chhabraaf80b832023-07-21 15:27:20 +053068 logger.Errorw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()})
Akash Soni87a19072023-02-28 00:46:59 +053069 http.Error(w, err.Error(), http.StatusConflict)
70 return
71 }
72 app.GetApplication().UpdateDeviceConfig(cntx, req)
Hitesh Chhabraaf80b832023-07-21 15:27:20 +053073 logger.Debugw(ctx, "Added Device Config ", log.Fields{"Req": req})
Akash Soni87a19072023-02-28 00:46:59 +053074}
75
76func (oh *DeviceConfigHandle) FetchDeviceConfig(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Akash Soni87a19072023-02-28 00:46:59 +053077 vars := mux.Vars(r)
78 serialNum := vars["serialNumber"]
Hitesh Chhabraaf80b832023-07-21 15:27:20 +053079 logger.Infow(cntx, "Inside FetchDeviceConfig method", log.Fields{"serialNum": serialNum})
Akash Soni87a19072023-02-28 00:46:59 +053080 deviceInfo := DeviceConfigPayload{}
Akash Soni6f369452023-09-19 11:18:28 +053081 var voltAppIntr app.VoltAppInterface
82 voltApp := app.GetApplication()
83 voltAppIntr = voltApp
84 dc := voltAppIntr.GetDeviceConfig(serialNum)
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053085 if dc != nil {
86 deviceInfo.DeviceConfig = dc
87 oltInfoJSON, err := json.Marshal(deviceInfo)
88 if err != nil {
Hitesh Chhabraaf80b832023-07-21 15:27:20 +053089 logger.Errorw(ctx, "Failed to marshal olt payload response", log.Fields{"deviceInfo": deviceInfo, "Error": err})
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053090 w.WriteHeader(http.StatusInternalServerError)
91 return
92 }
Akash Soni87a19072023-02-28 00:46:59 +053093
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053094 w.Header().Add("Content-Type", "application/json")
95 _, err = w.Write(oltInfoJSON)
96 if err != nil {
Hitesh Chhabraaf80b832023-07-21 15:27:20 +053097 logger.Errorw(ctx, "Failed to write olt payload response", log.Fields{"deviceInfo": deviceInfo, "Error": err})
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053098 w.WriteHeader(http.StatusInternalServerError)
Hitesh Chhabraaf80b832023-07-21 15:27:20 +053099 return
Hitesh Chhabra7d249a02023-07-04 21:33:49 +0530100 }
101 } else {
102 logger.Warnw(ctx, "Device not found", log.Fields{"serialNum": serialNum})
103 err := errorCodes.ErrDeviceNotFound
104 http.Error(w, err.Error(), http.StatusBadRequest)
Akash Soni87a19072023-02-28 00:46:59 +0530105 }
Akash Soni87a19072023-02-28 00:46:59 +0530106}