blob: a5f1d85dcb2e07f943e378f9ba45aa8c1fe29311 [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 {
Matteo Scandolo8a574812021-05-20 15:18:53 -070051 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 Scandolo4a036262020-08-17 15:56:13 -070056 if service.DhcpState != expectedDhcpState || service.EapolState != expectedEapolState {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070057 res = false
58 log.WithFields(log.Fields{
Matteo Scandolo4a036262020-08-17 15:56:13 -070059 "OnuSN": service.OnuSn,
60 "ServiceName": service.Name,
Matteo Scandolo8a574812021-05-20 15:18:53 -070061 "UniId": service.UniId,
Matteo Scandolo4a036262020-08-17 15:56:13 -070062 "DhcpState": service.DhcpState,
63 "EapolState": service.EapolState,
64 "ExpectedDhcpState": expectedDhcpState,
65 "ExpectedEapolState": expectedEapolState,
66 }).Fatal("Not matching expected state")
Matteo Scandolo40e067f2019-10-16 16:59:41 -070067 }
68 }
69
Shrey Baid688b4242020-07-10 20:40:10 +053070 if res {
Matteo Scandolo4a036262020-08-17 15:56:13 -070071 // 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 -070072 log.WithFields(log.Fields{
Matteo Scandolo4a036262020-08-17 15:56:13 -070073 "ExpectedState": expectedDhcpState,
Matteo Scandolo8a574812021-05-20 15:18:53 -070074 }).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 Scandolo40e067f2019-10-16 16:59:41 -070075 }
76
77 olt.conn.Close()
78}
79
80func 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}