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" |
Hitesh Chhabra | 7d249a0 | 2023-07-04 21:33:49 +0530 | [diff] [blame^] | 25 | errorCodes "voltha-go-controller/internal/pkg/errorcodes" |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 26 | "voltha-go-controller/log" |
| 27 | |
| 28 | "github.com/gorilla/mux" |
| 29 | ) |
| 30 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 31 | const ( |
| 32 | cPost = "POST" |
| 33 | cGet = "GET" |
| 34 | cDelete = "DELETE" |
| 35 | ) |
| 36 | |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 37 | // DeviceConfigHandle handles DeviceConfig Requests |
| 38 | type DeviceConfigHandle struct { |
| 39 | } |
| 40 | |
| 41 | // ServeHTTP to serve HTTP requests |
| 42 | func (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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 45 | case cPost: |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 46 | oh.AddDeviceConfig(context.Background(), w, r) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 47 | case cGet: |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 48 | oh.FetchDeviceConfig(context.Background(), w, r) |
| 49 | default: |
| 50 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
Hitesh Chhabra | 7d249a0 | 2023-07-04 21:33:49 +0530 | [diff] [blame^] | 51 | err := errorCodes.ErrOperationNotSupported |
| 52 | http.Error(w, err.Error(), http.StatusBadRequest) |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | func (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 | |
| 74 | func (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 Chhabra | 7d249a0 | 2023-07-04 21:33:49 +0530 | [diff] [blame^] | 80 | 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 Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 88 | |
Hitesh Chhabra | 7d249a0 | 2023-07-04 21:33:49 +0530 | [diff] [blame^] | 89 | 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 Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 99 | } |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 100 | } |