blob: 58e9bfa3833363927306e5ee76e85191073f3441 [file] [log] [blame]
Ray Milkeye8274232014-05-27 16:03:12 -07001package net.onrc.onos.api.rest;
2
3
Ray Milkey2fa6ca42014-06-13 15:38:20 -07004import com.google.common.net.InetAddresses;
Ray Milkeye8274232014-05-27 16:03:12 -07005import net.onrc.onos.core.intent.IntentOperation;
6import net.onrc.onos.core.intent.IntentOperationList;
7import net.onrc.onos.core.intent.ShortestPathIntent;
Ray Milkeye8274232014-05-27 16:03:12 -07008import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
Ray Milkeye8274232014-05-27 16:03:12 -07009import org.junit.After;
10import org.junit.Assert;
11import org.junit.Before;
12import org.junit.Test;
13import org.junit.runner.RunWith;
14import org.powermock.core.classloader.annotations.PrepareForTest;
15import org.powermock.modules.junit4.PowerMockRunner;
16import org.restlet.data.Status;
17import org.restlet.resource.ClientResource;
18import org.restlet.resource.ResourceException;
19
20import java.util.Collection;
Ray Milkeye8274232014-05-27 16:03:12 -070021import java.util.Map;
22
Ray Milkey531bb232014-06-03 09:08:15 -070023import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf;
Ray Milkeye8274232014-05-27 16:03:12 -070024import static org.hamcrest.MatcherAssert.assertThat;
25import static org.hamcrest.Matchers.equalTo;
26import static org.hamcrest.Matchers.hasKey;
27import static org.hamcrest.Matchers.hasSize;
28import static org.hamcrest.Matchers.is;
29import static org.hamcrest.Matchers.notNullValue;
30
31/**
Ray Milkey531bb232014-06-03 09:08:15 -070032 * Unit tests to test the Intents GET REST APIs.
Ray Milkeye8274232014-05-27 16:03:12 -070033 */
34@RunWith(PowerMockRunner.class)
35@PrepareForTest(PathCalcRuntimeModule.class)
Ray Milkey531bb232014-06-03 09:08:15 -070036public class TestRestIntentHighGet extends TestRestIntent {
Ray Milkeye8274232014-05-27 16:03:12 -070037 private static final Long LOCAL_PORT = 0xFFFEL;
Ray Milkey531bb232014-06-03 09:08:15 -070038 private static final String BAD_SWITCH_INTENT_NAME = "No Such Switch Intent";
Ray Milkey2fa6ca42014-06-13 15:38:20 -070039 private static final String IP_ADDRESS_1 = "127.0.0.1";
40 private static final String IP_ADDRESS_2 = "127.0.0.2";
41 private static final String IP_ADDRESS_3 = "127.0.0.3";
Ray Milkeye8274232014-05-27 16:03:12 -070042
43 /**
44 * Create the web server, PathCalcRuntime, and mocks required for
45 * all of the tests.
Ray Milkeye8274232014-05-27 16:03:12 -070046 */
47 @Before
Ray Milkey531bb232014-06-03 09:08:15 -070048 public void beforeTest() {
49 setRestPort(generateRandomPort());
50 setUp();
Ray Milkeye8274232014-05-27 16:03:12 -070051 }
52
53
54 /**
55 * Remove anything that will interfere with the next test running correctly.
56 * Shuts down the test REST web server and removes the mocks.
Ray Milkeye8274232014-05-27 16:03:12 -070057 */
58 @After
Ray Milkey531bb232014-06-03 09:08:15 -070059 public void afterTest() {
60 tearDown();
Ray Milkeye8274232014-05-27 16:03:12 -070061 }
62
63
64 /**
65 * Make a set of Intents that can be used as test data.
66 */
67 private void makeDefaultIntents() {
Ray Milkey2fa6ca42014-06-13 15:38:20 -070068 final int ipAddress1AsInt = InetAddresses.coerceToInteger(
69 InetAddresses.forString(IP_ADDRESS_1));
70 final int ipAddress2AsInt = InetAddresses.coerceToInteger(
71 InetAddresses.forString(IP_ADDRESS_2));
72 final int ipAddress3AsInt = InetAddresses.coerceToInteger(
73 InetAddresses.forString(IP_ADDRESS_3));
74
Ray Milkeye8274232014-05-27 16:03:12 -070075 // create shortest path intents
76 final IntentOperationList opList = new IntentOperationList();
77 opList.add(IntentOperation.Operator.ADD,
78 new ShortestPathIntent(BAD_SWITCH_INTENT_NAME, 111L, 12L,
79 LOCAL_PORT, 2L, 21L, LOCAL_PORT));
80 opList.add(IntentOperation.Operator.ADD,
Ray Milkey2fa6ca42014-06-13 15:38:20 -070081 new ShortestPathIntent("1:2", 1L, 14L, LOCAL_PORT, ipAddress1AsInt,
82 4L, 41L, LOCAL_PORT, ipAddress2AsInt));
Ray Milkeye8274232014-05-27 16:03:12 -070083 opList.add(IntentOperation.Operator.ADD,
Ray Milkey2fa6ca42014-06-13 15:38:20 -070084 new ShortestPathIntent("1:3", 2L, 23L, LOCAL_PORT, ipAddress2AsInt,
85 3L, 32L, LOCAL_PORT, ipAddress3AsInt));
Ray Milkeye8274232014-05-27 16:03:12 -070086
87 // compile high-level intent operations into low-level intent
88 // operations (calculate paths)
89
90 final IntentOperationList pathIntentOpList =
Ray Milkey531bb232014-06-03 09:08:15 -070091 getRuntime().executeIntentOperations(opList);
Ray Milkeye8274232014-05-27 16:03:12 -070092 assertThat(pathIntentOpList, notNullValue());
93
94 }
95
96 /**
Ray Milkeye8274232014-05-27 16:03:12 -070097 * Test that the GET of all Intents REST call returns the proper result.
98 * The call to get all Intents should return 3 items, an HTTP status of OK,
99 * and the proper Intent data.
Ray Milkeye8274232014-05-27 16:03:12 -0700100 */
101 @Test
Ray Milkey531bb232014-06-03 09:08:15 -0700102 public void testFetchOfAllIntents() {
Ray Milkeye8274232014-05-27 16:03:12 -0700103
104 makeDefaultIntents();
105
Ray Milkey531bb232014-06-03 09:08:15 -0700106 final ClientResource client = new ClientResource(getHighRestIntentUrl());
Ray Milkeye8274232014-05-27 16:03:12 -0700107 final Collection<Map<String, String>> intents = getIntentsCollection(client);
108
109 // HTTP status should be OK
Ray Milkey531bb232014-06-03 09:08:15 -0700110 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
Ray Milkeye8274232014-05-27 16:03:12 -0700111
112 // 3 intents should have been fetched
113 assertThat(intents, hasSize(3));
114
115 // check that the Intent with id "3" is present, and has the right data
116 final Map<String, String> mapForIntent3 = findIntentWithId(intents, "1:3");
117 // Intent 3 must exist
118 assertThat(mapForIntent3, notNullValue());
119 // Data must be correct
120 assertThat(mapForIntent3, hasKey("state"));
121 final String state = mapForIntent3.get("state");
122 assertThat(state, is(equalTo("INST_REQ")));
Ray Milkey531bb232014-06-03 09:08:15 -0700123
124 // check that the Intent with the bad switch ID is present, and has the right data
125 final Map<String, String> mapForIntentBadIntent =
126 findIntentWithId(intents, BAD_SWITCH_INTENT_NAME);
127 // Intent must exist
128 assertThat(mapForIntentBadIntent, notNullValue());
129 // Data must be correct
130 assertThat(mapForIntentBadIntent, hasKey("state"));
131 final String badIntentState = mapForIntentBadIntent.get("state");
132 assertThat(badIntentState, is(equalTo("INST_NACK")));
133
Ray Milkeye8274232014-05-27 16:03:12 -0700134 }
135
136 /**
137 * Test that the GET of a single Intent REST call returns the proper result
138 * when given a bad Intent id. The call to get the Intent should return a
139 * status of NOT_FOUND.
Ray Milkeye8274232014-05-27 16:03:12 -0700140 */
141 @Test
Ray Milkey531bb232014-06-03 09:08:15 -0700142 public void testFetchOfBadIntent() {
Ray Milkeye8274232014-05-27 16:03:12 -0700143
144 makeDefaultIntents();
145
Ray Milkey531bb232014-06-03 09:08:15 -0700146 final ClientResource client = new ClientResource(getHighRestIntentUrl() + "/2334");
Ray Milkeye8274232014-05-27 16:03:12 -0700147
148 try {
149 getIntent(client);
150 // The get operation should have thrown a ResourceException.
151 // Fail because the Exception was not seen.
152 Assert.fail("Invalid intent fetch did not cause an exception");
153 } catch (ResourceException resourceError) {
154 // The HTTP status should be NOT FOUND
Ray Milkey531bb232014-06-03 09:08:15 -0700155 assertThat(client, hasStatusOf(Status.CLIENT_ERROR_NOT_FOUND));
Ray Milkeye8274232014-05-27 16:03:12 -0700156 }
157 }
158
159 /**
160 * Test that the GET of a single Intent REST call returns the proper result
161 * for an existing Intent. The call to get the Intent should return a
162 * status of OK, and the data for the Intent should be correct.
Ray Milkeye8274232014-05-27 16:03:12 -0700163 */
164 @Test
Ray Milkey531bb232014-06-03 09:08:15 -0700165 public void testFetchOfGoodIntent() {
Ray Milkeye8274232014-05-27 16:03:12 -0700166
167 makeDefaultIntents();
168
Ray Milkey531bb232014-06-03 09:08:15 -0700169 final ClientResource client = new ClientResource(getHighRestIntentUrl() + "/2");
Ray Milkeye8274232014-05-27 16:03:12 -0700170 final Map<String, String> intent;
171 intent = getIntent(client);
172
173 // HTTP status should be OK
Ray Milkey531bb232014-06-03 09:08:15 -0700174 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
Ray Milkeye8274232014-05-27 16:03:12 -0700175
176 // Intent data should be correct
177 assertThat(intent, is(notNullValue()));
178 assertThat(intent, hasKey("id"));
179 assertThat(intent.get("id"), is(equalTo("1:2")));
180 assertThat(intent, hasKey("state"));
181 assertThat(intent.get("state"), is(equalTo("INST_REQ")));
Ray Milkey2fa6ca42014-06-13 15:38:20 -0700182
183 assertThat(intent, hasKey("srcSwitchDpid"));
184 assertThat(intent.get("srcSwitchDpid"), is(equalTo("00:00:00:00:00:00:00:01")));
185 assertThat(intent, hasKey("srcPortNumber"));
186 assertThat(intent.get("srcPortNumber"), is(equalTo("14")));
187 assertThat(intent, hasKey("srcMac"));
Pavlin Radoslavovdc59c152014-06-17 17:04:41 -0700188 assertThat(intent.get("srcMac"), is(equalTo("00:00:00:00:ff:fe")));
Ray Milkey2fa6ca42014-06-13 15:38:20 -0700189 assertThat(intent, hasKey("srcMac"));
190 assertThat(intent.get("srcIp"), is(equalTo(IP_ADDRESS_1)));
191
192 assertThat(intent, hasKey("dstSwitchDpid"));
193 assertThat(intent.get("dstSwitchDpid"), is(equalTo("00:00:00:00:00:00:00:04")));
194 assertThat(intent, hasKey("dstPortNumber"));
195 assertThat(intent.get("dstPortNumber"), is(equalTo("41")));
196 assertThat(intent, hasKey("dstMac"));
Pavlin Radoslavovdc59c152014-06-17 17:04:41 -0700197 assertThat(intent.get("dstMac"), is(equalTo("00:00:00:00:ff:fe")));
Ray Milkey2fa6ca42014-06-13 15:38:20 -0700198 assertThat(intent, hasKey("dstMac"));
199 assertThat(intent.get("dstIp"), is(equalTo(IP_ADDRESS_2)));
Ray Milkeye8274232014-05-27 16:03:12 -0700200 }
201}