blob: 755fa87f40521dec74ce39c9c0987b068d5a6c93 [file] [log] [blame]
Sho SHIMIZU913969b2014-08-18 15:04:21 -07001package net.onrc.onos.core.newintent;
2
3import net.onrc.onos.api.flowmanager.FakeFlowManagerService;
4import net.onrc.onos.api.flowmanager.FlowId;
5import net.onrc.onos.api.flowmanager.FlowLink;
6import net.onrc.onos.api.flowmanager.FlowManagerService;
7import net.onrc.onos.api.flowmanager.SingleSrcTreeFlow;
8import net.onrc.onos.api.flowmanager.Tree;
9import net.onrc.onos.api.newintent.IntentId;
10import net.onrc.onos.core.matchaction.action.OutputAction;
11import net.onrc.onos.core.matchaction.match.PacketMatchBuilder;
12import net.onrc.onos.core.util.Dpid;
13import net.onrc.onos.core.util.Pair;
14import net.onrc.onos.core.util.SwitchPort;
15import org.junit.Test;
16
17import java.util.HashSet;
18import java.util.Set;
19
20import static net.onrc.onos.api.flowmanager.FlowState.COMPILED;
21import static net.onrc.onos.api.flowmanager.FlowState.INSTALLED;
22import static net.onrc.onos.api.flowmanager.FlowState.SUBMITTED;
23import static net.onrc.onos.api.flowmanager.FlowState.WITHDRAWING;
24
25/**
26 * Suites of test of {@link SingleSrcTreeFlowIntentInstaller}.
27 */
28public class SingleSrcTreeFlowIntentInstallerTest {
29 private final FlowId flowId = new FlowId(1);
30 private final SwitchPort port12 = new SwitchPort(1, (short) 1);
31 private final SwitchPort port13 = new SwitchPort(1, (short) 3);
32 private final SwitchPort port21 = new SwitchPort(2, (short) 2);
33 private final SwitchPort port31 = new SwitchPort(3, (short) 3);
34 private final SwitchPort ingress1 = new SwitchPort(1, (short) 3);
35 private final SwitchPort egress1 = new SwitchPort(2, (short) 100);
36 private final SwitchPort egress2 = new SwitchPort(3, (short) 101);
37
38 /**
39 * Tests intent installation that the state is changed
40 * to SUBMITTED, COMPILED, then INSTALLED.
41 */
42 @Test
43 public void testNormalStateTransition() {
44 FlowManagerService flowManager =
45 new FakeFlowManagerService(flowId, false, SUBMITTED, COMPILED, INSTALLED);
46 SingleSrcTreeFlowIntentInstaller sut =
47 new SingleSrcTreeFlowIntentInstaller(flowManager);
48
49 SingleSrcTreeFlow flow = createFlow();
50 SingleSrcTreeFlowIntent intent = new SingleSrcTreeFlowIntent(new IntentId(1), flow);
51
52 sut.install(intent);
53 }
54
55 /**
56 * Tests intent installation that addFlow() returns null.
57 */
58 @Test(expected = IntentInstallationException.class)
59 public void testInstallationFails() {
60 FlowManagerService flowManager =
61 new FakeFlowManagerService(flowId, true, SUBMITTED);
62 SingleSrcTreeFlowIntentInstaller sut =
63 new SingleSrcTreeFlowIntentInstaller(flowManager);
64
65 SingleSrcTreeFlow flow = createFlow();
66 SingleSrcTreeFlowIntent intent = new SingleSrcTreeFlowIntent(new IntentId(1), flow);
67
68 sut.install(intent);
69 }
70
71 /**
72 * Tests intent removal that removeFlow() returns null.
73 */
74 @Test(expected = IntentRemovalException.class)
75 public void testRemovalFails() {
76 FlowManagerService flowManager =
77 new FakeFlowManagerService(flowId, true, WITHDRAWING);
78 SingleSrcTreeFlowIntentInstaller sut =
79 new SingleSrcTreeFlowIntentInstaller(flowManager);
80
81 SingleSrcTreeFlow flow = createFlow();
82 SingleSrcTreeFlowIntent intent = new SingleSrcTreeFlowIntent(new IntentId(1), flow);
83
84 sut.remove(intent);
85 }
86
87 private SingleSrcTreeFlow createFlow() {
88 Tree tree = new Tree();
89 tree.add(new FlowLink(port12, port21));
90 tree.add(new FlowLink(port13, port31));
91
92 Set<Pair<Dpid, OutputAction>> actions = new HashSet<>();
93 actions.add(new Pair<>(egress1.getDpid(), new OutputAction(egress1.getPortNumber())));
94 actions.add(new Pair<>(egress2.getDpid(), new OutputAction(egress2.getPortNumber())));
95
96 return new SingleSrcTreeFlow(
97 flowId,
98 new PacketMatchBuilder().build(),
99 ingress1,
100 tree,
101 actions
102 );
103 }
104}