From 3ead3d4a66e014b3cfa3ea1f731630c36906a4b7 Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Thu, 25 Feb 2016 16:57:26 +0100 Subject: [PATCH] Use a utility function for key-value to map conversions YangUtils::convertKeyValueListToMap() reproduces the behaviour expected when converting a list of YANG key values to a map: it checks that all the keys and values are non-null, and converts them to strings using the appropriate methods in each YANG type. This patch can't be back-ported to Beryllium since it uses Java 8 constructs. Change-Id: Iaeb751ca552caf6b8f5a6d5a6e506e5d2aa4e63f Signed-off-by: Stephen Kitt --- utils/pom.xml | 1 + utils/yang-utils/pom.xml | 50 +++++++++++++ .../ovsdb/utils/yang/YangUtils.java | 75 +++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 utils/yang-utils/pom.xml create mode 100644 utils/yang-utils/src/main/java/org/opendaylight/ovsdb/utils/yang/YangUtils.java diff --git a/utils/pom.xml b/utils/pom.xml index 90ac03ae9e..ce6acfb639 100644 --- a/utils/pom.xml +++ b/utils/pom.xml @@ -57,6 +57,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html servicehelper southbound-utils hwvtepsouthbound-utils + yang-utils diff --git a/utils/yang-utils/pom.xml b/utils/yang-utils/pom.xml new file mode 100644 index 0000000000..b8a502d5c5 --- /dev/null +++ b/utils/yang-utils/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + org.opendaylight.odlparent + bundle-parent + 1.7.0-SNAPSHOT + + + + org.opendaylight.ovsdb + utils.yang-utils + 1.3.0-SNAPSHOT + bundle + + + + junit + junit + test + + + com.google.guava + guava + + + com.google.code.findbugs + jsr305 + + + + + + + org.apache.felix + maven-bundle-plugin + true + + + + org.opendaylight.ovsdb.utils.yang + + + + + + + diff --git a/utils/yang-utils/src/main/java/org/opendaylight/ovsdb/utils/yang/YangUtils.java b/utils/yang-utils/src/main/java/org/opendaylight/ovsdb/utils/yang/YangUtils.java new file mode 100644 index 0000000000..6470e6f210 --- /dev/null +++ b/utils/yang-utils/src/main/java/org/opendaylight/ovsdb/utils/yang/YangUtils.java @@ -0,0 +1,75 @@ +/* + * Copyright © 2016 Red Hat, Inc. and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.ovsdb.utils.yang; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import com.google.common.base.Preconditions; + +/** + * YANG utility functions. + */ +public final class YangUtils { + /** + * Prevent instantiation. + */ + private YangUtils() { + // Nothing to do + } + + /** + * Copies a list of YANG key-value items to the given map. Any {@code null} key or value will cause an error. + * + * @param map The map to fill. + * @param yangList The list of YANG key-value items. + * @param keyExtractor The key extractor function to use. + * @param valueExtractor The value extractor function to use. + * @param The YANG item type. + * @param The key type. + * @param The value type. + * @return The map. + */ + @Nonnull + public static Map copyYangKeyValueListToMap(@Nonnull Map map, @Nullable Iterable yangList, + @Nonnull Function keyExtractor, + @Nonnull Function valueExtractor) { + if (yangList != null) { + for (T yangValue : yangList) { + K key = keyExtractor.apply(yangValue); + V value = valueExtractor.apply(yangValue); + Preconditions.checkNotNull(key); + Preconditions.checkNotNull(value); + map.put(key, value); + } + } + return map; + } + + /** + * Converts a list of YANG key-value items to a map. + * + * @param yangList The list of YANG key-value items. + * @param keyExtractor The key extractor function to use. + * @param valueExtractor The value extractor function to use. + * @param The YANG item type. + * @param The key type. + * @param The value type. + * @return The map. + */ + @Nonnull + public static Map convertYangKeyValueListToMap(@Nullable Iterable yangList, + @Nonnull Function keyExtractor, + @Nonnull Function valueExtractor) { + return copyYangKeyValueListToMap(new HashMap<>(), yangList, keyExtractor, valueExtractor); + } +} -- 2.36.6