Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 1 | /* |
Joey Armstrong | 14628cd | 2023-01-10 08:38:31 -0500 | [diff] [blame^] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 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"` |
Andrea Campanella | 6f5f355 | 2022-03-10 17:14:25 +0100 | [diff] [blame] | 93 | NniDhcpTrapVid int `json:"nniDhcpTrapVid,omitempty"` |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 96 | type SadisOnuEntryV2 struct { |
| 97 | ID string `json:"id"` |
| 98 | NasPortID string `json:"nasPortId"` |
| 99 | CircuitID string `json:"circuitId"` |
| 100 | RemoteID string `json:"remoteId"` |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 101 | UniTagList []SadisUniTag `json:"uniTagList"` // this can be SadisUniTagAtt, SadisUniTagDt |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 102 | } |
| 103 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 104 | type SadisUniTag struct { |
| 105 | UniTagMatch int `json:"uniTagMatch,omitempty"` |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 106 | PonCTag int `json:"ponCTag,omitempty"` |
| 107 | PonSTag int `json:"ponSTag,omitempty"` |
| 108 | TechnologyProfileID int `json:"technologyProfileId,omitempty"` |
| 109 | UpstreamBandwidthProfile string `json:"upstreamBandwidthProfile,omitempty"` |
| 110 | DownstreamBandwidthProfile string `json:"downstreamBandwidthProfile,omitempty"` |
| 111 | IsDhcpRequired bool `json:"isDhcpRequired,omitempty"` |
| 112 | IsIgmpRequired bool `json:"isIgmpRequired,omitempty"` |
Andrea Campanella | 2989045 | 2022-02-03 16:00:19 +0100 | [diff] [blame] | 113 | IsPPPoERequired bool `json:"isPppoeRequired,omitempty"` |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 114 | ConfiguredMacAddress string `json:"configuredMacAddress,omitempty"` |
Andrea Campanella | 2989045 | 2022-02-03 16:00:19 +0100 | [diff] [blame] | 115 | EnableMacLearning bool `json:"enableMacLearning,omitempty"` |
Matteo Scandolo | 8d28137 | 2020-09-03 16:23:37 -0700 | [diff] [blame] | 116 | UsPonCTagPriority uint8 `json:"usPonCTagPriority,omitempty"` |
| 117 | UsPonSTagPriority uint8 `json:"usPonSTagPriority,omitempty"` |
| 118 | DsPonCTagPriority uint8 `json:"dsPonCTagPriority,omitempty"` |
| 119 | DsPonSTagPriority uint8 `json:"dsPonSTagPriority,omitempty"` |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 120 | ServiceName string `json:"serviceName,omitempty"` |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 123 | // SADIS BandwithProfile Entry |
| 124 | type SadisBWPEntry struct { |
Matteo Scandolo | 9496714 | 2021-05-28 11:37:06 -0700 | [diff] [blame] | 125 | // common attributes |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 126 | ID string `json:"id"` |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 127 | CBS int `json:"cbs"` |
| 128 | CIR int `json:"cir"` |
Matteo Scandolo | 9496714 | 2021-05-28 11:37:06 -0700 | [diff] [blame] | 129 | // MEF attributes |
| 130 | AIR int `json:"air,omitempty"` |
| 131 | EBS int `json:"ebs,omitempty"` |
| 132 | EIR int `json:"eir,omitempty"` |
| 133 | // IETF attributes |
| 134 | GIR int `json:"gir,omitempty"` |
| 135 | PIR int `json:"pir,omitempty"` |
| 136 | PBS int `json:"pbs,omitempty"` |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // 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] | 140 | func GetSadisConfig(olt *devices.OltDevice, version string) *SadisConfig { |
| 141 | sadisEntries, _ := GetSadisEntries(olt, version) |
| 142 | bwpEntries := getBWPEntries(version) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 143 | |
| 144 | conf := &SadisConfig{} |
| 145 | conf.Sadis = *sadisEntries |
| 146 | conf.BandwidthProfile = *bwpEntries |
| 147 | |
| 148 | return conf |
| 149 | } |
| 150 | |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 151 | func GetSadisEntries(olt *devices.OltDevice, version string) (*SadisEntries, error) { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 152 | solt, _ := GetOltEntry(olt) |
| 153 | |
| 154 | entries := []interface{}{} |
| 155 | entries = append(entries, solt) |
| 156 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 157 | a := strings.Split(common.Config.BBSim.SadisRestAddress, ":") |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 158 | port := a[len(a)-1] |
| 159 | |
| 160 | integration := SadisIntegration{} |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 161 | integration.URL = "http://bbsim:" + port + "/" + version + "/subscribers/%s" |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 162 | integration.Cache.Enabled = false |
| 163 | integration.Cache.MaxSize = 50 |
| 164 | integration.Cache.TTL = "PT0m" |
| 165 | |
| 166 | sadis := &SadisEntries{ |
| 167 | integration, |
| 168 | entries, |
| 169 | } |
| 170 | |
| 171 | return sadis, nil |
| 172 | } |
| 173 | |
| 174 | func GetOltEntry(olt *devices.OltDevice) (*SadisOltEntry, error) { |
| 175 | ip, _ := common.GetIPAddr("nni") // TODO verify which IP to report |
| 176 | solt := &SadisOltEntry{ |
| 177 | ID: olt.SerialNumber, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 178 | HardwareIdentifier: common.Config.Olt.DeviceId, |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 179 | IPAddress: ip, |
| 180 | NasID: olt.SerialNumber, |
Girish Gowdra | 9500560 | 2021-10-22 11:33:01 -0700 | [diff] [blame] | 181 | UplinkPort: 16777216, // TODO currently assumes we only have one NNI port |
Andrea Campanella | 6f5f355 | 2022-03-10 17:14:25 +0100 | [diff] [blame] | 182 | NniDhcpTrapVid: olt.NniDhcpTrapVid, |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 183 | } |
| 184 | return solt, nil |
| 185 | } |
| 186 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 187 | func GetOnuEntryV2(olt *devices.OltDevice, onu *devices.Onu, uniStr string) (*SadisOnuEntryV2, error) { |
| 188 | uniSuffix := "-" + uniStr |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 189 | |
| 190 | sonuv2 := &SadisOnuEntryV2{ |
Matteo Scandolo | df08044 | 2020-10-09 11:57:38 -0700 | [diff] [blame] | 191 | ID: onu.Sn() + uniSuffix, |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 192 | } |
Matteo Scandolo | 51d6a31 | 2020-03-12 15:54:43 -0700 | [diff] [blame] | 193 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 194 | uniId, err := strconv.ParseUint(uniStr, 10, 32) |
| 195 | if err != nil { |
| 196 | return nil, err |
| 197 | } |
| 198 | |
| 199 | // find the correct UNI |
| 200 | // NOTE that in SADIS uni.Id 0 corresponds to BBSM00000101-1 |
| 201 | uni, err := onu.FindUniById(uint32(uniId - 1)) |
| 202 | if err != nil { |
| 203 | return nil, err |
| 204 | } |
| 205 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 206 | // createUniTagList |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 207 | for _, s := range uni.Services { |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 208 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 209 | service := s.(*devices.Service) |
| 210 | |
| 211 | tag := SadisUniTag{ |
| 212 | ServiceName: service.Name, |
| 213 | IsIgmpRequired: service.NeedsIgmp, |
| 214 | IsDhcpRequired: service.NeedsDhcp, |
Andrea Campanella | 2989045 | 2022-02-03 16:00:19 +0100 | [diff] [blame] | 215 | IsPPPoERequired: service.NeedsPPPoE, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 216 | TechnologyProfileID: service.TechnologyProfileID, |
| 217 | UpstreamBandwidthProfile: "User_Bandwidth1", |
| 218 | DownstreamBandwidthProfile: "User_Bandwidth2", |
Andrea Campanella | 2989045 | 2022-02-03 16:00:19 +0100 | [diff] [blame] | 219 | EnableMacLearning: service.EnableMacLearning, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 220 | PonCTag: service.CTag, |
| 221 | PonSTag: service.STag, |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 222 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 223 | |
| 224 | if service.UniTagMatch != 0 { |
| 225 | tag.UniTagMatch = service.UniTagMatch |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 226 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 227 | |
| 228 | if service.ConfigureMacAddress { |
| 229 | tag.ConfiguredMacAddress = service.HwAddress.String() |
| 230 | } |
| 231 | |
| 232 | if service.UsPonCTagPriority != 0 { |
| 233 | tag.UsPonCTagPriority = service.UsPonCTagPriority |
| 234 | } |
| 235 | |
| 236 | if service.UsPonSTagPriority != 0 { |
| 237 | tag.UsPonSTagPriority = service.UsPonSTagPriority |
| 238 | } |
| 239 | |
| 240 | if service.DsPonCTagPriority != 0 { |
| 241 | tag.DsPonCTagPriority = service.DsPonCTagPriority |
| 242 | } |
| 243 | |
| 244 | if service.DsPonSTagPriority != 0 { |
| 245 | tag.DsPonSTagPriority = service.DsPonSTagPriority |
| 246 | } |
| 247 | |
| 248 | sonuv2.UniTagList = append(sonuv2.UniTagList, tag) |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 249 | } |
Matteo Scandolo | 51d6a31 | 2020-03-12 15:54:43 -0700 | [diff] [blame] | 250 | |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 251 | return sonuv2, nil |
| 252 | } |
| 253 | |
| 254 | func getBWPEntries(version string) *BandwidthProfileEntries { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 255 | a := strings.Split(common.Config.BBSim.SadisRestAddress, ":") |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 256 | port := a[len(a)-1] |
| 257 | |
| 258 | integration := SadisIntegration{} |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 259 | integration.URL = "http://bbsim:" + port + "/" + version + "/bandwidthprofiles/%s" |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 260 | integration.Cache.Enabled = true |
| 261 | integration.Cache.MaxSize = 40 |
| 262 | integration.Cache.TTL = "PT1m" |
| 263 | |
| 264 | bwp := &BandwidthProfileEntries{ |
| 265 | Integration: integration, |
| 266 | } |
| 267 | |
| 268 | return bwp |
| 269 | } |
| 270 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 271 | func (s *SadisServer) ServeBaseConfig(w http.ResponseWriter, r *http.Request) { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 272 | w.Header().Set("Content-Type", "application/json") |
| 273 | w.WriteHeader(http.StatusOK) |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 274 | vars := mux.Vars(r) |
| 275 | |
| 276 | if vars["version"] != "v1" && vars["version"] != "v2" { |
| 277 | w.WriteHeader(http.StatusNotFound) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 278 | _, _ = w.Write([]byte("{}")) |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 279 | return |
| 280 | } |
| 281 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 282 | sadisConf := GetSadisConfig(s.Olt, vars["version"]) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 283 | |
| 284 | sadisJSON, _ := json.Marshal(sadisConf) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 285 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 286 | _, _ = w.Write([]byte(sadisJSON)) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 287 | |
| 288 | } |
| 289 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 290 | func (s *SadisServer) ServeStaticConfig(w http.ResponseWriter, r *http.Request) { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 291 | w.Header().Set("Content-Type", "application/json") |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 292 | |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 293 | vars := mux.Vars(r) |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 294 | |
| 295 | if vars["version"] == "v1" { |
| 296 | // TODO format error |
| 297 | http.Error(w, fmt.Sprintf("api-v1-unsupported"), http.StatusBadRequest) |
| 298 | return |
| 299 | } |
| 300 | |
| 301 | w.WriteHeader(http.StatusOK) |
| 302 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 303 | sadisConf := GetSadisConfig(s.Olt, vars["version"]) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 304 | |
| 305 | sadisConf.Sadis.Integration.URL = "" |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 306 | for i := range s.Olt.Pons { |
| 307 | for _, onu := range s.Olt.Pons[i].Onus { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 308 | if vars["version"] == "v2" { |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 309 | for _, u := range onu.UniPorts { |
| 310 | uni := u.(*devices.UniPort) |
| 311 | sonuV2, _ := GetOnuEntryV2(s.Olt, onu, fmt.Sprintf("%d", uni.ID+1)) |
| 312 | sadisConf.Sadis.Entries = append(sadisConf.Sadis.Entries, sonuV2) |
| 313 | } |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 314 | } |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | sadisConf.BandwidthProfile.Integration.URL = "" |
Matteo Scandolo | 9496714 | 2021-05-28 11:37:06 -0700 | [diff] [blame] | 319 | sadisConf.BandwidthProfile.Entries = bandwidthProfiles[common.Config.BBSim.BandwidthProfileFormat] |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 320 | |
| 321 | sadisJSON, _ := json.Marshal(sadisConf) |
| 322 | sadisLogger.Tracef("SADIS JSON: %s", sadisJSON) |
| 323 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 324 | _, _ = w.Write([]byte(sadisJSON)) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 325 | |
| 326 | } |
| 327 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 328 | func (s *SadisServer) ServeEntry(w http.ResponseWriter, r *http.Request) { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 329 | w.Header().Set("Content-Type", "application/json") |
| 330 | vars := mux.Vars(r) |
| 331 | |
| 332 | // check if the requested ID is for the OLT |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 333 | if s.Olt.SerialNumber == vars["ID"] { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 334 | sadisLogger.WithFields(log.Fields{ |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 335 | "OltSn": s.Olt.SerialNumber, |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 336 | }).Debug("Received SADIS OLT request") |
| 337 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 338 | sadisConf, _ := GetOltEntry(s.Olt) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 339 | |
| 340 | w.WriteHeader(http.StatusOK) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 341 | _ = json.NewEncoder(w).Encode(sadisConf) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 342 | return |
| 343 | } |
| 344 | |
| 345 | i := strings.Split(vars["ID"], "-") // split ID to get serial number and uni port |
| 346 | if len(i) != 2 { |
| 347 | w.WriteHeader(http.StatusUnprocessableEntity) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 348 | _, _ = w.Write([]byte("{}")) |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 349 | sadisLogger.Warnf("Received invalid SADIS SubscriberId: %s", vars["ID"]) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 350 | return |
| 351 | } |
| 352 | sn, uni := i[0], i[len(i)-1] |
| 353 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 354 | onu, err := s.Olt.FindOnuBySn(sn) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 355 | if err != nil { |
| 356 | w.WriteHeader(http.StatusNotFound) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 357 | _, _ = w.Write([]byte("{}")) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 358 | sadisLogger.WithFields(log.Fields{ |
| 359 | "OnuSn": sn, |
| 360 | "OnuId": "NA", |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 361 | }).Warnf("Requested Subscriber entry not found for OnuSn: %s", vars["ID"]) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 362 | return |
| 363 | } |
| 364 | |
| 365 | sadisLogger.WithFields(log.Fields{ |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 366 | "OnuId": onu.ID, |
| 367 | "OnuSn": sn, |
| 368 | "UniId": uni, |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 369 | }).Debug("Received SADIS request") |
| 370 | |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 371 | if vars["version"] == "v1" { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 372 | // TODO format error |
| 373 | w.WriteHeader(http.StatusBadRequest) |
| 374 | _ = 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] | 375 | } else if vars["version"] == "v2" { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 376 | w.WriteHeader(http.StatusOK) |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 377 | sadisConf, _ := GetOnuEntryV2(s.Olt, onu, uni) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 378 | _ = json.NewEncoder(w).Encode(sadisConf) |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 379 | } |
| 380 | |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 383 | func (s *SadisServer) ServeBWPEntry(w http.ResponseWriter, r *http.Request) { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 384 | w.Header().Set("Content-Type", "application/json") |
| 385 | vars := mux.Vars(r) |
| 386 | id := vars["ID"] |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 387 | |
| 388 | if vars["version"] != "v1" && vars["version"] != "v2" { |
| 389 | w.WriteHeader(http.StatusNotFound) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 390 | _, _ = w.Write([]byte("{}")) |
Anand S Katti | b409ee0 | 2020-02-20 20:10:00 +0530 | [diff] [blame] | 391 | return |
| 392 | } |
| 393 | |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 394 | sadisLogger.Debugf("Received request for SADIS bandwidth profile %s", id) |
| 395 | |
Matteo Scandolo | 9496714 | 2021-05-28 11:37:06 -0700 | [diff] [blame] | 396 | for _, bwpEntry := range bandwidthProfiles[common.Config.BBSim.BandwidthProfileFormat] { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 397 | if bwpEntry.ID == id { |
| 398 | w.WriteHeader(http.StatusOK) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 399 | _ = json.NewEncoder(w).Encode(bwpEntry) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 400 | return |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | w.WriteHeader(http.StatusNotFound) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 405 | _, _ = w.Write([]byte("{}")) |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 406 | } |