Catalog alignment
[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 org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
24 import org.openecomp.sdc.be.model.ArtifactDefinition;
25 import org.openecomp.sdc.be.model.Component;
26 import org.openecomp.sdc.be.model.ComponentInstance;
27 import org.openecomp.sdc.be.model.User;
28 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
29 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
30 import org.openecomp.sdc.common.api.Constants;
31 import org.springframework.beans.factory.annotation.Autowired;
32
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.Objects;
36 import java.util.Optional;
37 import java.util.stream.Collectors;
38
39 /**
40  * Created by chaya on 9/20/2017.
41  */
42 @org.springframework.stereotype.Component("ComponentInstanceArtifactsMerge")
43 public class ComponentInstanceArtifactsMerge implements ComponentInstanceMergeInterface {
44
45     @Autowired
46     ToscaOperationFacade toscaOperationFacade;
47
48     @Autowired
49     ArtifactsBusinessLogic artifactsBusinessLogic;
50
51     @Override
52     public void saveDataBeforeMerge(DataForMergeHolder dataHolder, Component containerComponent, ComponentInstance currentResourceInstance, Component originComponent) {
53         Map<String, ArtifactDefinition> componentInstancesDeploymentArtifacts = currentResourceInstance.safeGetDeploymentArtifacts();
54         Map<String, ArtifactDefinition> originalComponentDeploymentArtifacts = originComponent.getDeploymentArtifacts();
55         Map<String, ArtifactDefinition> deploymentArtifactsCreatedOnTheInstance = componentInstancesDeploymentArtifacts.entrySet()
56                 .stream()
57                 .filter(i -> !originalComponentDeploymentArtifacts.containsKey(i.getKey()))
58                 .filter(i -> !ArtifactTypeEnum.VF_MODULES_METADATA.name().equals(i.getValue().getArtifactType()))
59                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
60
61         dataHolder.setOrigComponentDeploymentArtifactsCreatedOnTheInstance(deploymentArtifactsCreatedOnTheInstance);
62 //        dataHolder.setComponentInstanceDeploymentArtifactsTimeOut(componentInstancesDeploymentArtifacts.entrySet().stream()
63 //                .collect(Collectors.toMap(Map.Entry::getKey, artifact -> artifact.getValue().getTimeout())));
64         dataHolder.setComponentInstanceDeploymentArtifactsTimeOut(componentInstancesDeploymentArtifacts.entrySet().stream()
65                 .collect(HashMap::new, (map,entry) -> map.put(entry.getKey(), entry.getValue().getTimeout()) ,HashMap::putAll));
66         Map<String, ArtifactDefinition> componentInstancesInformationalArtifacts = currentResourceInstance.safeGetArtifacts();
67         Map<String, ArtifactDefinition> originalComponentInformationalArtifacts = originComponent.getArtifacts();
68         Map<String, ArtifactDefinition> informationalArtifactsCreatedOnTheInstance = componentInstancesInformationalArtifacts.entrySet()
69                 .stream()
70                 .filter(i -> !originalComponentInformationalArtifacts.containsKey(i.getKey()))
71                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
72         dataHolder.setOrigComponentInformationalArtifactsCreatedOnTheInstance(informationalArtifactsCreatedOnTheInstance);
73     }
74
75     private void addEsIdToArtifactJson(Map<String, Object> artifactJson, String origEsId) {
76         artifactJson.put(Constants.ARTIFACT_ES_ID, origEsId);
77     }
78
79     @Override
80     public Component mergeDataAfterCreate(User user, DataForMergeHolder dataHolder, Component updatedContainerComponent, String newInstanceId) {
81         Map<String, ArtifactDefinition> origInstanceDeploymentArtifactsCreatedOnTheInstance = dataHolder.getOrigComponentDeploymentArtifactsCreatedOnTheInstance();
82         Map<String, ArtifactDefinition> currentInstanceDeploymentArtifacts = updatedContainerComponent.safeGetComponentInstanceDeploymentArtifacts(newInstanceId);
83         Map<String, ArtifactDefinition> filteredDeploymentArtifactsToAdd = Optional.ofNullable(origInstanceDeploymentArtifactsCreatedOnTheInstance).orElse(new HashMap<>()).entrySet().stream()
84                 .filter(artifact -> noArtifactWithTheSameLabel(artifact.getValue().getArtifactLabel(), currentInstanceDeploymentArtifacts))
85                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
86         Map<String, ArtifactDefinition> updatedTimeOutDeploymentArtifacts = getUpdatedTimeOutDeploymentArtifacts(dataHolder, currentInstanceDeploymentArtifacts);
87         Map<String, ArtifactDefinition> origInstanceInformationalArtifactsCreatedOnTheInstance = dataHolder.getOrigComponentInformationalArtifactsCreatedOnTheInstance();
88         Map<String, ArtifactDefinition> currentInstanceInformationalArtifacts = updatedContainerComponent.safeGetComponentInstanceInformationalArtifacts(newInstanceId);
89         Map<String, ArtifactDefinition> filteredInformationalArtifactsToAdd = Optional.ofNullable(origInstanceInformationalArtifactsCreatedOnTheInstance).orElse(new HashMap<>()).entrySet().stream()
90                 .filter(artifact -> noArtifactWithTheSameLabel(artifact.getValue().getArtifactLabel(), currentInstanceInformationalArtifacts))
91                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
92         Map<String, ArtifactDefinition> allFilteredArtifactsToAdd = new HashMap<>();
93         allFilteredArtifactsToAdd.putAll(filteredDeploymentArtifactsToAdd);
94         allFilteredArtifactsToAdd.putAll(updatedTimeOutDeploymentArtifacts);
95         allFilteredArtifactsToAdd.putAll(filteredInformationalArtifactsToAdd);
96
97         for (Map.Entry<String, ArtifactDefinition> currentArtifactDefinition :  allFilteredArtifactsToAdd.entrySet()) {
98             Map<String, Object> jsonForUpdateArtifact = artifactsBusinessLogic.buildJsonForUpdateArtifact(
99                     currentArtifactDefinition.getValue().getUniqueId(), 
100                     currentArtifactDefinition.getValue().getArtifactName(),
101                     currentArtifactDefinition.getValue().getArtifactType(), 
102                     currentArtifactDefinition.getValue().getArtifactGroupType(),
103                     currentArtifactDefinition.getValue().getArtifactLabel(), 
104                     currentArtifactDefinition.getValue().getArtifactDisplayName(),
105                     currentArtifactDefinition.getValue().getDescription(), 
106                     currentArtifactDefinition.getValue().getPayloadData(),
107                     null, currentArtifactDefinition.getValue().getListHeatParameters());
108             addEsIdToArtifactJson(jsonForUpdateArtifact, currentArtifactDefinition.getValue().getEsId());
109             artifactsBusinessLogic.updateResourceInstanceArtifactNoContent(newInstanceId, updatedContainerComponent,
110                             user, jsonForUpdateArtifact, artifactsBusinessLogic.new ArtifactOperationInfo(
111                                     false, false, ArtifactsBusinessLogic.ArtifactOperationEnum.LINK), currentArtifactDefinition.getValue());
112         }
113         return updatedContainerComponent;
114     }
115
116     private Map<String, ArtifactDefinition> getUpdatedTimeOutDeploymentArtifacts(DataForMergeHolder dataHolder, Map<String, ArtifactDefinition> currentInstanceDeploymentArtifacts) {
117         return currentInstanceDeploymentArtifacts.entrySet().stream()
118                 .filter(artifact -> Objects.isNull(artifact.getValue().getTimeout()) || !artifact.getValue().getTimeout()
119                         .equals(dataHolder.getComponentInstanceDeploymentArtifactsTimeOut().get(artifact.getKey())))
120                 .collect(Collectors.toMap(Map.Entry::getKey, artifact -> mergeTimeOut(artifact.getValue(), dataHolder
121                         .getComponentInstanceDeploymentArtifactsTimeOut().get(artifact.getKey()))));
122     }
123
124     private ArtifactDefinition mergeTimeOut(ArtifactDefinition artifact, Integer updatedTimeOut) {
125         artifact.setTimeout(updatedTimeOut);
126         return artifact;
127     }
128
129     private boolean noArtifactWithTheSameLabel(String artifactLabel, Map<String, ArtifactDefinition> currDeploymentArtifacts) {
130         for (Map.Entry<String, ArtifactDefinition> artifact : currDeploymentArtifacts.entrySet()) {
131             if (artifact.getValue().getArtifactLabel().equals(artifactLabel)) {
132                 return false;
133             }
134         }
135         return true;
136     }
137 }