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