1 package org.openecomp.sdc.be.components.merge.property;
3 import java.util.Collections;
7 import org.openecomp.sdc.be.dao.api.ActionStatus;
8 import org.openecomp.sdc.be.impl.ComponentsUtils;
9 import org.openecomp.sdc.be.model.Component;
10 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
11 import org.openecomp.sdc.be.model.InputDefinition;
12 import org.openecomp.sdc.be.model.Resource;
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 {
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;
30 public ActionStatus mergeComponentInstancesProperties(Component oldComponent, Resource newResource) {
31 Map<String, List<ComponentInstanceProperty>> newInstProps = newResource.getComponentInstancesProperties();
32 if (newInstProps == null) {
33 return ActionStatus.OK;
35 newInstProps.forEach((instanceId, newProps) -> mergeOldInstancePropertiesValues(oldComponent, newResource, instanceId, newProps) );
36 return updateComponentInstancesProperties(newResource, newInstProps);
40 public ActionStatus mergeComponentInstanceProperties(List<ComponentInstanceProperty> oldInstProps, List<InputDefinition> oldInputs, Component newComponent, String instanceId) {
41 List<ComponentInstanceProperty> newInstProps = newComponent.safeGetComponentInstanceProperties(instanceId);
42 if (newInstProps == null) {
43 return ActionStatus.OK;
45 propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstProps, oldInputs, newInstProps, newComponent.getInputs());
46 return updateComponentInstanceProperties(newComponent, instanceId, newInstProps);
49 private void mergeOldInstancePropertiesValues(Component oldComponent, Component newComponent, String instanceId, List<ComponentInstanceProperty> newProps) {
50 List<ComponentInstanceProperty> oldInstProperties = oldComponent == null ? Collections.emptyList() : oldComponent.safeGetComponentInstanceProperties(instanceId);
51 List<InputDefinition> oldInputs = oldComponent == null ? Collections.emptyList() : oldComponent.getInputs();
52 propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstProperties, oldInputs, newProps, newComponent.getInputs());
55 private ActionStatus updateComponentInstancesProperties(Component newComponent, Map<String, List<ComponentInstanceProperty>> newInstProps) {
56 Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> mapStorageOperationStatusEither = toscaOperationFacade.updateComponentInstancePropsToComponent(newInstProps, newComponent.getUniqueId());
57 if (mapStorageOperationStatusEither.isRight()) {
58 return componentsUtils.convertFromStorageResponse(mapStorageOperationStatusEither.right().value());
60 return ActionStatus.OK;
63 private ActionStatus updateComponentInstanceProperties(Component component, String instanceId, List<ComponentInstanceProperty> newInstProps) {
64 StorageOperationStatus storageOperationStatus = toscaOperationFacade.updateComponentInstanceProperties(component, instanceId, newInstProps);
65 if (storageOperationStatus != StorageOperationStatus.OK) {
66 return componentsUtils.convertFromStorageResponse(storageOperationStatus);
68 return ActionStatus.OK;