Implement Attributes/Outputs BE (part 1)
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstanceMergeDataBusinessLogic.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.components.impl.exceptions.ByActionStatusComponentException;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.impl.ComponentsUtils;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.ComponentInstance;
29 import org.openecomp.sdc.be.model.ComponentParametersView;
30 import org.openecomp.sdc.be.model.User;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
32 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.context.annotation.Lazy;
36
37 import java.util.List;
38
39 /**
40  * Created by chaya on 9/12/2017.
41  */
42 @org.springframework.stereotype.Component("componentInstanceMergeDataBusinessLogic")
43 public class ComponentInstanceMergeDataBusinessLogic {
44
45     private static final Logger log = Logger.getLogger(ComponentInstanceMergeDataBusinessLogic.class);
46
47     @Autowired
48     @Lazy
49     private List<ComponentInstanceMergeInterface> componentInstancesMergeBLs;
50
51     @Autowired
52     private ToscaOperationFacade toscaOperationFacade;
53
54     @Autowired
55     private ComponentsUtils componentsUtils;
56
57     //for testing only
58     protected void setComponentInstancesMergeBLs(List<ComponentInstanceMergeInterface> componentInstancesMergeBLs) {
59         this.componentInstancesMergeBLs = componentInstancesMergeBLs;
60     }
61
62     /**
63      * Saves all containerComponents data before deleting, in order to merge once creating a new instance
64      * @param containerComponent
65      * @param currentResourceInstance
66      */
67     public DataForMergeHolder saveAllDataBeforeDeleting(org.openecomp.sdc.be.model.Component containerComponent, ComponentInstance currentResourceInstance, Component originComponent) {
68         DataForMergeHolder dataHolder = new DataForMergeHolder();
69         for (ComponentInstanceMergeInterface compInstMergeBL : componentInstancesMergeBLs) {
70             compInstMergeBL.saveDataBeforeMerge(dataHolder, containerComponent, currentResourceInstance, originComponent);
71         }
72         return dataHolder;
73     }
74
75     /**
76      * Merges inputs and instance inputs/props of the new Container component with the old container component data (before deleting)
77      * @param containerComponent
78      * @param newContainerComponentId
79      * @param newInstanceId
80      * @return
81      */
82     public Component mergeComponentUserOrigData(User user, DataForMergeHolder dataHolder, org.openecomp.sdc.be.model.Component containerComponent, String newContainerComponentId, String newInstanceId) {
83
84         Either<Component, StorageOperationStatus> componentWithInstancesInputsAndProperties = getComponentWithInstancesMergeEntities(newContainerComponentId);
85         if (componentWithInstancesInputsAndProperties.isRight()) {
86             log.error("Component with id {} was not found", newContainerComponentId);
87             StorageOperationStatus storageOperationStatus = componentWithInstancesInputsAndProperties.right().value();
88             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(storageOperationStatus, containerComponent.getComponentType());
89             throw new ByActionStatusComponentException(actionStatus);
90         }
91         Component updatedContainerComponent = componentWithInstancesInputsAndProperties.left().value();
92         componentInstancesMergeBLs.forEach(c-> c.mergeDataAfterCreate(user, dataHolder, updatedContainerComponent, newInstanceId));
93         return updatedContainerComponent;
94     }
95
96     private Either<Component, StorageOperationStatus> getComponentWithInstancesMergeEntities(String containerComponentId) {
97         ComponentParametersView filter = new ComponentParametersView(true);
98         filter.setIgnoreComponentInstances(false);
99         filter.setIgnoreComponentInstancesInputs(false);
100         filter.setIgnoreInputs(false);//dr
101         filter.setIgnoreComponentInstancesProperties(false);
102         filter.setIgnoreCapabilities(false);
103         filter.setIgnoreCapabiltyProperties(false);
104         filter.setIgnoreArtifacts(false);
105         filter.setIgnoreServicePath(false);
106         filter.setIgnoreComponentInstancesInterfaces(false);
107         return toscaOperationFacade.getToscaElement(containerComponentId, filter);
108     }
109 }