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 { |
| 51 | if service.DhcpState != expectedDhcpState || service.EapolState != expectedEapolState { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 52 | res = false |
| 53 | log.WithFields(log.Fields{ |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 54 | "OnuSN": service.OnuSn, |
| 55 | "ServiceName": service.Name, |
| 56 | "DhcpState": service.DhcpState, |
| 57 | "EapolState": service.EapolState, |
| 58 | "ExpectedDhcpState": expectedDhcpState, |
| 59 | "ExpectedEapolState": expectedEapolState, |
| 60 | }).Fatal("Not matching expected state") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 64 | if res { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 65 | // 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] | 66 | log.WithFields(log.Fields{ |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 67 | "ExpectedState": expectedDhcpState, |
| 68 | }).Infof("%d ONUs matching expected state", len(services.Items)) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | olt.conn.Close() |
| 72 | } |
| 73 | |
| 74 | func ApiConnect(ip string, port string) (bbsim.BBSimClient, *grpc.ClientConn) { |
| 75 | server := fmt.Sprintf("%s:%s", ip, port) |
| 76 | conn, err := grpc.Dial(server, grpc.WithInsecure()) |
| 77 | |
| 78 | if err != nil { |
| 79 | log.Fatalf("did not connect: %v", err) |
| 80 | return nil, conn |
| 81 | } |
| 82 | return bbsim.NewBBSimClient(conn), conn |
| 83 | } |