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=========================================================
20 package org.openecomp.sdc.be.components.merge.instance;
22 import static org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic.ANY_ORDER_COMMAND;
24 import fj.data.Either;
25 import java.util.Collections;
26 import java.util.List;
28 import java.util.stream.Collectors;
29 import org.openecomp.sdc.be.components.merge.VspComponentsMergeCommand;
30 import org.openecomp.sdc.be.components.merge.property.DataDefinitionsValuesMergingBusinessLogic;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.Component;
34 import org.openecomp.sdc.be.model.ComponentInstance;
35 import org.openecomp.sdc.be.model.ComponentInstanceInput;
36 import org.openecomp.sdc.be.model.InputDefinition;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import org.springframework.core.annotation.Order;
41 @org.springframework.stereotype.Component
42 @Order(ANY_ORDER_COMMAND)
43 public class ComponentInstanceInputsMergeBL implements VspComponentsMergeCommand {
45 private final ToscaOperationFacade toscaOperationFacade;
46 private final ComponentsUtils componentsUtils;
47 private final DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
49 public ComponentInstanceInputsMergeBL(ToscaOperationFacade toscaOperationFacade, ComponentsUtils componentsUtils,
50 DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic) {
51 this.toscaOperationFacade = toscaOperationFacade;
52 this.componentsUtils = componentsUtils;
53 this.propertyValuesMergingBusinessLogic = propertyValuesMergingBusinessLogic;
57 public ActionStatus mergeComponents(Component prevComponent, Component currentComponent) {
58 Map<String, List<ComponentInstanceInput>> componentInstancesInputs = currentComponent.getComponentInstancesInputs();
59 if (componentInstancesInputs == null) {
60 return ActionStatus.OK;
62 componentInstancesInputs
63 .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,
73 Component newComponent, String instanceId) {
74 List<ComponentInstanceInput> newInstInputs = newComponent.safeGetComponentInstanceInput(instanceId);
75 if (newInstInputs == null) {
76 return ActionStatus.OK;
78 List<ComponentInstanceInput> oldRedeclaredInputs = findComponentInputs(oldInstProps);
79 oldRedeclaredInputs.forEach(
80 oldInput -> newInstInputs.stream().filter(newInstInput -> oldInput.getName().equals(newInstInput.getName())).forEach(this::switchValues));
81 propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstProps, oldInputs, newInstInputs, newComponent.getInputs());
82 return updateComponentInstanceInputs(newComponent, instanceId, newInstInputs);
85 private void switchValues(ComponentInstanceInput input) {
86 String tempDefaultValue = input.getDefaultValue();
87 input.setDefaultValue(input.getValue());
88 input.setValue(tempDefaultValue);
91 private List<ComponentInstanceInput> findComponentInputs(List<ComponentInstanceInput> oldInstProps) {
92 return oldInstProps.stream().filter(ComponentInstanceInput::isGetInputProperty).collect(Collectors.toList());
95 private ActionStatus updateComponentInstanceInputs(Component newComponent, String instanceId, List<ComponentInstanceInput> newInstInput) {
96 StorageOperationStatus storageOperationStatus = toscaOperationFacade.updateComponentInstanceInputs(newComponent, instanceId, newInstInput);
97 if (storageOperationStatus != StorageOperationStatus.OK) {
98 return componentsUtils.convertFromStorageResponse(storageOperationStatus);
100 return ActionStatus.OK;
103 private ActionStatus updateComponentInstancesInputs(Component component, Map<String, List<ComponentInstanceInput>> componentInstancesInputs) {
104 Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> mapStorageOperationStatusEither = toscaOperationFacade
105 .updateComponentInstanceInputsToComponent(componentInstancesInputs, component.getUniqueId());
106 if (mapStorageOperationStatusEither.isRight()) {
107 return componentsUtils.convertFromStorageResponse(mapStorageOperationStatusEither.right().value());
109 return ActionStatus.OK;
112 private void mergeOldInstanceInputsValues(Component oldComponent, Component newComponent, String instanceId,
113 List<ComponentInstanceInput> instInputs) {
114 ComponentInstance currentCompInstance = newComponent.getComponentInstanceById(instanceId).get();
115 List<ComponentInstanceInput> oldInstInputs =
116 oldComponent == null ? Collections.emptyList() : oldComponent.safeGetComponentInstanceInputsByName(currentCompInstance.getName());
117 List<InputDefinition> oldInputs = oldComponent == null ? Collections.emptyList() : oldComponent.getInputs();
118 propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(oldInstInputs, oldInputs, instInputs, newComponent.getInputs());