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