blob: fa66ea10a3b893900191b14fa7828407cdeecae7 [file] [log] [blame]
Pavlin Radoslavov13669052014-05-13 10:33:39 -07001package net.onrc.onos.core.intent.runtime.web;
2
Ray Milkey3aeda4f2014-05-21 14:38:58 -07003import net.onrc.onos.api.rest.RestError;
4import net.onrc.onos.api.rest.RestErrorCodes;
Pavlin Radoslavov13669052014-05-13 10:33:39 -07005import net.onrc.onos.core.intent.Intent;
6import net.onrc.onos.core.intent.IntentMap;
7import net.onrc.onos.core.intent.IntentOperation;
8import net.onrc.onos.core.intent.IntentOperationList;
9import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070010
Ray Milkey3aeda4f2014-05-21 14:38:58 -070011import org.restlet.data.Status;
12import org.restlet.representation.Representation;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070013import org.restlet.resource.Delete;
14import org.restlet.resource.Get;
15import org.restlet.resource.ServerResource;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070016
17/**
18 * A class to access a single high-level intent.
19 */
20public class IntentHighObjectResource extends ServerResource {
Pavlin Radoslavov13669052014-05-13 10:33:39 -070021 // 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 Milkey3aeda4f2014-05-21 14:38:58 -070027 * @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 Radoslavov13669052014-05-13 10:33:39 -070030 */
31 @Get("json")
Ray Milkey3aeda4f2014-05-21 14:38:58 -070032 public Representation retrieve() {
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070033 IPathCalcRuntimeService pathRuntime =
34 (IPathCalcRuntimeService) getContext().getAttributes()
35 .get(IPathCalcRuntimeService.class.getCanonicalName());
Ray Milkey3aeda4f2014-05-21 14:38:58 -070036
37 Representation result;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070038
39 String intentId = (String) getRequestAttributes().get("intent-id");
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();
45 String applnIntentId = APPLN_ID + ":" + intentId;
46 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 Radoslavov13669052014-05-13 10:33:39 -070072
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 Milkey3aeda4f2014-05-21 14:38:58 -070087 setStatus(Status.SUCCESS_NO_CONTENT);
88 return null;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070089 }
90}