Tinoj Joseph | 4ead4e0 | 2023-01-30 03:12:44 +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 | |
| 16 | package onos_nbi |
| 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 | |
| 28 | // OltFlowServiceHandle handles OltFlowService Requests |
| 29 | type OltFlowServiceHandle struct { |
| 30 | } |
| 31 | |
| 32 | // ServeHTTP to serve HTTP requests |
| 33 | func (oh *OltFlowServiceHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 34 | logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL}) |
| 35 | switch r.Method { |
| 36 | case "POST": |
| 37 | oh.configureOltFlowService(context.Background(), w, r) |
| 38 | default: |
| 39 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func (oh *OltFlowServiceHandle) configureOltFlowService(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
| 44 | |
| 45 | // Get the payload to process the request |
| 46 | d := new(bytes.Buffer) |
| 47 | if _, err := d.ReadFrom(r.Body); err != nil { |
| 48 | logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()}) |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | // Unmarshal the request into service configuration structure |
| 53 | req := &app.OltFlowService{} |
| 54 | if err := json.Unmarshal(d.Bytes(), req); err != nil { |
| 55 | logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()}) |
| 56 | http.Error(w, err.Error(), http.StatusConflict) |
| 57 | return |
| 58 | } |
| 59 | app.GetApplication().UpdateOltFlowService(cntx, *req) |
| 60 | } |
| 61 | |