Upgrade SDC from Titan to Janus Graph
[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 fj.data.Either;
25 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.ComponentInstance;
29 import org.openecomp.sdc.be.model.User;
30 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ExternalReferencesOperation;
31 import org.openecomp.sdc.exception.ResponseFormat;
32
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Optional;
36
37 import static org.apache.commons.collections.MapUtils.isEmpty;
38
39 @org.springframework.stereotype.Component
40 public class ExternalRefsMergeBL implements ComponentInstanceMergeInterface {
41
42     private final ExternalReferencesOperation externalReferencesOperation;
43
44     ExternalRefsMergeBL(ExternalReferencesOperation externalReferencesOperation) {
45         this.externalReferencesOperation = externalReferencesOperation;
46     }
47
48     @Override
49     public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent,
50                                     ComponentInstance currentResourceInstance, Component originComponent) {
51         Map<String, List<String>> externalRefs = externalReferencesOperation.getAllExternalReferences(containerComponent.getUniqueId(),
52                 currentResourceInstance.getUniqueId());
53         dataHolder.setOrigComponentInstanceExternalRefs(externalRefs);
54     }
55
56     @Override
57     public Either<Component, ResponseFormat> mergeDataAfterCreate(User user, DataForMergeHolder dataHolder, Component updatedContainerComponent, String newInstanceId) {
58         Optional<ComponentInstance> componentInstance = updatedContainerComponent.getComponentInstanceById(newInstanceId);
59         if (!componentInstance.isPresent()) {
60             throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND,
61                     newInstanceId);
62         }
63         Map<String, List<String>>  savedExternalRefs = dataHolder.getOrigCompInstExternalRefs();
64         if (!isEmpty(savedExternalRefs)) {
65             externalReferencesOperation.addAllExternalReferences(updatedContainerComponent.getUniqueId(),
66                     componentInstance.get().getUniqueId(), savedExternalRefs);
67         }
68         return Either.left(updatedContainerComponent);
69     }
70 }