Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the License); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an AS IS BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package sadis |
| 18 | |
| 19 | import ( |
| 20 | "encoding/json" |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 21 | "fmt" |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 22 | "net/http" |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 23 | "strconv" |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 24 | "strings" |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 25 | |
| 26 | "github.com/gorilla/mux" |
| 27 | "github.com/opencord/bbsim/internal/bbsim/devices" |
| 28 | "github.com/opencord/bbsim/internal/common" |
| 29 | log "github.com/sirupsen/logrus" |
| 30 | ) |
| 31 | |
| 32 | var sadisLogger = log.WithFields(log.Fields{ |
| 33 | "module": "SADIS", |
| 34 | }) |
| 35 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 36 | const ( |
| 37 | BaseConfigUrl = "/{version}/cfg" |
| 38 | StaticConfigUrl = "/{version}/static" |
| 39 | SadisEntryUrl = "/{version}/subscribers/{ID}" |
| 40 | SadisBwUrl = "/{version}/bandwidthprofiles/{ID}" |
| 41 | ) |
| 42 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 43 | type SadisServer struct { |
| 44 | Olt *devices.OltDevice |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // bandwidthProfiles contains some dummy profiles |
Matteo Scandolo | 9496714 | 2021-05-28 11:37:06 -0700 | [diff] [blame] | 48 | var bandwidthProfiles = map[string][]*SadisBWPEntry{ |
| 49 | common.BP_FORMAT_MEF: { |
| 50 | {ID: "User_Bandwidth1", AIR: 100000, CBS: 10000, CIR: 30000, EBS: 1000, EIR: 100000}, |
| 51 | {ID: "User_Bandwidth2", AIR: 100000, CBS: 5000, CIR: 100000, EBS: 5000, EIR: 100000}, |
| 52 | {ID: "User_Bandwidth3", AIR: 100000, CBS: 5000, CIR: 1000000, EBS: 5000, EIR: 1000000}, |
| 53 | {ID: "Default", AIR: 100000, CBS: 30, CIR: 600, EBS: 30, EIR: 400}, |
| 54 | }, |
| 55 | common.BP_FORMAT_IETF: { |
| 56 | {ID: "User_Bandwidth1", CBS: 10000, CIR: 30000, GIR: 100000, PIR: 20000, PBS: 1000}, |
| 57 | {ID: "User_Bandwidth2", CBS: 5000, CIR: 100000, GIR: 100000, PIR: 30000, PBS: 5000}, |
| 58 | {ID: "User_Bandwidth3", CBS: 5000, CIR: 1000000, GIR: 100000, PIR: 40000, PBS: 5000}, |
| 59 | {ID: "Default", CBS: 30, CIR: 600, GIR: 0, PIR: 32000, PBS: 30}, |
| 60 | }, |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // SadisConfig is the top-level SADIS configuration struct |
| 64 | type SadisConfig struct { |
| 65 | Sadis SadisEntries `json:"sadis"` |
| 66 | BandwidthProfile BandwidthProfileEntries `json:"bandwidthprofile"` |
| 67 | } |
| 68 | |
| 69 | type SadisEntries struct { |
| 70 | Integration SadisIntegration `json:"integration"` |
| 71 | Entries []interface{} `json:"entries,omitempty"` |
| 72 | } |
| 73 | type BandwidthProfileEntries struct { |
| 74 | Integration SadisIntegration `json:"integration"` |
Matteo Scandolo | 51d6a31 | 2020-03-12 15:54:43 -0700 | [diff] [blame] | 75 | Entries []*SadisBWPEntry `json:"entries,omitempty"` |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | type SadisIntegration struct { |
| 79 | URL string `json:"url,omitempty"` |
| 80 | Cache struct { |
| 81 | Enabled bool `json:"enabled"` |
| 82 | MaxSize int `json:"maxsize"` |
| 83 | TTL string `json:"ttl"` |
| 84 | } `json:"cache"` |
| 85 | } |
| 86 | |
| 87 | type SadisOltEntry struct { |
| 88 | ID string `json:"id"` |
| 89 | HardwareIdentifier string `json:"hardwareIdentifier"` |
| 90 | IPAddress string `json:"ipAddress"` |
| 91 | NasID string `json:"nasId"` |
| 92 | UplinkPort int `json:"uplinkPort"` |
| 93 | } |
| 94 | |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 95 | type SadisOnuEntryV2 struct { |
| 96 | ID string `json:"id"` |
| 97 | NasPortID string `json:"nasPortId"` |
| 98 | CircuitID string `json:"circuitId"` |
| 99 | RemoteID string `json:"remoteId"` |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 100 | UniTagList []SadisUniTag `json:"uniTagList"` // this can be SadisUniTagAtt, SadisUniTagDt |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 101 | } |
| 102 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 103 | type SadisUniTag struct { |
| 104 | UniTagMatch int `json:"uniTagMatch,omitempty"` |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 105 | PonCTag int `json:"ponCTag,omitempty"` |
| 106 | PonSTag int `json:"ponSTag,omitempty"` |
| 107 | TechnologyProfileID int `json:"technologyProfileId,omitempty"` |
| 108 | UpstreamBandwidthProfile string `json:"upstreamBandwidthProfile,omitempty"` |
| 109 | DownstreamBandwidthProfile string `json:"downstreamBandwidthProfile,omitempty"` |
| 110 | IsDhcpRequired bool `json:"isDhcpRequired,omitempty"` |
| 111 | IsIgmpRequired bool `json:"isIgmpRequired,omitempty"` |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 112 | ConfiguredMacAddress string `json:"configuredMacAddress,omitempty"` |
Matteo Scandolo | 8d28137 | 2020-09-03 16:23:37 -0700 | [diff] [blame] | 113 | UsPonCTagPriority uint8 `json:"usPonCTagPriority,omitempty"` |
| 114 | UsPonSTagPriority uint8 `json:"usPonSTagPriority,omitempty"` |
| 115 | DsPonCTagPriority uint8 `json:"dsPonCTagPriority,omitempty"` |
| 116 | DsPonSTagPriority uint8 `json:"dsPonSTagPriority,omitempty"` |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 117 | ServiceName string `json:"serviceName,omitempty"` |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 120 | // SADIS BandwithProfile Entry |
| 121 | type SadisBWPEntry struct { |
Matteo Scandolo | 9496714 | 2021-05-28 11:37:06 -0700 | [diff] [blame] | 122 | // common attributes |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 123 | ID string `json:"id"` |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 124 | CBS int `json:"cbs"` |
| 125 | CIR int `json:"cir"` |
Matteo Scandolo | 9496714 | 2021-05-28 11:37:06 -0700 | [diff] [blame] | 126 | // MEF attributes |
| 127 | AIR int `json:"air,omitempty"` |
| 128 | EBS int `json:"ebs,omitempty"` |
| 129 | EIR int `json:"eir,omitempty"` |
| 130 | // IETF attributes |
| 131 | GIR int `json:"gir,omitempty"` |
| 132 | PIR int `json:"pir,omitempty"` |
| 133 | PBS int `json:"pbs,omitempty"` |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // GetSadisConfig returns a full SADIS configuration struct ready to be marshalled into JSON |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 137 | func GetSadisConfig(olt *devices.OltDevice, version string) *SadisConfig { |
| 138 | sadisEntries, _ := GetSadisEntries(olt, version) |
| 139 | bwpEntries := getBWPEntries(version) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 140 | |
| 141 | conf := &SadisConfig{} |
| 142 | conf.Sadis = *sadisEntries |
| 143 | conf.BandwidthProfile = *bwpEntries |
| 144 | |
| 145 | return conf |
| 146 | } |
| 147 | |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 148 | func GetSadisEntries(olt *devices.OltDevice, version string) (*SadisEntries, error) { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 149 | solt, _ := GetOltEntry(olt) |
| 150 | |
| 151 | entries := []interface{}{} |
| 152 | entries = append(entries, solt) |
| 153 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 154 | a := strings.Split(common.Config.BBSim.SadisRestAddress, ":") |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 155 | port := a[len(a)-1] |
| 156 | |
| 157 | integration := SadisIntegration{} |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 158 | integration.URL = "http://bbsim:" + port + "/" + version + "/subscribers/%s" |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 159 | integration.Cache.Enabled = false |
| 160 | integration.Cache.MaxSize = 50 |
| 161 | integration.Cache.TTL = "PT0m" |
| 162 | |
| 163 | sadis := &SadisEntries{ |
| 164 | integration, |
| 165 | entries, |
| 166 | } |
| 167 | |
| 168 | return sadis, nil |
| 169 | } |
| 170 | |
| 171 | func GetOltEntry(olt *devices.OltDevice) (*SadisOltEntry, error) { |
| 172 | ip, _ := common.GetIPAddr("nni") // TODO verify which IP to report |
| 173 | solt := &SadisOltEntry{ |
| 174 | ID: olt.SerialNumber, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 175 | HardwareIdentifier: common.Config.Olt.DeviceId, |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 176 | IPAddress: ip, |
| 177 | NasID: olt.SerialNumber, |
Girish Gowdra | 9500560 | 2021-10-22 11:33:01 -0700 | [diff] [blame^] | 178 | UplinkPort: 16777216, // TODO currently assumes we only have one NNI port |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 179 | } |
| 180 | return solt, nil |
| 181 | } |
| 182 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 183 | func GetOnuEntryV2(olt *devices.OltDevice, onu *devices.Onu, uniStr string) (*SadisOnuEntryV2, error) { |
| 184 | uniSuffix := "-" + uniStr |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 185 | |
| 186 | sonuv2 := &SadisOnuEntryV2{ |
Matteo Scandolo | df08044 | 2020-10-09 11:57:38 -0700 | [diff] [blame] | 187 | ID: onu.Sn() + uniSuffix, |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 188 | } |
Matteo Scandolo | 51d6a31 | 2020-03-12 15:54:43 -0700 | [diff] [blame] | 189 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 190 | uniId, err := strconv.ParseUint(uniStr, 10, 32) |
| 191 | if err != nil { |
| 192 | return nil, err |
| 193 | } |
| 194 | |
| 195 | // find the correct UNI |
| 196 | // NOTE that in SADIS uni.Id 0 corresponds to BBSM00000101-1 |
| 197 | uni, err := onu.FindUniById(uint32(uniId - 1)) |
| 198 | if err != nil { |
| 199 | return nil, err |
| 200 | } |
| 201 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 202 | // createUniTagList |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 203 | for _, s := range uni.Services { |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 204 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 205 | service := s.(*devices.Service) |
| 206 | |
| 207 | tag := SadisUniTag{ |
| 208 | ServiceName: service.Name, |
| 209 | IsIgmpRequired: service.NeedsIgmp, |
| 210 | IsDhcpRequired: service.NeedsDhcp, |
| 211 | TechnologyProfileID: service.TechnologyProfileID, |
| 212 | UpstreamBandwidthProfile: "User_Bandwidth1", |
| 213 | DownstreamBandwidthProfile: "User_Bandwidth2", |
| 214 | PonCTag: service.CTag, |
| 215 | PonSTag: service.STag, |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 216 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 217 | |
| 218 | if service.UniTagMatch != 0 { |
| 219 | tag.UniTagMatch = service.UniTagMatch |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 220 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 221 | |
| 222 | if service.ConfigureMacAddress { |
| 223 | tag.ConfiguredMacAddress = service.HwAddress.String() |
| 224 | } |
| 225 | |
| 226 | if service.UsPonCTagPriority != 0 { |
| 227 | tag.UsPonCTagPriority = service.UsPonCTagPriority |
| 228 | } |
| 229 | |
| 230 | if service.UsPonSTagPriority != 0 { |
| 231 | tag.UsPonSTagPriority = service.UsPonSTagPriority |
| 232 | } |
| 233 | |
| 234 | if service.DsPonCTagPriority != 0 { |
| 235 | tag.DsPonCTagPriority = service.DsPonCTagPriority |
| 236 | } |
| 237 | |
| 238 | if service.DsPonSTagPriority != 0 { |
| 239 | tag.DsPonSTagPriority = service.DsPonSTagPriority |
| 240 | } |
| 241 | |
| 242 | sonuv2.UniTagList = append(sonuv2.UniTagList, tag) |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 243 | } |
Matteo Scandolo | 51d6a31 | 2020-03-12 15:54:43 -0700 | [diff] [blame] | 244 | |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 245 | return sonuv2, nil |
| 246 | } |
| 247 | |
| 248 | func getBWPEntries(version string) *BandwidthProfileEntries { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 249 | a := strings.Split(common.Config.BBSim.SadisRestAddress, ":") |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 250 | port := a[len(a)-1] |
| 251 | |
| 252 | integration := SadisIntegration{} |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 253 | integration.URL = "http://bbsim:" + port + "/" + version + "/bandwidthprofiles/%s" |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 254 | integration.Cache.Enabled = true |
| 255 | integration.Cache.MaxSize = 40 |
| 256 | integration.Cache.TTL = "PT1m" |
| 257 | |
| 258 | bwp := &BandwidthProfileEntries{ |
| 259 | Integration: integration, |
| 260 | } |
| 261 | |
| 262 | return bwp |
| 263 | } |
| 264 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 265 | func (s *SadisServer) ServeBaseConfig(w http.ResponseWriter, r *http.Request) { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 266 | w.Header().Set("Content-Type", "application/json") |
| 267 | w.WriteHeader(http.StatusOK) |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 268 | vars := mux.Vars(r) |
| 269 | |
| 270 | if vars["version"] != "v1" && vars["version"] != "v2" { |
| 271 | w.WriteHeader(http.StatusNotFound) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 272 | _, _ = w.Write([]byte("{}")) |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 273 | return |
| 274 | } |
| 275 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 276 | sadisConf := GetSadisConfig(s.Olt, vars["version"]) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 277 | |
| 278 | sadisJSON, _ := json.Marshal(sadisConf) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 279 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 280 | _, _ = w.Write([]byte(sadisJSON)) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 281 | |
| 282 | } |
| 283 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 284 | func (s *SadisServer) ServeStaticConfig(w http.ResponseWriter, r *http.Request) { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 285 | w.Header().Set("Content-Type", "application/json") |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 286 | |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 287 | vars := mux.Vars(r) |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 288 | |
| 289 | if vars["version"] == "v1" { |
| 290 | // TODO format error |
| 291 | http.Error(w, fmt.Sprintf("api-v1-unsupported"), http.StatusBadRequest) |
| 292 | return |
| 293 | } |
| 294 | |
| 295 | w.WriteHeader(http.StatusOK) |
| 296 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 297 | sadisConf := GetSadisConfig(s.Olt, vars["version"]) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 298 | |
| 299 | sadisConf.Sadis.Integration.URL = "" |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 300 | for i := range s.Olt.Pons { |
| 301 | for _, onu := range s.Olt.Pons[i].Onus { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 302 | if vars["version"] == "v2" { |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 303 | for _, u := range onu.UniPorts { |
| 304 | uni := u.(*devices.UniPort) |
| 305 | sonuV2, _ := GetOnuEntryV2(s.Olt, onu, fmt.Sprintf("%d", uni.ID+1)) |
| 306 | sadisConf.Sadis.Entries = append(sadisConf.Sadis.Entries, sonuV2) |
| 307 | } |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 308 | } |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | sadisConf.BandwidthProfile.Integration.URL = "" |
Matteo Scandolo | 9496714 | 2021-05-28 11:37:06 -0700 | [diff] [blame] | 313 | sadisConf.BandwidthProfile.Entries = bandwidthProfiles[common.Config.BBSim.BandwidthProfileFormat] |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 314 | |
| 315 | sadisJSON, _ := json.Marshal(sadisConf) |
| 316 | sadisLogger.Tracef("SADIS JSON: %s", sadisJSON) |
| 317 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 318 | _, _ = w.Write([]byte(sadisJSON)) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 319 | |
| 320 | } |
| 321 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 322 | func (s *SadisServer) ServeEntry(w http.ResponseWriter, r *http.Request) { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 323 | w.Header().Set("Content-Type", "application/json") |
| 324 | vars := mux.Vars(r) |
| 325 | |
| 326 | // check if the requested ID is for the OLT |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 327 | if s.Olt.SerialNumber == vars["ID"] { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 328 | sadisLogger.WithFields(log.Fields{ |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 329 | "OltSn": s.Olt.SerialNumber, |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 330 | }).Debug("Received SADIS OLT request") |
| 331 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 332 | sadisConf, _ := GetOltEntry(s.Olt) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 333 | |
| 334 | w.WriteHeader(http.StatusOK) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 335 | _ = json.NewEncoder(w).Encode(sadisConf) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 336 | return |
| 337 | } |
| 338 | |
| 339 | i := strings.Split(vars["ID"], "-") // split ID to get serial number and uni port |
| 340 | if len(i) != 2 { |
| 341 | w.WriteHeader(http.StatusUnprocessableEntity) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 342 | _, _ = w.Write([]byte("{}")) |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 343 | sadisLogger.Warnf("Received invalid SADIS SubscriberId: %s", vars["ID"]) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 344 | return |
| 345 | } |
| 346 | sn, uni := i[0], i[len(i)-1] |
| 347 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 348 | onu, err := s.Olt.FindOnuBySn(sn) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 349 | if err != nil { |
| 350 | w.WriteHeader(http.StatusNotFound) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 351 | _, _ = w.Write([]byte("{}")) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 352 | sadisLogger.WithFields(log.Fields{ |
| 353 | "OnuSn": sn, |
| 354 | "OnuId": "NA", |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 355 | }).Warnf("Requested Subscriber entry not found for OnuSn: %s", vars["ID"]) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 356 | return |
| 357 | } |
| 358 | |
| 359 | sadisLogger.WithFields(log.Fields{ |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 360 | "OnuId": onu.ID, |
| 361 | "OnuSn": sn, |
| 362 | "UniId": uni, |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 363 | }).Debug("Received SADIS request") |
| 364 | |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 365 | if vars["version"] == "v1" { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 366 | // TODO format error |
| 367 | w.WriteHeader(http.StatusBadRequest) |
| 368 | _ = json.NewEncoder(w).Encode("Sadis v1 is not supported anymore, please go back to an earlier BBSim version") |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 369 | } else if vars["version"] == "v2" { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 370 | w.WriteHeader(http.StatusOK) |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 371 | sadisConf, _ := GetOnuEntryV2(s.Olt, onu, uni) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 372 | _ = json.NewEncoder(w).Encode(sadisConf) |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 373 | } |
| 374 | |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 375 | } |
| 376 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 377 | func (s *SadisServer) ServeBWPEntry(w http.ResponseWriter, r *http.Request) { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 378 | w.Header().Set("Content-Type", "application/json") |
| 379 | vars := mux.Vars(r) |
| 380 | id := vars["ID"] |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 381 | |
| 382 | if vars["version"] != "v1" && vars["version"] != "v2" { |
| 383 | w.WriteHeader(http.StatusNotFound) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 384 | _, _ = w.Write([]byte("{}")) |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 385 | return |
| 386 | } |
| 387 | |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 388 | sadisLogger.Debugf("Received request for SADIS bandwidth profile %s", id) |
| 389 | |
Matteo Scandolo | 9496714 | 2021-05-28 11:37:06 -0700 | [diff] [blame] | 390 | for _, bwpEntry := range bandwidthProfiles[common.Config.BBSim.BandwidthProfileFormat] { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 391 | if bwpEntry.ID == id { |
| 392 | w.WriteHeader(http.StatusOK) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 393 | _ = json.NewEncoder(w).Encode(bwpEntry) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 394 | return |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | w.WriteHeader(http.StatusNotFound) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 399 | _, _ = w.Write([]byte("{}")) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 400 | } |