From 704247137bdba671e55c0ebde7e226fff422b608 Mon Sep 17 00:00:00 2001 From: talio Date: Sun, 18 Feb 2018 11:52:25 +0200 Subject: Change Tosca structure change Tosca Service Template according to Tosca-spec-1.1 Change-Id: I8f689b02e0349fc8c787868661f498d7d530d445 Issue-ID: SDC-1043 Signed-off-by: talio --- .../errors/CreateInterfaceObjectErrorBuilder.java | 24 ++++ ...CreateInterfaceOperationObjectErrorBuilder.java | 22 +++ .../sdc/tosca/errors/ToscaErrorCodes.java | 3 +- .../ToscaInvalidInterfaceValueErrorBuilder.java | 21 +++ .../sdc/tosca/services/DataModelUtil.java | 155 +++++++++++++++++++++ 5 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/CreateInterfaceObjectErrorBuilder.java create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/CreateInterfaceOperationObjectErrorBuilder.java create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/errors/ToscaInvalidInterfaceValueErrorBuilder.java (limited to 'openecomp-be/lib/openecomp-tosca-lib/src/main/java') 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 interfaceTypes = serviceTemplate.getInterface_types(); + if (MapUtils.isEmpty(interfaceTypes) + || Objects.isNull(interfaceTypes.get(interfaceId))) { + return; + } + + Object interfaceObject = interfaceTypes.get(interfaceId); + Map interfaceAsMap = CommonUtil.getObjectAsMap(interfaceObject); + interfaceAsMap.put(operationId, operationDefinition); + } + + public static Map getInterfaceTypes(ServiceTemplate serviceTemplate) { + Map interfaceTypes = serviceTemplate.getInterface_types(); + + if (MapUtils.isEmpty(interfaceTypes)) { + return new HashMap<>(); + } + + Map convertedInterfaceTypes = new HashMap<>(); + for (Map.Entry interfaceEntry : interfaceTypes.entrySet()) { + try { + Optional 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 convertObjToInterfaceDefinition( + String interfaceId, Object interfaceObj) + throws CoreException { + + try { + Optional 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 convertInterfaceDefinitionToObj( + InterfaceDefinition interfaceDefinition) { + return converInetrfaceToToscaInterfaceObj(interfaceDefinition); + } + + public static Optional convertObjToInterfaceType(String interfaceId, + Object interfaceObj) + throws CoreException { + try { + Optional 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 convertInterfaceTypeToObj(InterfaceType interfaceType) { + return converInetrfaceToToscaInterfaceObj(interfaceType); + } + + private static Optional converInetrfaceToToscaInterfaceObj(Object interfaceEntity) { + if (Objects.isNull(interfaceEntity)) { + return Optional.empty(); + } + + Map interfaceAsMap = CommonUtil.getObjectAsMap(interfaceEntity); + Map operations = (Map) 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 interfaceAsMap, + InterfaceType interfaceType) { + + Set fieldNames = CommonUtil.getClassFieldNames(InterfaceType.class); + + for (Map.Entry entry : interfaceAsMap.entrySet()) { + Optional operationDefinition = + createOperation(entry.getKey(), entry.getValue(), fieldNames); + operationDefinition + .ifPresent(operation -> interfaceType.addOperation(entry.getKey(), operation)); + } + } + + private static Optional createOperation(String propertyName, + Object operationCandidate, + Set 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 interfaceAsMap, + InterfaceDefinition interfaceDefinition) { + + Set fieldNames = CommonUtil.getClassFieldNames(InterfaceDefinition.class); + + for (Map.Entry entry : interfaceAsMap.entrySet()) { + Optional operationDefinition = + createOperation(entry.getKey(), entry.getValue(), fieldNames); + operationDefinition + .ifPresent(operation -> interfaceDefinition.addOperation(entry.getKey(), operation)); + + } + } + public static void addSubstitutionNodeTypeRequirements(NodeType substitutionNodeType, List> requirementsList, -- cgit 1.2.3-korg