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