Akash Soni | 87a1907 | 2023-02-28 00:46:59 +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 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 16 | package onosnbi |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 17 | |
| 18 | import ( |
| 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 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 30 | const ( |
| 31 | cPost = "POST" |
| 32 | cGet = "GET" |
| 33 | cDelete = "DELETE" |
| 34 | ) |
| 35 | |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 36 | // DeviceConfigHandle handles DeviceConfig Requests |
| 37 | type DeviceConfigHandle struct { |
| 38 | } |
| 39 | |
| 40 | // ServeHTTP to serve HTTP requests |
| 41 | func (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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 44 | case cPost: |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 45 | oh.AddDeviceConfig(context.Background(), w, r) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 46 | case cGet: |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 47 | oh.FetchDeviceConfig(context.Background(), w, r) |
| 48 | default: |
| 49 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func (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 | |
| 71 | func (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 Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 91 | } |