1 package org.openecomp.sdc.be.components.merge.instance;
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;
15 import java.util.ArrayList;
16 import java.util.List;
19 * Created by chaya on 9/20/2017.
21 @org.springframework.stereotype.Component("ComponentInstancePropsAndInputsMerge")
22 public class ComponentInstancePropsAndInputsMerge implements ComponentInstanceMergeInterface {
24 private static final Logger LOGGER = LoggerFactory.getLogger(ComponentInstancePropsAndInputsMerge.class);
27 private ToscaOperationFacade toscaOperationFacade;
30 private ComponentsUtils componentsUtils;
33 private ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL;
36 private ComponentInstanceInputsMergeBL resourceInstanceInputsMergeBL;
39 private ComponentInputsMergeBL resourceInputsMergeBL;
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());
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));
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));
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));
65 return Either.left(updatedContainerComponent);
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);
74 if (actionStatus != ActionStatus.OK) {
75 LOGGER.error("Failed to update component {} with merged instance properties", updatedComponent.getUniqueId(), newComponentInstancesProps);
76 return Either.right(actionStatus);
78 return Either.left(newComponentInstancesProps);
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);
90 return Either.left(newComponentInstancesInputs);
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()));
103 Component updatedContainerComponent = componentWithInstancesInputsAndProperties.left().value();
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);
111 return Either.left(inputsToAddToContainer);
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);