blob: e6d959488f2991cbebe78143a500d76ed72048a9 [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
38 onus, err := client.GetONUs(ctx, &bbsim.Empty{})
39
40 if err != nil {
41 log.WithFields(log.Fields{
42 "error": err,
43 }).Fatalf("Can't reach BBSim API")
44 }
45
46 expectedState := "dhcp_ack_received"
47
48 res := true
49 for _, onu := range onus.Items {
50 if onu.InternalState != expectedState {
51 res = false
52 log.WithFields(log.Fields{
53 "OnuSN": onu.SerialNumber,
54 "OnuId": onu.ID,
55 "InternalState": onu.InternalState,
56 "ExpectedSatte": expectedState,
57 }).Error("Not matching expected state")
58 }
59 }
60
Shrey Baid688b4242020-07-10 20:40:10 +053061 if res {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070062 log.WithFields(log.Fields{
63 "ExpectedState": expectedState,
64 }).Infof("%d ONUs matching expected state", len(onus.Items))
65 }
66
67 olt.conn.Close()
68}
69
70func ApiConnect(ip string, port string) (bbsim.BBSimClient, *grpc.ClientConn) {
71 server := fmt.Sprintf("%s:%s", ip, port)
72 conn, err := grpc.Dial(server, grpc.WithInsecure())
73
74 if err != nil {
75 log.Fatalf("did not connect: %v", err)
76 return nil, conn
77 }
78 return bbsim.NewBBSimClient(conn), conn
79}