Open Kilda Java Documentation
TopologyVerificationSteps.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.atdd.staging.steps;
17 
18 import static java.lang.String.format;
19 import static java.util.stream.Collectors.toList;
20 import static org.hamcrest.Matchers.containsInAnyOrder;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertThat;
23 import static org.junit.Assert.assertTrue;
24 
35 
36 import cucumber.api.Scenario;
37 import cucumber.api.java.Before;
38 import cucumber.api.java.en.And;
39 import cucumber.api.java.en.Given;
40 import cucumber.api.java8.En;
41 import org.springframework.beans.factory.annotation.Autowired;
42 
43 import java.util.List;
44 import java.util.Set;
45 import java.util.stream.Collectors;
46 import java.util.stream.Stream;
47 
48 public class TopologyVerificationSteps implements En {
49 
50  @Autowired
51  private TopologyEngineService topologyEngineService;
52 
53  @Autowired
54  private TopologyDefinition topologyDefinition;
55 
56  @Autowired
57  private NorthboundService northboundService;
58 
59  private List<TopologyDefinition.Switch> referenceSwitches;
60  private List<TopologyDefinition.Isl> referenceLinks;
61  private List<SwitchInfoData> actualSwitches;
62  private List<IslInfoData> actualLinks;
63 
64  private Scenario scenario;
65 
66  @Before
67  public void before(Scenario scenario) {
68  this.scenario = scenario;
69  }
70 
71  @Given("^the reference topology$")
72  public void checkTheTopology() {
73  Set<SwitchId> skippedSwitches = topologyDefinition.getSkippedSwitchIds();
74 
75  referenceSwitches = topologyDefinition.getActiveSwitches();
76  actualSwitches = northboundService.getActiveSwitches().stream()
77  .filter(sw -> !skippedSwitches.contains(sw.getSwitchId()))
78  .collect(toList());
79 
80  referenceLinks = topologyDefinition.getIslsForActiveSwitches();
81  actualLinks = northboundService.getActiveLinks().stream()
82  .filter(sw -> !skippedSwitches.contains(sw.getPath().get(0).getSwitchId()))
83  .filter(sw -> !skippedSwitches.contains(sw.getPath().get(1).getSwitchId()))
84  .collect(Collectors.toList());
85  }
86 
87 
88  @And("^all defined switches are discovered")
89  public void checkDiscoveredSwitches() {
90  assertFalse("No switches were discovered", actualSwitches.isEmpty());
91 
92  assertThat("Discovered switches don't match expected", actualSwitches, containsInAnyOrder(
93  referenceSwitches.stream().map(SwitchMatcher::new).collect(toList())));
94 
95  }
96 
97  @And("^all defined links are detected")
98  public void checkDiscoveredLinks() {
99  if (actualLinks.isEmpty() && referenceLinks.isEmpty()) {
100  scenario.write("There are no links discovered as expected");
101  return;
102  }
103 
104  assertFalse("No links were discovered", actualLinks.isEmpty());
105 
106  assertThat("Discovered links don't match expected", actualLinks, containsInAnyOrder(
107  referenceLinks.stream()
108  .flatMap(link -> {
109  //in kilda we have forward and reverse isl, that's why we have to divide into 2
110  Isl pairedLink = Isl.factory(link.getDstSwitch(), link.getDstPort(),
111  link.getSrcSwitch(), link.getSrcPort(), link.getMaxBandwidth(), link.getAswitch());
112  return Stream.of(link, pairedLink);
113  })
114  .map(IslMatcher::new)
115  .collect(toList())));
116  }
117 
118  @And("^all active switches have correct rules installed per Northbound validation")
119  public void validateSwitchRules() {
120  actualSwitches.forEach(sw -> {
121  SwitchId switchId = sw.getSwitchId();
122  RulesValidationResult validationResult = northboundService.validateSwitchRules(switchId);
123  assertTrue(format("The switch '%s' is missing rules: %s", switchId, validationResult.getMissingRules()),
124  validationResult.getMissingRules().isEmpty());
125  assertTrue(format("The switch '%s' has excess rules: %s", switchId, validationResult.getExcessRules()),
126  validationResult.getExcessRules().isEmpty());
127  });
128  }
129 }