SDN Blueprints Processor Input Resource Assignment 51/64551/2
authorSingal, Kapil (ks220y) <ks220y@att.com>
Wed, 5 Sep 2018 01:31:10 +0000 (21:31 -0400)
committerSingal, Kapil (ks220y) <ks220y@att.com>
Wed, 5 Sep 2018 01:35:06 +0000 (21:35 -0400)
Creating SDN Controller Blueprints Input Resource Assignment Processor

Change-Id: I64aec06cf09797177598c2348778e1c73dae3b5b
Issue-ID: CCSDK-500
Signed-off-by: Singal, Kapil (ks220y) <ks220y@att.com>
blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/processor/InputResourceProcessor.java [new file with mode: 0644]
blueprints-processor/plugin/assignment-provider/src/test/java/org/onap/ccsdk/config/assignment/processor/InputResourceProcessorTest.java [new file with mode: 0644]
blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/dt-location.json [new file with mode: 0644]
blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/input-complex.json [new file with mode: 0644]
blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/input-simple.json [new file with mode: 0644]
blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/resource-assignments-complex.json [new file with mode: 0644]
blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/resource-assignments-simple.json [new file with mode: 0644]
blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/sample-location.json [new file with mode: 0644]

diff --git a/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/processor/InputResourceProcessor.java b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/processor/InputResourceProcessor.java
new file mode 100644 (file)
index 0000000..e32e7af
--- /dev/null
@@ -0,0 +1,96 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * Modifications Copyright © 2018 IBM.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.assignment.processor;\r
+\r
+import java.util.List;\r
+import java.util.Map;\r
+import org.apache.commons.lang3.StringUtils;\r
+import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService;\r
+import org.onap.ccsdk.config.model.ConfigModelConstant;\r
+import org.onap.ccsdk.config.model.ConfigModelException;\r
+import org.onap.ccsdk.config.model.data.ResourceAssignment;\r
+import org.onap.ccsdk.config.model.service.ComponentNode;\r
+import org.onap.ccsdk.config.model.utils.ResourceAssignmentUtils;\r
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
+import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
+\r
+public class InputResourceProcessor implements ComponentNode {\r
+\r
+    public InputResourceProcessor(ConfigResourceService configResourceService) {}\r
+\r
+    @Override\r
+    public Boolean preCondition(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
+            throws SvcLogicException {\r
+        return Boolean.TRUE;\r
+    }\r
+\r
+    @Override\r
+    public void preProcess(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
+            throws SvcLogicException {\r
+        // Auto-generated method stub\r
+    }\r
+\r
+    @Override\r
+    public void process(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {\r
+        // Auto-generated method stub\r
+    }\r
+\r
+    @SuppressWarnings("unchecked")\r
+    @Override\r
+    public void process(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
+            throws SvcLogicException {\r
+        try {\r
+            List<ResourceAssignment> batchResourceAssignment =\r
+                    (List<ResourceAssignment>) componentContext.get(ConfigModelConstant.PROPERTY_RESOURCE_ASSIGNMENTS);\r
+            if (batchResourceAssignment != null && !batchResourceAssignment.isEmpty()) {\r
+                for (ResourceAssignment resourceAssignment : batchResourceAssignment) {\r
+                    processResourceAssignment(ctx, componentContext, resourceAssignment);\r
+                }\r
+            }\r
+        } catch (Exception e) {\r
+            throw new SvcLogicException(String.format("InputResourceProcessor Exception : (%s)", e), e);\r
+        }\r
+    }\r
+\r
+    private void processResourceAssignment(SvcLogicContext ctx, Map<String, Object> componentContext,\r
+            ResourceAssignment resourceAssignment) throws ConfigModelException, SvcLogicException {\r
+        try {\r
+            if (StringUtils.isNotBlank(resourceAssignment.getName())) {\r
+                String value = ctx.getAttribute(resourceAssignment.getName());\r
+                // if value is null don't call setResourceDataValue to populate the value\r
+                if (StringUtils.isNotBlank(value)) {\r
+                    ResourceAssignmentUtils.setResourceDataValue(componentContext, resourceAssignment, value);\r
+                }\r
+            }\r
+\r
+            // Check the value has populated for mandatory case\r
+            ResourceAssignmentUtils.assertTemplateKeyValueNotNull(componentContext, resourceAssignment);\r
+        } catch (Exception e) {\r
+            ResourceAssignmentUtils.setFailedResourceDataValue(componentContext, resourceAssignment, e.getMessage());\r
+            throw new SvcLogicException(\r
+                    String.format("Failed in template key (%s) assignments with : (%s)", resourceAssignment, e), e);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public void postProcess(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
+            throws SvcLogicException {\r
+        // Auto-generated method stub\r
+    }\r
+\r
+}\r
diff --git a/blueprints-processor/plugin/assignment-provider/src/test/java/org/onap/ccsdk/config/assignment/processor/InputResourceProcessorTest.java b/blueprints-processor/plugin/assignment-provider/src/test/java/org/onap/ccsdk/config/assignment/processor/InputResourceProcessorTest.java
new file mode 100644 (file)
index 0000000..2aabdb3
--- /dev/null
@@ -0,0 +1,178 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * Modifications Copyright © 2018 IBM.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.assignment.processor;\r
+\r
+import static org.mockito.Matchers.any;\r
+import java.io.File;\r
+import java.nio.charset.Charset;\r
+import java.util.Arrays;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import org.apache.commons.io.FileUtils;\r
+import org.junit.Assert;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+import org.mockito.Mock;\r
+import org.mockito.Mockito;\r
+import org.mockito.MockitoAnnotations;\r
+import org.mockito.invocation.InvocationOnMock;\r
+import org.mockito.runners.MockitoJUnitRunner;\r
+import org.mockito.stubbing.Answer;\r
+import org.onap.ccsdk.config.assignment.service.ConfigResourceAssignmentTestUtils;\r
+import org.onap.ccsdk.config.data.adaptor.domain.TransactionLog;\r
+import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService;\r
+import org.onap.ccsdk.config.model.ConfigModelConstant;\r
+import org.onap.ccsdk.config.model.data.ResourceAssignment;\r
+import org.onap.ccsdk.config.model.data.dict.ResourceDefinition;\r
+import org.onap.ccsdk.config.model.utils.TransformationUtils;\r
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
+import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
+\r
+@RunWith(MockitoJUnitRunner.class)\r
+public class InputResourceProcessorTest {\r
+\r
+    private static EELFLogger logger = EELFManager.getInstance().getLogger(InputResourceProcessorTest.class);\r
+\r
+    @Mock\r
+    private ConfigResourceService configResourceService;\r
+\r
+    @SuppressWarnings("unchecked")\r
+    @Before\r
+    public void before() {\r
+        MockitoAnnotations.initMocks(this);\r
+\r
+        try {\r
+            Mockito.doAnswer(new Answer<Void>() {\r
+                @Override\r
+                public Void answer(InvocationOnMock invocationOnMock) throws Throwable {\r
+                    Object[] args = invocationOnMock.getArguments();\r
+                    if (args != null) {\r
+                        logger.trace("Transaction info " + Arrays.asList(args));\r
+                    }\r
+                    return null;\r
+                }\r
+            }).when(configResourceService).save(any(TransactionLog.class));\r
+\r
+        } catch (SvcLogicException e) {\r
+            e.printStackTrace();\r
+        }\r
+    }\r
+\r
+    @Test\r
+    public void testInputSimpleProcess() throws Exception {\r
+        logger.info(" *******************************  testInputSimpleProcess  ***************************");\r
+\r
+        String recipeName = "sample-recipe";\r
+\r
+        String resourceassignmentContent = FileUtils.readFileToString(\r
+                new File("src/test/resources/mapping/input/resource-assignments-simple.json"),\r
+                Charset.defaultCharset());\r
+        List<ResourceAssignment> batchResourceAssignment =\r
+                TransformationUtils.getListfromJson(resourceassignmentContent, ResourceAssignment.class);\r
+\r
+        String dictionaryContent = FileUtils.readFileToString(\r
+                new File("src/test/resources/mapping/default/default-simple.json"), Charset.defaultCharset());\r
+        Map<String, ResourceDefinition> dictionaries =\r
+                ConfigResourceAssignmentTestUtils.getMapfromJson(dictionaryContent);\r
+\r
+        InputResourceProcessor inputResourceProcessor = new InputResourceProcessor(configResourceService);\r
+        Map<String, Object> componentContext = new HashMap<>();\r
+        componentContext.put(ConfigModelConstant.PROPERTY_RESOURCE_ASSIGNMENTS, batchResourceAssignment);\r
+        componentContext.put(ConfigModelConstant.PROPERTY_ACTION_NAME, recipeName);\r
+        componentContext.put(ConfigModelConstant.PROPERTY_TEMPLATE_NAME, "sample-template");\r
+        componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARIES, dictionaries);\r
+        componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + recipeName + ".profile_name", "sample");\r
+\r
+        Map<String, String> inParams = new HashMap<>();\r
+        SvcLogicContext ctx = new SvcLogicContext();\r
+        ctx.setAttribute("country", "US");\r
+        ctx.setAttribute("port", "830");\r
+        ctx.setAttribute("voip-enabled", "true");\r
+\r
+        inputResourceProcessor.process(inParams, ctx, componentContext);\r
+        logger.trace(" componentContext " + componentContext);\r
+\r
+        Assert.assertEquals("Failed to populate default recipe country value ", "US",\r
+                componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.country"));\r
+        Assert.assertEquals("Failed to populate default dictionary country value ", "US",\r
+                componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.country"));\r
+\r
+        Assert.assertEquals("Failed to populate default recipe port value ", 830,\r
+                componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.port"));\r
+        Assert.assertEquals("Failed to populate default dictionary port value ", 830,\r
+                componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.port"));\r
+\r
+        Assert.assertEquals("Failed to populate default recipe voip-enabled value ", true,\r
+                componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.voip-enabled"));\r
+        Assert.assertEquals("Failed to populate default dictionary voip-enabled value ", true,\r
+                componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.voip-enabled"));\r
+\r
+    }\r
+\r
+    @Test\r
+    public void testInputComplexProcess() throws Exception {\r
+        logger.info(" *******************************  testInputComplexProcess  ***************************");\r
+\r
+        String recipeName = "sample-recipe";\r
+\r
+        String resourceassignmentContent = FileUtils.readFileToString(\r
+                new File("src/test/resources/mapping/input/resource-assignments-complex.json"),\r
+                Charset.defaultCharset());\r
+        List<ResourceAssignment> batchResourceAssignment =\r
+                TransformationUtils.getListfromJson(resourceassignmentContent, ResourceAssignment.class);\r
+\r
+        String dictionaryContent = FileUtils.readFileToString(\r
+                new File("src/test/resources/mapping/input/input-complex.json"), Charset.defaultCharset());\r
+        Map<String, ResourceDefinition> dictionaries =\r
+                ConfigResourceAssignmentTestUtils.getMapfromJson(dictionaryContent);\r
+\r
+        InputResourceProcessor inputResourceProcessor = new InputResourceProcessor(configResourceService);\r
+        Map<String, Object> componentContext = new HashMap<>();\r
+        componentContext.put(ConfigModelConstant.PROPERTY_RESOURCE_ASSIGNMENTS, batchResourceAssignment);\r
+        componentContext.put(ConfigModelConstant.PROPERTY_ACTION_NAME, recipeName);\r
+        componentContext.put(ConfigModelConstant.PROPERTY_TEMPLATE_NAME, "sample-template");\r
+        componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARIES, dictionaries);\r
+        componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + recipeName + ".profile_name", "sample");\r
+\r
+        Map<String, String> inParams = new HashMap<>();\r
+        SvcLogicContext ctx = new SvcLogicContext();\r
+        String datatypeContent = FileUtils.readFileToString(\r
+                new File("src/test/resources/mapping/input/dt-location.json"), Charset.defaultCharset());\r
+        ctx.setAttribute("data_types.dt-location", datatypeContent);\r
+\r
+        String samplelocation = FileUtils.readFileToString(\r
+                new File("src/test/resources/mapping/input/sample-location.json"), Charset.defaultCharset());\r
+        ctx.setAttribute("location", samplelocation);\r
+\r
+        inputResourceProcessor.process(inParams, ctx, componentContext);\r
+\r
+        logger.trace(" componentContext " + componentContext);\r
+        logger.trace(" Resource Mapping " + TransformationUtils.getJson(batchResourceAssignment, true));\r
+\r
+        Assert.assertNotNull("Failed to populate input recipe location value ",\r
+                componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.location"));\r
+        Assert.assertNotNull("Failed to populate input dictionary location  value ",\r
+                componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.location"));\r
+\r
+    }\r
+\r
+}\r
diff --git a/blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/dt-location.json b/blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/dt-location.json
new file mode 100644 (file)
index 0000000..52e0a79
--- /dev/null
@@ -0,0 +1,15 @@
+{\r
+       "version": "1.0.0",\r
+       "description": "test Data Type",\r
+       "properties": {\r
+               "country": {\r
+                       "required": true,\r
+                       "type": "string"\r
+               },\r
+               "state": {\r
+                       "required": false,\r
+                       "type": "string"\r
+               }\r
+       },\r
+       "derived_from": "tosca.datatypes.Root"\r
+}\r
diff --git a/blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/input-complex.json b/blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/input-complex.json
new file mode 100644 (file)
index 0000000..7eabe44
--- /dev/null
@@ -0,0 +1,24 @@
+{\r
+       "location": {\r
+               "name": "location",\r
+               "property": {\r
+                       "type": "dt-location"\r
+               },\r
+               "sources": {\r
+                       "default": {\r
+                               "type": "source-input"\r
+                       }\r
+               }\r
+       },\r
+       "profile_name": {\r
+               "name": "profile_name",\r
+               "property": {\r
+                       "type": "string"\r
+               },\r
+               "sources": {\r
+                       "default": {\r
+                               "type": "source-input"\r
+                       }\r
+               }\r
+       }\r
+}\r
diff --git a/blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/input-simple.json b/blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/input-simple.json
new file mode 100644 (file)
index 0000000..7b663f3
--- /dev/null
@@ -0,0 +1,35 @@
+{\r
+       "country": {\r
+               "name": "country",\r
+               "property": {\r
+                       "type": "string"\r
+               },\r
+               "sources": {\r
+                       "default": {\r
+                               "type": "source-input"\r
+                       }\r
+               }\r
+       },\r
+       "port": {\r
+               "name": "port",\r
+               "property": {\r
+                       "type": "integer"\r
+               },\r
+               "sources": {\r
+                       "default": {\r
+                               "type": "source-input"\r
+                       }\r
+               }\r
+       },\r
+       "voip-enabled": {\r
+               "name": "voip-enabled",\r
+               "property": {\r
+                       "type": "boolean"\r
+               },\r
+               "sources": {\r
+                       "default": {\r
+                               "type": "source-input"\r
+                       }\r
+               }\r
+       }\r
+}\r
diff --git a/blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/resource-assignments-complex.json b/blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/resource-assignments-complex.json
new file mode 100644 (file)
index 0000000..9e17841
--- /dev/null
@@ -0,0 +1,13 @@
+[\r
+       {\r
+               "name": "location",\r
+               "input-param": true,\r
+               "property": {\r
+                       "type": "dt-location",\r
+                       "required": true\r
+               },\r
+               "dictionary-name": "location",\r
+               "dictionary-source": "input",\r
+               "dependencies": []\r
+       }\r
+]\r
diff --git a/blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/resource-assignments-simple.json b/blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/resource-assignments-simple.json
new file mode 100644 (file)
index 0000000..e3b2bcc
--- /dev/null
@@ -0,0 +1,32 @@
+[\r
+       {\r
+               "name": "country",\r
+               "input-param": true,\r
+               "property": {\r
+                       "type": "string",\r
+                       "default": "US"\r
+               },\r
+               "dictionary-source": "input",\r
+               "dependencies": []\r
+       },\r
+       {\r
+               "name": "port",\r
+               "input-param": true,\r
+               "property": {\r
+                       "type": "integer",\r
+                       "default": 830\r
+               },\r
+               "dictionary-source": "input",\r
+               "dependencies": []\r
+       },\r
+       {\r
+               "name": "voip-enabled",\r
+               "input-param": true,\r
+               "property": {\r
+                       "type": "boolean",\r
+                       "default": true\r
+               },\r
+               "dictionary-source": "input",\r
+               "dependencies": []\r
+       }\r
+]\r
diff --git a/blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/sample-location.json b/blueprints-processor/plugin/assignment-provider/src/test/resources/mapping/input/sample-location.json
new file mode 100644 (file)
index 0000000..2f3f1c1
--- /dev/null
@@ -0,0 +1,4 @@
+{\r
+       "country": "US",\r
+       "state": "NJ"\r
+}\r