2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.merge.instance;
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;
36 import java.util.Collections;
37 import java.util.List;
39 import java.util.stream.Collectors;
41 import static org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic.ANY_ORDER_COMMAND;
43 @org.springframework.stereotype.Component
44 @Order(ANY_ORDER_COMMAND)
45 public class ComponentInstanceInputsMergeBL implements VspComponentsMergeCommand {
47 private final ToscaOperationFacade toscaOperationFacade;
48 private final ComponentsUtils componentsUtils;
49 private final DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
51 public ComponentInstanceInputsMergeBL(ToscaOperationFacade toscaOperationFacade, ComponentsUtils componentsUtils, DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic) {
52 this.toscaOperationFacade = toscaOperationFacade;
53 this.componentsUtils = componentsUtils;
54 this.propertyValuesMergingBusinessLogic = propertyValuesMergingBusinessLogic;
58 public ActionStatus mergeComponents(Component prevComponent, Component currentComponent) {
59 Map<String, List<ComponentInstanceInput>> componentInstancesInputs = currentComponent.getComponentInstancesInputs();
60 if (componentInstancesInputs == null) {
61 return ActionStatus.OK;
63 componentInstancesInputs.forEach((instanceId, instInputs) -> mergeOldInstanceInputsValues(prevComponent, currentComponent, instanceId, instInputs));
64 return updateComponentInstancesInputs(currentComponent, componentInstancesInputs);
68 public String description() {
69 return "merge component instance inputs";
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;
78 List<ComponentInstanceInput> oldRedeclaredInputs = findComponentInputs(oldInstProps);
79 oldRedeclaredInputs.forEach(oldInput -> newInstInputs.stream()
80 .filter(newInstInput -> oldInput.getName().equals(newInstInput.getName()))
81 .forEach(this::switchValues));
83 propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstProps, oldInputs, newInstInputs, newComponent.getInputs());
84 return updateComponentInstanceInputs(newComponent, instanceId, newInstInputs);
87 private void switchValues(ComponentInstanceInput input) {
88 String tempDefaultValue = input.getDefaultValue();
89 input.setDefaultValue(input.getValue());
90 input.setValue(tempDefaultValue);
93 private List<ComponentInstanceInput> findComponentInputs(List<ComponentInstanceInput> oldInstProps) {
94 return oldInstProps.stream()
95 .filter(ComponentInstanceInput::isGetInputProperty)
96 .collect(Collectors.toList());
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);
104 return ActionStatus.OK;
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());
112 return ActionStatus.OK;
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());