Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 2c03936 | 2024-02-04 18:51:52 -0500 | [diff] [blame] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [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 devices |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 22 | "time" |
| 23 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 24 | "github.com/opencord/bbsim/api/bbsim" |
Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame] | 25 | pb "github.com/opencord/bbsim/api/bbsim" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 26 | log "github.com/sirupsen/logrus" |
| 27 | "google.golang.org/grpc" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | func ValidateAndClose(olt *OltMock) { |
| 31 | |
| 32 | // connect to the BBSim control APIs to check that all the ONUs are in the correct state |
| 33 | client, conn := ApiConnect(olt.BBSimIp, olt.BBSimApiPort) |
| 34 | defer conn.Close() |
| 35 | |
| 36 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 37 | defer cancel() |
| 38 | |
Nitin Subramanian | b0a333a | 2021-07-08 15:01:41 -0700 | [diff] [blame] | 39 | req := pb.UNIRequest{ |
| 40 | OnuSerialNumber: "", |
| 41 | UniID: "", |
| 42 | } |
| 43 | |
| 44 | services, err := client.GetServices(ctx, &req) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 45 | |
| 46 | if err != nil { |
| 47 | log.WithFields(log.Fields{ |
| 48 | "error": err, |
| 49 | }).Fatalf("Can't reach BBSim API") |
| 50 | } |
| 51 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 52 | expectedEapolState := "eap_response_success_received" |
| 53 | expectedDhcpState := "dhcp_ack_received" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 54 | |
| 55 | res := true |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 56 | for _, service := range services.Items { |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 57 | if service.UniId != 0 { |
| 58 | // BBR only interacts with the first UNI, rightfully so services for different UNIs |
| 59 | // won't reach the desired state |
| 60 | continue |
| 61 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 62 | if service.DhcpState != expectedDhcpState || service.EapolState != expectedEapolState { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 63 | res = false |
| 64 | log.WithFields(log.Fields{ |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 65 | "OnuSN": service.OnuSn, |
| 66 | "ServiceName": service.Name, |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 67 | "UniId": service.UniId, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 68 | "DhcpState": service.DhcpState, |
| 69 | "EapolState": service.EapolState, |
| 70 | "ExpectedDhcpState": expectedDhcpState, |
| 71 | "ExpectedEapolState": expectedEapolState, |
| 72 | }).Fatal("Not matching expected state") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 76 | if res { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 77 | // NOTE that in BBR we expect to have a single service but this is not always the case |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 78 | log.WithFields(log.Fields{ |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 79 | "ExpectedState": expectedDhcpState, |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 80 | }).Infof("%d ONUs matching expected state", len(services.Items)/4) // for now BBSim has 4 UNIs per ONU (each UNI has a single service in BBR) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | olt.conn.Close() |
| 84 | } |
| 85 | |
| 86 | func ApiConnect(ip string, port string) (bbsim.BBSimClient, *grpc.ClientConn) { |
| 87 | server := fmt.Sprintf("%s:%s", ip, port) |
| 88 | conn, err := grpc.Dial(server, grpc.WithInsecure()) |
| 89 | |
| 90 | if err != nil { |
| 91 | log.Fatalf("did not connect: %v", err) |
| 92 | return nil, conn |
| 93 | } |
| 94 | return bbsim.NewBBSimClient(conn), conn |
| 95 | } |