Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstancePropsAndInputsMerge.java
1 package org.openecomp.sdc.be.components.merge.instance;
2
3 import fj.data.Either;
4 import org.openecomp.sdc.be.components.merge.input.ComponentInputsMergeBL;
5 import org.openecomp.sdc.be.dao.api.ActionStatus;
6 import org.openecomp.sdc.be.impl.ComponentsUtils;
7 import org.openecomp.sdc.be.model.*;
8 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
9 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
10 import org.openecomp.sdc.exception.ResponseFormat;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.springframework.beans.factory.annotation.Autowired;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 /**
19  * Created by chaya on 9/20/2017.
20  */
21 @org.springframework.stereotype.Component("ComponentInstancePropsAndInputsMerge")
22 public class ComponentInstancePropsAndInputsMerge implements ComponentInstanceMergeInterface {
23
24     private static final Logger LOGGER = LoggerFactory.getLogger(ComponentInstancePropsAndInputsMerge.class);
25
26     @Autowired
27     private ToscaOperationFacade toscaOperationFacade;
28
29     @Autowired
30     private ComponentsUtils componentsUtils;
31
32     @Autowired
33     private ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL;
34
35     @Autowired
36     private ComponentInstanceInputsMergeBL resourceInstanceInputsMergeBL;
37
38     @Autowired
39     private ComponentInputsMergeBL resourceInputsMergeBL;
40
41     @Override
42     public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent, ComponentInstance currentResourceInstance, Component originComponent) {
43         dataHolder.setOrigComponentInstanceInputs(containerComponent.safeGetComponentInstanceInputsByName(currentResourceInstance.getName()));
44         dataHolder.setOrigComponentInstanceProperties(containerComponent.safeGetComponentInstanceProperties(currentResourceInstance.getUniqueId()));
45         dataHolder.setOrigComponentInputs(containerComponent.getInputs());
46     }
47
48     @Override
49     public Either<Component, ResponseFormat> mergeDataAfterCreate(User user, DataForMergeHolder dataHolder, Component updatedContainerComponent, String newInstanceId) {
50         Either<List<ComponentInstanceInput>, ActionStatus> instanceInputsEither = mergeComponentInstanceInputsIntoContainer(dataHolder, updatedContainerComponent, newInstanceId);
51         if (instanceInputsEither.isRight()) {
52             ActionStatus actionStatus = instanceInputsEither.right().value();
53             return Either.right(componentsUtils.getResponseFormat(actionStatus));
54         }
55         Either<List<ComponentInstanceProperty>, ActionStatus> instancePropsEither = mergeComponentInstancePropsIntoContainer(dataHolder, updatedContainerComponent, newInstanceId);
56         if (instancePropsEither.isRight()) {
57             ActionStatus actionStatus = instancePropsEither.right().value();
58             return Either.right(componentsUtils.getResponseFormat(actionStatus));
59         }
60         Either<List<InputDefinition>, ActionStatus> inputsEither = mergeComponentInputsIntoContainer(dataHolder, updatedContainerComponent.getUniqueId(), newInstanceId);
61         if (inputsEither.isRight()) {
62             ActionStatus actionStatus = inputsEither.right().value();
63             return Either.right(componentsUtils.getResponseFormat(actionStatus));
64         }
65         return Either.left(updatedContainerComponent);
66     }
67
68     private Either<List<ComponentInstanceProperty>, ActionStatus> mergeComponentInstancePropsIntoContainer(DataForMergeHolder dataHolder, Component updatedComponent, String instanceId) {
69         List<ComponentInstanceProperty> originComponentInstanceProps = dataHolder.getOrigComponentInstanceProperties();
70         List<InputDefinition> originComponentsInputs = dataHolder.getOrigComponentInputs();
71         List<ComponentInstanceProperty> newComponentInstancesProps = updatedComponent.safeGetComponentInstanceProperties(instanceId);
72         ActionStatus actionStatus = componentInstancePropertiesMergeBL.mergeComponentInstanceProperties(originComponentInstanceProps, originComponentsInputs, updatedComponent, instanceId);
73
74         if (actionStatus != ActionStatus.OK) {
75             LOGGER.error("Failed to update component {} with merged instance properties", updatedComponent.getUniqueId(), newComponentInstancesProps);
76             return Either.right(actionStatus);
77         }
78         return Either.left(newComponentInstancesProps);
79     }
80
81     private Either<List<ComponentInstanceInput>, ActionStatus> mergeComponentInstanceInputsIntoContainer(DataForMergeHolder dataHolder, Component updatedComponent, String instanceId) {
82         List<ComponentInstanceInput> originComponentInstanceInputs = dataHolder.getOrigComponentInstanceInputs();
83         List<InputDefinition> originComponentsInputs = dataHolder.getOrigComponentInputs();
84         List<ComponentInstanceInput> newComponentInstancesInputs = updatedComponent.safeGetComponentInstanceInput(instanceId);
85         ActionStatus actionStatus = resourceInstanceInputsMergeBL.mergeComponentInstanceInputs(originComponentInstanceInputs, originComponentsInputs, updatedComponent, instanceId);
86         if (actionStatus != ActionStatus.OK) {
87             LOGGER.error("Failed to update component {} with merged instance properties", updatedComponent.getUniqueId(), newComponentInstancesInputs);
88             return Either.right(actionStatus);
89         }
90         return Either.left(newComponentInstancesInputs);
91     }
92
93     private Either<List<InputDefinition>, ActionStatus> mergeComponentInputsIntoContainer(DataForMergeHolder dataHolder, String newContainerComponentId, String newInstanceId) {
94         List<InputDefinition> origComponentInputs = dataHolder.getOrigComponentInputs();
95         List<InputDefinition> inputsToAddToContainer = new ArrayList<>();
96         if (origComponentInputs != null && !origComponentInputs.isEmpty()) {
97             // get  instance inputs and properties after merge
98             Either<Component, StorageOperationStatus> componentWithInstancesInputsAndProperties = getComponentWithInstancesInputsAndProperties(newContainerComponentId);
99             if (componentWithInstancesInputsAndProperties.isRight()) {
100                 LOGGER.error("Component %s was not found", newContainerComponentId);
101                 return Either.right(componentsUtils.convertFromStorageResponse(componentWithInstancesInputsAndProperties.right().value()));
102             }
103             Component updatedContainerComponent = componentWithInstancesInputsAndProperties.left().value();
104
105             ActionStatus redeclareStatus = resourceInputsMergeBL.redeclareComponentInputsForInstance(origComponentInputs, updatedContainerComponent, newInstanceId);
106             if (redeclareStatus != ActionStatus.OK) {
107                 LOGGER.error("Failed to update component {} with merged inputs {}", newContainerComponentId, inputsToAddToContainer);
108                 Either.right(redeclareStatus);
109             }
110         }
111         return Either.left(inputsToAddToContainer);
112     }
113
114     private Either<Component, StorageOperationStatus> getComponentWithInstancesInputsAndProperties(String containerComponentId) {
115         ComponentParametersView filter = new ComponentParametersView(true);
116         filter.setIgnoreComponentInstances(false);
117         filter.setIgnoreComponentInstancesInputs(false);
118         filter.setIgnoreComponentInstancesProperties(false);
119         filter.setIgnoreArtifacts(false);
120         return toscaOperationFacade.getToscaElement(containerComponentId, filter);
121     }
122 }