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/src/main | |
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/src/main')
5 files changed, 224 insertions, 1 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, |