blob: d4e3454f15881b76ee5bb15b63d35dd22d6d5c4e [file] [log] [blame]
Tinoj Joseph4ead4e02023-01-30 03:12:44 +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.
Akash Soni467553b2023-02-16 15:54:58 +053014 */
Tinoj Joseph4ead4e02023-01-30 03:12:44 +053015
vinokuma926cb3e2023-03-29 11:41:06 +053016package onosnbi
Tinoj Joseph4ead4e02023-01-30 03:12:44 +053017
18import (
19 "bytes"
20 "context"
Akash Soni467553b2023-02-16 15:54:58 +053021 "encoding/json"
22 "net/http"
Tinoj Joseph4ead4e02023-01-30 03:12:44 +053023
Akash Soni467553b2023-02-16 15:54:58 +053024 app "voltha-go-controller/internal/pkg/application"
25 "voltha-go-controller/log"
Tinoj Joseph4ead4e02023-01-30 03:12:44 +053026)
27
28// OltFlowServiceHandle handles OltFlowService Requests
29type OltFlowServiceHandle struct {
30}
31
32// ServeHTTP to serve HTTP requests
33func (oh *OltFlowServiceHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Akash Soni467553b2023-02-16 15:54:58 +053034 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
35 switch r.Method {
vinokuma926cb3e2023-03-29 11:41:06 +053036 case cPost:
Akash Soni467553b2023-02-16 15:54:58 +053037 oh.configureOltFlowService(context.Background(), w, r)
vinokuma926cb3e2023-03-29 11:41:06 +053038 case cGet:
Akash Soni467553b2023-02-16 15:54:58 +053039 oh.fetchOltFlowService(context.Background(), w, r)
40 default:
41 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
42 }
Tinoj Joseph4ead4e02023-01-30 03:12:44 +053043}
44
45func (oh *OltFlowServiceHandle) configureOltFlowService(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Akash Soni467553b2023-02-16 15:54:58 +053046 // 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 Joseph4ead4e02023-01-30 03:12:44 +053052
Akash Soni467553b2023-02-16 15:54:58 +053053 // 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 Joseph4ead4e02023-01-30 03:12:44 +053060 app.GetApplication().UpdateOltFlowService(cntx, *req)
61}
62
Akash Soni467553b2023-02-16 15:54:58 +053063func (oh *OltFlowServiceHandle) fetchOltFlowService(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Akash Soni467553b2023-02-16 15:54:58 +053064 logger.Info(cntx, "Inside fetchOltFlowService method")
Akash Soni87a19072023-02-28 00:46:59 +053065 oltFlowSer := OltFlowServiceConfig{}
Akash Soni467553b2023-02-16 15:54:58 +053066 va := app.GetApplication()
67
Akash Soni87a19072023-02-28 00:46:59 +053068 oltFlowSer.OltFlowService = va.OltFlowServiceConfig
69 OltFlowRespJSON, err := json.Marshal(oltFlowSer)
Akash Soni467553b2023-02-16 15:54:58 +053070 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}