ConfigMergeHelper { @SuppressWarnings("unchecked") public Map<String, Map<String, ThreeWayValue>> getConflicts(String clusterName, StackId targetStack) throws AmbariException { Cluster cluster = m_clusters.get().getCluster(clusterName); StackId oldStack = cluster.getCurrentStackVersion(); Map<String, Map<String, String>> oldMap = new HashMap<>(); Map<String, Map<String, String>> newMap = new HashMap<>(); for (String serviceName : cluster.getServices().keySet()) { Set<PropertyInfo> oldStackProperties = m_ambariMetaInfo.get().getServiceProperties( oldStack.getStackName(), oldStack.getStackVersion(), serviceName); addToMap(oldMap, oldStackProperties); Set<PropertyInfo> newStackProperties = m_ambariMetaInfo.get().getServiceProperties( targetStack.getStackName(), targetStack.getStackVersion(), serviceName); addToMap(newMap, newStackProperties); } Set<PropertyInfo> set = m_ambariMetaInfo.get().getStackProperties( oldStack.getStackName(), oldStack.getStackVersion()); addToMap(oldMap, set); set = m_ambariMetaInfo.get().getStackProperties( targetStack.getStackName(), targetStack.getStackVersion()); addToMap(newMap, set); Map<String, Map<String, ThreeWayValue>> result = new HashMap<String, Map<String, ThreeWayValue>>(); for (Entry<String, Map<String, String>> entry : oldMap.entrySet()) { if (!newMap.containsKey(entry.getKey())) { LOG.info("Stack {} does not have an equivalent config type {} in {}", oldStack.getStackId(), entry.getKey(), targetStack.getStackId()); continue; } Map<String, String> oldPairs = entry.getValue(); Map<String, String> newPairs = newMap.get(entry.getKey()); Collection<String> customValueKeys = null; Config config = cluster.getDesiredConfigByType(entry.getKey()); if (null != config) { Set<String> valueKeys = config.getProperties().keySet(); customValueKeys = CollectionUtils.subtract(valueKeys, oldPairs.keySet()); } if (null != customValueKeys) { for (String prop : customValueKeys) { String newVal = newPairs.get(prop); String savedVal = config.getProperties().get(prop); if (null != newVal && null != savedVal && !newVal.equals(savedVal)) { ThreeWayValue twv = new ThreeWayValue(); twv.oldStackValue = null; twv.newStackValue = normalizeValue(savedVal, newVal.trim()); twv.savedValue = savedVal.trim(); if (!result.containsKey(entry.getKey())) { result.put(entry.getKey(), new HashMap<String, ThreeWayValue>()); } result.get(entry.getKey()).put(prop, twv); } } } Collection<String> common = CollectionUtils.intersection(newPairs.keySet(), oldPairs.keySet()); for (String prop : common) { String oldStackVal = oldPairs.get(prop); String newStackVal = newPairs.get(prop); String savedVal = ""; if (null != config) { savedVal = config.getProperties().get(prop); } if (!(newStackVal == null || oldStackVal == null) && !newStackVal.equals(savedVal) && (!oldStackVal.equals(newStackVal) || !oldStackVal.equals(savedVal))) { ThreeWayValue twv = new ThreeWayValue(); twv.oldStackValue = normalizeValue(savedVal, oldStackVal.trim()); twv.newStackValue = normalizeValue(savedVal, newStackVal.trim()); twv.savedValue = (null == savedVal) ? null : savedVal.trim(); if (!result.containsKey(entry.getKey())) { result.put(entry.getKey(), new HashMap<String, ThreeWayValue>()); } result.get(entry.getKey()).put(prop, twv); } } } return result; }  @SuppressWarnings("unchecked") Map<String, Map<String, ThreeWayValue>> getConflicts(String clusterName, StackId targetStack);  }
@Test public void testGetConflicts() throws Exception { Cluster clusterMock = createNiceMock(Cluster.class); expect(clustersMock.getCluster(anyString())).andReturn(clusterMock); expect(clusterMock.getCurrentStackVersion()).andReturn(currentStackId); expect(clusterMock.getServices()).andReturn(new HashMap<String, Service>() {{ put("HDFS", createNiceMock(Service.class)); put("ZK", createNiceMock(Service.class)); }}); Set<PropertyInfo> currentHDFSProperties = new HashSet<PropertyInfo>() {{ add(createPropertyInfo("hdfs-env.xml", "equal.key", "equal-value")); }}; Set<PropertyInfo> currentZKProperties = new HashSet<PropertyInfo>() {{ add(createPropertyInfo("zk-env.xml", "different.key", "different-value-1")); }}; Set<PropertyInfo> currentStackProperties = new HashSet<PropertyInfo>() {{ add(createPropertyInfo("hadoop-env.xml", "equal.key", "modified.value")); }}; expect(ambariMetaInfoMock.getServiceProperties(currentStackId.getStackName(), currentStackId.getStackVersion(), "HDFS")).andReturn(currentHDFSProperties); expect(ambariMetaInfoMock.getServiceProperties(currentStackId.getStackName(), currentStackId.getStackVersion(), "ZK")).andReturn(currentZKProperties); expect(ambariMetaInfoMock.getStackProperties(currentStackId.getStackName(), currentStackId.getStackVersion())).andReturn(currentStackProperties); Set<PropertyInfo> newHDFSProperties = new HashSet<PropertyInfo>() {{ add(createPropertyInfo("hdfs-env.xml", "equal.key", "equal-value")); add(createPropertyInfo("new-hdfs-config.xml", "equal.key", "equal-value")); }}; Set<PropertyInfo> newZKProperties = new HashSet<PropertyInfo>() {{ add(createPropertyInfo("zk-env.xml", "equal.key", "different-value-2")); add(createPropertyInfo("zk-env.xml", "new.key", "new-value-2")); }}; Set<PropertyInfo> newStackProperties = new HashSet<PropertyInfo>() {{ add(createPropertyInfo("hadoop-env.xml", "equal.key", "another.value")); }}; expect(ambariMetaInfoMock.getServiceProperties(newStackId.getStackName(), newStackId.getStackVersion(), "HDFS")).andReturn(newHDFSProperties); expect(ambariMetaInfoMock.getServiceProperties(newStackId.getStackName(), newStackId.getStackVersion(), "ZK")).andReturn(newZKProperties); expect(ambariMetaInfoMock.getStackProperties(newStackId.getStackName(), newStackId.getStackVersion())).andReturn(newStackProperties); Map<String, String> desiredHdfsEnvProperties = new HashMap<>(); expect(clusterMock.getDesiredConfigByType("hdfs-env.xml")).andReturn( createConfigMock(desiredHdfsEnvProperties) ); Map<String, String> desiredZkEnvProperties = new HashMap<>(); expect(clusterMock.getDesiredConfigByType("hdfs-env.xml")).andReturn( createConfigMock(desiredZkEnvProperties) ); Map<String, String> desiredHadoopEnvProperties = new HashMap<>(); expect(clusterMock.getDesiredConfigByType("hadoop-env.xml")).andReturn( createConfigMock(desiredHadoopEnvProperties) ); replay(clusterMock, clustersMock, ambariMetaInfoMock); ConfigMergeHelper configMergeHelper = injector.getInstance(ConfigMergeHelper.class); Map<String, Map<String, ThreeWayValue>> conflicts = configMergeHelper.getConflicts( "clustername", newStackId); assertNotNull(conflicts); assertEquals(2, conflicts.size()); for (String key : conflicts.keySet()) { if (key.equals("hdfs-env")) { Map<String, ThreeWayValue> stringThreeWayValueMap = conflicts.get(key); assertEquals(1, stringThreeWayValueMap.size()); assertEquals("equal-value", stringThreeWayValueMap.get("equal.key").oldStackValue); assertEquals("equal-value", stringThreeWayValueMap.get("equal.key").newStackValue); assertEquals("", stringThreeWayValueMap.get("equal.key").savedValue); } else if (key.equals("hadoop-env")) { Map<String, ThreeWayValue> stringThreeWayValueMap = conflicts.get(key); assertEquals(1, stringThreeWayValueMap.size()); assertEquals("modified.value", stringThreeWayValueMap.get("equal.key").oldStackValue); assertEquals("another.value", stringThreeWayValueMap.get("equal.key").newStackValue); assertEquals("", stringThreeWayValueMap.get("equal.key").savedValue); } else { fail("Unexpected key"); } } assertEquals(2, conflicts.size()); }