/*- * ============LICENSE_START======================================================= * SDC * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ package org.openecomp.sdc.be.components.merge.property; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; import org.openecomp.sdc.be.dao.utils.MapUtil; import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.model.InputDefinition; class PropertyInstanceMergeDataBuilder { private PropertyInstanceMergeDataBuilder() { } static List buildDataForMerging(List oldProps, List oldInputs, List newProps, List newInputs) { Map oldPropsByName = MapUtil.toMap(oldProps, T::getName); Map oldInputsByName = MapUtil.toMap(oldInputs, InputDefinition::getName); Map newPropsByName = MapUtil.toMap(newProps, T::getName); Map newInputsByName = MapUtil.toMap(newInputs, InputDefinition::getName); return buildMergeData(oldPropsByName, oldInputsByName, newPropsByName, newInputsByName); } private static List buildMergeData(Map oldPropsByName, Map oldInputsByName, Map newPropsByName, Map newInputsByName) { List mergeData = new ArrayList<>(); newPropsByName.forEach((name, prop) -> { if (oldPropsByName.containsKey(name)) { mergeData.add(buildMergePropertyData(oldPropsByName.get(name), oldInputsByName, prop, newInputsByName)); } }); return mergeData; } private static MergePropertyData buildMergePropertyData(PropertyDataDefinition oldProp, Map oldInputsByName, PropertyDataDefinition newProp, Map newInputsByName) { MergePropertyData mergePropertyData = new MergePropertyData(); mergePropertyData.setOldProp(oldProp).setNewProp(newProp); if (oldProp.isGetInputProperty()) { setGetInputData(oldProp, oldInputsByName, newInputsByName, mergePropertyData); } return mergePropertyData; } private static void setGetInputData(PropertyDataDefinition oldProp, Map oldInputsByName, Map newInputsByName, MergePropertyData mergePropertyData) { List oldDeclaredByUserInputNames = getOldDeclaredInputsByUser(oldProp.getGetInputValues(), oldInputsByName); List oldGetInputNamesWhichExistInNewVersion = getOldGetInputNamesWhichExistInNewVersion(oldProp.getGetInputValues(), newInputsByName); mergePropertyData.addAddGetInputNamesToMerge(oldDeclaredByUserInputNames); mergePropertyData.addAddGetInputNamesToMerge(oldGetInputNamesWhichExistInNewVersion); } private static List getOldGetInputNamesWhichExistInNewVersion(List getInputValues, Map newInputsByName) { return getInputValues.stream().map(GetInputValueDataDefinition::getInputName).filter(newInputsByName::containsKey) .collect(Collectors.toList()); } private static List getOldDeclaredInputsByUser(List getInputValues, Map oldInputsByName) { return getInputValues.stream().map(GetInputValueDataDefinition::getInputName).map(oldInputsByName::get) .filter(oldInput -> Objects.nonNull(oldInput) && oldInput.getInstanceUniqueId() != null).map(PropertyDataDefinition::getName) .collect(Collectors.toList()); } }