1 package org.openecomp.sdc.be.components.merge.instance;
3 import java.util.ArrayList;
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;
23 import fj.data.Either;
26 * Created by chaya on 9/20/2017.
28 @org.springframework.stereotype.Component("ComponentInstancePropsAndInputsMerge")
29 public class ComponentInstancePropsAndInputsMerge implements ComponentInstanceMergeInterface {
31 private static final Logger LOGGER = LoggerFactory.getLogger(ComponentInstancePropsAndInputsMerge.class);
34 private ToscaOperationFacade toscaOperationFacade;
37 private ComponentsUtils componentsUtils;
40 private ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL;
43 private ComponentInstanceInputsMergeBL resourceInstanceInputsMergeBL;
46 private ComponentInputsMergeBL resourceInputsMergeBL;
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());
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));
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));
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));
72 return Either.left(updatedContainerComponent);
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);
81 if (actionStatus != ActionStatus.OK) {
82 LOGGER.error("Failed to update component {} with merged instance properties", updatedComponent.getUniqueId(), newComponentInstancesProps);
83 return Either.right(actionStatus);
85 return Either.left(newComponentInstancesProps);
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);
97 return Either.left(newComponentInstancesInputs);
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()));
110 Component updatedContainerComponent = componentWithInstancesInputsAndProperties.left().value();
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);
118 return Either.left(inputsToAddToContainer);
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);