Workflow register using json

Change-Id: I91efadcad6feecd69762b94cc188dc4a842b0344
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowConstants.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowConstants.java
new file mode 100644
index 0000000..f14cf89
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowConstants.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2024-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.api;
+
+public final class WorkflowConstants {
+
+    public static final String SLASH = "/";
+    public static final String PAYLOAD = "payload";
+    public static final String MSG = "msg";
+    public static final String EXTRA = "extra";
+    public static final String CHANGED = "changed";
+    public static final String ARCHIVED = "archived";
+    public static final String CANDIDATED = "candidated";
+    public static final String WORKFLOW_PARAMS = "workflowParams";
+    public static final long TIMEOUT_MS = 60000L;
+    public static final long DEFAULT_ADDITIONAL_TIMEOUT = 500L;
+    public static final String PARAMS = "params";
+    public static final String WF_ID = "id";
+    public static final String WORKPLACE_NAME = "workplaceName";
+    public static final String OPERATION = "op";
+    public static final String WORKFLOW_INVOKE = "workflow.invoke";
+    public static final String WORFLOWS = "workflows";
+    public static final String CONFIGURATION = "configuration";
+    public static final String CONFIGURATION_PATH = SLASH + CONFIGURATION;
+    public static final String VXLAN = "vxlan";
+    public static final String HOSTNAME = "hostname";
+    public static final String HOSTNAME_PATH = SLASH + HOSTNAME;
+    public static final String MODEL = "model";
+    public static final String MODEL_PATH = SLASH + MODEL;
+    public static final String VENDOR = "vendor";
+    public static final String JUNIPER_VENDOR = "JUNIPER";
+    public static final String UBIQUOSS_VENDOR = "UBIQUOSS";
+    public static final String VENDOR_PATH = SLASH + VENDOR;
+    public static final String WORKSPACE = "workspace";
+    public static final String WORKLETS = "worklets";
+    public static final String NAME = "name";
+
+
+
+    protected WorkflowConstants() {
+    }
+
+}
+
diff --git a/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowRegistrar.java b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowRegistrar.java
new file mode 100644
index 0000000..9eeebc4
--- /dev/null
+++ b/apps/workflow/api/src/main/java/org/onosproject/workflow/api/WorkflowRegistrar.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2024-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.api;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+/**
+ * Interface for workflow register.
+ */
+public interface WorkflowRegistrar {
+
+    /**
+     * Register workflow using json.
+     *
+     * @param jsonNode json node
+     */
+    void registerWorkflowUsingJson(JsonNode jsonNode);
+}
+
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowRegistrarImpl.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowRegistrarImpl.java
new file mode 100644
index 0000000..439f014
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/impl/WorkflowRegistrarImpl.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2024-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.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.apache.commons.lang.StringUtils;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.onosproject.workflow.api.ImmutableListWorkflow;
+import org.onosproject.workflow.api.WorkflowAttribute;
+import org.onosproject.workflow.api.WorkflowConstants;
+import org.onosproject.workflow.api.WorkflowStore;
+import org.onosproject.workflow.api.WorkflowRegistrar;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.URI;
+import java.util.Optional;
+import java.util.stream.StreamSupport;
+
+import static org.onosproject.workflow.api.WorkflowConstants.WORFLOWS;
+import static org.onosproject.workflow.api.WorkflowConstants.WORKLETS;
+import static org.onosproject.workflow.api.WorkflowConstants.NAME;
+
+
+@Component(immediate = true, service = WorkflowRegistrar.class)
+public class WorkflowRegistrarImpl implements WorkflowRegistrar {
+
+    private static final Logger log = LoggerFactory.getLogger(WorkflowRegistrarImpl.class);
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    private WorkflowStore workflowStore;
+
+    /**
+     * Register workflow using json.
+     *
+     * @param jsonNode json node
+     */
+    @Override
+    public void registerWorkflowUsingJson(JsonNode jsonNode) {
+        StreamSupport.stream(Optional.ofNullable(jsonNode.get(WORFLOWS))
+                .filter(JsonNode::isArray)
+                .orElseThrow(() ->
+                    new IllegalArgumentException("there is no " + WORFLOWS + " node")).spliterator(), false)
+            .forEach(workflow -> {
+                String id = Optional.ofNullable(workflow.get(WorkflowConstants.WF_ID))
+                        .orElseThrow(() ->
+                                new IllegalArgumentException("there is no " + WorkflowConstants.WF_ID))
+                        .asText();
+                ImmutableListWorkflow.Builder builder = new ImmutableListWorkflow.Builder()
+                        .id(URI.create(id))
+                        .attribute(WorkflowAttribute.REMOVE_AFTER_COMPLETE);
+
+                Optional.ofNullable(workflow.path(WORKLETS))
+                        .filter(JsonNode::isArray)
+                        .ifPresent(worklets -> StreamSupport.stream(worklets.spliterator(), false)
+                                .map(worklet -> worklet.path(NAME).asText())
+                                .filter(StringUtils::isNotEmpty)
+                                .forEach(builder::chain)
+                        );
+                try {
+                    workflowStore.register(builder.build());
+                } catch (Exception e) {
+                    log.error("registering invalid workflow. ", e);
+                }
+            }
+        );
+    }
+}
+