Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstancePropertiesMergeBL.java
1 package org.openecomp.sdc.be.components.merge.instance;
2
3 import fj.data.Either;
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;
12
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Map;
16
17 @org.springframework.stereotype.Component
18 public class ComponentInstancePropertiesMergeBL implements ComponentsMergeCommand {
19
20     @javax.annotation.Resource
21     private ToscaOperationFacade toscaOperationFacade;
22
23     @javax.annotation.Resource(name = "componentUtils")
24     private ComponentsUtils componentsUtils;
25
26     @javax.annotation.Resource
27     private DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
28
29     @Override
30     public ActionStatus mergeComponents(Component prevComponent, Component currentComponent) {
31         Map<String, List<ComponentInstanceProperty>> newInstProps = currentComponent.getComponentInstancesProperties();
32         if (newInstProps == null) {
33             return ActionStatus.OK;
34         }
35         newInstProps.forEach((instanceId, newProps) -> mergeOldInstancePropertiesValues(prevComponent, currentComponent, instanceId, newProps) );
36         return updateComponentInstancesProperties(currentComponent, newInstProps);
37     }
38
39     @Override
40     public String description() {
41         return "merge component instance properties";
42     }
43
44
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;
49         }
50         propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstProps, oldInputs, newInstProps, newComponent.getInputs());
51         return updateComponentInstanceProperties(newComponent, instanceId, newInstProps);
52     }
53
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());
58     }
59
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());
64         }
65         return ActionStatus.OK;
66     }
67
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);
72         }
73         return ActionStatus.OK;
74     }
75
76 }