blob: 709ba447e7e71a834b6941a4ab59252447a86764 [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 {
61 logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()})
62 return
63 }
64 // Unmarshal the request into device configuration structure
65 req := &app.DeviceConfig{}
66 if err := json.Unmarshal(d.Bytes(), req); err != nil {
67 logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()})
68 http.Error(w, err.Error(), http.StatusConflict)
69 return
70 }
71 app.GetApplication().UpdateDeviceConfig(cntx, req)
72}
73
74func (oh *DeviceConfigHandle) FetchDeviceConfig(cntx context.Context, w http.ResponseWriter, r *http.Request) {
75 logger.Info(cntx, "Inside FetchDeviceConfig method")
76 vars := mux.Vars(r)
77 serialNum := vars["serialNumber"]
78 deviceInfo := DeviceConfigPayload{}
79 dc := app.GetApplication().GetDeviceConfig(serialNum)
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053080 if dc != nil {
81 deviceInfo.DeviceConfig = dc
82 oltInfoJSON, err := json.Marshal(deviceInfo)
83 if err != nil {
84 logger.Errorw(ctx, "Failed to marshal olt payload response", log.Fields{"Error": err})
85 w.WriteHeader(http.StatusInternalServerError)
86 return
87 }
Akash Soni87a19072023-02-28 00:46:59 +053088
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053089 w.Header().Add("Content-Type", "application/json")
90 _, err = w.Write(oltInfoJSON)
91 if err != nil {
92 logger.Errorw(ctx, "Failed to write olt payload response", log.Fields{"Error": err})
93 w.WriteHeader(http.StatusInternalServerError)
94 }
95 } else {
96 logger.Warnw(ctx, "Device not found", log.Fields{"serialNum": serialNum})
97 err := errorCodes.ErrDeviceNotFound
98 http.Error(w, err.Error(), http.StatusBadRequest)
Akash Soni87a19072023-02-28 00:46:59 +053099 }
Akash Soni87a19072023-02-28 00:46:59 +0530100}