Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / instance / ComponentInstanceArtifactsMerge.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  */
20
21 package org.openecomp.sdc.be.components.merge.instance;
22
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;
30
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Optional;
34 import java.util.stream.Collectors;
35
36 /**
37  * Created by chaya on 9/20/2017.
38  */
39 @org.springframework.stereotype.Component("ComponentInstanceArtifactsMerge")
40 public class ComponentInstanceArtifactsMerge implements ComponentInstanceMergeInterface {
41
42     @Autowired
43     ToscaOperationFacade toscaOperationFacade;
44
45     @Autowired
46     ArtifactsBusinessLogic artifactsBusinessLogic;
47
48     @Override
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()
53                 .stream()
54                 .filter(i -> !originalComponentDeploymentArtifacts.containsKey(i.getKey()))
55                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
56
57         dataHolder.setOrigComponentDeploymentArtifactsCreatedOnTheInstance(deploymentArtifactsCreatedOnTheInstance);
58
59         Map<String, ArtifactDefinition> componentInstancesInformationalArtifacts = currentResourceInstance.safeGetArtifacts();
60         Map<String, ArtifactDefinition> originalComponentInformationalArtifacts = originComponent.getArtifacts();
61         Map<String, ArtifactDefinition> informationalArtifactsCreatedOnTheInstance = componentInstancesInformationalArtifacts.entrySet()
62                 .stream()
63                 .filter(i -> !originalComponentInformationalArtifacts.containsKey(i.getKey()))
64                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
65         dataHolder.setOrigComponentInformationalArtifactsCreatedOnTheInstance(informationalArtifactsCreatedOnTheInstance);
66     }
67
68     private void addEsIdToArtifactJson(Map<String, Object> artifactJson, String origEsId) {
69         artifactJson.put(Constants.ARTIFACT_ES_ID, origEsId);
70     }
71
72     @Override
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);
87
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());
106             }
107         }
108         return Either.left(updatedContainerComponent);
109     }
110
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)) {
114                 return false;
115             }
116         }
117         return true;
118     }
119 }