1 package org.openecomp.sdc.be.components.merge.instance;
4 import org.openecomp.sdc.be.components.merge.property.DataDefinitionsValuesMergingBusinessLogic;
5 import org.openecomp.sdc.be.dao.api.ActionStatus;
6 import org.openecomp.sdc.be.impl.ComponentsUtils;
7 import org.openecomp.sdc.be.model.Component;
8 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
9 import org.openecomp.sdc.be.model.InputDefinition;
10 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
11 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
13 import java.util.Collections;
14 import java.util.List;
17 @org.springframework.stereotype.Component
18 public class ComponentInstancePropertiesMergeBL implements ComponentsMergeCommand {
20 @javax.annotation.Resource
21 private ToscaOperationFacade toscaOperationFacade;
23 @javax.annotation.Resource(name = "componentUtils")
24 private ComponentsUtils componentsUtils;
26 @javax.annotation.Resource
27 private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
30 public ActionStatus mergeComponents(Component prevComponent, Component currentComponent) {
31 Map<String, List<ComponentInstanceProperty>> newInstProps = currentComponent.getComponentInstancesProperties();
32 if (newInstProps == null) {
33 return ActionStatus.OK;
35 newInstProps.forEach((instanceId, newProps) -> mergeOldInstancePropertiesValues(prevComponent, currentComponent, instanceId, newProps) );
36 return updateComponentInstancesProperties(currentComponent, newInstProps);
40 public String description() {
41 return "merge component instance properties";
45 public ActionStatus mergeComponentInstanceProperties(List<ComponentInstanceProperty> oldInstProps, List<InputDefinition> oldInputs, Component newComponent, String instanceId) {
46 List<ComponentInstanceProperty> newInstProps = newComponent.safeGetComponentInstanceProperties(instanceId);
47 if (newInstProps == null) {
48 return ActionStatus.OK;
50 propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstProps, oldInputs, newInstProps, newComponent.getInputs());
51 return updateComponentInstanceProperties(newComponent, instanceId, newInstProps);
54 private void mergeOldInstancePropertiesValues(Component oldComponent, Component newComponent, String instanceId, List<ComponentInstanceProperty> newProps) {
55 List<ComponentInstanceProperty> oldInstProperties = oldComponent == null ? Collections.emptyList() : oldComponent.safeGetComponentInstanceProperties(instanceId);
56 List<InputDefinition> oldInputs = oldComponent == null ? Collections.emptyList() : oldComponent.getInputs();
57 propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstProperties, oldInputs, newProps, newComponent.getInputs());
60 private ActionStatus updateComponentInstancesProperties(Component newComponent, Map<String, List<ComponentInstanceProperty>> newInstProps) {
61 Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> mapStorageOperationStatusEither = toscaOperationFacade.updateComponentInstancePropsToComponent(newInstProps, newComponent.getUniqueId());
62 if (mapStorageOperationStatusEither.isRight()) {
63 return componentsUtils.convertFromStorageResponse(mapStorageOperationStatusEither.right().value());
65 return ActionStatus.OK;
68 private ActionStatus updateComponentInstanceProperties(Component component, String instanceId, List<ComponentInstanceProperty> newInstProps) {
69 StorageOperationStatus storageOperationStatus = toscaOperationFacade.updateComponentInstanceProperties(component, instanceId, newInstProps);
70 if (storageOperationStatus != StorageOperationStatus.OK) {
71 return componentsUtils.convertFromStorageResponse(storageOperationStatus);
73 return ActionStatus.OK;