blob: e853a29acd0bbac50895fd7b0971734160de1fd1 [file] [log] [blame]
Ray Milkeye6684082014-10-16 16:59:47 -07001package org.onlab.onos.net.intent.impl;
2
Ray Milkeye6684082014-10-16 16:59:47 -07003import org.hamcrest.Matchers;
4import org.junit.Before;
5import org.junit.Test;
Thomas Vachuskab97cf282014-10-20 23:31:12 -07006import org.onlab.onos.ApplicationId;
7import org.onlab.onos.TestApplicationId;
Ray Milkeye6684082014-10-16 16:59:47 -07008import org.onlab.onos.net.Host;
9import org.onlab.onos.net.HostId;
10import org.onlab.onos.net.flow.TrafficSelector;
11import org.onlab.onos.net.flow.TrafficTreatment;
12import org.onlab.onos.net.host.HostService;
13import org.onlab.onos.net.intent.HostToHostIntent;
14import org.onlab.onos.net.intent.Intent;
Ray Milkeye6684082014-10-16 16:59:47 -070015import org.onlab.onos.net.intent.IntentTestsMocks;
16import org.onlab.onos.net.intent.PathIntent;
17import org.onlab.packet.MacAddress;
18import org.onlab.packet.VlanId;
19
Thomas Vachuskab97cf282014-10-20 23:31:12 -070020import java.util.List;
21
22import static org.easymock.EasyMock.*;
Ray Milkeye6684082014-10-16 16:59:47 -070023import static org.hamcrest.CoreMatchers.notNullValue;
24import static org.hamcrest.MatcherAssert.assertThat;
25import static org.hamcrest.Matchers.hasSize;
26import static org.hamcrest.Matchers.is;
27import static org.onlab.onos.net.NetTestTools.hid;
28import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
29
30/**
31 * Unit tests for the HostToHost intent compiler.
32 */
33public class TestHostToHostIntentCompiler {
34 private static final String HOST_ONE_MAC = "00:00:00:00:00:01";
35 private static final String HOST_TWO_MAC = "00:00:00:00:00:02";
36 private static final String HOST_ONE_VLAN = "-1";
37 private static final String HOST_TWO_VLAN = "-1";
38 private static final String HOST_ONE = HOST_ONE_MAC + "/" + HOST_ONE_VLAN;
39 private static final String HOST_TWO = HOST_TWO_MAC + "/" + HOST_TWO_VLAN;
40
Thomas Vachuskab97cf282014-10-20 23:31:12 -070041 private static final ApplicationId APPID = new TestApplicationId("foo");
42
Ray Milkeye6684082014-10-16 16:59:47 -070043 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
44 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
45
46 private HostId hostOneId = HostId.hostId(HOST_ONE);
47 private HostId hostTwoId = HostId.hostId(HOST_TWO);
48 private HostService mockHostService;
49
50 @Before
51 public void setup() {
52 Host hostOne = createMock(Host.class);
53 expect(hostOne.mac()).andReturn(new MacAddress(HOST_ONE_MAC.getBytes())).anyTimes();
54 expect(hostOne.vlan()).andReturn(VlanId.vlanId()).anyTimes();
55 replay(hostOne);
56
57 Host hostTwo = createMock(Host.class);
58 expect(hostTwo.mac()).andReturn(new MacAddress(HOST_TWO_MAC.getBytes())).anyTimes();
59 expect(hostTwo.vlan()).andReturn(VlanId.vlanId()).anyTimes();
60 replay(hostTwo);
61
62 mockHostService = createMock(HostService.class);
63 expect(mockHostService.getHost(eq(hostOneId))).andReturn(hostOne).anyTimes();
64 expect(mockHostService.getHost(eq(hostTwoId))).andReturn(hostTwo).anyTimes();
65 replay(mockHostService);
66 }
67
68 /**
69 * Creates a HostToHost intent based on two host Ids.
70 *
71 * @param oneIdString string for host one id
72 * @param twoIdString string for host two id
73 * @return HostToHostIntent for the two hosts
74 */
75 private HostToHostIntent makeIntent(String oneIdString, String twoIdString) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070076 return new HostToHostIntent(APPID, hid(oneIdString), hid(twoIdString),
77 selector, treatment);
Ray Milkeye6684082014-10-16 16:59:47 -070078 }
79
80 /**
81 * Creates a compiler for HostToHost intents.
82 *
83 * @param hops string array describing the path hops to use when compiling
84 * @return HostToHost intent compiler
85 */
86 private HostToHostIntentCompiler makeCompiler(String[] hops) {
87 HostToHostIntentCompiler compiler =
88 new HostToHostIntentCompiler();
89 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
90 compiler.hostService = mockHostService;
Ray Milkeye6684082014-10-16 16:59:47 -070091 return compiler;
92 }
93
94
95 /**
96 * Tests a pair of hosts with 8 hops between them.
97 */
98 @Test
99 public void testSingleLongPathCompilation() {
100
101 HostToHostIntent intent = makeIntent(HOST_ONE,
102 HOST_TWO);
103 assertThat(intent, is(notNullValue()));
104
105 String[] hops = {HOST_ONE, "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", HOST_TWO};
106 HostToHostIntentCompiler compiler = makeCompiler(hops);
107 assertThat(compiler, is(notNullValue()));
108
109 List<Intent> result = compiler.compile(intent);
110 assertThat(result, is(Matchers.notNullValue()));
111 assertThat(result, hasSize(2));
112 Intent forwardResultIntent = result.get(0);
113 assertThat(forwardResultIntent instanceof PathIntent, is(true));
114 Intent reverseResultIntent = result.get(1);
115 assertThat(reverseResultIntent instanceof PathIntent, is(true));
116
117 if (forwardResultIntent instanceof PathIntent) {
118 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
119 assertThat(forwardPathIntent.path().links(), hasSize(9));
120 assertThat(forwardPathIntent.path().links(), linksHasPath(HOST_ONE, "h1"));
121 assertThat(forwardPathIntent.path().links(), linksHasPath("h1", "h2"));
122 assertThat(forwardPathIntent.path().links(), linksHasPath("h2", "h3"));
123 assertThat(forwardPathIntent.path().links(), linksHasPath("h3", "h4"));
124 assertThat(forwardPathIntent.path().links(), linksHasPath("h4", "h5"));
125 assertThat(forwardPathIntent.path().links(), linksHasPath("h5", "h6"));
126 assertThat(forwardPathIntent.path().links(), linksHasPath("h6", "h7"));
127 assertThat(forwardPathIntent.path().links(), linksHasPath("h7", "h8"));
128 assertThat(forwardPathIntent.path().links(), linksHasPath("h8", HOST_TWO));
129 }
130
131 if (reverseResultIntent instanceof PathIntent) {
132 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
133 assertThat(reversePathIntent.path().links(), hasSize(9));
134 assertThat(reversePathIntent.path().links(), linksHasPath("h1", HOST_ONE));
135 assertThat(reversePathIntent.path().links(), linksHasPath("h2", "h1"));
136 assertThat(reversePathIntent.path().links(), linksHasPath("h3", "h2"));
137 assertThat(reversePathIntent.path().links(), linksHasPath("h4", "h3"));
138 assertThat(reversePathIntent.path().links(), linksHasPath("h5", "h4"));
139 assertThat(reversePathIntent.path().links(), linksHasPath("h6", "h5"));
140 assertThat(reversePathIntent.path().links(), linksHasPath("h7", "h6"));
141 assertThat(reversePathIntent.path().links(), linksHasPath("h8", "h7"));
142 assertThat(reversePathIntent.path().links(), linksHasPath(HOST_TWO, "h8"));
143 }
144 }
145}