2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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=========================================================
20 package org.openecomp.sdc.be.components.merge.instance;
22 import fj.data.Either;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.List;
29 import java.util.stream.Collectors;
30 import org.javatuples.Pair;
31 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
32 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
33 import org.openecomp.sdc.be.dao.api.ActionStatus;
34 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
35 import org.openecomp.sdc.be.impl.ComponentsUtils;
36 import org.openecomp.sdc.be.impl.ForwardingPathUtils;
37 import org.openecomp.sdc.be.model.CapabilityDefinition;
38 import org.openecomp.sdc.be.model.Component;
39 import org.openecomp.sdc.be.model.ComponentInstance;
40 import org.openecomp.sdc.be.model.Service;
41 import org.openecomp.sdc.be.model.User;
42 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
43 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
44 import org.openecomp.sdc.common.log.wrappers.Logger;
45 import org.springframework.beans.factory.annotation.Autowired;
47 @org.springframework.stereotype.Component
48 public class ComponentInstanceForwardingPathMerge implements ComponentInstanceMergeInterface {
50 private static Logger log = Logger.getLogger(ComponentInstanceForwardingPathMerge.class);
52 private ServiceBusinessLogic serviceBusinessLogic;
54 private ToscaOperationFacade toscaOperationFacade;
56 private ComponentsUtils componentsUtils;
59 public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent, ComponentInstance currentResourceInstance,
60 Component originComponent) {
61 dataHolder.setOrigInstanceCapabilities(getAllInstanceCapabilities(currentResourceInstance));
62 dataHolder.setOrigInstanceNode(originComponent);
63 dataHolder.setOrigComponentInstId(currentResourceInstance.getName());
67 public Component mergeDataAfterCreate(User user, DataForMergeHolder dataHolder, Component updatedContainerComponent, String newInstanceId) {
68 if (!(updatedContainerComponent instanceof Service)) {
69 // no need to handle forwarding paths
70 return updatedContainerComponent;
72 Service service = (Service) updatedContainerComponent;
73 ComponentInstance ci = service.getComponentInstanceById(newInstanceId).orElse(null);
75 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, newInstanceId);
77 Either<Component, StorageOperationStatus> resourceEither = toscaOperationFacade.getToscaFullElement(ci.getComponentUid());
78 if (resourceEither.isRight()) {
79 log.debug("Failed to fetch resource with id {} for instance {}", ci.getComponentUid(), ci.getUniqueId());
80 throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(resourceEither.right().value()));
82 Component fetchedComponent = resourceEither.left().value();
83 Pair<Map<String, ForwardingPathDataDefinition>, Map<String, ForwardingPathDataDefinition>> pair = new ForwardingPathUtils()
84 .updateForwardingPathOnVersionChange(service, dataHolder, fetchedComponent, newInstanceId);
85 Map<String, ForwardingPathDataDefinition> updated = pair.getValue0();
86 Map<String, ForwardingPathDataDefinition> deleted = pair.getValue1();
87 if (deleted != null && !deleted.isEmpty()) {
88 Set<String> deleteEither = serviceBusinessLogic
89 .deleteForwardingPaths(service.getUniqueId(), new HashSet<>(deleted.keySet()), user, false);
90 deleted.keySet().forEach(key -> service.getForwardingPaths().remove(key));
92 if (updated != null && !updated.isEmpty()) {
93 Service updateFPService = new Service();
94 updateFPService.setForwardingPaths(updated);
95 Service updateFPEither = serviceBusinessLogic.updateForwardingPath(service.getUniqueId(), updateFPService, user, false);
96 updated.forEach((key, forwardingPathDataDefinition) -> service.getForwardingPaths().put(key, forwardingPathDataDefinition));
98 return updatedContainerComponent;
101 private List<CapabilityDefinition> getAllInstanceCapabilities(ComponentInstance currentResourceInstance) {
102 if (currentResourceInstance.getCapabilities() == null || currentResourceInstance.getCapabilities().isEmpty()) {
103 return Collections.EMPTY_LIST;
105 return currentResourceInstance.getCapabilities().values().stream().flatMap(Collection::stream).collect(Collectors.toList());