Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +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 onos_nbi |
| 17 | |
| 18 | import ( |
| 19 | "bytes" |
| 20 | "context" |
| 21 | "encoding/json" |
| 22 | "net/http" |
| 23 | "strconv" |
| 24 | |
| 25 | "github.com/gorilla/mux" |
| 26 | "voltha-go-controller/internal/pkg/of" |
| 27 | app "voltha-go-controller/internal/pkg/application" |
| 28 | "voltha-go-controller/log" |
| 29 | ) |
| 30 | |
| 31 | const ( |
| 32 | PORTNAME string = "portName" |
| 33 | DEVICE string = "device" |
| 34 | STAG string = "sTag" |
| 35 | CTAG string = "cTag" |
| 36 | TPID string = "tpId" |
| 37 | ) |
| 38 | // FlowHandle struct to handle flow related REST calls |
| 39 | type SubscriberInfo struct { |
| 40 | Location string `json:"location"` |
| 41 | TagInfo UniTagInformation `json:"tagInfo"` |
| 42 | } |
| 43 | |
| 44 | //UniTagInformation - Service information |
| 45 | type UniTagInformation struct { |
| 46 | UniTagMatch int `json:"uniTagMatch"` |
| 47 | PonCTag int `json:"ponCTag"` |
| 48 | PonSTag int `json:"ponSTag"` |
| 49 | UsPonCTagPriority int `json:"usPonCTagPriority"` |
| 50 | UsPonSTagPriority int `json:"usPonSTagPriority"` |
| 51 | DsPonCTagPriority int `json:"dsPonCTagPriority"` |
| 52 | DsPonSTagPriority int `json:"dsPonSTagPriority"` |
| 53 | TechnologyProfileID int `json:"technologyProfileId"` |
| 54 | UpstreamBandwidthProfile string `json:"upstreamBandwidthProfile"` |
| 55 | DownstreamBandwidthProfile string `json:"downstreamBandwidthProfile"` |
| 56 | UpstreamOltBandwidthProfile string `json:"upstreamOltBandwidthProfile"` |
| 57 | DownstreamOltBandwidthProfile string `json:"downstreamOltBandwidthProfile"` |
| 58 | ServiceName string `json:"serviceName"` |
| 59 | EnableMacLearning bool `json:"enableMacLearning"` |
| 60 | ConfiguredMacAddress string `json:"configuredMacAddress"` |
| 61 | IsDhcpRequired bool `json:"isDhcpRequired"` |
| 62 | IsIgmpRequired bool `json:"isIgmpRequired"` |
| 63 | IsPppoeRequired bool `json:"isPppoeRequired"` |
| 64 | } |
| 65 | |
| 66 | type ServiceAdapter struct { |
| 67 | } |
| 68 | |
| 69 | func (sa *ServiceAdapter) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 70 | logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL}) |
| 71 | switch r.Method { |
| 72 | case "POST": |
| 73 | sa.ActivateService(context.Background(), w, r) |
| 74 | case "DELETE": |
| 75 | sa.DeactivateService(context.Background(), w, r) |
| 76 | case "GET": |
| 77 | sa.GetProgrammedSubscribers(context.Background(), w, r) |
| 78 | default: |
| 79 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func (sa *ServiceAdapter) ServeHTTPWithPortName(w http.ResponseWriter, r *http.Request) { |
| 84 | logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL}) |
| 85 | switch r.Method { |
| 86 | case "POST": |
| 87 | sa.ActivateServiceWithPortName(context.Background(), w, r) |
| 88 | case "DELETE": |
| 89 | sa.DeactivateServiceWithPortName(context.Background(), w, r) |
| 90 | default: |
| 91 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func (sa *ServiceAdapter) ActivateService(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
| 96 | vars := mux.Vars(r) |
| 97 | deviceID := vars[DEVICE] |
| 98 | portNo := vars["port"] |
| 99 | |
| 100 | // Get the payload to process the request |
| 101 | d := new(bytes.Buffer) |
| 102 | if _, err := d.ReadFrom(r.Body); err != nil { |
| 103 | logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()}) |
| 104 | return |
| 105 | } |
| 106 | |
| 107 | if len(deviceID) > 0 && len(portNo) > 0 { |
| 108 | app.GetApplication().ActivateService(cntx, deviceID, portNo, of.VlanNone, of.VlanNone, 0) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func (sa *ServiceAdapter) DeactivateService(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
| 113 | vars := mux.Vars(r) |
| 114 | deviceID := vars[DEVICE] |
| 115 | portNo := vars["port"] |
| 116 | |
| 117 | // Get the payload to process the request |
| 118 | d := new(bytes.Buffer) |
| 119 | if _, err := d.ReadFrom(r.Body); err != nil { |
| 120 | logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()}) |
| 121 | return |
| 122 | } |
| 123 | |
| 124 | if len(deviceID) > 0 && len(portNo) > 0 { |
| 125 | app.GetApplication().DeactivateService(cntx, deviceID, portNo, of.VlanNone, of.VlanNone, 0) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func (sa *ServiceAdapter) ActivateServiceWithPortName(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
| 130 | vars := mux.Vars(r) |
| 131 | portNo := vars[PORTNAME] |
| 132 | sTag := vars[STAG] |
| 133 | cTag := vars[CTAG] |
| 134 | tpID := vars[TPID] |
| 135 | sVlan := of.VlanNone |
| 136 | cVlan := of.VlanNone |
| 137 | techProfile := uint16(0) |
| 138 | |
| 139 | if len(sTag) > 0 { |
| 140 | sv, err := strconv.Atoi(sTag) |
| 141 | if err != nil { |
| 142 | logger.Warnw(ctx, "Wrong vlan value", log.Fields{"sTag": sTag}) |
| 143 | return |
| 144 | } |
| 145 | sVlan = of.VlanType(sv) |
| 146 | } |
| 147 | if len(cTag) > 0 { |
| 148 | cv, err := strconv.Atoi(cTag) |
| 149 | if err != nil { |
| 150 | logger.Warnw(ctx, "Wrong vlan value", log.Fields{"cTag": cTag}) |
| 151 | return |
| 152 | } |
| 153 | cVlan = of.VlanType(cv) |
| 154 | } |
| 155 | if len(tpID) > 0 { |
| 156 | tp, err := strconv.Atoi(tpID) |
| 157 | if err != nil { |
| 158 | logger.Warnw(ctx, "Wrong tech profile value", log.Fields{"tpID": tpID}) |
| 159 | return |
| 160 | } |
| 161 | techProfile = uint16(tp) |
| 162 | } |
| 163 | |
| 164 | if len(portNo) > 0 { |
| 165 | app.GetApplication().ActivateService(cntx, app.DeviceAny, portNo, sVlan, cVlan, techProfile) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | func (sa *ServiceAdapter) DeactivateServiceWithPortName(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
| 170 | vars := mux.Vars(r) |
| 171 | portNo := vars[PORTNAME] |
| 172 | sTag := vars[STAG] |
| 173 | cTag := vars[CTAG] |
| 174 | tpID := vars[TPID] |
| 175 | sVlan := of.VlanNone |
| 176 | cVlan := of.VlanNone |
| 177 | techProfile := uint16(0) |
| 178 | |
| 179 | if len(sTag) > 0 { |
| 180 | sv, err := strconv.Atoi(sTag) |
| 181 | if err != nil { |
| 182 | logger.Warnw(ctx, "Wrong vlan value", log.Fields{"sTag": sTag}) |
| 183 | return |
| 184 | } |
| 185 | sVlan = of.VlanType(sv) |
| 186 | } |
| 187 | if len(cTag) > 0 { |
| 188 | cv, err := strconv.Atoi(cTag) |
| 189 | if err != nil { |
| 190 | logger.Warnw(ctx, "Wrong vlan value", log.Fields{"cTag": cTag}) |
| 191 | return |
| 192 | } |
| 193 | cVlan = of.VlanType(cv) |
| 194 | } |
| 195 | if len(tpID) > 0 { |
| 196 | tp, err := strconv.Atoi(tpID) |
| 197 | if err != nil { |
| 198 | logger.Warnw(ctx, "Wrong tech profile value", log.Fields{"tpID": tpID}) |
| 199 | return |
| 200 | } |
| 201 | techProfile = uint16(tp) |
| 202 | } |
| 203 | |
| 204 | // Get the payload to process the request |
| 205 | d := new(bytes.Buffer) |
| 206 | if _, err := d.ReadFrom(r.Body); err != nil { |
| 207 | logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()}) |
| 208 | return |
| 209 | } |
| 210 | |
| 211 | if len(portNo) > 0 { |
| 212 | app.GetApplication().DeactivateService(cntx, app.DeviceAny, portNo, sVlan, cVlan, techProfile) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | func (sa *ServiceAdapter) GetProgrammedSubscribers(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
| 217 | vars := mux.Vars(r) |
| 218 | deviceID := vars[DEVICE] |
| 219 | portNo := vars["port"] |
| 220 | |
| 221 | svcs, err := app.GetApplication().GetProgrammedSubscribers(cntx, deviceID, portNo) |
| 222 | if err != nil { |
| 223 | logger.Errorw(ctx, "Failed to get subscribers", log.Fields{"Reason": err.Error()}) |
| 224 | } |
| 225 | subs := convertServiceToSubscriberInfo(svcs) |
| 226 | subsJSON, err := json.Marshal(subs) |
| 227 | if err != nil { |
| 228 | logger.Errorw(ctx, "Error occurred while marshaling subscriber response", log.Fields{"Error": err}) |
| 229 | w.WriteHeader(http.StatusInternalServerError) |
| 230 | return |
| 231 | } |
| 232 | |
| 233 | w.Header().Add("Content-Type", "application/json") |
| 234 | _, err = w.Write(subsJSON) |
| 235 | if err != nil { |
| 236 | logger.Errorw(ctx, "error in sending subscriber response", log.Fields{"Error": err}) |
| 237 | w.WriteHeader(http.StatusInternalServerError) |
| 238 | } |
| 239 | |
| 240 | } |