Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
| 3 | |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 4 | import com.google.common.net.InetAddresses; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.intent.IntentOperation; |
| 6 | import net.onrc.onos.core.intent.IntentOperationList; |
| 7 | import net.onrc.onos.core.intent.ShortestPathIntent; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 9 | import org.junit.Assert; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 10 | import org.junit.Test; |
| 11 | import org.junit.runner.RunWith; |
| 12 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 13 | import org.powermock.modules.junit4.PowerMockRunner; |
| 14 | import org.restlet.data.Status; |
| 15 | import org.restlet.resource.ClientResource; |
| 16 | import org.restlet.resource.ResourceException; |
| 17 | |
| 18 | import java.util.Collection; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 19 | import java.util.Map; |
| 20 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 21 | import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 22 | import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | import static org.hamcrest.Matchers.equalTo; |
| 24 | import static org.hamcrest.Matchers.hasKey; |
| 25 | import static org.hamcrest.Matchers.hasSize; |
| 26 | import static org.hamcrest.Matchers.is; |
| 27 | import static org.hamcrest.Matchers.notNullValue; |
| 28 | |
| 29 | /** |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 30 | * Unit tests to test the Intents GET REST APIs. |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 31 | */ |
| 32 | @RunWith(PowerMockRunner.class) |
| 33 | @PrepareForTest(PathCalcRuntimeModule.class) |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 34 | public class TestRestIntentHighGet extends TestRestIntent { |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 35 | private static final Long LOCAL_PORT = 0xFFFEL; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 36 | private static final String BAD_SWITCH_INTENT_NAME = "No Such Switch Intent"; |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 37 | 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 Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 42 | * Make a set of Intents that can be used as test data. |
| 43 | */ |
| 44 | private void makeDefaultIntents() { |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 45 | 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 Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 52 | // 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 Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 58 | new ShortestPathIntent("1:2", 1L, 14L, LOCAL_PORT, ipAddress1AsInt, |
| 59 | 4L, 41L, LOCAL_PORT, ipAddress2AsInt)); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 60 | opList.add(IntentOperation.Operator.ADD, |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 61 | new ShortestPathIntent("1:3", 2L, 23L, LOCAL_PORT, ipAddress2AsInt, |
| 62 | 3L, 32L, LOCAL_PORT, ipAddress3AsInt)); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 63 | |
| 64 | // compile high-level intent operations into low-level intent |
| 65 | // operations (calculate paths) |
| 66 | |
| 67 | final IntentOperationList pathIntentOpList = |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 68 | getRuntime().executeIntentOperations(opList); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 69 | assertThat(pathIntentOpList, notNullValue()); |
| 70 | |
| 71 | } |
| 72 | |
| 73 | /** |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 74 | * 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 Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 77 | */ |
| 78 | @Test |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 79 | public void testFetchOfAllIntents() { |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 80 | |
| 81 | makeDefaultIntents(); |
| 82 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 83 | final ClientResource client = new ClientResource(getHighRestIntentUrl()); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 84 | final Collection<Map<String, String>> intents = getIntentsCollection(client); |
| 85 | |
| 86 | // HTTP status should be OK |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 87 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 88 | |
| 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 Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 100 | |
| 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 Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 111 | } |
| 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 Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 117 | */ |
| 118 | @Test |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 119 | public void testFetchOfBadIntent() { |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 120 | |
| 121 | makeDefaultIntents(); |
| 122 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 123 | final ClientResource client = new ClientResource(getHighRestIntentUrl() + "/2334"); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 124 | |
| 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 Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 132 | assertThat(client, hasStatusOf(Status.CLIENT_ERROR_NOT_FOUND)); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 133 | } |
| 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 Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 140 | */ |
| 141 | @Test |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 142 | public void testFetchOfGoodIntent() { |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 143 | |
| 144 | makeDefaultIntents(); |
| 145 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 146 | final ClientResource client = new ClientResource(getHighRestIntentUrl() + "/2"); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 147 | final Map<String, String> intent; |
| 148 | intent = getIntent(client); |
| 149 | |
| 150 | // HTTP status should be OK |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 151 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 152 | |
| 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 Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 159 | |
| 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 Radoslavov | dc59c15 | 2014-06-17 17:04:41 -0700 | [diff] [blame] | 165 | assertThat(intent.get("srcMac"), is(equalTo("00:00:00:00:ff:fe"))); |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 166 | 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 Radoslavov | dc59c15 | 2014-06-17 17:04:41 -0700 | [diff] [blame] | 174 | assertThat(intent.get("dstMac"), is(equalTo("00:00:00:00:ff:fe"))); |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 175 | assertThat(intent, hasKey("dstMac")); |
| 176 | assertThat(intent.get("dstIp"), is(equalTo(IP_ADDRESS_2))); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 177 | } |
| 178 | } |