blob: 4587e422c8aa5ff1db162f1e249f3e089d33a65c [file] [log] [blame]
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolo40e067f2019-10-16 16:59:41 -07003
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"
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070025 pb "github.com/opencord/bbsim/api/bbsim"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070026 log "github.com/sirupsen/logrus"
27 "google.golang.org/grpc"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070028)
29
30func ValidateAndClose(olt *OltMock) {
31
32 // connect to the BBSim control APIs to check that all the ONUs are in the correct state
33 client, conn := ApiConnect(olt.BBSimIp, olt.BBSimApiPort)
34 defer conn.Close()
35
36 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
37 defer cancel()
38
Nitin Subramanianb0a333a2021-07-08 15:01:41 -070039 req := pb.UNIRequest{
40 OnuSerialNumber: "",
41 UniID: "",
42 }
43
44 services, err := client.GetServices(ctx, &req)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070045
46 if err != nil {
47 log.WithFields(log.Fields{
48 "error": err,
49 }).Fatalf("Can't reach BBSim API")
50 }
51
Matteo Scandolo4a036262020-08-17 15:56:13 -070052 expectedEapolState := "eap_response_success_received"
53 expectedDhcpState := "dhcp_ack_received"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070054
55 res := true
Matteo Scandolo4a036262020-08-17 15:56:13 -070056 for _, service := range services.Items {
Matteo Scandolo8a574812021-05-20 15:18:53 -070057 if service.UniId != 0 {
58 // BBR only interacts with the first UNI, rightfully so services for different UNIs
59 // won't reach the desired state
60 continue
61 }
Matteo Scandolo4a036262020-08-17 15:56:13 -070062 if service.DhcpState != expectedDhcpState || service.EapolState != expectedEapolState {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070063 res = false
64 log.WithFields(log.Fields{
Matteo Scandolo4a036262020-08-17 15:56:13 -070065 "OnuSN": service.OnuSn,
66 "ServiceName": service.Name,
Matteo Scandolo8a574812021-05-20 15:18:53 -070067 "UniId": service.UniId,
Matteo Scandolo4a036262020-08-17 15:56:13 -070068 "DhcpState": service.DhcpState,
69 "EapolState": service.EapolState,
70 "ExpectedDhcpState": expectedDhcpState,
71 "ExpectedEapolState": expectedEapolState,
72 }).Fatal("Not matching expected state")
Matteo Scandolo40e067f2019-10-16 16:59:41 -070073 }
74 }
75
Shrey Baid688b4242020-07-10 20:40:10 +053076 if res {
Matteo Scandolo4a036262020-08-17 15:56:13 -070077 // 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 -070078 log.WithFields(log.Fields{
Matteo Scandolo4a036262020-08-17 15:56:13 -070079 "ExpectedState": expectedDhcpState,
Matteo Scandolo8a574812021-05-20 15:18:53 -070080 }).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 -070081 }
82
83 olt.conn.Close()
84}
85
86func ApiConnect(ip string, port string) (bbsim.BBSimClient, *grpc.ClientConn) {
87 server := fmt.Sprintf("%s:%s", ip, port)
88 conn, err := grpc.Dial(server, grpc.WithInsecure())
89
90 if err != nil {
91 log.Fatalf("did not connect: %v", err)
92 return nil, conn
93 }
94 return bbsim.NewBBSimClient(conn), conn
95}