Use a utility function for key-value to map conversions 09/35409/2
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>
12 files changed:
southbound/southbound-features/src/main/features/features.xml
southbound/southbound-impl/pom.xml
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/OvsdbConnectionInstance.java
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/BridgeUpdateCommand.java
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/OvsdbNodeUpdateCommand.java
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/QosUpdateCommand.java
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/QueueUpdateCommand.java
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointCreateCommand.java
southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointUpdateCommand.java
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 0fbba27f2d06675e04500b8b70751d4d691384cb..d7b8c7dd0ed8ebd9353c9547ab9a5181dda43384 100644 (file)
@@ -24,6 +24,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
     <feature version='${project.version}'>odl-ovsdb-southbound-api</feature>
     <bundle>mvn:org.opendaylight.ovsdb/southbound-impl/{{VERSION}}</bundle>
     <bundle>mvn:org.opendaylight.ovsdb/library/{{VERSION}}</bundle>
+    <bundle>mvn:org.opendaylight.ovsdb/utils.yang-utils/{{VERSION}}</bundle>
     <bundle>mvn:com.fasterxml.jackson.core/jackson-annotations/{{VERSION}}</bundle>
     <bundle>mvn:com.fasterxml.jackson.core/jackson-core/{{VERSION}}</bundle>
     <bundle>mvn:com.fasterxml.jackson.core/jackson-databind/{{VERSION}}</bundle>
index ac63bf2c1cb4c6580adac1b3d3aaa357f39364c3..062bf28e41655e6f3cb694d6fc7d1abbbc316ddd 100644 (file)
@@ -63,6 +63,11 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <artifactId>schema.openvswitch</artifactId>
       <version>${project.version}</version>
     </dependency>
+    <dependency>
+      <groupId>org.opendaylight.ovsdb</groupId>
+      <artifactId>utils.yang-utils</artifactId>
+      <version>${project.version}</version>
+    </dependency>
     <dependency>
       <groupId>com.google.guava</groupId>
       <artifactId>guava</artifactId>
index 33184082ca1860ea717c42682a2eaea425fd3d4c..d2565bb41d0a37fb28797e89d9dfe0ffba69a9a5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015, 2016 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -48,6 +48,7 @@ import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactInvoker;
 import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactInvokerImpl;
 import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactUtils;
 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
+import org.opendaylight.ovsdb.utils.yang.YangUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
@@ -59,7 +60,6 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Lists;
 import com.google.common.util.concurrent.ListenableFuture;
 
@@ -160,20 +160,15 @@ public class OvsdbConnectionInstance implements OvsdbClient {
 
             // OpenVSwitchPart
             OpenVSwitch ovs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), OpenVSwitch.class);
-            Map<String, String> externalIdsMap = new HashMap<>();
 
             List<OpenvswitchExternalIds> externalIds = this.initialCreateData.getOpenvswitchExternalIds();
 
