blob: e6d3eecba025eab4d1922967c852d83ebfc78202 [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
16package onos_nbi
17
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
30// DeviceConfigHandle handles DeviceConfig Requests
31type DeviceConfigHandle struct {
32}
33
34// ServeHTTP to serve HTTP requests
35func (oh *DeviceConfigHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
36 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
37 switch r.Method {
38 case "POST":
39 oh.AddDeviceConfig(context.Background(), w, r)
40 case "GET":
41 oh.FetchDeviceConfig(context.Background(), w, r)
42 default:
43 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
44 }
45}
46
47func (oh *DeviceConfigHandle) AddDeviceConfig(cntx context.Context, w http.ResponseWriter, r *http.Request) {
48 logger.Info(cntx, "Inside AddDeviceConfig method")
49 // Get the payload to process the request
50 d := new(bytes.Buffer)
51 if _, err := d.ReadFrom(r.Body); err != nil {
52 logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()})
53 return
54 }
55 // Unmarshal the request into device configuration structure
56 req := &app.DeviceConfig{}
57 if err := json.Unmarshal(d.Bytes(), req); err != nil {
58 logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()})
59 http.Error(w, err.Error(), http.StatusConflict)
60 return
61 }
62 app.GetApplication().UpdateDeviceConfig(cntx, req)
63}
64
65func (oh *DeviceConfigHandle) FetchDeviceConfig(cntx context.Context, w http.ResponseWriter, r *http.Request) {
66 logger.Info(cntx, "Inside FetchDeviceConfig method")
67 vars := mux.Vars(r)
68 serialNum := vars["serialNumber"]
69 deviceInfo := DeviceConfigPayload{}
70 dc := app.GetApplication().GetDeviceConfig(serialNum)
71 deviceInfo.DeviceConfig = dc
72 oltInfoJSON, err := json.Marshal(deviceInfo)
73 if err != nil {
74 logger.Errorw(ctx, "Failed to marshal olt payload response", log.Fields{"Error": err})
75 w.WriteHeader(http.StatusInternalServerError)
76 return
77 }
78
79 w.Header().Add("Content-Type", "application/json")
80 _, err = w.Write(oltInfoJSON)
81 if err != nil {
82 logger.Errorw(ctx, "Failed to write olt payload response", log.Fields{"Error": err})
83 w.WriteHeader(http.StatusInternalServerError)
84 }
85
86}