Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [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 devices |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 21 | "encoding/hex" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 22 | "fmt" |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 23 | "io" |
| 24 | "reflect" |
| 25 | "time" |
| 26 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 27 | "github.com/google/gopacket" |
| 28 | "github.com/google/gopacket/layers" |
| 29 | "github.com/opencord/bbsim/internal/bbsim/devices" |
| 30 | "github.com/opencord/bbsim/internal/bbsim/packetHandlers" |
| 31 | "github.com/opencord/bbsim/internal/common" |
Matteo Scandolo | 4f4ac79 | 2020-10-01 16:33:21 -0700 | [diff] [blame] | 32 | "github.com/opencord/voltha-protos/v4/go/openolt" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 33 | log "github.com/sirupsen/logrus" |
| 34 | "google.golang.org/grpc" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | type OltMock struct { |
Matteo Scandolo | 583f17d | 2020-02-13 10:35:17 -0800 | [diff] [blame] | 38 | LastUsedOnuId map[uint32]uint32 |
| 39 | Olt *devices.OltDevice |
| 40 | BBSimIp string |
| 41 | BBSimPort string |
| 42 | BBSimApiPort string |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 43 | |
| 44 | conn *grpc.ClientConn |
| 45 | |
| 46 | TargetOnus int |
| 47 | CompletedOnus int // Number of ONUs that have received a DHCPAck |
| 48 | } |
| 49 | |
| 50 | // trigger an enable call and start the same listeners on the gRPC stream that VOLTHA would create |
| 51 | // this method is blocking |
| 52 | func (o *OltMock) Start() { |
| 53 | log.Info("Starting Mock OLT") |
| 54 | |
| 55 | for _, pon := range o.Olt.Pons { |
| 56 | for _, onu := range pon.Onus { |
Matteo Scandolo | d32c382 | 2019-11-26 15:57:46 -0700 | [diff] [blame] | 57 | if err := onu.InternalState.Event("initialize"); err != nil { |
| 58 | log.Fatalf("Error initializing ONU: %v", err) |
| 59 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 60 | log.Debugf("Created ONU: %s", onu.Sn()) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | client, conn := Connect(o.BBSimIp, o.BBSimPort) |
| 65 | o.conn = conn |
| 66 | defer conn.Close() |
| 67 | |
| 68 | deviceInfo, err := o.getDeviceInfo(client) |
| 69 | |
| 70 | if err != nil { |
| 71 | log.WithFields(log.Fields{ |
| 72 | "error": err, |
| 73 | }).Fatal("Can't read device info") |
| 74 | } |
| 75 | |
| 76 | log.WithFields(log.Fields{ |
| 77 | "Vendor": deviceInfo.Vendor, |
| 78 | "Model": deviceInfo.Model, |
| 79 | "DeviceSerialNumber": deviceInfo.DeviceSerialNumber, |
| 80 | "PonPorts": deviceInfo.PonPorts, |
| 81 | }).Info("Retrieved device info") |
| 82 | |
| 83 | o.readIndications(client) |
| 84 | |
| 85 | } |
| 86 | |
| 87 | func (o *OltMock) getDeviceInfo(client openolt.OpenoltClient) (*openolt.DeviceInfo, error) { |
| 88 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 89 | defer cancel() |
| 90 | |
| 91 | return client.GetDeviceInfo(ctx, new(openolt.Empty)) |
| 92 | } |
| 93 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 94 | func (o *OltMock) readIndications(client openolt.OpenoltClient) { |
| 95 | defer func() { |
| 96 | log.Info("OLT readIndications done") |
| 97 | }() |
| 98 | |
| 99 | // Tell the OLT to start sending indications |
| 100 | indications, err := client.EnableIndication(context.Background(), new(openolt.Empty)) |
| 101 | if err != nil { |
| 102 | log.WithFields(log.Fields{ |
| 103 | "error": err, |
| 104 | }).Error("Failed to enable indication stream") |
| 105 | return |
| 106 | } |
| 107 | |
| 108 | // listen for indications |
| 109 | for { |
| 110 | indication, err := indications.Recv() |
| 111 | if err == io.EOF { |
| 112 | break |
| 113 | } |
| 114 | if err != nil { |
| 115 | |
| 116 | // the connection is closed once we have sent the DHCP_ACK packet to all of the ONUs |
| 117 | // it means BBR completed, it's not an error |
| 118 | |
| 119 | log.WithFields(log.Fields{ |
| 120 | "error": err, |
| 121 | }).Debug("Failed to read from indications") |
| 122 | break |
| 123 | } |
| 124 | |
| 125 | o.handleIndication(client, indication) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func (o *OltMock) handleIndication(client openolt.OpenoltClient, indication *openolt.Indication) { |
| 130 | switch indication.Data.(type) { |
| 131 | case *openolt.Indication_OltInd: |
| 132 | log.Info("Received Indication_OltInd") |
| 133 | case *openolt.Indication_IntfInd: |
| 134 | log.Info("Received Indication_IntfInd") |
| 135 | case *openolt.Indication_IntfOperInd: |
| 136 | log.Info("Received Indication_IntfOperInd") |
| 137 | case *openolt.Indication_OnuDiscInd: |
| 138 | onuDiscInd := indication.GetOnuDiscInd() |
| 139 | o.handleOnuDiscIndication(client, onuDiscInd) |
| 140 | case *openolt.Indication_OnuInd: |
| 141 | onuInd := indication.GetOnuInd() |
| 142 | o.handleOnuIndication(client, onuInd) |
| 143 | case *openolt.Indication_OmciInd: |
| 144 | omciIndication := indication.GetOmciInd() |
| 145 | o.handleOmciIndication(client, omciIndication) |
| 146 | case *openolt.Indication_PktInd: |
| 147 | pktIndication := indication.GetPktInd() |
| 148 | o.handlePktIndication(client, pktIndication) |
| 149 | case *openolt.Indication_PortStats: |
| 150 | case *openolt.Indication_FlowStats: |
| 151 | case *openolt.Indication_AlarmInd: |
| 152 | default: |
| 153 | log.WithFields(log.Fields{ |
| 154 | "data": indication.Data, |
| 155 | "type": reflect.TypeOf(indication.Data), |
| 156 | }).Warn("Indication unsupported") |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func (o *OltMock) handleOnuDiscIndication(client openolt.OpenoltClient, onuDiscInd *openolt.OnuDiscIndication) { |
| 161 | log.WithFields(log.Fields{ |
| 162 | "IntfId": onuDiscInd.IntfId, |
| 163 | "SerialNumber": common.OnuSnToString(onuDiscInd.SerialNumber), |
| 164 | }).Info("Received Onu discovery indication") |
| 165 | |
| 166 | onu, err := o.Olt.FindOnuBySn(common.OnuSnToString(onuDiscInd.SerialNumber)) |
| 167 | |
| 168 | if err != nil { |
| 169 | log.WithFields(log.Fields{ |
| 170 | "IntfId": onuDiscInd.IntfId, |
| 171 | "SerialNumber": common.OnuSnToString(onuDiscInd.SerialNumber), |
Matteo Scandolo | 583f17d | 2020-02-13 10:35:17 -0800 | [diff] [blame] | 172 | "Err": err, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 173 | }).Fatal("Cannot find ONU") |
| 174 | } |
| 175 | |
Matteo Scandolo | 583f17d | 2020-02-13 10:35:17 -0800 | [diff] [blame] | 176 | // creating and storing ONU IDs |
| 177 | id := o.LastUsedOnuId[onuDiscInd.IntfId] + 1 |
| 178 | o.LastUsedOnuId[onuDiscInd.IntfId] = o.LastUsedOnuId[onuDiscInd.IntfId] + 1 |
| 179 | onu.SetID(id) |
| 180 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 181 | var pir uint32 = 1000000 |
| 182 | Onu := openolt.Onu{ |
| 183 | IntfId: onu.PonPortID, |
Matteo Scandolo | 583f17d | 2020-02-13 10:35:17 -0800 | [diff] [blame] | 184 | OnuId: id, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 185 | SerialNumber: onu.SerialNumber, |
| 186 | Pir: pir, |
| 187 | } |
| 188 | |
| 189 | if _, err := client.ActivateOnu(context.Background(), &Onu); err != nil { |
| 190 | log.WithFields(log.Fields{ |
| 191 | "IntfId": onuDiscInd.IntfId, |
| 192 | "SerialNumber": common.OnuSnToString(onuDiscInd.SerialNumber), |
| 193 | }).Error("Failed to activate ONU") |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func (o *OltMock) handleOnuIndication(client openolt.OpenoltClient, onuInd *openolt.OnuIndication) { |
| 198 | log.WithFields(log.Fields{ |
| 199 | "IntfId": onuInd.IntfId, |
| 200 | "SerialNumber": common.OnuSnToString(onuInd.SerialNumber), |
| 201 | }).Info("Received Onu indication") |
| 202 | |
| 203 | onu, err := o.Olt.FindOnuBySn(common.OnuSnToString(onuInd.SerialNumber)) |
| 204 | |
| 205 | if err != nil { |
| 206 | log.WithFields(log.Fields{ |
| 207 | "IntfId": onuInd.IntfId, |
| 208 | "SerialNumber": common.OnuSnToString(onuInd.SerialNumber), |
| 209 | }).Fatal("Cannot find ONU") |
| 210 | } |
| 211 | |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 212 | ctx, cancel := context.WithCancel(context.TODO()) |
| 213 | go onu.ProcessOnuMessages(ctx, nil, client) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 214 | |
| 215 | go func() { |
| 216 | |
| 217 | defer func() { |
| 218 | log.WithFields(log.Fields{ |
| 219 | "onuSn": common.OnuSnToString(onuInd.SerialNumber), |
| 220 | "CompletedOnus": o.CompletedOnus, |
| 221 | "TargetOnus": o.TargetOnus, |
| 222 | }).Debugf("Onu done") |
| 223 | |
Matteo Scandolo | 569e717 | 2019-12-20 11:51:51 -0800 | [diff] [blame] | 224 | // close the ONU channel |
| 225 | cancel() |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 226 | }() |
| 227 | |
| 228 | for message := range onu.DoneChannel { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 229 | if message { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 230 | o.CompletedOnus++ |
| 231 | if o.CompletedOnus == o.TargetOnus { |
| 232 | // NOTE once all the ONUs are completed, exit |
| 233 | // closing the connection is not the most elegant way, |
| 234 | // but I haven't found any other way to stop |
| 235 | // the indications.Recv() infinite loop |
| 236 | log.Info("Simulation Done") |
| 237 | ValidateAndClose(o) |
| 238 | } |
| 239 | |
| 240 | break |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | }() |
| 245 | |
| 246 | // TODO change the state instead of calling an ONU method from here |
| 247 | onu.StartOmci(client) |
| 248 | } |
| 249 | |
| 250 | func (o *OltMock) handleOmciIndication(client openolt.OpenoltClient, omciInd *openolt.OmciIndication) { |
| 251 | |
| 252 | pon, err := o.Olt.GetPonById(omciInd.IntfId) |
| 253 | if err != nil { |
| 254 | log.WithFields(log.Fields{ |
| 255 | "OnuId": omciInd.OnuId, |
| 256 | "IntfId": omciInd.IntfId, |
| 257 | "err": err, |
| 258 | }).Fatal("Can't find PonPort") |
| 259 | } |
| 260 | onu, _ := pon.GetOnuById(omciInd.OnuId) |
| 261 | if err != nil { |
| 262 | log.WithFields(log.Fields{ |
| 263 | "OnuId": omciInd.OnuId, |
| 264 | "IntfId": omciInd.IntfId, |
| 265 | "err": err, |
| 266 | }).Fatal("Can't find Onu") |
| 267 | } |
| 268 | |
| 269 | log.WithFields(log.Fields{ |
| 270 | "IntfId": onu.PonPortID, |
| 271 | "OnuId": onu.ID, |
| 272 | "OnuSn": onu.Sn(), |
| 273 | "Pkt": omciInd.Pkt, |
| 274 | }).Trace("Received Onu omci indication") |
| 275 | |
| 276 | msg := devices.Message{ |
| 277 | Type: devices.OmciIndication, |
| 278 | Data: devices.OmciIndicationMessage{ |
| 279 | OnuSN: onu.SerialNumber, |
| 280 | OnuID: onu.ID, |
| 281 | OmciInd: omciInd, |
| 282 | }, |
| 283 | } |
| 284 | onu.Channel <- msg |
| 285 | } |
| 286 | |
| 287 | func (o *OltMock) handlePktIndication(client openolt.OpenoltClient, pktIndication *openolt.PacketIndication) { |
| 288 | |
| 289 | pkt := gopacket.NewPacket(pktIndication.Pkt, layers.LayerTypeEthernet, gopacket.Default) |
| 290 | |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 291 | pktType, err := packetHandlers.GetPktType(pkt) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 292 | |
| 293 | if err != nil { |
| 294 | log.Warnf("Ignoring packet as it's neither EAPOL or DHCP") |
| 295 | return |
| 296 | } |
| 297 | |
| 298 | log.WithFields(log.Fields{ |
| 299 | "IntfType": pktIndication.IntfType, |
| 300 | "IntfId": pktIndication.IntfId, |
| 301 | "GemportId": pktIndication.GemportId, |
| 302 | "FlowId": pktIndication.FlowId, |
| 303 | "PortNo": pktIndication.PortNo, |
| 304 | "Cookie": pktIndication.Cookie, |
| 305 | "pktType": pktType, |
| 306 | }).Trace("Received PktIndication") |
| 307 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 308 | if pktIndication.IntfType == "nni" { |
| 309 | // This is an packet that is arriving from the NNI and needs to be sent to an ONU |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 310 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 311 | onuMac, err := packetHandlers.GetDstMacAddressFromPacket(pkt) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 312 | |
| 313 | if err != nil { |
| 314 | log.WithFields(log.Fields{ |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 315 | "IntfType": "nni", |
| 316 | "Pkt": hex.EncodeToString(pkt.Data()), |
| 317 | }).Fatal("Can't find Dst MacAddress in packet") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 320 | s, err := o.Olt.FindServiceByMacAddress(onuMac) |
| 321 | if err != nil { |
| 322 | log.WithFields(log.Fields{ |
| 323 | "IntfType": "nni", |
| 324 | "Pkt": hex.EncodeToString(pkt.Data()), |
| 325 | "MacAddress": onuMac.String(), |
| 326 | }).Fatal("Can't find ONU with MacAddress") |
| 327 | } |
| 328 | |
| 329 | service := s.(*devices.Service) |
| 330 | onu := service.Onu |
| 331 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 332 | msg := devices.Message{ |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 333 | Type: devices.OnuPacketIn, |
| 334 | Data: devices.OnuPacketMessage{ |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 335 | IntfId: pktIndication.IntfId, |
| 336 | OnuId: onu.ID, |
| 337 | Packet: pkt, |
| 338 | Type: pktType, |
| 339 | GemPortId: pktIndication.GemportId, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 340 | }, |
| 341 | } |
| 342 | // NOTE we send it on the ONU channel so that is handled as all the others packets in a separate thread |
| 343 | onu.Channel <- msg |
| 344 | } else { |
| 345 | // TODO a very similar construct is used in many places, |
| 346 | // abstract this in an OLT method |
| 347 | pon, err := o.Olt.GetPonById(pktIndication.IntfId) |
| 348 | if err != nil { |
| 349 | log.WithFields(log.Fields{ |
| 350 | "OnuId": pktIndication.PortNo, |
| 351 | "IntfId": pktIndication.IntfId, |
| 352 | "err": err, |
| 353 | }).Fatal("Can't find PonPort") |
| 354 | } |
| 355 | onu, err := pon.GetOnuById(pktIndication.PortNo) |
| 356 | if err != nil { |
| 357 | log.WithFields(log.Fields{ |
| 358 | "OnuId": pktIndication.PortNo, |
| 359 | "IntfId": pktIndication.IntfId, |
| 360 | "err": err, |
| 361 | }).Fatal("Can't find Onu") |
| 362 | } |
| 363 | // NOTE when we push the EAPOL flow we set the PortNo = OnuId for convenience sake |
| 364 | // BBsim responds setting the port number that was sent with the flow |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 365 | msg := devices.Message{ |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 366 | Type: devices.OnuPacketIn, |
| 367 | Data: devices.OnuPacketMessage{ |
| 368 | IntfId: pktIndication.IntfId, |
| 369 | OnuId: pktIndication.PortNo, |
| 370 | Packet: pkt, |
| 371 | Type: pktType, |
| 372 | }, |
| 373 | } |
| 374 | onu.Channel <- msg |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // TODO Move in a different file |
| 379 | func Connect(ip string, port string) (openolt.OpenoltClient, *grpc.ClientConn) { |
| 380 | server := fmt.Sprintf("%s:%s", ip, port) |
| 381 | conn, err := grpc.Dial(server, grpc.WithInsecure()) |
| 382 | |
| 383 | if err != nil { |
| 384 | log.Fatalf("did not connect: %v", err) |
| 385 | return nil, conn |
| 386 | } |
| 387 | return openolt.NewOpenoltClient(conn), conn |
| 388 | } |