From 316d5824042bcb4b86937979d1b7f24149925455 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 --- .../src/main/features/features.xml | 1 + southbound/southbound-impl/pom.xml | 5 ++ .../southbound/OvsdbConnectionInstance.java | 23 ++---- .../ovsdb/transact/BridgeUpdateCommand.java | 35 ++++----- .../transact/OvsdbNodeUpdateCommand.java | 25 ++----- .../ovsdb/transact/QosUpdateCommand.java | 38 +++------- .../ovsdb/transact/QueueUpdateCommand.java | 37 +++------ .../TerminationPointCreateCommand.java | 40 +++------- .../TerminationPointUpdateCommand.java | 40 +++------- utils/pom.xml | 1 + utils/yang-utils/pom.xml | 50 +++++++++++++ .../ovsdb/utils/yang/YangUtils.java | 75 +++++++++++++++++++ 12 files changed, 203 insertions(+), 167 deletions(-) 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/southbound/southbound-features/src/main/features/features.xml b/southbound/southbound-features/src/main/features/features.xml index 0fbba27f2..d7b8c7dd0 100644 --- a/southbound/southbound-features/src/main/features/features.xml +++ b/southbound/southbound-features/src/main/features/features.xml @@ -24,6 +24,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html odl-ovsdb-southbound-api mvn:org.opendaylight.ovsdb/southbound-impl/{{VERSION}} mvn:org.opendaylight.ovsdb/library/{{VERSION}} + mvn:org.opendaylight.ovsdb/utils.yang-utils/{{VERSION}} mvn:com.fasterxml.jackson.core/jackson-annotations/{{VERSION}} mvn:com.fasterxml.jackson.core/jackson-core/{{VERSION}} mvn:com.fasterxml.jackson.core/jackson-databind/{{VERSION}} diff --git a/southbound/southbound-impl/pom.xml b/southbound/southbound-impl/pom.xml index ac63bf2c1..062bf28e4 100644 --- a/southbound/southbound-impl/pom.xml +++ b/southbound/southbound-impl/pom.xml @@ -63,6 +63,11 @@ and is available at http://www.eclipse.org/legal/epl-v10.html schema.openvswitch ${project.version} + + org.opendaylight.ovsdb + utils.yang-utils + ${project.version} + com.google.guava guava diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/OvsdbConnectionInstance.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/OvsdbConnectionInstance.java index 33184082c..d2565bb41 100644 --- a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/OvsdbConnectionInstance.java +++ b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/OvsdbConnectionInstance.java @@ -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 externalIdsMap = new HashMap<>(); List 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 mutate = op.mutate(ovs) .addMutation(ovs.getExternalIdsColumn().getSchema(), Mutator.INSERT, @@ -187,12 +182,10 @@ public class OvsdbConnectionInstance implements OvsdbClient { List otherConfigs = this.initialCreateData.getOpenvswitchOtherConfigs(); if (otherConfigs != null) { - Map 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())); diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/BridgeUpdateCommand.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/BridgeUpdateCommand.java index 6fe618438..96adcb395 100644 --- a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/BridgeUpdateCommand.java +++ b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/BridgeUpdateCommand.java @@ -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 externalIdMap = new HashMap<>(); externalIdMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY, SouthboundUtil.serializeInstanceIdentifier(iid)); // Set user provided external ids - List 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 bridgeOtherConfig = ovsdbManagedNode.getBridgeOtherConfigs(); - if (bridgeOtherConfig != null) { - Map 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); } } diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/OvsdbNodeUpdateCommand.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/OvsdbNodeUpdateCommand.java index a36060cf2..f675b6b44 100644 --- a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/OvsdbNodeUpdateCommand.java +++ b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/OvsdbNodeUpdateCommand.java @@ -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, 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 externalIdsMap = new HashMap<>(); - - List 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 mutate = op.mutate(ovs) .addMutation(ovs.getExternalIdsColumn().getSchema(), Mutator.INSERT, @@ -82,12 +72,9 @@ public class OvsdbNodeUpdateCommand implements TransactCommand { List otherConfigs = ovsdbNode.getOpenvswitchOtherConfigs(); if (otherConfigs != null) { - Map 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())); diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/QosUpdateCommand.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/QosUpdateCommand.java index 9f895600a..8cf3c77d9 100644 --- a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/QosUpdateCommand.java +++ b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/QosUpdateCommand.java @@ -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 externalIds = qosEntry.getQosExternalIds(); Map 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 otherConfigs = qosEntry.getQosOtherConfig(); - if (otherConfigs != null) { - Map 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(); diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/QueueUpdateCommand.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/QueueUpdateCommand.java index 1b991bbf1..cd6d76855 100644 --- a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/QueueUpdateCommand.java +++ b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/QueueUpdateCommand.java @@ -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 externalIds = queueEntry.getQueuesExternalIds(); Map 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 otherConfigs = queueEntry.getQueuesOtherConfig(); - if (otherConfigs != null) { - Map 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(); diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointCreateCommand.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointCreateCommand.java index ce3a14643..d6eba9c64 100644 --- a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointCreateCommand.java +++ b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointCreateCommand.java @@ -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 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 = terminationPoint.getInterfaceExternalIds(); if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) { - Map 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 interfaceLldpList = terminationPoint.getInterfaceLldp(); if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) { - Map 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 = terminationPoint.getPortExternalIds(); if (portExternalIds != null && !portExternalIds.isEmpty()) { - Map 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 = terminationPoint.getPortOtherConfigs(); if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) { - Map 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); } diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointUpdateCommand.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointUpdateCommand.java index 159161905..a7c788db5 100644 --- a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointUpdateCommand.java +++ b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TerminationPointUpdateCommand.java @@ -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 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 = terminationPoint.getInterfaceExternalIds(); if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) { - Map 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 interfaceLldpList = terminationPoint.getInterfaceLldp(); if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) { - Map 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 = terminationPoint.getPortExternalIds(); if (portExternalIds != null && !portExternalIds.isEmpty()) { - Map 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 = terminationPoint.getPortOtherConfigs(); if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) { - Map 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); } diff --git a/utils/pom.xml b/utils/pom.xml index 90ac03ae9..ce6acfb63 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 000000000..b8a502d5c --- /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 000000000..6470e6f21 --- /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