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.heat;
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;
29 import java.util.ArrayList;
30 import java.util.List;
34 * Created by chaya on 9/14/2017.
37 public class HeatEnvArtifactsMergeBusinessLogic {
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);
51 return artifactsToUpdate;
54 private Boolean mergeHeatEnvArtifactsParameters(ArtifactDefinition currArtifact, ArtifactDefinition origArtifact) {
55 List<HeatParameterDefinition> currentHeatEnvParams = currArtifact.getListHeatParameters();
56 List<HeatParameterDefinition> origHeatEnvParams = origArtifact.getListHeatParameters();
57 boolean wasChanged = false;
59 if (CollectionUtils.isEmpty(origHeatEnvParams) || CollectionUtils.isEmpty(currentHeatEnvParams)) {
63 Map<String, HeatParameterDefinition> origHeatParametersByName = MapUtil.toMap(origHeatEnvParams, HeatParameterDefinition::getName);
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());
73 currArtifact.setListHeatParameters(currentHeatEnvParams);
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());