Catalog alignment
[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 import java.util.stream.Collectors;
34
35 import static java.util.Collections.emptyList;
36 import static org.apache.commons.collections.CollectionUtils.isEmpty;
37
38 public abstract class InputsMergeCommand {
39
40     private static final Logger log = Logger.getLogger(InputsMergeCommand.class);
41
42     private InputsValuesMergingBusinessLogic inputsValuesMergingBusinessLogic;
43     private DeclaredInputsResolver declaredInputsResolver;
44     private ToscaOperationFacade toscaOperationFacade;
45     private ComponentsUtils componentsUtils;
46
47     public InputsMergeCommand(InputsValuesMergingBusinessLogic inputsValuesMergingBusinessLogic, DeclaredInputsResolver declaredInputsResolver, ToscaOperationFacade toscaOperationFacade, ComponentsUtils componentsUtils) {
48         this.inputsValuesMergingBusinessLogic = inputsValuesMergingBusinessLogic;
49         this.declaredInputsResolver = declaredInputsResolver;
50         this.toscaOperationFacade = toscaOperationFacade;
51         this.componentsUtils = componentsUtils;
52     }
53
54     abstract List<InputDefinition> getInputsToMerge(Component component);
55
56     abstract Map<String, List<PropertyDataDefinition>> getProperties(Component component);
57
58     ActionStatus redeclareAndMergeInputsValues(Component prevComponent, Component currComponent) {
59         if (prevComponent == null || isEmpty(prevComponent.getInputs())) {
60             return ActionStatus.OK;
61         }
62         List<InputDefinition> mergedInputs = mergeInputsValues(prevComponent, currComponent);
63         List<InputDefinition> previouslyDeclaredInputsToMerge = getUniquePreviouslyDeclaredInputsToMerge(prevComponent, currComponent, mergedInputs);
64         mergedInputs.addAll(previouslyDeclaredInputsToMerge);
65         return updateInputs(currComponent.getUniqueId(), mergedInputs);
66     }
67
68
69
70     private List<InputDefinition> mergeInputsValues(Component prevComponent, Component currComponent) {
71         log.debug("#mergeInputsValues - merge inputs values from previous component {} to current component {}", prevComponent.getUniqueId(), currComponent.getUniqueId());
72         List<InputDefinition> inputsToMerge = getInputsToMerge(currComponent);
73         List<InputDefinition> prevInputs = prevComponent.safeGetInputs();
74         inputsValuesMergingBusinessLogic.mergeComponentInputs(prevInputs, inputsToMerge);
75         return inputsToMerge;
76     }
77
78     private List<InputDefinition> getUniquePreviouslyDeclaredInputsToMerge(Component prevComponent, Component currComponent, List<InputDefinition> mergedInputs) {
79         List<InputDefinition> previouslyDeclaredInputsToMerge = getPreviouslyDeclaredInputsToMerge(prevComponent, currComponent);
80         return previouslyDeclaredInputsToMerge.stream()
81                 .filter(prev -> mergedInputs.stream()
82                         .noneMatch(merged -> merged.getName().equals(prev.getName()))).collect(Collectors.toList());
83     }
84
85
86     private List<InputDefinition> getPreviouslyDeclaredInputsToMerge(Component prevComponent, Component currComponent) {
87         log.debug("#getPreviouslyDeclaredInputsToMerge - getting inputs that were previously declared from previous component {} and setting on current component {}", prevComponent.getUniqueId(), currComponent.getUniqueId());
88         if (isEmpty(prevComponent.getInputs())) {
89             return emptyList();
90         }
91         Map<String, List<PropertyDataDefinition>> props = getProperties(currComponent);
92         return declaredInputsResolver.getPreviouslyDeclaredInputsToMerge(prevComponent, currComponent, props);
93     }
94
95     private ActionStatus updateInputs(String containerId, List<InputDefinition> inputsToUpdate) {
96         log.debug("#updateInputs - updating inputs for container {}", containerId);
97         return toscaOperationFacade.updateInputsToComponent(inputsToUpdate, containerId)
98                 .either(updatedInputs -> ActionStatus.OK,
99                         componentsUtils::convertFromStorageResponse);
100     }
101
102 }