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. |
| 14 | */ |
| 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 | |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame^] | 24 | "github.com/gorilla/mux" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 25 | app "voltha-go-controller/internal/pkg/application" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 26 | "voltha-go-controller/log" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 27 | ) |
| 28 | |
Tinoj Joseph | af37ce8 | 2022-12-28 11:59:43 +0530 | [diff] [blame^] | 29 | type BWEntry struct { |
| 30 | Entries []BWProfile `json:"entry"` |
| 31 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 32 | //BWProfile - Sadis BW Profile |
| 33 | type 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 |
| 46 | type ProfileDelReq struct { |
| 47 | ID string |
| 48 | } |
| 49 | |
| 50 | // ProfileHandle handle Profile Requests |
| 51 | type ProfileHandle struct { |
| 52 | } |
| 53 | |
| 54 | // ServeHTTP to serve the HTTP request |
| 55 | func (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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 59 | mh.GetProfile(context.Background(), w, r) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 60 | case "POST": |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 61 | mh.AddProfile(context.Background(), w, r) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 62 | case "DELETE": |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 63 | mh.DelProfile(context.Background(), w, r) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 64 | default: |
| 65 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // AddProfile to add meter |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 70 | func (mh *ProfileHandle) AddProfile(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 71 | // 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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 97 | app.GetApplication().AddMeterProf(cntx, metercfg) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 98 | logger.Debugw(ctx, "northbound-add-meter-successful", log.Fields{"req": req}) |
| 99 | } |
| 100 | |
| 101 | // GetProfile to get meter |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 102 | 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^] | 103 | 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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // DelProfile to delete meter |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 144 | func (mh *ProfileHandle) DelProfile(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 145 | //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 Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 161 | if err := app.GetApplication().DelMeterProf(cntx, meterName); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 162 | 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 | } |