Open Kilda Java Documentation
FlowManagerTest.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.service;
17 
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;
29 
37 
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;
48 
49 import java.io.IOException;
50 import java.util.List;
51 import java.util.Set;
52 import java.util.stream.Collectors;
53 
54 public class FlowManagerTest {
55 
56  @Mock
57  private NorthboundService northboundService;
58 
59  @Mock
60  private TopologyEngineService topologyEngineService;
61 
62  @Mock
63  private TopologyDefinition topologyDefinition;
64 
65  @InjectMocks
66  private FlowManagerImpl flowManager;
67 
68  @Rule
69  public ExpectedException expectedException = ExpectedException.none();
70 
71  @Before
72  public void setUp() throws IOException {
73  MockitoAnnotations.initMocks(this);
74 
75  ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
76  mapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
77  TopologyDefinition topology = mapper.readValue(
78  getClass().getResourceAsStream("/5-switch-test-topology.yaml"), TopologyDefinition.class);
79 
80  when(topologyDefinition.getActiveSwitches()).thenReturn(topology.getActiveSwitches());
81  when(topologyDefinition.getTraffGens()).thenReturn(topology.getTraffGens());
82  }
83 
84  @Test
86  // given
87  when(topologyEngineService.getPaths(
88  eq(new SwitchId(1L)),
89  eq(new SwitchId(2L))))
90  .thenReturn(singletonList(new PathInfoData()));
91  when(topologyEngineService.getPaths(
92  eq(new SwitchId(2L)),
93  eq(new SwitchId(1L))))
94  .thenReturn(singletonList(new PathInfoData()));
95 
96  // when
97  final Set<FlowPayload> flows = flowManager.allActiveSwitchesFlows();
98 
99  // then
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))))
110  )));
111  }
112 
113  @Test
115  // given
116  when(topologyEngineService.getPaths(
117  eq(new SwitchId(1L)),
118  eq(new SwitchId(2L))))
119  .thenReturn(singletonList(new PathInfoData()));
120  when(topologyEngineService.getPaths(
121  eq(new SwitchId(2L)),
122  eq(new SwitchId(1L))))
123  .thenReturn(singletonList(new PathInfoData()));
124  when(topologyEngineService.getPaths(
125  eq(new SwitchId(2L)),
126  eq(new SwitchId(3L))))
127  .thenReturn(singletonList(new PathInfoData()));
128  when(topologyEngineService.getPaths(
129  eq(new SwitchId(3L)),
130  eq(new SwitchId(2L))))
131  .thenReturn(singletonList(new PathInfoData()));
132 
133  // when
134  final Set<FlowPayload> flows = flowManager.allActiveSwitchesFlows();
135 
136  // then
137  assertEquals(2, flows.size());
138 
139  assertThat(flows, hasItems(
140  allOf(
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))))
149  ),
150  allOf(
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))))
159  )
160  ));
161  }
162 
163  @Test
165  // given
166  when(topologyEngineService.getPaths(
167  eq(new SwitchId(1L)),
168  eq(new SwitchId(1L))))
169  .thenReturn(singletonList(new PathInfoData()));
170 
171  // when
172  final Set<FlowPayload> flows = flowManager.allActiveSwitchesFlows();
173 
174  // then
175  assertEquals(0, flows.size());
176  }
177 
178  @Test
180  // given
181  when(topologyEngineService.getPaths(
182  eq(new SwitchId(1L)),
183  eq(new SwitchId(4L))))
184  .thenReturn(singletonList(new PathInfoData()));
185  when(topologyEngineService.getPaths(
186  eq(new SwitchId(4L)),
187  eq(new SwitchId(1L))))
188  .thenReturn(singletonList(new PathInfoData()));
189 
190  // when
191  final Set<FlowPayload> flows = flowManager.allActiveSwitchesFlows();
192 
193  // then
194  assertEquals(1, flows.size());
195 
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))))
203  )));
204  }
205 
206  @Test
207  public void failIfNoVlanAvailable() {
208  // given
209  when(topologyEngineService.getPaths(
210  eq(new SwitchId(4L)),
211  eq(new SwitchId(2L))))
212  .thenReturn(singletonList(new PathInfoData()));
213  when(topologyEngineService.getPaths(
214  eq(new SwitchId(4L)),
215  eq(new SwitchId(1L))))
216  .thenReturn(singletonList(new PathInfoData()));
217  when(topologyEngineService.getPaths(
218  eq(new SwitchId(2L)),
219  eq(new SwitchId(4L))))
220  .thenReturn(singletonList(new PathInfoData()));
221  when(topologyEngineService.getPaths(
222  eq(new SwitchId(1L)),
223  eq(new SwitchId(4L))))
224  .thenReturn(singletonList(new PathInfoData()));
225 
226  expectedException.expect(IllegalStateException.class);
227 
228  // when
229  flowManager.allActiveSwitchesFlows();
230 
231  // then
232  fail();
233  }
234 
235  @Test
237  // given
238  final List<TopologyDefinition.TraffGen> activeTraffGens = topologyDefinition.getTraffGens().stream()
239  .filter(traffGen -> {
240  SwitchId dpId = traffGen.getSwitchConnected().getDpId();
241  return dpId.equals(new SwitchId(1L))
242  || dpId.equals(new SwitchId(2L));
243  })
244  .collect(Collectors.toList());
245  when(topologyDefinition.getActiveTraffGens()).thenReturn(activeTraffGens);
246 
247  // when
248  final Set<FlowPayload> flows = flowManager.allActiveTraffgenFlows();
249 
250  // then
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))))
261  )));
262  }
263 
264  @Test
266  // given
267  final List<TopologyDefinition.TraffGen> activeTraffGens = topologyDefinition.getTraffGens();
268  when(topologyDefinition.getActiveTraffGens()).thenReturn(activeTraffGens);
269 
270  // when
271  final Set<FlowPayload> flows = flowManager.allActiveTraffgenFlows();
272 
273  // then
274  assertEquals(3, flows.size());
275  assertThat(flows, hasItems(
276  allOf(
277  hasProperty("source",
278  hasProperty("switchDpId", equalTo(new SwitchId(1L)))),
279  hasProperty("destination",
280  hasProperty("switchDpId", equalTo(new SwitchId(2L))))
281  ),
282  allOf(
283  hasProperty("source",
284  hasProperty("switchDpId", equalTo(new SwitchId(1L)))),
285  hasProperty("destination",
286  hasProperty("switchDpId", equalTo(new SwitchId(3L))))
287  ),
288  allOf(
289  hasProperty("source",
290  hasProperty("switchDpId", equalTo(new SwitchId(2L)))),
291  hasProperty("destination",
292  hasProperty("switchDpId", equalTo(new SwitchId(3L))))
293  )
294  ));
295  }
296 }