Add REST API for invoking workflow.
Currently, there is no way to invoking using REST API.
I've added REST API.
json data example:
{
"op": "workflow.invoke",
"params": {
"workplace": "Nova-000",
"id": "of-overlay.workflow-nova",
"data": {
"mgmtIp": "192.168.10.8",
"ovsdbPort": 6641,
"sshAccessInfo": {
"remoteIp": "192.168.10.8",
"port": 22,
"user": "root",
"password": "iloveyou",
"keyfile": "~/.ssh/id_rsa"
},
"ovsDatapathType": "system",
"physicalPorts": [
"nova0_1"
],
"vtepIp": "120.0.0.200/24",
"annotations": {
"rackId": 1,
"rackPosition": 3
}
}
}
}
Change-Id: I2ce3bc5a59ce1cbe91c8c6f83bd1f25e81933ce3
diff --git a/apps/workflow/app/BUILD b/apps/workflow/app/BUILD
index d5af5a2..a0339da 100644
--- a/apps/workflow/app/BUILD
+++ b/apps/workflow/app/BUILD
@@ -1,10 +1,17 @@
-COMPILE_DEPS = CORE_DEPS + KRYO + JACKSON + CLI + [
+COMPILE_DEPS = CORE_DEPS + KRYO + JACKSON + CLI + REST + [
+ "@jersey_server//jar",
"//core/store/serializers:onos-core-serializers",
"//core/store/primitives:onos-core-primitives",
"//apps/workflow/api:onos-apps-workflow-api",
]
-osgi_jar(
+osgi_jar_with_tests(
+ api_description = "REST API for workflow",
+ api_package = "org.onosproject.workflow.rest",
+ api_title = "Workflow REST API",
+ api_version = "1.0",
karaf_command_packages = ["org.onosproject.workflow.cli"],
+ test_deps = TEST_ADAPTERS,
+ web_context = "/onos/workflow",
deps = COMPILE_DEPS,
)
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowNetConfigListener.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowNetConfigListener.java
index bb7f875..86eccfa 100644
--- a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowNetConfigListener.java
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowNetConfigListener.java
@@ -44,6 +44,11 @@
public static final String EXECUTOR_GROUPNAME = "onos/workflow-netcfg";
public static final String EXECUTOR_PATTERN = "netcfg-event-handler";
+ public static final String WORKPLACE_CREATE = "workplace.create";
+ public static final String WORKPLACE_REMOVE = "workplace.remove";
+ public static final String WORKFLOW_INVOKE = "workflow.invoke";
+ public static final String WORKFLOW_TERMINATE = " workflow.terminate";
+
private final ConfigFactory<ApplicationId, WorkflowNetConfig> configFactory =
new ConfigFactory<ApplicationId, WorkflowNetConfig>(
SubjectFactories.APP_SUBJECT_FACTORY, WorkflowNetConfig.class, CONFIG_KEY) {
@@ -128,13 +133,13 @@
private static Map<String, RpcCall> rpcMap = new HashMap<>();
static {
- rpcMap.put("workplace.create",
+ rpcMap.put(WORKPLACE_CREATE,
(service, desc) -> service.createWorkplace(DefaultWorkplaceDescription.valueOf(desc.params())));
- rpcMap.put("workplace.remove",
+ rpcMap.put(WORKPLACE_REMOVE,
(service, desc) -> service.removeWorkplace(DefaultWorkplaceDescription.valueOf(desc.params())));
- rpcMap.put("workflow.invoke",
+ rpcMap.put(WORKFLOW_INVOKE,
(service, desc) -> service.invokeWorkflow(desc.params()));
- rpcMap.put("workflow.terminate",
+ rpcMap.put(WORKFLOW_TERMINATE,
(service, desc) -> service.terminateWorkflow(DefaultWorkflowDescription.valueOf(desc.params())));
}
}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/rest/WorkflowRestApi.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/rest/WorkflowRestApi.java
new file mode 100644
index 0000000..fc66e06
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/rest/WorkflowRestApi.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.rest;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.rest.AbstractWebResource;
+import org.onosproject.workflow.api.WorkflowException;
+import org.onosproject.workflow.api.WorkflowService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.onlab.util.Tools.readTreeFromStream;
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
+import static org.onosproject.workflow.api.WorkflowDescription.WF_ID;
+import static org.onosproject.workflow.impl.WorkflowNetConfigListener.WORKFLOW_INVOKE;
+
+@Path("workflow")
+public class WorkflowRestApi extends AbstractWebResource {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+ private WorkflowService service = get(WorkflowService.class);
+ private static final String OPERATION = "op";
+ private static final String PARAMS = "params";
+
+
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("/invoke")
+ public Response invoke(InputStream stream) {
+ ObjectNode ret = JsonNodeFactory.instance.objectNode();
+ try {
+ ObjectNode payload = readTreeFromStream(mapper(), stream);
+ String operation = payload.path(OPERATION).asText();
+
+ if (!(WORKFLOW_INVOKE.equals(operation))) {
+ log.error("Operation is not matched");
+ return Response.status(BAD_REQUEST)
+ .entity("Operation is not matched!").build();
+ }
+
+ JsonNode wfDescJson = payload.path(PARAMS).deepCopy();
+ service.invokeWorkflow(wfDescJson);
+ ret.put(WF_ID, wfDescJson.path(WF_ID).asText());
+
+ return ok(ret).build();
+ } catch (JsonProcessingException e) {
+ log.error("Failed to get json ", e);
+ ret.put(WF_ID, "Failed to get Json " + e.getCause());
+ return Response.status(BAD_REQUEST)
+ .entity(ret).build();
+ } catch (IOException e) {
+ log.error("Failed to get request body ", e);
+ ret.put(WF_ID, "Failed to get request body " + e.getCause());
+ return Response.status(INTERNAL_SERVER_ERROR)
+ .entity(ret).build();
+ } catch (WorkflowException e) {
+ log.error("Failed to invoke workflow ", e);
+ ret.put(WF_ID, "Failed to invoke workflow " + e.getCause());
+ return Response.status(INTERNAL_SERVER_ERROR)
+ .entity(ret).build();
+
+ }
+ }
+
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/rest/WorkflowWebApplication.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/rest/WorkflowWebApplication.java
new file mode 100644
index 0000000..f15e313
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/rest/WorkflowWebApplication.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.workflow.rest;
+
+import org.onlab.rest.AbstractWebApplication;
+
+import java.util.Set;
+
+public class WorkflowWebApplication extends AbstractWebApplication {
+ @Override
+ public Set<Class<?>> getClasses() {
+ return getClasses(WorkflowRestApi.class);
+ }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/rest/package-info.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/rest/package-info.java
new file mode 100644
index 0000000..eebdc13
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/rest/package-info.java
@@ -0,0 +1,19 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * Ansible Executor Rest API package.
+ */
+package org.onosproject.workflow.rest;
\ No newline at end of file
diff --git a/apps/workflow/app/src/main/webapp/WEB-INF/web.xml b/apps/workflow/app/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000..f148fdd
--- /dev/null
+++ b/apps/workflow/app/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2019-present Open Networking Foundation
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ id="ONOS" version="2.5">
+ <display-name>Workflow REST API</display-name>
+
+ <security-constraint>
+ <web-resource-collection>
+ <web-resource-name>Secured</web-resource-name>
+ <url-pattern>/*</url-pattern>
+ </web-resource-collection>
+ <auth-constraint>
+ <role-name>admin</role-name>
+ <role-name>viewer</role-name>
+ </auth-constraint>
+ </security-constraint>
+
+ <security-role>
+ <role-name>admin</role-name>
+ <role-name>viewer</role-name>
+ </security-role>
+
+ <login-config>
+ <auth-method>BASIC</auth-method>
+ <realm-name>karaf</realm-name>
+ </login-config>
+
+ <servlet>
+ <servlet-name>JAX-RS Service</servlet-name>
+ <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
+ <init-param>
+ <param-name>javax.ws.rs.Application</param-name>
+ <param-value>org.onosproject.workflow.rest.WorkflowWebApplication</param-value>
+ </init-param>
+ <load-on-startup>1</load-on-startup>
+ </servlet>
+
+
+ <servlet-mapping>
+ <servlet-name>JAX-RS Service</servlet-name>
+ <url-pattern>/*</url-pattern>
+ </servlet-mapping>
+</web-app>