+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
package org.openecomp.sdc.be.components.impl;
import fj.data.Either;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.EnumMap;
import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.MapUtils;
+import org.apache.commons.lang.StringUtils;
+import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
+import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
+import org.openecomp.sdc.be.components.property.PropertyDeclarationOrchestrator;
+import org.openecomp.sdc.be.components.validation.PolicyUtils;
import org.openecomp.sdc.be.dao.api.ActionStatus;
-import org.openecomp.sdc.be.datatypes.elements.PolicyDataDefinition;
+import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
-import org.openecomp.sdc.be.model.*;
+import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
+import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
+import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
+import org.openecomp.sdc.be.model.Component;
+import org.openecomp.sdc.be.model.ComponentInstInputsMap;
+import org.openecomp.sdc.be.model.ComponentInstance;
+import org.openecomp.sdc.be.model.ComponentInstanceProperty;
+import org.openecomp.sdc.be.model.ComponentParametersView;
+import org.openecomp.sdc.be.model.GroupDefinition;
+import org.openecomp.sdc.be.model.PolicyDefinition;
+import org.openecomp.sdc.be.model.PolicyTypeDefinition;
+import org.openecomp.sdc.be.model.Resource;
+import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
+import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
+import org.openecomp.sdc.be.model.operations.api.IElementOperation;
+import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
+import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
+import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
+import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
import org.openecomp.sdc.common.datastructure.Wrapper;
+import org.openecomp.sdc.common.log.elements.LoggerSupportability;
+import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions;
+import org.openecomp.sdc.common.log.enums.StatusCode;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.openecomp.sdc.exception.ResponseFormat;
+import org.springframework.beans.factory.annotation.Autowired;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toMap;
-import static org.openecomp.sdc.be.components.validation.PolicyUtils.*;
+import static org.openecomp.sdc.be.components.validation.PolicyUtils.getExcludedPolicyTypesByComponent;
+import static org.openecomp.sdc.be.components.validation.PolicyUtils.getNextPolicyCounter;
+import static org.openecomp.sdc.be.components.validation.PolicyUtils.validatePolicyFields;
/**
* Provides specified business logic to create, retrieve, update, delete a policy
public class PolicyBusinessLogic extends BaseBusinessLogic {
private static final String FAILED_TO_VALIDATE_COMPONENT = "#{} - failed to validate the component {} before policy processing. ";
+ private static final String DECLARE_PROPERTIES_TO_POLICIES = "declare properties to policies";
private static final Logger log = Logger.getLogger(PolicyBusinessLogic.class);
+ private static final LoggerSupportability loggerSupportability= LoggerSupportability.getLogger(PolicyBusinessLogic.class.getName());
+
+ private PropertyDeclarationOrchestrator propertyDeclarationOrchestrator;
+
+ @Autowired
+ public PolicyBusinessLogic(IElementOperation elementDao,
+ IGroupOperation groupOperation,
+ IGroupInstanceOperation groupInstanceOperation,
+ IGroupTypeOperation groupTypeOperation,
+ InterfaceOperation interfaceOperation,
+ InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
+ ArtifactsOperations artifactToscaOperation) {
+ super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
+ interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
+ }
+
+ @Autowired
+ public void setPropertyDeclarationOrchestrator(PropertyDeclarationOrchestrator propertyDeclarationOrchestrator) {
+ this.propertyDeclarationOrchestrator = propertyDeclarationOrchestrator;
+ }
/**
* Adds the newly created policy of the specified type to the component
* @return a policy or an error in a response format
*/
- public Either<PolicyDefinition, ResponseFormat> createPolicy(ComponentTypeEnum componentType, String componentId, String policyTypeName, String userId, boolean shouldLock) {
+ public PolicyDefinition createPolicy(ComponentTypeEnum componentType, String componentId, String policyTypeName, String userId, boolean shouldLock) {
+
+ log.trace("#createPolicy - starting to create policy of the type {} on the component {}. ", policyTypeName, componentId);
+ Component component = null;
+ boolean failed = false;
+ try {
+ component = validateAndLockComponentAndUserBeforeWriteOperation(componentType, componentId, userId, shouldLock);
+ return createPolicy(policyTypeName, component);
+ }catch (ComponentException e){
+ failed = true;
+ throw e;
+ }finally {
+ unlockComponent(shouldLock, failed, component);
+ }
+ }
+
+ public Map<String, PolicyDefinition> createPoliciesFromParsedCsar(Component component,
+ final Map<String, PolicyDefinition> incomingPolicyDefinitions) {
+ if (MapUtils.isEmpty(incomingPolicyDefinitions)) {
+ return Collections.emptyMap();
+ }
+ Map<String, PolicyDefinition> createdPolicies = new HashMap<>();
+ for (Map.Entry<String, PolicyDefinition> policyEntry : incomingPolicyDefinitions.entrySet()) {
+ PolicyDefinition incomingPolicyDefinition = policyEntry.getValue();
+ String policyName = incomingPolicyDefinition.getName();
+ log.trace("Going to create policy {}", incomingPolicyDefinition);
+ loggerSupportability.log(LoggerSupportabilityActions.CREATE_GROUP_POLICY,component.getComponentMetadataForSupportLog(),
+ StatusCode.STARTED,"Start to create policy: {} for component {}", policyName, component.getName());
+ String policyType = incomingPolicyDefinition.getType();
+ if (StringUtils.isEmpty(policyType)) {
+ log.debug("Policy type '{}' for policy '{}' not found.", policyType, policyName);
+ throw new ByActionStatusComponentException(ActionStatus.POLICY_MISSING_POLICY_TYPE, policyName);
+ }
+ // create policyDefinition
+ String policyTypeName = incomingPolicyDefinition.getPolicyTypeName();
+ PolicyDefinition createdPolicyDefinition = createPolicy(policyTypeName, component);
+ // set isFromCsar
+ createdPolicyDefinition.setToscaPresentationValue(JsonPresentationFields.IS_FROM_CSAR, true);
+ // link policy to component
+ component.addPolicy(createdPolicyDefinition);
+ // process targets
+ Map<PolicyTargetType, List<String>> policyTargets = incomingPolicyDefinition.getTargets();
+ createdPolicyDefinition = setUpdatePolicyTargets(component, createdPolicyDefinition, policyTargets);
+ // process policy properties
+ List<PropertyDataDefinition> properties = incomingPolicyDefinition.getProperties();
+ createdPolicyDefinition = setUpdatePolicyProperties(component, createdPolicyDefinition, properties);
+ createdPolicies.put(policyName, createdPolicyDefinition);
+ loggerSupportability.log(LoggerSupportabilityActions.CREATE_POLICIES,component.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,"policy {} has been created ", policyName);
+ }
+ return createdPolicies;
+ }
+
+ private PolicyDefinition setUpdatePolicyProperties(Component component, PolicyDefinition policyDefinition, List<PropertyDataDefinition> properties) {
+ if (CollectionUtils.isNotEmpty(properties)) {
+ PropertyDataDefinition[] propertiesArray = properties.toArray(new PropertyDataDefinition[properties.size()]);
+ List<PropertyDataDefinition> updatedPropertiesList = setComponentValidateUpdatePolicyProperties(
+ policyDefinition.getUniqueId(),
+ propertiesArray,
+ component
+ );
+ policyDefinition.setProperties(updatedPropertiesList);
+ }
+ return policyDefinition;
+ }
+
+ private PolicyDefinition setUpdatePolicyTargets(Component component, PolicyDefinition policyDefinition,
+ Map<PolicyTargetType, List<String>> targets) {
+ if (MapUtils.isEmpty(targets)) {
+ return policyDefinition;
+ }
+ policyDefinition.setTargets(getPolicyTargets(component, targets));
+ policyDefinition = validateAndUpdatePolicyTargets(component, policyDefinition.getUniqueId(),
+ policyDefinition.getTargets());
+ return policyDefinition;
+ }
+
+ private EnumMap<PolicyTargetType, List<String>> getPolicyTargets(Component component,
+ Map<PolicyTargetType, List<String>> targets) {
+ EnumMap<PolicyTargetType, List<String>> updatedTargets = new EnumMap<>(PolicyTargetType.class);
+ handlePolicyTargetsFromComponentInstances(component, targets, updatedTargets);
+ handlePolicyTargetsFromGroups(component, targets, updatedTargets);
+ return updatedTargets;
+ }
+
+ private void handlePolicyTargetsFromGroups(Component component, Map<PolicyTargetType, List<String>> targets,
+ EnumMap<PolicyTargetType, List<String>> updatedTargets) {
+ List<String> targetsToUpdate = targets.getOrDefault(PolicyTargetType.GROUPS, Collections.EMPTY_LIST);
+
+ List<String> targetUniqueIds = new ArrayList<>();
+
+ for (String targetName : targetsToUpdate) {
+ Optional<GroupDefinition> group = component.getGroupByInvariantName(targetName);
+ String groupUniqueId = group
+ .orElseThrow(() -> new ByActionStatusComponentException(
+ ActionStatus.GROUP_INSTANCE_NOT_FOUND_ON_COMPONENT_INSTANCE)).getUniqueId();
+ targetUniqueIds.add(groupUniqueId);
+ }
+ updatedTargets.put(PolicyTargetType.GROUPS, targetUniqueIds);
+ }
+
+ private void handlePolicyTargetsFromComponentInstances(Component component,
+ Map<PolicyTargetType, List<String>> targets, EnumMap<PolicyTargetType, List<String>> updatedTargets) {
+
+ List<String> targetsToUpdate = targets.getOrDefault(PolicyTargetType.COMPONENT_INSTANCES, Collections.EMPTY_LIST);
+
+ // update targets to uniqueIds of respective component instance
+ List<String> targetsUniqueIds = new ArrayList<>();
+ for (String targetName : targetsToUpdate) {
+ Optional<ComponentInstance> componentInstance = component.getComponentInstanceByName(targetName);
+ String componentUniqueId = componentInstance
+ .orElseThrow(
+ () -> new ByActionStatusComponentException(
+ ActionStatus.COMPONENT_INSTANCE_NOT_FOUND
+ )
+ )
+ .getUniqueId();
+ targetsUniqueIds.add(componentUniqueId);
+ }
+ updatedTargets.put(PolicyTargetType.COMPONENT_INSTANCES, targetsUniqueIds);
+ }
+
+ /*public Either<PolicyDefinition, ResponseFormat> createPolicy(ComponentTypeEnum componentType, String componentId, String policyTypeName, String userId, boolean shouldLock) {
Either<PolicyDefinition, ResponseFormat> result = null;
log.trace("#createPolicy - starting to create policy of the type {} on the component {}. ", policyTypeName, componentId);
return createPolicy(policyTypeName, c);
});
} catch (Exception e) {
+ if (ComponentException.class.equals(e.getClass())) {
+ throw e;
+ }
log.error("#createPolicy - the exception occurred upon creation of a policy of the type {} for the component {}: ", policyTypeName, componentId, e);
result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
} finally {
-
+ //TODO Andrey result = boolean
unlockComponent(shouldLock, result, component);
}
return result;
+ }*/
+
+ public Either<List<PolicyDefinition>, ResponseFormat> getPoliciesList(ComponentTypeEnum componentType, String componentId, String userId) {
+ Either<List<PolicyDefinition>, ResponseFormat> result;
+ log.trace("#getPolicies - starting to retrieve policies of component {}. ", componentId);
+ try {
+ Component component = validateContainerComponentAndUserBeforeReadOperation(componentType, componentId, userId);
+ result = Either.left(component.resolvePoliciesList());
+ } catch (Exception e) {
+ log.error("#getPolicy - the exception occurred upon retrieving policies list of component {}: ", componentId, e);
+ result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
+ }
+ return result;
}
+
+
/**
* Retrieves the policy of the component by UniqueId
*
* @param componentId the ID of the component
* @param policyId the ID of the policy
* @param userId the ID of the user
- * @return either policy or error response
+ * @return either policy or error response
*/
- public Either<PolicyDefinition, ResponseFormat> getPolicy(ComponentTypeEnum componentType, String componentId, String policyId, String userId) {
+ public PolicyDefinition getPolicy(ComponentTypeEnum componentType, String componentId, String policyId, String userId) {
+ log.trace("#getPolicy - starting to retrieve the policy {} of the component {}. ", policyId, componentId);
+ Component component = validateContainerComponentAndUserBeforeReadOperation(componentType, componentId, userId);
+ return getPolicyById(component, policyId);
+ }
+
+ /*public Either<PolicyDefinition, ResponseFormat> getPolicy(ComponentTypeEnum componentType, String componentId, String policyId, String userId) {
Either<PolicyDefinition, ResponseFormat> result;
log.trace("#getPolicy - starting to retrieve the policy {} of the component {}. ", policyId, componentId);
try {
result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
}
return result;
- }
+ }*/
/**
* Updates the policy of the component
* @param shouldLock the flag defining if the component should be locked
* @return a policy or an error in a response format
*/
- public Either<PolicyDefinition, ResponseFormat> updatePolicy(ComponentTypeEnum componentType, String componentId, PolicyDefinition policy, String userId, boolean shouldLock) {
+ public PolicyDefinition updatePolicy(ComponentTypeEnum componentType, String componentId, PolicyDefinition policy, String userId, boolean shouldLock) {
+ Component component = null;
+ boolean failed = false;
+ log.trace("#updatePolicy - starting to update the policy {} on the component {}. ", policy.getUniqueId(), componentId);
+ try {
+ component = validateAndLockComponentAndUserBeforeWriteOperation(componentType, componentId, userId, shouldLock);
+ return validateAndUpdatePolicy(component, policy);
+ } catch (ComponentException e) {
+ failed = true;
+ log.error("#updatePolicy - the exception occurred upon update of a policy of the type {} for the component {}: ", policy.getUniqueId(), componentId, e);
+ throw e;
+ } finally {
+ //TODO Andrey result = boolean
+ unlockComponent(shouldLock, failed, component);
+ }
+ }
+
+
+ /*public Either<PolicyDefinition, ResponseFormat> updatePolicy(ComponentTypeEnum componentType, String componentId, PolicyDefinition policy, String userId, boolean shouldLock) {
Either<PolicyDefinition, ResponseFormat> result = null;
log.trace("#updatePolicy - starting to update the policy {} on the component {}. ", policy.getUniqueId(), componentId);
Wrapper<Component> component = new Wrapper<>();
log.error("#updatePolicy - the exception occurred upon update of a policy of the type {} for the component {}: ", policy.getUniqueId(), componentId, e);
result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
} finally {
+ //TODO Andrey result = boolean
unlockComponent(shouldLock, result, component);
}
return result;
- }
+ }*/
/**
* Deletes the policy from the component
* @param shouldLock the flag defining if the component should be locked
* @return a policy or an error in a response format
*/
- public Either<PolicyDefinition, ResponseFormat> deletePolicy(ComponentTypeEnum componentType, String componentId, String policyId, String userId, boolean shouldLock) {
+ public PolicyDefinition deletePolicy(ComponentTypeEnum componentType, String componentId, String policyId, String userId, boolean shouldLock) {
+ PolicyDefinition result = null;
+ log.trace("#deletePolicy - starting to update the policy {} on the component {}. ", policyId, componentId);
+ Component component = null;
+ boolean failed= false;
+ try {
+ component = validateAndLockComponentAndUserBeforeWriteOperation(componentType, componentId, userId, shouldLock);
+ return deletePolicy(component, policyId);
+ } catch (ComponentException e) {
+ failed = true;
+ log.error("#deletePolicy - the exception occurred upon update of a policy of the type {} for the component {}: ", policyId, componentId, e);
+ throw e;
+ } finally {
+ unlockComponent(shouldLock, failed, component);
+ }
+ }
+
+
+ /*public Either<PolicyDefinition, ResponseFormat> deletePolicy(ComponentTypeEnum componentType, String componentId, String policyId, String userId, boolean shouldLock) {
Either<PolicyDefinition, ResponseFormat> result = null;
log.trace("#deletePolicy - starting to update the policy {} on the component {}. ", policyId, componentId);
Wrapper<Component> component = new Wrapper<>();
try {
- result = validateAndLockComponentAndUserBeforeWriteOperation(componentType, componentId, userId, shouldLock)
- .left()
- .bind(c -> {
- component.setInnerElement(c);
- return deletePolicy(c, policyId);
- });
+ Either<Component, ResponseFormat> componentEither =
+ validateAndLockComponentAndUserBeforeWriteOperation(componentType, componentId, userId, shouldLock);
+ if (componentEither.isRight()) {
+ return Either.right(componentEither.right().value());
+ }
+
+ ComponentParametersView componentParametersView = new ComponentParametersView();
+ componentParametersView.disableAll();
+ componentParametersView.setIgnoreComponentInstances(false);
+ componentParametersView.setIgnoreComponentInstancesProperties(false);
+ componentParametersView.setIgnorePolicies(false);
+ componentParametersView.setIgnoreProperties(false);
+
+ Either<Component, StorageOperationStatus> componentWithFilters =
+ toscaOperationFacade.getToscaElement(componentId, componentParametersView);
+ if (componentWithFilters.isRight()) {
+ return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(componentWithFilters.right().value())));
+ }
+
+ Component containerComponent = componentWithFilters.left().value();
+ component.setInnerElement(containerComponent);
+ result = deletePolicy(containerComponent, policyId);
+
+ if(result.isRight()) {
+ log.error("#deletePolicy - could not delete policy of the type {} for the component {}: ", policyId, componentId);
+ return result;
+ }
+
+ PolicyDefinition policyToDelete = result.left().value();
+
+ StorageOperationStatus storageOperationStatus = propertyDeclarationOrchestrator.unDeclarePropertiesAsPolicies(
+ containerComponent, policyToDelete);
+ if (storageOperationStatus != StorageOperationStatus.OK) {
+ log.debug("Component id: {} update properties declared as policy for policy id: {} failed", componentId, policyId);
+ return Either.right(componentsUtils.getResponseFormat(componentsUtils
+ .convertFromStorageResponse(storageOperationStatus), containerComponent.getName()));
+ }
+
+ return result;
} catch (Exception e) {
log.error("#deletePolicy - the exception occurred upon update of a policy of the type {} for the component {}: ", policyId, componentId, e);
+ return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR, e.getMessage()));
} finally {
unlockComponent(shouldLock, result, component);
}
- return result;
- }
-
- public Either<PolicyDefinition, ResponseFormat> updatePolicyTargets(ComponentTypeEnum componentTypeEnum, String componentId, String policyId, Map<PolicyTargetType, List<String>> targets, String userId) {
+ }*/
+ public Either<PolicyDefinition, ResponseFormat> undeclarePolicy(ComponentTypeEnum componentType, String componentId, String policyId, String userId, boolean shouldLock) {
Either<PolicyDefinition, ResponseFormat> result = null;
- log.debug("updating the policy id {} targets with the components {}. ", policyId, componentId);
+ log.trace("#undeclarePolicy - starting to undeclare policy {} on component {}. ", policyId, componentId);
+ Wrapper<Component> component = new Wrapper<>();
try {
- //not right error response
- result = validateAndLockComponentAndUserBeforeWriteOperation(componentTypeEnum, componentId, userId, true)
- .left()
- .bind(cmpt -> validateAndUpdatePolicyTargets(cmpt, policyId, targets));
+ validateAndLockComponentAndUserBeforeWriteOperation(componentType, componentId, userId, shouldLock);
+
+ ComponentParametersView componentParametersView = new ComponentParametersView();
+ componentParametersView.disableAll();
+ componentParametersView.setIgnoreComponentInstances(false);
+ componentParametersView.setIgnoreComponentInstancesProperties(false);
+ componentParametersView.setIgnorePolicies(false);
+
+ Either<Component, StorageOperationStatus> componentWithFilters =
+ toscaOperationFacade.getToscaElement(componentId, componentParametersView);
+ if (componentWithFilters.isRight()) {
+ return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(componentWithFilters.right().value())));
+ }
+
+ Component containerComponent = componentWithFilters.left().value();
+
+ Optional<PolicyDefinition> policyCandidate = getPolicyForUndeclaration(policyId, containerComponent);
+ if(policyCandidate.isPresent()) {
+ result = undeclarePolicy(policyCandidate.get(), containerComponent);
+ }
return result;
+ }catch (Exception e) {
+ log.error("#undeclarePolicy - the exception occurred upon update of a policy of type {} for component {}: ", policyId, componentId, e);
+ return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR, e.getMessage()));
} finally {
+ if (result == null || result.isRight()){
+ unlockComponent(shouldLock, true, component);
+ } else {
+ unlockComponent(shouldLock, false, component);
+ }
+ }
+ }
- unlockComponentById(result, componentId);
+ private Either<PolicyDefinition, ResponseFormat> undeclarePolicy(PolicyDefinition policyDefinition, Component containerComponent) {
+ StorageOperationStatus undeclareStatus = propertyDeclarationOrchestrator
+ .unDeclarePropertiesAsPolicies(containerComponent, policyDefinition);
+ if(undeclareStatus != StorageOperationStatus.OK){
+ return Either.right(componentsUtils.getResponseFormat(undeclareStatus));
+ } else {
+ return Either.left(policyDefinition);
+ }
+ }
+
+
+ private Optional<PolicyDefinition> getPolicyForUndeclaration(String policyId, Component component) {
+ Map<String, PolicyDefinition> policies = component.getPolicies();
+ if(MapUtils.isNotEmpty(policies) && policies.containsKey(policyId)) {
+ return Optional.of(policies.get(policyId));
+ }
+
+ Map<String, List<ComponentInstanceProperty>> componentInstancesProperties =
+ MapUtils.isEmpty(component.getComponentInstancesProperties()) ? new HashMap<>() : component.getComponentInstancesProperties();
+ for(Map.Entry<String, List<ComponentInstanceProperty>> instancePropertyEntry : componentInstancesProperties.entrySet()) {
+ Optional<ComponentInstanceProperty> propertyCandidate = getPropertyForDeclaredPolicy(policyId, instancePropertyEntry.getValue());
+
+ if(propertyCandidate.isPresent()) {
+ return Optional.of(
+ PolicyUtils.getDeclaredPolicyDefinition(instancePropertyEntry.getKey(), propertyCandidate.get()));
+ }
+ }
+
+ return Optional.empty();
}
- }
+ private Optional<ComponentInstanceProperty> getPropertyForDeclaredPolicy(String policyId, List<ComponentInstanceProperty> componentInstanceProperties) {
+ for(ComponentInstanceProperty property : componentInstanceProperties) {
+ Optional<GetPolicyValueDataDefinition> getPolicyCandidate = property.safeGetGetPolicyValues().stream()
+ .filter(getPolicyValue -> getPolicyValue.getPolicyId()
+ .equals(policyId))
+ .findAny();
- private Either<PolicyDefinition, ResponseFormat> validateAndUpdatePolicyTargets(Component component, String policyId, Map<PolicyTargetType, List<String>> targets) {
- return validateTargetsExistAndTypesCorrect(component.getUniqueId(), targets)
- .left()
- .bind(cmp ->updateTargets(component.getUniqueId(), component.getPolicyById(policyId), targets, policyId));
+ if(getPolicyCandidate.isPresent()) {
+ return Optional.of(property);
+ }
+ }
+ return Optional.empty();
+ }
+
+
+ public PolicyDefinition updatePolicyTargets(ComponentTypeEnum componentTypeEnum, String componentId, String policyId, Map<PolicyTargetType, List<String>> targets, String userId) {
+
+ Either<PolicyDefinition, ResponseFormat> result = null;
+ log.debug("updating the policy id {} targets with the components {}. ", policyId, componentId);
+ boolean failed = false;
+ try {
+ //not right error response
+ Component component = validateAndLockComponentAndUserBeforeWriteOperation(componentTypeEnum, componentId, userId, true);
+ return validateAndUpdatePolicyTargets(component, policyId, targets);
+ }catch (ComponentException e){
+ failed = true;
+ throw e;
+ }finally {
+ unlockComponentById(failed, componentId);
+ }
}
- private Either<Component, ResponseFormat> validateTargetsExistAndTypesCorrect(String componentId, Map<PolicyTargetType, List<String>> targets) {
+ private PolicyDefinition validateAndUpdatePolicyTargets(Component component, String policyId, Map<PolicyTargetType, List<String>> targets) {
+ validateTargetsExistAndTypesCorrect(component.getUniqueId(), targets);
+ return updateTargets(component.getUniqueId(), component.getPolicyById(policyId), targets, policyId);
+ }
+
+ private Component validateTargetsExistAndTypesCorrect(String componentId, Map<PolicyTargetType, List<String>> targets) {
Either<Component, StorageOperationStatus> componentEither = toscaOperationFacade.getToscaFullElement(componentId);
if (componentEither.isRight()) {
- return Either.right(componentsUtils.getResponseFormat(componentEither.right().value()));
+ throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(componentEither.right().value()));
}
Component parentComponent = componentEither.left().value();
return validateTargetExists(parentComponent, targets.entrySet());
}
-
- private Either<Component, ResponseFormat> validateTargetExists(Component parentComponent, Set<Map.Entry<PolicyTargetType, List<String>>> entries) {
- for(Map.Entry<PolicyTargetType, List<String>> entry : entries){
- Either<Component, ResponseFormat> result = checkTargetNotExistOnComponentByType(parentComponent, entry);
- if(result.isRight()){
- return result;
- }
+ private Component validateTargetExists(Component parentComponent, Set<Map.Entry<PolicyTargetType, List<String>>> entries) {
+ for (Map.Entry<PolicyTargetType, List<String>> entry : entries) {
+ checkTargetNotExistOnComponentByType(parentComponent, entry);
}
- return Either.left(parentComponent);
+ return parentComponent;
}
- private Either<Component, ResponseFormat> checkTargetNotExistOnComponentByType(Component parentComponent, Map.Entry<PolicyTargetType, List<String>> targetEntry) {
+ private Component checkTargetNotExistOnComponentByType(Component parentComponent, Map.Entry<PolicyTargetType, List<String>> targetEntry) {
- for(String id : targetEntry.getValue()){
- if(checkNotPresenceInComponentByType(parentComponent, id, targetEntry.getKey().getName())){
- return Either.right(componentsUtils.getResponseFormat(ActionStatus.POLICY_TARGET_DOES_NOT_EXIST, id));
+ for (String id : targetEntry.getValue()) {
+ if (checkNotPresenceInComponentByType(parentComponent, id, targetEntry.getKey().getName())) {
+ throw new ByActionStatusComponentException(ActionStatus.POLICY_TARGET_DOES_NOT_EXIST, id);
}
}
- return Either.left(parentComponent);
+ return parentComponent;
}
private boolean checkNotPresenceInComponentByType(Component parentComponent, String uniqueId, String type) {
* @param userId the user id
* @return a list of policy properties or an error in a response format
*/
- public Either<List<PropertyDataDefinition>, ResponseFormat> getPolicyProperties(ComponentTypeEnum componentType, String componentId, String policyId, String userId) {
+ public List<PropertyDataDefinition> getPolicyProperties(ComponentTypeEnum componentType, String componentId, String policyId, String userId) {
+ log.debug("#getPolicyProperties - fetching policy properties for component {} and policy {}", componentId, policyId);
+ try {
+ Component component = validateContainerComponentAndUserBeforeReadOperation(componentType, componentId, userId);
+ return getPolicyById(component, policyId).getProperties();
+ } finally {
+ janusGraphDao.commit();
+ }
+ }
+
+ /*public Either<List<PropertyDataDefinition>, ResponseFormat> getPolicyProperties(ComponentTypeEnum componentType, String componentId, String policyId, String userId) {
log.debug("#getPolicyProperties - fetching policy properties for component {} and policy {}", componentId, policyId);
try {
return validateContainerComponentAndUserBeforeReadOperation(componentType, componentId, userId)
.left()
.bind(cmpt -> getPolicyById(cmpt, policyId)).left().map(PolicyDataDefinition::getProperties);
} finally {
- titanDao.commit();
+ janusGraphDao.commit();
}
- }
+ }*/
/**
* Updates the policy properties of the component
* @param shouldLock the flag defining if the component should be locked
* @return a list of policy properties or anerrorin a response format
*/
- public Either<List<PropertyDataDefinition>, ResponseFormat> updatePolicyProperties(ComponentTypeEnum componentType, String componentId, String policyId, PropertyDataDefinition[] properties, String userId, boolean shouldLock) {
- Either<List<PropertyDataDefinition>, ResponseFormat> result = null;
+ public List<PropertyDataDefinition> updatePolicyProperties(ComponentTypeEnum componentType, String componentId, String policyId, PropertyDataDefinition[] properties, String userId, boolean shouldLock) {
+ List<PropertyDataDefinition> result;
+ Component component = null;
log.trace("#updatePolicyProperties - starting to update properties of the policy {} on the component {}. ", policyId, componentId);
- Wrapper<Component> component = new Wrapper<>();
+ boolean failed = true;
try {
- result = validateAndLockComponentAndUserBeforeWriteOperation(componentType, componentId, userId, shouldLock).left()
- .bind(c -> setComponentValidateUpdatePolicyProperties(policyId, properties, component, c));
- } catch (Exception e) {
- log.error("#updatePolicyProperties - the exception {} occurred upon update properties of the policy {} for the component {}: ", policyId, componentId, e);
- result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
+ component = validateAndLockComponentAndUserBeforeWriteOperation(componentType, componentId, userId, shouldLock);
+ failed = false;
+ result = setComponentValidateUpdatePolicyProperties(policyId, properties, component);
} finally {
- if (shouldLock && !component.isEmpty()) {
- unlockComponent(result, component.getInnerElement());
+ if (shouldLock && !failed) {
+ unlockComponent(failed, component);
}
}
return result;
}
- private Either<List<PropertyDataDefinition>, ResponseFormat> setComponentValidateUpdatePolicyProperties(String policyId, PropertyDataDefinition[] properties, Wrapper<Component> component, Component c) {
- component.setInnerElement(c);
+ @Override
+ public Either<List<PolicyDefinition>, ResponseFormat> declareProperties(String userId, String componentId,
+ ComponentTypeEnum componentTypeEnum, ComponentInstInputsMap componentInstInputsMap) {
+ return declarePropertiesToPolicies(userId, componentId, componentTypeEnum, componentInstInputsMap, true, false);
+
+ }
+
+ private Either<List<PolicyDefinition>, ResponseFormat> declarePropertiesToPolicies(String userId, String componentId,
+ ComponentTypeEnum componentTypeEnum, ComponentInstInputsMap componentInstInputsMap, boolean shouldLock,
+ boolean inTransaction) {
+ Either<List<PolicyDefinition>, ResponseFormat> result = null;
+ org.openecomp.sdc.be.model.Component component = null;
+
+ try {
+ validateUserExists(userId);
+
+ ComponentParametersView componentParametersView = new ComponentParametersView();
+ componentParametersView.disableAll();
+ componentParametersView.setIgnoreComponentInstances(false);
+ componentParametersView.setIgnoreComponentInstancesProperties(false);
+ componentParametersView.setIgnorePolicies(false);
+ componentParametersView.setIgnoreUsers(false);
+
+ component = validateComponentExists(componentId, componentTypeEnum, componentParametersView);
+
+ if (shouldLock) {
+ lockComponent(component, DECLARE_PROPERTIES_TO_POLICIES);
+ }
+
+ validateCanWorkOnComponent(component, userId);
+
+ Either<List<PolicyDefinition>, StorageOperationStatus> declarePropertiesEither =
+ propertyDeclarationOrchestrator.declarePropertiesToPolicies(component, componentInstInputsMap);
+
+ if(declarePropertiesEither.isRight()) {
+ return Either.right(componentsUtils.getResponseFormat(declarePropertiesEither.right().value()));
+ }
+
+ result = Either.left(declarePropertiesEither.left().value());
+ return result;
+ } finally {
+ if(!inTransaction) {
+ commitOrRollback(result);
+ }
+ // unlock resource
+ if (shouldLock && component != null) {
+ graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
+ }
+ }
+ }
+
+ private List<PropertyDataDefinition> setComponentValidateUpdatePolicyProperties(String policyId, PropertyDataDefinition[] properties, Component component) {
Set<String> updatedPropertyNames = Arrays.stream(properties).map(PropertyDataDefinition::getName).collect(Collectors.toSet());
- return validateAndUpdatePolicyProperties(c, policyId, properties)
- .left()
- .map(policyDefinition -> getFilteredProperties(policyDefinition.getProperties(), updatedPropertyNames));
+
+ PolicyDefinition policyDefinition = validateAndUpdatePolicyProperties(component, policyId, properties);
+ return getFilteredProperties(policyDefinition.getProperties(), updatedPropertyNames);
}
private List<PropertyDataDefinition> getFilteredProperties(List<PropertyDataDefinition> all, Set<String> filtered) {
return all.stream().filter(pd -> filtered.contains(pd.getName())).collect(Collectors.toList());
}
- private void unlockComponent(boolean shouldLock, Either<PolicyDefinition, ResponseFormat> result, Wrapper<Component> component) {
+ private void unlockComponent(boolean shouldLock, boolean result, Component component) {
+ if (shouldLock && component != null) {
+ unlockComponent(result, component);
+ }
+ }
+
+
+ private void unlockComponent(boolean shouldLock, boolean result, Wrapper<Component> component) {
if (shouldLock && !component.isEmpty()) {
unlockComponent(result, component.getInnerElement());
}
}
- private Either<PolicyDefinition, ResponseFormat> getPolicyById(Component component, String policyId) {
+ private PolicyDefinition getPolicyById(Component component, String policyId) {
PolicyDefinition policyById = component.getPolicyById(policyId);
if (policyById == null) {
String cmptId = component.getUniqueId();
log.debug("#getPolicyById - policy with id {} does not exist on component with id {}", policyId, cmptId);
- return Either.right(componentsUtils.getResponseFormat(ActionStatus.POLICY_NOT_FOUND_ON_CONTAINER, policyId, cmptId));
+ throw new ByActionStatusComponentException(ActionStatus.POLICY_NOT_FOUND_ON_CONTAINER, policyId, cmptId);
}
- return Either.left(policyById);
+ return policyById;
+ }
+
+ private PolicyDefinition createPolicy(String policyTypeName, Component component) {
+ PolicyTypeDefinition policyTypeDefinition = validatePolicyTypeOnCreatePolicy(policyTypeName, component);
+ return addPolicyToComponent(policyTypeDefinition, component);
}
- private Either<PolicyDefinition, ResponseFormat> createPolicy(String policyTypeName, Component component) {
+ /*private Either<PolicyDefinition, ResponseFormat> createPolicy(String policyTypeName, Component component) {
return validatePolicyTypeOnCreatePolicy(policyTypeName, component).left().bind(type -> addPolicyToComponent(type, component));
+ }*/
+
+ private PolicyDefinition addPolicyToComponent(PolicyTypeDefinition policyType, Component component) {
+ Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponent =
+ toscaOperationFacade.associatePolicyToComponent(component.getUniqueId(), new PolicyDefinition(policyType), getNextPolicyCounter(component.getPolicies()));
+ if(associatePolicyToComponent.isRight()){
+ throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(associatePolicyToComponent.right().value()));
+ }
+ return associatePolicyToComponent.left().value();
}
- private Either<PolicyDefinition, ResponseFormat> addPolicyToComponent(PolicyTypeDefinition policyType, Component component) {
+ /*private Either<PolicyDefinition, ResponseFormat> addPolicyToComponent(PolicyTypeDefinition policyType, Component component) {
return toscaOperationFacade.associatePolicyToComponent(component.getUniqueId(), new PolicyDefinition(policyType), getNextPolicyCounter(component.getPolicies()))
.either(Either::left, r -> Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(r))));
+ }*/
+
+ private PolicyTypeDefinition validatePolicyTypeOnCreatePolicy(String policyTypeName, Component component) {
+ Either<PolicyTypeDefinition, StorageOperationStatus> latestPolicyTypeByType = policyTypeOperation.getLatestPolicyTypeByType(policyTypeName);
+ if(latestPolicyTypeByType.isRight()){
+ throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(latestPolicyTypeByType.right().value()));
+ }
+ return validatePolicyTypeNotExcluded(latestPolicyTypeByType.left().value(), component);
}
- private Either<PolicyTypeDefinition, ResponseFormat> validatePolicyTypeOnCreatePolicy(String policyTypeName, Component component) {
+ /*private Either<PolicyTypeDefinition, ResponseFormat> validatePolicyTypeOnCreatePolicy(String policyTypeName, Component component) {
return policyTypeOperation.getLatestPolicyTypeByType(policyTypeName)
.either(l -> validatePolicyTypeNotExcluded(l, component), r -> Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(r))));
- }
+ }*/
- private Either<PolicyTypeDefinition, ResponseFormat> validatePolicyTypeNotExcluded(PolicyTypeDefinition policyType, Component component) {
+ private PolicyTypeDefinition validatePolicyTypeNotExcluded(PolicyTypeDefinition policyType, Component component) {
if (getExcludedPolicyTypesByComponent(component).contains(policyType.getType())) {
- return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCLUDED_POLICY_TYPE, policyType.getType(), getComponentOrResourceTypeName(component)));
+ throw new ByActionStatusComponentException(ActionStatus.EXCLUDED_POLICY_TYPE, policyType.getType(), getComponentOrResourceTypeName(component));
}
- return Either.left(policyType);
+ return policyType;
}
private String getComponentOrResourceTypeName(Component component) {
return component.getComponentType() == ComponentTypeEnum.SERVICE ? ComponentTypeEnum.SERVICE.name() : ((Resource) component).getResourceType().name();
}
- private Either<Component, ResponseFormat> validateAndLockComponentAndUserBeforeWriteOperation(ComponentTypeEnum componentType, String componentId, String userId, boolean shouldLock) {
+ private Component validateAndLockComponentAndUserBeforeWriteOperation(ComponentTypeEnum componentType, String componentId, String userId, boolean shouldLock) {
+ Component component = validateContainerComponentAndUserBeforeReadOperation(componentType, componentId, userId);
+ validateComponentIsTopologyTemplate(component);
+ validateCanWorkOnComponent(component, userId);
+ lockComponent(component, shouldLock, "policyWritingOperation");
+ return component;
+ }
+
+ /*private Either<Component, ResponseFormat> validateAndLockComponentAndUserBeforeWriteOperation(ComponentTypeEnum componentType, String componentId, String userId, boolean shouldLock) {
Wrapper<Component> component = new Wrapper<>();
return validateContainerComponentAndUserBeforeReadOperation(componentType, componentId, userId)
.left()
.left()
.bind(c -> {
component.setInnerElement(c);
- return validateCanWorkOnComponent(c, userId);
+ validateCanWorkOnComponent(c, userId);
+ return Either.left(component);
})
.left()
.bind(l -> lockComponent(component.getInnerElement(), shouldLock, "policyWritingOperation"))
log.error(FAILED_TO_VALIDATE_COMPONENT, componentId);
return Either.right(r);
});
+ }*/
+
+ private Component validateComponentIsTopologyTemplate(Component component) {
+ if (!component.isTopologyTemplate()) {
+ log.error("#validateComponentIsTopologyTemplate - policy association to a component of Tosca type {} is not allowed. ",
+ component.getToscaType());
+ throw new ByActionStatusComponentException(ActionStatus.RESOURCE_CANNOT_CONTAIN_POLICIES,
+ "#validateAndLockComponentAndUserBeforeWriteOperation", component.getUniqueId(), component.getToscaType());
+ }
+ return component;
}
- private Either<Component, ResponseFormat> validateComponentIsTopologyTemplate(Component component) {
+ /*private Either<Component, ResponseFormat> validateComponentIsTopologyTemplate(Component component) {
if (!component.isTopologyTemplate()) {
log.error("#validateComponentIsTopologyTemplate - policy association to a component of Tosca type {} is not allowed. ", component.getToscaType());
return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_CANNOT_CONTAIN_POLICIES, "#validateAndLockComponentAndUserBeforeWriteOperation", component.getUniqueId(), component.getToscaType()));
}
return Either.left(component);
- }
-
- private Either<Component, ResponseFormat> validateContainerComponentAndUserBeforeReadOperation(ComponentTypeEnum componentType, String componentId, String userId) {
- Either<Component, ResponseFormat> result;
+ }*/
+ private Component validateContainerComponentAndUserBeforeReadOperation(ComponentTypeEnum componentType, String componentId, String userId) {
log.trace("#validateContainerComponentAndUserBeforeReadOperation - starting to validate the user {} before policy processing. ", userId);
- validateUserExists(userId, "create Policy", false);
- result = validateComponentExists(componentType, componentId);
- if (result.isRight()) {
- log.error(FAILED_TO_VALIDATE_COMPONENT, "#validateContainerComponentAndUserBeforeReadOperation", componentId);
- }
- return result;
+ validateUserExists(userId);
+ return validateComponentExists(componentType, componentId);
}
- private Either<Component, ResponseFormat> validateComponentExists(ComponentTypeEnum componentType, String componentId) {
+ private Component validateComponentExists(ComponentTypeEnum componentType, String componentId) {
ComponentParametersView filter = new ComponentParametersView(true);
filter.setIgnorePolicies(false);
}
- private Either<PolicyDefinition, ResponseFormat> validateAndUpdatePolicy(Component component, PolicyDefinition policy) {
+ private PolicyDefinition validateAndUpdatePolicy(Component component, PolicyDefinition policy) {
+ PolicyDefinition policyById = getPolicyById(component, policy.getUniqueId());
+ PolicyDefinition policyDefinition = validateUpdatePolicyBeforeUpdate(policy, policyById, component.getPolicies());
+ return updatePolicyOfComponent(component, policyDefinition);
+ }
+
+ /*private Either<PolicyDefinition, ResponseFormat> validateAndUpdatePolicy(Component component, PolicyDefinition policy) {
return getPolicyById(component, policy.getUniqueId())
.left()
.bind(np -> validateUpdatePolicyBeforeUpdate(policy, np, component.getPolicies()))
.left()
.bind(p -> updatePolicyOfComponent(component, p));
- }
+ }*/
- private Either<PolicyDefinition, ResponseFormat> validateAndUpdatePolicyProperties(Component component, String policyId, PropertyDataDefinition[] properties) {
- return getPolicyById(component, policyId)
- .left()
- .bind(p -> validateUpdatePolicyPropertiesBeforeUpdate(p, properties))
- .left().bind(l -> updatePolicyOfComponent(component.getUniqueId(), l));
+ private PolicyDefinition validateAndUpdatePolicyProperties(Component component, String policyId, PropertyDataDefinition[] properties) {
+
+ PolicyDefinition policyById = getPolicyById(component, policyId);
+ policyById = validateUpdatePolicyPropertiesBeforeUpdate(policyById, properties);
+ return updatePolicyOfComponent(component.getUniqueId(), policyById);
}
- private Either<PolicyDefinition, ResponseFormat> updatePolicyOfComponent(String componentId, PolicyDefinition policy) {
- return toscaOperationFacade.updatePolicyOfComponent(componentId, policy)
- .right()
- .bind(r -> Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(r))));
+ private PolicyDefinition updatePolicyOfComponent(String componentId, PolicyDefinition policy) {
+ return toscaOperationFacade.updatePolicyOfComponent(componentId, policy, PromoteVersionEnum.MINOR)
+ .left()
+ .on(ce->componentExceptionPolicyDefinition(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(ce))));
}
- private Either<PolicyDefinition, ResponseFormat> validateUpdatePolicyPropertiesBeforeUpdate(PolicyDefinition policy, PropertyDataDefinition[] newProperties) {
+ private PolicyDefinition validateUpdatePolicyPropertiesBeforeUpdate(PolicyDefinition policy, PropertyDataDefinition[] newProperties) {
if (CollectionUtils.isEmpty(policy.getProperties())) {
log.error("#validateUpdatePolicyPropertiesBeforeUpdate - failed to update properites of the policy. Properties were not found on the policy. ");
- return Either.right(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND));
+ throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND);
}
return updatePropertyValues(policy, newProperties);
}
- private Either<PolicyDefinition, ResponseFormat> updatePropertyValues(PolicyDefinition policy, PropertyDataDefinition[] newProperties) {
+ private PolicyDefinition updatePropertyValues(PolicyDefinition policy, PropertyDataDefinition[] newProperties) {
Map<String, PropertyDataDefinition> oldProperties = policy.getProperties().stream().collect(toMap(PropertyDataDefinition::getName, Function.identity()));
for (PropertyDataDefinition newProperty : newProperties) {
if (!oldProperties.containsKey(newProperty.getName())) {
log.error("#updatePropertyValues - failed to update properites of the policy {}. Properties were not found on the policy. ", policy.getName());
- return Either.right(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, newProperty.getName()));
+ throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, newProperty.getName());
}
- Either<String, ResponseFormat> newPropertyValueEither = updateInputPropertyObjectValue(newProperty);
- if (newPropertyValueEither.isRight()) {
- return Either.right(newPropertyValueEither.right().value());
- }
- oldProperties.get(newProperty.getName()).setValue(newPropertyValueEither.left().value());
+ String newPropertyValueEither = updateInputPropertyObjectValue(newProperty);
+ oldProperties.get(newProperty.getName()).setValue(newPropertyValueEither);
}
- return Either.left(policy);
+ return policy;
}
- private Either<PolicyDefinition, ResponseFormat> deletePolicy(Component component, String policyId) {
- return getPolicyById(component, policyId)
- .left()
- .bind(p -> removePolicyFromComponent(component, p));
+ private PolicyDefinition deletePolicy(Component component, String policyId) {
+ PolicyDefinition policyById = getPolicyById(component, policyId);
+ return removePolicyFromComponent(component, policyById);
}
- private Either<PolicyDefinition, ResponseFormat> updatePolicyOfComponent(Component component, PolicyDefinition policy) {
- Either<PolicyDefinition, StorageOperationStatus> updatePolicyRes = toscaOperationFacade.updatePolicyOfComponent(component.getUniqueId(), policy);
+ private PolicyDefinition updatePolicyOfComponent(Component component, PolicyDefinition policy) {
+
+ Either<PolicyDefinition, StorageOperationStatus> updatePolicyRes = toscaOperationFacade.updatePolicyOfComponent(component.getUniqueId(), policy, PromoteVersionEnum.MINOR);
if (updatePolicyRes.isRight()) {
log.error("#updatePolicyOfComponent - failed to update policy {} of the component {}. The status is {}. ", policy.getUniqueId(), component.getName(), updatePolicyRes.right().value());
- return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updatePolicyRes.right().value())));
+ throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(updatePolicyRes.right().value()));
} else {
log.trace("#updatePolicyOfComponent - the policy with the name {} was updated. ", updatePolicyRes.left().value().getName());
- return Either.left(updatePolicyRes.left().value());
+ return updatePolicyRes.left().value();
}
}
- private Either<PolicyDefinition, ResponseFormat> removePolicyFromComponent(Component component, PolicyDefinition policy) {
+ private PolicyDefinition removePolicyFromComponent(Component component, PolicyDefinition policy) {
StorageOperationStatus updatePolicyStatus = toscaOperationFacade.removePolicyFromComponent(component.getUniqueId(), policy.getUniqueId());
if (updatePolicyStatus != StorageOperationStatus.OK) {
log.error("#removePolicyFromComponent - failed to remove policy {} from the component {}. The status is {}. ", policy.getUniqueId(), component.getName(), updatePolicyStatus);
- return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updatePolicyStatus)));
+ throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(updatePolicyStatus));
} else {
log.trace("#removePolicyFromComponent - the policy with the name {} was deleted. ", updatePolicyStatus);
- return Either.left(policy);
+ return policy;
}
}
- private Either<PolicyDefinition, ResponseFormat> validateUpdatePolicyBeforeUpdate(PolicyDefinition recievedPolicy, PolicyDefinition oldPolicy, Map<String, PolicyDefinition> policies) {
+ private PolicyDefinition validateUpdatePolicyBeforeUpdate(PolicyDefinition recievedPolicy, PolicyDefinition oldPolicy, Map<String, PolicyDefinition> policies) {
+
+ Either<PolicyDefinition, ActionStatus> policyDefinitionActionStatusEither = validatePolicyFields(recievedPolicy, new PolicyDefinition(oldPolicy), policies);
+ if(policyDefinitionActionStatusEither.isRight()){
+ throw new ByActionStatusComponentException(policyDefinitionActionStatusEither.right().value(), recievedPolicy.getName());
+ }
+ return policyDefinitionActionStatusEither.left().value();
+ }
+
+ /*private Either<PolicyDefinition, ResponseFormat> validateUpdatePolicyBeforeUpdate(PolicyDefinition recievedPolicy, PolicyDefinition oldPolicy, Map<String, PolicyDefinition> policies) {
return validatePolicyFields(recievedPolicy, new PolicyDefinition(oldPolicy), policies)
.right()
.bind(r -> Either.right(componentsUtils.getResponseFormat(r, recievedPolicy.getName())));
- }
+ }*/
- private Either<PolicyDefinition, ResponseFormat> updateTargets(String componentId, PolicyDefinition policy, Map<PolicyTargetType, List<String>> targets, String policyId) {
- if(policy == null){
- return Either.right(componentsUtils.getResponseFormat(ActionStatus.POLICY_NOT_FOUND_ON_CONTAINER, policyId, componentId));
+ private PolicyDefinition updateTargets(String componentId, PolicyDefinition policy, Map<PolicyTargetType, List<String>> targets, String policyId) {
+ if (policy == null) {
+ throw new ByActionStatusComponentException(ActionStatus.POLICY_NOT_FOUND_ON_CONTAINER, policyId, componentId);
}
PolicyDefinition updatedPolicy = setPolicyTargets(policy, targets);
return updatePolicyOfComponent(componentId, updatedPolicy);