blob: 6f1c0984c68512dcee990b09056221fefae459e4 [file] [log] [blame]
Pavlin Radoslavov13669052014-05-13 10:33:39 -07001package net.onrc.onos.core.intent.runtime.web;
2
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -07003import java.util.Arrays;
4import java.util.List;
5
Ray Milkey3aeda4f2014-05-21 14:38:58 -07006import net.onrc.onos.api.rest.RestError;
7import net.onrc.onos.api.rest.RestErrorCodes;
Pavlin Radoslavov13669052014-05-13 10:33:39 -07008import net.onrc.onos.core.intent.Intent;
9import net.onrc.onos.core.intent.IntentMap;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070010import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070011
Ray Milkey3aeda4f2014-05-21 14:38:58 -070012import org.restlet.data.Status;
13import org.restlet.representation.Representation;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070014import org.restlet.resource.Delete;
15import org.restlet.resource.Get;
16import org.restlet.resource.ServerResource;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070017
18/**
19 * A class to access a single high-level intent.
20 */
21public class IntentHighObjectResource extends ServerResource {
Pavlin Radoslavov13669052014-05-13 10:33:39 -070022 // TODO need to assign proper application id.
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -070023 private static final String APPLICATION_ID = "1";
Pavlin Radoslavov13669052014-05-13 10:33:39 -070024
25 /**
26 * Gets a single high-level intent.
27 *
Ray Milkey3aeda4f2014-05-21 14:38:58 -070028 * @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 Radoslavov13669052014-05-13 10:33:39 -070031 */
32 @Get("json")
Ray Milkey3aeda4f2014-05-21 14:38:58 -070033 public Representation retrieve() {
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070034 IPathCalcRuntimeService pathRuntime =
35 (IPathCalcRuntimeService) getContext().getAttributes()
36 .get(IPathCalcRuntimeService.class.getCanonicalName());
Ray Milkey3aeda4f2014-05-21 14:38:58 -070037
Pavlin Radoslavov13669052014-05-13 10:33:39 -070038 String intentId = (String) getRequestAttributes().get("intent-id");
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -070039 Representation result;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070040
41 //
42 // Get a single high-level Intent: use the Intent ID to find it
43 //
44 IntentMap intentMap = pathRuntime.getHighLevelIntents();
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -070045 String applnIntentId = APPLICATION_ID + ":" + intentId;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070046 Intent intent = intentMap.getIntent(applnIntentId);
47 if (intent != null) {
Ray Milkey3aeda4f2014-05-21 14:38:58 -070048 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 Radoslavov13669052014-05-13 10:33:39 -070055 }
56
Ray Milkey3aeda4f2014-05-21 14:38:58 -070057 return result;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070058 }
59
60 /**
61 * Deletes a single high-level intent.
62 *
Ray Milkey3aeda4f2014-05-21 14:38:58 -070063 * @return a null Representation.
Pavlin Radoslavov13669052014-05-13 10:33:39 -070064 */
65 @Delete("json")
Ray Milkey3aeda4f2014-05-21 14:38:58 -070066 public Representation remove() {
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070067 IPathCalcRuntimeService pathRuntime =
68 (IPathCalcRuntimeService) getContext().getAttributes()
69 .get(IPathCalcRuntimeService.class.getCanonicalName());
Pavlin Radoslavov13669052014-05-13 10:33:39 -070070
71 String intentId = (String) getRequestAttributes().get("intent-id");
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -070072 List<String> intentIds = Arrays.asList(intentId);
Pavlin Radoslavov13669052014-05-13 10:33:39 -070073
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -070074 if (pathRuntime.removeApplicationIntents(APPLICATION_ID, intentIds)) {
75 setStatus(Status.SUCCESS_NO_CONTENT);
76 } else {
77 setStatus(Status.SERVER_ERROR_INTERNAL);
Pavlin Radoslavov13669052014-05-13 10:33:39 -070078 }
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -070079
Ray Milkey3aeda4f2014-05-21 14:38:58 -070080 return null;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070081 }
82}