Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
| 3 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 4 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
Ray Milkey | d83844d | 2014-06-20 13:16:01 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.intent.Intent; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService; |
Ray Milkey | d83844d | 2014-06-20 13:16:01 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.intent.runtime.IntentStateList; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.intent.runtime.IntentTestMocks; |
| 9 | import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule; |
| 10 | import net.onrc.onos.core.intent.runtime.web.IntentWebRoutable; |
| 11 | import org.restlet.resource.ClientResource; |
| 12 | |
| 13 | import java.util.Collection; |
| 14 | import java.util.Map; |
| 15 | |
| 16 | /** |
| 17 | * Base class for Intents REST API tests. |
| 18 | * Maintains the web server and restlets used for the REST calls |
| 19 | * Maintains whatever mocks are required to interact with the Intents framework |
| 20 | * Provides JSON related utility routines for processing Intents APIs return values |
| 21 | */ |
| 22 | public class TestRestIntent extends TestRest { |
| 23 | |
| 24 | private PathCalcRuntimeModule runtime; |
| 25 | private IntentTestMocks mocks; |
| 26 | |
| 27 | /** |
| 28 | * Fetch the Path Calc Runtime. |
| 29 | * |
| 30 | * @return path calc runtime. |
| 31 | */ |
| 32 | PathCalcRuntimeModule getRuntime() { |
| 33 | return runtime; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Fetch the Intent mocking object. |
| 38 | * |
| 39 | * @return intent mocking object |
| 40 | */ |
| 41 | IntentTestMocks getMocks() { |
| 42 | return mocks; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Create the web server, PathCalcRuntime, and mocks required for |
| 47 | * all of the tests. |
| 48 | */ |
| 49 | @Override |
| 50 | public void setUp() { |
| 51 | mocks = new IntentTestMocks(); |
| 52 | mocks.setUpIntentMocks(); |
| 53 | |
| 54 | addRestlet(new IntentWebRoutable()); |
| 55 | super.setUp(); |
| 56 | |
| 57 | runtime = new PathCalcRuntimeModule(); |
| 58 | final FloodlightModuleContext moduleContext = getMocks().getModuleContext(); |
| 59 | try { |
| 60 | runtime.init(moduleContext); |
| 61 | } catch (FloodlightModuleException floodlightEx) { |
| 62 | throw new IllegalArgumentException(floodlightEx); |
| 63 | } |
| 64 | runtime.startUp(moduleContext); |
| 65 | |
| 66 | getRestApiServer().addAttribute(IPathCalcRuntimeService.class.getCanonicalName(), |
| 67 | runtime); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Remove anything that will interfere with the next test running correctly. |
| 72 | * Shuts down the test REST web server and removes the mocks. |
| 73 | */ |
| 74 | @Override |
| 75 | public void tearDown() { |
| 76 | getMocks().tearDownIntentMocks(); |
| 77 | super.tearDown(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Fetch the base URL for Intents REST APIs. |
| 82 | * |
| 83 | * @return base URL |
| 84 | */ |
| 85 | String getBaseRestIntentUrl() { |
| 86 | return getBaseRestUrl() + "/intent"; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Fetch the URL to use for High Level Intents REST API calls. |
| 91 | * |
| 92 | * @return high level intents REST API URL |
| 93 | */ |
| 94 | String getHighRestIntentUrl() { |
| 95 | return getBaseRestIntentUrl() + "/high"; |
| 96 | } |
| 97 | |
| 98 | /** |
Ray Milkey | 213e56a | 2014-06-23 10:45:45 -0700 | [diff] [blame] | 99 | * Fetch the URL to use for Low Level Intents REST API calls. |
| 100 | * |
| 101 | * @return low level intents REST API URL |
| 102 | */ |
| 103 | String getLowRestIntentUrl() { |
| 104 | return getBaseRestIntentUrl() + "/low"; |
| 105 | } |
| 106 | |
| 107 | /** |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 108 | * Utility function to locate an intent in a JSON collection |
| 109 | * that has the given id. |
| 110 | * The JSON collection of intents looks like: |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 111 | * {@code |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 112 | * MAP = |
| 113 | * [0] = |
| 114 | * MAP = |
| 115 | * id = "1" |
| 116 | * ... |
| 117 | * [1] |
| 118 | * MAP = |
| 119 | * id = "2" |
| 120 | * ... |
| 121 | * [2] |
| 122 | * MAP = |
| 123 | * id = "3" |
| 124 | * ... |
| 125 | * ... |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 126 | * } |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 127 | * |
| 128 | * @param intents collection map to search |
| 129 | * @param id id of the intent to look for |
| 130 | * @return map for the intent if one was found, null otherwise |
| 131 | */ |
| 132 | Map<String, String> findIntentWithId(final Collection<Map<String, String>> intents, |
| 133 | final String id) { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 134 | for (final Map<String, String> intentMap : intents) { |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 135 | if (id.equals(intentMap.get("id"))) { |
| 136 | return intentMap; |
| 137 | } |
| 138 | } |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * Convenience function to fetch a collection of Intents from the JSON |
| 145 | * result of a REST call. Hides the ugliness of the unchecked conversion |
| 146 | * to the proper Collection of Map type. |
| 147 | * |
| 148 | * @param client ClientResource that was used to make the REST call |
| 149 | * @return Collection of Maps that hold the Intent data |
| 150 | */ |
| 151 | @SuppressWarnings("unchecked") |
| 152 | Collection<Map<String, String>> getIntentsCollection(final ClientResource client) { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 153 | return client.get(Collection.class); |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Convenience function to fetch a single Intent from the JSON |
| 158 | * result of a REST call. Hides the ugliness of the unchecked conversion |
| 159 | * to the proper Map type. |
| 160 | * |
| 161 | * @param client ClientResource that was used to make the REST call |
| 162 | * @return Map that hold the Intent data |
| 163 | */ |
| 164 | @SuppressWarnings("unchecked") |
| 165 | Map<String, String> getIntent(final ClientResource client) { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 166 | return client.get(Map.class); |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 167 | } |
Ray Milkey | d83844d | 2014-06-20 13:16:01 -0700 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * Modify the state of an intent by directly calling the Intent state |
| 171 | * machine. Needed in unit tests because of mocking of back end |
| 172 | * components. |
| 173 | * |
| 174 | * @param intentId id of the intent to modify |
| 175 | * @param newState assign this state to the intent |
| 176 | */ |
| 177 | public void modifyIntentState(final String intentId, |
| 178 | final Intent.IntentState newState) { |
| 179 | final IntentStateList intentStateList = new IntentStateList(); |
| 180 | intentStateList.put(intentId, newState); |
| 181 | getRuntime().entryUpdated(intentStateList); |
| 182 | } |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 183 | } |