blob: 29fcba4d34e90474a4fa213a2e315d1c398921c5 [file] [log] [blame]
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001/*
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
17package devices
18
19import (
20 "context"
21 "fmt"
Shrey Baid688b4242020-07-10 20:40:10 +053022 "time"
23
Matteo Scandolo40e067f2019-10-16 16:59:41 -070024 "github.com/opencord/bbsim/api/bbsim"
25 log "github.com/sirupsen/logrus"
26 "google.golang.org/grpc"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070027)
28
29func 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 Scandolo4a036262020-08-17 15:56:13 -070038 services, err := client.GetServices(ctx, &bbsim.Empty{})
Matteo Scandolo40e067f2019-10-16 16:59:41 -070039
40 if err != nil {
41 log.WithFields(log.Fields{
42 "error": err,
43 }).Fatalf("Can't reach BBSim API")
44 }
45
Matteo Scandolo4a036262020-08-17 15:56:13 -070046 expectedEapolState := "eap_response_success_received"
47 expectedDhcpState := "dhcp_ack_received"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070048
49 res := true
Matteo Scandolo4a036262020-08-17 15:56:13 -070050 for _, service := range services.Items {
51 if service.DhcpState != expectedDhcpState || service.EapolState != expectedEapolState {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070052 res = false
53 log.WithFields(log.Fields{
Matteo Scandolo4a036262020-08-17 15:56:13 -070054 "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 Scandolo40e067f2019-10-16 16:59:41 -070061 }
62 }
63
Shrey Baid688b4242020-07-10 20:40:10 +053064 if res {
Matteo Scandolo4a036262020-08-17 15:56:13 -070065 // NOTE that in BBR we expect to have a single service but this is not always the case
Matteo Scandolo40e067f2019-10-16 16:59:41 -070066 log.WithFields(log.Fields{
Matteo Scandolo4a036262020-08-17 15:56:13 -070067 "ExpectedState": expectedDhcpState,
68 }).Infof("%d ONUs matching expected state", len(services.Items))
Matteo Scandolo40e067f2019-10-16 16:59:41 -070069 }
70
71 olt.conn.Close()
72}
73
74func 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}