-            if (externalIds != null) {
-                for (OpenvswitchExternalIds externalId : externalIds) {
-                    externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
-                }
-            }
-
             stampInstanceIdentifier(transaction,this.instanceIdentifier.firstIdentifierOf(Node.class));
 
             try {
-                ovs.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
+                ovs.setExternalIds(
+                        YangUtils.convertYangKeyValueListToMap(externalIds, OpenvswitchExternalIds::getExternalIdKey,
+                                OpenvswitchExternalIds::getExternalIdValue));
                 Mutate<GenericTableSchema> mutate = op.mutate(ovs)
                             .addMutation(ovs.getExternalIdsColumn().getSchema(),
                                 Mutator.INSERT,
@@ -187,12 +182,10 @@ public class OvsdbConnectionInstance implements OvsdbClient {
 
             List<OpenvswitchOtherConfigs> otherConfigs = this.initialCreateData.getOpenvswitchOtherConfigs();
             if (otherConfigs != null) {
-                Map<String, String> otherConfigsMap = new HashMap<>();
-                for (OpenvswitchOtherConfigs otherConfig : otherConfigs) {
-                    otherConfigsMap.put(otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
-                }
                 try {
-                    ovs.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
+                    ovs.setOtherConfig(YangUtils.convertYangKeyValueListToMap(otherConfigs,
+                            OpenvswitchOtherConfigs::getOtherConfigKey,
+                            OpenvswitchOtherConfigs::getOtherConfigValue));
                     transaction.add(op.mutate(ovs).addMutation(ovs.getOtherConfigColumn().getSchema(),
                         Mutator.INSERT,
                         ovs.getOtherConfigColumn().getData()));
index 6fe618438047d404c65d929e6f51db672c91f54f..96adcb39520dd76bc1f4fd32eb889dd044f865fe 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015, 2016 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -11,10 +11,11 @@ import static org.opendaylight.ovsdb.lib.operations.Operations.op;
 
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import javax.annotation.Nonnull;
+
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
 import org.opendaylight.ovsdb.lib.notation.UUID;
 import org.opendaylight.ovsdb.lib.operations.Insert;
@@ -28,6 +29,7 @@ import org.opendaylight.ovsdb.schema.openvswitch.Port;
 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
+import org.opendaylight.ovsdb.utils.yang.YangUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeInternal;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
@@ -39,7 +41,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Sets;
 
 public class BridgeUpdateCommand extends AbstractTransactCommand {
@@ -127,33 +128,23 @@ public class BridgeUpdateCommand extends AbstractTransactCommand {
         Map<String, String> externalIdMap = new HashMap<>();
         externalIdMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY, SouthboundUtil.serializeInstanceIdentifier(iid));
         // Set user provided external ids
-        List<BridgeExternalIds> bridgeExternalId = ovsdbManagedNode.getBridgeExternalIds();
-        if (bridgeExternalId != null) {
-            for (BridgeExternalIds externalId : bridgeExternalId) {
-                externalIdMap.put(externalId.getBridgeExternalIdKey(), externalId.getBridgeExternalIdValue());
-            }
-        }
         try {
-            bridge.setExternalIds(ImmutableMap.copyOf(externalIdMap));
+            YangUtils.copyYangKeyValueListToMap(externalIdMap, ovsdbManagedNode.getBridgeExternalIds(),
+                    BridgeExternalIds::getBridgeExternalIdKey, BridgeExternalIds::getBridgeExternalIdValue);
         } catch (NullPointerException e) {
             LOG.warn("Incomplete bridge external Id", e);
         }
+        bridge.setExternalIds(externalIdMap);
     }
 
 
 
-    private void setOpenDaylightOtherConfig(Bridge bridge, OvsdbBridgeAugmentation ovsdbManagedNode) {
-        List<BridgeOtherConfigs> bridgeOtherConfig = ovsdbManagedNode.getBridgeOtherConfigs();
-        if (bridgeOtherConfig != null) {
-            Map<String, String> otherConfigMap = new HashMap<>();
-            for (BridgeOtherConfigs otherConf : bridgeOtherConfig) {
-                otherConfigMap.put(otherConf.getBridgeOtherConfigKey(), otherConf.getBridgeOtherConfigValue());
-            }
-            try {
-                bridge.setOtherConfig(ImmutableMap.copyOf(otherConfigMap));
-            } catch (NullPointerException e) {
-                LOG.warn("Incomplete bridge other config", e);
-            }
+    private void setOpenDaylightOtherConfig(@Nonnull Bridge bridge, @Nonnull OvsdbBridgeAugmentation ovsdbManagedNode) {
+        try {
+            bridge.setOtherConfig(YangUtils.convertYangKeyValueListToMap(ovsdbManagedNode.getBridgeOtherConfigs(),
+                    BridgeOtherConfigs::getBridgeOtherConfigKey, BridgeOtherConfigs::getBridgeOtherConfigValue));
+        } catch (NullPointerException e) {
+            LOG.warn("Incomplete bridge other config", e);
         }
     }
 
index a36060cf246ad9b04328d9b78a8507482d5b6be6..f675b6b4462f31638e362c3f34d0eda20b897113 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Intel Corporation and others.  All rights reserved.
+ * Copyright (c) 2015, 2016 Intel Corporation and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -10,7 +10,6 @@ package org.opendaylight.ovsdb.southbound.ovsdb.transact;
 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
 
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -22,6 +21,7 @@ import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
+import org.opendaylight.ovsdb.utils.yang.YangUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
@@ -31,8 +31,6 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.ImmutableMap;
-
 public class OvsdbNodeUpdateCommand implements TransactCommand {
     private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes;
     private static final Logger LOG = LoggerFactory.getLogger(OvsdbNodeUpdateCommand.class);
@@ -55,20 +53,12 @@ public class OvsdbNodeUpdateCommand implements TransactCommand {
 
             // OpenVSwitchPart
             OpenVSwitch ovs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), OpenVSwitch.class);
-            Map<String, String> externalIdsMap = new HashMap<>();
-
-            List<OpenvswitchExternalIds> externalIds = ovsdbNode.getOpenvswitchExternalIds();
-
-            if (externalIds != null) {
-                for (OpenvswitchExternalIds externalId : externalIds) {
-                    externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
-                }
-            }
 
             stampInstanceIdentifier(transaction,ovsdbNodeEntry.getKey().firstIdentifierOf(Node.class));
 
             try {
-                ovs.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
+                ovs.setExternalIds(YangUtils.convertYangKeyValueListToMap(ovsdbNode.getOpenvswitchExternalIds(),
+                        OpenvswitchExternalIds::getExternalIdKey, OpenvswitchExternalIds::getExternalIdValue));
                 Mutate<GenericTableSchema> mutate = op.mutate(ovs)
                             .addMutation(ovs.getExternalIdsColumn().getSchema(),
                                 Mutator.INSERT,
@@ -82,12 +72,9 @@ public class OvsdbNodeUpdateCommand implements TransactCommand {
 
             List<OpenvswitchOtherConfigs> otherConfigs = ovsdbNode.getOpenvswitchOtherConfigs();
             if (otherConfigs != null) {
-                Map<String, String> otherConfigsMap = new HashMap<>();
-                for (OpenvswitchOtherConfigs otherConfig : otherConfigs) {
-                    otherConfigsMap.put(otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
-                }
                 try {
-                    ovs.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
+                    ovs.setOtherConfig(YangUtils.convertYangKeyValueListToMap(otherConfigs,
+                            OpenvswitchOtherConfigs::getOtherConfigKey, OpenvswitchOtherConfigs::getOtherConfigValue));
                     transaction.add(op.mutate(ovs).addMutation(ovs.getOtherConfigColumn().getSchema(),
                         Mutator.INSERT,
                         ovs.getOtherConfigColumn().getData()));
index 9f895600a1dd0b8ae943a2a95bf61d8e0261d32b..8cf3c77d9092d2760c9f5bdda1ad691d8912c8a3 100644 (file)
@@ -10,27 +10,21 @@ package org.opendaylight.ovsdb.southbound.ovsdb.transact;
 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
 
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
-import org.opendaylight.ovsdb.lib.notation.Condition;
-import org.opendaylight.ovsdb.lib.notation.Mutator;
 import org.opendaylight.ovsdb.lib.notation.UUID;
-import org.opendaylight.ovsdb.lib.operations.Mutate;
 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
-import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
-import org.opendaylight.ovsdb.schema.openvswitch.Port;
 import org.opendaylight.ovsdb.schema.openvswitch.Qos;
 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
+import org.opendaylight.ovsdb.utils.yang.YangUtils;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosExternalIds;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.qos.entries.QosOtherConfig;
@@ -40,8 +34,6 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.ImmutableMap;
-
 public class QosUpdateCommand extends AbstractTransactCommand {
     private static final Logger LOG = LoggerFactory.getLogger(QosUpdateCommand.class);
 
@@ -101,31 +93,21 @@ public class QosUpdateCommand extends AbstractTransactCommand {
                 }
                 qos.setQueues(newQueueList);
 
-                List<QosExternalIds> externalIds = qosEntry.getQosExternalIds();
                 Map<String, String> externalIdsMap = new HashMap<>();
-                if (externalIds != null) {
-                    for (QosExternalIds externalId : externalIds) {
-                        externalIdsMap.put(externalId.getQosExternalIdKey(), externalId.getQosExternalIdValue());
-                    }
-                }
-                externalIdsMap.put(SouthboundConstants.QOS_ID_EXTERNAL_ID_KEY, qosEntry.getQosId().getValue());
                 try {
-                    qos.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
+                    YangUtils.copyYangKeyValueListToMap(externalIdsMap, qosEntry.getQosExternalIds(),
+                            QosExternalIds::getQosExternalIdKey, QosExternalIds::getQosExternalIdValue);
                 } catch (NullPointerException e) {
                     LOG.warn("Incomplete Qos external IDs", e);
                 }
+                externalIdsMap.put(SouthboundConstants.QOS_ID_EXTERNAL_ID_KEY, qosEntry.getQosId().getValue());
+                qos.setExternalIds(externalIdsMap);
 
-                List<QosOtherConfig> otherConfigs = qosEntry.getQosOtherConfig();
-                if (otherConfigs != null) {
-                    Map<String, String> otherConfigsMap = new HashMap<>();
-                    for (QosOtherConfig otherConfig : otherConfigs) {
-                        otherConfigsMap.put(otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
-                    }
-                    try {
-                        qos.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
-                    } catch (NullPointerException e) {
-                        LOG.warn("Incomplete Qos other_config", e);
-                    }
+                try {
+                    qos.setOtherConfig(YangUtils.convertYangKeyValueListToMap(qosEntry.getQosOtherConfig(),
+                            QosOtherConfig::getOtherConfigKey, QosOtherConfig::getOtherConfigValue));
+                } catch (NullPointerException e) {
+                    LOG.warn("Incomplete Qos other_config", e);
                 }
                 if (uuid == null) {
                     transaction.add(op.insert(qos)).build();
index 1b991bbf16165c59b97130cec9fc8a9411c6c30c..cd6d768555ea1e925d1fd847ecb2c6f1cc170126 100644 (file)
@@ -17,20 +17,15 @@ import java.util.Map.Entry;
 import java.util.Set;
 
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
-import org.opendaylight.ovsdb.lib.notation.Mutator;
 import org.opendaylight.ovsdb.lib.notation.UUID;
-import org.opendaylight.ovsdb.lib.operations.Mutate;
 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
-import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
-import org.opendaylight.ovsdb.schema.openvswitch.Qos;
 import org.opendaylight.ovsdb.schema.openvswitch.Queue;
 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
-import org.opendaylight.ovsdb.southbound.SouthboundMapper;
+import org.opendaylight.ovsdb.utils.yang.YangUtils;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesExternalIds;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfig;
@@ -39,8 +34,6 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.ImmutableMap;
-
 public class QueueUpdateCommand extends AbstractTransactCommand {
     private static final Logger LOG = LoggerFactory.getLogger(QueueUpdateCommand.class);
 
@@ -98,31 +91,21 @@ public class QueueUpdateCommand extends AbstractTransactCommand {
                     uuid = new UUID(queueUuid.getValue());
                 }
 
-                List<QueuesExternalIds> externalIds = queueEntry.getQueuesExternalIds();
                 Map<String, String> externalIdsMap = new HashMap<>();
-                if (externalIds != null) {
-                    for (QueuesExternalIds externalId : externalIds) {
-                        externalIdsMap.put(externalId.getQueuesExternalIdKey(), externalId.getQueuesExternalIdValue());
-                    }
-                }
-                externalIdsMap.put(SouthboundConstants.QUEUE_ID_EXTERNAL_ID_KEY, queueEntry.getQueueId().getValue());
                 try {
-                    queue.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
+                    YangUtils.copyYangKeyValueListToMap(externalIdsMap, queueEntry.getQueuesExternalIds(),
+                            QueuesExternalIds::getQueuesExternalIdKey, QueuesExternalIds::getQueuesExternalIdValue);
                 } catch (NullPointerException e) {
                     LOG.warn("Incomplete Queue external IDs", e);
                 }
+                externalIdsMap.put(SouthboundConstants.QUEUE_ID_EXTERNAL_ID_KEY, queueEntry.getQueueId().getValue());
+                queue.setExternalIds(externalIdsMap);
 
-                List<QueuesOtherConfig> otherConfigs = queueEntry.getQueuesOtherConfig();
-                if (otherConfigs != null) {
-                    Map<String, String> otherConfigsMap = new HashMap<>();
-                    for (QueuesOtherConfig otherConfig : otherConfigs) {
-                        otherConfigsMap.put(otherConfig.getQueueOtherConfigKey(), otherConfig.getQueueOtherConfigValue());
-                    }
-                    try {
-                        queue.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
-                    } catch (NullPointerException e) {
-                        LOG.warn("Incomplete Queue other_config", e);
-                    }
+                try {
+                    queue.setOtherConfig(YangUtils.convertYangKeyValueListToMap(queueEntry.getQueuesOtherConfig(),
+                            QueuesOtherConfig::getQueueOtherConfigKey, QueuesOtherConfig::getQueueOtherConfigValue));
+                } catch (NullPointerException e) {
+                    LOG.warn("Incomplete Queue other_config", e);
                 }
                 if (uuid == null) {
                     transaction.add(op.insert(queue)).build();
index ce3a1464376fac82265bd62b89080aa8cf4efd4b..d6eba9c642666a1c094562adee0399984dbd106e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015, 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -34,6 +34,7 @@ import org.opendaylight.ovsdb.schema.openvswitch.Port;
 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
 import org.opendaylight.ovsdb.southbound.SouthboundProvider;
+import org.opendaylight.ovsdb.utils.yang.YangUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
@@ -53,7 +54,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.CheckedFuture;
 
@@ -174,12 +174,9 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
 
         //Configure optional input
         if (terminationPoint.getOptions() != null) {
-            Map<String, String> optionsMap = new HashMap<>();
-            for (Options option : terminationPoint.getOptions()) {
-                optionsMap.put(option.getOption(), option.getValue());
-            }
             try {
-                ovsInterface.setOptions(ImmutableMap.copyOf(optionsMap));
+                ovsInterface.setOptions(YangUtils.convertYangKeyValueListToMap(terminationPoint.getOptions(),
+                        Options::getOption, Options::getValue));
             } catch (NullPointerException e) {
                 LOG.warn("Incomplete OVSDB interface options", e);
             }
@@ -193,12 +190,9 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
         List<InterfaceExternalIds> interfaceExternalIds =
                 terminationPoint.getInterfaceExternalIds();
         if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
-            Map<String, String> externalIdsMap = new HashMap<>();
-            for (InterfaceExternalIds externalId: interfaceExternalIds) {
-                externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
-            }
             try {
-                ovsInterface.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
+                ovsInterface.setExternalIds(YangUtils.convertYangKeyValueListToMap(interfaceExternalIds,
+                        InterfaceExternalIds::getExternalIdKey, InterfaceExternalIds::getExternalIdValue));
             } catch (NullPointerException e) {
                 LOG.warn("Incomplete OVSDB interface external_ids", e);
             }
@@ -233,12 +227,9 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
             List<InterfaceLldp> interfaceLldpList =
                     terminationPoint.getInterfaceLldp();
             if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) {
-                Map<String, String> interfaceLldpMap = new HashMap<>();
-                for (InterfaceLldp interfaceLldp : interfaceLldpList) {
-                    interfaceLldpMap.put(interfaceLldp.getLldpKey(), interfaceLldp.getLldpValue());
-                }
                 try {
-                    ovsInterface.setLldp(ImmutableMap.copyOf(interfaceLldpMap));
+                    ovsInterface.setLldp(YangUtils.convertYangKeyValueListToMap(interfaceLldpList,
+                            InterfaceLldp::getLldpKey, InterfaceLldp::getLldpValue));
                 } catch (NullPointerException e) {
                     LOG.warn("Incomplete OVSDB interface lldp", e);
                 }
@@ -254,12 +245,9 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
 
         List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
         if (portExternalIds != null && !portExternalIds.isEmpty()) {
-            Map<String, String> externalIdsMap = new HashMap<>();
-            for (PortExternalIds externalId: portExternalIds) {
-                externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
-            }
             try {
-                port.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
+                port.setExternalIds(YangUtils.convertYangKeyValueListToMap(portExternalIds,
+                        PortExternalIds::getExternalIdKey, PortExternalIds::getExternalIdValue));
             } catch (NullPointerException e) {
                 LOG.warn("Incomplete OVSDB port external_ids", e);
             }
@@ -310,13 +298,9 @@ public class TerminationPointCreateCommand extends AbstractTransactCommand {
         List<PortOtherConfigs> portOtherConfigs =
                 terminationPoint.getPortOtherConfigs();
         if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
-            Map<String, String> otherConfigsMap = new HashMap<>();
-            for (PortOtherConfigs portOtherConfig : portOtherConfigs) {
-                otherConfigsMap.put(portOtherConfig.getOtherConfigKey(),
-                        portOtherConfig.getOtherConfigValue());
-            }
             try {
-                ovsPort.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
+                ovsPort.setOtherConfig(YangUtils.convertYangKeyValueListToMap(portOtherConfigs,
+                        PortOtherConfigs::getOtherConfigKey, PortOtherConfigs::getOtherConfigValue));
             } catch (NullPointerException e) {
                 LOG.warn("Incomplete OVSDB port other_config", e);
             }
index 159161905fd26f9bfa851df82d9df96fced078ba..a7c788db57795b771f8fb245f25bac6b7bea3cfe 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015, 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -24,6 +24,7 @@ import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
 import org.opendaylight.ovsdb.schema.openvswitch.Port;
 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
+import org.opendaylight.ovsdb.utils.yang.YangUtils;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
@@ -40,7 +41,6 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Sets;
 
 public class TerminationPointUpdateCommand extends AbstractTransactCommand {
@@ -166,12 +166,9 @@ public class TerminationPointUpdateCommand extends AbstractTransactCommand {
 
         //Configure optional input
         if (terminationPoint.getOptions() != null) {
-            Map<String, String> optionsMap = new HashMap<>();
-            for (Options option : terminationPoint.getOptions()) {
-                optionsMap.put(option.getOption(), option.getValue());
-            }
             try {
-                ovsInterface.setOptions(ImmutableMap.copyOf(optionsMap));
+                ovsInterface.setOptions(YangUtils.convertYangKeyValueListToMap(terminationPoint.getOptions(),
+                        Options::getOption, Options::getValue));
             } catch (NullPointerException e) {
                 LOG.warn("Incomplete OVSDB interface options", e);
             }
@@ -185,12 +182,9 @@ public class TerminationPointUpdateCommand extends AbstractTransactCommand {
         List<InterfaceExternalIds> interfaceExternalIds =
                 terminationPoint.getInterfaceExternalIds();
         if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
-            Map<String, String> externalIdsMap = new HashMap<>();
-            for (InterfaceExternalIds externalId: interfaceExternalIds) {
-                externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
-            }
             try {
-                ovsInterface.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
+                ovsInterface.setExternalIds(YangUtils.convertYangKeyValueListToMap(interfaceExternalIds,
+                        InterfaceExternalIds::getExternalIdKey, InterfaceExternalIds::getExternalIdValue));
             } catch (NullPointerException e) {
                 LOG.warn("Incomplete OVSDB interface external_ids", e);
             }
@@ -205,12 +199,9 @@ public class TerminationPointUpdateCommand extends AbstractTransactCommand {
             List<InterfaceLldp> interfaceLldpList =
                     terminationPoint.getInterfaceLldp();
             if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) {
-                Map<String, String> interfaceLldpMap = new HashMap<>();
-                for (InterfaceLldp interfaceLldp : interfaceLldpList) {
-                    interfaceLldpMap.put(interfaceLldp.getLldpKey(), interfaceLldp.getLldpValue());
-                }
                 try {
-                    ovsInterface.setLldp(ImmutableMap.copyOf(interfaceLldpMap));
+                    ovsInterface.setLldp(YangUtils.convertYangKeyValueListToMap(interfaceLldpList,
+                            InterfaceLldp::getLldpKey, InterfaceLldp::getLldpValue));
                 } catch (NullPointerException e) {
                     LOG.warn("Incomplete OVSDB interface lldp", e);
                 }
@@ -246,12 +237,9 @@ public class TerminationPointUpdateCommand extends AbstractTransactCommand {
 
         List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
         if (portExternalIds != null && !portExternalIds.isEmpty()) {
-            Map<String, String> externalIdsMap = new HashMap<>();
-            for (PortExternalIds externalId: portExternalIds) {
-                externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
-            }
             try {
-                port.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
+                port.setExternalIds(YangUtils.convertYangKeyValueListToMap(portExternalIds,
+                        PortExternalIds::getExternalIdKey, PortExternalIds::getExternalIdValue));
             } catch (NullPointerException e) {
                 LOG.warn("Incomplete OVSDB port external_ids", e);
             }
@@ -302,13 +290,9 @@ public class TerminationPointUpdateCommand extends AbstractTransactCommand {
         List<PortOtherConfigs> portOtherConfigs =
                 terminationPoint.getPortOtherConfigs();
         if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
-            Map<String, String> otherConfigsMap = new HashMap<>();
-            for (PortOtherConfigs portOtherConfig : portOtherConfigs) {
-                otherConfigsMap.put(portOtherConfig.getOtherConfigKey(),
-                        portOtherConfig.getOtherConfigValue());
-            }
             try {
-                ovsPort.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
+                ovsPort.setOtherConfig(YangUtils.convertYangKeyValueListToMap(portOtherConfigs,
+                        PortOtherConfigs::getOtherConfigKey, PortOtherConfigs::getOtherConfigValue));
             } catch (NullPointerException e) {
                 LOG.warn("Incomplete OVSDB port other_config", e);
             }
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);
+    }
+}