Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstanceCapabilitiesPropertiesMerge.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 org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
24 import org.openecomp.sdc.be.dao.api.ActionStatus;
25 import org.openecomp.sdc.be.impl.ComponentsUtils;
26 import org.openecomp.sdc.be.model.CapabilityDefinition;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.ComponentInstance;
29 import org.openecomp.sdc.be.model.User;
30
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.stream.Collectors;
35
36 import static org.apache.commons.collections.MapUtils.isNotEmpty;
37
38 @org.springframework.stereotype.Component
39 public class ComponentInstanceCapabilitiesPropertiesMerge implements ComponentInstanceMergeInterface {
40
41     private ComponentCapabilitiesPropertiesMergeBL capabilitiesPropertiesMergeBL;
42     private ComponentsUtils componentsUtils;
43
44     public ComponentInstanceCapabilitiesPropertiesMerge(ComponentCapabilitiesPropertiesMergeBL capabilitiesPropertiesMergeBL, ComponentsUtils componentsUtils) {
45         this.capabilitiesPropertiesMergeBL = capabilitiesPropertiesMergeBL;
46         this.componentsUtils = componentsUtils;
47     }
48
49     @Override
50     public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent, ComponentInstance currentResourceInstance, Component originComponent) {
51         dataHolder.setOrigInstanceCapabilities(getAllInstanceCapabilities(currentResourceInstance));
52         dataHolder.setOrigInstanceNode(originComponent);
53     }
54
55     @Override
56     public Component mergeDataAfterCreate(User user, DataForMergeHolder dataHolder, Component updatedContainerComponent, String newInstanceId) {
57         Component origInstanceNode = dataHolder.getOrigInstanceNode();
58         List<CapabilityDefinition> origInstanceCapabilities = dataHolder.getOrigInstanceCapabilities();
59         ActionStatus mergeStatus = capabilitiesPropertiesMergeBL.mergeComponentInstanceCapabilities(updatedContainerComponent, origInstanceNode, newInstanceId, origInstanceCapabilities);
60         if(!ActionStatus.OK.equals(mergeStatus)){
61             throw new ByActionStatusComponentException(mergeStatus);
62         }
63         return  updatedContainerComponent;
64     }
65
66     private List<CapabilityDefinition> getAllInstanceCapabilities(ComponentInstance currentResourceInstance) {
67         return isNotEmpty( currentResourceInstance.getCapabilities() )  ? currentResourceInstance.getCapabilities().values().stream().flatMap(Collection::stream).collect(Collectors.toList()) :  new ArrayList<>() ;
68     }
69 }