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