Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstanceRelationMergeCommand.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 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.apache.commons.collections.CollectionUtils;
25 import org.openecomp.sdc.be.components.merge.VspComponentsMergeCommand;
26 import org.openecomp.sdc.be.components.merge.utils.MergeInstanceUtils;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.impl.ComponentsUtils;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
32 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
33 import org.springframework.core.annotation.Order;
34
35 import java.util.List;
36
37 import static org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic.LAST_COMMAND;
38
39 @org.springframework.stereotype.Component
40 @Order(LAST_COMMAND)//must run after all merge commands
41 public class ComponentInstanceRelationMergeCommand implements VspComponentsMergeCommand {
42
43     private final ToscaOperationFacade toscaOperationFacade;
44     private final MergeInstanceUtils mergeInstanceUtils;
45     private final ComponentsUtils componentsUtils;
46
47     public ComponentInstanceRelationMergeCommand(ToscaOperationFacade toscaOperationFacade, MergeInstanceUtils mergeInstanceUtils, ComponentsUtils componentsUtils) {
48         this.toscaOperationFacade = toscaOperationFacade;
49         this.mergeInstanceUtils = mergeInstanceUtils;
50         this.componentsUtils = componentsUtils;
51     }
52     @Override
53     public ActionStatus mergeComponents(Component prevComponent, Component currentComponent) {
54         List<RequirementCapabilityRelDef> updatedUiRelations = mergeInstanceUtils.getUpdatedUiRelations(prevComponent, currentComponent);
55         if(CollectionUtils.isNotEmpty(updatedUiRelations)){
56             return associateResourceInstances(currentComponent, updatedUiRelations);
57         }
58         return ActionStatus.OK;
59     }
60
61     private ActionStatus associateResourceInstances(Component currentComponent, List<RequirementCapabilityRelDef> updatedUiRelations) {
62         Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> listStorageOperationStatusEither = toscaOperationFacade.associateResourceInstances(null, currentComponent.getUniqueId(), updatedUiRelations);
63         if (listStorageOperationStatusEither.isLeft()) {
64             currentComponent.getComponentInstancesRelations().addAll(updatedUiRelations);
65         } else {
66             return componentsUtils.convertFromStorageResponse(listStorageOperationStatusEither.right().value());
67         }
68         return ActionStatus.OK;
69     }
70
71     @Override
72     public String description() {
73         return "merge component instances from old component to new component";
74     }
75 }