From 7a7b13726c195e2944ccc59e4d5c5ade57318763 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Wed, 15 Jun 2022 15:30:40 +0100 Subject: [PATCH] Support TOSCA get_attribute function Adds support to TOSCA get_attribute function in the Property Assignment TOSCA Function modal. Change-Id: I73dda215a7c9d7fecf0803cc259634279c3bdfb6 Issue-ID: SDC-4053 Signed-off-by: andre.schmid --- .../impl/ComponentInstanceBusinessLogic.java | 30 ++++++++--- .../impl/ComponentInstanceBusinessLogicTest.java | 53 +------------------ .../config/catalog-be/error-configuration.yaml | 9 ++++ .../sdc/be/model/AttributeDefinition.java | 13 ++++- .../openecomp/sdc/be/model/PropertyDefinition.java | 2 +- .../openecomp/sdc/be/model/ToscaPropertyData.java | 31 +++++++++++ .../tosca-function/tosca-function.component.html | 4 +- .../tosca-function/tosca-function.component.ts | 61 +++++++++++++++++----- .../topology-template.service.ts | 54 +++++++++---------- catalog-ui/src/assets/languages/en_US.json | 2 + .../elements/ToscaGetFunctionDataDefinition.java | 14 +++-- .../ToscaGetFunctionDataDefinitionTest.java | 19 ++++--- 12 files changed, 176 insertions(+), 116 deletions(-) create mode 100644 catalog-model/src/main/java/org/openecomp/sdc/be/model/ToscaPropertyData.java diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java index ab01b99957..4bf81727e6 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java @@ -107,6 +107,7 @@ import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; import org.openecomp.sdc.be.model.RequirementDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.ToscaPropertyData; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.jsonjanusgraph.config.ContainerInstanceTypesData; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations; @@ -2387,13 +2388,25 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { return; } + if (toscaGetFunction.getFunctionType() == ToscaGetFunctionType.GET_ATTRIBUTE) { + if (toscaGetFunction.getPropertySource() == PropertySource.SELF) { + validateGetFunction(property, parentComponent.getAttributes(), parentComponent.getModel()); + } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE) { + final ComponentInstance componentInstance = + parentComponent.getComponentInstanceById(toscaGetFunction.getSourceUniqueId()) + .orElseThrow(ToscaGetFunctionExceptionSupplier.instanceNotFound(toscaGetFunction.getSourceName())); + validateGetFunction(property, componentInstance.getAttributes(), parentComponent.getModel()); + } + + return; + } throw ToscaGetFunctionExceptionSupplier.functionNotSupported(toscaGetFunction.getFunctionType()).get(); } - private void validateGetFunction(final T property, - final List parentProperties, - final String model) { + private void validateGetFunction(final T property, + final List parentProperties, + final String model) { final ToscaGetFunctionDataDefinition toscaGetFunction = property.getToscaGetFunction(); if (CollectionUtils.isEmpty(parentProperties)) { throw ToscaGetFunctionExceptionSupplier @@ -2402,7 +2415,7 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { ).get(); } final String getFunctionPropertyUniqueId = toscaGetFunction.getPropertyUniqueId(); - T referredProperty = (T) parentProperties.stream() + ToscaPropertyData referredProperty = parentProperties.stream() .filter(property1 -> getFunctionPropertyUniqueId.equals(property1.getUniqueId())) .findFirst() .orElseThrow(ToscaGetFunctionExceptionSupplier @@ -2423,8 +2436,9 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { } } - private T findSubProperty(final T referredProperty, final ToscaGetFunctionDataDefinition toscaGetFunction, - final String model) { + private ToscaPropertyData findSubProperty(final ToscaPropertyData referredProperty, + final ToscaGetFunctionDataDefinition toscaGetFunction, + final String model) { final Map dataTypeMap = loadDataTypes(model); final List propertyPathFromSource = toscaGetFunction.getPropertyPathFromSource(); DataTypeDefinition dataType = dataTypeMap.get(referredProperty.getType()); @@ -2432,10 +2446,10 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { throw ToscaGetFunctionExceptionSupplier .propertyDataTypeNotFound(propertyPathFromSource.get(0), referredProperty.getType(), toscaGetFunction.getFunctionType()).get(); } - T foundProperty = referredProperty; + ToscaPropertyData foundProperty = referredProperty; for (int i = 1; i < propertyPathFromSource.size(); i++) { final String currentPropertyName = propertyPathFromSource.get(i); - foundProperty = (T) dataType.getProperties().stream() + foundProperty = dataType.getProperties().stream() .filter(propertyDefinition -> currentPropertyName.equals(propertyDefinition.getName())).findFirst() .orElseThrow( ToscaGetFunctionExceptionSupplier diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogicTest.java index bf3ec3cb7e..cd916d0d1e 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogicTest.java @@ -678,54 +678,6 @@ class ComponentInstanceBusinessLogicTest { assertEquals(expectedResponse.getStatus(), actualResponse.getStatus()); } - @Test - void testToscaGetFunctionValidation_toscaFunctionNotSupportedTest() { - final String userId = "userId"; - final String containerComponentId = "containerComponentId"; - final String containerComponentName = "containerComponentName"; - final String resourceInstanceId = "resourceInstanceId"; - final List properties = new ArrayList<>(); - final ComponentInstanceProperty propertyGetInput = new ComponentInstanceProperty(); - propertyGetInput.setName("anyName"); - final var toscaGetFunction = new ToscaGetFunctionDataDefinition(); - toscaGetFunction.setFunctionType(ToscaGetFunctionType.GET_ATTRIBUTE); - toscaGetFunction.setPropertySource(PropertySource.SELF); - toscaGetFunction.setPropertyPathFromSource(List.of("sourcePath")); - toscaGetFunction.setSourceName("sourceName"); - toscaGetFunction.setSourceUniqueId("sourceUniqueId"); - toscaGetFunction.setPropertyName("propertyName"); - toscaGetFunction.setPropertyUniqueId("propertyId"); - propertyGetInput.setToscaGetFunction(toscaGetFunction); - properties.add(propertyGetInput); - - final Component component = new Service(); - component.setName(containerComponentName); - component.setUniqueId(containerComponentId); - component.setLastUpdaterUserId(userId); - component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); - - final Map> componentInstanceProps = new HashMap<>(); - componentInstanceProps.put(resourceInstanceId, properties); - component.setComponentInstancesProperties(componentInstanceProps); - - final ComponentInstance resourceInstance = createComponentInstance("componentInstance1"); - resourceInstance.setUniqueId(resourceInstanceId); - component.setComponentInstances(List.of(resourceInstance)); - - mockComponentForToscaGetFunctionValidation(component); - //when - final Either, ResponseFormat> responseFormatEither = - componentInstanceBusinessLogic - .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId); - //then - assertTrue(responseFormatEither.isRight(), "Expecting an error"); - final ResponseFormat actualResponse = responseFormatEither.right().value(); - final ResponseFormat expectedResponse = ToscaGetFunctionExceptionSupplier - .functionNotSupported(toscaGetFunction.getFunctionType()).get().getResponseFormat(); - assertEquals(expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage()); - assertEquals(expectedResponse.getStatus(), actualResponse.getStatus()); - } - @ParameterizedTest @MethodSource("getToscaFunctionForValidation") void testToscaGetFunctionValidation_AttributesNotFoundTest(final ToscaGetFunctionDataDefinition toscaGetFunction, @@ -762,9 +714,8 @@ class ComponentInstanceBusinessLogicTest { //then assertTrue(responseFormatEither.isRight(), "Expecting an error"); final ResponseFormat actualResponse = responseFormatEither.right().value(); - final ResponseFormat expectedResponse = expectedValidationResponse; - assertEquals(expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage()); - assertEquals(expectedResponse.getStatus(), actualResponse.getStatus()); + assertEquals(expectedValidationResponse.getFormattedMessage(), actualResponse.getFormattedMessage()); + assertEquals(expectedValidationResponse.getStatus(), actualResponse.getStatus()); } @Test diff --git a/catalog-be/src/test/resources/config/catalog-be/error-configuration.yaml b/catalog-be/src/test/resources/config/catalog-be/error-configuration.yaml index 2100dc8292..293bd49632 100644 --- a/catalog-be/src/test/resources/config/catalog-be/error-configuration.yaml +++ b/catalog-be/src/test/resources/config/catalog-be/error-configuration.yaml @@ -2392,3 +2392,12 @@ errors: message: "The %1 '%2' type '%3' was not found.", messageId: "SVC4170" } + + #-----------SVC4172--------------------------- + #%1 - TOSCA function attribute + TOSCA_FUNCTION_MISSING_ATTRIBUTE: { + code: 400, + message: "Missing TOSCA function '%1'.", + messageId: "SVC4172" + } + diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/AttributeDefinition.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/AttributeDefinition.java index d1bcedc8ea..be9bee0d53 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/AttributeDefinition.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/AttributeDefinition.java @@ -21,9 +21,10 @@ package org.openecomp.sdc.be.model; import lombok.NoArgsConstructor; import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; @NoArgsConstructor -public class AttributeDefinition extends AttributeDataDefinition implements IOperationParameter, IComplexDefaultValue { +public class AttributeDefinition extends AttributeDataDefinition implements IOperationParameter, IComplexDefaultValue, ToscaPropertyData { public AttributeDefinition(final AttributeDataDefinition attributeDataDefinition) { super(attributeDataDefinition); @@ -49,4 +50,14 @@ public class AttributeDefinition extends AttributeDataDefinition implements IOpe return "AttributeDefinition{" + "name=" + getName() + "uniqueId=" + getUniqueId() + "ownerId=" + getOwnerId() + "type=" + getType() + "description=" + getDescription() + "default=" + getDefaultValue() + '}'; } + + @Override + public String getSchemaType() { + final SchemaDefinition schema = getSchema(); + if (schema == null || schema.getProperty() == null) { + return null; + } + return schema.getProperty().getType(); + } + } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyDefinition.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyDefinition.java index 6e313eda6c..d9add3f425 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyDefinition.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyDefinition.java @@ -31,7 +31,7 @@ import org.apache.commons.collections.CollectionUtils; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.model.operations.impl.PropertyOperation; -public class PropertyDefinition extends PropertyDataDefinition implements IOperationParameter, IComplexDefaultValue { +public class PropertyDefinition extends PropertyDataDefinition implements IOperationParameter, IComplexDefaultValue, ToscaPropertyData { private List constraints; diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/ToscaPropertyData.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/ToscaPropertyData.java new file mode 100644 index 0000000000..14c95757c7 --- /dev/null +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/ToscaPropertyData.java @@ -0,0 +1,31 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.model; + +/** + * Represents basic/common TOSCA Property, Attribute or Input methods + */ +public interface ToscaPropertyData { + String getUniqueId(); + String getType(); + String getSchemaType(); +} diff --git a/catalog-ui/src/app/ng2/pages/properties-assignment/tosca-function/tosca-function.component.html b/catalog-ui/src/app/ng2/pages/properties-assignment/tosca-function/tosca-function.component.html index f0db645b0e..b6b313d93c 100644 --- a/catalog-ui/src/app/ng2/pages/properties-assignment/tosca-function/tosca-function.component.html +++ b/catalog-ui/src/app/ng2/pages/properties-assignment/tosca-function/tosca-function.component.html @@ -27,14 +27,14 @@ [ngValue]="toscaFunction">{{toscaFunction | lowercase}} -
+
-
+