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" |
| 22 | "github.com/opencord/bbsim/api/bbsim" |
| 23 | log "github.com/sirupsen/logrus" |
| 24 | "google.golang.org/grpc" |
| 25 | "time" |
| 26 | ) |
| 27 | |
| 28 | func ValidateAndClose(olt *OltMock) { |
| 29 | |
| 30 | // connect to the BBSim control APIs to check that all the ONUs are in the correct state |
| 31 | client, conn := ApiConnect(olt.BBSimIp, olt.BBSimApiPort) |
| 32 | defer conn.Close() |
| 33 | |
| 34 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 35 | defer cancel() |
| 36 | |
| 37 | onus, err := client.GetONUs(ctx, &bbsim.Empty{}) |
| 38 | |
| 39 | if err != nil { |
| 40 | log.WithFields(log.Fields{ |
| 41 | "error": err, |
| 42 | }).Fatalf("Can't reach BBSim API") |
| 43 | } |
| 44 | |
| 45 | expectedState := "dhcp_ack_received" |
| 46 | |
| 47 | res := true |
| 48 | for _, onu := range onus.Items { |
| 49 | if onu.InternalState != expectedState { |
| 50 | res = false |
| 51 | log.WithFields(log.Fields{ |
| 52 | "OnuSN": onu.SerialNumber, |
| 53 | "OnuId": onu.ID, |
| 54 | "InternalState": onu.InternalState, |
| 55 | "ExpectedSatte": expectedState, |
| 56 | }).Error("Not matching expected state") |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if res == true { |
| 61 | log.WithFields(log.Fields{ |
| 62 | "ExpectedState": expectedState, |
| 63 | }).Infof("%d ONUs matching expected state", len(onus.Items)) |
| 64 | } |
| 65 | |
| 66 | olt.conn.Close() |
| 67 | } |
| 68 | |
| 69 | func ApiConnect(ip string, port string) (bbsim.BBSimClient, *grpc.ClientConn) { |
| 70 | server := fmt.Sprintf("%s:%s", ip, port) |
| 71 | conn, err := grpc.Dial(server, grpc.WithInsecure()) |
| 72 | |
| 73 | if err != nil { |
| 74 | log.Fatalf("did not connect: %v", err) |
| 75 | return nil, conn |
| 76 | } |
| 77 | return bbsim.NewBBSimClient(conn), conn |
| 78 | } |