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" |
| 23 | "net/http" |
| 24 | "strconv" |
| 25 | |
| 26 | "github.com/google/gopacket/layers" |
| 27 | "github.com/gorilla/mux" |
| 28 | "voltha-go-controller/internal/pkg/application" |
| 29 | app "voltha-go-controller/internal/pkg/application" |
| 30 | "voltha-go-controller/internal/pkg/of" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 31 | "voltha-go-controller/log" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | //SubscriberDeviceInfo - Subcriber Device Info |
| 35 | type SubscriberDeviceInfo struct { |
| 36 | ID string `json:"id"` |
| 37 | NasPortID string `json:"nasPortId"` |
| 38 | UplinkPort int `json:"uplinkPort"` |
| 39 | Slot int `json:"slot"` |
| 40 | HardwareIdentifier string `json:"hardwareIdentifier"` |
| 41 | IPAddress net.IP `json:"ipAddress"` |
| 42 | NasID string `json:"nasId"` |
| 43 | CircuitID string `json:"circuitId"` |
| 44 | RemoteID string `json:"remoteId"` |
| 45 | UniTagList []UniTagInformation `json:"uniTagList"` |
| 46 | } |
| 47 | |
| 48 | //UniTagInformation - Service information |
| 49 | type UniTagInformation struct { |
| 50 | UniTagMatch int `json:"uniTagMatch"` |
| 51 | PonCTag int `json:"ponCTag"` |
| 52 | PonSTag int `json:"ponSTag"` |
| 53 | UsPonCTagPriority int `json:"usPonCTagPriority"` |
| 54 | UsPonSTagPriority int `json:"usPonSTagPriority"` |
| 55 | DsPonCTagPriority int `json:"dsPonCTagPriority"` |
| 56 | DsPonSTagPriority int `json:"dsPonSTagPriority"` |
| 57 | TechnologyProfileID int `json:"technologyProfileId"` |
| 58 | UpstreamBandwidthProfile string `json:"upstreamBandwidthProfile"` |
| 59 | DownstreamBandwidthProfile string `json:"downstreamBandwidthProfile"` |
| 60 | UpstreamOltBandwidthProfile string `json:"upstreamOltBandwidthProfile"` |
| 61 | DownstreamOltBandwidthProfile string `json:"downstreamOltBandwidthProfile"` |
| 62 | ServiceName string `json:"serviceName"` |
| 63 | EnableMacLearning bool `json:"enableMacLearning"` |
| 64 | ConfiguredMacAddress string `json:"configuredMacAddress"` |
| 65 | IsDhcpRequired bool `json:"isDhcpRequired"` |
| 66 | IsIgmpRequired bool `json:"isIgmpRequired"` |
| 67 | IsPppoeRequired bool `json:"isPppoeRequired"` |
| 68 | } |
| 69 | |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame^] | 70 | |
| 71 | func init() { |
| 72 | // Setup this package so that it's log level can be modified at run time |
| 73 | var err error |
| 74 | logger, err = log.AddPackageWithDefaultParam() |
| 75 | if err != nil { |
| 76 | panic(err) |
| 77 | } |
| 78 | } |
| 79 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 80 | // SubscriberHandle handle SubscriberInfo Requests |
| 81 | type SubscriberHandle struct { |
| 82 | } |
| 83 | |
| 84 | // ServeHTTP to serve http request |
| 85 | func (sh *SubscriberHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 86 | logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL}) |
| 87 | switch r.Method { |
| 88 | case "POST": |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 89 | sh.AddSubscriberInfo(context.Background(), w, r) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 90 | case "DELETE": |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 91 | sh.DelSubscriberInfo(context.Background(), w, r) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 92 | default: |
| 93 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // AddSubscriberInfo to add service |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 98 | 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] | 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 | // 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 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 118 | go 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 | |
| 123 | //vsCfgList := getVoltServiceFromSrvInfo(srvInfo) |
| 124 | |
| 125 | for _, uniTagInfo := range srvInfo.UniTagList { |
| 126 | var vs application.VoltServiceCfg |
| 127 | |
| 128 | svcname := srvInfo.ID + "_" |
| 129 | svcname = svcname + srvInfo.NasPortID + "-" |
| 130 | svcname = svcname + strconv.Itoa(uniTagInfo.UniTagMatch) + "-" |
| 131 | svcname = svcname + strconv.Itoa(uniTagInfo.PonSTag) + "-" |
| 132 | svcname = svcname + strconv.Itoa(uniTagInfo.PonCTag) + "-" |
| 133 | vs.Name = svcname + strconv.Itoa(uniTagInfo.TechnologyProfileID) |
| 134 | |
| 135 | vs.Port = srvInfo.NasPortID |
| 136 | vs.SVlan = of.VlanType(uniTagInfo.PonSTag) |
| 137 | vs.CVlan = of.VlanType(uniTagInfo.PonCTag) |
| 138 | vs.UniVlan = of.VlanType(uniTagInfo.UniTagMatch) |
| 139 | vs.TechProfileID = uint16(uniTagInfo.TechnologyProfileID) |
| 140 | vs.UsMeterProfile = uniTagInfo.UpstreamBandwidthProfile |
| 141 | vs.DsMeterProfile = uniTagInfo.DownstreamBandwidthProfile |
| 142 | vs.IgmpEnabled = uniTagInfo.IsIgmpRequired |
| 143 | //vs.McastService = uniTagInfo.IsIgmpRequired |
| 144 | if vs.IgmpEnabled { |
| 145 | vs.MvlanProfileName = "mvlan" + strconv.Itoa(uniTagInfo.PonSTag) |
| 146 | } |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame^] | 147 | if uniTagInfo.UsPonSTagPriority == -1 { |
| 148 | vs.Pbits = append(vs.Pbits, of.PbitMatchAll) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 149 | // Process the p-bits received in the request |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame^] | 150 | } else { |
| 151 | if uniTagInfo.UsPonSTagPriority < 8 { |
| 152 | vs.Pbits = append(vs.Pbits, of.PbitType(uniTagInfo.UsPonCTagPriority)) |
| 153 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 154 | |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame^] | 155 | if uniTagInfo.UsPonSTagPriority < 8 && uniTagInfo.UsPonSTagPriority != uniTagInfo.DsPonSTagPriority { |
| 156 | vs.Pbits = append(vs.Pbits, of.PbitType(uniTagInfo.DsPonCTagPriority)) |
| 157 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* |
| 161 | var err error |
| 162 | if vs.MacAddr, err = net.ParseMAC(srvInfo.HardwareIdentifier); err != nil { |
| 163 | vs.MacAddr, _ = net.ParseMAC("00:00:00:00:00:00") |
| 164 | }*/ |
| 165 | |
| 166 | vs.MacAddr, _ = net.ParseMAC("00:00:00:00:00:00") |
| 167 | if len(vs.Pbits) == 0 { |
| 168 | vs.Pbits = append(vs.Pbits, of.PbitMatchNone) |
| 169 | } |
| 170 | |
| 171 | vnetName := strconv.FormatUint(uint64(vs.SVlan), 10) + "-" |
| 172 | vnetName = vnetName + strconv.FormatUint(uint64(vs.CVlan), 10) + "-" |
| 173 | vnetName = vnetName + strconv.FormatUint(uint64(vs.UniVlan), 10) |
| 174 | |
| 175 | vnetcfg := app.VnetConfig{ |
| 176 | Name: vnetName, |
| 177 | SVlan: vs.SVlan, |
| 178 | CVlan: vs.CVlan, |
| 179 | UniVlan: vs.UniVlan, |
| 180 | SVlanTpid: layers.EthernetTypeDot1Q, |
| 181 | DhcpRelay: uniTagInfo.IsDhcpRequired, |
| 182 | //MacLearning: req.MacLearning, |
| 183 | //ONTEtherTypeClassification: req.ONTEtherTypeClassification, |
| 184 | //VlanControl: app.VlanControl(req.VlanControl), //TODO |
| 185 | } |
| 186 | if uniTagInfo.UsPonSTagPriority < 8 { |
| 187 | vnetcfg.UsDhcpPbit = append(vnetcfg.UsDhcpPbit, of.PbitType(uniTagInfo.UsPonSTagPriority)) |
| 188 | } |
| 189 | |
| 190 | if vs.CVlan != of.VlanAny && vs.SVlan != of.VlanAny { |
| 191 | vnetcfg.VlanControl = app.ONUCVlanOLTSVlan |
| 192 | } else if vs.CVlan == of.VlanAny && vs.UniVlan == of.VlanAny { |
| 193 | vnetcfg.VlanControl = app.OLTSVlan |
| 194 | } |
| 195 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 196 | if err := app.GetApplication().AddVnet(cntx, vnetcfg, nil); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 197 | logger.Errorw(ctx, "AddVnet Failed", log.Fields{"VnetName": vnetName, "Error": err}) |
| 198 | } |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 199 | if err := app.GetApplication().AddService(cntx, vs, nil); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 200 | logger.Errorw(ctx, "AddService Failed", log.Fields{"Service": vs.Name, "Error": err}) |
| 201 | } |
| 202 | |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // DelSubscriberInfo to delete service |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 207 | 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] | 208 | |
| 209 | vars := mux.Vars(r) |
| 210 | id := vars["id"] |
| 211 | |
| 212 | logger.Debugw(ctx, "Received-northbound-del-service-request", log.Fields{"req": id}) |
| 213 | |
| 214 | // HTTP response with 202 accepted for service delete request |
| 215 | w.WriteHeader(http.StatusAccepted) |
| 216 | |
| 217 | logger.Warnw(ctx, "northbound-del-service-req", log.Fields{"ServiceName": id}) |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 218 | go app.GetApplication().DelServiceWithPrefix(cntx, id) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 219 | } |