Open Kilda Java Documentation
DiscoveryMechanismSteps.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.util.stream.Collectors.toList;
19 import static org.hamcrest.Matchers.containsInAnyOrder;
20 import static org.junit.Assert.assertThat;
21 import static org.junit.Assert.assertTrue;
22 
30 
31 import cucumber.api.Scenario;
32 import cucumber.api.java.Before;
33 import cucumber.api.java.en.Then;
34 import cucumber.api.java8.En;
35 import org.springframework.beans.factory.annotation.Autowired;
36 
37 import java.util.List;
38 import java.util.Set;
39 
40 public class DiscoveryMechanismSteps implements En {
41 
42  @Autowired
43  private FloodlightService floodlightService;
44 
45  @Autowired
46  private TopologyDefinition topologyDefinition;
47 
48  private Scenario scenario;
49 
50  @Before
51  public void before(Scenario scenario) {
52  this.scenario = scenario;
53  }
54 
55  @Then("^floodlight should not find redundant switches")
56  public void checkFloodlightSwitches() {
57  List<TopologyDefinition.Switch> expectedSwitches = topologyDefinition.getActiveSwitches();
58  List<SwitchEntry> floodlightSwitches = fetchFloodlightSwitches();
59 
60  assertThat("Discovered switches don't match expected", floodlightSwitches, containsInAnyOrder(
61  expectedSwitches.stream().map(SwitchEntryMatcher::new).collect(toList())));
62  }
63 
64  @Then("^default rules for switches are installed")
65  public void checkDefaultRules() {
66  List<SwitchEntry> floodlightSwitches = fetchFloodlightSwitches();
67 
68  List<SwitchEntry> switchesWithInvalidFlows = floodlightSwitches.stream()
69  .filter(sw -> {
70  FlowEntriesMap flows = floodlightService.getFlows(sw.getSwitchId());
71  return !DefaultFlowsChecker.validateDefaultRules(sw, flows, scenario);
72  })
73  .collect(toList());
74  assertTrue("There were found switches with incorrect default flows",
75  switchesWithInvalidFlows.isEmpty());
76  }
77 
78  private List<SwitchEntry> fetchFloodlightSwitches() {
79  Set<SwitchId> skippedSwitches = topologyDefinition.getSkippedSwitchIds();
80 
81  return floodlightService.getSwitches().stream()
82  .filter(sw -> !skippedSwitches.contains(sw.getSwitchId()))
83  .collect(toList());
84  }
85 }
static boolean validateDefaultRules(SwitchEntry sw, FlowEntriesMap map, Scenario scenario)