Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstancePropsAndInputsMerge.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.merge.instance;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.dao.api.ActionStatus;
25 import org.openecomp.sdc.be.impl.ComponentsUtils;
26 import org.openecomp.sdc.be.model.*;
27 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
28 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
30 import org.openecomp.sdc.exception.ResponseFormat;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
36
37 /**
38  * Created by chaya on 9/20/2017.
39  */
40 @org.springframework.stereotype.Component("ComponentInstancePropsAndInputsMerge")
41 public class ComponentInstancePropsAndInputsMerge implements ComponentInstanceMergeInterface {
42
43     private static final Logger log = Logger.getLogger(ComponentInstancePropsAndInputsMerge.class);
44
45     private final ToscaOperationFacade toscaOperationFacade;
46     private final ComponentsUtils componentsUtils;
47     private final ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL;
48     private final ComponentInstanceInputsMergeBL resourceInstanceInputsMergeBL;
49     private final ComponentInstanceInputsRedeclareHandler instanceInputsRedeclareHandler;
50
51     public ComponentInstancePropsAndInputsMerge(ToscaOperationFacade toscaOperationFacade, ComponentsUtils componentsUtils, ComponentInstancePropertiesMergeBL componentInstancePropertiesMergeBL, ComponentInstanceInputsMergeBL resourceInstanceInputsMergeBL, ComponentInstanceInputsRedeclareHandler instanceInputsRedeclareHandler) {
52         this.toscaOperationFacade = toscaOperationFacade;
53         this.componentsUtils = componentsUtils;
54         this.componentInstancePropertiesMergeBL = componentInstancePropertiesMergeBL;
55         this.resourceInstanceInputsMergeBL = resourceInstanceInputsMergeBL;
56         this.instanceInputsRedeclareHandler = instanceInputsRedeclareHandler;
57     }
58
59     @Override
60     public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent, ComponentInstance currentResourceInstance, Component originComponent) {
61         dataHolder.setOrigComponentInstanceInputs(containerComponent.safeGetComponentInstanceInputsByName(currentResourceInstance.getName()));
62         dataHolder.setOrigComponentInstanceProperties(containerComponent.safeGetComponentInstanceProperties(currentResourceInstance.getUniqueId()));
63         dataHolder.setOrigComponentInputs(containerComponent.getInputs());
64     }
65
66     @Override
67     public Either<Component, ResponseFormat> mergeDataAfterCreate(User user, DataForMergeHolder dataHolder, Component updatedContainerComponent, String newInstanceId) {
68         Either<List<ComponentInstanceInput>, ActionStatus> instanceInputsEither = mergeComponentInstanceInputsIntoContainer(dataHolder, updatedContainerComponent, newInstanceId);
69         if (instanceInputsEither.isRight()) {
70             ActionStatus actionStatus = instanceInputsEither.right().value();
71             return Either.right(componentsUtils.getResponseFormat(actionStatus));
72         }
73         Either<List<ComponentInstanceProperty>, ActionStatus> instancePropsEither = mergeComponentInstancePropsIntoContainer(dataHolder, updatedContainerComponent, newInstanceId);
74         if (instancePropsEither.isRight()) {
75             ActionStatus actionStatus = instancePropsEither.right().value();
76             return Either.right(componentsUtils.getResponseFormat(actionStatus));
77         }
78         Either<List<InputDefinition>, ActionStatus> inputsEither = mergeComponentInputsIntoContainer(dataHolder, updatedContainerComponent.getUniqueId(), newInstanceId);
79         if (inputsEither.isRight()) {
80             ActionStatus actionStatus = inputsEither.right().value();
81             return Either.right(componentsUtils.getResponseFormat(actionStatus));
82         }
83         return Either.left(updatedContainerComponent);
84     }
85
86     private Either<List<ComponentInstanceProperty>, ActionStatus> mergeComponentInstancePropsIntoContainer(DataForMergeHolder dataHolder, Component updatedComponent, String instanceId) {
87         List<ComponentInstanceProperty> originComponentInstanceProps = dataHolder.getOrigComponentInstanceProperties();
88         List<InputDefinition> originComponentsInputs = dataHolder.getOrigComponentInputs();
89         List<ComponentInstanceProperty> newComponentInstancesProps = updatedComponent.safeGetComponentInstanceProperties(instanceId);
90         ActionStatus actionStatus = componentInstancePropertiesMergeBL.mergeComponentInstanceProperties(originComponentInstanceProps, originComponentsInputs, updatedComponent, instanceId);
91
92         if (actionStatus != ActionStatus.OK) {
93             log.error("Failed to update component {} with merged instance properties", updatedComponent.getUniqueId(), newComponentInstancesProps);
94             return Either.right(actionStatus);
95         }
96         return Either.left(newComponentInstancesProps);
97     }
98
99     private Either<List<ComponentInstanceInput>, ActionStatus> mergeComponentInstanceInputsIntoContainer(DataForMergeHolder dataHolder, Component updatedComponent, String instanceId) {
100         List<ComponentInstanceInput> originComponentInstanceInputs = dataHolder.getOrigComponentInstanceInputs();
101         List<InputDefinition> originComponentsInputs = dataHolder.getOrigComponentInputs();
102         List<ComponentInstanceInput> newComponentInstancesInputs = updatedComponent.safeGetComponentInstanceInput(instanceId);
103         ActionStatus actionStatus = resourceInstanceInputsMergeBL.mergeComponentInstanceInputs(originComponentInstanceInputs, originComponentsInputs, updatedComponent, instanceId);
104         if (actionStatus != ActionStatus.OK) {
105             log.error("Failed to update component {} with merged instance properties", updatedComponent.getUniqueId(), newComponentInstancesInputs);
106             return Either.right(actionStatus);
107         }
108         return Either.left(newComponentInstancesInputs);
109     }
110
111     private Either<List<InputDefinition>, ActionStatus> mergeComponentInputsIntoContainer(DataForMergeHolder dataHolder, String newContainerComponentId, String newInstanceId) {
112         List<InputDefinition> origComponentInputs = dataHolder.getOrigComponentInputs();
113         List<InputDefinition> inputsToAddToContainer = new ArrayList<>();
114         if (isNotEmpty(origComponentInputs)) {
115             // get  instance inputs and properties after merge
116             Either<Component, StorageOperationStatus> componentWithInstancesInputsAndProperties = getComponentWithInstancesInputsAndProperties(newContainerComponentId);
117             if (componentWithInstancesInputsAndProperties.isRight()) {
118                 log.error("Component %s was not found", newContainerComponentId);
119                 return Either.right(componentsUtils.convertFromStorageResponse(componentWithInstancesInputsAndProperties.right().value()));
120             }
121             Component updatedContainerComponent = componentWithInstancesInputsAndProperties.left().value();
122             Component currInstanceOriginType = dataHolder.getCurrInstanceNode();
123             ActionStatus redeclareStatus = instanceInputsRedeclareHandler.redeclareComponentInputsForInstance(updatedContainerComponent, newInstanceId, currInstanceOriginType, origComponentInputs);
124             if (redeclareStatus != ActionStatus.OK) {
125                 log.error("Failed to update component {} with merged inputs {}", newContainerComponentId, inputsToAddToContainer);
126                 return Either.right(redeclareStatus);
127             }
128         }
129         return Either.left(inputsToAddToContainer);
130     }
131
132     private Either<Component, StorageOperationStatus> getComponentWithInstancesInputsAndProperties(String containerComponentId) {
133         ComponentParametersView filter = new ComponentParametersView(true);
134         filter.setIgnoreComponentInstances(false);
135         filter.setIgnoreComponentInstancesInputs(false);
136         filter.setIgnoreComponentInstancesProperties(false);
137         filter.setIgnoreArtifacts(false);
138         return toscaOperationFacade.getToscaElement(containerComponentId, filter);
139     }
140 }