diff options
author | talio <tali.orenbach@amdocs.com> | 2018-02-18 11:52:25 +0200 |
---|---|---|
committer | Vitaly Emporopulo <Vitaliy.Emporopulo@amdocs.com> | 2018-03-15 09:14:51 +0000 |
commit | 704247137bdba671e55c0ebde7e226fff422b608 (patch) | |
tree | ad4124c20d60e37917376ebca977ac8f04cb2ec8 /openecomp-be/lib/openecomp-tosca-lib | |
parent | 9c40f569b817e0886d95ad8b1ac235d8644873da (diff) |
Change Tosca structure
change Tosca Service Template according to Tosca-spec-1.1
Change-Id: I8f689b02e0349fc8c787868661f498d7d530d445
Issue-ID: SDC-1043
Signed-off-by: talio <tali.orenbach@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-lib')
10 files changed, 722 insertions, 22 deletions
diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/CreateInterfaceObjectErrorBuilder.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/CreateInterfaceObjectErrorBuilder.java new file mode 100644 index 0000000000..9eb2a4d5b2 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/CreateInterfaceObjectErrorBuilder.java @@ -0,0 +1,24 @@ +package org.openecomp.sdc.tosca.errors; + +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; + +public class CreateInterfaceObjectErrorBuilder { + + private static final String COULD_NOT_CREATE_OBJECT_MSG = + "Could not create %s from %s. Reason - %s"; + private final ErrorCode.ErrorCodeBuilder builder = new ErrorCode.ErrorCodeBuilder(); + + public CreateInterfaceObjectErrorBuilder(String interfaceClassName, + String interfaceId, + String reason) { + builder.withId(ToscaErrorCodes.INVALID_INTERFACE_VALUE); + builder.withCategory(ErrorCategory.APPLICATION); + builder.withMessage( + String.format(COULD_NOT_CREATE_OBJECT_MSG, interfaceClassName, interfaceId, reason)); + } + + public ErrorCode build() { + return builder.build(); + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/CreateInterfaceOperationObjectErrorBuilder.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/CreateInterfaceOperationObjectErrorBuilder.java new file mode 100644 index 0000000000..83ead4fe66 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/CreateInterfaceOperationObjectErrorBuilder.java @@ -0,0 +1,22 @@ +package org.openecomp.sdc.tosca.errors; + +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; + +public class CreateInterfaceOperationObjectErrorBuilder { + private static final String COULD_NOT_CREATE_OBJECT_MSG = + "Could not create Operation from %s. Reason - %s"; + private final ErrorCode.ErrorCodeBuilder builder = new ErrorCode.ErrorCodeBuilder(); + + public CreateInterfaceOperationObjectErrorBuilder(String operationId, + String reason) { + builder.withId(ToscaErrorCodes.INVALID_INTERFACE_VALUE); + builder.withCategory(ErrorCategory.APPLICATION); + builder.withMessage( + String.format(COULD_NOT_CREATE_OBJECT_MSG, operationId, reason)); + } + + public ErrorCode build() { + return builder.build(); + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/ToscaErrorCodes.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/ToscaErrorCodes.java index 099f0771f2..2e449e4acc 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/ToscaErrorCodes.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/ToscaErrorCodes.java @@ -20,7 +20,7 @@ package org.openecomp.sdc.tosca.errors; -public class ToscaErrorCodes { +class ToscaErrorCodes { static final String INVALID_SUBSTITUTE_NODE_TEMPLATE = "INVALID_SUBSTITUTE_NODE_TEMPLATE"; static final String INVALID_SUBSTITUTION_SERVICE_TEMPLATE = @@ -31,6 +31,7 @@ public class ToscaErrorCodes { static final String TOSCA_INVALID_SUBSTITUTE_NODE_TEMPLATE = "TOSCA_INVALID_SUBSTITUTE_NODE_TEMPLATE"; static final String TOSCA_INVALID_ADD_ACTION_NULL_ENTITY = "TOSCA_INVALID_ADD_ACTION_NULL_ENTITY"; + static final String INVALID_INTERFACE_VALUE = "INVALID_INTERFACE_VALUE"; } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/ToscaInvalidInterfaceValueErrorBuilder.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/ToscaInvalidInterfaceValueErrorBuilder.java new file mode 100644 index 0000000000..09b36a083c --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/ToscaInvalidInterfaceValueErrorBuilder.java @@ -0,0 +1,21 @@ +package org.openecomp.sdc.tosca.errors; + +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; + +public class ToscaInvalidInterfaceValueErrorBuilder { + + private static final String INVALID_INTERFACE_MSG = + "Cannot create interface object. reason - %s"; + private final ErrorCode.ErrorCodeBuilder builder = new ErrorCode.ErrorCodeBuilder(); + + public ToscaInvalidInterfaceValueErrorBuilder(String reason) { + builder.withId(ToscaErrorCodes.INVALID_INTERFACE_VALUE); + builder.withCategory(ErrorCategory.APPLICATION); + builder.withMessage(String.format(INVALID_INTERFACE_MSG, reason)); + } + + public ErrorCode build() { + return builder.build(); + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java index 8da2fc8c54..aef23be03f 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java @@ -20,10 +20,13 @@ package org.openecomp.sdc.tosca.services; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.openecomp.core.utilities.CommonMethods; import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.utils.CommonUtil; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType; @@ -36,8 +39,11 @@ import org.openecomp.sdc.tosca.datatypes.model.Constraint; import org.openecomp.sdc.tosca.datatypes.model.EntrySchema; import org.openecomp.sdc.tosca.datatypes.model.GroupDefinition; import org.openecomp.sdc.tosca.datatypes.model.Import; +import org.openecomp.sdc.tosca.datatypes.model.InterfaceDefinition; +import org.openecomp.sdc.tosca.datatypes.model.InterfaceType; import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; import org.openecomp.sdc.tosca.datatypes.model.NodeType; +import org.openecomp.sdc.tosca.datatypes.model.OperationDefinition; import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition; import org.openecomp.sdc.tosca.datatypes.model.PolicyDefinition; import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition; @@ -49,8 +55,11 @@ import org.openecomp.sdc.tosca.datatypes.model.Status; import org.openecomp.sdc.tosca.datatypes.model.SubstitutionMapping; import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate; import org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt; +import org.openecomp.sdc.tosca.errors.CreateInterfaceObjectErrorBuilder; +import org.openecomp.sdc.tosca.errors.CreateInterfaceOperationObjectErrorBuilder; import org.openecomp.sdc.tosca.errors.InvalidAddActionNullEntityErrorBuilder; import org.openecomp.sdc.tosca.errors.InvalidRequirementAssignmentErrorBuilder; +import org.openecomp.sdc.tosca.errors.ToscaInvalidInterfaceValueErrorBuilder; import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl; import java.io.ByteArrayInputStream; @@ -67,6 +76,7 @@ import java.util.ListIterator; import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.Set; /** * The type Data model util. @@ -83,6 +93,7 @@ public class DataModelUtil { private static final Logger logger = LoggerFactory.getLogger(DataModelUtil.class); private static final String SERVICE_TEMPLATE = "Service Template"; private static final String NODE_TYPE = "Node Type"; + private static final String OPERATIONS = "operations"; /** * Add substitution mapping. @@ -1162,6 +1173,150 @@ public class DataModelUtil { return capabilityMapping; } + + public static void addInterfaceOperation(ServiceTemplate serviceTemplate, + String interfaceId, + String operationId, + OperationDefinition operationDefinition) { + Map<String, Object> interfaceTypes = serviceTemplate.getInterface_types(); + if (MapUtils.isEmpty(interfaceTypes) + || Objects.isNull(interfaceTypes.get(interfaceId))) { + return; + } + + Object interfaceObject = interfaceTypes.get(interfaceId); + Map<String, Object> interfaceAsMap = CommonUtil.getObjectAsMap(interfaceObject); + interfaceAsMap.put(operationId, operationDefinition); + } + + public static Map<String, InterfaceType> getInterfaceTypes(ServiceTemplate serviceTemplate) { + Map<String, Object> interfaceTypes = serviceTemplate.getInterface_types(); + + if (MapUtils.isEmpty(interfaceTypes)) { + return new HashMap<>(); + } + + Map<String, InterfaceType> convertedInterfaceTypes = new HashMap<>(); + for (Map.Entry<String, Object> interfaceEntry : interfaceTypes.entrySet()) { + try { + Optional<InterfaceType> interfaceType = + convertObjToInterfaceType(interfaceEntry.getKey(), interfaceEntry.getValue()); + interfaceType.ifPresent( + interfaceValue -> convertedInterfaceTypes.put(interfaceEntry.getKey(), interfaceValue)); + } catch (Exception e) { + throw new CoreException( + new ToscaInvalidInterfaceValueErrorBuilder(e.getMessage()).build()); + } + } + + return convertedInterfaceTypes; + } + + public static Optional<InterfaceDefinition> convertObjToInterfaceDefinition( + String interfaceId, Object interfaceObj) + throws CoreException { + + try { + Optional<InterfaceDefinition> interfaceDefinition = + CommonUtil.createObjectUsingSetters(interfaceObj, InterfaceDefinition.class); + interfaceDefinition.ifPresent(interfaceDefinition1 -> updateInterfaceDefinitionOperations( + CommonUtil.getObjectAsMap(interfaceObj), + interfaceDefinition1)); + return interfaceDefinition; + } catch (Exception ex) { + throw new CoreException( + new CreateInterfaceObjectErrorBuilder(InterfaceDefinition.class.getName(), interfaceId, + ex.getMessage()).build()); + } + + } + + public static Optional<Object> convertInterfaceDefinitionToObj( + InterfaceDefinition interfaceDefinition) { + return converInetrfaceToToscaInterfaceObj(interfaceDefinition); + } + + public static Optional<InterfaceType> convertObjToInterfaceType(String interfaceId, + Object interfaceObj) + throws CoreException { + try { + Optional<InterfaceType> interfaceType = + CommonUtil.createObjectUsingSetters(interfaceObj, InterfaceType.class); + interfaceType.ifPresent( + interfaceType1 -> updateInterfaceTypeOperations(CommonUtil.getObjectAsMap(interfaceObj), + interfaceType1)); + return interfaceType; + } catch (Exception ex) { + throw new CoreException( + new CreateInterfaceObjectErrorBuilder(InterfaceType.class.getName(), interfaceId, + ex.getMessage()).build()); + } + } + + public static Optional<Object> convertInterfaceTypeToObj(InterfaceType interfaceType) { + return converInetrfaceToToscaInterfaceObj(interfaceType); + } + + private static Optional<Object> converInetrfaceToToscaInterfaceObj(Object interfaceEntity) { + if (Objects.isNull(interfaceEntity)) { + return Optional.empty(); + } + + Map<String, Object> interfaceAsMap = CommonUtil.getObjectAsMap(interfaceEntity); + Map<String, Object> operations = (Map<String, Object>) interfaceAsMap.get(OPERATIONS); + if (MapUtils.isNotEmpty(operations)) { + interfaceAsMap.remove(OPERATIONS); + interfaceAsMap.putAll(operations); + } + + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); + return Optional.of(objectMapper.convertValue(interfaceAsMap, Object.class)); + } + + private static void updateInterfaceTypeOperations(Map<String, Object> interfaceAsMap, + InterfaceType interfaceType) { + + Set<String> fieldNames = CommonUtil.getClassFieldNames(InterfaceType.class); + + for (Map.Entry<String, Object> entry : interfaceAsMap.entrySet()) { + Optional<OperationDefinition> operationDefinition = + createOperation(entry.getKey(), entry.getValue(), fieldNames); + operationDefinition + .ifPresent(operation -> interfaceType.addOperation(entry.getKey(), operation)); + } + } + + private static Optional<OperationDefinition> createOperation(String propertyName, + Object operationCandidate, + Set<String> fieldNames) + throws CoreException { + if (!fieldNames.contains(propertyName)) { + try { + return CommonUtil.createObjectUsingSetters(operationCandidate, OperationDefinition.class); + } catch (Exception ex) { + throw new CoreException( + new CreateInterfaceOperationObjectErrorBuilder(propertyName, ex.getMessage()).build()); + } + } + + return Optional.empty(); + } + + private static void updateInterfaceDefinitionOperations(Map<String, Object> interfaceAsMap, + InterfaceDefinition interfaceDefinition) { + + Set<String> fieldNames = CommonUtil.getClassFieldNames(InterfaceDefinition.class); + + for (Map.Entry<String, Object> entry : interfaceAsMap.entrySet()) { + Optional<OperationDefinition> operationDefinition = + createOperation(entry.getKey(), entry.getValue(), fieldNames); + operationDefinition + .ifPresent(operation -> interfaceDefinition.addOperation(entry.getKey(), operation)); + + } + } + public static void addSubstitutionNodeTypeRequirements(NodeType substitutionNodeType, List<Map<String, RequirementDefinition>> requirementsList, diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java index 60f59dbd81..31dc3ab035 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java @@ -29,9 +29,12 @@ import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition; import org.openecomp.sdc.tosca.datatypes.model.Constraint; import org.openecomp.sdc.tosca.datatypes.model.Directive; import org.openecomp.sdc.tosca.datatypes.model.Import; +import org.openecomp.sdc.tosca.datatypes.model.InterfaceDefinition; +import org.openecomp.sdc.tosca.datatypes.model.InterfaceType; import org.openecomp.sdc.tosca.datatypes.model.NodeFilter; import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; import org.openecomp.sdc.tosca.datatypes.model.NodeType; +import org.openecomp.sdc.tosca.datatypes.model.OperationDefinition; import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition; import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition; import org.openecomp.sdc.tosca.datatypes.model.PropertyType; @@ -52,13 +55,23 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; public class ToscaModelTest { + private YamlUtil yamlUtil = new YamlUtil(); + private static final String INTERFACE_ID = "inter_1"; + private static final String NODE_TEMPLATE_ID = "firstNodeTemplate"; + private static final String BASE_DIR = "/mock/model"; + private static final String ST = "/serviceTemplate.yaml"; + private static final String ST_WITH_INTERFACE = "/serviceTemplateWithInterface.yaml"; + private static final String ST_WITH_OPERATIONS = "/serviceTemplateWithInterfaceAndOperation.yaml"; + private static final String ST_WITH_INTERFACE_DEF = + "/serviceTemplateWithNodeTemplateInterface.yaml"; + @Test public void testServiceTemplateJavaToYaml() { - ServiceTemplate serviceTemplate = new ServiceTemplate(); Map<String, String> metadata = new HashMap<>(); metadata.put("Template_author", "OPENECOMP"); @@ -80,6 +93,14 @@ public class ToscaModelTest { imports.add(importsMap); serviceTemplate.setImports(imports); + OperationDefinition operationDefinition = new OperationDefinition(); + operationDefinition.setDescription("test operation"); + InterfaceType interfaceType = new InterfaceType(); + interfaceType.setDerived_from("derived_from"); + interfaceType.setDescription("desc"); + interfaceType.addOperation("test", operationDefinition); + serviceTemplate.addInterfaceType("test_interface", interfaceType); + ArtifactType artifact = new ArtifactType(); artifact.setMime_type("application/java-archive"); ArrayList<String> ext = new ArrayList<>(); @@ -272,30 +293,163 @@ public class ToscaModelTest { @Test public void testYamlToServiceTemplateObj() throws IOException { - try (InputStream yamlFile = new YamlUtil().loadYamlFileIs("/mock/model/serviceTemplate.yaml")) { - ServiceTemplate serviceTemplateFromYaml = - new YamlUtil().yamlToObject(yamlFile, ServiceTemplate.class); - Assert.assertNotNull(serviceTemplateFromYaml); - } + ServiceTemplate serviceTemplateFromYaml = + getServiceTemplate(BASE_DIR + ST); + Assert.assertNotNull(serviceTemplateFromYaml); } + @Test + public void testYamlWithInterfaceToServiceTemplateObj() throws IOException { + ServiceTemplate serviceTemplateWithOperation = + getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS); + Assert.assertNotNull(serviceTemplateWithOperation); + + InterfaceType expectedInterfaceType = getInterfaceType(); + + Map<String, InterfaceType> interfaceTypes = + DataModelUtil.getInterfaceTypes(serviceTemplateWithOperation); + Assert.assertEquals(1, interfaceTypes.size()); + InterfaceType actualInterfaceType = interfaceTypes.get(INTERFACE_ID); + Assert.assertEquals(expectedInterfaceType, actualInterfaceType); + } + + @Test + public void testAddOperationToInterface() throws IOException { + YamlUtil yamlUtil = new YamlUtil(); + ServiceTemplate serviceTemplateWithInterface = + getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE); + ServiceTemplate serviceTemplateWithOperation = + getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS); + + OperationDefinition operationDefinition = getOperationDefinition(); + + DataModelUtil + .addInterfaceOperation(serviceTemplateWithInterface, INTERFACE_ID, "start", + operationDefinition); + String expectedServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithOperation); + String actualServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithInterface); + Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate); + } + + @Test + public void testInterfaceTypeToObjConversion() throws IOException { + ServiceTemplate serviceTemplateWithInterface = + getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE); + ServiceTemplate serviceTemplateWithOperation = + getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS); + InterfaceType interfaceType = getInterfaceType(); + + Optional<Object> interfaceAsObj = DataModelUtil.convertInterfaceTypeToObj(interfaceType); + Assert.assertTrue(interfaceAsObj.isPresent()); + + Map<String, Object> interfaceTypes = new HashMap<>(); + interfaceTypes.put(INTERFACE_ID, interfaceAsObj.get()); + serviceTemplateWithInterface.setInterface_types(interfaceTypes); + + String expectedServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithOperation); + String actualServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithInterface); + Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate); + } + + @Test + public void testObjToInterfaceTypeConversion() throws IOException, ReflectiveOperationException { + ServiceTemplate serviceTemplateWithOperation = + getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS); + Map<String, Object> interfaceTypes = serviceTemplateWithOperation.getInterface_types(); + Object interfaceObj = interfaceTypes.get(INTERFACE_ID); + Optional<InterfaceType> actualInterfaceType = + DataModelUtil.convertObjToInterfaceType(INTERFACE_ID, interfaceObj); + + Assert.assertTrue(actualInterfaceType.isPresent()); + InterfaceType expectedInterfaceType = getInterfaceType(); + Assert.assertEquals(expectedInterfaceType, actualInterfaceType.get()); + } + + @Test + public void testInterfaceDefinitionToObjConversion() throws IOException { + ServiceTemplate serviceTemplate = + getServiceTemplate(BASE_DIR + ST); + ServiceTemplate serviceTemplateWithInterfaceDef = + getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE_DEF); + + NodeTemplate nodeTemplate = + DataModelUtil.getNodeTemplate(serviceTemplate, NODE_TEMPLATE_ID); + InterfaceDefinition interfaceDefinition = getInterfaceDefinition(); + Optional<Object> interfaceObj = DataModelUtil.convertInterfaceDefinitionToObj(interfaceDefinition); + + Assert.assertTrue(interfaceObj.isPresent()); + Map<String, Object> interfaces = new HashMap<>(); + interfaces.put(INTERFACE_ID, interfaceObj.get()); + nodeTemplate.setInterfaces(interfaces); + + String expectedServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithInterfaceDef); + String actualServiceTemplate = yamlUtil.objectToYaml(serviceTemplate); + Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate); + } + + @Test + public void testObjToInterfaceDefinitionConversion() + throws IOException, ReflectiveOperationException { + ServiceTemplate serviceTemplateWithInterfaceDef = + getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE_DEF); + NodeTemplate nodeTemplateWithInterface = + DataModelUtil.getNodeTemplate(serviceTemplateWithInterfaceDef, NODE_TEMPLATE_ID); + Map<String, Object> interfaces = nodeTemplateWithInterface.getInterfaces(); + Object interfaceObj = interfaces.get(INTERFACE_ID); + Optional<InterfaceDefinition> actualInterfaceDefinition = + DataModelUtil.convertObjToInterfaceDefinition(INTERFACE_ID, interfaceObj); + + Assert.assertTrue(actualInterfaceDefinition.isPresent()); + + InterfaceDefinition expectedInterfaceDefinition = getInterfaceDefinition(); + Assert.assertEquals(expectedInterfaceDefinition, actualInterfaceDefinition.get()); + } @Test public void testYamlToServiceTemplateIncludingHeatExtend() throws IOException { ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); try (InputStream yamlFile = - toscaExtensionYamlUtil.loadYamlFileIs("/mock/model/serviceTemplateHeatExtend.yaml")) { + toscaExtensionYamlUtil.loadYamlFileIs(BASE_DIR + "/serviceTemplateHeatExtend.yaml")) { ServiceTemplate serviceTemplateFromYaml = - toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); ParameterDefinitionExt parameterDefinitionExt = - (ParameterDefinitionExt) serviceTemplateFromYaml.getTopology_template().getInputs() - .get("inParam1"); + (ParameterDefinitionExt) serviceTemplateFromYaml.getTopology_template().getInputs() + .get("inParam1"); Assert.assertNotNull(parameterDefinitionExt.getLabel()); String backToYamlString = toscaExtensionYamlUtil.objectToYaml(serviceTemplateFromYaml); Assert.assertNotNull(backToYamlString); } } + private ServiceTemplate getServiceTemplate(String inputPath) throws IOException { + try (InputStream yamlFile = yamlUtil.loadYamlFileIs(inputPath)) { + return yamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + } + } + + private InterfaceType getInterfaceType() { + OperationDefinition operationDefinition = getOperationDefinition(); + InterfaceType interfaceType = new InterfaceType(); + interfaceType.setDescription("test interface"); + interfaceType.addOperation("start", operationDefinition); + return interfaceType; + } + + private OperationDefinition getOperationDefinition() { + OperationDefinition operationDefinition = new OperationDefinition(); + operationDefinition.setDescription("start operation"); + operationDefinition.setImplementation("start.sh"); + return operationDefinition; + } + + private InterfaceDefinition getInterfaceDefinition() { + OperationDefinition operationDefinition = getOperationDefinition(); + InterfaceDefinition interfaceDefinition = new InterfaceDefinition(); + interfaceDefinition.setType("test interface"); + interfaceDefinition.addOperation("start", operationDefinition); + return interfaceDefinition; + } + } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplate.yaml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplate.yaml index c49f30f97b..a2bc15ea19 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplate.yaml +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplate.yaml @@ -68,7 +68,7 @@ topology_template: entry_schema: type: tosca.myType node_templates: - firatNodeTemplate: + firstNodeTemplate: type: nodeTypeRef directives: - selectable @@ -98,16 +98,6 @@ topology_template: capability: capA node: nodeA relationship: relationB - capabilities: - cap1: - properties: - num_cpus: '{ get_input: cpus }' - attributes: - num_cpus: '66' - node_filter: - properties: - test1: - - equal: 1 MB substitution_mappings: node_type: myNodeType.node capabilities: diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithInterface.yaml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithInterface.yaml new file mode 100644 index 0000000000..46d61079a2 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithInterface.yaml @@ -0,0 +1,109 @@ +tosca_definitions_version: tosca_simple_yaml_1_0_0 +metadata: + template_name: Test + template_author: OPENECOMP + template_version: 1.0.0 +description: testing desc tosca service template +imports: +- myfile1: + file: path1/path2/file1.yaml +- myfile2: + file: path1/path2/file2.yaml +artifact_types: + one_artifact: + mime_type: application/java-archive + file_ext: + - yaml + - xml +interface_types: + inter_1: + description: test interface +node_types: + compute_node_type: + derived_from: tosca.nodes.Root + version: 1.0.0 + description: tosca compute test + properties: + cpu_num: + type: integer + description: Number of CPUs requested for a software node instance + required: true + default: 1 + status: SUPPORTED + constraints: + - greater_or_equal: 5.0 + - equal: 5 + - greater_than: 6.02 + - in_range: + - 0 + - UNBOUNDED + attributes: + attDef1: + type: string + default: hi + status: SUPPORTED + requirements: + - re1: + capability: tosca.cap1 + occurrences: + - 5 + - 1 + capabilities: + cap1: + type: tosca.cap + valid_source_types: + - node1 + - node2 + occurrences: + - 1 + - UNBOUNDED +topology_template: + description: topologi template descroption + inputs: + inParam1: + type: string + description: desc + required: false + default: my default val + constraints: + - greater_than: 6 + - greater_or_equal: 9 + entry_schema: + type: tosca.myType + node_templates: + firstNodeTemplate: + type: nodeTypeRef + directives: + - selectable + - substitutable + properties: + prop2: '{ get_input: my_mysql_rootpw }' + prop1: abcd + attributes: + att2: '{ get_input: my_mysql_rootpw }' + att1: att1Val + requirements: + - req1: + capability: capA + node: nodeA + relationship: relationB + node_filter: + properties: + propName1: + - greater_or_equal: 9 + propName2: + - min_length: 1 + - max_length: 2 + occurrences: + - 1 + - 2 + - req2: + capability: capA + node: nodeA + relationship: relationB + substitution_mappings: + node_type: myNodeType.node + capabilities: + database_endpoint: + - database + - database_endpoint diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithInterfaceAndOperation.yaml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithInterfaceAndOperation.yaml new file mode 100644 index 0000000000..90376b7205 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithInterfaceAndOperation.yaml @@ -0,0 +1,112 @@ +tosca_definitions_version: tosca_simple_yaml_1_0_0 +metadata: + template_name: Test + template_author: OPENECOMP + template_version: 1.0.0 +description: testing desc tosca service template +imports: +- myfile1: + file: path1/path2/file1.yaml +- myfile2: + file: path1/path2/file2.yaml +artifact_types: + one_artifact: + mime_type: application/java-archive + file_ext: + - yaml + - xml +interface_types: + inter_1: + description: test interface + start: + description: start operation + implementation: start.sh +node_types: + compute_node_type: + derived_from: tosca.nodes.Root + version: 1.0.0 + description: tosca compute test + properties: + cpu_num: + type: integer + description: Number of CPUs requested for a software node instance + required: true + default: 1 + status: SUPPORTED + constraints: + - greater_or_equal: 5.0 + - equal: 5 + - greater_than: 6.02 + - in_range: + - 0 + - UNBOUNDED + attributes: + attDef1: + type: string + default: hi + status: SUPPORTED + requirements: + - re1: + capability: tosca.cap1 + occurrences: + - 5 + - 1 + capabilities: + cap1: + type: tosca.cap + valid_source_types: + - node1 + - node2 + occurrences: + - 1 + - UNBOUNDED +topology_template: + description: topologi template descroption + inputs: + inParam1: + type: string + description: desc + required: false + default: my default val + constraints: + - greater_than: 6 + - greater_or_equal: 9 + entry_schema: + type: tosca.myType + node_templates: + firstNodeTemplate: + type: nodeTypeRef + directives: + - selectable + - substitutable + properties: + prop2: '{ get_input: my_mysql_rootpw }' + prop1: abcd + attributes: + att2: '{ get_input: my_mysql_rootpw }' + att1: att1Val + requirements: + - req1: + capability: capA + node: nodeA + relationship: relationB + node_filter: + properties: + propName1: + - greater_or_equal: 9 + propName2: + - min_length: 1 + - max_length: 2 + occurrences: + - 1 + - 2 + - req2: + capability: capA + node: nodeA + relationship: relationB + substitution_mappings: + node_type: myNodeType.node + capabilities: + database_endpoint: + - database + - database_endpoint diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithNodeTemplateInterface.yaml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithNodeTemplateInterface.yaml new file mode 100644 index 0000000000..6df071c570 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithNodeTemplateInterface.yaml @@ -0,0 +1,112 @@ +tosca_definitions_version: tosca_simple_yaml_1_0_0 +metadata: + template_name: Test + template_author: OPENECOMP + template_version: 1.0.0 +description: testing desc tosca service template +imports: +- myfile1: + file: path1/path2/file1.yaml +- myfile2: + file: path1/path2/file2.yaml +artifact_types: + one_artifact: + mime_type: application/java-archive + file_ext: + - yaml + - xml +node_types: + compute_node_type: + derived_from: tosca.nodes.Root + version: 1.0.0 + description: tosca compute test + properties: + cpu_num: + type: integer + description: Number of CPUs requested for a software node instance + required: true + default: 1 + status: SUPPORTED + constraints: + - greater_or_equal: 5.0 + - equal: 5 + - greater_than: 6.02 + - in_range: + - 0 + - UNBOUNDED + attributes: + attDef1: + type: string + default: hi + status: SUPPORTED + requirements: + - re1: + capability: tosca.cap1 + occurrences: + - 5 + - 1 + capabilities: + cap1: + type: tosca.cap + valid_source_types: + - node1 + - node2 + occurrences: + - 1 + - UNBOUNDED +topology_template: + description: topologi template descroption + inputs: + inParam1: + type: string + description: desc + required: false + default: my default val + constraints: + - greater_than: 6 + - greater_or_equal: 9 + entry_schema: + type: tosca.myType + node_templates: + firstNodeTemplate: + type: nodeTypeRef + directives: + - selectable + - substitutable + interfaces: + inter_1: + type: test interface + start: + description: start operation + implementation: start.sh + properties: + prop2: '{ get_input: my_mysql_rootpw }' + prop1: abcd + attributes: + att2: '{ get_input: my_mysql_rootpw }' + att1: att1Val + requirements: + - req1: + capability: capA + node: nodeA + relationship: relationB + node_filter: + properties: + propName1: + - greater_or_equal: 9 + propName2: + - min_length: 1 + - max_length: 2 + occurrences: + - 1 + - 2 + - req2: + capability: capA + node: nodeA + relationship: relationB + substitution_mappings: + node_type: myNodeType.node + capabilities: + database_endpoint: + - database + - database_endpoint |