1 package org.openecomp.sdc.be.components.merge.instance;
3 import java.util.Collections;
7 import org.openecomp.sdc.be.components.merge.property.DataDefinitionsValuesMergingBusinessLogic;
8 import org.openecomp.sdc.be.dao.api.ActionStatus;
9 import org.openecomp.sdc.be.impl.ComponentsUtils;
10 import org.openecomp.sdc.be.model.Component;
11 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
12 import org.openecomp.sdc.be.model.InputDefinition;
13 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
14 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
16 import fj.data.Either;
18 @org.springframework.stereotype.Component
19 public class ComponentInstancePropertiesMergeBL implements ComponentsMergeCommand {
21 @javax.annotation.Resource
22 private ToscaOperationFacade toscaOperationFacade;
24 @javax.annotation.Resource(name = "componentUtils")
25 private ComponentsUtils componentsUtils;
27 @javax.annotation.Resource
28 private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
31 public ActionStatus mergeComponents(Component prevComponent, Component currentComponent) {
32 Map<String, List<ComponentInstanceProperty>> newInstProps = currentComponent.getComponentInstancesProperties();
33 if (newInstProps == null) {
34 return ActionStatus.OK;
36 newInstProps.forEach((instanceId, newProps) -> mergeOldInstancePropertiesValues(prevComponent, currentComponent, instanceId, newProps) );
37 return updateComponentInstancesProperties(currentComponent, newInstProps);
41 public String description() {
42 return "merge component instance properties";
46 public ActionStatus mergeComponentInstanceProperties(List<ComponentInstanceProperty> oldInstProps, List<InputDefinition> oldInputs, Component newComponent, String instanceId) {
47 List<ComponentInstanceProperty> newInstProps = newComponent.safeGetComponentInstanceProperties(instanceId);
48 if (newInstProps == null) {
49 return ActionStatus.OK;
51 propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstProps, oldInputs, newInstProps, newComponent.getInputs());
52 return updateComponentInstanceProperties(newComponent, instanceId, newInstProps);
55 private void mergeOldInstancePropertiesValues(Component oldComponent, Component newComponent, String instanceId, List<ComponentInstanceProperty> newProps) {
56 List<ComponentInstanceProperty> oldInstProperties = oldComponent == null ? Collections.emptyList() : oldComponent.safeGetComponentInstanceProperties(instanceId);
57 List<InputDefinition> oldInputs = oldComponent == null ? Collections.emptyList() : oldComponent.getInputs();
58 propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstProperties, oldInputs, newProps, newComponent.getInputs());
61 private ActionStatus updateComponentInstancesProperties(Component newComponent, Map<String, List<ComponentInstanceProperty>> newInstProps) {
62 Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> mapStorageOperationStatusEither = toscaOperationFacade.updateComponentInstancePropsToComponent(newInstProps, newComponent.getUniqueId());
63 if (mapStorageOperationStatusEither.isRight()) {
64 return componentsUtils.convertFromStorageResponse(mapStorageOperationStatusEither.right().value());
66 return ActionStatus.OK;
69 private ActionStatus updateComponentInstanceProperties(Component component, String instanceId, List<ComponentInstanceProperty> newInstProps) {
70 StorageOperationStatus storageOperationStatus = toscaOperationFacade.updateComponentInstanceProperties(component, instanceId, newInstProps);
71 if (storageOperationStatus != StorageOperationStatus.OK) {
72 return componentsUtils.convertFromStorageResponse(storageOperationStatus);
74 return ActionStatus.OK;