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