Use a utility function for key-value to map conversions
authorStephen Kitt <skitt@redhat.com>
Thu, 25 Feb 2016 15:57:26 +0000 (16:57 +0100)
committerStephen Kitt <skitt@redhat.com>
Wed, 9 Mar 2016 09:23:25 +0000 (10:23 +0100)
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 <skitt@redhat.com>
utils/pom.xml
utils/yang-utils/pom.xml [new file with mode: 0644]
utils/yang-utils/src/main/java/org/opendaylight/ovsdb/utils/yang/YangUtils.java [new file with mode: 0644]

index 90ac03ae9e867cb9560d23ac4ddc495b46c59309..ce6acfb639c57d4a44cf088b366ffc2bd4eefd14 100644 (file)
@@ -57,6 +57,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
     <module>servicehelper</module>
     <module>southbound-utils</module>
     <module>hwvtepsouthbound-utils</module>
+    <module>yang-utils</module>
   </modules>
 
 <!-- DO NOT install or deploy the repo root pom as it's only needed to initiate a build -->
diff --git a/utils/yang-utils/pom.xml b/utils/yang-utils/pom.xml
new file mode 100644 (file)
index 0000000..b8a502d
--- /dev/null
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.opendaylight.odlparent</groupId>
+    <artifactId>bundle-parent</artifactId>
+    <version>1.7.0-SNAPSHOT</version>
+    <relativePath/>
+  </parent>
+
+  <groupId>org.opendaylight.ovsdb</groupId>
+  <artifactId>utils.yang-utils</artifactId>
+  <version>1.3.0-SNAPSHOT</version>
+  <packaging>bundle</packaging>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.google.guava</groupId>
+      <artifactId>guava</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>com.google.code.findbugs</groupId>
+      <artifactId>jsr305</artifactId>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <Export-Package>
+              org.opendaylight.ovsdb.utils.yang
+            </Export-Package>
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
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 (file)
index 0000000..6470e6f
--- /dev/null
@@ -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 <T> The YANG item type.
+     * @param <K> The key type.
+     * @param <V> The value type.
+     * @return The map.
+     */
+    @Nonnull
+    public static <T, K, V> Map<K, V> copyYangKeyValueListToMap(@Nonnull Map<K, V> map, @Nullable Iterable<T> yangList,
+                                                                @Nonnull Function<T, K> keyExtractor,
+                                                                @Nonnull Function<T, V> 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 <T> The YANG item type.
+     * @param <K> The key type.
+     * @param <V> The value type.
+     * @return The map.
+     */
+    @Nonnull
+    public static <T, K, V> Map<K, V> convertYangKeyValueListToMap(@Nullable Iterable<T> yangList,
+                                                                   @Nonnull Function<T, K> keyExtractor,
+                                                                   @Nonnull Function<T, V> valueExtractor) {
+        return copyYangKeyValueListToMap(new HashMap<>(), yangList, keyExtractor, valueExtractor);
+    }
+}