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