04af6b7fc0050a21c67f553639e4e2a776f4cf1e
[sdc.git] /
1 package org.openecomp.sdc.be.components.merge.instance;
2
3 import java.util.List;
4
5 import org.openecomp.sdc.be.dao.api.ActionStatus;
6 import org.openecomp.sdc.be.impl.ComponentsUtils;
7 import org.openecomp.sdc.be.model.Component;
8 import org.openecomp.sdc.be.model.ComponentInstance;
9 import org.openecomp.sdc.be.model.ComponentParametersView;
10 import org.openecomp.sdc.be.model.User;
11 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
12 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
13 import org.openecomp.sdc.exception.ResponseFormat;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.springframework.beans.factory.annotation.Autowired;
17
18 import fj.data.Either;
19 /**
20  * Created by chaya on 9/12/2017.
21  */
22 @org.springframework.stereotype.Component("componentInstanceMergeDataBusinessLogic")
23 public class ComponentInstanceMergeDataBusinessLogic {
24
25     private static Logger log = LoggerFactory.getLogger(ComponentInstanceMergeDataBusinessLogic.class.getName());
26
27     @Autowired
28     private List<ComponentInstanceMergeInterface> componentInstancesMergeBLs;
29
30     @Autowired
31     private ToscaOperationFacade toscaOperationFacade;
32
33     @Autowired
34     private ComponentsUtils componentsUtils;
35
36     //for testing only
37     protected void setComponentInstancesMergeBLs(List<ComponentInstanceMergeInterface> componentInstancesMergeBLs) {
38         this.componentInstancesMergeBLs = componentInstancesMergeBLs;
39     }
40
41     /**
42      * Saves all containerComponents data before deleting, in order to merge once creating a new instance
43      * @param containerComponent
44      * @param currentResourceInstance
45      */
46     public DataForMergeHolder saveAllDataBeforeDeleting(org.openecomp.sdc.be.model.Component containerComponent, ComponentInstance currentResourceInstance, Component originComponent) {
47         DataForMergeHolder dataHolder = new DataForMergeHolder();
48         for (ComponentInstanceMergeInterface compInstMergeBL : componentInstancesMergeBLs) {
49             compInstMergeBL.saveDataBeforeMerge(dataHolder, containerComponent, currentResourceInstance, originComponent);
50         }
51         return dataHolder;
52     }
53
54     /**
55      * Merges inputs and instance inputs/props of the new Container component with the old container component data (before deleting)
56      * @param containerComponent
57      * @param newContainerComponentId
58      * @param newInstanceId
59      * @return
60      */
61     public Either<Component, ResponseFormat> mergeComponentUserOrigData(User user, DataForMergeHolder dataHolder, org.openecomp.sdc.be.model.Component containerComponent, String newContainerComponentId, String newInstanceId) {
62
63         Either<Component, StorageOperationStatus> componentWithInstancesInputsAndProperties = getComponentWithInstancesInputsAndProperties(newContainerComponentId);
64         if (componentWithInstancesInputsAndProperties.isRight()) {
65             log.error("Component with id {} was not found", newContainerComponentId);
66             StorageOperationStatus storageOperationStatus = componentWithInstancesInputsAndProperties.right().value();
67             ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(storageOperationStatus, containerComponent.getComponentType());
68             return Either.right(componentsUtils.getResponseFormat(actionStatus));
69         }
70         Component updatedContainerComponent = componentWithInstancesInputsAndProperties.left().value();
71
72         for (ComponentInstanceMergeInterface compInstMergeBL: componentInstancesMergeBLs) {
73             Either<Component, ResponseFormat> compInstanceMergeEither = compInstMergeBL.mergeDataAfterCreate(user, dataHolder, updatedContainerComponent, newInstanceId);
74             if (compInstanceMergeEither.isRight()) {
75                 return Either.right(compInstanceMergeEither.right().value());
76             }
77         }
78
79         return Either.left(updatedContainerComponent);
80     }
81
82     private Either<Component, StorageOperationStatus> getComponentWithInstancesInputsAndProperties(String containerComponentId) {
83         ComponentParametersView filter = new ComponentParametersView(true);
84         filter.setIgnoreComponentInstances(false);
85         filter.setIgnoreComponentInstancesInputs(false);
86         filter.setIgnoreComponentInstancesProperties(false);
87         filter.setIgnoreArtifacts(false);
88         return toscaOperationFacade.getToscaElement(containerComponentId, filter);
89     }
90 }