Added oparent to sdc main
[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 fj.data.Either;
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 import org.openecomp.sdc.exception.ResponseFormat;
31
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.stream.Collectors;
36
37 import static org.apache.commons.collections.MapUtils.isNotEmpty;
38
39 @org.springframework.stereotype.Component
40 public class ComponentInstanceCapabilitiesPropertiesMerge implements ComponentInstanceMergeInterface {
41
42     private ComponentCapabilitiesPropertiesMergeBL capabilitiesPropertiesMergeBL;
43     private ComponentsUtils componentsUtils;
44
45     public ComponentInstanceCapabilitiesPropertiesMerge(ComponentCapabilitiesPropertiesMergeBL capabilitiesPropertiesMergeBL, ComponentsUtils componentsUtils) {
46         this.capabilitiesPropertiesMergeBL = capabilitiesPropertiesMergeBL;
47         this.componentsUtils = componentsUtils;
48     }
49
50     @Override
51     public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent, ComponentInstance currentResourceInstance, Component originComponent) {
52         dataHolder.setOrigInstanceCapabilities(getAllInstanceCapabilities(currentResourceInstance));
53         dataHolder.setOrigInstanceNode(originComponent);
54     }
55
56     @Override
57     public Either<Component, ResponseFormat> mergeDataAfterCreate(User user, DataForMergeHolder dataHolder, Component updatedContainerComponent, String newInstanceId) {
58         Component origInstanceNode = dataHolder.getOrigInstanceNode();
59         List<CapabilityDefinition> origInstanceCapabilities = dataHolder.getOrigInstanceCapabilities();
60         ActionStatus mergeStatus = capabilitiesPropertiesMergeBL.mergeComponentInstanceCapabilities(updatedContainerComponent, origInstanceNode, newInstanceId, origInstanceCapabilities);
61         return Either.iif(!ActionStatus.OK.equals(mergeStatus), () -> componentsUtils.getResponseFormat(mergeStatus), () -> updatedContainerComponent);
62     }
63
64     private List<CapabilityDefinition> getAllInstanceCapabilities(ComponentInstance currentResourceInstance) {
65         return isNotEmpty( currentResourceInstance.getCapabilities() )  ? currentResourceInstance.getCapabilities().values().stream().flatMap(Collection::stream).collect(Collectors.toList()) :  new ArrayList<>() ;
66     }
67 }