Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / instance / ExternalRefsMergeBL.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22 package org.openecomp.sdc.be.components.merge.instance;
23
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.model.Component;
27 import org.openecomp.sdc.be.model.ComponentInstance;
28 import org.openecomp.sdc.be.model.User;
29 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ExternalReferencesOperation;
30
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Optional;
34
35 import static org.apache.commons.collections.MapUtils.isEmpty;
36
37 @org.springframework.stereotype.Component
38 public class ExternalRefsMergeBL implements ComponentInstanceMergeInterface {
39
40     private final ExternalReferencesOperation externalReferencesOperation;
41
42     ExternalRefsMergeBL(ExternalReferencesOperation externalReferencesOperation) {
43         this.externalReferencesOperation = externalReferencesOperation;
44     }
45
46     @Override
47     public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent,
48                                     ComponentInstance currentResourceInstance, Component originComponent) {
49         Map<String, List<String>> externalRefs = externalReferencesOperation.getAllExternalReferences(containerComponent.getUniqueId(),
50                 currentResourceInstance.getUniqueId());
51         dataHolder.setOrigComponentInstanceExternalRefs(externalRefs);
52     }
53
54     @Override
55     public Component mergeDataAfterCreate(User user, DataForMergeHolder dataHolder, Component updatedContainerComponent, String newInstanceId) {
56         Optional<ComponentInstance> componentInstance = updatedContainerComponent.getComponentInstanceById(newInstanceId);
57         if (!componentInstance.isPresent()) {
58             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND,
59                     newInstanceId);
60         }
61         Map<String, List<String>>  savedExternalRefs = dataHolder.getOrigCompInstExternalRefs();
62         if (!isEmpty(savedExternalRefs)) {
63             externalReferencesOperation.addAllExternalReferences(updatedContainerComponent.getUniqueId(),
64                     componentInstance.get().getUniqueId(), savedExternalRefs);
65         }
66         return updatedContainerComponent;
67     }
68 }