Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +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 | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 14 | */ |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 15 | |
| 16 | package nbi |
| 17 | |
| 18 | import ( |
| 19 | "bytes" |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 20 | "context" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 21 | "encoding/json" |
| 22 | "net/http" |
| 23 | |
| 24 | app "voltha-go-controller/internal/pkg/application" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 25 | "voltha-go-controller/log" |
Akash Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 26 | |
| 27 | "github.com/gorilla/mux" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 28 | ) |
| 29 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 30 | const ( |
| 31 | cPost = "POST" |
| 32 | cGet = "GET" |
| 33 | cDelete = "DELETE" |
| 34 | ) |
| 35 | |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 36 | type BWEntry struct { |
| 37 | Entries []BWProfile `json:"entry"` |
| 38 | } |
Akash Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 39 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 40 | // BWProfile - Sadis BW Profile |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 41 | type 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 |
| 54 | type ProfileDelReq struct { |
| 55 | ID string |
| 56 | } |
| 57 | |
| 58 | // ProfileHandle handle Profile Requests |
| 59 | type ProfileHandle struct { |
| 60 | } |
| 61 | |
| 62 | // ServeHTTP to serve the HTTP request |
| 63 | func (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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 66 | case cGet: |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 67 | mh.GetProfile(context.Background(), w, r) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 68 | case cPost: |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 69 | mh.AddProfile(context.Background(), w, r) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 70 | case cDelete: |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 71 | mh.DelProfile(context.Background(), w, r) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 72 | default: |
| 73 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // AddProfile to add meter |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 78 | func (mh *ProfileHandle) AddProfile(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
Akash Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 79 | vars := mux.Vars(r) |
| 80 | profileName := vars["id"] |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 81 | // Get the payload to process the request |
| 82 | d := new(bytes.Buffer) |
Akash Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 83 | if _, err := d.ReadFrom(r.Body); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 84 | 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 Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 97 | Name: profileName, |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 98 | 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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 107 | app.GetApplication().AddMeterProf(cntx, metercfg) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 108 | logger.Debugw(ctx, "northbound-add-meter-successful", log.Fields{"req": req}) |
| 109 | } |
| 110 | |
| 111 | // GetProfile to get meter |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 112 | func (mh *ProfileHandle) GetProfile(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 113 | 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 Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 126 | ID: cfg.Name, |
| 127 | CommittedInformationRate: cfg.Cir, |
| 128 | CommittedBurstSize: cfg.Cbs, |
| 129 | PeakInformationRate: cfg.Pir, |
| 130 | PeakBurstSize: cfg.Pbs, |
| 131 | AssuredInformationRate: cfg.Air, |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 132 | GuaranteedInformationRate: cfg.Gir, |
Akash Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 133 | ExceededInformationRate: cfg.Eir, |
| 134 | ExceededBurstSize: cfg.Ebs, |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame] | 135 | } |
| 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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // DelProfile to delete meter |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 153 | func (mh *ProfileHandle) DelProfile(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
Akash Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 154 | vars := mux.Vars(r) |
| 155 | profileName := vars["id"] |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 156 | // TODO : Change the URL and Mux to fetch meter id from the request |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 157 | d := new(bytes.Buffer) |
Akash Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 158 | if _, err := d.ReadFrom(r.Body); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 159 | 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 Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 171 | meterName := profileName |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 172 | if err := app.GetApplication().DelMeterProf(cntx, meterName); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 173 | 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 | } |