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 java.util.List;
23 import org.apache.commons.collections.CollectionUtils;
24 import org.apache.commons.collections.MapUtils;
25 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
28 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
29 import org.openecomp.sdc.be.impl.ComponentsUtils;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.ComponentInstance;
32 import org.openecomp.sdc.be.model.ComponentInstanceInterface;
33 import org.openecomp.sdc.be.model.User;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.springframework.beans.factory.annotation.Autowired;
38 @org.springframework.stereotype.Component("ComponentInstanceInterfacesMerge")
39 public class ComponentInstanceInterfacesMerge implements ComponentInstanceMergeInterface {
42 private ComponentsUtils componentsUtils;
44 private ToscaOperationFacade toscaOperationFacade;
47 public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent, ComponentInstance currentResourceInstance,
48 Component originComponent) {
49 dataHolder.setOrigInstanceNode(originComponent);
50 dataHolder.setOrigComponentInstanceInterfaces(containerComponent.safeGetComponentInstanceInterfaces(currentResourceInstance.getUniqueId()));
54 public Component mergeDataAfterCreate(User user, DataForMergeHolder dataHolder, Component updatedContainerComponent, String newInstanceId) {
55 List<ComponentInstanceInterface> origInstanceInterfaces = dataHolder.getOrigComponentInstanceInterfaces();
56 ActionStatus mergeStatus = mergeComponentInstanceInterfaces(updatedContainerComponent, newInstanceId, origInstanceInterfaces);
57 if (!ActionStatus.OK.equals(mergeStatus)) {
58 throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(mergeStatus));
60 return updatedContainerComponent;
64 private ActionStatus mergeComponentInstanceInterfaces(Component currentComponent, String instanceId,
65 List<ComponentInstanceInterface> prevInstanceInterfaces) {
66 if (CollectionUtils.isEmpty(prevInstanceInterfaces) || MapUtils.isEmpty(currentComponent.getComponentInstancesInterfaces())) {
67 return ActionStatus.OK;
69 if (CollectionUtils.isEmpty(currentComponent.getComponentInstancesInterfaces().get(instanceId))) {
70 return ActionStatus.OK;
72 currentComponent.getComponentInstancesInterfaces().get(instanceId).stream().forEach(
73 newInterfaceDef -> newInterfaceDef.getOperationsMap().values().forEach(
74 newOperationDef -> prevInstanceInterfaces.stream().filter(in -> in.getUniqueId().equals(newInterfaceDef.getUniqueId())).forEach(
75 prevInterfaceDef -> prevInterfaceDef.getOperationsMap().values().stream()
76 .filter(in1 -> in1.getUniqueId().equals(newOperationDef.getUniqueId()))
77 .forEach(oldOperationDef -> {
78 if(oldOperationDef.getInputs() != null) {
79 if(newOperationDef.getInputs() == null) {
80 newOperationDef.setInputs(new ListDataDefinition<>());
82 mergeOperationInputDefinitions(oldOperationDef.getInputs(), newOperationDef.getInputs());
85 StorageOperationStatus updateStatus = toscaOperationFacade.updateComponentInstanceInterfaces(currentComponent, instanceId);
86 return componentsUtils.convertFromStorageResponse(updateStatus);
89 private void mergeOperationInputDefinitions(ListDataDefinition<OperationInputDefinition> origInputs,
90 ListDataDefinition<OperationInputDefinition> newInputs) {
91 newInputs.getListToscaDataDefinition().
92 forEach(inp -> origInputs.getListToscaDataDefinition().stream().filter(in -> in.getInputId().equals(inp.getInputId())).
94 inp.setSourceProperty(in.getSourceProperty());
95 inp.setSource(in.getSource());
96 inp.setValue(in.getValue());
98 origInputs.getListToscaDataDefinition().stream().
99 filter(inp -> newInputs.getListToscaDataDefinition().stream().noneMatch(in -> in.getInputId().equals(inp.getInputId()))).
100 forEach(inp -> newInputs.getListToscaDataDefinition().add(inp));