gyewan.an | ecb3269 | 2019-07-10 14:36:58 +0900 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.workflow.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.onosproject.rest.AbstractWebResource; |
| 23 | import org.onosproject.workflow.api.WorkflowException; |
| 24 | import org.onosproject.workflow.api.WorkflowService; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import javax.ws.rs.Consumes; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.Produces; |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | |
| 37 | import static org.onlab.util.Tools.readTreeFromStream; |
| 38 | import static javax.ws.rs.core.Response.Status.BAD_REQUEST; |
| 39 | import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; |
| 40 | import static org.onosproject.workflow.api.WorkflowDescription.WF_ID; |
| 41 | import static org.onosproject.workflow.impl.WorkflowNetConfigListener.WORKFLOW_INVOKE; |
| 42 | |
| 43 | @Path("workflow") |
| 44 | public 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 | } |