blob: fc66e0621944d1072b9a0953c5d3d328901093d5 [file] [log] [blame]
gyewan.anecb32692019-07-10 14:36:58 +09001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.workflow.rest;
17
18import com.fasterxml.jackson.core.JsonProcessingException;
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.JsonNodeFactory;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.onosproject.rest.AbstractWebResource;
23import org.onosproject.workflow.api.WorkflowException;
24import org.onosproject.workflow.api.WorkflowService;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import javax.ws.rs.Consumes;
29import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
34import java.io.IOException;
35import java.io.InputStream;
36
37import static org.onlab.util.Tools.readTreeFromStream;
38import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
39import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
40import static org.onosproject.workflow.api.WorkflowDescription.WF_ID;
41import static org.onosproject.workflow.impl.WorkflowNetConfigListener.WORKFLOW_INVOKE;
42
43@Path("workflow")
44public class WorkflowRestApi extends AbstractWebResource {
45
46 private final Logger log = LoggerFactory.getLogger(getClass());
47 private WorkflowService service = get(WorkflowService.class);
48 private static final String OPERATION = "op";
49 private static final String PARAMS = "params";
50
51
52 @POST
53 @Consumes(MediaType.APPLICATION_JSON)
54 @Produces(MediaType.APPLICATION_JSON)
55 @Path("/invoke")
56 public Response invoke(InputStream stream) {
57 ObjectNode ret = JsonNodeFactory.instance.objectNode();
58 try {
59 ObjectNode payload = readTreeFromStream(mapper(), stream);
60 String operation = payload.path(OPERATION).asText();
61
62 if (!(WORKFLOW_INVOKE.equals(operation))) {
63 log.error("Operation is not matched");
64 return Response.status(BAD_REQUEST)
65 .entity("Operation is not matched!").build();
66 }
67
68 JsonNode wfDescJson = payload.path(PARAMS).deepCopy();
69 service.invokeWorkflow(wfDescJson);
70 ret.put(WF_ID, wfDescJson.path(WF_ID).asText());
71
72 return ok(ret).build();
73 } catch (JsonProcessingException e) {
74 log.error("Failed to get json ", e);
75 ret.put(WF_ID, "Failed to get Json " + e.getCause());
76 return Response.status(BAD_REQUEST)
77 .entity(ret).build();
78 } catch (IOException e) {
79 log.error("Failed to get request body ", e);
80 ret.put(WF_ID, "Failed to get request body " + e.getCause());
81 return Response.status(INTERNAL_SERVER_ERROR)
82 .entity(ret).build();
83 } catch (WorkflowException e) {
84 log.error("Failed to invoke workflow ", e);
85 ret.put(WF_ID, "Failed to invoke workflow " + e.getCause());
86 return Response.status(INTERNAL_SERVER_ERROR)
87 .entity(ret).build();
88
89 }
90 }
91
92}