blob: a02554f5f989bd1532b436574fb6994582e8a69b [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +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.
14*/
15
16package nbi
17
18import (
19 "bytes"
Tinoj Joseph07cc5372022-07-18 22:53:51 +053020 "context"
Naveen Sampath04696f72022-06-13 15:19:14 +053021 "encoding/json"
22 "net/http"
23
Tinoj Josephaf37ce82022-12-28 11:59:43 +053024 "github.com/gorilla/mux"
Naveen Sampath04696f72022-06-13 15:19:14 +053025 app "voltha-go-controller/internal/pkg/application"
Tinoj Joseph1d108322022-07-13 10:07:39 +053026 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053027)
28
Tinoj Josephaf37ce82022-12-28 11:59:43 +053029type BWEntry struct {
30 Entries []BWProfile `json:"entry"`
31}
Naveen Sampath04696f72022-06-13 15:19:14 +053032//BWProfile - Sadis BW Profile
33type BWProfile struct {
34 ID string `json:"id"`
35 PeakInformationRate uint32 `json:"pir"`
36 PeakBurstSize uint32 `json:"pbs"`
37 CommittedInformationRate uint32 `json:"cir"`
38 CommittedBurstSize uint32 `json:"cbs"`
39 ExceededInformationRate uint32 `json:"eir"`
40 ExceededBurstSize uint32 `json:"ebs"`
41 AssuredInformationRate uint32 `json:"air"`
42 GuaranteedInformationRate uint32 `json:"gir"`
43}
44
45// ProfileDelReq structure
46type ProfileDelReq struct {
47 ID string
48}
49
50// ProfileHandle handle Profile Requests
51type ProfileHandle struct {
52}
53
54// ServeHTTP to serve the HTTP request
55func (mh *ProfileHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
56 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
57 switch r.Method {
58 case "GET":
Tinoj Joseph07cc5372022-07-18 22:53:51 +053059 mh.GetProfile(context.Background(), w, r)
Naveen Sampath04696f72022-06-13 15:19:14 +053060 case "POST":
Tinoj Joseph07cc5372022-07-18 22:53:51 +053061 mh.AddProfile(context.Background(), w, r)
Naveen Sampath04696f72022-06-13 15:19:14 +053062 case "DELETE":
Tinoj Joseph07cc5372022-07-18 22:53:51 +053063 mh.DelProfile(context.Background(), w, r)
Naveen Sampath04696f72022-06-13 15:19:14 +053064 default:
65 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
66 }
67}
68
69// AddProfile to add meter
Tinoj Joseph07cc5372022-07-18 22:53:51 +053070func (mh *ProfileHandle) AddProfile(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Naveen Sampath04696f72022-06-13 15:19:14 +053071 // Get the payload to process the request
72 d := new(bytes.Buffer)
73 if _, err := d.ReadFrom(r.Body); err != nil {
74 logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()})
75 return
76 }
77
78 // Unmarshal the request into service configuration structure
79 req := &BWProfile{}
80 if err := json.Unmarshal(d.Bytes(), req); err != nil {
81 logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()})
82 http.Error(w, err.Error(), http.StatusConflict)
83 return
84 }
85 logger.Debugw(ctx, "Received-northbound-add-meter-request", log.Fields{"req": req, "d": d.String()})
86 metercfg := app.VoltMeter{
87 Name: req.ID,
88 Cir: req.CommittedInformationRate,
89 Cbs: req.CommittedBurstSize,
90 Pir: req.PeakInformationRate,
91 Pbs: req.PeakBurstSize,
92 Air: req.AssuredInformationRate,
93 Gir: req.GuaranteedInformationRate,
94 Eir: req.ExceededInformationRate,
95 Ebs: req.ExceededBurstSize,
96 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +053097 app.GetApplication().AddMeterProf(cntx, metercfg)
Naveen Sampath04696f72022-06-13 15:19:14 +053098 logger.Debugw(ctx, "northbound-add-meter-successful", log.Fields{"req": req})
99}
100
101// GetProfile to get meter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530102func (mh *ProfileHandle) GetProfile(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530103 vars := mux.Vars(r)
104 profileName := vars["id"]
105
106 var bwEntryResp BWEntry
107 bwEntryResp.Entries = []BWProfile{}
108
109 cfg, ok := app.GetApplication().GetMeterByName(profileName)
110 if !ok {
111 logger.Warnw(ctx, "Meter profile does not exist", log.Fields{"Name": profileName})
112 w.WriteHeader(http.StatusConflict)
113 return
114 }
115 profileResp := BWProfile{
116 ID: cfg.Name,
117 CommittedInformationRate : cfg.Cir,
118 CommittedBurstSize : cfg.Cbs,
119 PeakInformationRate: cfg.Pir,
120 PeakBurstSize: cfg.Pbs,
121 AssuredInformationRate: cfg.Air,
122 GuaranteedInformationRate: cfg.Gir,
123 ExceededInformationRate: cfg.Eir,
124 ExceededBurstSize: cfg.Ebs,
125 }
126 bwEntryResp.Entries = append(bwEntryResp.Entries, profileResp)
127 profileRespJSON, err := json.Marshal(bwEntryResp)
128 if err != nil {
129 logger.Errorw(ctx, "Failed to marshal profile response", log.Fields{"Error": err})
130 w.WriteHeader(http.StatusInternalServerError)
131 return
132 }
133
134 w.Header().Add("Content-Type", "application/json")
135 _, err = w.Write(profileRespJSON)
136 if err != nil {
137 logger.Errorw(ctx, "Failed to write profile response", log.Fields{"Error": err})
138 w.WriteHeader(http.StatusInternalServerError)
139 }
140
Naveen Sampath04696f72022-06-13 15:19:14 +0530141}
142
143// DelProfile to delete meter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530144func (mh *ProfileHandle) DelProfile(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530145 //TODO : Change the URL and Mux to fetch meter id from the request
146 d := new(bytes.Buffer)
147 if _, err := d.ReadFrom(r.Body); err != nil {
148 logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()})
149 return
150 }
151
152 req := &ProfileDelReq{}
153 if err := json.Unmarshal(d.Bytes(), req); err != nil {
154 logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()})
155 http.Error(w, err.Error(), http.StatusConflict)
156 return
157 }
158 logger.Debugw(ctx, "Received-northbound-del-meter-request", log.Fields{"req": req})
159
160 meterName := req.ID
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530161 if err := app.GetApplication().DelMeterProf(cntx, meterName); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530162 logger.Errorw(ctx, "northbound-del-meter-failed", log.Fields{"req": req})
163 http.Error(w, err.Error(), http.StatusConflict)
164 return
165 }
166 logger.Debugw(ctx, "northbound-del-meter-successful", log.Fields{"req": req})
167}