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