blob: d87325a3a5dc5040712fc10f69c02347fb454f3c [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.Assert;
Ray Milkeye8274232014-05-27 16:03:12 -070010import org.junit.Test;
11import org.junit.runner.RunWith;
12import org.powermock.core.classloader.annotations.PrepareForTest;
13import org.powermock.modules.junit4.PowerMockRunner;
14import org.restlet.data.Status;
15import org.restlet.resource.ClientResource;
16import org.restlet.resource.ResourceException;
17
18import java.util.Collection;
Ray Milkeye8274232014-05-27 16:03:12 -070019import java.util.Map;
20
Ray Milkey531bb232014-06-03 09:08:15 -070021import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf;
Ray Milkeye8274232014-05-27 16:03:12 -070022import static org.hamcrest.MatcherAssert.assertThat;
23import static org.hamcrest.Matchers.equalTo;
24import static org.hamcrest.Matchers.hasKey;
25import static org.hamcrest.Matchers.hasSize;
26import static org.hamcrest.Matchers.is;
27import static org.hamcrest.Matchers.notNullValue;
28
29/**
Ray Milkey531bb232014-06-03 09:08:15 -070030 * Unit tests to test the Intents GET REST APIs.
Ray Milkeye8274232014-05-27 16:03:12 -070031 */
32@RunWith(PowerMockRunner.class)
33@PrepareForTest(PathCalcRuntimeModule.class)
Ray Milkey531bb232014-06-03 09:08:15 -070034public class TestRestIntentHighGet extends TestRestIntent {
Ray Milkeye8274232014-05-27 16:03:12 -070035 private static final Long LOCAL_PORT = 0xFFFEL;
Ray Milkey531bb232014-06-03 09:08:15 -070036 private static final String BAD_SWITCH_INTENT_NAME = "No Such Switch Intent";
Ray Milkey2fa6ca42014-06-13 15:38:20 -070037 private static final String IP_ADDRESS_1 = "127.0.0.1";
38 private static final String IP_ADDRESS_2 = "127.0.0.2";
39 private static final String IP_ADDRESS_3 = "127.0.0.3";
Ray Milkeye8274232014-05-27 16:03:12 -070040
41 /**
Ray Milkeye8274232014-05-27 16:03:12 -070042 * Make a set of Intents that can be used as test data.
43 */
44 private void makeDefaultIntents() {
Ray Milkey2fa6ca42014-06-13 15:38:20 -070045 final int ipAddress1AsInt = InetAddresses.coerceToInteger(
46 InetAddresses.forString(IP_ADDRESS_1));
47 final int ipAddress2AsInt = InetAddresses.coerceToInteger(
48 InetAddresses.forString(IP_ADDRESS_2));
49 final int ipAddress3AsInt = InetAddresses.coerceToInteger(
50 InetAddresses.forString(IP_ADDRESS_3));
51
Ray Milkeye8274232014-05-27 16:03:12 -070052 // create shortest path intents
53 final IntentOperationList opList = new IntentOperationList();
54 opList.add(IntentOperation.Operator.ADD,
55 new ShortestPathIntent(BAD_SWITCH_INTENT_NAME, 111L, 12L,
56 LOCAL_PORT, 2L, 21L, LOCAL_PORT));
57 opList.add(IntentOperation.Operator.ADD,
Ray Milkey2fa6ca42014-06-13 15:38:20 -070058 new ShortestPathIntent("1:2", 1L, 14L, LOCAL_PORT, ipAddress1AsInt,
59 4L, 41L, LOCAL_PORT, ipAddress2AsInt));
Ray Milkeye8274232014-05-27 16:03:12 -070060 opList.add(IntentOperation.Operator.ADD,
Ray Milkey2fa6ca42014-06-13 15:38:20 -070061 new ShortestPathIntent("1:3", 2L, 23L, LOCAL_PORT, ipAddress2AsInt,
62 3L, 32L, LOCAL_PORT, ipAddress3AsInt));
Ray Milkeye8274232014-05-27 16:03:12 -070063
64 // compile high-level intent operations into low-level intent
65 // operations (calculate paths)
66
67 final IntentOperationList pathIntentOpList =
Ray Milkey531bb232014-06-03 09:08:15 -070068 getRuntime().executeIntentOperations(opList);
Ray Milkeye8274232014-05-27 16:03:12 -070069 assertThat(pathIntentOpList, notNullValue());
70
71 }
72
73 /**
Ray Milkeye8274232014-05-27 16:03:12 -070074 * Test that the GET of all Intents REST call returns the proper result.
75 * The call to get all Intents should return 3 items, an HTTP status of OK,
76 * and the proper Intent data.
Ray Milkeye8274232014-05-27 16:03:12 -070077 */
78 @Test
Ray Milkey531bb232014-06-03 09:08:15 -070079 public void testFetchOfAllIntents() {
Ray Milkeye8274232014-05-27 16:03:12 -070080
81 makeDefaultIntents();
82
Ray Milkey531bb232014-06-03 09:08:15 -070083 final ClientResource client = new ClientResource(getHighRestIntentUrl());
Ray Milkeye8274232014-05-27 16:03:12 -070084 final Collection<Map<String, String>> intents = getIntentsCollection(client);
85
86 // HTTP status should be OK
Ray Milkey531bb232014-06-03 09:08:15 -070087 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
Ray Milkeye8274232014-05-27 16:03:12 -070088
89 // 3 intents should have been fetched
90 assertThat(intents, hasSize(3));
91
92 // check that the Intent with id "3" is present, and has the right data
93 final Map<String, String> mapForIntent3 = findIntentWithId(intents, "1:3");
94 // Intent 3 must exist
95 assertThat(mapForIntent3, notNullValue());
96 // Data must be correct
97 assertThat(mapForIntent3, hasKey("state"));
98 final String state = mapForIntent3.get("state");
99 assertThat(state, is(equalTo("INST_REQ")));
Ray Milkey531bb232014-06-03 09:08:15 -0700100
101 // check that the Intent with the bad switch ID is present, and has the right data
102 final Map<String, String> mapForIntentBadIntent =
103 findIntentWithId(intents, BAD_SWITCH_INTENT_NAME);
104 // Intent must exist
105 assertThat(mapForIntentBadIntent, notNullValue());
106 // Data must be correct
107 assertThat(mapForIntentBadIntent, hasKey("state"));
108 final String badIntentState = mapForIntentBadIntent.get("state");
109 assertThat(badIntentState, is(equalTo("INST_NACK")));
110
Ray Milkeye8274232014-05-27 16:03:12 -0700111 }
112
113 /**
114 * Test that the GET of a single Intent REST call returns the proper result
115 * when given a bad Intent id. The call to get the Intent should return a
116 * status of NOT_FOUND.
Ray Milkeye8274232014-05-27 16:03:12 -0700117 */
118 @Test
Ray Milkey531bb232014-06-03 09:08:15 -0700119 public void testFetchOfBadIntent() {
Ray Milkeye8274232014-05-27 16:03:12 -0700120
121 makeDefaultIntents();
122
Ray Milkey531bb232014-06-03 09:08:15 -0700123 final ClientResource client = new ClientResource(getHighRestIntentUrl() + "/2334");
Ray Milkeye8274232014-05-27 16:03:12 -0700124
125 try {
126 getIntent(client);
127 // The get operation should have thrown a ResourceException.
128 // Fail because the Exception was not seen.
129 Assert.fail("Invalid intent fetch did not cause an exception");
130 } catch (ResourceException resourceError) {
131 // The HTTP status should be NOT FOUND
Ray Milkey531bb232014-06-03 09:08:15 -0700132 assertThat(client, hasStatusOf(Status.CLIENT_ERROR_NOT_FOUND));
Ray Milkeye8274232014-05-27 16:03:12 -0700133 }
134 }
135
136 /**
137 * Test that the GET of a single Intent REST call returns the proper result
138 * for an existing Intent. The call to get the Intent should return a
139 * status of OK, and the data for the Intent should be correct.
Ray Milkeye8274232014-05-27 16:03:12 -0700140 */
141 @Test
Ray Milkey531bb232014-06-03 09:08:15 -0700142 public void testFetchOfGoodIntent() {
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() + "/2");
Ray Milkeye8274232014-05-27 16:03:12 -0700147 final Map<String, String> intent;
148 intent = getIntent(client);
149
150 // HTTP status should be OK
Ray Milkey531bb232014-06-03 09:08:15 -0700151 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
Ray Milkeye8274232014-05-27 16:03:12 -0700152
153 // Intent data should be correct
154 assertThat(intent, is(notNullValue()));
155 assertThat(intent, hasKey("id"));
156 assertThat(intent.get("id"), is(equalTo("1:2")));
157 assertThat(intent, hasKey("state"));
158 assertThat(intent.get("state"), is(equalTo("INST_REQ")));
Ray Milkey2fa6ca42014-06-13 15:38:20 -0700159
160 assertThat(intent, hasKey("srcSwitchDpid"));
161 assertThat(intent.get("srcSwitchDpid"), is(equalTo("00:00:00:00:00:00:00:01")));
162 assertThat(intent, hasKey("srcPortNumber"));
163 assertThat(intent.get("srcPortNumber"), is(equalTo("14")));
164 assertThat(intent, hasKey("srcMac"));
Pavlin Radoslavovdc59c152014-06-17 17:04:41 -0700165 assertThat(intent.get("srcMac"), is(equalTo("00:00:00:00:ff:fe")));
Ray Milkey2fa6ca42014-06-13 15:38:20 -0700166 assertThat(intent, hasKey("srcMac"));
167 assertThat(intent.get("srcIp"), is(equalTo(IP_ADDRESS_1)));
168
169 assertThat(intent, hasKey("dstSwitchDpid"));
170 assertThat(intent.get("dstSwitchDpid"), is(equalTo("00:00:00:00:00:00:00:04")));
171 assertThat(intent, hasKey("dstPortNumber"));
172 assertThat(intent.get("dstPortNumber"), is(equalTo("41")));
173 assertThat(intent, hasKey("dstMac"));
Pavlin Radoslavovdc59c152014-06-17 17:04:41 -0700174 assertThat(intent.get("dstMac"), is(equalTo("00:00:00:00:ff:fe")));
Ray Milkey2fa6ca42014-06-13 15:38:20 -0700175 assertThat(intent, hasKey("dstMac"));
176 assertThat(intent.get("dstIp"), is(equalTo(IP_ADDRESS_2)));
Ray Milkeye8274232014-05-27 16:03:12 -0700177 }
178}