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