Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstanceInputsMergeBL.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.components.merge.VspComponentsMergeCommand;
25 import org.openecomp.sdc.be.components.merge.property.DataDefinitionsValuesMergingBusinessLogic;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.impl.ComponentsUtils;
28 import org.openecomp.sdc.be.model.Component;
29 import org.openecomp.sdc.be.model.ComponentInstance;
30 import org.openecomp.sdc.be.model.ComponentInstanceInput;
31 import org.openecomp.sdc.be.model.InputDefinition;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
33 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
34 import org.springframework.core.annotation.Order;
35
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.stream.Collectors;
40
41 import static org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic.ANY_ORDER_COMMAND;
42
43 @org.springframework.stereotype.Component
44 @Order(ANY_ORDER_COMMAND)
45 public class ComponentInstanceInputsMergeBL implements VspComponentsMergeCommand {
46
47     private final ToscaOperationFacade toscaOperationFacade;
48     private final ComponentsUtils componentsUtils;
49     private final DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
50
51     public ComponentInstanceInputsMergeBL(ToscaOperationFacade toscaOperationFacade, ComponentsUtils componentsUtils, DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic) {
52         this.toscaOperationFacade = toscaOperationFacade;
53         this.componentsUtils = componentsUtils;
54         this.propertyValuesMergingBusinessLogic = propertyValuesMergingBusinessLogic;
55     }
56
57     @Override
58     public ActionStatus mergeComponents(Component prevComponent, Component currentComponent) {
59         Map<String, List<ComponentInstanceInput>> componentInstancesInputs = currentComponent.getComponentInstancesInputs();
60         if (componentInstancesInputs == null) {
61             return ActionStatus.OK;
62         }
63         componentInstancesInputs.forEach((instanceId, instInputs) -> mergeOldInstanceInputsValues(prevComponent, currentComponent, instanceId, instInputs));
64         return updateComponentInstancesInputs(currentComponent, componentInstancesInputs);
65     }
66
67     @Override
68     public String description() {
69         return "merge component instance inputs";
70     }
71
72     public ActionStatus mergeComponentInstanceInputs(List<ComponentInstanceInput> oldInstProps, List<InputDefinition> oldInputs, Component newComponent, String instanceId) {
73         List<ComponentInstanceInput> newInstInputs = newComponent.safeGetComponentInstanceInput(instanceId);
74         if (newInstInputs == null) {
75             return ActionStatus.OK;
76         }
77         
78         List<ComponentInstanceInput> oldRedeclaredInputs = findComponentInputs(oldInstProps);
79         oldRedeclaredInputs.forEach(oldInput -> newInstInputs.stream()
80                                                               .filter(newInstInput -> oldInput.getName().equals(newInstInput.getName()))
81                                                               .forEach(this::switchValues));
82         
83         propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstProps, oldInputs, newInstInputs, newComponent.getInputs());
84         return updateComponentInstanceInputs(newComponent, instanceId, newInstInputs);
85     }
86     
87     private void switchValues(ComponentInstanceInput input) {
88         String tempDefaultValue = input.getDefaultValue();
89         input.setDefaultValue(input.getValue());
90         input.setValue(tempDefaultValue);
91     }
92     
93     private List<ComponentInstanceInput> findComponentInputs(List<ComponentInstanceInput> oldInstProps) {
94         return oldInstProps.stream()
95                            .filter(ComponentInstanceInput::isGetInputProperty)
96                            .collect(Collectors.toList());
97     }
98
99     private ActionStatus updateComponentInstanceInputs(Component newComponent, String instanceId, List<ComponentInstanceInput> newInstInput) {
100         StorageOperationStatus storageOperationStatus = toscaOperationFacade.updateComponentInstanceInputs(newComponent, instanceId, newInstInput);
101         if (storageOperationStatus != StorageOperationStatus.OK) {
102             return componentsUtils.convertFromStorageResponse(storageOperationStatus);
103         }
104         return ActionStatus.OK;
105     }
106
107     private ActionStatus updateComponentInstancesInputs(Component component, Map<String, List<ComponentInstanceInput>> componentInstancesInputs) {
108         Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> mapStorageOperationStatusEither = toscaOperationFacade.updateComponentInstanceInputsToComponent(componentInstancesInputs, component.getUniqueId());
109         if (mapStorageOperationStatusEither.isRight()) {
110             return componentsUtils.convertFromStorageResponse(mapStorageOperationStatusEither.right().value());
111         }
112         return ActionStatus.OK;
113     }
114
115     private void mergeOldInstanceInputsValues(Component oldComponent, Component newComponent, String instanceId, List<ComponentInstanceInput> instInputs) {
116         ComponentInstance currentCompInstance = newComponent.getComponentInstanceById(instanceId).get();
117         List<ComponentInstanceInput> oldInstInputs = oldComponent == null ? Collections.emptyList() : oldComponent.safeGetComponentInstanceInputsByName(currentCompInstance.getName());
118         List<InputDefinition> oldInputs = oldComponent == null ? Collections.emptyList() : oldComponent.getInputs();
119         propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstInputs, oldInputs, instInputs, newComponent.getInputs());
120     }
121
122 }