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 | "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" |
| 25 | log "github.com/sirupsen/logrus" |
| 26 | "google.golang.org/grpc" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | func ValidateAndClose(olt *OltMock) { |
| 30 | |
| 31 | // connect to the BBSim control APIs to check that all the ONUs are in the correct state |
| 32 | client, conn := ApiConnect(olt.BBSimIp, olt.BBSimApiPort) |
| 33 | defer conn.Close() |
| 34 | |
| 35 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 36 | defer cancel() |
| 37 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 38 | services, err := client.GetServices(ctx, &bbsim.Empty{}) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 39 | |
| 40 | if err != nil { |
| 41 | log.WithFields(log.Fields{ |
| 42 | "error": err, |
| 43 | }).Fatalf("Can't reach BBSim API") |
| 44 | } |
| 45 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 46 | expectedEapolState := "eap_response_success_received" |
| 47 | expectedDhcpState := "dhcp_ack_received" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 48 | |
| 49 | res := true |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 50 | for _, service := range services.Items { |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 51 | if service.UniId != 0 { |
| 52 | // BBR only interacts with the first UNI, rightfully so services for different UNIs |
| 53 | // won't reach the desired state |
| 54 | continue |
| 55 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 56 | if service.DhcpState != expectedDhcpState || service.EapolState != expectedEapolState { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 57 | res = false |
| 58 | log.WithFields(log.Fields{ |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 59 | "OnuSN": service.OnuSn, |
| 60 | "ServiceName": service.Name, |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 61 | "UniId": service.UniId, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 62 | "DhcpState": service.DhcpState, |
| 63 | "EapolState": service.EapolState, |
| 64 | "ExpectedDhcpState": expectedDhcpState, |
| 65 | "ExpectedEapolState": expectedEapolState, |
| 66 | }).Fatal("Not matching expected state") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 70 | if res { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 71 | // 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] | 72 | log.WithFields(log.Fields{ |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 73 | "ExpectedState": expectedDhcpState, |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 74 | }).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] | 75 | } |
| 76 | |
| 77 | olt.conn.Close() |
| 78 | } |
| 79 | |
| 80 | func ApiConnect(ip string, port string) (bbsim.BBSimClient, *grpc.ClientConn) { |
| 81 | server := fmt.Sprintf("%s:%s", ip, port) |
| 82 | conn, err := grpc.Dial(server, grpc.WithInsecure()) |
| 83 | |
| 84 | if err != nil { |
| 85 | log.Fatalf("did not connect: %v", err) |
| 86 | return nil, conn |
| 87 | } |
| 88 | return bbsim.NewBBSimClient(conn), conn |
| 89 | } |