Centralize TOSCA function validation
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / BaseBusinessLogic.java
index fbfeaa5..1f6d2a4 100644 (file)
@@ -22,6 +22,7 @@
 package org.openecomp.sdc.be.components.impl;
 
 import static org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR;
+import static org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode.DATA_ERROR;
 
 import com.google.gson.JsonElement;
 import fj.data.Either;
@@ -41,9 +42,9 @@ import org.openecomp.sdc.be.components.validation.UserValidations;
 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.JanusGraphDao;
 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
-import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
 import org.openecomp.sdc.be.datamodel.utils.ArtifactUtils;
 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.PropertyRule;
@@ -106,6 +107,7 @@ public abstract class BaseBusinessLogic {
     private static final String PROPERTY_IN_SCHEMA_DEFINITION_INSIDE_PROPERTY_OF_TYPE_DOESN_T_EXIST = "Property in Schema Definition inside property of type {} doesn't exist";
     private static final String ADD_PROPERTY_VALUE = "Add property value";
     private static final String THE_VALUE_OF_PROPERTY_FROM_TYPE_IS_INVALID = "The value {} of property from type {} is invalid";
+    private static final String INVALID_PROPERTY_TYPE = "The property type {} is invalid";
     protected IGroupTypeOperation groupTypeOperation;
     protected InterfaceOperation interfaceOperation;
     protected IElementOperation elementDao;
@@ -118,7 +120,6 @@ public abstract class BaseBusinessLogic {
     protected AttributeOperation attributeOperation;
     protected ApplicationDataTypeCache applicationDataTypeCache;
     protected ToscaOperationFacade toscaOperationFacade;
-    protected ApplicationDataTypeCache dataTypeCache;
     protected IGroupOperation groupOperation;
     protected IGroupInstanceOperation groupInstanceOperation;
     protected InterfaceLifecycleOperation interfaceLifecycleTypeOperation;
@@ -190,11 +191,6 @@ public abstract class BaseBusinessLogic {
         this.policyTypeOperation = policyTypeOperation;
     }
 
-    @Autowired
-    public void setDataTypeCache(ApplicationDataTypeCache dataTypeCache) {
-        this.dataTypeCache = dataTypeCache;
-    }
-
     @Autowired
     public void setPropertyOperation(PropertyOperation propertyOperation) {
         this.propertyOperation = propertyOperation;
@@ -340,38 +336,6 @@ public abstract class BaseBusinessLogic {
         return cmpt;
     }
 
-    <T extends PropertyDataDefinition> String updateInputPropertyObjectValue(T property) {
-        Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypesEither = dataTypeCache.getAll();
-        if (allDataTypesEither.isRight()) {
-            JanusGraphOperationStatus status = allDataTypesEither.right().value();
-            BeEcompErrorManager.getInstance()
-                .logInternalFlowError("UpdatePropertyValueOnComponentInstance", "Failed to update property value on instance. Status is " + status,
-                    ErrorSeverity.ERROR);
-            throw new ByActionStatusComponentException(
-                componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)));
-        }
-        Map<String, DataTypeDefinition> allDataTypes = allDataTypesEither.left().value();
-        String propertyType = property.getType();
-        String innerType = getInnerType(property);
-        // Specific Update Logic
-        Either<Object, Boolean> isValid = propertyOperation
-            .validateAndUpdatePropertyValue(propertyType, property.getValue(), true, innerType, allDataTypes);
-        String newValue = property.getValue();
-        if (isValid.isRight()) {
-            Boolean res = isValid.right().value();
-            if (Boolean.FALSE.equals(res)) {
-                throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(
-                    DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.ILLEGAL_ARGUMENT)));
-            }
-        } else {
-            Object object = isValid.left().value();
-            if (object != null) {
-                newValue = object.toString();
-            }
-        }
-        return newValue;
-    }
-
     <T extends PropertyDataDefinition> String getInnerType(T property) {
         ToscaPropertyType type = ToscaPropertyType.isValidType(property.getType());
         log.debug("#getInnerType - The type of the property {} is {}", property.getUniqueId(), property.getType());
@@ -433,25 +397,10 @@ public abstract class BaseBusinessLogic {
         return null;
     }
 
-    protected Map<String, DataTypeDefinition> getAllDataTypes(ApplicationDataTypeCache applicationDataTypeCache) {
-        Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = applicationDataTypeCache.getAll();
-        if (allDataTypes.isRight()) {
-            JanusGraphOperationStatus operationStatus = allDataTypes.right().value();
-            if (operationStatus == JanusGraphOperationStatus.NOT_FOUND) {
-                BeEcompErrorManager.getInstance().logInternalDataError("FetchDataTypes", "Data types are not loaded", ErrorSeverity.ERROR);
-                throw new ByActionStatusComponentException(ActionStatus.DATA_TYPE_CANNOT_BE_EMPTY);
-            } else {
-                BeEcompErrorManager.getInstance().logInternalFlowError("FetchDataTypes", "Failed to fetch data types", ErrorSeverity.ERROR);
-                throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
-            }
-        }
-        return allDataTypes.left().value();
-    }
-
     Either<Boolean, ResponseFormat> validatePropertyDefaultValue(IComplexDefaultValue property, Map<String, DataTypeDefinition> dataTypes) {
         String type;
         String innerType = null;
-        if (!propertyOperation.isPropertyTypeValid(property)) {
+        if (!propertyOperation.isPropertyTypeValid(property, dataTypes)) {
             log.info("Invalid type for property '{}' type '{}'", property.getName(), property.getType());
             ResponseFormat responseFormat = componentsUtils
                 .getResponseFormat(ActionStatus.INVALID_PROPERTY_TYPE, property.getType(), property.getName());
@@ -557,7 +506,8 @@ public abstract class BaseBusinessLogic {
         if (isValid.isRight()) {
             Boolean res = isValid.right().value();
             if (Boolean.FALSE.equals(res)) {
-                throw new StorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.ILLEGAL_ARGUMENT));
+                log.error(DATA_ERROR, this.getClass().getName(), "Dropping invalid value for property: {} , value: ", property, value);
+                return "";
             }
         } else {
             Object object = isValid.left().value();
@@ -604,6 +554,10 @@ public abstract class BaseBusinessLogic {
         if (isValidate) {
             if (type == null) {
                 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
+                if (dataTypeDefinition == null) {
+                    log.debug(INVALID_PROPERTY_TYPE, propertyType);
+                    return Either.right(false);
+                }
                 ImmutablePair<JsonElement, Boolean> validateResult = dataTypeValidatorConverter
                     .validateAndUpdate(value, dataTypeDefinition, dataTypes);
                 if (Boolean.FALSE.equals(validateResult.right)) {
@@ -706,7 +660,8 @@ public abstract class BaseBusinessLogic {
         if (inputPathArr.length > 1) {
             inputPathArr = ArrayUtils.remove(inputPathArr, 0);
         }
-        Map<String, DataTypeDefinition> dataTypeDefinitionMap = applicationDataTypeCache.getAll().left().value();
+        final Map<String, DataTypeDefinition> dataTypeDefinitionMap =
+            componentsUtils.getAllDataTypes(applicationDataTypeCache, inputDefinition.getModel());
         String propertyType = inputDefinition.getParentPropertyType();
         for (String anInputPathArr : inputPathArr) {
             if (ToscaType.isPrimitiveType(propertyType)) {
@@ -774,4 +729,8 @@ public abstract class BaseBusinessLogic {
         }
         return result.left().value();
     }
+
+    public String getComponentModelByComponentId(final String componentId) throws BusinessLogicException {
+        return getComponent(componentId).getModel();
+    }
 }