Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / input / InputsMergeCommand.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.input;
22
23 import org.openecomp.sdc.be.dao.api.ActionStatus;
24 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
25 import org.openecomp.sdc.be.impl.ComponentsUtils;
26 import org.openecomp.sdc.be.model.Component;
27 import org.openecomp.sdc.be.model.InputDefinition;
28 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
30
31 import java.util.List;
32 import java.util.Map;
33
34 import static java.util.Collections.emptyList;
35 import static org.apache.commons.collections.CollectionUtils.isEmpty;
36
37 public abstract class InputsMergeCommand {
38
39     private static final Logger log = Logger.getLogger(InputsMergeCommand.class);
40
41     private InputsValuesMergingBusinessLogic inputsValuesMergingBusinessLogic;
42     private DeclaredInputsResolver declaredInputsResolver;
43     private ToscaOperationFacade toscaOperationFacade;
44     private ComponentsUtils componentsUtils;
45
46     public InputsMergeCommand(InputsValuesMergingBusinessLogic inputsValuesMergingBusinessLogic, DeclaredInputsResolver declaredInputsResolver, ToscaOperationFacade toscaOperationFacade, ComponentsUtils componentsUtils) {
47         this.inputsValuesMergingBusinessLogic = inputsValuesMergingBusinessLogic;
48         this.declaredInputsResolver = declaredInputsResolver;
49         this.toscaOperationFacade = toscaOperationFacade;
50         this.componentsUtils = componentsUtils;
51     }
52
53     abstract List<InputDefinition> getInputsToMerge(Component component);
54
55     abstract Map<String, List<PropertyDataDefinition>> getProperties(Component component);
56
57     ActionStatus redeclareAndMergeInputsValues(Component prevComponent, Component currComponent) {
58         if (prevComponent == null || isEmpty(prevComponent.getInputs())) {
59             return ActionStatus.OK;
60         }
61         List<InputDefinition> mergedInputs = mergeInputsValues(prevComponent, currComponent);
62         List<InputDefinition> previouslyDeclaredInputsToMerge = getPreviouslyDeclaredInputsToMerge(prevComponent, currComponent);
63         mergedInputs.addAll(previouslyDeclaredInputsToMerge);
64         return updateInputs(currComponent.getUniqueId(), mergedInputs);
65     }
66
67     private List<InputDefinition> mergeInputsValues(Component prevComponent, Component currComponent) {
68         log.debug("#mergeInputsValues - merge inputs values from previous component {} to current component {}", prevComponent.getUniqueId(), currComponent.getUniqueId());
69         List<InputDefinition> inputsToMerge = getInputsToMerge(currComponent);
70         List<InputDefinition> prevInputs = prevComponent.safeGetInputs();
71         inputsValuesMergingBusinessLogic.mergeComponentInputs(prevInputs, inputsToMerge);
72         return inputsToMerge;
73     }
74
75     private List<InputDefinition> getPreviouslyDeclaredInputsToMerge(Component prevComponent, Component currComponent) {
76         log.debug("#getPreviouslyDeclaredInputsToMerge - getting inputs that were previously declared from previous component {} and setting on current component {}", prevComponent.getUniqueId(), currComponent.getUniqueId());
77         if (isEmpty(prevComponent.getInputs())) {
78             return emptyList();
79         }
80         Map<String, List<PropertyDataDefinition>> props = getProperties(currComponent);
81         return declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(prevComponent, currComponent, props);
82     }
83
84     private ActionStatus updateInputs(String containerId, List<InputDefinition> inputsToUpdate) {
85         log.debug("#updateInputs - updating inputs for container {}", containerId);
86         return toscaOperationFacade.updateInputsToComponent(inputsToUpdate, containerId)
87                 .either(updatedInputs -> ActionStatus.OK,
88                         componentsUtils::convertFromStorageResponse);
89     }
90
91 }