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 | a824697 | 2023-01-03 10:37:08 +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" |
| 23 | "net/http" |
| 24 | "strconv" |
| 25 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 26 | "voltha-go-controller/internal/pkg/application" |
| 27 | app "voltha-go-controller/internal/pkg/application" |
| 28 | "voltha-go-controller/internal/pkg/of" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 29 | "voltha-go-controller/log" |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 30 | |
| 31 | "github.com/google/gopacket/layers" |
| 32 | "github.com/gorilla/mux" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 33 | ) |
| 34 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 35 | // SubscriberDeviceInfo - Subcriber Device Info |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 36 | type SubscriberDeviceInfo struct { |
| 37 | ID string `json:"id"` |
| 38 | NasPortID string `json:"nasPortId"` |
Akash Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 39 | UplinkPort string `json:"uplinkPort"` |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 40 | HardwareIdentifier string `json:"hardwareIdentifier"` |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame] | 41 | IPAddress string `json:"ipAddress"` |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 42 | NasID string `json:"nasId"` |
| 43 | CircuitID string `json:"circuitId"` |
| 44 | RemoteID string `json:"remoteId"` |
| 45 | UniTagList []UniTagInformation `json:"uniTagList"` |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 46 | NniDhcpTrapVid int `json:"nniDhcpTrapVid"` |
| 47 | Slot int `json:"slot"` |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 48 | } |
| 49 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 50 | // UniTagInformation - Service information |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 51 | type UniTagInformation struct { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 52 | UpstreamBandwidthProfile string `json:"upstreamBandwidthProfile"` |
| 53 | DownstreamBandwidthProfile string `json:"downstreamBandwidthProfile"` |
| 54 | UpstreamOltBandwidthProfile string `json:"upstreamOltBandwidthProfile"` |
| 55 | DownstreamOltBandwidthProfile string `json:"downstreamOltBandwidthProfile"` |
| 56 | ServiceName string `json:"serviceName"` |
| 57 | ConfiguredMacAddress string `json:"configuredMacAddress"` |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 58 | UniTagMatch int `json:"uniTagMatch"` |
| 59 | PonCTag int `json:"ponCTag"` |
| 60 | PonSTag int `json:"ponSTag"` |
| 61 | UsPonCTagPriority int `json:"usPonCTagPriority"` |
| 62 | UsPonSTagPriority int `json:"usPonSTagPriority"` |
| 63 | DsPonCTagPriority int `json:"dsPonCTagPriority"` |
| 64 | DsPonSTagPriority int `json:"dsPonSTagPriority"` |
| 65 | TechnologyProfileID int `json:"technologyProfileId"` |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 66 | EnableMacLearning bool `json:"enableMacLearning"` |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 67 | IsDhcpRequired bool `json:"isDhcpRequired"` |
| 68 | IsIgmpRequired bool `json:"isIgmpRequired"` |
| 69 | IsPppoeRequired bool `json:"isPppoeRequired"` |
| 70 | } |
| 71 | |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 72 | func init() { |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 73 | // Setup this package so that it's log level can be modified at run time |
| 74 | var err error |
| 75 | logger, err = log.AddPackageWithDefaultParam() |
| 76 | if err != nil { |
| 77 | panic(err) |
| 78 | } |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 79 | } |
| 80 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 81 | // SubscriberHandle handle SubscriberInfo Requests |
| 82 | type SubscriberHandle struct { |
| 83 | } |
| 84 | |
| 85 | // ServeHTTP to serve http request |
| 86 | func (sh *SubscriberHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 87 | logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL}) |
| 88 | switch r.Method { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 89 | case cPost: |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 90 | sh.AddSubscriberInfo(context.Background(), w, r) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 91 | case cDelete: |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 92 | sh.DelSubscriberInfo(context.Background(), w, r) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 93 | default: |
| 94 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // AddSubscriberInfo to add service |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 99 | func (sh *SubscriberHandle) AddSubscriberInfo(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 100 | // Get the payload to process the request |
| 101 | d := new(bytes.Buffer) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 102 | if _, err := d.ReadFrom(r.Body); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 103 | logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()}) |
| 104 | return |
| 105 | } |
| 106 | |
| 107 | // Unmarshal the request into service configuration structure |
| 108 | req := &SubscriberDeviceInfo{} |
| 109 | if err := json.Unmarshal(d.Bytes(), req); err != nil { |
| 110 | logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()}) |
| 111 | http.Error(w, err.Error(), http.StatusConflict) |
| 112 | return |
| 113 | } |
| 114 | logger.Debugw(ctx, "Received-northbound-add-service-request", log.Fields{"req": req}) |
| 115 | |
| 116 | //vsCfgList := getVoltServiceFromSrvInfo(req) |
| 117 | |
Akash Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 118 | addAllService(cntx, req) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 119 | } |
| 120 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 121 | func addAllService(cntx context.Context, srvInfo *SubscriberDeviceInfo) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 122 | //vsCfgList := getVoltServiceFromSrvInfo(srvInfo) |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 123 | va := app.GetApplication() |
| 124 | if len(srvInfo.UniTagList) == 0 { |
| 125 | logger.Debugw(ctx, "Received OLT configuration", log.Fields{"req": srvInfo}) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 126 | err := va.AddDeviceConfig(cntx, srvInfo.ID, srvInfo.HardwareIdentifier, srvInfo.NasID, srvInfo.IPAddress, srvInfo.UplinkPort, srvInfo.NniDhcpTrapVid) |
| 127 | if err != nil { |
| 128 | logger.Warnw(ctx, "Device config addition failed :", log.Fields{"Reason": err.Error()}) |
| 129 | } |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 130 | return |
| 131 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 132 | for _, uniTagInfo := range srvInfo.UniTagList { |
| 133 | var vs application.VoltServiceCfg |
| 134 | |
| 135 | svcname := srvInfo.ID + "_" |
| 136 | svcname = svcname + srvInfo.NasPortID + "-" |
| 137 | svcname = svcname + strconv.Itoa(uniTagInfo.UniTagMatch) + "-" |
| 138 | svcname = svcname + strconv.Itoa(uniTagInfo.PonSTag) + "-" |
| 139 | svcname = svcname + strconv.Itoa(uniTagInfo.PonCTag) + "-" |
| 140 | vs.Name = svcname + strconv.Itoa(uniTagInfo.TechnologyProfileID) |
| 141 | |
| 142 | vs.Port = srvInfo.NasPortID |
| 143 | vs.SVlan = of.VlanType(uniTagInfo.PonSTag) |
| 144 | vs.CVlan = of.VlanType(uniTagInfo.PonCTag) |
| 145 | vs.UniVlan = of.VlanType(uniTagInfo.UniTagMatch) |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 146 | vs.UsPonCTagPriority = of.PbitType(uniTagInfo.UsPonCTagPriority) |
| 147 | vs.UsPonSTagPriority = of.PbitType(uniTagInfo.UsPonSTagPriority) |
| 148 | vs.DsPonCTagPriority = of.PbitType(uniTagInfo.UsPonCTagPriority) |
| 149 | vs.DsPonSTagPriority = of.PbitType(uniTagInfo.UsPonSTagPriority) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 150 | vs.TechProfileID = uint16(uniTagInfo.TechnologyProfileID) |
| 151 | vs.UsMeterProfile = uniTagInfo.UpstreamBandwidthProfile |
| 152 | vs.DsMeterProfile = uniTagInfo.DownstreamBandwidthProfile |
| 153 | vs.IgmpEnabled = uniTagInfo.IsIgmpRequired |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 154 | vs.ServiceType = uniTagInfo.ServiceName |
| 155 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 156 | if uniTagInfo.ServiceName == app.DpuMgmtTraffic || |
| 157 | uniTagInfo.ServiceName == app.DpuAncpTraffic || |
| 158 | uniTagInfo.ServiceName == app.FttbSubscriberTraffic { |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 159 | vs.UniVlan = vs.CVlan |
| 160 | vs.Pbits = append(vs.Pbits, of.PbitMatchAll) |
| 161 | } else { |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 162 | if uniTagInfo.UsPonSTagPriority == -1 { |
| 163 | vs.Pbits = append(vs.Pbits, of.PbitMatchAll) |
| 164 | // Process the p-bits received in the request |
| 165 | } else { |
| 166 | if uniTagInfo.UsPonSTagPriority < 8 { |
| 167 | vs.Pbits = append(vs.Pbits, of.PbitType(uniTagInfo.UsPonCTagPriority)) |
| 168 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 169 | |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 170 | if uniTagInfo.UsPonSTagPriority < 8 && uniTagInfo.UsPonSTagPriority != uniTagInfo.DsPonSTagPriority { |
| 171 | vs.Pbits = append(vs.Pbits, of.PbitType(uniTagInfo.DsPonCTagPriority)) |
| 172 | } |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 173 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 174 | } |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 175 | //vs.McastService = uniTagInfo.IsIgmpRequired |
| 176 | if vs.IgmpEnabled { |
| 177 | vs.MvlanProfileName = "mvlan" + strconv.Itoa(uniTagInfo.PonSTag) |
| 178 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 179 | /* |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 180 | var err error |
| 181 | if vs.MacAddr, err = net.ParseMAC(srvInfo.HardwareIdentifier); err != nil { |
| 182 | vs.MacAddr, _ = net.ParseMAC("00:00:00:00:00:00") |
| 183 | }*/ |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 184 | |
| 185 | vs.MacAddr, _ = net.ParseMAC("00:00:00:00:00:00") |
| 186 | if len(vs.Pbits) == 0 { |
| 187 | vs.Pbits = append(vs.Pbits, of.PbitMatchNone) |
| 188 | } |
| 189 | |
| 190 | vnetName := strconv.FormatUint(uint64(vs.SVlan), 10) + "-" |
| 191 | vnetName = vnetName + strconv.FormatUint(uint64(vs.CVlan), 10) + "-" |
| 192 | vnetName = vnetName + strconv.FormatUint(uint64(vs.UniVlan), 10) |
| 193 | |
| 194 | vnetcfg := app.VnetConfig{ |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 195 | Name: vnetName, |
| 196 | SVlan: vs.SVlan, |
| 197 | CVlan: vs.CVlan, |
| 198 | UniVlan: vs.UniVlan, |
| 199 | SVlanTpid: layers.EthernetTypeDot1Q, |
| 200 | DhcpRelay: uniTagInfo.IsDhcpRequired, |
| 201 | VnetType: uniTagInfo.ServiceName, |
| 202 | UsPonCTagPriority: vs.UsPonCTagPriority, |
| 203 | UsPonSTagPriority: vs.UsPonSTagPriority, |
| 204 | DsPonCTagPriority: vs.UsPonCTagPriority, |
| 205 | DsPonSTagPriority: vs.UsPonSTagPriority, |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 206 | //ONTEtherTypeClassification: req.ONTEtherTypeClassification, |
| 207 | //VlanControl: app.VlanControl(req.VlanControl), //TODO |
| 208 | } |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 209 | if uniTagInfo.EnableMacLearning { |
| 210 | vnetcfg.MacLearning = app.Learn |
| 211 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 212 | if uniTagInfo.UsPonSTagPriority < 8 { |
| 213 | vnetcfg.UsDhcpPbit = append(vnetcfg.UsDhcpPbit, of.PbitType(uniTagInfo.UsPonSTagPriority)) |
| 214 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 215 | if vs.CVlan != of.VlanAny && vs.SVlan != of.VlanAny { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 216 | if uniTagInfo.ServiceName == app.DpuMgmtTraffic || |
| 217 | uniTagInfo.ServiceName == app.DpuAncpTraffic { |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 218 | vnetcfg.VlanControl = app.ONUCVlan |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 219 | } else if uniTagInfo.ServiceName == app.FttbSubscriberTraffic { |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 220 | vnetcfg.VlanControl = app.OLTSVlan |
| 221 | } else { |
| 222 | vnetcfg.VlanControl = app.ONUCVlanOLTSVlan |
| 223 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 224 | } else if vs.CVlan == of.VlanAny && vs.UniVlan == of.VlanAny { |
| 225 | vnetcfg.VlanControl = app.OLTSVlan |
| 226 | } |
| 227 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 228 | if err := app.GetApplication().AddVnet(cntx, vnetcfg, nil); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 229 | logger.Errorw(ctx, "AddVnet Failed", log.Fields{"VnetName": vnetName, "Error": err}) |
| 230 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 231 | if err := app.GetApplication().AddService(cntx, vs, nil); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 232 | logger.Errorw(ctx, "AddService Failed", log.Fields{"Service": vs.Name, "Error": err}) |
| 233 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | // DelSubscriberInfo to delete service |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 238 | func (sh *SubscriberHandle) DelSubscriberInfo(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 239 | vars := mux.Vars(r) |
| 240 | id := vars["id"] |
| 241 | |
| 242 | logger.Debugw(ctx, "Received-northbound-del-service-request", log.Fields{"req": id}) |
| 243 | |
| 244 | // HTTP response with 202 accepted for service delete request |
| 245 | w.WriteHeader(http.StatusAccepted) |
| 246 | |
Sridhar Ravindra | b8374ae | 2023-04-14 15:49:25 +0530 | [diff] [blame^] | 247 | d := new(bytes.Buffer) |
| 248 | if _, err := d.ReadFrom(r.Body); err != nil { |
| 249 | logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()}) |
| 250 | return |
| 251 | } |
| 252 | |
| 253 | // Unmarshal the request into service configuration structure |
| 254 | req := &SubscriberDeviceInfo{} |
| 255 | if err := json.Unmarshal(d.Bytes(), req); err != nil { |
| 256 | logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()}) |
| 257 | http.Error(w, err.Error(), http.StatusConflict) |
| 258 | return |
| 259 | } |
| 260 | |
| 261 | for _, uniTagInfo := range req.UniTagList { |
| 262 | svcname := req.ID + "_" |
| 263 | svcname = svcname + req.NasPortID + "-" |
| 264 | svcname = svcname + strconv.Itoa(uniTagInfo.UniTagMatch) + "-" |
| 265 | svcname = svcname + strconv.Itoa(uniTagInfo.PonSTag) + "-" |
| 266 | svcname = svcname + strconv.Itoa(uniTagInfo.PonCTag) + "-" |
| 267 | svcname = svcname + strconv.Itoa(uniTagInfo.TechnologyProfileID) |
| 268 | |
| 269 | if uniTagInfo.ServiceName == app.FttbSubscriberTraffic { |
| 270 | id = svcname |
| 271 | } |
| 272 | } |
| 273 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 274 | logger.Warnw(ctx, "northbound-del-service-req", log.Fields{"ServiceName": id}) |
Akash Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 275 | app.GetApplication().DelServiceWithPrefix(cntx, id) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 276 | } |