16 package org.openkilda.atdd.staging.service;
18 import static java.util.Collections.singletonList;
19 import static org.hamcrest.CoreMatchers.hasItems;
20 import static org.hamcrest.Matchers.allOf;
21 import static org.hamcrest.Matchers.equalTo;
22 import static org.hamcrest.Matchers.hasItem;
23 import static org.hamcrest.Matchers.hasProperty;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.fail;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.when;
38 import com.fasterxml.jackson.databind.MapperFeature;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
41 import org.junit.Before;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.rules.ExpectedException;
45 import org.mockito.InjectMocks;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
49 import java.io.IOException;
50 import java.util.List;
52 import java.util.stream.Collectors;
72 public void setUp() throws IOException {
73 MockitoAnnotations.initMocks(
this);
75 ObjectMapper mapper =
new ObjectMapper(
new YAMLFactory());
76 mapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
78 getClass().getResourceAsStream(
"/5-switch-test-topology.yaml"),
TopologyDefinition.class);
80 when(topologyDefinition.getActiveSwitches()).thenReturn(
topology.getActiveSwitches());
81 when(topologyDefinition.getTraffGens()).thenReturn(
topology.getTraffGens());
87 when(topologyEngineService.getPaths(
91 when(topologyEngineService.getPaths(
97 final Set<FlowPayload> flows = flowManager.allActiveSwitchesFlows();
100 assertEquals(1, flows.size());
101 assertThat(flows, hasItem(allOf(
102 hasProperty(
"source", allOf(
103 hasProperty(
"switchDpId", equalTo(
new SwitchId(1L))),
104 hasProperty(
"portId", equalTo(10)),
105 hasProperty(
"vlanId", equalTo(1)))),
106 hasProperty(
"destination", allOf(
107 hasProperty(
"switchDpId", equalTo(
new SwitchId(2L))),
108 hasProperty(
"portId", equalTo(10)),
109 hasProperty(
"vlanId", equalTo(1))))
116 when(topologyEngineService.getPaths(
120 when(topologyEngineService.getPaths(
124 when(topologyEngineService.getPaths(
128 when(topologyEngineService.getPaths(
134 final Set<FlowPayload> flows = flowManager.allActiveSwitchesFlows();
137 assertEquals(2, flows.size());
139 assertThat(flows, hasItems(
141 hasProperty(
"source", allOf(
142 hasProperty(
"switchDpId", equalTo(
new SwitchId(1L))),
143 hasProperty(
"portId", equalTo(10)),
144 hasProperty(
"vlanId", equalTo(1)))),
145 hasProperty(
"destination", allOf(
146 hasProperty(
"switchDpId", equalTo(
new SwitchId(2L))),
147 hasProperty(
"portId", equalTo(10)),
148 hasProperty(
"vlanId", equalTo(1))))
151 hasProperty(
"source", allOf(
152 hasProperty(
"switchDpId", equalTo(
new SwitchId(2L))),
153 hasProperty(
"portId", equalTo(10)),
154 hasProperty(
"vlanId", equalTo(2)))),
155 hasProperty(
"destination", allOf(
156 hasProperty(
"switchDpId", equalTo(
new SwitchId(3L))),
157 hasProperty(
"portId", equalTo(10)),
158 hasProperty(
"vlanId", equalTo(2))))
166 when(topologyEngineService.getPaths(
172 final Set<FlowPayload> flows = flowManager.allActiveSwitchesFlows();
175 assertEquals(0, flows.size());
181 when(topologyEngineService.getPaths(
185 when(topologyEngineService.getPaths(
191 final Set<FlowPayload> flows = flowManager.allActiveSwitchesFlows();
194 assertEquals(1, flows.size());
196 assertThat(flows, hasItem(allOf(
197 hasProperty(
"source", allOf(
198 hasProperty(
"portId", equalTo(10)),
199 hasProperty(
"vlanId", equalTo(1)))),
200 hasProperty(
"destination", allOf(
201 hasProperty(
"portId", equalTo(10)),
202 hasProperty(
"vlanId", equalTo(50))))
209 when(topologyEngineService.getPaths(
213 when(topologyEngineService.getPaths(
217 when(topologyEngineService.getPaths(
221 when(topologyEngineService.getPaths(
229 flowManager.allActiveSwitchesFlows();
239 .filter(traffGen -> {
240 SwitchId dpId = traffGen.getSwitchConnected().getDpId();
244 .collect(Collectors.toList());
245 when(topologyDefinition.getActiveTraffGens()).thenReturn(activeTraffGens);
248 final Set<FlowPayload> flows = flowManager.allActiveTraffgenFlows();
251 assertEquals(1, flows.size());
252 assertThat(flows, hasItem(allOf(
253 hasProperty(
"source", allOf(
254 hasProperty(
"switchDpId", equalTo(
new SwitchId(1L))),
255 hasProperty(
"portId", equalTo(10)),
256 hasProperty(
"vlanId", equalTo(1)))),
257 hasProperty(
"destination", allOf(
258 hasProperty(
"switchDpId", equalTo(
new SwitchId(2L))),
259 hasProperty(
"portId", equalTo(10)),
260 hasProperty(
"vlanId", equalTo(1))))
268 when(topologyDefinition.getActiveTraffGens()).thenReturn(activeTraffGens);
271 final Set<FlowPayload> flows = flowManager.allActiveTraffgenFlows();
274 assertEquals(3, flows.size());
275 assertThat(flows, hasItems(
277 hasProperty(
"source",
278 hasProperty(
"switchDpId", equalTo(
new SwitchId(1L)))),
279 hasProperty(
"destination",
280 hasProperty(
"switchDpId", equalTo(
new SwitchId(2L))))
283 hasProperty(
"source",
284 hasProperty(
"switchDpId", equalTo(
new SwitchId(1L)))),
285 hasProperty(
"destination",
286 hasProperty(
"switchDpId", equalTo(
new SwitchId(3L))))
289 hasProperty(
"source",
290 hasProperty(
"switchDpId", equalTo(
new SwitchId(2L)))),
291 hasProperty(
"destination",
292 hasProperty(
"switchDpId", equalTo(
new SwitchId(3L))))
void shouldDefineFlowsOver2Switches()
ExpectedException expectedException
void shouldSkipFlowsOverTheSameSwitches()
void shouldDefineFlowCrossVlan()
void shouldDefineFlowsOver2TraffGens()
void shouldDefineFlowsOver3TraffGens()
void shouldDefineFlowsOver3Switches()
void failIfNoVlanAvailable()