From 95ad93091d5fa6e377e76f312d495151bddc9da8 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Wed, 14 Oct 2020 20:56:11 +0100 Subject: Support interface input during import VFC Change-Id: I097ce258a7dadbedf1aff6ce53dd1286da074552 Issue-ID: SDC-3372 Signed-off-by: andre.schmid --- .../sdc/be/components/impl/ImportUtils.java | 51 +++-- .../impl/InterfaceDefinitionHandler.java | 255 +++++++++++++++++++++ .../be/components/impl/ResourceImportManager.java | 194 ++-------------- .../be/tosca/InterfacesOperationsConverter.java | 85 ++++--- .../be/tosca/model/ToscaInterfaceDefinition.java | 27 +-- 5 files changed, 369 insertions(+), 243 deletions(-) create mode 100644 catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceDefinitionHandler.java (limited to 'catalog-be/src/main/java') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java index 4127faa820..614b261b4a 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java @@ -47,6 +47,7 @@ import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentEx import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.elements.Annotation; +import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; @@ -348,24 +349,24 @@ public final class ImportUtils { } } - private static List getPropertyConstraints(Map propertyValue, - String propertyType) { - List propertyFieldConstraints = findCurrentLevelConstraintsElement(propertyValue); - if (CollectionUtils.isNotEmpty(propertyFieldConstraints)) { - List constraintList = new ArrayList<>(); - Type constraintType = new TypeToken() { - }.getType(); - Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()) - .create(); + private static List getPropertyConstraints(final Map propertyValue, + final String propertyType) { + final List propertyFieldConstraints = findCurrentLevelConstraintsElement(propertyValue); + if (CollectionUtils.isEmpty(propertyFieldConstraints)) { + return Collections.emptyList(); + } + final List constraintList = new ArrayList<>(); + final Type constraintType = new TypeToken() { + }.getType(); + final Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()) + .create(); - for (Object constraintJson : propertyFieldConstraints) { - PropertyConstraint propertyConstraint = validateAndGetPropertyConstraint(propertyType, constraintType, - gson, constraintJson); - constraintList.add(propertyConstraint); - } - return constraintList; + for (final Object constraintJson : propertyFieldConstraints) { + final PropertyConstraint propertyConstraint = validateAndGetPropertyConstraint(propertyType, constraintType, + gson, constraintJson); + constraintList.add(propertyConstraint); } - return null; + return constraintList; } private static List findCurrentLevelConstraintsElement(Map toscaJson) { @@ -485,10 +486,13 @@ public final class ImportUtils { return result; } - public static InputDefinition createModuleInput(Map inputValue, - AnnotationTypeOperations annotationTypeOperations) { + public static InputDefinition createModuleInput(final Map inputValue, + final AnnotationTypeOperations annotationTypeOperations) { + return parseAnnotationsAndAddItToInput(createModuleInput(inputValue), inputValue, annotationTypeOperations); + } - InputDefinition inputDef = new InputDefinition(); + public static InputDefinition createModuleInput(final Map inputValue) { + final InputDefinition inputDef = new InputDefinition(); setField(inputValue, TypeUtils.ToscaTagNamesEnum.TYPE, inputDef::setType); setFieldBoolean(inputValue, ToscaTagNamesEnum.REQUIRED, req -> inputDef.setRequired(Boolean.parseBoolean(req))); setField(inputValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, inputDef::setDescription); @@ -504,9 +508,7 @@ public final class ImportUtils { setScheme(inputValue, inputDef); setPropertyConstraints(inputValue, inputDef); - - return parseAnnotationsAndAddItToInput(inputDef, inputValue, annotationTypeOperations); - + return inputDef; } public static InputDefinition parseAnnotationsAndAddItToInput(InputDefinition inputDef, @@ -608,6 +610,11 @@ public final class ImportUtils { } + public static Either, ResultStatusEnum> getInputs(final Map toscaJson) { + return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS, ImportUtils::createInputs, + ImportUtils::createModuleInput); + } + public static Either, ResultStatusEnum> getElements(Map toscaJson, TypeUtils.ToscaTagNamesEnum elementTagName, Function elementGenByName, diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceDefinitionHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceDefinitionHandler.java new file mode 100644 index 0000000000..aeb4376c15 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceDefinitionHandler.java @@ -0,0 +1,255 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.components.impl; + +import static org.openecomp.sdc.be.components.impl.ImportUtils.Constants.QUOTE; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DEFAULT; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DESCRIPTION; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.IMPLEMENTATION; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.INPUTS; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.NOTIFICATIONS; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.OPERATIONS; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.REQUIRED; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.STATUS; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TYPE; + +import fj.data.Either; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.stream.Collectors; +import org.apache.commons.collections.MapUtils; +import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum; +import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.InputDataDefinition; +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.model.InputDefinition; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.tosca.utils.OperationArtifactUtil; +import org.openecomp.sdc.exception.ResponseFormat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +/** + * Handles interface definition TOSCA conversions + */ +@Component("interfaceDefinitionHandler") +public class InterfaceDefinitionHandler { + + private static final Logger LOGGER = LoggerFactory.getLogger(InterfaceDefinitionHandler.class); + + private final InterfaceOperationBusinessLogic interfaceOperationBusinessLogic; + + public InterfaceDefinitionHandler(final InterfaceOperationBusinessLogic interfaceOperationBusinessLogic) { + this.interfaceOperationBusinessLogic = interfaceOperationBusinessLogic; + } + + /** + * Creates an interface definition based on a TOSCA map representing an interface definition. + * + * @param interfaceDefinitionToscaMap the TOSCA interface definition structure + * @return an interface definition representation + */ + public InterfaceDefinition create(final Map interfaceDefinitionToscaMap) { + + final InterfaceDefinition interfaceDefinition = new InterfaceDefinition(); + + if (interfaceDefinitionToscaMap.containsKey(TYPE.getElementName())) { + final Object typeObj = interfaceDefinitionToscaMap.get(TYPE.getElementName()); + if (!(typeObj instanceof String)) { + throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML); + } + final String type = (String) typeObj; + interfaceDefinition.setType(type); + interfaceDefinition.setUniqueId(type.toLowerCase()); + } + + final Map inputDefinitionMap = handleInputs(interfaceDefinitionToscaMap); + if (!inputDefinitionMap.isEmpty()) { + final Map collect = inputDefinitionMap.entrySet().stream(). + collect(Collectors.toMap(Entry::getKey, value -> new InputDataDefinition(value.getValue()))); + interfaceDefinition.setInputs(collect); + } + final Map operationMap; + if (interfaceDefinitionToscaMap.containsKey(OPERATIONS.getElementName()) || interfaceDefinitionToscaMap + .containsKey(NOTIFICATIONS.getElementName())) { + operationMap = handleOperations(interfaceDefinitionToscaMap); + //TODO handle notifications + } else { + operationMap = handleLegacyOperations(interfaceDefinitionToscaMap); + } + if (!operationMap.isEmpty()) { + validateOperations(interfaceDefinition.getType(), operationMap); + interfaceDefinition.setOperations(operationMap); + } + + return interfaceDefinition; + } + + private void validateOperations(final String interfaceType, + final Map operationMap) { + if (MapUtils.isEmpty(operationMap)) { + return; + } + Either, ResponseFormat> interfaceDefinitionMapEither = + interfaceOperationBusinessLogic.getAllInterfaceLifecycleTypes(); + if (interfaceDefinitionMapEither.isRight() || MapUtils.isEmpty(interfaceDefinitionMapEither.left().value())) { + throw new ByActionStatusComponentException(ActionStatus.INTERFACE_UNKNOWN, interfaceType); + } + + final Map interfaceDefinitionMap = interfaceDefinitionMapEither.left().value(); + final Optional interfaceDefinitionOptional = interfaceDefinitionMap.entrySet().stream() + .filter(interfaceDefinitionEntry -> interfaceDefinitionEntry.getKey().equalsIgnoreCase(interfaceType)) + .map(Entry::getValue).findFirst(); + if (interfaceDefinitionOptional.isEmpty()) { + throw new ByActionStatusComponentException(ActionStatus.INTERFACE_UNKNOWN, interfaceType); + } + final InterfaceDefinition interfaceDefinition = interfaceDefinitionOptional.get(); + operationMap.keySet().forEach(operation1 -> { + if (!interfaceDefinition.hasOperation(operation1)) { + throw new ByActionStatusComponentException(ActionStatus.INTERFACE_OPERATION_NOT_DEFINED, + operation1, interfaceType); + } + }); + } + + private Map handleOperations( + final Map interfaceDefinitionToscaMap) { + if (!interfaceDefinitionToscaMap.containsKey(OPERATIONS.getElementName())) { + return Collections.emptyMap(); + } + final Map operationMap = + (Map) interfaceDefinitionToscaMap.get(OPERATIONS.getElementName()); + return operationMap.entrySet().stream() + .map(interfaceEntry -> createOperation(interfaceEntry.getKey(), + (Map) interfaceEntry.getValue())) + .collect( + Collectors.toMap(OperationDataDefinition::getName, operationDataDefinition -> operationDataDefinition)); + } + + private Map handleLegacyOperations( + final Map interfaceDefinitionToscaMap) { + final List notALegacyOperationEntry = Arrays + .asList(OPERATIONS.getElementName(), + TYPE.getElementName(), + INPUTS.getElementName(), + NOTIFICATIONS.getElementName()); + return interfaceDefinitionToscaMap.entrySet().stream() + .filter(interfaceEntry -> !notALegacyOperationEntry.contains(interfaceEntry.getKey())) + .map(interfaceEntry -> createOperation(interfaceEntry.getKey(), + (Map) interfaceEntry.getValue())) + .collect( + Collectors.toMap(OperationDataDefinition::getName, operationDataDefinition -> operationDataDefinition)); + } + + + private OperationDataDefinition createOperation(final String operationName, + final Map operationDefinitionMap) { + final OperationDataDefinition operation = new OperationDataDefinition(); + operation.setName(operationName); + + handleOperationImplementation(operationDefinitionMap).ifPresent(operation::setImplementation); + if (operationDefinitionMap.containsKey(INPUTS.getElementName())) { + final Map interfaceInputs = + (Map) operationDefinitionMap.get(INPUTS.getElementName()); + operation.setInputs(handleInterfaceOperationInputs(interfaceInputs)); + } + + return operation; + } + + private ListDataDefinition handleInterfaceOperationInputs( + final Map interfaceInputs) { + final ListDataDefinition inputs = new ListDataDefinition<>(); + for (final Entry interfaceInput : interfaceInputs.entrySet()) { + final OperationInputDefinition operationInput = new OperationInputDefinition(); + operationInput.setName(interfaceInput.getKey()); + if (interfaceInput.getValue() instanceof Map) { + final LinkedHashMap inputPropertyValue = + (LinkedHashMap) interfaceInput.getValue(); + LOGGER.info("createModuleInterface: i interfaceInput.getKey() {}, {} , {} ", + interfaceInput.getKey(), inputPropertyValue.keySet(), inputPropertyValue.values()); + if (inputPropertyValue.get(TYPE.getElementName()) != null) { + operationInput.setType(inputPropertyValue.get(TYPE.getElementName()).toString()); + } + if (inputPropertyValue.get(DESCRIPTION.getElementName()) != null) { + operationInput.setDescription(inputPropertyValue.get(DESCRIPTION.getElementName()).toString()); + } + if (inputPropertyValue.get(REQUIRED.getElementName()) != null) { + operationInput.setRequired( + Boolean.getBoolean(inputPropertyValue.get(REQUIRED.getElementName()).toString())); + } + if (inputPropertyValue.get(DEFAULT.getElementName()) != null) { + operationInput.setToscaDefaultValue(inputPropertyValue.get(DEFAULT.getElementName()).toString()); + } + if (inputPropertyValue.get(STATUS.getElementName()) != null) { + operationInput.setStatus(inputPropertyValue.get(STATUS.getElementName()).toString()); + } + + } else if (interfaceInput.getValue() instanceof String) { + final String value = (String) interfaceInput.getValue(); + operationInput.setDefaultValue(value); + operationInput.setToscaDefaultValue(value); + operationInput.setValue(value); + } + inputs.add(operationInput); + } + return inputs; + } + + private Optional handleOperationImplementation( + final Map operationDefinitionMap) { + if (!operationDefinitionMap.containsKey(IMPLEMENTATION.getElementName())) { + return Optional.empty(); + } + final ArtifactDataDefinition artifactDataDefinition = new ArtifactDataDefinition(); + final String artifactName = (String) operationDefinitionMap.get(IMPLEMENTATION.getElementName()); + if (OperationArtifactUtil.artifactNameIsALiteralValue(artifactName)) { + artifactDataDefinition.setArtifactName(artifactName); + } else { + artifactDataDefinition.setArtifactName(QUOTE + artifactName + QUOTE); + } + return Optional.of(artifactDataDefinition); + } + + private Map handleInputs(final Map interfaceDefinitionToscaMap) { + if (!interfaceDefinitionToscaMap.containsKey(INPUTS.getElementName())) { + return Collections.emptyMap(); + } + + final Either, ResultStatusEnum> inputMapEither = + ImportUtils.getInputs(interfaceDefinitionToscaMap); + if (inputMapEither.isRight()) { + return Collections.emptyMap(); + } + + return inputMapEither.left().value(); + } + +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java index 7ff916df32..b96e4e58e2 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java @@ -22,7 +22,6 @@ package org.openecomp.sdc.be.components.impl; -import static org.openecomp.sdc.be.components.impl.ImportUtils.Constants.QUOTE; import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataType; import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataTypeDefinitionWithName; import static org.openecomp.sdc.be.utils.TypeUtils.setField; @@ -34,7 +33,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -62,11 +60,7 @@ import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; -import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; -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.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; @@ -94,7 +88,6 @@ import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData; import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo; import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo; -import org.openecomp.sdc.be.tosca.utils.OperationArtifactUtil; import org.openecomp.sdc.be.utils.TypeUtils; import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; import org.openecomp.sdc.common.log.wrappers.Logger; @@ -112,13 +105,7 @@ public class ResourceImportManager { private static final Logger log = Logger.getLogger(ResourceImportManager.class); static final Pattern PROPERTY_NAME_PATTERN_IGNORE_LENGTH = Pattern.compile("[\\w\\-\\_\\d\\:]+"); - private static final String IMPLEMENTATION = "implementation"; - private static final String INPUTS = "inputs"; - private static final String TYPE = "type"; - private static final String DESCRIPTION = "description"; - private static final String REQUIRED = "required"; - private static final String DEFAULT = "default"; - private static final String STATUS = "status"; + private final InterfaceDefinitionHandler interfaceDefinitionHandler; private ServletContext servletContext; @@ -134,7 +121,6 @@ public class ResourceImportManager { @Autowired private ServiceBusinessLogic serviceBusinessLogic; - private InterfaceOperationBusinessLogic interfaceOperationBusinessLogic; private IGraphLockOperation graphLockOperation; private ToscaOperationFacade toscaOperationFacade; @@ -145,9 +131,11 @@ public class ResourceImportManager { private ResponseFormatManager responseFormatManager; @Autowired - public ResourceImportManager(ComponentsUtils componentsUtils, CapabilityTypeOperation capabilityTypeOperation) { + public ResourceImportManager(ComponentsUtils componentsUtils, CapabilityTypeOperation capabilityTypeOperation, + final InterfaceDefinitionHandler interfaceDefinitionHandler) { this.componentsUtils = componentsUtils; this.capabilityTypeOperation = capabilityTypeOperation; + this.interfaceDefinitionHandler = interfaceDefinitionHandler; } @Autowired @@ -380,11 +368,9 @@ public class ResourceImportManager { if (toscaInterfaces.isLeft()) { Map jsonInterfaces = toscaInterfaces.left().value(); Map moduleInterfaces = new HashMap<>(); - Iterator> interfacesNameValue = jsonInterfaces.entrySet().iterator(); - while (interfacesNameValue.hasNext()) { - Entry interfaceNameValue = interfacesNameValue.next(); - Either eitherInterface = createModuleInterface(interfaceNameValue - .getValue(), resource); + for (final Entry interfaceNameValue : jsonInterfaces.entrySet()) { + final Either eitherInterface = + createModuleInterface(interfaceNameValue.getValue()); if (eitherInterface.isRight()) { log.info("error when creating interface:{}, for resource:{}", interfaceNameValue.getKey(), resource.getName()); @@ -393,165 +379,32 @@ public class ResourceImportManager { } } - if (moduleInterfaces.size() > 0) { + if (!moduleInterfaces.isEmpty()) { resource.setInterfaces(moduleInterfaces); } } } - private Either createModuleInterface(Object interfaceJson, - Resource resource) { - final InterfaceDefinition interf = new InterfaceDefinition(); - Either result = Either.left(interf); - + private Either createModuleInterface(final Object interfaceJson) { try { if (interfaceJson instanceof String) { - final String requirementJsonString = (String) interfaceJson; - interf.setType(requirementJsonString); - } else if (interfaceJson instanceof Map && ResourceTypeEnum.VFC.equals(resource.getResourceType())) { - final Map requirementJsonMap = (Map) interfaceJson; - final Map operations = new HashMap<>(); - - for (final Entry entry : requirementJsonMap.entrySet()) { - if (entryIsInterfaceType(entry)) { - final String type = (String) requirementJsonMap - .get(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName()); - interf.setType(type); - interf.setUniqueId(type.toLowerCase()); - } else if (entryContainsImplementationForAKnownOperation(entry, interf.getType())) { - - final OperationDataDefinition operation = new OperationDataDefinition(); - operation.setName(entry.getKey()); - - final Map entryValue = (Map) entry.getValue(); - if (entryValue.containsKey(IMPLEMENTATION)) { - operation.setImplementation(handleOperationImplementation(entry)); - } - if (entryValue.containsKey(INPUTS)) { - final Map interfaceInputs = (Map) entryValue - .get(ToscaTagNamesEnum.INPUTS.getElementName()); - operation.setInputs(handleInterfaceInput(interfaceInputs)); - } - operations.put(entry.getKey(), operation); - } - } - if (!operations.isEmpty()) { - interf.setOperations(operations); - } - } else { - result = Either.right(ResultStatusEnum.GENERAL_ERROR); - } - - } catch (Exception e) { - BeEcompErrorManager.getInstance().logBeSystemError("Import Resource- create interface"); - log.debug("error when creating interface, message:{}", e.getMessage(), e); - result = Either.right(ResultStatusEnum.GENERAL_ERROR); - } - - return result; - } - - private ArtifactDataDefinition handleOperationImplementation(final Entry entry) { - final ArtifactDataDefinition implementation = new ArtifactDataDefinition(); - final String artifactName = ((Map) entry.getValue()).get(IMPLEMENTATION); - if (OperationArtifactUtil.artifactNameIsALiteralValue(artifactName)) { - implementation.setArtifactName(artifactName); - } else { - implementation.setArtifactName(QUOTE + artifactName + QUOTE); - } - return implementation; - } - - private ListDataDefinition handleInterfaceInput( - final Map interfaceInputs) { - final ListDataDefinition inputs = new ListDataDefinition<>(); - for (final Entry interfaceInput : interfaceInputs.entrySet()) { - final OperationInputDefinition operationInput = new OperationInputDefinition(); - operationInput.setName(interfaceInput.getKey()); - if (interfaceInput.getValue() instanceof Map) { - final LinkedHashMap inputPropertyValue = - (LinkedHashMap) interfaceInput.getValue(); - log.info("createModuleInterface: i interfaceInput.getKey() {}, {} , {} ", - interfaceInput.getKey(), inputPropertyValue.keySet(), - inputPropertyValue.values()); - if (inputPropertyValue.get(TYPE) != null) { - operationInput.setType(inputPropertyValue.get(TYPE).toString()); - } - if (inputPropertyValue.get(DESCRIPTION) != null) { - operationInput.setDescription(inputPropertyValue.get(DESCRIPTION).toString()); - } - if (inputPropertyValue.get(REQUIRED) != null) { - operationInput.setRequired( - Boolean.getBoolean(inputPropertyValue.get(REQUIRED).toString())); - } - if (inputPropertyValue.get(DEFAULT) != null) { - operationInput.setToscaDefaultValue(inputPropertyValue.get(DEFAULT).toString()); - } - if (inputPropertyValue.get(STATUS) != null) { - operationInput.setStatus(inputPropertyValue.get(STATUS).toString()); - } - - } else if (interfaceInput.getValue() instanceof String) { - final String value = (String) interfaceInput.getValue(); - operationInput.setDefaultValue(value); - operationInput.setToscaDefaultValue(value); - operationInput.setValue(value); + final InterfaceDefinition interfaceDefinition = new InterfaceDefinition(); + interfaceDefinition.setType((String) interfaceJson); + return Either.left(interfaceDefinition); } - inputs.getListToscaDataDefinition().add(operationInput); - inputs.add(operationInput); - } - return inputs; - } - private boolean entryIsInterfaceType(final Entry entry) { - if(entry.getKey().equals(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())) { - if (entry.getValue() instanceof String) { - return true; + if (interfaceJson instanceof Map) { + final Map interfaceJsonMap = (Map) interfaceJson; + final InterfaceDefinition interfaceDefinition = interfaceDefinitionHandler.create(interfaceJsonMap); + return Either.left(interfaceDefinition); } - throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML); - } - return false; - } - - private boolean entryContainsImplementationForAKnownOperation(final Entry entry, - final String interfaceType) { - if (entry.getValue() instanceof Map && ((Map)entry.getValue()).containsKey(IMPLEMENTATION)) { - if (isAKnownOperation(interfaceType, entry.getKey())){ - return true; - } - throw new ByActionStatusComponentException(ActionStatus.INTERFACE_OPERATION_NOT_FOUND); - } - return false; - } - - private boolean isAKnownOperation(String interfaceType, String operation) { - Either, ResponseFormat> interfaceLifecycleTypes = interfaceOperationBusinessLogic - .getAllInterfaceLifecycleTypes(); - if (interfaceLifecycleTypes.isRight() || interfaceLifecycleTypes.left().value() == null) { - return false; - } - for (Entry interfaceLifecycleType : interfaceLifecycleTypes.left().value() - .entrySet()) { - if (interfaceTypeAndOperationMatches(interfaceLifecycleType, interfaceType, operation)) { - return true; - } - } - return false; - } - - private boolean interfaceTypeAndOperationMatches(Entry interfaceLifecycleType, - String interfaceType, String operation) { - if (interfaceLifecycleType.getKey().equalsIgnoreCase(interfaceType) - && interfaceLifecycleType.getValue().getOperations() != null) { - for (String interfaceLifecycleTypeOperation : interfaceLifecycleType.getValue().getOperations().keySet()) { - if (interfaceLifecycleTypeOperation != null && interfaceLifecycleTypeOperation - .equalsIgnoreCase(operation)) { - return true; - } - } + return Either.right(ResultStatusEnum.GENERAL_ERROR); + } catch (final Exception e) { + BeEcompErrorManager.getInstance().logBeSystemError("Import Resource- create interface"); + log.debug("error when creating interface, message:{}", e.getMessage(), e); + return Either.right(ResultStatusEnum.GENERAL_ERROR); } - return false; } private void setRequirements(Map toscaJson, Resource resource, @@ -1165,11 +1018,6 @@ public class ResourceImportManager { this.resourceBusinessLogic = resourceBusinessLogic; } - @Autowired - public void setInterfaceOperationBusinessLogic(InterfaceOperationBusinessLogic interfaceOperationBusinessLogic) { - this.interfaceOperationBusinessLogic = interfaceOperationBusinessLogic; - } - public IGraphLockOperation getGraphLockOperation() { return graphLockOperation; } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java index 9e9d834691..1f1dcfed8c 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java @@ -16,10 +16,21 @@ package org.openecomp.sdc.be.tosca; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DEFAULT; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.INPUTS; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.OPERATIONS; + import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; import org.apache.commons.collections.MapUtils; +import org.openecomp.sdc.be.datatypes.elements.InputDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; import org.openecomp.sdc.be.model.Component; @@ -27,30 +38,27 @@ import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Product; -import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; +import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.be.tosca.PropertyConvertor.PropertyType; +import org.openecomp.sdc.be.tosca.model.ToscaInput; 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; import org.openecomp.sdc.be.tosca.utils.OperationArtifactUtil; +import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; +import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; - @Service public class InterfacesOperationsConverter { private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard"; private static final String DERIVED_FROM_BASE_DEFAULT = "org.openecomp.interfaces.node.lifecycle."; - private static final String OPERATIONS_KEY = "operations"; - private static final String DEFAULT = "default"; private static final String DEFAULT_HAS_UNDERSCORE = "_default"; private static final String DOT = "."; private static final String DEFAULTP = "defaultp"; @@ -58,10 +66,10 @@ public class InterfacesOperationsConverter { public static final String SELF = "SELF"; private static final String LOCAL_INTERFACE_TYPE = "Local"; - private PropertyConvertor propertyConvertor; + private final PropertyConvertor propertyConvertor; @Autowired - public InterfacesOperationsConverter(PropertyConvertor propertyConvertor) { + public InterfacesOperationsConverter(final PropertyConvertor propertyConvertor) { this.propertyConvertor = propertyConvertor; } @@ -96,7 +104,7 @@ public class InterfacesOperationsConverter { } toscaInterfaceType.setOperations(toscaOperations); Map interfacesAsMap = getObjectAsMap(toscaInterfaceType); - Map operationsMap = (Map) interfacesAsMap.remove(OPERATIONS_KEY); + Map operationsMap = (Map) interfacesAsMap.remove(OPERATIONS.getElementName()); interfacesAsMap.putAll(operationsMap); toscaInterfaceTypes.put(getInterfaceType(component, LOCAL_INTERFACE_TYPE), interfacesAsMap); @@ -134,12 +142,12 @@ public class InterfacesOperationsConverter { return getInterfacesMap(component, null, component.getInterfaces(), dataTypes, isAssociatedComponent, false); } - public Map getInterfacesMap(Component component, - ComponentInstance componentInstance, - Map interfaces, - Map dataTypes, - boolean isAssociatedComponent, - boolean isServiceProxyInterface) { + public Map getInterfacesMap(final Component component, + final ComponentInstance componentInstance, + final Map interfaces, + final Map dataTypes, + final boolean isAssociatedComponent, + final boolean isServiceProxyInterface) { if(MapUtils.isEmpty(interfaces)) { return null; } @@ -155,7 +163,7 @@ public class InterfacesOperationsConverter { } toscaInterfaceDefinition.setType(interfaceType); final Map operations = interfaceDefinition.getOperations(); - Map toscaOperations = new HashMap<>(); + Map toscaOperationMap = new HashMap<>(); String operationArtifactPath; for (Map.Entry operationEntry : operations.entrySet()) { @@ -169,12 +177,20 @@ public class InterfacesOperationsConverter { toscaOperation.setDescription(operationEntry.getValue().getDescription()); fillToscaOperationInputs(operationEntry.getValue(), dataTypes, toscaOperation, isServiceProxyInterface); - toscaOperations.put(operationEntry.getValue().getName(), toscaOperation); + toscaOperationMap.put(operationEntry.getValue().getName(), toscaOperation); + } + toscaInterfaceDefinition.setOperations(toscaOperationMap); + + final Map interfaceInputMap = createInterfaceInputMap(interfaceDefinition, dataTypes); + if (!interfaceInputMap.isEmpty()) { + toscaInterfaceDefinition.setInputs(interfaceInputMap); } - toscaInterfaceDefinition.setOperations(toscaOperations); Map interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition); - Map operationsMap = (Map) interfaceDefAsMap.remove(OPERATIONS_KEY); + if (interfaceDefAsMap.containsKey(INPUTS.getElementName())) { + handleDefaults((Map) interfaceDefAsMap.get(INPUTS.getElementName())); + } + Map operationsMap = (Map) interfaceDefAsMap.remove(OPERATIONS.getElementName()); if (isServiceProxyInterface) { //Remove input type and copy default value directly into the proxy node template from the node type handleServiceProxyOperationInputValue(operationsMap, interfaceType); @@ -188,6 +204,22 @@ public class InterfacesOperationsConverter { return toscaInterfaceDefinitions; } + private Map createInterfaceInputMap(final InterfaceDefinition interfaceDefinition, + final Map allDataTypeMap) { + final Map inputMap = interfaceDefinition.getInputs(); + if (MapUtils.isEmpty(inputMap)) { + return Collections.emptyMap(); + } + final Map toscaInterfaceInputMap = new HashMap<>(); + for (final Entry inputEntry : inputMap.entrySet()) { + final InputDataDefinition inputDataDefinition = inputEntry.getValue(); + final ToscaProperty toscaProperty = propertyConvertor + .convertProperty(allDataTypeMap, new PropertyDefinition(inputDataDefinition), PropertyType.INPUT); + toscaInterfaceInputMap.put(inputEntry.getKey(), new ToscaInput(toscaProperty)); + } + return toscaInterfaceInputMap; + } + private static void handleServiceProxyOperationInputValue(Map operationsMap, String parentKey) { for (Map.Entry operationEntry : operationsMap.entrySet()) { final Object value = operationEntry.getValue(); @@ -223,7 +255,7 @@ public class InterfacesOperationsConverter { * ToscaExportHandler so, any string Map key named "defaultp" will have its named changed to "default" * @param operationsMap the map to update */ - private static void handleDefaults(Map operationsMap) { + private void handleDefaults(Map operationsMap) { for (Map.Entry operationEntry : operationsMap.entrySet()) { final Object value = operationEntry.getValue(); if (value instanceof Map) { @@ -232,7 +264,7 @@ public class InterfacesOperationsConverter { final String key = operationEntry.getKey(); if (key.equals(DEFAULTP)) { Object removed = operationsMap.remove(key); - operationsMap.put(DEFAULT, removed); + operationsMap.put(ToscaTagNamesEnum.DEFAULT.getElementName(), removed); } } } @@ -314,10 +346,9 @@ public class InterfacesOperationsConverter { Map objectAsMap = obj instanceof Map ? (Map) obj : objectMapper.convertValue(obj, Map.class); - if (objectAsMap.containsKey(DEFAULT)) { - Object defaultValue = objectAsMap.get(DEFAULT); - objectAsMap.remove(DEFAULT); - objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue); + final String defaultEntry = DEFAULT.getElementName(); + if (objectAsMap.containsKey(defaultEntry)) { + objectAsMap.put(DEFAULT_HAS_UNDERSCORE, objectAsMap.remove(defaultEntry)); } return objectAsMap; } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaInterfaceDefinition.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaInterfaceDefinition.java index 1d444e697a..2f4a3d8ac1 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaInterfaceDefinition.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaInterfaceDefinition.java @@ -16,35 +16,20 @@ package org.openecomp.sdc.be.tosca.model; import java.util.Map; +import lombok.Getter; +import lombok.Setter; /** * @author KATYR * @since March 26, 2018 */ - +@Setter +@Getter public class ToscaInterfaceDefinition { private String type; - private Map operations; // ToscaLifecycleOperationDefinition <-> Object - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public Map getOperations() { - return operations; - } - -// public void setOperations(Map operations) { -// this.operations = operations; -// } + private Map operations; + private Map inputs; - public void setOperations(Map toscaOperations) { - this.operations = toscaOperations; - } } -- cgit 1.2.3-korg