Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / heat / HeatEnvArtifactsMergeBusinessLogic.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.heat;
22
23 import org.openecomp.sdc.be.dao.utils.MapUtil;
24 import org.openecomp.sdc.be.model.ArtifactDefinition;
25 import org.openecomp.sdc.be.model.HeatParameterDefinition;
26 import org.springframework.stereotype.Component;
27 import org.springframework.util.CollectionUtils;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32
33 /**
34  * Created by chaya on 9/14/2017.
35  */
36 @Component
37 public class HeatEnvArtifactsMergeBusinessLogic {
38
39     public List<ArtifactDefinition> mergeInstanceHeatEnvArtifacts(List<ArtifactDefinition> origHeatEnvArtifacts, List<ArtifactDefinition> newHeatEnvArtifacts) {
40         Map<String, ArtifactDefinition> origArtifactDefinitionByLabel = MapUtil.toMap(origHeatEnvArtifacts, ArtifactDefinition::getArtifactLabel);
41         List<ArtifactDefinition> artifactsToUpdate = new ArrayList<>();
42         newHeatEnvArtifacts.stream()
43                 .filter(heatEnvArtifact -> origArtifactDefinitionByLabel.containsKey(heatEnvArtifact.getArtifactLabel()))
44                 .forEach(heatEnvArtifact -> {
45                     ArtifactDefinition origHeatEnvArtifact = origArtifactDefinitionByLabel.get(heatEnvArtifact.getArtifactLabel());
46                     Boolean wasMergedHeatEnvArtifact = mergeHeatEnvArtifactsParameters(heatEnvArtifact, origHeatEnvArtifact);
47                     if (wasMergedHeatEnvArtifact) {
48                         artifactsToUpdate.add(heatEnvArtifact);
49                     }
50                 });
51         return artifactsToUpdate;
52     }
53
54     private Boolean mergeHeatEnvArtifactsParameters(ArtifactDefinition currArtifact, ArtifactDefinition origArtifact) {
55         List<HeatParameterDefinition> currentHeatEnvParams = currArtifact.getListHeatParameters();
56         List<HeatParameterDefinition> origHeatEnvParams = origArtifact.getListHeatParameters();
57         boolean wasChanged = false;
58
59         if (CollectionUtils.isEmpty(origHeatEnvParams) || CollectionUtils.isEmpty(currentHeatEnvParams)) {
60             return false;
61         }
62
63         Map<String, HeatParameterDefinition> origHeatParametersByName = MapUtil.toMap(origHeatEnvParams, HeatParameterDefinition::getName);
64
65         for (HeatParameterDefinition currHeatParam : currentHeatEnvParams) {
66             String paramName = currHeatParam.getName();
67             HeatParameterDefinition origHeatParam = origHeatParametersByName.get(paramName);
68             if (isSameHeatWithDiffValue(origHeatParam, currHeatParam)) {
69                 currHeatParam.setCurrentValue(origHeatParam.getCurrentValue());
70                 wasChanged = true;
71             }
72         }
73         currArtifact.setListHeatParameters(currentHeatEnvParams);
74         return wasChanged;
75     }
76
77     private boolean isSameHeatWithDiffValue(HeatParameterDefinition origHeatParam, HeatParameterDefinition newHeatParam) {
78         return origHeatParam != null &&
79                origHeatParam.getCurrentValue() != null &&
80                origHeatParam.getType().equals(newHeatParam.getType()) &&
81               !origHeatParam.getCurrentValue().equals(newHeatParam.getCurrentValue());
82     }
83
84 }