1 package org.openecomp.sdc.be.components.impl;
3 import static java.util.stream.Collectors.toMap;
4 import static org.openecomp.sdc.be.components.validation.PolicyUtils.getExcludedPolicyTypesByComponent;
5 import static org.openecomp.sdc.be.components.validation.PolicyUtils.getNextPolicyCounter;
6 import static org.openecomp.sdc.be.components.validation.PolicyUtils.validatePolicyFields;
9 import java.util.Arrays;
10 import java.util.HashMap;
11 import java.util.List;
13 import java.util.Optional;
15 import java.util.function.Function;
16 import java.util.stream.Collectors;
17 import javax.inject.Inject;
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.collections.MapUtils;
20 import org.openecomp.sdc.be.components.property.PropertyDeclarationOrchestrator;
21 import org.openecomp.sdc.be.components.validation.PolicyUtils;
22 import org.openecomp.sdc.be.dao.api.ActionStatus;
23 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
24 import org.openecomp.sdc.be.datatypes.elements.PolicyDataDefinition;
25 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
26 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
27 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
28 import org.openecomp.sdc.be.model.Component;
29 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
30 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
31 import org.openecomp.sdc.be.model.ComponentParametersView;
32 import org.openecomp.sdc.be.model.PolicyDefinition;
33 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
34 import org.openecomp.sdc.be.model.Resource;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.common.datastructure.Wrapper;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
38 import org.openecomp.sdc.exception.ResponseFormat;
41 * Provides specified business logic to create, retrieve, update, delete a policy
43 @org.springframework.stereotype.Component("policyBusinessLogic")
44 public class PolicyBusinessLogic extends BaseBusinessLogic {
46 private static final String FAILED_TO_VALIDATE_COMPONENT = "#{} - failed to validate the component {} before policy processing. ";
47 private static final String DECLARE_PROPERTIES_TO_POLICIES = "declare properties to policies";
48 private static final String EXECUTE_ROLLBACK = "execute rollback";
49 private static final String EXECUTE_COMMIT = "execute commit";
50 private static final Logger log = Logger.getLogger(PolicyBusinessLogic.class);
53 private PropertyDeclarationOrchestrator propertyDeclarationOrchestrator;
55 public PolicyBusinessLogic() {
58 public PolicyBusinessLogic(PropertyDeclarationOrchestrator propertyDeclarationOrchestrator) {
59 this.propertyDeclarationOrchestrator = propertyDeclarationOrchestrator;
62 public void setPropertyDeclarationOrchestrator(PropertyDeclarationOrchestrator propertyDeclarationOrchestrator) {
63 this.propertyDeclarationOrchestrator = propertyDeclarationOrchestrator;
67 * Adds the newly created policy of the specified type to the component
69 * @param componentType the type of the component
70 * @param componentId the id of the component which the policy resides under
71 * @param policyTypeName the name of the policy type
72 * @param userId the user creator id
73 * @param shouldLock the flag defining if the component should be locked
74 * @return a policy or an error in a response format
77 public Either<PolicyDefinition, ResponseFormat> createPolicy(ComponentTypeEnum componentType, String componentId, String policyTypeName, String userId, boolean shouldLock) {
79 Either<PolicyDefinition, ResponseFormat> result = null;
80 log.trace("#createPolicy - starting to create policy of the type {} on the component {}. ", policyTypeName, componentId);
81 Wrapper<Component> component = new Wrapper<>();
83 result = validateAndLockComponentAndUserBeforeWriteOperation(componentType, componentId, userId, shouldLock)
86 component.setInnerElement(c);
87 return createPolicy(policyTypeName, c);
89 } catch (Exception e) {
90 log.error("#createPolicy - the exception occurred upon creation of a policy of the type {} for the component {}: ", policyTypeName, componentId, e);
91 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
94 unlockComponent(shouldLock, result, component);
99 public Either<List<PolicyDefinition>, ResponseFormat> getPoliciesList(ComponentTypeEnum componentType, String componentId, String userId) {
100 Either<List<PolicyDefinition>, ResponseFormat> result;
101 log.trace("#getPolicies - starting to retrieve policies of component {}. ", componentId);
103 result = validateContainerComponentAndUserBeforeReadOperation(componentType, componentId, userId)
105 .bind(c -> Either.left(c.resolvePoliciesList()));
106 } catch (Exception e) {
107 log.error("#getPolicy - the exception occurred upon retrieving policies list of component {}: ", componentId, e);
108 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
114 * Retrieves the policy of the component by UniqueId
116 * @param componentType the type of the component
117 * @param componentId the ID of the component
118 * @param policyId the ID of the policy
119 * @param userId the ID of the user
120 * @return either policy or error response
122 public Either<PolicyDefinition, ResponseFormat> getPolicy(ComponentTypeEnum componentType, String componentId, String policyId, String userId) {
123 Either<PolicyDefinition, ResponseFormat> result;
124 log.trace("#getPolicy - starting to retrieve the policy {} of the component {}. ", policyId, componentId);
126 result = validateContainerComponentAndUserBeforeReadOperation(componentType, componentId, userId)
128 .bind(c -> getPolicyById(c, policyId));
129 } catch (Exception e) {
130 log.error("#getPolicy - the exception occurred upon retrieving the policy {} of the component {}: ", policyId, componentId, e);
131 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
137 * Updates the policy of the component
139 * @param componentType the type of the component
140 * @param componentId the id of the component which the policy resides under
141 * @param policy the policy to update
142 * @param userId the user modifier id
143 * @param shouldLock the flag defining if the component should be locked
144 * @return a policy or an error in a response format
146 public Either<PolicyDefinition, ResponseFormat> updatePolicy(ComponentTypeEnum componentType, String componentId, PolicyDefinition policy, String userId, boolean shouldLock) {
147 Either<PolicyDefinition, ResponseFormat> result = null;
148 log.trace("#updatePolicy - starting to update the policy {} on the component {}. ", policy.getUniqueId(), componentId);
149 Wrapper<Component> component = new Wrapper<>();
151 result = validateAndLockComponentAndUserBeforeWriteOperation(componentType, componentId, userId, shouldLock)
154 component.setInnerElement(c);
155 return validateAndUpdatePolicy(c, policy);
157 } catch (Exception e) {
158 log.error("#updatePolicy - the exception occurred upon update of a policy of the type {} for the component {}: ", policy.getUniqueId(), componentId, e);
159 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
161 unlockComponent(shouldLock, result, component);
167 * Deletes the policy from the component
169 * @param componentType the type of the component
170 * @param componentId the id of the component which the policy resides under
171 * @param policyId the id of the policy which its properties to return
172 * @param userId the user modifier id
173 * @param shouldLock the flag defining if the component should be locked
174 * @return a policy or an error in a response format
176 public Either<PolicyDefinition, ResponseFormat> deletePolicy(ComponentTypeEnum componentType, String componentId, String policyId, String userId, boolean shouldLock) {
177 Either<PolicyDefinition, ResponseFormat> result = null;
178 log.trace("#deletePolicy - starting to update the policy {} on the component {}. ", policyId, componentId);
179 Wrapper<Component> component = new Wrapper<>();
181 Either<Component, ResponseFormat> componentEither =
182 validateAndLockComponentAndUserBeforeWriteOperation(componentType, componentId, userId, shouldLock);
183 if (componentEither.isRight()) {
184 return Either.right(componentEither.right().value());
187 ComponentParametersView componentParametersView = new ComponentParametersView();
188 componentParametersView.disableAll();
189 componentParametersView.setIgnoreComponentInstances(false);
190 componentParametersView.setIgnoreComponentInstancesProperties(false);
191 componentParametersView.setIgnorePolicies(false);
192 componentParametersView.setIgnoreProperties(false);
194 Either<Component, StorageOperationStatus> componentWithFilters =
195 toscaOperationFacade.getToscaElement(componentId, componentParametersView);
196 if (componentWithFilters.isRight()) {
197 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(componentWithFilters.right().value())));
200 Component containerComponent = componentWithFilters.left().value();
201 component.setInnerElement(containerComponent);
202 result = deletePolicy(containerComponent, policyId);
204 if(result.isRight()) {
205 log.error("#deletePolicy - could not delete policy of the type {} for the component {}: ", policyId, componentId);
209 PolicyDefinition policyToDelete = result.left().value();
211 StorageOperationStatus storageOperationStatus = propertyDeclarationOrchestrator.unDeclarePropertiesAsPolicies(
212 containerComponent, policyToDelete);
213 if (storageOperationStatus != StorageOperationStatus.OK) {
214 log.debug("Component id: {} update properties declared as policy for policy id: {} failed", componentId, policyId);
215 return Either.right(componentsUtils.getResponseFormat(componentsUtils
216 .convertFromStorageResponse(storageOperationStatus), containerComponent.getName()));
220 } catch (Exception e) {
221 log.error("#deletePolicy - the exception occurred upon update of a policy of the type {} for the component {}: ", policyId, componentId, e);
222 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR, e.getMessage()));
224 unlockComponent(shouldLock, result, component);
228 public Either<PolicyDefinition, ResponseFormat> undeclarePolicy(ComponentTypeEnum componentType, String componentId, String policyId, String userId, boolean shouldLock) {
229 Either<PolicyDefinition, ResponseFormat> result = null;
230 log.trace("#undeclarePolicy - starting to undeclare policy {} on component {}. ", policyId, componentId);
231 Wrapper<Component> component = new Wrapper<>();
233 Either<Component, ResponseFormat> componentEither =
234 validateAndLockComponentAndUserBeforeWriteOperation(componentType, componentId, userId, shouldLock);
235 if (componentEither.isRight()) {
236 return Either.right(componentEither.right().value());
239 ComponentParametersView componentParametersView = new ComponentParametersView();
240 componentParametersView.disableAll();
241 componentParametersView.setIgnoreComponentInstances(false);
242 componentParametersView.setIgnoreComponentInstancesProperties(false);
243 componentParametersView.setIgnorePolicies(false);
245 Either<Component, StorageOperationStatus> componentWithFilters =
246 toscaOperationFacade.getToscaElement(componentId, componentParametersView);
247 if (componentWithFilters.isRight()) {
248 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(componentWithFilters.right().value())));
251 Component containerComponent = componentWithFilters.left().value();
253 Optional<PolicyDefinition> policyCandidate = getPolicyForUndeclaration(policyId, containerComponent);
254 if(policyCandidate.isPresent()) {
255 result = undeclarePolicy(policyCandidate.get(), containerComponent);
259 } catch (Exception e) {
260 log.error("#undeclarePolicy - the exception occurred upon update of a policy of type {} for component {}: ", policyId, componentId, e);
261 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR, e.getMessage()));
263 unlockComponent(shouldLock, result, component);
268 private Either<PolicyDefinition, ResponseFormat> undeclarePolicy(PolicyDefinition policyDefinition, Component containerComponent) {
269 StorageOperationStatus undeclareStatus = propertyDeclarationOrchestrator
270 .unDeclarePropertiesAsPolicies(containerComponent, policyDefinition);
271 if(undeclareStatus != StorageOperationStatus.OK){
272 return Either.right(componentsUtils.getResponseFormat(undeclareStatus));
274 return Either.left(policyDefinition);
279 private Optional<PolicyDefinition> getPolicyForUndeclaration(String policyId, Component component) {
280 Map<String, PolicyDefinition> policies = component.getPolicies();
281 if(MapUtils.isNotEmpty(policies) && policies.containsKey(policyId)) {
282 return Optional.of(policies.get(policyId));
285 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties =
286 MapUtils.isEmpty(component.getComponentInstancesProperties()) ? new HashMap<>() : component.getComponentInstancesProperties();
288 for(Map.Entry<String, List<ComponentInstanceProperty>> instancePropertyEntry : componentInstancesProperties.entrySet()) {
289 Optional<ComponentInstanceProperty> propertyCandidate = getPropertyForDeclaredPolicy(policyId, instancePropertyEntry.getValue());
291 if(propertyCandidate.isPresent()) {
293 PolicyUtils.getDeclaredPolicyDefinition(instancePropertyEntry.getKey(), propertyCandidate.get()));
297 return Optional.empty();
300 private Optional<ComponentInstanceProperty> getPropertyForDeclaredPolicy(String policyId, List<ComponentInstanceProperty> componentInstanceProperties) {
301 for(ComponentInstanceProperty property : componentInstanceProperties) {
302 Optional<GetPolicyValueDataDefinition> getPolicyCandidate = property.safeGetGetPolicyValues().stream()
303 .filter(getPolicyValue -> getPolicyValue.getPolicyId()
307 if(getPolicyCandidate.isPresent()) {
308 return Optional.of(property);
312 return Optional.empty();
315 public Either<PolicyDefinition, ResponseFormat> updatePolicyTargets(ComponentTypeEnum componentTypeEnum, String componentId, String policyId, Map<PolicyTargetType, List<String>> targets, String userId) {
317 Either<PolicyDefinition, ResponseFormat> result = null;
318 log.debug("updating the policy id {} targets with the components {}. ", policyId, componentId);
320 //not right error response
321 result = validateAndLockComponentAndUserBeforeWriteOperation(componentTypeEnum, componentId, userId, true)
323 .bind(cmpt -> validateAndUpdatePolicyTargets(cmpt, policyId, targets));
328 unlockComponentById(result, componentId);
334 private Either<PolicyDefinition, ResponseFormat> validateAndUpdatePolicyTargets(Component component, String policyId, Map<PolicyTargetType, List<String>> targets) {
335 return validateTargetsExistAndTypesCorrect(component.getUniqueId(), targets)
337 .bind(cmp ->updateTargets(component.getUniqueId(), component.getPolicyById(policyId), targets, policyId));
341 private Either<Component, ResponseFormat> validateTargetsExistAndTypesCorrect(String componentId, Map<PolicyTargetType, List<String>> targets) {
342 Either<Component, StorageOperationStatus> componentEither = toscaOperationFacade.getToscaFullElement(componentId);
343 if (componentEither.isRight()) {
344 return Either.right(componentsUtils.getResponseFormat(componentEither.right().value()));
346 Component parentComponent = componentEither.left().value();
347 return validateTargetExists(parentComponent, targets.entrySet());
352 private Either<Component, ResponseFormat> validateTargetExists(Component parentComponent, Set<Map.Entry<PolicyTargetType, List<String>>> entries) {
353 for(Map.Entry<PolicyTargetType, List<String>> entry : entries){
354 Either<Component, ResponseFormat> result = checkTargetNotExistOnComponentByType(parentComponent, entry);
355 if(result.isRight()){
359 return Either.left(parentComponent);
362 private Either<Component, ResponseFormat> checkTargetNotExistOnComponentByType(Component parentComponent, Map.Entry<PolicyTargetType, List<String>> targetEntry) {
364 for(String id : targetEntry.getValue()){
365 if(checkNotPresenceInComponentByType(parentComponent, id, targetEntry.getKey().getName())){
366 return Either.right(componentsUtils.getResponseFormat(ActionStatus.POLICY_TARGET_DOES_NOT_EXIST, id));
369 return Either.left(parentComponent);
372 private boolean checkNotPresenceInComponentByType(Component parentComponent, String uniqueId, String type) {
373 if (type.equalsIgnoreCase(PolicyTargetType.GROUPS.getName()) && parentComponent.getGroups() != null) {
374 return !parentComponent.getGroupById(uniqueId).isPresent();
375 } else if (type.equalsIgnoreCase(PolicyTargetType.COMPONENT_INSTANCES.getName()) && parentComponent.getComponentInstances() != null) {
376 return !parentComponent.getComponentInstanceById(uniqueId).isPresent();
381 private PolicyDefinition setPolicyTargets(PolicyDefinition policyDefinition, Map<PolicyTargetType, List<String>> targets) {
382 policyDefinition.setTargets(targets);
383 return policyDefinition;
388 * @param componentType the type of the component
389 * @param componentId the id of the component which the policy resides under
390 * @param policyId the id of the policy which its properties to return
391 * @param userId the user id
392 * @return a list of policy properties or an error in a response format
394 public Either<List<PropertyDataDefinition>, ResponseFormat> getPolicyProperties(ComponentTypeEnum componentType, String componentId, String policyId, String userId) {
395 log.debug("#getPolicyProperties - fetching policy properties for component {} and policy {}", componentId, policyId);
397 return validateContainerComponentAndUserBeforeReadOperation(componentType, componentId, userId)
399 .bind(cmpt -> getPolicyById(cmpt, policyId)).left().map(PolicyDataDefinition::getProperties);
401 janusGraphDao.commit();
406 * Updates the policy properties of the component
408 * @param componentType the type of the component
409 * @param componentId the id of the component which the policy resides under
410 * @param policyId the id of the policy which its properties to return
411 * @param properties a list of policy properties containing updated values
412 * @param userId the user modifier id
413 * @param shouldLock the flag defining if the component should be locked
414 * @return a list of policy properties or anerrorin a response format
416 public Either<List<PropertyDataDefinition>, ResponseFormat> updatePolicyProperties(ComponentTypeEnum componentType, String componentId, String policyId, PropertyDataDefinition[] properties, String userId, boolean shouldLock) {
417 Either<List<PropertyDataDefinition>, ResponseFormat> result = null;
418 log.trace("#updatePolicyProperties - starting to update properties of the policy {} on the component {}. ", policyId, componentId);
419 Wrapper<Component> component = new Wrapper<>();
421 result = validateAndLockComponentAndUserBeforeWriteOperation(componentType, componentId, userId, shouldLock).left()
422 .bind(c -> setComponentValidateUpdatePolicyProperties(policyId, properties, component, c));
423 } catch (Exception e) {
424 log.error("#updatePolicyProperties - the exception {} occurred upon update properties of the policy {} for the component {}: ", policyId, componentId, e);
425 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
427 if (shouldLock && !component.isEmpty()) {
428 unlockComponent(result, component.getInnerElement());
435 public Either<List<PolicyDefinition>, ResponseFormat> declareProperties(String userId, String componentId,
436 ComponentTypeEnum componentTypeEnum, ComponentInstInputsMap componentInstInputsMap) {
437 return declarePropertiesToPolicies(userId, componentId, componentTypeEnum, componentInstInputsMap, true, false);
441 private Either<List<PolicyDefinition>, ResponseFormat> declarePropertiesToPolicies(String userId, String componentId,
442 ComponentTypeEnum componentTypeEnum, ComponentInstInputsMap componentInstInputsMap, boolean shouldLock,
443 boolean inTransaction) {
444 Either<List<PolicyDefinition>, ResponseFormat> result = null;
445 org.openecomp.sdc.be.model.Component component = null;
448 validateUserExists(userId, DECLARE_PROPERTIES_TO_POLICIES, false);
450 ComponentParametersView componentParametersView = new ComponentParametersView();
451 componentParametersView.disableAll();
452 componentParametersView.setIgnoreComponentInstances(false);
453 componentParametersView.setIgnoreComponentInstancesProperties(false);
454 componentParametersView.setIgnorePolicies(false);
455 componentParametersView.setIgnoreUsers(false);
457 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponent = validateComponentExists(componentId, componentTypeEnum, componentParametersView);
459 if (validateComponent.isRight()) {
460 result = Either.right(validateComponent.right().value());
463 component = validateComponent.left().value();
466 Either<Boolean, ResponseFormat> lockComponent = lockComponent(component, DECLARE_PROPERTIES_TO_POLICIES);
467 if (lockComponent.isRight()) {
468 result = Either.right(lockComponent.right().value());
473 Either<Boolean, ResponseFormat> canWork = validateCanWorkOnComponent(component, userId);
474 if (canWork.isRight()) {
475 result = Either.right(canWork.right().value());
479 Either<List<PolicyDefinition>, StorageOperationStatus> declarePropertiesEither =
480 propertyDeclarationOrchestrator.declarePropertiesToPolicies(component, componentInstInputsMap);
482 if(declarePropertiesEither.isRight()) {
483 return Either.right(componentsUtils.getResponseFormat(declarePropertiesEither.right().value()));
486 result = Either.left(declarePropertiesEither.left().value());
490 commitOrRollback(result);
493 if (shouldLock && component != null) {
494 graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
499 private Either<List<PropertyDataDefinition>, ResponseFormat> setComponentValidateUpdatePolicyProperties(String policyId, PropertyDataDefinition[] properties, Wrapper<Component> component, Component c) {
500 component.setInnerElement(c);
501 Set<String> updatedPropertyNames = Arrays.stream(properties).map(PropertyDataDefinition::getName).collect(Collectors.toSet());
502 return validateAndUpdatePolicyProperties(c, policyId, properties)
504 .map(policyDefinition -> getFilteredProperties(policyDefinition.getProperties(), updatedPropertyNames));
507 private List<PropertyDataDefinition> getFilteredProperties(List<PropertyDataDefinition> all, Set<String> filtered) {
508 return all.stream().filter(pd -> filtered.contains(pd.getName())).collect(Collectors.toList());
511 private void unlockComponent(boolean shouldLock, Either<PolicyDefinition, ResponseFormat> result, Wrapper<Component> component) {
512 if (shouldLock && !component.isEmpty()) {
513 unlockComponent(result, component.getInnerElement());
517 private Either<PolicyDefinition, ResponseFormat> getPolicyById(Component component, String policyId) {
518 PolicyDefinition policyById = component.getPolicyById(policyId);
519 if (policyById == null) {
520 String cmptId = component.getUniqueId();
521 log.debug("#getPolicyById - policy with id {} does not exist on component with id {}", policyId, cmptId);
522 return Either.right(componentsUtils.getResponseFormat(ActionStatus.POLICY_NOT_FOUND_ON_CONTAINER, policyId, cmptId));
524 return Either.left(policyById);
527 private Either<PolicyDefinition, ResponseFormat> createPolicy(String policyTypeName, Component component) {
528 return validatePolicyTypeOnCreatePolicy(policyTypeName, component).left().bind(type -> addPolicyToComponent(type, component));
531 private Either<PolicyDefinition, ResponseFormat> addPolicyToComponent(PolicyTypeDefinition policyType, Component component) {
532 return toscaOperationFacade.associatePolicyToComponent(component.getUniqueId(), new PolicyDefinition(policyType), getNextPolicyCounter(component.getPolicies()))
533 .either(Either::left, r -> Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(r))));
536 private Either<PolicyTypeDefinition, ResponseFormat> validatePolicyTypeOnCreatePolicy(String policyTypeName, Component component) {
537 return policyTypeOperation.getLatestPolicyTypeByType(policyTypeName)
538 .either(l -> validatePolicyTypeNotExcluded(l, component), r -> Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(r))));
541 private Either<PolicyTypeDefinition, ResponseFormat> validatePolicyTypeNotExcluded(PolicyTypeDefinition policyType, Component component) {
542 if (getExcludedPolicyTypesByComponent(component).contains(policyType.getType())) {
543 return Either.right(componentsUtils.getResponseFormat(ActionStatus.EXCLUDED_POLICY_TYPE, policyType.getType(), getComponentOrResourceTypeName(component)));
545 return Either.left(policyType);
548 private String getComponentOrResourceTypeName(Component component) {
549 return component.getComponentType() == ComponentTypeEnum.SERVICE ? ComponentTypeEnum.SERVICE.name() : ((Resource) component).getResourceType().name();
552 private Either<Component, ResponseFormat> validateAndLockComponentAndUserBeforeWriteOperation(ComponentTypeEnum componentType, String componentId, String userId, boolean shouldLock) {
553 Wrapper<Component> component = new Wrapper<>();
554 return validateContainerComponentAndUserBeforeReadOperation(componentType, componentId, userId)
556 .bind(this::validateComponentIsTopologyTemplate)
559 component.setInnerElement(c);
560 return validateCanWorkOnComponent(c, userId);
563 .bind(l -> lockComponent(component.getInnerElement(), shouldLock, "policyWritingOperation"))
564 .either(l -> Either.left(component.getInnerElement()), r -> {
565 log.error(FAILED_TO_VALIDATE_COMPONENT, componentId);
566 return Either.right(r);
570 private Either<Component, ResponseFormat> validateComponentIsTopologyTemplate(Component component) {
571 if (!component.isTopologyTemplate()) {
572 log.error("#validateComponentIsTopologyTemplate - policy association to a component of Tosca type {} is not allowed. ", component.getToscaType());
573 return Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_CANNOT_CONTAIN_POLICIES, "#validateAndLockComponentAndUserBeforeWriteOperation", component.getUniqueId(), component.getToscaType()));
575 return Either.left(component);
578 private Either<Component, ResponseFormat> validateContainerComponentAndUserBeforeReadOperation(ComponentTypeEnum componentType, String componentId, String userId) {
579 Either<Component, ResponseFormat> result;
580 log.trace("#validateContainerComponentAndUserBeforeReadOperation - starting to validate the user {} before policy processing. ", userId);
581 validateUserExists(userId, "create Policy", false);
582 result = validateComponentExists(componentType, componentId);
583 if (result.isRight()) {
584 log.error(FAILED_TO_VALIDATE_COMPONENT, "#validateContainerComponentAndUserBeforeReadOperation", componentId);
589 private Either<Component, ResponseFormat> validateComponentExists(ComponentTypeEnum componentType, String componentId) {
591 ComponentParametersView filter = new ComponentParametersView(true);
592 filter.setIgnorePolicies(false);
593 filter.setIgnoreUsers(false);
594 filter.setIgnoreComponentInstances(false);
595 filter.setIgnoreGroups(false);
596 return validateComponentExists(componentId, componentType, filter);
600 private Either<PolicyDefinition, ResponseFormat> validateAndUpdatePolicy(Component component, PolicyDefinition policy) {
601 return getPolicyById(component, policy.getUniqueId())
603 .bind(np -> validateUpdatePolicyBeforeUpdate(policy, np, component.getPolicies()))
605 .bind(p -> updatePolicyOfComponent(component, p));
608 private Either<PolicyDefinition, ResponseFormat> validateAndUpdatePolicyProperties(Component component, String policyId, PropertyDataDefinition[] properties) {
609 return getPolicyById(component, policyId)
611 .bind(p -> validateUpdatePolicyPropertiesBeforeUpdate(p, properties))
612 .left().bind(l -> updatePolicyOfComponent(component.getUniqueId(), l));
615 private Either<PolicyDefinition, ResponseFormat> updatePolicyOfComponent(String componentId, PolicyDefinition policy) {
616 return toscaOperationFacade.updatePolicyOfComponent(componentId, policy)
618 .bind(r -> Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(r))));
621 private Either<PolicyDefinition, ResponseFormat> validateUpdatePolicyPropertiesBeforeUpdate(PolicyDefinition policy, PropertyDataDefinition[] newProperties) {
622 if (CollectionUtils.isEmpty(policy.getProperties())) {
623 log.error("#validateUpdatePolicyPropertiesBeforeUpdate - failed to update properites of the policy. Properties were not found on the policy. ");
624 return Either.right(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND));
626 return updatePropertyValues(policy, newProperties);
629 private Either<PolicyDefinition, ResponseFormat> updatePropertyValues(PolicyDefinition policy, PropertyDataDefinition[] newProperties) {
631 Map<String, PropertyDataDefinition> oldProperties = policy.getProperties().stream().collect(toMap(PropertyDataDefinition::getName, Function.identity()));
632 for (PropertyDataDefinition newProperty : newProperties) {
633 if (!oldProperties.containsKey(newProperty.getName())) {
634 log.error("#updatePropertyValues - failed to update properites of the policy {}. Properties were not found on the policy. ", policy.getName());
635 return Either.right(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, newProperty.getName()));
637 Either<String, ResponseFormat> newPropertyValueEither = updateInputPropertyObjectValue(newProperty);
638 if (newPropertyValueEither.isRight()) {
639 return Either.right(newPropertyValueEither.right().value());
641 oldProperties.get(newProperty.getName()).setValue(newPropertyValueEither.left().value());
643 return Either.left(policy);
646 private Either<PolicyDefinition, ResponseFormat> deletePolicy(Component component, String policyId) {
647 return getPolicyById(component, policyId)
649 .bind(p -> removePolicyFromComponent(component, p));
652 private Either<PolicyDefinition, ResponseFormat> updatePolicyOfComponent(Component component, PolicyDefinition policy) {
653 Either<PolicyDefinition, StorageOperationStatus> updatePolicyRes = toscaOperationFacade.updatePolicyOfComponent(component.getUniqueId(), policy);
654 if (updatePolicyRes.isRight()) {
655 log.error("#updatePolicyOfComponent - failed to update policy {} of the component {}. The status is {}. ", policy.getUniqueId(), component.getName(), updatePolicyRes.right().value());
656 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updatePolicyRes.right().value())));
658 log.trace("#updatePolicyOfComponent - the policy with the name {} was updated. ", updatePolicyRes.left().value().getName());
659 return Either.left(updatePolicyRes.left().value());
663 private Either<PolicyDefinition, ResponseFormat> removePolicyFromComponent(Component component, PolicyDefinition policy) {
664 StorageOperationStatus updatePolicyStatus = toscaOperationFacade.removePolicyFromComponent(component.getUniqueId(), policy.getUniqueId());
665 if (updatePolicyStatus != StorageOperationStatus.OK) {
666 log.error("#removePolicyFromComponent - failed to remove policy {} from the component {}. The status is {}. ", policy.getUniqueId(), component.getName(), updatePolicyStatus);
667 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updatePolicyStatus)));
669 log.trace("#removePolicyFromComponent - the policy with the name {} was deleted. ", updatePolicyStatus);
670 return Either.left(policy);
674 private Either<PolicyDefinition, ResponseFormat> validateUpdatePolicyBeforeUpdate(PolicyDefinition recievedPolicy, PolicyDefinition oldPolicy, Map<String, PolicyDefinition> policies) {
675 return validatePolicyFields(recievedPolicy, new PolicyDefinition(oldPolicy), policies)
677 .bind(r -> Either.right(componentsUtils.getResponseFormat(r, recievedPolicy.getName())));
680 private Either<PolicyDefinition, ResponseFormat> updateTargets(String componentId, PolicyDefinition policy, Map<PolicyTargetType, List<String>> targets, String policyId) {
682 return Either.right(componentsUtils.getResponseFormat(ActionStatus.POLICY_NOT_FOUND_ON_CONTAINER, policyId, componentId));
684 PolicyDefinition updatedPolicy = setPolicyTargets(policy, targets);
685 return updatePolicyOfComponent(componentId, updatedPolicy);