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. |
Akash Soni | 467553b | 2023-02-16 15:54:58 +0530 | [diff] [blame] | 14 | */ |
Tinoj Joseph | 4ead4e0 | 2023-01-30 03:12:44 +0530 | [diff] [blame] | 15 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 16 | package onosnbi |
Tinoj Joseph | 4ead4e0 | 2023-01-30 03:12:44 +0530 | [diff] [blame] | 17 | |
| 18 | import ( |
| 19 | "bytes" |
| 20 | "context" |
Akash Soni | 467553b | 2023-02-16 15:54:58 +0530 | [diff] [blame] | 21 | "encoding/json" |
| 22 | "net/http" |
Tinoj Joseph | 4ead4e0 | 2023-01-30 03:12:44 +0530 | [diff] [blame] | 23 | |
Akash Soni | 467553b | 2023-02-16 15:54:58 +0530 | [diff] [blame] | 24 | app "voltha-go-controller/internal/pkg/application" |
| 25 | "voltha-go-controller/log" |
Tinoj Joseph | 4ead4e0 | 2023-01-30 03:12:44 +0530 | [diff] [blame] | 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) { |
Akash Soni | 467553b | 2023-02-16 15:54:58 +0530 | [diff] [blame] | 34 | logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL}) |
| 35 | switch r.Method { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 36 | case cPost: |
Akash Soni | 467553b | 2023-02-16 15:54:58 +0530 | [diff] [blame] | 37 | oh.configureOltFlowService(context.Background(), w, r) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 38 | case cGet: |
Akash Soni | 467553b | 2023-02-16 15:54:58 +0530 | [diff] [blame] | 39 | oh.fetchOltFlowService(context.Background(), w, r) |
| 40 | default: |
| 41 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 42 | } |
Tinoj Joseph | 4ead4e0 | 2023-01-30 03:12:44 +0530 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | func (oh *OltFlowServiceHandle) configureOltFlowService(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
Akash Soni | 467553b | 2023-02-16 15:54:58 +0530 | [diff] [blame] | 46 | // Get the payload to process the request |
| 47 | d := new(bytes.Buffer) |
| 48 | if _, err := d.ReadFrom(r.Body); err != nil { |
| 49 | logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()}) |
| 50 | return |
| 51 | } |
Tinoj Joseph | 4ead4e0 | 2023-01-30 03:12:44 +0530 | [diff] [blame] | 52 | |
Akash Soni | 467553b | 2023-02-16 15:54:58 +0530 | [diff] [blame] | 53 | // Unmarshal the request into service configuration structure |
| 54 | req := &app.OltFlowService{} |
| 55 | if err := json.Unmarshal(d.Bytes(), req); err != nil { |
| 56 | logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()}) |
| 57 | http.Error(w, err.Error(), http.StatusConflict) |
| 58 | return |
| 59 | } |
Tinoj Joseph | 4ead4e0 | 2023-01-30 03:12:44 +0530 | [diff] [blame] | 60 | app.GetApplication().UpdateOltFlowService(cntx, *req) |
| 61 | } |
| 62 | |
Akash Soni | 467553b | 2023-02-16 15:54:58 +0530 | [diff] [blame] | 63 | func (oh *OltFlowServiceHandle) fetchOltFlowService(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
Akash Soni | 467553b | 2023-02-16 15:54:58 +0530 | [diff] [blame] | 64 | logger.Info(cntx, "Inside fetchOltFlowService method") |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 65 | oltFlowSer := OltFlowServiceConfig{} |
Akash Soni | 467553b | 2023-02-16 15:54:58 +0530 | [diff] [blame] | 66 | va := app.GetApplication() |
| 67 | |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 68 | oltFlowSer.OltFlowService = va.OltFlowServiceConfig |
| 69 | OltFlowRespJSON, err := json.Marshal(oltFlowSer) |
Akash Soni | 467553b | 2023-02-16 15:54:58 +0530 | [diff] [blame] | 70 | if err != nil { |
| 71 | logger.Errorw(ctx, "Error occurred while marshaling oltFlowService response", log.Fields{"Error": err}) |
| 72 | w.WriteHeader(http.StatusInternalServerError) |
| 73 | return |
| 74 | } |
| 75 | w.Header().Add("Content-Type", "application/json") |
| 76 | _, err = w.Write(OltFlowRespJSON) |
| 77 | if err != nil { |
| 78 | logger.Errorw(ctx, "error in sending olt flow service response", log.Fields{"Error": err}) |
| 79 | w.WriteHeader(http.StatusInternalServerError) |
| 80 | } |
| 81 | } |