blob: 3177b0a8f6f8fcc01928b5eff5caba8d88977764 [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"
25 "voltha-go-controller/log"
26
27 "github.com/gorilla/mux"
28)
29
vinokuma926cb3e2023-03-29 11:41:06 +053030const (
31 cPost = "POST"
32 cGet = "GET"
33 cDelete = "DELETE"
34)
35
Akash Soni87a19072023-02-28 00:46:59 +053036// DeviceConfigHandle handles DeviceConfig Requests
37type DeviceConfigHandle struct {
38}
39
40// ServeHTTP to serve HTTP requests
41func (oh *DeviceConfigHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
42 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
43 switch r.Method {
vinokuma926cb3e2023-03-29 11:41:06 +053044 case cPost:
Akash Soni87a19072023-02-28 00:46:59 +053045 oh.AddDeviceConfig(context.Background(), w, r)
vinokuma926cb3e2023-03-29 11:41:06 +053046 case cGet:
Akash Soni87a19072023-02-28 00:46:59 +053047 oh.FetchDeviceConfig(context.Background(), w, r)
48 default:
49 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
50 }
51}
52
53func (oh *DeviceConfigHandle) AddDeviceConfig(cntx context.Context, w http.ResponseWriter, r *http.Request) {
54 logger.Info(cntx, "Inside AddDeviceConfig method")
55 // Get the payload to process the request
56 d := new(bytes.Buffer)
57 if _, err := d.ReadFrom(r.Body); err != nil {
58 logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()})
59 return
60 }
61 // Unmarshal the request into device configuration structure
62 req := &app.DeviceConfig{}
63 if err := json.Unmarshal(d.Bytes(), req); err != nil {
64 logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()})
65 http.Error(w, err.Error(), http.StatusConflict)
66 return
67 }
68 app.GetApplication().UpdateDeviceConfig(cntx, req)
69}
70
71func (oh *DeviceConfigHandle) FetchDeviceConfig(cntx context.Context, w http.ResponseWriter, r *http.Request) {
72 logger.Info(cntx, "Inside FetchDeviceConfig method")
73 vars := mux.Vars(r)
74 serialNum := vars["serialNumber"]
75 deviceInfo := DeviceConfigPayload{}
76 dc := app.GetApplication().GetDeviceConfig(serialNum)
77 deviceInfo.DeviceConfig = dc
78 oltInfoJSON, err := json.Marshal(deviceInfo)
79 if err != nil {
80 logger.Errorw(ctx, "Failed to marshal olt payload response", log.Fields{"Error": err})
81 w.WriteHeader(http.StatusInternalServerError)
82 return
83 }
84
85 w.Header().Add("Content-Type", "application/json")
86 _, err = w.Write(oltInfoJSON)
87 if err != nil {
88 logger.Errorw(ctx, "Failed to write olt payload response", log.Fields{"Error": err})
89 w.WriteHeader(http.StatusInternalServerError)
90 }
Akash Soni87a19072023-02-28 00:46:59 +053091}