Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent.runtime.web; |
| 2 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 3 | import java.util.Arrays; |
| 4 | import java.util.List; |
| 5 | |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 6 | import net.onrc.onos.api.rest.RestError; |
Ray Milkey | 6fdd91d | 2014-07-30 16:27:07 -0700 | [diff] [blame] | 7 | import net.onrc.onos.api.rest.RestErrorCode; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.intent.Intent; |
| 9 | import net.onrc.onos.core.intent.IntentMap; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService; |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 11 | |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 12 | import org.restlet.data.Status; |
| 13 | import org.restlet.representation.Representation; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 14 | import org.restlet.resource.Delete; |
| 15 | import org.restlet.resource.Get; |
| 16 | import org.restlet.resource.ServerResource; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * A class to access a single high-level intent. |
| 20 | */ |
| 21 | public class IntentHighObjectResource extends ServerResource { |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 22 | // TODO need to assign proper application id. |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 23 | private static final String APPLICATION_ID = "1"; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Gets a single high-level intent. |
| 27 | * |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 28 | * @return a Representation of a single high-level intent. If the |
| 29 | * intent is not found, return a Representation of a RestError indicating |
| 30 | * the problem. |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 31 | */ |
| 32 | @Get("json") |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 33 | public Representation retrieve() { |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 34 | IPathCalcRuntimeService pathRuntime = |
| 35 | (IPathCalcRuntimeService) getContext().getAttributes() |
| 36 | .get(IPathCalcRuntimeService.class.getCanonicalName()); |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 37 | |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 38 | String intentId = (String) getRequestAttributes().get("intent-id"); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 39 | Representation result; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 40 | |
| 41 | // |
| 42 | // Get a single high-level Intent: use the Intent ID to find it |
| 43 | // |
| 44 | IntentMap intentMap = pathRuntime.getHighLevelIntents(); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 45 | String applnIntentId = APPLICATION_ID + ":" + intentId; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 46 | Intent intent = intentMap.getIntent(applnIntentId); |
| 47 | if (intent != null) { |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 48 | result = toRepresentation(intent, null); |
| 49 | } else { |
| 50 | setStatus(Status.CLIENT_ERROR_NOT_FOUND); |
| 51 | final RestError notFound = |
Ray Milkey | 6fdd91d | 2014-07-30 16:27:07 -0700 | [diff] [blame] | 52 | RestError.createRestError(RestErrorCode.INTENT_NOT_FOUND, |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 53 | applnIntentId); |
| 54 | result = toRepresentation(notFound, null); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 57 | return result; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Deletes a single high-level intent. |
| 62 | * |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 63 | * @return a null Representation. |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 64 | */ |
| 65 | @Delete("json") |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 66 | public Representation remove() { |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame] | 67 | IPathCalcRuntimeService pathRuntime = |
| 68 | (IPathCalcRuntimeService) getContext().getAttributes() |
| 69 | .get(IPathCalcRuntimeService.class.getCanonicalName()); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 70 | |
| 71 | String intentId = (String) getRequestAttributes().get("intent-id"); |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 72 | List<String> intentIds = Arrays.asList(intentId); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 73 | |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 74 | if (pathRuntime.removeApplicationIntents(APPLICATION_ID, intentIds)) { |
| 75 | setStatus(Status.SUCCESS_NO_CONTENT); |
| 76 | } else { |
| 77 | setStatus(Status.SERVER_ERROR_INTERNAL); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 78 | } |
Pavlin Radoslavov | e2238bc | 2014-06-09 18:05:23 -0700 | [diff] [blame] | 79 | |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 80 | return null; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 81 | } |
| 82 | } |