blob: 2406674662ca56b57e3d37655dcf24147e3f7ed9 [file] [log] [blame]
Pavlin Radoslavov13669052014-05-13 10:33:39 -07001package net.onrc.onos.core.intent.runtime.web;
2
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -07003import java.io.IOException;
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -07004import java.util.Arrays;
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -07005import java.util.Collection;
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -07006import java.util.List;
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -07007
Pavlin Radoslavov13669052014-05-13 10:33:39 -07008import net.onrc.onos.api.intent.ApplicationIntent;
Ray Milkeya8091b12014-05-16 14:42:47 -07009import net.onrc.onos.api.rest.RestError;
10import net.onrc.onos.api.rest.RestErrorCodes;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070011import net.onrc.onos.core.intent.Intent;
12import net.onrc.onos.core.intent.IntentMap;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070013import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070014
Pavlin Radoslavov13669052014-05-13 10:33:39 -070015import org.codehaus.jackson.map.ObjectMapper;
Ray Milkeya8091b12014-05-16 14:42:47 -070016import org.restlet.data.Status;
Ray Milkey3aeda4f2014-05-21 14:38:58 -070017import org.restlet.representation.Representation;
Pavlin Radoslavov13669052014-05-13 10:33:39 -070018import org.restlet.resource.Delete;
19import org.restlet.resource.Get;
20import org.restlet.resource.Post;
21import org.restlet.resource.ServerResource;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25/**
26 * A class to access the high-level intents.
27 */
28public class IntentHighResource extends ServerResource {
29 private static final Logger log = LoggerFactory.getLogger(IntentHighResource.class);
30 // TODO need to assign proper application id.
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -070031 private static final String APPLICATION_ID = "1";
Pavlin Radoslavov13669052014-05-13 10:33:39 -070032
33 /**
34 * Gets all high-level intents.
35 *
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -070036 * @return a Representation for a collection with all high-level intents.
Pavlin Radoslavov13669052014-05-13 10:33:39 -070037 */
38 @Get("json")
Ray Milkey3aeda4f2014-05-21 14:38:58 -070039 public Representation retrieve() throws IOException {
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070040 IPathCalcRuntimeService pathRuntime =
41 (IPathCalcRuntimeService) getContext().getAttributes()
42 .get(IPathCalcRuntimeService.class.getCanonicalName());
Pavlin Radoslavov13669052014-05-13 10:33:39 -070043
44 IntentMap intentMap = pathRuntime.getHighLevelIntents();
45 Collection<Intent> intents = intentMap.getAllIntents();
46
Ray Milkey3aeda4f2014-05-21 14:38:58 -070047 return toRepresentation(intents, null);
Pavlin Radoslavov13669052014-05-13 10:33:39 -070048 }
49
50 /**
51 * Adds a collection of high-level intents.
52 *
Ray Milkey3aeda4f2014-05-21 14:38:58 -070053 * @param jsonIntent JSON representation of the intents to add.
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -070054 * @return a Representation of a collection containing the intents that
55 * were added.
Pavlin Radoslavov13669052014-05-13 10:33:39 -070056 */
57 @Post("json")
Ray Milkey3aeda4f2014-05-21 14:38:58 -070058 public Representation store(String jsonIntent) {
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -070059 IPathCalcRuntimeService pathRuntime =
60 (IPathCalcRuntimeService) getContext().getAttributes()
61 .get(IPathCalcRuntimeService.class.getCanonicalName());
Pavlin Radoslavov13669052014-05-13 10:33:39 -070062
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -070063 //
64 // Extract the Application Intents
65 //
Pavlin Radoslavov13669052014-05-13 10:33:39 -070066 ObjectMapper mapper = new ObjectMapper();
67 ApplicationIntent[] addOperations = null;
68 try {
Ray Milkeya8091b12014-05-16 14:42:47 -070069 if (jsonIntent != null) {
70 addOperations = mapper.readValue(jsonIntent, ApplicationIntent[].class);
71 }
Pavlin Radoslavov13669052014-05-13 10:33:39 -070072 } catch (IOException ex) {
Ray Milkeya8091b12014-05-16 14:42:47 -070073 log.error("Exception occurred parsing inbound JSON", ex);
Pavlin Radoslavov13669052014-05-13 10:33:39 -070074 }
75 if (addOperations == null) {
Ray Milkeya8091b12014-05-16 14:42:47 -070076 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
77 final RestError error =
78 RestError.createRestError(RestErrorCodes.RestErrorCode.INTENT_INVALID);
Ray Milkey3aeda4f2014-05-21 14:38:58 -070079 return toRepresentation(error, null);
Pavlin Radoslavov13669052014-05-13 10:33:39 -070080 }
81
82 //
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -070083 // Add the intents
Pavlin Radoslavov13669052014-05-13 10:33:39 -070084 //
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -070085 if (pathRuntime.addApplicationIntents(APPLICATION_ID,
86 Arrays.asList(addOperations))) {
87 setStatus(Status.SUCCESS_CREATED);
88 } else {
89 setStatus(Status.SERVER_ERROR_INTERNAL);
Pavlin Radoslavov13669052014-05-13 10:33:39 -070090 }
Pavlin Radoslavov13669052014-05-13 10:33:39 -070091
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -070092 return toRepresentation(addOperations, null);
Pavlin Radoslavov13669052014-05-13 10:33:39 -070093 }
94
95 /**
96 * Deletes all high-level intents.
97 *
Ray Milkey3aeda4f2014-05-21 14:38:58 -070098 * @return a null Representation.
Pavlin Radoslavov13669052014-05-13 10:33:39 -070099 */
100 @Delete("json")
Ray Milkey3aeda4f2014-05-21 14:38:58 -0700101 public Representation remove() {
Pavlin Radoslavovc097fdf2014-05-23 17:40:57 -0700102 IPathCalcRuntimeService pathRuntime =
103 (IPathCalcRuntimeService) getContext().getAttributes()
104 .get(IPathCalcRuntimeService.class.getCanonicalName());
Pavlin Radoslavov13669052014-05-13 10:33:39 -0700105
Pavlin Radoslavove2238bc2014-06-09 18:05:23 -0700106 //
107 // Get the optional query values: comma-separated list of Intent IDs
108 //
109 String intentIdValue = getQueryValue("intent_id");
110 boolean success;
111
112 //
113 // Delete the intents
114 //
115 if (intentIdValue != null) {
116 // Delete a collection of intents, specified by Intent IDs
117 List<String> intentIds = Arrays.asList(intentIdValue.split(","));
118 success = pathRuntime.removeApplicationIntents(APPLICATION_ID,
119 intentIds);
120 } else {
121 // Delete all intents
122 success = pathRuntime.removeAllApplicationIntents(APPLICATION_ID);
123 }
124
125 if (success) {
126 setStatus(Status.SUCCESS_NO_CONTENT);
127 } else {
128 setStatus(Status.SERVER_ERROR_INTERNAL);
129 }
130
131 return null;
Pavlin Radoslavov13669052014-05-13 10:33:39 -0700132 }
133}