Worklow Operation output tosca 67/55467/2
authorojasdubey <ojas.dubey@amdocs.com>
Wed, 27 Jun 2018 09:29:48 +0000 (14:59 +0530)
committerOren Kleks <orenkle@amdocs.com>
Sun, 15 Jul 2018 10:35:19 +0000 (10:35 +0000)
Added implementation to export workflow
operation outputs to TOSCA template

Change-Id: Id4c7104f725f7e9f5c97ae738387d2592bb80f7b
Issue-ID: SDC-1450
Signed-off-by: ojasdubey <ojas.dubey@amdocs.com>
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaAttribute.java [new file with mode: 0644]
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaLifecycleOperationDefinition.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaNodeType.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtil.java
catalog-be/src/test/java/org/openecomp/sdc/be/datamodel/utils/InterfaceUIDataConverterTest.java
catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtilTest.java

index 8d8eac3..4455c32 100644 (file)
@@ -577,10 +577,10 @@ public class ToscaExportHandler {
             inputDef.forEach(i -> {
                 ToscaProperty property = propertyConvertor.convertProperty(dataTypes, i, false);
                 inputs.put(i.getName(), property);
-                addInterfaceDefinitionElement(component, toscaNodeType);
             });
             if (!inputs.isEmpty()) {
                 toscaNodeType.setProperties(inputs);
+                addInterfaceDefinitionElement(component, toscaNodeType);
             }
         }
 
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaAttribute.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaAttribute.java
new file mode 100644 (file)
index 0000000..c32eed9
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * Copyright © 2016-2018 European Support Limited
+ *
+ * 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.openecomp.sdc.be.tosca.model;
+
+public class ToscaAttribute {
+
+    private String type;
+    private String description;
+    private Object _defaultp_;
+    private String status;
+    private EntrySchema entry_schema;
+
+    public ToscaAttribute() {
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public Object getDefaultp() {
+        return _defaultp_;
+    }
+
+    public void setDefaultp(Object defaultp) {
+        this._defaultp_ = defaultp;
+    }
+
+    public String getStatus() {
+        return status;
+    }
+
+    public void setStatus(String status) {
+        this.status = status;
+    }
+
+    public EntrySchema getEntry_schema() {
+        return entry_schema;
+    }
+
+    public void setEntry_schema(EntrySchema entry_schema) {
+        this.entry_schema = entry_schema;
+    }
+}
index f801ac0..3317778 100644 (file)
@@ -19,16 +19,12 @@ package org.openecomp.sdc.be.tosca.model;
 import java.util.Map;
 import java.util.Objects;
 
-/**
- * @author KATYR
- * @since March 26, 2018
- */
-
 public class ToscaLifecycleOperationDefinition {
 
     private String description;
     private String implementation;
     private Map<String, ToscaProperty> inputs;
+    private Map<String, ToscaAttribute> outputs;
 
 
     public String getImplementation() {
@@ -47,6 +43,13 @@ public class ToscaLifecycleOperationDefinition {
         this.inputs = inputs;
     }
 
+    public Map<String, ToscaAttribute> getOutputs() {
+        return outputs;
+    }
+
+    public void setOutputs(Map<String, ToscaAttribute> outputs) {
+        this.outputs = outputs;
+    }
 
     @Override
     public boolean equals(Object o) {
@@ -57,13 +60,13 @@ public class ToscaLifecycleOperationDefinition {
             return false;
         }
         ToscaLifecycleOperationDefinition that = (ToscaLifecycleOperationDefinition) o;
-        return Objects.equals(implementation, that.implementation) && Objects.equals(inputs, that.inputs);
+        return Objects.equals(implementation, that.implementation) && Objects.equals(inputs, that.inputs)
+                && Objects.equals(outputs, that.outputs);
     }
 
     @Override
     public int hashCode() {
-
-        return Objects.hash(implementation, inputs);
+        return Objects.hash(implementation, inputs, outputs);
     }
 
     public String getDescription() {
index 0d0cfb2..b6763ad 100644 (file)
@@ -33,6 +33,7 @@ public class ToscaNodeType {
     private String description;
 
     private Map<String, ToscaProperty> properties;
+    private Map<String, ToscaAttribute> attributes;
     private Map<String, Object> interfaces; //ToscaInterfaceDefinition
     private Map<String, ToscaCapability> capabilities;
 
@@ -46,6 +47,14 @@ public class ToscaNodeType {
         this.properties = properties;
     }
 
+    public Map<String, ToscaAttribute> getAttributes() {
+        return attributes;
+    }
+
+    public void setAttributes(Map<String, ToscaAttribute> attributes) {
+        this.attributes = attributes;
+    }
+
     public Map<String, ToscaCapability> getCapabilities() {
         return capabilities;
     }
index 8c9c63a..81b5c99 100644 (file)
 
 package org.openecomp.sdc.be.tosca.utils;
 
+import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.databind.ObjectMapper;
+
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+
 import org.apache.commons.collections.MapUtils;
 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
+import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
 import org.openecomp.sdc.be.model.Component;
 import org.openecomp.sdc.be.model.InterfaceDefinition;
 import org.openecomp.sdc.be.model.Product;
 import org.openecomp.sdc.be.model.Resource;
 import org.openecomp.sdc.be.model.Service;
+import org.openecomp.sdc.be.tosca.model.ToscaAttribute;
 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType;
 import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition;
 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
 
-/**
- * @author KATYR
- * @since March 20, 2018
- */
 
 public class InterfacesOperationsToscaUtil {
 
@@ -50,8 +51,10 @@ public class InterfacesOperationsToscaUtil {
     private static final String DEFAULT_HAS_UNDERSCORE = "_default";
     private static final String DOT = ".";
     private static final String DEFAULT_INPUT_TYPE = "string";
+    private static final String DEFAULT_OUTPUT_TYPE = "string";
     private static final String SELF = "SELF";
     private static final String GET_PROPERTY = "get_property";
+    private static final String GET_OPERATION_OUTPUT = "get_operation_output";
     private static final String DEFAULTP = "defaultp";
 
     private InterfacesOperationsToscaUtil() {
@@ -120,7 +123,7 @@ public class InterfacesOperationsToscaUtil {
             toscaInterfaceDefinition.setType(toscaResourceName);
             final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
             Map<String, Object> toscaOperations = new HashMap<>();
-
+            String interfaceName = getLastPartOfName(toscaResourceName);
             String operationArtifactPath;
             for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
                 ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition();
@@ -132,8 +135,8 @@ public class InterfacesOperationsToscaUtil {
                     toscaOperation.setImplementation(operationArtifactPath);
                 }
                 toscaOperation.setDescription(operationEntry.getValue().getDescription());
-                fillToscaOperationInputs(operationEntry.getValue(), toscaOperation);
-
+                fillToscaOperationInputs(operationEntry.getValue(), toscaOperation, nodeType);
+                fillToscaOperationOutputs(operationEntry.getValue(), toscaOperation, interfaceName, nodeType);
                 toscaOperations.put(operationEntry.getValue().getName(), toscaOperation);
             }
 
@@ -179,7 +182,8 @@ public class InterfacesOperationsToscaUtil {
     }
 
     private static void fillToscaOperationInputs(OperationDataDefinition operation,
-            ToscaLifecycleOperationDefinition toscaOperation) {
+                                                 ToscaLifecycleOperationDefinition toscaOperation,
+                                                 ToscaNodeType nodeType) {
         if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) {
             toscaOperation.setInputs(null);
             return;
@@ -189,15 +193,55 @@ public class InterfacesOperationsToscaUtil {
         for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) {
             ToscaProperty toscaInput = new ToscaProperty();
             toscaInput.setDescription(input.getDescription());
-            toscaInput.setType(DEFAULT_INPUT_TYPE);
-
-            toscaInput.setDefaultp(createDefaultValue(getLastPartOfName(input.getInputId())));
+            String mappedPropertyName = getLastPartOfName(input.getInputId());
+            toscaInput.setType(getOperationInputType(mappedPropertyName, nodeType));
+            toscaInput.setDefaultp(createDefaultValue(mappedPropertyName));
             toscaInputs.put(input.getName(), toscaInput);
         }
 
         toscaOperation.setInputs(toscaInputs);
     }
 
+    private static void fillToscaOperationOutputs(OperationDataDefinition operation,
+                                                 ToscaLifecycleOperationDefinition toscaOperation,
+                                                 String interfaceName,
+                                                 ToscaNodeType nodeType) {
+        if (Objects.isNull(operation.getOutputs()) || operation.getOutputs().isEmpty()) {
+            toscaOperation.setOutputs(null);
+            return;
+        }
+        Map<String, ToscaAttribute> toscaOutputs = new HashMap<>();
+        for (OperationOutputDefinition output : operation.getOutputs().getListToscaDataDefinition()) {
+            if (Objects.nonNull(output.getInputId())) {
+                ToscaAttribute toscaOutput = new ToscaAttribute();
+                toscaOutput.setDescription(output.getDescription());
+                String outputName = output.getName();
+                String mappedAttributeName = getLastPartOfName(output.getInputId());
+                toscaOutput.setType(getOperationOutputType(mappedAttributeName, nodeType));
+                toscaOutputs.put(outputName, toscaOutput);
+                createDefaultValueForMappedAttribute(nodeType, mappedAttributeName, outputName, interfaceName,
+                        operation.getName());
+            }
+        }
+        toscaOperation.setOutputs(toscaOutputs);
+    }
+
+    private static String getOperationInputType(String inputName, ToscaNodeType nodeType) {
+        if (nodeType.getProperties() != null
+                &&  nodeType.getProperties().containsKey(inputName)) {
+            return nodeType.getProperties().get(inputName).getType();
+        }
+        return DEFAULT_INPUT_TYPE;
+    }
+
+    private static String getOperationOutputType(String inputName, ToscaNodeType nodeType) {
+        if (nodeType.getProperties() != null
+                &&  nodeType.getProperties().containsKey(inputName)) {
+            return nodeType.getProperties().get(inputName).getType();
+        }
+        return DEFAULT_OUTPUT_TYPE;
+    }
+
     private static Map<String, List<String>> createDefaultValue(String propertyName) {
         Map<String, List<String>> getPropertyMap = new HashMap<>();
         List<String> values = new ArrayList<>();
@@ -208,10 +252,41 @@ public class InterfacesOperationsToscaUtil {
         return getPropertyMap;
     }
 
+    private static Map<String, List<String>> createAttributeDefaultValue(String outputName,
+                                                                         String interfaceName,
+                                                                         String operationName) {
+        Map<String, List<String>> attributeDefaultValue = new HashMap<>();
+        List<String> values = new ArrayList<>();
+        values.add(SELF);
+        values.add(interfaceName);
+        values.add(operationName);
+        values.add(outputName);
+        attributeDefaultValue.put(GET_OPERATION_OUTPUT, values);
+        return attributeDefaultValue;
+    }
+
+    private static void createDefaultValueForMappedAttribute(ToscaNodeType nodeType, String mappedAttributeName,
+                                                      String outputName, String interfaceName, String operationName) {
+        if (Objects.isNull(nodeType.getAttributes())) {
+            nodeType.setAttributes(new HashMap<>());
+        }
+        if (!nodeType.getAttributes().containsKey(mappedAttributeName)) {
+            ToscaAttribute toscaAttribute = new ToscaAttribute();
+            toscaAttribute.setType(getOperationOutputType(mappedAttributeName, nodeType));
+            nodeType.getAttributes().put(mappedAttributeName, toscaAttribute);
+        }
+        nodeType.getAttributes().get(mappedAttributeName)
+                .setDefaultp(createAttributeDefaultValue(outputName, interfaceName, operationName));
+    }
 
     private static Map<String, Object> getObjectAsMap(Object obj) {
+        ObjectMapper objectMapper = new ObjectMapper();
+        if (obj instanceof ToscaInterfaceDefinition) {
+            //Prevent empty field serialization in interface definition
+            objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+        }
         Map<String, Object> objectAsMap =
-                obj instanceof Map ? (Map<String, Object>) obj : new ObjectMapper().convertValue(obj, Map.class);
+                obj instanceof Map ? (Map<String, Object>) obj : objectMapper.convertValue(obj, Map.class);
 
         if (objectAsMap.containsKey(DEFAULT)) {
             Object defaultValue = objectAsMap.get(DEFAULT);
index b158ddf..7e8dde9 100644 (file)
@@ -1,15 +1,10 @@
 package org.openecomp.sdc.be.datamodel.utils;
 
-import static org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields.IO_INPUT_PARAMETERS;
-
-import java.util.LinkedList;
-
 import org.junit.Test;
 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.InterfaceOperationDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
-import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
 import org.openecomp.sdc.be.model.Operation;
 
 public class InterfaceUIDataConverterTest {
@@ -28,9 +23,8 @@ public class InterfaceUIDataConverterTest {
                Operation operationData = new Operation();
                InterfaceOperationDataDefinition result;
                ListDataDefinition<OperationInputDefinition> inputs = new ListDataDefinition<>();
-               ListDataDefinition<OperationOutputDefinition> outputs = new ListDataDefinition<>();
                operationData.setInputs(inputs);
-               operationData.setOutputs(outputs);
+               operationData.setOutputs(new ListDataDefinition<>());
                operationData.setImplementation(new ArtifactDataDefinition());
                // default test
                result = InterfaceUIDataConverter.convertOperationDataToInterfaceData(operationData);
index 67b27f6..0c5db04 100644 (file)
@@ -27,6 +27,7 @@ import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
+import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
 import org.openecomp.sdc.be.model.Component;
 import org.openecomp.sdc.be.model.InterfaceDefinition;
 import org.openecomp.sdc.be.model.Resource;
@@ -35,11 +36,6 @@ import org.openecomp.sdc.be.tosca.ToscaRepresentation;
 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
 
-/**
- * @author KATYR
- * @since April 12, 2018
- */
-
 public class InterfacesOperationsToscaUtilTest {
 
     @BeforeClass
@@ -54,7 +50,7 @@ public class InterfacesOperationsToscaUtilTest {
         component.setNormalizedName("normalizedComponentName");
         InterfaceDefinition addedInterface = new InterfaceDefinition();
         addedInterface.setToscaResourceName("interface.types.test_resource_name");
-        addOperationsToInterface(addedInterface, 5, 3, true);
+        addOperationsToInterface(addedInterface, 5, 3, 0, true, false);
         final String interfaceType = "normalizedComponentName-interface";
         ((Resource) component).setInterfaces(new HashMap<>());
         ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
@@ -77,7 +73,7 @@ public class InterfacesOperationsToscaUtilTest {
         InterfaceDefinition addedInterface = new InterfaceDefinition();
         addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
 
-        addOperationsToInterface(addedInterface, 3, 2, true);
+        addOperationsToInterface(addedInterface, 3, 2, 0, true, false);
         final String interfaceType = "normalizedComponentName-interface";
         ((Resource) component).setInterfaces(new HashMap<>());
         ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
@@ -107,7 +103,7 @@ public class InterfacesOperationsToscaUtilTest {
         InterfaceDefinition addedInterface = new InterfaceDefinition();
         addedInterface.setToscaResourceName("com.some.resource.or.other.resourceNameNoInputs");
 
-        addOperationsToInterface(addedInterface, 3, 3, false);
+        addOperationsToInterface(addedInterface, 3, 3, 0, false, false);
         final String interfaceType = "normalizedComponentName-interface";
         ((Resource) component).setInterfaces(new HashMap<>());
         ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
@@ -129,9 +125,38 @@ public class InterfacesOperationsToscaUtilTest {
         Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
     }
 
+    @Test
+    public void addInterfaceDefinitionElementWithOutputs() {
+        Component component = new Resource();
+        component.setNormalizedName("normalizedComponentName");
+        InterfaceDefinition addedInterface = new InterfaceDefinition();
+        addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName");
+
+        addOperationsToInterface(addedInterface, 2, 0, 2, false, true);
+        final String interfaceType = "normalizedComponentName-interface";
+        ((Resource) component).setInterfaces(new HashMap<>());
+        ((Resource) component).getInterfaces().put(interfaceType, addedInterface);
+        ToscaNodeType nodeType = new ToscaNodeType();
+        InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType);
+
+        ToscaExportHandler handler = new ToscaExportHandler();
+        ToscaTemplate template = new ToscaTemplate("test");
+        Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
+        nodeTypes.put("test", nodeType);
+        template.setNode_types(nodeTypes);
+        final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template);
+
+        Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations"));
+        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceName:"));
+        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("outputs:"));
+        Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp"));
+        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description"));
+        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("naming_function_"));
+        Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName"));
+    }
 
     private void addOperationsToInterface(InterfaceDefinition addedInterface, int numOfOps, int numOfInputsPerOp,
-            boolean hasInputs) {
+                                          int numOfOutputsPerOp, boolean hasInputs, boolean hasOutputs) {
 
         addedInterface.setOperations(new HashMap<>());
         for (int i = 0; i < numOfOps; i++) {
@@ -144,6 +169,9 @@ public class InterfacesOperationsToscaUtilTest {
             if (hasInputs) {
                 operation.setInputs(createInputs(numOfInputsPerOp));
             }
+            if (hasOutputs) {
+                operation.setOutputs(createOutputs(numOfOutputsPerOp));
+            }
             addedInterface.getOperations().put(operation.getName(), operation);
         }
     }
@@ -158,6 +186,18 @@ public class InterfacesOperationsToscaUtilTest {
         return operationInputDefinitionList;
     }
 
+    private ListDataDefinition<OperationOutputDefinition> createOutputs(int numOfOutputs) {
+        ListDataDefinition<OperationOutputDefinition> operationOutputDefinitionList = new ListDataDefinition<>();
+        for (int i = 0; i < numOfOutputs; i++) {
+            operationOutputDefinitionList.add(createMockOperationOutputDefinition("output_" + i,
+                    java.util.UUID.randomUUID().toString() + "." + "naming_function_" + i, getTestOutputType(i)));
+        }
+        return operationOutputDefinitionList;
+    }
+
+    private String getTestOutputType(int i) {
+        return i%2 == 0 ? "integer" : "boolean";
+    }
 
     private OperationInputDefinition createMockOperationInputDefinition(String name, String id) {
         OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
@@ -165,4 +205,13 @@ public class InterfacesOperationsToscaUtilTest {
         operationInputDefinition.setInputId(id);
         return operationInputDefinition;
     }
+
+    private OperationOutputDefinition createMockOperationOutputDefinition(String name, String id, String type) {
+        OperationOutputDefinition operationOutputDefinition = new OperationOutputDefinition();
+        operationOutputDefinition.setName(name);
+        operationOutputDefinition.setInputId(id);
+        operationOutputDefinition.setType(type);
+        return operationOutputDefinition;
+    }
+
 }