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.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
28 import org.openecomp.sdc.be.impl.ComponentsUtils;
29 import org.openecomp.sdc.be.impl.ForwardingPathUtils;
30 import org.openecomp.sdc.be.model.*;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
32 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.openecomp.sdc.exception.ResponseFormat;
35 import org.springframework.beans.factory.annotation.Autowired;
38 import java.util.stream.Collectors;
40 @org.springframework.stereotype.Component
41 public class ComponentInstanceForwardingPathMerge implements ComponentInstanceMergeInterface {
43 private static Logger log = Logger.getLogger(ComponentInstanceForwardingPathMerge.class);
46 private ServiceBusinessLogic serviceBusinessLogic;
49 private ToscaOperationFacade toscaOperationFacade;
52 private ComponentsUtils componentsUtils;
55 public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent,
56 ComponentInstance currentResourceInstance, Component originComponent) {
57 dataHolder.setOrigInstanceCapabilities(getAllInstanceCapabilities(currentResourceInstance));
58 dataHolder.setOrigInstanceNode(originComponent);
59 dataHolder.setOrigComponentInstId(currentResourceInstance.getName());
63 public Either<Component, ResponseFormat> mergeDataAfterCreate(User user, DataForMergeHolder dataHolder,
64 Component updatedContainerComponent, String newInstanceId) {
65 if (!(updatedContainerComponent instanceof Service)) {
66 // no need to handle forwarding paths
67 return Either.left(updatedContainerComponent);
69 Service service = (Service) updatedContainerComponent;
70 ComponentInstance ci = service.getComponentInstanceById(newInstanceId).orElse(null);
72 ResponseFormat responseFormat = componentsUtils
73 .getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, newInstanceId);
74 return Either.right(responseFormat);
76 Either<Component, StorageOperationStatus> resourceEither = toscaOperationFacade.getToscaFullElement(ci.getComponentUid());
77 if (resourceEither.isRight() ) {
78 log.debug("Failed to fetch resource with id {} for instance {}",ci.getComponentUid() ,ci.getUniqueId());
79 ResponseFormat responseFormat = componentsUtils
80 .getResponseFormat(componentsUtils.convertFromStorageResponse(resourceEither.right().value()));
81 return Either.right(responseFormat);
84 Component fetchedComponent = resourceEither.left().value();
86 Pair<Map<String, ForwardingPathDataDefinition>, Map<String, ForwardingPathDataDefinition>> pair = new ForwardingPathUtils()
87 .updateForwardingPathOnVersionChange(service, dataHolder, fetchedComponent, newInstanceId);
88 Map<String, ForwardingPathDataDefinition> updated = pair.getValue0();
89 Map<String, ForwardingPathDataDefinition> deleted = pair.getValue1();
90 if (deleted != null && !deleted.isEmpty()) {
91 Either<Set<String>, ResponseFormat> deleteEither = serviceBusinessLogic
92 .deleteForwardingPaths(service.getUniqueId(), new HashSet<>(deleted.keySet()), user, false);
93 if (deleteEither.isRight()) {
94 if (log.isDebugEnabled()) {
95 log.debug("Failed to delete forwarding paths : {}", deleted.values().stream()
96 .map(ForwardingPathDataDefinition::getName).collect(Collectors.joining(", ", "( ", " )")));
98 return Either.right(deleteEither.right().value());
100 deleted.keySet().forEach(key -> service.getForwardingPaths().remove(key));
102 if (updated != null && !updated.isEmpty()) {
103 Service updateFPService = new Service();
104 updateFPService.setForwardingPaths(updated);
105 Either<Service, ResponseFormat> updateFPEither = serviceBusinessLogic
106 .updateForwardingPath(service.getUniqueId(), updateFPService, user, false);
107 if (updateFPEither.isRight()) {
108 if (log.isDebugEnabled()) {
109 log.debug("Failed to update forwarding paths : {}", updated.values().stream()
110 .map(ForwardingPathDataDefinition::getName).collect(Collectors.joining(", ", "( ", " )")));
112 return Either.right(updateFPEither.right().value());
114 updated.forEach((key, forwardingPathDataDefinition) -> service.getForwardingPaths().put(key,forwardingPathDataDefinition));
116 return Either.left(updatedContainerComponent);
120 private List<CapabilityDefinition> getAllInstanceCapabilities(ComponentInstance currentResourceInstance) {
121 if(currentResourceInstance.getCapabilities() == null || currentResourceInstance.getCapabilities().isEmpty()){
122 return Collections.EMPTY_LIST;
124 return currentResourceInstance.getCapabilities().values().stream().flatMap(Collection::stream)
125 .collect(Collectors.toList());