16 package org.openkilda.atdd.staging.service;
18 import static java.util.Arrays.asList;
19 import static java.util.Collections.emptyList;
20 import static java.util.Collections.singletonList;
21 import static java.util.stream.Collectors.toList;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
63 import org.apache.commons.lang3.SerializationUtils;
64 import org.mockito.stubbing.Answer;
66 import java.time.LocalTime;
67 import java.util.ArrayList;
68 import java.util.Arrays;
69 import java.util.HashMap;
70 import java.util.List;
72 import java.util.stream.Stream;
80 private final Map<String, FlowPayload> flowPayloads =
new HashMap<>();
81 private final Map<String, ImmutablePair<Flow, Flow>> flows =
new HashMap<>();
82 private int meterCounter = 1;
87 this.topologyDefinition = topologyDefinition;
96 when(serviceMock.
getFlow(any()))
97 .thenAnswer(invocation -> {
98 String flowId = (String) invocation.getArguments()[0];
99 return flows.get(flowId);
102 when(serviceMock.
getPaths(any(), any()))
115 .thenAnswer(invocation -> {
118 .filter(sw -> sw.getDpId().equals(switchId))
119 .map(Switch::getOfVersion)
123 return buildFlowEntries(switchId, switchVersion);
127 .then((Answer<MetersEntriesMap>) invocation -> {
131 flows.values().forEach(flowPair -> {
132 if (flowPair.getLeft().getSourceSwitch().equals(switchId)
133 || flowPair.getRight().getSourceSwitch().equals(switchId)) {
136 singletonList(
new MeterBand(flowPair.getLeft().getBandwidth(), 0,
"", 1)),
"");
137 result.put(entry.getMeterId(), entry);
145 .then((Answer<List<SwitchEntry>>) invocation -> topologyDefinition.
getActiveSwitches().stream()
146 .map(sw ->
SwitchEntry.builder().switchId(sw.getDpId()).ofVersion(sw.getOfVersion()).
build())
158 .filter(isl -> isl.getAswitch() != null)
161 return Arrays.asList(
new ASwitchFlow(asw.getInPort(), asw.getOutPort()),
162 new ASwitchFlow(asw.getOutPort(), asw.getInPort()));
163 }).flatMap(List::stream).collect(toList());
166 .thenReturn(aswitchFlows);
174 FlowEntry flowEntry = buildFlowEntry(
"flow-0x8000000000000002",
178 .flowOutput(
"controller")
182 result.put(flowEntry.getCookie(), flowEntry);
188 .cookie(
"flow-0x8000000000000001")
190 result.put(dropFlow.getCookie(), dropFlow);
192 if (
"OF_13".equals(switchVersion)) {
194 FlowEntry flowFor13Version = buildFlowEntry(
"flow-0x8000000000000003",
198 .flowOutput(
"controller")
203 result.put(flowFor13Version.getCookie(), flowFor13Version);
209 private FlowEntry buildFlowEntry(String cookie, FlowMatchField match, FlowInstructions instructions) {
211 .instructions(instructions)
231 .flatMap(link -> Stream.of(
233 asList(
new PathNode(link.getSrcSwitch().getDpId(),
234 link.getSrcPort(), 0),
235 new PathNode(link.getDstSwitch().getDpId(),
236 link.getDstPort(), 1)),
239 asList(
new PathNode(link.getDstSwitch().getDpId(),
240 link.getDstPort(), 0),
241 new PathNode(link.getSrcSwitch().getDpId(),
242 link.getSrcPort(), 1)),
248 .thenReturn(
new ArrayList<>(flowPayloads.values()));
250 when(serviceMock.
getFlow(any()))
251 .thenAnswer(invocation -> {
252 String flowId = (String) invocation.getArguments()[0];
253 return flowPayloads.containsKey(flowId) ? SerializationUtils.clone(flowPayloads.get(flowId)) : null;
257 .thenAnswer(invocation -> {
258 String flowId = (String) invocation.getArguments()[0];
262 when(serviceMock.
addFlow(any()))
263 .thenAnswer(invocation -> {
265 result.setLastUpdated(LocalTime.now().toString());
272 .thenAnswer(invocation -> {
273 String flowId = (String) invocation.getArguments()[0];
275 result.setLastUpdated(LocalTime.now().toString());
281 .thenAnswer(invocation -> {
282 String flowId = (String) invocation.getArguments()[0];
283 flows.remove(flowId);
284 return flowPayloads.remove(flowId);
288 .thenReturn(
new RulesSyncResult(emptyList(), emptyList(), emptyList(), emptyList()));
296 private void putFlow(String flowId,
FlowPayload flowPayload) {
297 flowPayloads.put(flowId, flowPayload);
300 forwardFlow.setMeterId(meterCounter++);
302 Flow reverseFlow =
new Flow(forwardFlow);
303 reverseFlow.setSourceSwitch(forwardFlow.getDestinationSwitch());
304 reverseFlow.setSourcePort(forwardFlow.getDestinationPort());
305 reverseFlow.setSourceVlan(forwardFlow.getDestinationVlan());
306 reverseFlow.setDestinationSwitch(forwardFlow.getSourceSwitch());
307 reverseFlow.setDestinationPort(forwardFlow.getSourcePort());
308 reverseFlow.setDestinationVlan(forwardFlow.getSourceVlan());
320 .thenAnswer(invocation -> {
321 String hostName = (String) invocation.getArguments()[0];
323 when(
host.getName()).thenReturn(hostName);
328 .thenAnswer(invocation -> {
329 Exam exam = (
Exam) invocation.getArguments()[0];
331 when(report.
hasError()).thenReturn(
false);
332 when(report.
hasTraffic()).thenReturn(flows.containsKey(exam.getFlow().getId()));
MetersEntriesMap getMeters(SwitchId dpid)
List< Switch > getActiveSwitches()
FloodlightService getFloodlightStub()
default List< SwitchInfoData > getActiveSwitches()
TraffExamService getTraffExamStub()
ExamReport waitExam(Exam exam)
NorthboundService getNorthboundStub()
List< ASwitchFlow > getAllFlows()
FlowPayload getFlow(String flowId)
List< Isl > getIslsForActiveSwitches()
List< FlowPayload > getAllFlows()
List< SwitchEntry > getSwitches()
static Flow buildFlowByFlowPayload(FlowPayload flowPayload)
FlowPayload addFlow(FlowPayload payload)
Host hostByName(String name)
StubServiceFactory(TopologyDefinition topologyDefinition)
List< PathInfoData > getPaths(SwitchId srcSwitch, SwitchId dstSwitch)
RulesValidationResult validateSwitchRules(SwitchId switchId)
TopologyEngineService getTopologyEngineStub()
ImmutablePair< Flow, Flow > getFlow(String flowId)
RulesSyncResult synchronizeSwitchRules(SwitchId switchId)
default List< IslInfoData > getActiveLinks()
ASwitchService getASwitchStub()
FlowPayload updateFlow(String flowId, FlowPayload payload)
FlowPayload deleteFlow(String flowId)
FlowEntriesMap getFlows(SwitchId dpid)
FlowIdStatusPayload getFlowStatus(String flowId)