Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent.runtime.web; |
| 2 | |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 3 | import java.io.IOException; |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 4 | import java.util.Arrays; |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 5 | import java.util.Collection; |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 6 | import java.util.List; |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 7 | |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 8 | import net.onrc.onos.api.intent.ApplicationIntent; |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 9 | import net.onrc.onos.api.rest.RestError; |
| 10 | import net.onrc.onos.api.rest.RestErrorCodes; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 11 | import net.onrc.onos.core.intent.Intent; |
| 12 | import net.onrc.onos.core.intent.IntentMap; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService; |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 14 | |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 15 | import org.codehaus.jackson.map.ObjectMapper; |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 16 | import org.restlet.data.Status; |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 17 | import org.restlet.representation.Representation; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 18 | import org.restlet.resource.Delete; |
| 19 | import org.restlet.resource.Get; |
| 20 | import org.restlet.resource.Post; |
| 21 | import org.restlet.resource.ServerResource; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | /** |
| 26 | * A class to access the high-level intents. |
| 27 | */ |
| 28 | public class IntentHighResource extends ServerResource { |
| 29 | private static final Logger log = LoggerFactory.getLogger(IntentHighResource.class); |
| 30 | // TODO need to assign proper application id. |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 31 | private static final String APPLICATION_ID = "1"; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Gets all high-level intents. |
| 35 | * |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 36 | * @return a Representation for a collection with all high-level intents. |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 37 | */ |
| 38 | @Get("json") |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 39 | public Representation retrieve() throws IOException { |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 40 | IPathCalcRuntimeService pathRuntime = |
| 41 | (IPathCalcRuntimeService) getContext().getAttributes() |
| 42 | .get(IPathCalcRuntimeService.class.getCanonicalName()); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 43 | |
| 44 | IntentMap intentMap = pathRuntime.getHighLevelIntents(); |
| 45 | Collection<Intent> intents = intentMap.getAllIntents(); |
| 46 | |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 47 | return toRepresentation(intents, null); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Adds a collection of high-level intents. |
| 52 | * |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 53 | * @param jsonIntent JSON representation of the intents to add. |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 54 | * @return a Representation of a collection containing the intents that |
| 55 | * were added. |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 56 | */ |
| 57 | @Post("json") |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 58 | public Representation store(String jsonIntent) { |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 59 | IPathCalcRuntimeService pathRuntime = |
| 60 | (IPathCalcRuntimeService) getContext().getAttributes() |
| 61 | .get(IPathCalcRuntimeService.class.getCanonicalName()); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 62 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 63 | // |
| 64 | // Extract the Application Intents |
| 65 | // |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 66 | ObjectMapper mapper = new ObjectMapper(); |
| 67 | ApplicationIntent[] addOperations = null; |
| 68 | try { |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 69 | if (jsonIntent != null) { |
| 70 | addOperations = mapper.readValue(jsonIntent, ApplicationIntent[].class); |
| 71 | } |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 72 | } catch (IOException ex) { |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 73 | log.error("Exception occurred parsing inbound JSON", ex); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 74 | } |
| 75 | if (addOperations == null) { |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 76 | setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
| 77 | final RestError error = |
| 78 | RestError.createRestError(RestErrorCodes.RestErrorCode.INTENT_INVALID); |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 79 | return toRepresentation(error, null); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 83 | // Add the intents |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 84 | // |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 85 | if (pathRuntime.addApplicationIntents(APPLICATION_ID, |
| 86 | Arrays.asList(addOperations))) { |
| 87 | setStatus(Status.SUCCESS_CREATED); |
| 88 | } else { |
| 89 | setStatus(Status.SERVER_ERROR_INTERNAL); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 90 | } |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 91 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 92 | return toRepresentation(addOperations, null); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Deletes all high-level intents. |
| 97 | * |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 98 | * @return a null Representation. |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 99 | */ |
| 100 | @Delete("json") |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 101 | public Representation remove() { |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 102 | IPathCalcRuntimeService pathRuntime = |
| 103 | (IPathCalcRuntimeService) getContext().getAttributes() |
| 104 | .get(IPathCalcRuntimeService.class.getCanonicalName()); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 105 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 106 | // |
| 107 | // Get the optional query values: comma-separated list of Intent IDs |
| 108 | // |
| 109 | String intentIdValue = getQueryValue("intent_id"); |
| 110 | boolean success; |
| 111 | |
| 112 | // |
| 113 | // Delete the intents |
| 114 | // |
| 115 | if (intentIdValue != null) { |
| 116 | // Delete a collection of intents, specified by Intent IDs |
| 117 | List<String> intentIds = Arrays.asList(intentIdValue.split(",")); |
| 118 | success = pathRuntime.removeApplicationIntents(APPLICATION_ID, |
| 119 | intentIds); |
| 120 | } else { |
| 121 | // Delete all intents |
| 122 | success = pathRuntime.removeAllApplicationIntents(APPLICATION_ID); |
| 123 | } |
| 124 | |
| 125 | if (success) { |
| 126 | setStatus(Status.SUCCESS_NO_CONTENT); |
| 127 | } else { |
| 128 | setStatus(Status.SERVER_ERROR_INTERNAL); |
| 129 | } |
| 130 | |
| 131 | return null; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 132 | } |
| 133 | } |