Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent.runtime.web; |
| 2 | |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 3 | import net.onrc.onos.api.rest.RestError; |
| 4 | import net.onrc.onos.api.rest.RestErrorCodes; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.intent.Intent; |
| 6 | import net.onrc.onos.core.intent.IntentMap; |
| 7 | import net.onrc.onos.core.intent.IntentOperation; |
| 8 | import net.onrc.onos.core.intent.IntentOperationList; |
| 9 | import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService; |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame^] | 10 | |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 11 | import org.restlet.data.Status; |
| 12 | import org.restlet.representation.Representation; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 13 | import org.restlet.resource.Delete; |
| 14 | import org.restlet.resource.Get; |
| 15 | import org.restlet.resource.ServerResource; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * A class to access a single high-level intent. |
| 19 | */ |
| 20 | public class IntentHighObjectResource extends ServerResource { |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 21 | // TODO need to assign proper application id. |
| 22 | private static final String APPLN_ID = "1"; |
| 23 | |
| 24 | /** |
| 25 | * Gets a single high-level intent. |
| 26 | * |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 27 | * @return a Representation of a single high-level intent. If the |
| 28 | * intent is not found, return a Representation of a RestError indicating |
| 29 | * the problem. |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 30 | */ |
| 31 | @Get("json") |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 32 | public Representation retrieve() { |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame^] | 33 | IPathCalcRuntimeService pathRuntime = |
| 34 | (IPathCalcRuntimeService) getContext().getAttributes() |
| 35 | .get(IPathCalcRuntimeService.class.getCanonicalName()); |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 36 | |
| 37 | Representation result; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 38 | |
| 39 | String intentId = (String) getRequestAttributes().get("intent-id"); |
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(); |
| 45 | String applnIntentId = APPLN_ID + ":" + intentId; |
| 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 = |
| 52 | RestError.createRestError(RestErrorCodes.RestErrorCode.INTENT_NOT_FOUND, |
| 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 | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 72 | |
| 73 | // |
| 74 | // Remove a single high-level Intent: use the Intent ID to find it |
| 75 | // |
| 76 | // |
| 77 | // TODO: The implementation below is broken - waiting for the Java API |
| 78 | // TODO: The deletion should use synchronous Java API? |
| 79 | IntentMap intentMap = pathRuntime.getHighLevelIntents(); |
| 80 | String applnIntentId = APPLN_ID + ":" + intentId; |
| 81 | Intent intent = intentMap.getIntent(applnIntentId); |
| 82 | if (intent != null) { |
| 83 | IntentOperationList operations = new IntentOperationList(); |
| 84 | operations.add(IntentOperation.Operator.REMOVE, intent); |
| 85 | pathRuntime.executeIntentOperations(operations); |
| 86 | } |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 87 | setStatus(Status.SUCCESS_NO_CONTENT); |
| 88 | return null; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 89 | } |
| 90 | } |