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