765ddb57f6bd6b89e468f566121db874ffc66cc4
[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.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;
24
25 import fj.data.Either;
26
27 /**
28  * Created by chaya on 9/20/2017.
29  */
30 @org.springframework.stereotype.Component("ComponentInstancePropsAndInputsMerge")
31 public class ComponentInstancePropsAndInputsMerge implements ComponentInstanceMergeInterface {
32
33     private static Logger log = LoggerFactory.getLogger(ComponentInstancePropsAndInputsMerge.class.getName());
34
35     @Autowired
36     private ToscaOperationFacade toscaOperationFacade;
37
38     @Autowired
39     private ComponentsUtils componentsUtils;
40
41     @Autowired
42     private ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL;
43
44     @Autowired
45     private ComponentInstanceInputsMergeBL resourceInstanceInputsMergeBL;
46
47     @Autowired
48     private ComponentInputsMergeBL resourceInputsMergeBL;
49
50     @Override
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());
55     }
56
57     @Override
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));
63         }
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));
68         }
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));
73         }
74         return Either.left(updatedContainerComponent);
75     }
76
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);
82
83         if (actionStatus != ActionStatus.OK) {
84             log.error("Failed to update component {} with merged instance properties", updatedComponent.getUniqueId(), newComponentInstancesProps);
85             return Either.right(actionStatus);
86         }
87         return Either.left(newComponentInstancesProps);
88     }
89
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);
98         }
99         return Either.left(newComponentInstancesInputs);
100     }
101
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()));
111             }
112             Component updatedContainerComponent = componentWithInstancesInputsAndProperties.left().value();
113
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);
118             }
119         }
120         return Either.left(inputsToAddToContainer);
121     }
122
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);
130     }
131 }