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.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
25 import org.openecomp.sdc.be.model.*;
26 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
27 import org.openecomp.sdc.common.api.Constants;
28 import org.openecomp.sdc.exception.ResponseFormat;
29 import org.springframework.beans.factory.annotation.Autowired;
31 import java.util.HashMap;
33 import java.util.Optional;
34 import java.util.stream.Collectors;
37 * Created by chaya on 9/20/2017.
39 @org.springframework.stereotype.Component("ComponentInstanceArtifactsMerge")
40 public class ComponentInstanceArtifactsMerge implements ComponentInstanceMergeInterface {
43 ToscaOperationFacade toscaOperationFacade;
46 ArtifactsBusinessLogic artifactsBusinessLogic;
49 public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent, ComponentInstance currentResourceInstance, Component originComponent) {
50 Map<String, ArtifactDefinition> componentInstancesDeploymentArtifacts = currentResourceInstance.safeGetDeploymentArtifacts();
51 Map<String, ArtifactDefinition> originalComponentDeploymentArtifacts = originComponent.getDeploymentArtifacts();
52 Map<String, ArtifactDefinition> deploymentArtifactsCreatedOnTheInstance = componentInstancesDeploymentArtifacts.entrySet()
54 .filter(i -> !originalComponentDeploymentArtifacts.containsKey(i.getKey()))
55 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
57 dataHolder.setOrigComponentDeploymentArtifactsCreatedOnTheInstance(deploymentArtifactsCreatedOnTheInstance);
59 Map<String, ArtifactDefinition> componentInstancesInformationalArtifacts = currentResourceInstance.safeGetArtifacts();
60 Map<String, ArtifactDefinition> originalComponentInformationalArtifacts = originComponent.getArtifacts();
61 Map<String, ArtifactDefinition> informationalArtifactsCreatedOnTheInstance = componentInstancesInformationalArtifacts.entrySet()
63 .filter(i -> !originalComponentInformationalArtifacts.containsKey(i.getKey()))
64 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
65 dataHolder.setOrigComponentInformationalArtifactsCreatedOnTheInstance(informationalArtifactsCreatedOnTheInstance);
68 private void addEsIdToArtifactJson(Map<String, Object> artifactJson, String origEsId) {
69 artifactJson.put(Constants.ARTIFACT_ES_ID, origEsId);
73 public Either<Component, ResponseFormat> mergeDataAfterCreate(User user, DataForMergeHolder dataHolder, Component updatedContainerComponent, String newInstanceId) {
74 Map<String, ArtifactDefinition> origInstanceDeploymentArtifactsCreatedOnTheInstance = dataHolder.getOrigComponentDeploymentArtifactsCreatedOnTheInstance();
75 Map<String, ArtifactDefinition> currentInstanceDeploymentArtifacts = updatedContainerComponent.safeGetComponentInstanceDeploymentArtifacts(newInstanceId);
76 Map<String, ArtifactDefinition> filteredDeploymentArtifactsToAdd = Optional.ofNullable(origInstanceDeploymentArtifactsCreatedOnTheInstance).orElse(new HashMap<>()).entrySet().stream()
77 .filter(artifact -> noArtifactWithTheSameLabel(artifact.getValue().getArtifactLabel(), currentInstanceDeploymentArtifacts))
78 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
79 Map<String, ArtifactDefinition> origInstanceInformationalArtifactsCreatedOnTheInstance = dataHolder.getOrigComponentInformationalArtifactsCreatedOnTheInstance();
80 Map<String, ArtifactDefinition> currentInstanceInformationalArtifacts = updatedContainerComponent.safeGetComponentInstanceInformationalArtifacts(newInstanceId);
81 Map<String, ArtifactDefinition> filteredInformationalArtifactsToAdd = Optional.ofNullable(origInstanceInformationalArtifactsCreatedOnTheInstance).orElse(new HashMap<>()).entrySet().stream()
82 .filter(artifact -> noArtifactWithTheSameLabel(artifact.getValue().getArtifactLabel(), currentInstanceInformationalArtifacts))
83 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
84 Map<String, ArtifactDefinition> allFilteredArtifactsToAdd = new HashMap<>();
85 allFilteredArtifactsToAdd.putAll(filteredDeploymentArtifactsToAdd);
86 allFilteredArtifactsToAdd.putAll(filteredInformationalArtifactsToAdd);
88 for (Map.Entry<String, ArtifactDefinition> currentArtifactDefinition : allFilteredArtifactsToAdd.entrySet()) {
89 Map<String, Object> jsonForUpdateArtifact = artifactsBusinessLogic.buildJsonForUpdateArtifact(
90 currentArtifactDefinition.getValue().getUniqueId(),
91 currentArtifactDefinition.getValue().getArtifactName(),
92 currentArtifactDefinition.getValue().getArtifactType(),
93 currentArtifactDefinition.getValue().getArtifactGroupType(),
94 currentArtifactDefinition.getValue().getArtifactLabel(),
95 currentArtifactDefinition.getValue().getArtifactDisplayName(),
96 currentArtifactDefinition.getValue().getDescription(),
97 currentArtifactDefinition.getValue().getPayloadData(),
98 null, currentArtifactDefinition.getValue().getListHeatParameters());
99 addEsIdToArtifactJson(jsonForUpdateArtifact, currentArtifactDefinition.getValue().getEsId());
100 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> uploadArtifactToService =
101 artifactsBusinessLogic.updateResourceInstanceArtifactNoContent(newInstanceId, updatedContainerComponent,
102 user, jsonForUpdateArtifact, artifactsBusinessLogic.new ArtifactOperationInfo(
103 false, false, ArtifactsBusinessLogic.ArtifactOperationEnum.LINK), currentArtifactDefinition.getValue());
104 if (uploadArtifactToService.isRight()) {
105 return Either.right(uploadArtifactToService.right().value());
108 return Either.left(updatedContainerComponent);
111 private boolean noArtifactWithTheSameLabel(String artifactLabel, Map<String, ArtifactDefinition> currDeploymentArtifacts) {
112 for (Map.Entry<String, ArtifactDefinition> artifact : currDeploymentArtifacts.entrySet()) {
113 if (artifact.getValue().getArtifactLabel().equals(artifactLabel)) {