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 | |
| 35 | //SubscriberDeviceInfo - Subcriber Device Info |
| 36 | type SubscriberDeviceInfo struct { |
| 37 | ID string `json:"id"` |
| 38 | NasPortID string `json:"nasPortId"` |
| 39 | UplinkPort int `json:"uplinkPort"` |
| 40 | Slot int `json:"slot"` |
| 41 | HardwareIdentifier string `json:"hardwareIdentifier"` |
Akash Soni | 87a1907 | 2023-02-28 00:46:59 +0530 | [diff] [blame^] | 42 | IPAddress string `json:"ipAddress"` |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 43 | NasID string `json:"nasId"` |
| 44 | CircuitID string `json:"circuitId"` |
| 45 | RemoteID string `json:"remoteId"` |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 46 | NniDhcpTrapVid int `json:"nniDhcpTrapVid"` |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 47 | UniTagList []UniTagInformation `json:"uniTagList"` |
| 48 | } |
| 49 | |
| 50 | //UniTagInformation - Service information |
| 51 | type UniTagInformation struct { |
| 52 | UniTagMatch int `json:"uniTagMatch"` |
| 53 | PonCTag int `json:"ponCTag"` |
| 54 | PonSTag int `json:"ponSTag"` |
| 55 | UsPonCTagPriority int `json:"usPonCTagPriority"` |
| 56 | UsPonSTagPriority int `json:"usPonSTagPriority"` |
| 57 | DsPonCTagPriority int `json:"dsPonCTagPriority"` |
| 58 | DsPonSTagPriority int `json:"dsPonSTagPriority"` |
| 59 | TechnologyProfileID int `json:"technologyProfileId"` |
| 60 | UpstreamBandwidthProfile string `json:"upstreamBandwidthProfile"` |
| 61 | DownstreamBandwidthProfile string `json:"downstreamBandwidthProfile"` |
| 62 | UpstreamOltBandwidthProfile string `json:"upstreamOltBandwidthProfile"` |
| 63 | DownstreamOltBandwidthProfile string `json:"downstreamOltBandwidthProfile"` |
| 64 | ServiceName string `json:"serviceName"` |
| 65 | EnableMacLearning bool `json:"enableMacLearning"` |
| 66 | ConfiguredMacAddress string `json:"configuredMacAddress"` |
| 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 { |
| 89 | case "POST": |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 90 | sh.AddSubscriberInfo(context.Background(), w, r) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 91 | case "DELETE": |
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 | |
| 101 | // Get the payload to process the request |
| 102 | d := new(bytes.Buffer) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 103 | if _, err := d.ReadFrom(r.Body); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 104 | logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()}) |
| 105 | return |
| 106 | } |
| 107 | |
| 108 | // Unmarshal the request into service configuration structure |
| 109 | req := &SubscriberDeviceInfo{} |
| 110 | if err := json.Unmarshal(d.Bytes(), req); err != nil { |
| 111 | logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()}) |
| 112 | http.Error(w, err.Error(), http.StatusConflict) |
| 113 | return |
| 114 | } |
| 115 | logger.Debugw(ctx, "Received-northbound-add-service-request", log.Fields{"req": req}) |
| 116 | |
| 117 | //vsCfgList := getVoltServiceFromSrvInfo(req) |
| 118 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 119 | go addAllService(cntx, req) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 120 | } |
| 121 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 122 | func addAllService(cntx context.Context, srvInfo *SubscriberDeviceInfo) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 123 | |
| 124 | //vsCfgList := getVoltServiceFromSrvInfo(srvInfo) |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 125 | va := app.GetApplication() |
| 126 | if len(srvInfo.UniTagList) == 0 { |
| 127 | logger.Debugw(ctx, "Received OLT configuration", log.Fields{"req": srvInfo}) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 128 | err := va.AddDeviceConfig(cntx, srvInfo.ID, srvInfo.HardwareIdentifier, srvInfo.NasID, srvInfo.IPAddress, srvInfo.UplinkPort, srvInfo.NniDhcpTrapVid) |
| 129 | if err != nil { |
| 130 | logger.Warnw(ctx, "Device config addition failed :", log.Fields{"Reason": err.Error()}) |
| 131 | } |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 132 | return |
| 133 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 134 | for _, uniTagInfo := range srvInfo.UniTagList { |
| 135 | var vs application.VoltServiceCfg |
| 136 | |
| 137 | svcname := srvInfo.ID + "_" |
| 138 | svcname = svcname + srvInfo.NasPortID + "-" |
| 139 | svcname = svcname + strconv.Itoa(uniTagInfo.UniTagMatch) + "-" |
| 140 | svcname = svcname + strconv.Itoa(uniTagInfo.PonSTag) + "-" |
| 141 | svcname = svcname + strconv.Itoa(uniTagInfo.PonCTag) + "-" |
| 142 | vs.Name = svcname + strconv.Itoa(uniTagInfo.TechnologyProfileID) |
| 143 | |
| 144 | vs.Port = srvInfo.NasPortID |
| 145 | vs.SVlan = of.VlanType(uniTagInfo.PonSTag) |
| 146 | vs.CVlan = of.VlanType(uniTagInfo.PonCTag) |
| 147 | vs.UniVlan = of.VlanType(uniTagInfo.UniTagMatch) |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 148 | vs.UsPonCTagPriority = of.PbitType(uniTagInfo.UsPonCTagPriority) |
| 149 | vs.UsPonSTagPriority = of.PbitType(uniTagInfo.UsPonSTagPriority) |
| 150 | vs.DsPonCTagPriority = of.PbitType(uniTagInfo.UsPonCTagPriority) |
| 151 | vs.DsPonSTagPriority = of.PbitType(uniTagInfo.UsPonSTagPriority) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 152 | vs.TechProfileID = uint16(uniTagInfo.TechnologyProfileID) |
| 153 | vs.UsMeterProfile = uniTagInfo.UpstreamBandwidthProfile |
| 154 | vs.DsMeterProfile = uniTagInfo.DownstreamBandwidthProfile |
| 155 | vs.IgmpEnabled = uniTagInfo.IsIgmpRequired |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 156 | vs.ServiceType = uniTagInfo.ServiceName |
| 157 | |
| 158 | if uniTagInfo.ServiceName == app.DPU_MGMT_TRAFFIC || |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 159 | uniTagInfo.ServiceName == app.DPU_ANCP_TRAFFIC || |
| 160 | uniTagInfo.ServiceName == app.FTTB_SUBSCRIBER_TRAFFIC { |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 161 | vs.UniVlan = vs.CVlan |
| 162 | vs.Pbits = append(vs.Pbits, of.PbitMatchAll) |
| 163 | } else { |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 164 | if uniTagInfo.UsPonSTagPriority == -1 { |
| 165 | vs.Pbits = append(vs.Pbits, of.PbitMatchAll) |
| 166 | // Process the p-bits received in the request |
| 167 | } else { |
| 168 | if uniTagInfo.UsPonSTagPriority < 8 { |
| 169 | vs.Pbits = append(vs.Pbits, of.PbitType(uniTagInfo.UsPonCTagPriority)) |
| 170 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 171 | |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 172 | if uniTagInfo.UsPonSTagPriority < 8 && uniTagInfo.UsPonSTagPriority != uniTagInfo.DsPonSTagPriority { |
| 173 | vs.Pbits = append(vs.Pbits, of.PbitType(uniTagInfo.DsPonCTagPriority)) |
| 174 | } |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 175 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 176 | } |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 177 | //vs.McastService = uniTagInfo.IsIgmpRequired |
| 178 | if vs.IgmpEnabled { |
| 179 | vs.MvlanProfileName = "mvlan" + strconv.Itoa(uniTagInfo.PonSTag) |
| 180 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 181 | /* |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 182 | var err error |
| 183 | if vs.MacAddr, err = net.ParseMAC(srvInfo.HardwareIdentifier); err != nil { |
| 184 | vs.MacAddr, _ = net.ParseMAC("00:00:00:00:00:00") |
| 185 | }*/ |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 186 | |
| 187 | vs.MacAddr, _ = net.ParseMAC("00:00:00:00:00:00") |
| 188 | if len(vs.Pbits) == 0 { |
| 189 | vs.Pbits = append(vs.Pbits, of.PbitMatchNone) |
| 190 | } |
| 191 | |
| 192 | vnetName := strconv.FormatUint(uint64(vs.SVlan), 10) + "-" |
| 193 | vnetName = vnetName + strconv.FormatUint(uint64(vs.CVlan), 10) + "-" |
| 194 | vnetName = vnetName + strconv.FormatUint(uint64(vs.UniVlan), 10) |
| 195 | |
| 196 | vnetcfg := app.VnetConfig{ |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 197 | Name: vnetName, |
| 198 | SVlan: vs.SVlan, |
| 199 | CVlan: vs.CVlan, |
| 200 | UniVlan: vs.UniVlan, |
| 201 | SVlanTpid: layers.EthernetTypeDot1Q, |
| 202 | DhcpRelay: uniTagInfo.IsDhcpRequired, |
| 203 | VnetType: uniTagInfo.ServiceName, |
| 204 | UsPonCTagPriority: vs.UsPonCTagPriority, |
| 205 | UsPonSTagPriority: vs.UsPonSTagPriority, |
| 206 | DsPonCTagPriority: vs.UsPonCTagPriority, |
| 207 | DsPonSTagPriority: vs.UsPonSTagPriority, |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 208 | //ONTEtherTypeClassification: req.ONTEtherTypeClassification, |
| 209 | //VlanControl: app.VlanControl(req.VlanControl), //TODO |
| 210 | } |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 211 | if uniTagInfo.EnableMacLearning { |
| 212 | vnetcfg.MacLearning = app.Learn |
| 213 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 214 | if uniTagInfo.UsPonSTagPriority < 8 { |
| 215 | vnetcfg.UsDhcpPbit = append(vnetcfg.UsDhcpPbit, of.PbitType(uniTagInfo.UsPonSTagPriority)) |
| 216 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 217 | if vs.CVlan != of.VlanAny && vs.SVlan != of.VlanAny { |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 218 | if uniTagInfo.ServiceName == app.DPU_MGMT_TRAFFIC || |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 219 | uniTagInfo.ServiceName == app.DPU_ANCP_TRAFFIC { |
Tinoj Joseph | 50d722c | 2022-12-06 22:53:22 +0530 | [diff] [blame] | 220 | vnetcfg.VlanControl = app.ONUCVlan |
| 221 | } else if uniTagInfo.ServiceName == app.FTTB_SUBSCRIBER_TRAFFIC { |
| 222 | vnetcfg.VlanControl = app.OLTSVlan |
| 223 | } else { |
| 224 | vnetcfg.VlanControl = app.ONUCVlanOLTSVlan |
| 225 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 226 | } else if vs.CVlan == of.VlanAny && vs.UniVlan == of.VlanAny { |
| 227 | vnetcfg.VlanControl = app.OLTSVlan |
| 228 | } |
| 229 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 230 | if err := app.GetApplication().AddVnet(cntx, vnetcfg, nil); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 231 | logger.Errorw(ctx, "AddVnet Failed", log.Fields{"VnetName": vnetName, "Error": err}) |
| 232 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 233 | if err := app.GetApplication().AddService(cntx, vs, nil); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 234 | logger.Errorw(ctx, "AddService Failed", log.Fields{"Service": vs.Name, "Error": err}) |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // DelSubscriberInfo to delete service |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 241 | 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] | 242 | |
| 243 | vars := mux.Vars(r) |
| 244 | id := vars["id"] |
| 245 | |
| 246 | logger.Debugw(ctx, "Received-northbound-del-service-request", log.Fields{"req": id}) |
| 247 | |
| 248 | // HTTP response with 202 accepted for service delete request |
| 249 | w.WriteHeader(http.StatusAccepted) |
| 250 | |
| 251 | logger.Warnw(ctx, "northbound-del-service-req", log.Fields{"ServiceName": id}) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 252 | go app.GetApplication().DelServiceWithPrefix(cntx, id) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 253 | } |