blob: ee2075d62dfd41aa2f2c40d6bd47385af139ef86 [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.
Akash Soni53da2852023-03-15 00:31:31 +053014 */
Naveen Sampath04696f72022-06-13 15:19:14 +053015
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
24 app "voltha-go-controller/internal/pkg/application"
Tinoj Joseph1d108322022-07-13 10:07:39 +053025 "voltha-go-controller/log"
Akash Soni53da2852023-03-15 00:31:31 +053026
27 "github.com/gorilla/mux"
Naveen Sampath04696f72022-06-13 15:19:14 +053028)
29
vinokuma926cb3e2023-03-29 11:41:06 +053030const (
31 cPost = "POST"
32 cGet = "GET"
33 cDelete = "DELETE"
34)
35
Tinoj Josephaf37ce82022-12-28 11:59:43 +053036type BWEntry struct {
37 Entries []BWProfile `json:"entry"`
38}
Akash Soni53da2852023-03-15 00:31:31 +053039
vinokuma926cb3e2023-03-29 11:41:06 +053040// BWProfile - Sadis BW Profile
Naveen Sampath04696f72022-06-13 15:19:14 +053041type BWProfile struct {
42 ID string `json:"id"`
43 PeakInformationRate uint32 `json:"pir"`
44 PeakBurstSize uint32 `json:"pbs"`
45 CommittedInformationRate uint32 `json:"cir"`
46 CommittedBurstSize uint32 `json:"cbs"`
47 ExceededInformationRate uint32 `json:"eir"`
48 ExceededBurstSize uint32 `json:"ebs"`
49 AssuredInformationRate uint32 `json:"air"`
50 GuaranteedInformationRate uint32 `json:"gir"`
51}
52
53// ProfileDelReq structure
54type ProfileDelReq struct {
55 ID string
56}
57
58// ProfileHandle handle Profile Requests
59type ProfileHandle struct {
60}
61
62// ServeHTTP to serve the HTTP request
63func (mh *ProfileHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
64 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
65 switch r.Method {
vinokuma926cb3e2023-03-29 11:41:06 +053066 case cGet:
Tinoj Joseph07cc5372022-07-18 22:53:51 +053067 mh.GetProfile(context.Background(), w, r)
vinokuma926cb3e2023-03-29 11:41:06 +053068 case cPost:
Tinoj Joseph07cc5372022-07-18 22:53:51 +053069 mh.AddProfile(context.Background(), w, r)
vinokuma926cb3e2023-03-29 11:41:06 +053070 case cDelete:
Tinoj Joseph07cc5372022-07-18 22:53:51 +053071 mh.DelProfile(context.Background(), w, r)
Naveen Sampath04696f72022-06-13 15:19:14 +053072 default:
73 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
74 }
75}
76
77// AddProfile to add meter
Tinoj Joseph07cc5372022-07-18 22:53:51 +053078func (mh *ProfileHandle) AddProfile(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Akash Soni53da2852023-03-15 00:31:31 +053079 vars := mux.Vars(r)
80 profileName := vars["id"]
Naveen Sampath04696f72022-06-13 15:19:14 +053081 // Get the payload to process the request
82 d := new(bytes.Buffer)
Akash Soni53da2852023-03-15 00:31:31 +053083 if _, err := d.ReadFrom(r.Body); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +053084 logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()})
85 return
86 }
87
88 // Unmarshal the request into service configuration structure
89 req := &BWProfile{}
90 if err := json.Unmarshal(d.Bytes(), req); err != nil {
91 logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()})
92 http.Error(w, err.Error(), http.StatusConflict)
93 return
94 }
95 logger.Debugw(ctx, "Received-northbound-add-meter-request", log.Fields{"req": req, "d": d.String()})
96 metercfg := app.VoltMeter{
Akash Soni53da2852023-03-15 00:31:31 +053097 Name: profileName,
Naveen Sampath04696f72022-06-13 15:19:14 +053098 Cir: req.CommittedInformationRate,
99 Cbs: req.CommittedBurstSize,
100 Pir: req.PeakInformationRate,
101 Pbs: req.PeakBurstSize,
102 Air: req.AssuredInformationRate,
103 Gir: req.GuaranteedInformationRate,
104 Eir: req.ExceededInformationRate,
105 Ebs: req.ExceededBurstSize,
106 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530107 app.GetApplication().AddMeterProf(cntx, metercfg)
Naveen Sampath04696f72022-06-13 15:19:14 +0530108 logger.Debugw(ctx, "northbound-add-meter-successful", log.Fields{"req": req})
109}
110
111// GetProfile to get meter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530112func (mh *ProfileHandle) GetProfile(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530113 vars := mux.Vars(r)
114 profileName := vars["id"]
115
116 var bwEntryResp BWEntry
117 bwEntryResp.Entries = []BWProfile{}
118
119 cfg, ok := app.GetApplication().GetMeterByName(profileName)
120 if !ok {
121 logger.Warnw(ctx, "Meter profile does not exist", log.Fields{"Name": profileName})
122 w.WriteHeader(http.StatusConflict)
123 return
124 }
125 profileResp := BWProfile{
Akash Soni53da2852023-03-15 00:31:31 +0530126 ID: cfg.Name,
127 CommittedInformationRate: cfg.Cir,
128 CommittedBurstSize: cfg.Cbs,
129 PeakInformationRate: cfg.Pir,
130 PeakBurstSize: cfg.Pbs,
131 AssuredInformationRate: cfg.Air,
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530132 GuaranteedInformationRate: cfg.Gir,
Akash Soni53da2852023-03-15 00:31:31 +0530133 ExceededInformationRate: cfg.Eir,
134 ExceededBurstSize: cfg.Ebs,
Tinoj Josephaf37ce82022-12-28 11:59:43 +0530135 }
136 bwEntryResp.Entries = append(bwEntryResp.Entries, profileResp)
137 profileRespJSON, err := json.Marshal(bwEntryResp)
138 if err != nil {
139 logger.Errorw(ctx, "Failed to marshal profile response", log.Fields{"Error": err})
140 w.WriteHeader(http.StatusInternalServerError)
141 return
142 }
143
144 w.Header().Add("Content-Type", "application/json")
145 _, err = w.Write(profileRespJSON)
146 if err != nil {
147 logger.Errorw(ctx, "Failed to write profile response", log.Fields{"Error": err})
148 w.WriteHeader(http.StatusInternalServerError)
149 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530150}
151
152// DelProfile to delete meter
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530153func (mh *ProfileHandle) DelProfile(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Akash Soni53da2852023-03-15 00:31:31 +0530154 vars := mux.Vars(r)
155 profileName := vars["id"]
vinokuma926cb3e2023-03-29 11:41:06 +0530156 // TODO : Change the URL and Mux to fetch meter id from the request
Naveen Sampath04696f72022-06-13 15:19:14 +0530157 d := new(bytes.Buffer)
Akash Soni53da2852023-03-15 00:31:31 +0530158 if _, err := d.ReadFrom(r.Body); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530159 logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()})
160 return
161 }
162
163 req := &ProfileDelReq{}
164 if err := json.Unmarshal(d.Bytes(), req); err != nil {
165 logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()})
166 http.Error(w, err.Error(), http.StatusConflict)
167 return
168 }
169 logger.Debugw(ctx, "Received-northbound-del-meter-request", log.Fields{"req": req})
170
Akash Soni53da2852023-03-15 00:31:31 +0530171 meterName := profileName
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530172 if err := app.GetApplication().DelMeterProf(cntx, meterName); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530173 logger.Errorw(ctx, "northbound-del-meter-failed", log.Fields{"req": req})
174 http.Error(w, err.Error(), http.StatusConflict)
175 return
176 }
177 logger.Debugw(ctx, "northbound-del-meter-successful", log.Fields{"req": req})
178}