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.runtime.IPathCalcRuntimeService; |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame^] | 8 | |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 9 | import org.restlet.data.Status; |
| 10 | import org.restlet.representation.Representation; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 11 | import org.restlet.resource.Get; |
| 12 | import org.restlet.resource.ServerResource; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * A class to access a single low-level intent. |
| 16 | */ |
| 17 | public class IntentLowObjectResource extends ServerResource { |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 18 | // TODO need to assign proper application id. |
| 19 | private static final String APPLN_ID = "1"; |
| 20 | |
| 21 | /** |
| 22 | * Gets a single low-level intent. |
| 23 | * |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 24 | * @return a Representation of the single low-level intent if found, |
| 25 | * otherwise a Representation of a RestError object describing the problem. |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 26 | */ |
| 27 | @Get("json") |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 28 | public Representation retrieve() { |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame^] | 29 | IPathCalcRuntimeService pathRuntime = |
| 30 | (IPathCalcRuntimeService) getContext().getAttributes() |
| 31 | .get(IPathCalcRuntimeService.class.getCanonicalName()); |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 32 | |
| 33 | Representation result; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 34 | |
| 35 | String intentId = (String) getRequestAttributes().get("intent-id"); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 36 | |
| 37 | // |
| 38 | // Get a single low-level Intent: use the Intent ID to find it |
| 39 | // |
| 40 | IntentMap intentMap = pathRuntime.getPathIntents(); |
| 41 | String applnIntentId = APPLN_ID + ":" + intentId; |
| 42 | Intent intent = intentMap.getIntent(applnIntentId); |
| 43 | if (intent != null) { |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 44 | result = toRepresentation(intent, null); |
| 45 | } else { |
| 46 | setStatus(Status.CLIENT_ERROR_NOT_FOUND); |
| 47 | final RestError notFound = |
| 48 | RestError.createRestError(RestErrorCodes.RestErrorCode.INTENT_NOT_FOUND, |
Pavlin Radoslavov | c097fdf | 2014-05-23 17:40:57 -0700 | [diff] [blame^] | 49 | applnIntentId); |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 50 | result = toRepresentation(notFound, null); |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Ray Milkey | 3aeda4f | 2014-05-21 14:38:58 -0700 | [diff] [blame] | 53 | return result; |
Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 54 | } |
| 55 | } |