2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
22 package org.openecomp.sdc.be.components.merge.instance;
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.jsontitan.operations.ExternalReferencesOperation;
31 import org.openecomp.sdc.exception.ResponseFormat;
33 import java.util.List;
35 import java.util.Optional;
37 import static org.apache.commons.collections.MapUtils.isEmpty;
39 @org.springframework.stereotype.Component
40 public class ExternalRefsMergeBL implements ComponentInstanceMergeInterface {
42 private final ExternalReferencesOperation externalReferencesOperation;
44 ExternalRefsMergeBL(ExternalReferencesOperation externalReferencesOperation) {
45 this.externalReferencesOperation = externalReferencesOperation;
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);
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,
63 Map<String, List<String>> savedExternalRefs = dataHolder.getOrigCompInstExternalRefs();
64 if (!isEmpty(savedExternalRefs)) {
65 externalReferencesOperation.addAllExternalReferences(updatedContainerComponent.getUniqueId(),
66 componentInstance.get().getUniqueId(), savedExternalRefs);
68 return Either.left(updatedContainerComponent);