43da6181cd4615b7bdc1a574c629936ffe59061d
[sdc.git] /
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 package org.openecomp.sdc.be.components.merge.instance;
21
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;
37
38 @org.springframework.stereotype.Component("ComponentInstanceInterfacesMerge")
39 public class ComponentInstanceInterfacesMerge implements ComponentInstanceMergeInterface {
40
41     @Autowired
42     private ComponentsUtils componentsUtils;
43     @Autowired
44     private ToscaOperationFacade toscaOperationFacade;
45
46     @Override
47     public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent, ComponentInstance currentResourceInstance,
48                                     Component originComponent) {
49         dataHolder.setOrigInstanceNode(originComponent);
50         dataHolder.setOrigComponentInstanceInterfaces(containerComponent.safeGetComponentInstanceInterfaces(currentResourceInstance.getUniqueId()));
51     }
52
53     @Override
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));
59         } else {
60             return updatedContainerComponent;
61         }
62     }
63
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;
68         }
69         if (CollectionUtils.isEmpty(currentComponent.getComponentInstancesInterfaces().get(instanceId))) {
70             return ActionStatus.OK;
71         }
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<>());
81                                 }
82                                 mergeOperationInputDefinitions(oldOperationDef.getInputs(), newOperationDef.getInputs());
83                             }
84                         }))));
85         StorageOperationStatus updateStatus = toscaOperationFacade.updateComponentInstanceInterfaces(currentComponent, instanceId);
86         return componentsUtils.convertFromStorageResponse(updateStatus);
87     }
88
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())).
93                     forEach(in -> {
94                         inp.setSourceProperty(in.getSourceProperty());
95                         inp.setSource(in.getSource());
96                         inp.setValue(in.getValue());
97                     }));
98             origInputs.getListToscaDataDefinition().stream().
99                     filter(inp -> newInputs.getListToscaDataDefinition().stream().noneMatch(in -> in.getInputId().equals(inp.getInputId()))).
100                     forEach(inp -> newInputs.getListToscaDataDefinition().add(inp));
101     }
102 }