From d93c147816b2bc79423eaef57c0bab5f2dd2c0da Mon Sep 17 00:00:00 2001 From: Mohamed El-Serngawy Date: Tue, 23 Feb 2016 16:15:29 -0500 Subject: [PATCH] manually cherry pick this patch https://git.opendaylight.org/gerrit/#/c/34579/ to stable/beryllium Change-Id: Ib57d37da4e7705a1d931cdd564883ec0024d14c7 Signed-off-by: Mohamed El-Serngawy --- .../unimgr/api/AbstractCommand.java | 35 ++ .../unimgr/api/IUnimgrDataChangeListener.java | 25 -- .../api/UnimgrDataTreeChangeListener.java | 71 ++++ .../unimgr/command/AbstractCreateCommand.java | 24 -- .../unimgr/command/AbstractDeleteCommand.java | 23 -- .../unimgr/command/AbstractUpdateCommand.java | 24 -- .../opendaylight/unimgr/command/Command.java | 14 - ...cCreateCommand.java => EvcAddCommand.java} | 68 ++-- .../unimgr/command/EvcDeleteCommand.java | 75 ---- .../unimgr/command/EvcRemoveCommand.java | 74 ++++ .../unimgr/command/EvcUpdateCommand.java | 350 +++++++++--------- .../unimgr/command/OvsNodeAddCommand.java | 110 ++++++ .../unimgr/command/TransactionInvoker.java | 36 -- .../unimgr/command/UniAddCommand.java | 124 +++++++ .../unimgr/command/UniCreateCommand.java | 209 ----------- .../unimgr/command/UniDeleteCommand.java | 106 ------ .../unimgr/command/UniRemoveCommand.java | 89 +++++ .../unimgr/command/UniUpdateCommand.java | 141 +++---- .../impl/EvcDataTreeChangeListener.java | 79 ++++ .../impl/OvsNodeDataTreeChangeListener.java | 72 ++++ .../impl/UniDataTreeChangeListener.java | 83 +++++ .../unimgr/impl/UnimgrDataChangeListener.java | 121 ------ .../unimgr/impl/UnimgrProvider.java | 127 +++---- .../opendaylight/unimgr/utils/EvcUtils.java | 38 +- .../opendaylight/unimgr/utils/OvsdbUtils.java | 12 +- .../command/DataTreeModificationHelper.java | 194 ++++++++++ ...ommandTest.java => EvcAddCommandTest.java} | 43 +-- ...andTest.java => EvcRemoveCommandTest.java} | 33 +- .../unimgr/command/EvcUpdateCommandTest.java | 46 +-- ...ommandTest.java => UniAddCommandTest.java} | 86 ++--- ...andTest.java => UniRemoveCommandTest.java} | 48 +-- .../unimgr/command/UniUpdateCommandTest.java | 73 +--- .../impl/EvcDataTreeChangeListenerTest.java | 129 +++++++ .../impl/UniDataTreeChangeListenerTest.java | 129 +++++++ .../impl/UnimgrDataChangeListenerTest.java | 181 --------- .../unimgr/impl/UnimgrProviderTest.java | 232 ++++++------ .../org/opendaylight/unimgr/it/UnimgrIT.java | 111 +++--- 37 files changed, 1802 insertions(+), 1633 deletions(-) create mode 100644 impl/src/main/java/org/opendaylight/unimgr/api/AbstractCommand.java delete mode 100755 impl/src/main/java/org/opendaylight/unimgr/api/IUnimgrDataChangeListener.java create mode 100644 impl/src/main/java/org/opendaylight/unimgr/api/UnimgrDataTreeChangeListener.java delete mode 100644 impl/src/main/java/org/opendaylight/unimgr/command/AbstractCreateCommand.java delete mode 100644 impl/src/main/java/org/opendaylight/unimgr/command/AbstractDeleteCommand.java delete mode 100644 impl/src/main/java/org/opendaylight/unimgr/command/AbstractUpdateCommand.java delete mode 100644 impl/src/main/java/org/opendaylight/unimgr/command/Command.java rename impl/src/main/java/org/opendaylight/unimgr/command/{EvcCreateCommand.java => EvcAddCommand.java} (80%) delete mode 100644 impl/src/main/java/org/opendaylight/unimgr/command/EvcDeleteCommand.java create mode 100644 impl/src/main/java/org/opendaylight/unimgr/command/EvcRemoveCommand.java create mode 100644 impl/src/main/java/org/opendaylight/unimgr/command/OvsNodeAddCommand.java delete mode 100644 impl/src/main/java/org/opendaylight/unimgr/command/TransactionInvoker.java create mode 100644 impl/src/main/java/org/opendaylight/unimgr/command/UniAddCommand.java delete mode 100644 impl/src/main/java/org/opendaylight/unimgr/command/UniCreateCommand.java delete mode 100644 impl/src/main/java/org/opendaylight/unimgr/command/UniDeleteCommand.java create mode 100644 impl/src/main/java/org/opendaylight/unimgr/command/UniRemoveCommand.java create mode 100644 impl/src/main/java/org/opendaylight/unimgr/impl/EvcDataTreeChangeListener.java create mode 100644 impl/src/main/java/org/opendaylight/unimgr/impl/OvsNodeDataTreeChangeListener.java create mode 100644 impl/src/main/java/org/opendaylight/unimgr/impl/UniDataTreeChangeListener.java delete mode 100644 impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrDataChangeListener.java create mode 100644 impl/src/test/java/org/opendaylight/unimgr/command/DataTreeModificationHelper.java rename impl/src/test/java/org/opendaylight/unimgr/command/{EvcCreateCommandTest.java => EvcAddCommandTest.java} (85%) rename impl/src/test/java/org/opendaylight/unimgr/command/{EvcDeleteCommandTest.java => EvcRemoveCommandTest.java} (79%) rename impl/src/test/java/org/opendaylight/unimgr/command/{UniCreateCommandTest.java => UniAddCommandTest.java} (75%) rename impl/src/test/java/org/opendaylight/unimgr/command/{UniDeleteCommandTest.java => UniRemoveCommandTest.java} (78%) create mode 100644 impl/src/test/java/org/opendaylight/unimgr/impl/EvcDataTreeChangeListenerTest.java create mode 100644 impl/src/test/java/org/opendaylight/unimgr/impl/UniDataTreeChangeListenerTest.java delete mode 100644 impl/src/test/java/org/opendaylight/unimgr/impl/UnimgrDataChangeListenerTest.java diff --git a/impl/src/main/java/org/opendaylight/unimgr/api/AbstractCommand.java b/impl/src/main/java/org/opendaylight/unimgr/api/AbstractCommand.java new file mode 100644 index 00000000..0b40579b --- /dev/null +++ b/impl/src/main/java/org/opendaylight/unimgr/api/AbstractCommand.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2016 CableLabs 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.unimgr.api; + +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.yangtools.yang.binding.DataObject; + +public abstract class AbstractCommand { + + protected DataBroker dataBroker; + protected DataTreeModification dataObject; + + /** + * Abstract command basic constructor + * @param dataBroker + * @param dataObject + */ + public AbstractCommand(final DataBroker dataBroker, final DataTreeModification dataObject) { + this.dataBroker = dataBroker; + this.dataObject = dataObject; + } + + /** + * Abstract execute method + */ + public abstract void execute(); + +} diff --git a/impl/src/main/java/org/opendaylight/unimgr/api/IUnimgrDataChangeListener.java b/impl/src/main/java/org/opendaylight/unimgr/api/IUnimgrDataChangeListener.java deleted file mode 100755 index 50ca372e..00000000 --- a/impl/src/main/java/org/opendaylight/unimgr/api/IUnimgrDataChangeListener.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2015 CableLabs 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, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.unimgr.api; - -import java.util.Map; - -import org.opendaylight.controller.md.sal.binding.api.DataChangeListener; -import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public interface IUnimgrDataChangeListener extends DataChangeListener, - AutoCloseable { - - void create(Map, DataObject> changes); - - void update(Map, DataObject> changes); - - void delete(AsyncDataChangeEvent, DataObject> changes); -} diff --git a/impl/src/main/java/org/opendaylight/unimgr/api/UnimgrDataTreeChangeListener.java b/impl/src/main/java/org/opendaylight/unimgr/api/UnimgrDataTreeChangeListener.java new file mode 100644 index 00000000..d273e7c7 --- /dev/null +++ b/impl/src/main/java/org/opendaylight/unimgr/api/UnimgrDataTreeChangeListener.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2016 CableLabs 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.unimgr.api; + +import java.util.Collection; + +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataObjectModification; +import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link; +import org.opendaylight.yangtools.yang.binding.DataObject; + +/** + * abstract class for unimgr data tree changes. + * @author mohamed el-serngawy + * @param extended data object + */ +public abstract class UnimgrDataTreeChangeListener implements DataTreeChangeListener, AutoCloseable { + + protected DataBroker dataBroker; + + public UnimgrDataTreeChangeListener(final DataBroker dataBroker) { + this.dataBroker = dataBroker; + } + + /** + * Basic Implementation of DataTreeChange Listener to execute add, update or remove command + * based on the data object modification type. + */ + @Override + public void onDataTreeChanged(Collection> collection) { + for (final DataTreeModification change : collection) { + final DataObjectModification root = change.getRootNode(); + switch (root.getModificationType()) { + case SUBTREE_MODIFIED: + update(change); + break; + case WRITE: + add(change); + break; + case DELETE: + remove(change); + break; + } + } + } + + /** + * method should implements the added data object command + * @param newDataObject + */ + public abstract void add(DataTreeModification newDataObject); + + /** + * method should implements the removed data object command + * @param removedDataObject + */ + public abstract void remove(DataTreeModification removedDataObject); + + /** + * method should implements the updated data object command + * @param modifiedDataObject + */ + public abstract void update(DataTreeModification modifiedDataObject); +} diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/AbstractCreateCommand.java b/impl/src/main/java/org/opendaylight/unimgr/command/AbstractCreateCommand.java deleted file mode 100644 index 50ec65e1..00000000 --- a/impl/src/main/java/org/opendaylight/unimgr/command/AbstractCreateCommand.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2015 CableLabs 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, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.unimgr.command; - -import java.util.Map; - -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public abstract class AbstractCreateCommand implements Command { - - protected Map, DataObject> changes; - protected DataBroker dataBroker; - - @Override - public abstract void execute(); - -} diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/AbstractDeleteCommand.java b/impl/src/main/java/org/opendaylight/unimgr/command/AbstractDeleteCommand.java deleted file mode 100644 index 4b66a69e..00000000 --- a/impl/src/main/java/org/opendaylight/unimgr/command/AbstractDeleteCommand.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2015 CableLabs 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, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.unimgr.command; - -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public abstract class AbstractDeleteCommand implements Command { - - protected AsyncDataChangeEvent, DataObject> changes; - protected DataBroker dataBroker; - - @Override - public abstract void execute(); - -} diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/AbstractUpdateCommand.java b/impl/src/main/java/org/opendaylight/unimgr/command/AbstractUpdateCommand.java deleted file mode 100644 index 8dcb2849..00000000 --- a/impl/src/main/java/org/opendaylight/unimgr/command/AbstractUpdateCommand.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2015 CableLabs 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, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.unimgr.command; - -import java.util.Map; - -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public abstract class AbstractUpdateCommand implements Command { - - protected Map, DataObject> changes; - protected DataBroker dataBroker; - - @Override - public abstract void execute(); - -} diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/Command.java b/impl/src/main/java/org/opendaylight/unimgr/command/Command.java deleted file mode 100644 index 82ec728a..00000000 --- a/impl/src/main/java/org/opendaylight/unimgr/command/Command.java +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright (c) 2015 CableLabs 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, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.unimgr.command; - -public interface Command { - - public void execute(); - -} diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/EvcCreateCommand.java b/impl/src/main/java/org/opendaylight/unimgr/command/EvcAddCommand.java similarity index 80% rename from impl/src/main/java/org/opendaylight/unimgr/command/EvcCreateCommand.java rename to impl/src/main/java/org/opendaylight/unimgr/command/EvcAddCommand.java index 307a16d2..e84a408d 100644 --- a/impl/src/main/java/org/opendaylight/unimgr/command/EvcCreateCommand.java +++ b/impl/src/main/java/org/opendaylight/unimgr/command/EvcAddCommand.java @@ -1,17 +1,17 @@ /* - * Copyright (c) 2015 CableLabs and others. All rights reserved. + * Copyright (c) 2016 CableLabs 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, * and is available at http://www.eclipse.org/legal/epl-v10.html */ -package org.opendaylight.unimgr.command; -import java.util.Map; -import java.util.Map.Entry; +package org.opendaylight.unimgr.command; import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.unimgr.api.AbstractCommand; import org.opendaylight.unimgr.impl.UnimgrConstants; import org.opendaylight.unimgr.impl.UnimgrMapper; import org.opendaylight.unimgr.utils.EvcUtils; @@ -19,38 +19,38 @@ import org.opendaylight.unimgr.utils.MdsalUtils; import org.opendaylight.unimgr.utils.OvsdbUtils; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; -import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Optional; -public class EvcCreateCommand extends AbstractCreateCommand { +public class EvcAddCommand extends AbstractCommand { - private static final Logger LOG = LoggerFactory.getLogger(EvcCreateCommand.class); + private static final Logger LOG = LoggerFactory.getLogger(EvcAddCommand.class); - public EvcCreateCommand(DataBroker dataBroker, - Map, DataObject> changes) { - super.dataBroker = dataBroker; - super.changes = changes; + public EvcAddCommand(final DataBroker dataBroker, final DataTreeModification newEvcLink) { + super(dataBroker, newEvcLink); } @Override public void execute() { - for (Entry, DataObject> created : changes.entrySet()) { - if ((created.getValue() != null) && (created.getValue() instanceof EvcAugmentation)) { - EvcAugmentation evc = (EvcAugmentation) created.getValue(); - InstanceIdentifier evcKey = created.getKey(); + final InstanceIdentifier evcKey = dataObject.getRootPath().getRootIdentifier(); + Optional optLinks = MdsalUtils.readLink(dataBroker, LogicalDatastoreType.OPERATIONAL, evcKey); + if (!optLinks.isPresent()) { + final Link evcLink = dataObject.getRootNode().getDataAfter(); + final EvcAugmentation evc = evcLink.getAugmentation(EvcAugmentation.class); + if (evc != null) { // For now, we assume that there is 1 uni per source/destination if ((evc.getUniDest() == null) || evc.getUniDest().isEmpty()) { LOG.error("Destination UNI cannot be null."); - break; + return; } if ((evc.getUniSource() == null) || evc.getUniSource().isEmpty()) { LOG.error("Source UNI cannot be null."); - break; + return; } LOG.info("New EVC created, source IP: {} destination IP {}.", evc.getUniSource().iterator().next().getIpAddress().getIpv4Address(), @@ -59,7 +59,7 @@ public class EvcCreateCommand extends AbstractCreateCommand { InstanceIdentifier destinationUniIid; //FIXME we are assuming that there is only 1 UNI source and destination // per evc - InstanceIdentifier iidSource = evc.getUniSource().iterator().next().getUni(); + final InstanceIdentifier iidSource = evc.getUniSource().iterator().next().getUni(); if (iidSource != null) { sourceUniIid = iidSource.firstIdentifierOf(Node.class); } else { @@ -67,7 +67,7 @@ public class EvcCreateCommand extends AbstractCreateCommand { evc.getUniSource().iterator().next().getIpAddress(), LogicalDatastoreType.OPERATIONAL); } - InstanceIdentifier iidDest = evc.getUniDest().iterator().next().getUni(); + final InstanceIdentifier iidDest = evc.getUniDest().iterator().next().getUni(); if (iidDest != null) { destinationUniIid = iidDest.firstIdentifierOf(Node.class); } else { @@ -75,10 +75,10 @@ public class EvcCreateCommand extends AbstractCreateCommand { evc.getUniDest().iterator().next().getIpAddress(), LogicalDatastoreType.OPERATIONAL); } - Optional optionalUniSource = MdsalUtils.readNode(dataBroker, + final Optional optionalUniSource = MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, sourceUniIid); - Optional optionalUniDestination = MdsalUtils.readNode(dataBroker, + final Optional optionalUniDestination = MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, destinationUniIid); Node uniSource; @@ -88,31 +88,31 @@ public class EvcCreateCommand extends AbstractCreateCommand { uniSource = optionalUniSource.get(); uniDestination = optionalUniDestination.get(); // Set source and destination - UniAugmentation sourceUniAugmentation = + final UniAugmentation sourceUniAugmentation = uniSource.getAugmentation(UniAugmentation.class); - UniAugmentation destinationUniAugmentation = + final UniAugmentation destinationUniAugmentation = uniDestination.getAugmentation(UniAugmentation.class); - Optional optionalSourceOvsdbNode = + final Optional optionalSourceOvsdbNode = MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, sourceUniAugmentation .getOvsdbNodeRef() .getValue()); - Optional optionalDestinationOvsdbNode = + final Optional optionalDestinationOvsdbNode = MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, destinationUniAugmentation .getOvsdbNodeRef() .getValue()); if (optionalSourceOvsdbNode.isPresent() && optionalDestinationOvsdbNode.isPresent()) { - InstanceIdentifier sourceBridgeIid = + final InstanceIdentifier sourceBridgeIid = UnimgrMapper.getOvsdbBridgeNodeIid(optionalSourceOvsdbNode.get()); - Optional optionalSourceBr = MdsalUtils.readNode(dataBroker, + final Optional optionalSourceBr = MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, sourceBridgeIid); - InstanceIdentifier destinationBridgeIid = + final InstanceIdentifier destinationBridgeIid = UnimgrMapper.getOvsdbBridgeNodeIid(optionalDestinationOvsdbNode.get()); - Optional optionalDestinationBr = MdsalUtils.readNode(dataBroker, + final Optional optionalDestinationBr = MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, destinationBridgeIid); //update ovsdb qos-entry and queues with max-rate to match evc ingress BW @@ -144,11 +144,11 @@ public class EvcCreateCommand extends AbstractCreateCommand { UnimgrConstants.DEFAULT_BRIDGE_NAME, UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME); EvcUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION, - evcKey, - evc, - sourceUniIid, - destinationUniIid, - dataBroker); + evcKey, + evc, + sourceUniIid, + destinationUniIid, + dataBroker); EvcUtils.updateEvcNode(LogicalDatastoreType.OPERATIONAL, evcKey, evc, diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/EvcDeleteCommand.java b/impl/src/main/java/org/opendaylight/unimgr/command/EvcDeleteCommand.java deleted file mode 100644 index aba3ba39..00000000 --- a/impl/src/main/java/org/opendaylight/unimgr/command/EvcDeleteCommand.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (c) 2016 CableLabs 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, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.unimgr.command; - -import java.util.List; -import java.util.Set; - -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.unimgr.utils.EvcUtils; -import org.opendaylight.unimgr.utils.MdsalUtils; -import org.opendaylight.unimgr.utils.OvsdbUtils; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource; -import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -import com.google.common.base.Optional; - -public class EvcDeleteCommand extends AbstractDeleteCommand { - - public EvcDeleteCommand(DataBroker dataBroker, - AsyncDataChangeEvent, DataObject> changes) { - super.changes = changes; - super.dataBroker = dataBroker; - } - - @Override - public void execute() { - Set> removedEvcs = OvsdbUtils.extractRemoved(changes, - EvcAugmentation.class); - if (!removedEvcs.isEmpty()) { - for (InstanceIdentifier removedEvcIid: removedEvcs) { - EvcAugmentation evcAugmentation = MdsalUtils.read(dataBroker, - LogicalDatastoreType.OPERATIONAL, - removedEvcIid); - if (evcAugmentation != null) { - List unisSource = evcAugmentation.getUniSource(); - List unisDest = evcAugmentation.getUniDest(); - if (unisSource != null && !unisSource.isEmpty()) { - for (UniSource source: unisSource) { - if (source != null) { - Optional optionalSourceUniNode = - MdsalUtils.readNode(dataBroker, - LogicalDatastoreType.OPERATIONAL, - source.getUni()); - EvcUtils.deleteEvcData(dataBroker, optionalSourceUniNode); - } - } - } - if (unisDest != null && !unisDest.isEmpty()) { - for (UniDest dest : unisDest) { - if (dest != null) { - Optional optionalDestUniNode = - MdsalUtils.readNode(dataBroker, - LogicalDatastoreType.OPERATIONAL, - dest.getUni()); - EvcUtils.deleteEvcData(dataBroker, optionalDestUniNode); - } - } - } - } - MdsalUtils.deleteNode(dataBroker, removedEvcIid, LogicalDatastoreType.OPERATIONAL); - } - } - } -} diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/EvcRemoveCommand.java b/impl/src/main/java/org/opendaylight/unimgr/command/EvcRemoveCommand.java new file mode 100644 index 00000000..d811aeb2 --- /dev/null +++ b/impl/src/main/java/org/opendaylight/unimgr/command/EvcRemoveCommand.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2016 CableLabs 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.unimgr.command; + +import java.util.List; + +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.unimgr.api.AbstractCommand; +import org.opendaylight.unimgr.utils.EvcUtils; +import org.opendaylight.unimgr.utils.MdsalUtils; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Optional; + +public class EvcRemoveCommand extends AbstractCommand { + + private static final Logger LOG = LoggerFactory.getLogger(EvcRemoveCommand.class); + + public EvcRemoveCommand(final DataBroker dataBroker, final DataTreeModification removedEvcLink) { + super(dataBroker, removedEvcLink); + } + + @Override + public void execute() { + final Link evcLink = dataObject.getRootNode().getDataBefore(); + final EvcAugmentation evcAugmentation = evcLink.getAugmentation(EvcAugmentation.class); + final InstanceIdentifier removedEvcIid = dataObject.getRootPath().getRootIdentifier(); + if (evcAugmentation != null) { + final List unisSource = evcAugmentation.getUniSource(); + final List unisDest = evcAugmentation.getUniDest(); + if (unisSource != null && !unisSource.isEmpty()) { + for (final UniSource source: unisSource) { + if (source != null) { + final Optional optionalSourceUniNode = + MdsalUtils.readNode(dataBroker, + LogicalDatastoreType.OPERATIONAL, + source.getUni()); + EvcUtils.deleteEvcData(dataBroker, optionalSourceUniNode); + } + } + } + if (unisDest != null && !unisDest.isEmpty()) { + for (final UniDest dest : unisDest) { + if (dest != null) { + final Optional optionalDestUniNode = + MdsalUtils.readNode(dataBroker, + LogicalDatastoreType.OPERATIONAL, + dest.getUni()); + EvcUtils.deleteEvcData(dataBroker, optionalDestUniNode); + } + } + } + } + else { + LOG.info("EvcAugmentation is null"); + } + MdsalUtils.deleteNode(dataBroker, removedEvcIid, LogicalDatastoreType.OPERATIONAL); + } +} diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/EvcUpdateCommand.java b/impl/src/main/java/org/opendaylight/unimgr/command/EvcUpdateCommand.java index 37183b56..2941ff77 100644 --- a/impl/src/main/java/org/opendaylight/unimgr/command/EvcUpdateCommand.java +++ b/impl/src/main/java/org/opendaylight/unimgr/command/EvcUpdateCommand.java @@ -1,19 +1,21 @@ /* - * Copyright (c) 2015 CableLabs and others. All rights reserved. + * Copyright (c) 2016 CableLabs 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, * and is available at http://www.eclipse.org/legal/epl-v10.html */ + package org.opendaylight.unimgr.command; -import java.util.Map; -import java.util.Map.Entry; +import java.util.concurrent.ExecutionException; import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; import org.opendaylight.controller.md.sal.binding.api.ReadTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; +import org.opendaylight.unimgr.api.AbstractCommand; import org.opendaylight.unimgr.impl.UnimgrConstants; import org.opendaylight.unimgr.impl.UnimgrMapper; import org.opendaylight.unimgr.utils.EvcUtils; @@ -23,8 +25,8 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types. import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; -import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,197 +34,195 @@ import org.slf4j.LoggerFactory; import com.google.common.base.Optional; import com.google.common.util.concurrent.CheckedFuture; -public class EvcUpdateCommand extends AbstractUpdateCommand { +public class EvcUpdateCommand extends AbstractCommand { private static final Logger LOG = LoggerFactory.getLogger(EvcUpdateCommand.class); - public EvcUpdateCommand(final DataBroker dataBroker, - final Map, DataObject> changes) { - super.dataBroker = dataBroker; - super.changes = changes; + public EvcUpdateCommand(final DataBroker dataBroker, final DataTreeModification updatedEvcLink) { + super(dataBroker, updatedEvcLink); } - @SuppressWarnings("unchecked") @Override public void execute() { - for (final Entry, DataObject> updated : changes.entrySet()) { - if ((updated.getValue() != null) && (updated.getValue() instanceof EvcAugmentation)) { - final EvcAugmentation evc = (EvcAugmentation) updated.getValue(); - final InstanceIdentifier evcKey = updated.getKey(); - - // FIXME: For now, we assume that there is 1 uni per - // source/destination - if ((evc.getUniDest() == null) || evc.getUniDest().isEmpty()) { - LOG.error("Destination UNI cannot be null."); - break; - } - if ((evc.getUniSource() == null) || evc.getUniSource().isEmpty()) { - LOG.error("Source UNI cannot be null."); - break; - } - - final Ipv4Address laterUni1Ip = evc.getUniSource().iterator().next().getIpAddress().getIpv4Address(); - final Ipv4Address laterUni2Ip = evc.getUniDest().iterator().next().getIpAddress().getIpv4Address(); - LOG.trace("New EVC created, source IP: {} destination IP {}.", laterUni1Ip, laterUni2Ip); - - - final ReadTransaction readTransac = dataBroker.newReadOnlyTransaction(); - final CheckedFuture retFormerEvc = readTransac.read(LogicalDatastoreType.OPERATIONAL, evcKey); - EvcAugmentation formerEvc; - try { - formerEvc = (EvcAugmentation) ((Optional) retFormerEvc.checkedGet()).get(); - final Ipv4Address formerUni1ip = formerEvc.getUniSource().iterator().next().getIpAddress().getIpv4Address(); - final Ipv4Address formerUni2ip = formerEvc.getUniDest().iterator().next().getIpAddress().getIpv4Address(); - - if (formerUni1ip.equals(laterUni1Ip)) { - // do nothing - } else if (formerUni1ip.equals(laterUni2Ip)) { - // do nothing - } else { - LOG.info("{} is not part of the EVC, removing configuration", formerUni1ip); - final InstanceIdentifier formerUniIID = UnimgrMapper.getUniIid(dataBroker, new IpAddress(formerUni1ip), LogicalDatastoreType.OPERATIONAL); - final Optional formerUni = MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, formerUniIID); - EvcUtils.deleteEvcData(dataBroker, formerUni); - } - if (formerUni2ip.equals(laterUni1Ip)) { - // do nothing - } else if (formerUni2ip.equals(laterUni2Ip)) { - // do nothing - } else { - LOG.info("{} is not part of the EVC, removing configuration", formerUni2ip); - final InstanceIdentifier formerUniIID = UnimgrMapper.getUniIid(dataBroker, new IpAddress(formerUni2ip), LogicalDatastoreType.OPERATIONAL); - final Optional formerUni = MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, formerUniIID); - EvcUtils.deleteEvcData(dataBroker, formerUni); - } - } catch (final ReadFailedException e) { - LOG.error("Failed to retrieve former EVC {}", evcKey, e); - } - - InstanceIdentifier sourceUniIid; - InstanceIdentifier destinationUniIid; + final Link evcLink = dataObject.getRootNode().getDataAfter(); + final EvcAugmentation evc = evcLink.getAugmentation(EvcAugmentation.class); + final InstanceIdentifier evcKey = dataObject.getRootPath().getRootIdentifier(); + if (evc != null) { + // FIXME: For now, we assume that there is 1 uni per + // source/destination + if ((evc.getUniDest() == null) || evc.getUniDest().isEmpty()) { + LOG.error("Destination UNI cannot be null."); + return; + } + if ((evc.getUniSource() == null) || evc.getUniSource().isEmpty()) { + LOG.error("Source UNI cannot be null."); + return; + } - final InstanceIdentifier iidSource = evc.getUniSource().iterator().next().getUni(); - if (iidSource != null) { - sourceUniIid = iidSource.firstIdentifierOf(Node.class); + final Ipv4Address laterUni1Ip = evc.getUniSource().iterator().next().getIpAddress().getIpv4Address(); + final Ipv4Address laterUni2Ip = evc.getUniDest().iterator().next().getIpAddress().getIpv4Address(); + LOG.trace("New EVC created, source IP: {} destination IP {}.", laterUni1Ip, laterUni2Ip); + + final ReadTransaction readTransac = dataBroker.newReadOnlyTransaction(); + final CheckedFuture, ReadFailedException> retFormerEvc = readTransac.read(LogicalDatastoreType.OPERATIONAL, evcKey); + EvcAugmentation formerEvc; + try { + Optional optLinks = retFormerEvc.get(); + if(optLinks != null && optLinks.isPresent()) { + formerEvc = optLinks.get().getAugmentation(EvcAugmentation.class); + final Ipv4Address formerUni1ip = formerEvc.getUniSource().iterator().next().getIpAddress().getIpv4Address(); + final Ipv4Address formerUni2ip = formerEvc.getUniDest().iterator().next().getIpAddress().getIpv4Address(); + + if (formerUni1ip.equals(laterUni1Ip)) { + // do nothing + } else if (formerUni1ip.equals(laterUni2Ip)) { + // do nothing } else { - sourceUniIid = UnimgrMapper.getUniIid(dataBroker, - evc.getUniSource().iterator().next().getIpAddress(), - LogicalDatastoreType.OPERATIONAL); + LOG.info("{} is not part of the EVC, removing configuration", formerUni1ip); + final InstanceIdentifier formerUniIID = UnimgrMapper.getUniIid(dataBroker, new IpAddress(formerUni1ip), LogicalDatastoreType.OPERATIONAL); + final Optional formerUni = MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, formerUniIID); + EvcUtils.deleteEvcData(dataBroker, formerUni); } - final InstanceIdentifier iidDest = evc.getUniDest().iterator().next().getUni(); - if (iidDest != null) { - destinationUniIid = iidDest.firstIdentifierOf(Node.class); + if (formerUni2ip.equals(laterUni1Ip)) { + // do nothing + } else if (formerUni2ip.equals(laterUni2Ip)) { + // do nothing } else { - destinationUniIid = UnimgrMapper.getUniIid(dataBroker, - evc.getUniDest().iterator().next().getIpAddress(), - LogicalDatastoreType.OPERATIONAL); + LOG.info("{} is not part of the EVC, removing configuration", formerUni2ip); + final InstanceIdentifier formerUniIID = UnimgrMapper.getUniIid(dataBroker, new IpAddress(formerUni2ip), LogicalDatastoreType.OPERATIONAL); + final Optional formerUni = MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, formerUniIID); + EvcUtils.deleteEvcData(dataBroker, formerUni); + } } + } catch (final InterruptedException | ExecutionException e) { + LOG.error("Failed to retrieve former EVC {}", evcKey, e); + } - // Retrieve the source and destination UNIs - final Optional optionalUniSource = MdsalUtils.readNode(dataBroker, - LogicalDatastoreType.OPERATIONAL, - sourceUniIid); - final Optional optionalUniDestination = MdsalUtils.readNode(dataBroker, - LogicalDatastoreType.OPERATIONAL, - destinationUniIid); - - Node uniSource = null; - Node uniDestination = null; - - if (optionalUniSource.isPresent() && optionalUniDestination.isPresent()) { - uniSource = optionalUniSource.get(); - uniDestination = optionalUniDestination.get(); - - // Retrieve the source and/or destination OVSDB node - final UniAugmentation sourceUniAugmentation = - uniSource.getAugmentation(UniAugmentation.class); - final UniAugmentation destinationUniAugmentation = - uniDestination.getAugmentation(UniAugmentation.class); - final Optional optionalSourceOvsdbNode = - MdsalUtils.readNode(dataBroker, - LogicalDatastoreType.OPERATIONAL, - sourceUniAugmentation - .getOvsdbNodeRef() - .getValue()); - final Optional optionalDestinationOvsdbNode = - MdsalUtils.readNode(dataBroker, - LogicalDatastoreType.OPERATIONAL, - destinationUniAugmentation - .getOvsdbNodeRef() - .getValue()); - if (optionalSourceOvsdbNode.isPresent() && optionalDestinationOvsdbNode.isPresent()) { - // Retrieve the source and/or destination bridge - final InstanceIdentifier sourceBridgeIid = - UnimgrMapper.getOvsdbBridgeNodeIid(optionalSourceOvsdbNode.get()); - final Optional optionalSourceBr = MdsalUtils.readNode(dataBroker, + InstanceIdentifier sourceUniIid; + InstanceIdentifier destinationUniIid; + + final InstanceIdentifier iidSource = evc.getUniSource().iterator().next().getUni(); + if (iidSource != null) { + sourceUniIid = iidSource.firstIdentifierOf(Node.class); + } else { + sourceUniIid = UnimgrMapper.getUniIid(dataBroker, + evc.getUniSource().iterator().next().getIpAddress(), + LogicalDatastoreType.OPERATIONAL); + } + final InstanceIdentifier iidDest = evc.getUniDest().iterator().next().getUni(); + if (iidDest != null) { + destinationUniIid = iidDest.firstIdentifierOf(Node.class); + } else { + destinationUniIid = UnimgrMapper.getUniIid(dataBroker, + evc.getUniDest().iterator().next().getIpAddress(), + LogicalDatastoreType.OPERATIONAL); + } + + // Retrieve the source and destination UNIs + final Optional optionalUniSource = MdsalUtils.readNode(dataBroker, + LogicalDatastoreType.OPERATIONAL, + sourceUniIid); + final Optional optionalUniDestination = MdsalUtils.readNode(dataBroker, + LogicalDatastoreType.OPERATIONAL, + destinationUniIid); + + Node uniSource = null; + Node uniDestination = null; + + if (optionalUniSource.isPresent() && optionalUniDestination.isPresent()) { + uniSource = optionalUniSource.get(); + uniDestination = optionalUniDestination.get(); + + // Retrieve the source and/or destination OVSDB node + final UniAugmentation sourceUniAugmentation = + uniSource.getAugmentation(UniAugmentation.class); + final UniAugmentation destinationUniAugmentation = + uniDestination.getAugmentation(UniAugmentation.class); + final Optional optionalSourceOvsdbNode = + MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, - sourceBridgeIid); - final InstanceIdentifier destinationBridgeIid = - UnimgrMapper.getOvsdbBridgeNodeIid(optionalDestinationOvsdbNode.get()); - final Optional optionalDestinationBr = MdsalUtils.readNode(dataBroker, + sourceUniAugmentation + .getOvsdbNodeRef() + .getValue()); + final Optional optionalDestinationOvsdbNode = + MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, - destinationBridgeIid); - //update ovsdb qos-entry and queues with max-rate to match evc ingress BW - OvsdbUtils.updateMaxRate(dataBroker, sourceUniAugmentation, destinationUniAugmentation, evc); - Node sourceBr = null; - Node destinationBr = null; - if (optionalSourceBr.isPresent() && optionalDestinationBr.isPresent()) { - sourceBr = optionalSourceBr.get(); - destinationBr = optionalDestinationBr.get(); - - // Creating termination points (OVSDB CONFIG - // datastore) - OvsdbUtils.createTerminationPointNode(dataBroker, - uniSource.getAugmentation(UniAugmentation.class), - sourceBr, - UnimgrConstants.DEFAULT_BRIDGE_NAME, - UnimgrConstants.DEFAULT_TUNNEL_IFACE); - - // Create GRE tunnel (OVSDB CONFIG datastore) - OvsdbUtils.createGreTunnel(dataBroker, - uniSource.getAugmentation(UniAugmentation.class), - uniDestination.getAugmentation(UniAugmentation.class), - sourceBr, - UnimgrConstants.DEFAULT_BRIDGE_NAME, - UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME); - - // Create termination points (CONFIG datastore) - OvsdbUtils.createTerminationPointNode(dataBroker, - uniSource.getAugmentation(UniAugmentation.class), - destinationBr, - UnimgrConstants.DEFAULT_BRIDGE_NAME, - UnimgrConstants.DEFAULT_TUNNEL_IFACE); - - // Create GRE tunnel (OVSDB CONFIG datastore) - OvsdbUtils.createGreTunnel(dataBroker, - uniDestination.getAugmentation(UniAugmentation.class), - uniSource.getAugmentation(UniAugmentation.class), destinationBr, - UnimgrConstants.DEFAULT_BRIDGE_NAME, - UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME); - - // Update EVC - EvcUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION, - evcKey, - evc, - sourceUniIid, - destinationUniIid, - dataBroker); - EvcUtils.updateEvcNode(LogicalDatastoreType.OPERATIONAL, - evcKey, - evc, - sourceUniIid, - destinationUniIid, - dataBroker); - } else { - LOG.info("Unable to retrieve the source and/or destination bridge."); - } + destinationUniAugmentation + .getOvsdbNodeRef() + .getValue()); + if (optionalSourceOvsdbNode.isPresent() && optionalDestinationOvsdbNode.isPresent()) { + // Retrieve the source and/or destination bridge + final InstanceIdentifier sourceBridgeIid = + UnimgrMapper.getOvsdbBridgeNodeIid(optionalSourceOvsdbNode.get()); + final Optional optionalSourceBr = MdsalUtils.readNode(dataBroker, + LogicalDatastoreType.OPERATIONAL, + sourceBridgeIid); + final InstanceIdentifier destinationBridgeIid = + UnimgrMapper.getOvsdbBridgeNodeIid(optionalDestinationOvsdbNode.get()); + final Optional optionalDestinationBr = MdsalUtils.readNode(dataBroker, + LogicalDatastoreType.OPERATIONAL, + destinationBridgeIid); + //update ovsdb qos-entry and queues with max-rate to match evc ingress BW + OvsdbUtils.updateMaxRate(dataBroker, sourceUniAugmentation, destinationUniAugmentation, evc); + Node sourceBr = null; + Node destinationBr = null; + if (optionalSourceBr.isPresent() && optionalDestinationBr.isPresent()) { + sourceBr = optionalSourceBr.get(); + destinationBr = optionalDestinationBr.get(); + + // Creating termination points (OVSDB CONFIG + // datastore) + OvsdbUtils.createTerminationPointNode(dataBroker, + uniSource.getAugmentation(UniAugmentation.class), + sourceBr, + UnimgrConstants.DEFAULT_BRIDGE_NAME, + UnimgrConstants.DEFAULT_TUNNEL_IFACE); + + // Create GRE tunnel (OVSDB CONFIG datastore) + OvsdbUtils.createGreTunnel(dataBroker, + uniSource.getAugmentation(UniAugmentation.class), + uniDestination.getAugmentation(UniAugmentation.class), + sourceBr, + UnimgrConstants.DEFAULT_BRIDGE_NAME, + UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME); + + // Create termination points (CONFIG datastore) + OvsdbUtils.createTerminationPointNode(dataBroker, + uniSource.getAugmentation(UniAugmentation.class), + destinationBr, + UnimgrConstants.DEFAULT_BRIDGE_NAME, + UnimgrConstants.DEFAULT_TUNNEL_IFACE); + + // Create GRE tunnel (OVSDB CONFIG datastore) + OvsdbUtils.createGreTunnel(dataBroker, + uniDestination.getAugmentation(UniAugmentation.class), + uniSource.getAugmentation(UniAugmentation.class), destinationBr, + UnimgrConstants.DEFAULT_BRIDGE_NAME, + UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME); + + // Update EVC + EvcUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION, + evcKey, + evc, + sourceUniIid, + destinationUniIid, + dataBroker); + EvcUtils.updateEvcNode(LogicalDatastoreType.OPERATIONAL, + evcKey, + evc, + sourceUniIid, + destinationUniIid, + dataBroker); } else { - LOG.info("Uname to retrieve the source and/or destination ovsdbNode."); + LOG.info("Unable to retrieve the source and/or destination bridge."); } } else { - LOG.info("Unable to retrieve the source and/or destination Uni."); + LOG.info("Unable to retrieve the source and/or destination ovsdbNode."); } + } else { + LOG.info("Unable to retrieve the source and/or destination Uni."); } } } + } diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/OvsNodeAddCommand.java b/impl/src/main/java/org/opendaylight/unimgr/command/OvsNodeAddCommand.java new file mode 100644 index 00000000..3f38c17d --- /dev/null +++ b/impl/src/main/java/org/opendaylight/unimgr/command/OvsNodeAddCommand.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2016 CableLabs 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.unimgr.command; + +import java.util.List; + +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.unimgr.api.AbstractCommand; +import org.opendaylight.unimgr.impl.UnimgrConstants; +import org.opendaylight.unimgr.impl.UnimgrMapper; +import org.opendaylight.unimgr.utils.MdsalUtils; +import org.opendaylight.unimgr.utils.OvsdbUtils; +import org.opendaylight.unimgr.utils.UniUtils; +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.unimgr.rev151012.UniAugmentation; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Optional; + +public class OvsNodeAddCommand extends AbstractCommand { + + private static final Logger LOG = LoggerFactory.getLogger(OvsNodeAddCommand.class); + + public OvsNodeAddCommand(final DataBroker dataBroker, final DataTreeModification newOvsNode) { + super(dataBroker, newOvsNode); + } + + @Override + public void execute() { + final Node ovsNode = dataObject.getRootNode().getDataAfter(); + final OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsNode.getAugmentation(OvsdbNodeAugmentation.class); + final InstanceIdentifier ovsdbIid = dataObject.getRootPath().getRootIdentifier(); + if (ovsdbNodeAugmentation != null) { + LOG.info("Received an OVSDB node create {}", + ovsdbNodeAugmentation.getConnectionInfo() + .getRemoteIp() + .getIpv4Address() + .getValue()); + final List uniNodes = UniUtils.getUniNodes(dataBroker); + if (uniNodes != null && !uniNodes.isEmpty()) { + for (final Node uniNode: uniNodes) { + final UniAugmentation uniAugmentation = uniNode.getAugmentation(UniAugmentation.class); + if (uniAugmentation.getOvsdbNodeRef() != null + && uniAugmentation.getOvsdbNodeRef().getValue() != null) { + final InstanceIdentifier ovsdbNodeRefIid = uniAugmentation + .getOvsdbNodeRef() + .getValue() + .firstIdentifierOf(Node.class); + if (ovsdbNodeRefIid.equals(ovsdbIid)) { + final Optional optionalOvsdbNode = MdsalUtils.readNode(dataBroker, + LogicalDatastoreType.OPERATIONAL, + ovsdbIid); + if (optionalOvsdbNode.isPresent()) { + final InstanceIdentifier uniIid = + UnimgrMapper.getUniIid(dataBroker, + uniAugmentation.getIpAddress(), + LogicalDatastoreType.CONFIGURATION); + // Update QoS entries to ovsdb if speed is configured to UNI node + if (uniAugmentation.getSpeed() != null) { + OvsdbUtils.createQoSForOvsdbNode(dataBroker, uniAugmentation); + } + OvsdbUtils.createBridgeNode(dataBroker, + ovsdbIid, + uniAugmentation, + UnimgrConstants.DEFAULT_BRIDGE_NAME); + UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, + uniIid, + uniAugmentation, + ovsdbIid, + dataBroker); + } + } + } else if (ovsdbNodeAugmentation + .getConnectionInfo() + .getRemoteIp() + .equals(uniAugmentation.getIpAddress())) { + final InstanceIdentifier uniIid = UnimgrMapper.getUniIid(dataBroker, + uniAugmentation.getIpAddress(), + LogicalDatastoreType.CONFIGURATION); + OvsdbUtils.createBridgeNode(dataBroker, + ovsdbIid, + uniAugmentation, + UnimgrConstants.DEFAULT_BRIDGE_NAME); + UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, + uniIid, + uniAugmentation, + ovsdbIid, + dataBroker); + } + } + } else { + LOG.info("Received a new OVSDB node connection from {}" + + ovsdbNodeAugmentation.getConnectionInfo() + .getRemoteIp().getIpv4Address()); + } + } + } + +} diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/TransactionInvoker.java b/impl/src/main/java/org/opendaylight/unimgr/command/TransactionInvoker.java deleted file mode 100644 index 32e4338e..00000000 --- a/impl/src/main/java/org/opendaylight/unimgr/command/TransactionInvoker.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2015 CableLabs 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, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.unimgr.command; - -import java.util.List; - -public class TransactionInvoker { - - private Command command; - private List commands; - - public void setCommand(Command command) { - this.command = command; - } - - public void setCommands(List commands) { - this.commands = commands; - } - - public void invoke() { - if (command != null) { - command.execute(); - } - if (!commands.isEmpty()) { - for (Command invokableCommand: commands) { - invokableCommand.execute(); - } - } - } - -} diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/UniAddCommand.java b/impl/src/main/java/org/opendaylight/unimgr/command/UniAddCommand.java new file mode 100644 index 00000000..9533ad36 --- /dev/null +++ b/impl/src/main/java/org/opendaylight/unimgr/command/UniAddCommand.java @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2016 CableLabs 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.unimgr.command; + +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.unimgr.api.AbstractCommand; +import org.opendaylight.unimgr.impl.UnimgrConstants; +import org.opendaylight.unimgr.impl.UnimgrMapper; +import org.opendaylight.unimgr.utils.MdsalUtils; +import org.opendaylight.unimgr.utils.OvsdbUtils; +import org.opendaylight.unimgr.utils.UniUtils; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Optional; + +public class UniAddCommand extends AbstractCommand{ + + private static final Logger LOG = LoggerFactory.getLogger(UniAddCommand.class); + + public UniAddCommand(final DataBroker dataBroker, final DataTreeModification newUniNode) { + super(dataBroker, newUniNode); + } + + @Override + public void execute() { + final InstanceIdentifier uniKey = dataObject.getRootPath().getRootIdentifier(); + final Optional optNode = MdsalUtils.readNode(dataBroker, LogicalDatastoreType.OPERATIONAL, uniKey); + if(!optNode.isPresent()) { + final Node uniNode = dataObject.getRootNode().getDataAfter(); + final UniAugmentation uni = uniNode.getAugmentation(UniAugmentation.class); + if (uni != null) { + LOG.info("New UNI created {}.", uni.getIpAddress().getIpv4Address()); + // We assume the ovs is in active mode tcp:ipAddress:6640 + if (uni.getOvsdbNodeRef() != null) { + final OvsdbNodeRef ovsdbNodeRef = uni.getOvsdbNodeRef(); + final Optional optionalNode = MdsalUtils.readNode(dataBroker, + LogicalDatastoreType.OPERATIONAL, + ovsdbNodeRef.getValue()); + if (!optionalNode.isPresent()) { + LOG.info("Invalid OVSDB node instance identifier specified, " + + "attempting to retrieve the node."); + final Optional optionalOvsdbNode = OvsdbUtils.findOvsdbNode(dataBroker, + uni); + Node ovsdbNode; + if (optionalOvsdbNode.isPresent()) { + ovsdbNode = optionalOvsdbNode.get(); + LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId()); + // Update QoS entries to ovsdb if speed is configured to UNI node + if (uni.getSpeed() != null) { + OvsdbUtils.createQoSForOvsdbNode(dataBroker, uni); + } + UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION, + uniKey, + uni, + ovsdbNode, + dataBroker); + } else { + ovsdbNode = OvsdbUtils.createOvsdbNode(dataBroker, + uni); + LOG.info("Could not retrieve the OVSDB node," + + " created a new one: {}", ovsdbNode.getNodeId()); + UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION, + uniKey, + uni, + ovsdbNode, + dataBroker); + } + } + } else { + // We assume the ovs is in passive mode + // Check if the ovsdb node exist + final Optional optionalOvsdbNode = OvsdbUtils.findOvsdbNode(dataBroker, + uni); + Node ovsdbNode; + if (optionalOvsdbNode.isPresent()) { + ovsdbNode = optionalOvsdbNode.get(); + final InstanceIdentifier ovsdbIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNode.getNodeId()); + LOG.info("Retrieved the OVSDB node"); + // Update QoS entries to ovsdb if speed is configured to UNI node + if (uni.getSpeed() != null) { + OvsdbUtils.createQoSForOvsdbNode(dataBroker, uni); + } + OvsdbUtils.createBridgeNode(dataBroker, + ovsdbIid, + uni, + UnimgrConstants.DEFAULT_BRIDGE_NAME); + UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION, + uniKey, + uni, + ovsdbNode, + dataBroker); + UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniKey, uni, ovsdbNode, dataBroker); + } else { + ovsdbNode = OvsdbUtils.createOvsdbNode(dataBroker, + uni); + if (ovsdbNode != null) { + LOG.info("Could not retrieve the OVSDB node," + + "created a new one: {}", ovsdbNode.getNodeId()); + UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION, + uniKey, + uni, + ovsdbNode, + dataBroker); + } + } + } + } + } + } + +} diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/UniCreateCommand.java b/impl/src/main/java/org/opendaylight/unimgr/command/UniCreateCommand.java deleted file mode 100644 index a1ca0b18..00000000 --- a/impl/src/main/java/org/opendaylight/unimgr/command/UniCreateCommand.java +++ /dev/null @@ -1,209 +0,0 @@ -/* - * Copyright (c) 2015 CableLabs 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, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.unimgr.command; - -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.unimgr.impl.UnimgrConstants; -import org.opendaylight.unimgr.impl.UnimgrMapper; -import org.opendaylight.unimgr.utils.MdsalUtils; -import org.opendaylight.unimgr.utils.OvsdbUtils; -import org.opendaylight.unimgr.utils.UniUtils; -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.OvsdbNodeRef; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation; -import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.base.Optional; - -public class UniCreateCommand extends AbstractCreateCommand { - - private static final Logger LOG = LoggerFactory.getLogger(UniCreateCommand.class); - - public UniCreateCommand(DataBroker dataBroker, - Map, DataObject> changes) { - super.dataBroker = dataBroker; - super.changes = changes; - } - - @Override - public void execute() { - for (final Entry, DataObject> created : changes.entrySet()) { - if (created.getValue() != null && created.getValue() instanceof UniAugmentation) { - final UniAugmentation uni = (UniAugmentation) created.getValue(); - final InstanceIdentifier uniKey = created.getKey(); - LOG.trace("New UNI created {}.", uni.getIpAddress().getIpv4Address()); - /* We assume that when the user specifies the - * ovsdb-node-ref that the node already exists in - * the controller and that the OVS instance is in - * active mode. - * - * We assume that when the user doesn't specify the - * ovsdb-node-id that the node doesn't exist therefor - * has to be created with the IP address because it's - * in passive mode. - * - * Active mode (TCP): the UUID is in format ovsdb://UUID - * Passive mode (PTCP): the UUID is in format ovsdb://IP:6640 - * - */ - if (uni.getOvsdbNodeRef() != null) { - final OvsdbNodeRef ovsdbNodeRef = uni.getOvsdbNodeRef(); - final Optional optionalNode = MdsalUtils.readNode(dataBroker, - LogicalDatastoreType.OPERATIONAL, - ovsdbNodeRef.getValue()); - if (!optionalNode.isPresent()) { - LOG.info("Invalid OVSDB node instance identifier specified, " - + "attempting to retrieve the node."); - final Optional optionalOvsdbNode = OvsdbUtils.findOvsdbNode(dataBroker, - uni); - Node ovsdbNode; - if (optionalOvsdbNode.isPresent()) { - ovsdbNode = optionalOvsdbNode.get(); - LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId()); - // Update QoS entries to ovsdb if speed is configured to UNI node - if (uni.getSpeed() != null) { - OvsdbUtils.createQoSForOvsdbNode(dataBroker, uni); - } - UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION, - uniKey, - uni, - ovsdbNode, - dataBroker); - } else { - ovsdbNode = OvsdbUtils.createOvsdbNode(dataBroker, - uni); - LOG.info("Could not retrieve the OVSDB node," - + " created a new one: {}", ovsdbNode.getNodeId()); - UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION, - uniKey, - uni, - ovsdbNode, - dataBroker); - } - } - } else { - // We assume the ovs is in passive mode - // Check if the ovsdb node exist - final Optional optionalOvsdbNode = OvsdbUtils.findOvsdbNode(dataBroker, - uni); - Node ovsdbNode; - if (optionalOvsdbNode.isPresent()) { - ovsdbNode = optionalOvsdbNode.get(); - final InstanceIdentifier ovsdbIid = UnimgrMapper.getOvsdbNodeIid(ovsdbNode.getNodeId()); - LOG.info("Retrieved the OVSDB node"); - // Update QoS entries to ovsdb if speed is configured to UNI node - if (uni.getSpeed() != null) { - OvsdbUtils.createQoSForOvsdbNode(dataBroker, uni); - } - OvsdbUtils.createBridgeNode(dataBroker, - ovsdbIid, - uni, - UnimgrConstants.DEFAULT_BRIDGE_NAME); - UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION, - uniKey, - uni, - ovsdbNode, - dataBroker); - UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniKey, uni, ovsdbNode, dataBroker); - } else { - ovsdbNode = OvsdbUtils.createOvsdbNode(dataBroker, - uni); - if (ovsdbNode != null) { - LOG.info("Could not retrieve the OVSDB node," - + "created a new one: {}", ovsdbNode.getNodeId()); - UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION, - uniKey, - uni, - ovsdbNode, - dataBroker); - } - } - } - } - if (created.getValue() != null && created.getValue() instanceof OvsdbNodeAugmentation) { - final OvsdbNodeAugmentation ovsdbNodeAugmentation = (OvsdbNodeAugmentation) created - .getValue(); - final InstanceIdentifier ovsdbIid = created.getKey().firstIdentifierOf(Node.class); - if (ovsdbNodeAugmentation != null) { - LOG.info("Received an OVSDB node create {}", - ovsdbNodeAugmentation.getConnectionInfo() - .getRemoteIp() - .getIpv4Address() - .getValue()); - final List uniNodes = UniUtils.getUniNodes(dataBroker); - if (uniNodes != null && !uniNodes.isEmpty()) { - for (final Node uniNode: uniNodes) { - final UniAugmentation uniAugmentation = uniNode.getAugmentation(UniAugmentation.class); - if (uniAugmentation.getOvsdbNodeRef() != null - && uniAugmentation.getOvsdbNodeRef().getValue() != null) { - final InstanceIdentifier ovsdbNodeRefIid = uniAugmentation - .getOvsdbNodeRef() - .getValue() - .firstIdentifierOf(Node.class); - if (ovsdbNodeRefIid.equals(ovsdbIid)) { - final Optional optionalOvsdbNode = MdsalUtils.readNode(dataBroker, - LogicalDatastoreType.OPERATIONAL, - ovsdbIid); - if (optionalOvsdbNode.isPresent()) { - final InstanceIdentifier uniIid = - UnimgrMapper.getUniIid(dataBroker, - uniAugmentation.getIpAddress(), - LogicalDatastoreType.CONFIGURATION); - // Update QoS entries to ovsdb if speed is configured to UNI node - if (uniAugmentation.getSpeed() != null) { - OvsdbUtils.createQoSForOvsdbNode(dataBroker, uniAugmentation); - } - OvsdbUtils.createBridgeNode(dataBroker, - ovsdbIid, - uniAugmentation, - UnimgrConstants.DEFAULT_BRIDGE_NAME); - UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, - uniIid, - uniAugmentation, - ovsdbIid, - dataBroker); - } - } - } else if (ovsdbNodeAugmentation - .getConnectionInfo() - .getRemoteIp() - .equals(uniAugmentation.getIpAddress())) { - final InstanceIdentifier uniIid = UnimgrMapper.getUniIid(dataBroker, - uniAugmentation.getIpAddress(), - LogicalDatastoreType.CONFIGURATION); - OvsdbUtils.createBridgeNode(dataBroker, - ovsdbIid, - uniAugmentation, - UnimgrConstants.DEFAULT_BRIDGE_NAME); - UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, - uniIid, - uniAugmentation, - ovsdbIid, - dataBroker); - } - } - } else { - LOG.info("Received a new OVSDB node connection from {}" - + ovsdbNodeAugmentation.getConnectionInfo() - .getRemoteIp().getIpv4Address()); - } - } - } - } - } - -} diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/UniDeleteCommand.java b/impl/src/main/java/org/opendaylight/unimgr/command/UniDeleteCommand.java deleted file mode 100644 index 2d3e26ed..00000000 --- a/impl/src/main/java/org/opendaylight/unimgr/command/UniDeleteCommand.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (c) 2015 CableLabs 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, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.unimgr.command; - -import java.util.List; -import java.util.Set; - -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.unimgr.impl.UnimgrConstants; -import org.opendaylight.unimgr.impl.UnimgrMapper; -import org.opendaylight.unimgr.utils.MdsalUtils; -import org.opendaylight.unimgr.utils.OvsdbUtils; -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.OvsdbNodeRef; -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.QosEntriesKey; -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.QueuesKey; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation; -import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; -import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.base.Optional; - -public class UniDeleteCommand extends AbstractDeleteCommand { - - private static final Logger LOG = LoggerFactory.getLogger(UniDeleteCommand.class); - - public UniDeleteCommand(DataBroker dataBroker, - AsyncDataChangeEvent, DataObject> changes) { - super.changes = changes; - super.dataBroker = dataBroker; - } - - @Override - public void execute() { - Set> removedUnis = OvsdbUtils.extractRemoved(changes, - UniAugmentation.class); - if (!removedUnis.isEmpty()) { - for (InstanceIdentifier removedUniIid: removedUnis) { - UniAugmentation uniAugmentation = MdsalUtils.read(dataBroker, - LogicalDatastoreType.OPERATIONAL, - removedUniIid); - if (uniAugmentation != null) { - OvsdbNodeRef ovsNodedRef = uniAugmentation.getOvsdbNodeRef(); - InstanceIdentifier ovsdbNodeIid = ovsNodedRef.getValue().firstIdentifierOf(Node.class); - Optional optionalNode = MdsalUtils.readNode(dataBroker, - LogicalDatastoreType.OPERATIONAL, - ovsdbNodeIid); - if (optionalNode.isPresent()) { - Node ovsdbNode = optionalNode.get(); - LOG.info("Delete QoS and Queues entries"); - List qosList = ovsdbNode - .getAugmentation(OvsdbNodeAugmentation.class) - .getQosEntries(); - QosEntriesKey qosEntryKey = null; - for (final QosEntries qosEntry : qosList) { - qosEntryKey = qosEntry.getKey(); - InstanceIdentifier qosIid = UnimgrMapper.getOvsdbQoSEntriesIid(ovsdbNode, qosEntryKey); - MdsalUtils.deleteNode(dataBroker, qosIid, LogicalDatastoreType.CONFIGURATION); - } - - List queuesList = ovsdbNode - .getAugmentation(OvsdbNodeAugmentation.class) - .getQueues(); - QueuesKey queuesKey = null; - for (final Queues queue : queuesList) { - queuesKey = queue.getKey(); - InstanceIdentifier queuesIid = UnimgrMapper.getOvsdbQueuesIid(ovsdbNode, queuesKey); - MdsalUtils.deleteNode(dataBroker, queuesIid, LogicalDatastoreType.CONFIGURATION); - } - - LOG.info("Delete bridge node"); - InstanceIdentifier bridgeIid = UnimgrMapper.getOvsdbBridgeNodeIid(ovsdbNode); - Optional optBridgeNode = MdsalUtils.readNode(dataBroker, bridgeIid); - if (optBridgeNode.isPresent()) { - Node bridgeNode = optBridgeNode.get(); - InstanceIdentifier iidTermPoint = UnimgrMapper.getTerminationPointIid(bridgeNode, - UnimgrConstants.DEFAULT_BRIDGE_NAME); - MdsalUtils.deleteNode(dataBroker, iidTermPoint, LogicalDatastoreType.CONFIGURATION); - } - MdsalUtils.deleteNode(dataBroker, bridgeIid, LogicalDatastoreType.CONFIGURATION); - } - InstanceIdentifier iidUni = UnimgrMapper.getUniIid(dataBroker, uniAugmentation.getIpAddress(), - LogicalDatastoreType.OPERATIONAL); - if (iidUni != null) - MdsalUtils.deleteNode(dataBroker, iidUni, LogicalDatastoreType.OPERATIONAL); - } - else { - LOG.info("Received Uni Augmentation is null", removedUniIid); - } - } - } - } -} diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/UniRemoveCommand.java b/impl/src/main/java/org/opendaylight/unimgr/command/UniRemoveCommand.java new file mode 100644 index 00000000..3d29c777 --- /dev/null +++ b/impl/src/main/java/org/opendaylight/unimgr/command/UniRemoveCommand.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2016 CableLabs 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.unimgr.command; + +import java.util.List; + +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.unimgr.api.AbstractCommand; +import org.opendaylight.unimgr.impl.UnimgrMapper; +import org.opendaylight.unimgr.utils.MdsalUtils; +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.OvsdbNodeRef; +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.QosEntriesKey; +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.QueuesKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Optional; + +public class UniRemoveCommand extends AbstractCommand { + + private static final Logger LOG = LoggerFactory.getLogger(UniRemoveCommand.class); + + public UniRemoveCommand(final DataBroker dataBroker, final DataTreeModification removedUniNode) { + super(dataBroker, removedUniNode); + } + + @Override + public void execute() { + final Node uniNode = dataObject.getRootNode().getDataBefore(); + final UniAugmentation uniAugmentation = uniNode.getAugmentation(UniAugmentation.class); + final InstanceIdentifier removedUniIid = dataObject.getRootPath().getRootIdentifier() + .firstIdentifierOf(UniAugmentation.class); + if (uniAugmentation != null) { + final OvsdbNodeRef ovsNodedRef = uniAugmentation.getOvsdbNodeRef(); + final InstanceIdentifier ovsdbNodeIid = ovsNodedRef.getValue().firstIdentifierOf(Node.class); + final Optional optionalNode = MdsalUtils.readNode(dataBroker, + LogicalDatastoreType.OPERATIONAL, + ovsdbNodeIid); + if (optionalNode.isPresent()) { + final Node ovsdbNode = optionalNode.get(); + LOG.info("Delete QoS and Queues entries"); + List qosList = ovsdbNode + .getAugmentation(OvsdbNodeAugmentation.class) + .getQosEntries(); + QosEntriesKey qosEntryKey = null; + for (final QosEntries qosEntry : qosList) { + qosEntryKey = qosEntry.getKey(); + InstanceIdentifier qosIid = UnimgrMapper.getOvsdbQoSEntriesIid(ovsdbNode, qosEntryKey); + MdsalUtils.deleteNode(dataBroker, qosIid, LogicalDatastoreType.CONFIGURATION); + } + List queuesList = ovsdbNode + .getAugmentation(OvsdbNodeAugmentation.class) + .getQueues(); + QueuesKey queuesKey = null; + for (final Queues queue : queuesList) { + queuesKey = queue.getKey(); + InstanceIdentifier queuesIid = UnimgrMapper.getOvsdbQueuesIid(ovsdbNode, queuesKey); + MdsalUtils.deleteNode(dataBroker, queuesIid, LogicalDatastoreType.CONFIGURATION); + } + LOG.info("Delete bride node"); + final InstanceIdentifier bridgeIid = UnimgrMapper.getOvsdbBridgeNodeIid(ovsdbNode); + MdsalUtils.deleteNode(dataBroker, bridgeIid, LogicalDatastoreType.CONFIGURATION); + } + final InstanceIdentifier iidUni = UnimgrMapper.getUniIid(dataBroker, uniAugmentation.getIpAddress(), + LogicalDatastoreType.OPERATIONAL); + if (iidUni != null) { + MdsalUtils.deleteNode(dataBroker, iidUni, LogicalDatastoreType.OPERATIONAL); + } + } + else { + LOG.info("Received Uni Augmentation is null", removedUniIid); + } + } + +} diff --git a/impl/src/main/java/org/opendaylight/unimgr/command/UniUpdateCommand.java b/impl/src/main/java/org/opendaylight/unimgr/command/UniUpdateCommand.java index 7439b183..e4295e4e 100644 --- a/impl/src/main/java/org/opendaylight/unimgr/command/UniUpdateCommand.java +++ b/impl/src/main/java/org/opendaylight/unimgr/command/UniUpdateCommand.java @@ -5,24 +5,19 @@ * 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.unimgr.command; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; +package org.opendaylight.unimgr.command; import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.unimgr.impl.UnimgrMapper; +import org.opendaylight.unimgr.api.AbstractCommand; import org.opendaylight.unimgr.utils.MdsalUtils; import org.opendaylight.unimgr.utils.OvsdbUtils; import org.opendaylight.unimgr.utils.UniUtils; -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.OvsdbNodeRef; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; -import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,100 +25,66 @@ import org.slf4j.LoggerFactory; import com.google.common.base.Optional; import com.google.common.base.Preconditions; -public class UniUpdateCommand extends AbstractUpdateCommand { +public class UniUpdateCommand extends AbstractCommand { private static final Logger LOG = LoggerFactory.getLogger(UniUpdateCommand.class); - public UniUpdateCommand(DataBroker dataBroker, - Map, DataObject> changes) { - super.dataBroker = dataBroker; - super.changes = changes; + public UniUpdateCommand(final DataBroker dataBroker, final DataTreeModification updatedUniNode) { + super(dataBroker, updatedUniNode); } @Override public void execute() { - for (Entry, DataObject> updated : changes.entrySet()) { - if (updated.getValue() != null && updated.getValue() instanceof UniAugmentation) { - final UniAugmentation updatedUni = (UniAugmentation) updated.getValue(); - final UniAugmentation formerUni = UniUtils.getUni(dataBroker, LogicalDatastoreType.OPERATIONAL, updatedUni.getIpAddress()); - - if (formerUni != null) { - String formerUniIp = formerUni.getIpAddress().getIpv4Address().getValue(); - String updatedUniIp = updatedUni.getIpAddress().getIpv4Address().getValue(); - String uniKey = updated.getKey().firstKeyOf(Node.class).toString(); - Preconditions.checkArgument(formerUniIp.equals(updatedUniIp), - "Can't update UNI with a different IP address. Former IP was %s" - + "Updated IP is %s. Please create a UNI instead of updating this one %s", - formerUniIp, updatedUniIp, uniKey); - Node ovsdbNode; - if (updatedUni.getOvsdbNodeRef() != null) { - LOG.info("OVSDB NODE ref retreive for updated UNI {}", updatedUni.getOvsdbNodeRef()); - final OvsdbNodeRef ovsdbNodeRef = updatedUni.getOvsdbNodeRef(); - Optional optOvsdbNode = MdsalUtils.readNode(dataBroker,LogicalDatastoreType.OPERATIONAL, ovsdbNodeRef.getValue()); - if(optOvsdbNode.isPresent()) { - ovsdbNode= optOvsdbNode.get(); - LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId()); - // Update QoS entries to ovsdb if speed is configured to UNI node - if (updatedUni.getSpeed() != null) { - OvsdbUtils.createQoSForOvsdbNode(dataBroker, updatedUni); - } - UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION, - updated.getKey(), - updatedUni, - ovsdbNode, - dataBroker); - } else { - // This should never happen, because on creation, - // the UNI is assigned and OVSDB node - LOG.error("OVSDB node not found for UNI {}, but got OVSDB ref", uniKey, updatedUni.getOvsdbNodeRef()); - return; - } - } else { - Optional optOvsdbNode = OvsdbUtils.findOvsdbNode(dataBroker, updatedUni); - if (optOvsdbNode.isPresent()) { - ovsdbNode = optOvsdbNode.get(); - LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId()); - // Update QoS entries to ovsdb if speed is configured to UNI node - if (updatedUni.getSpeed() != null) { - - OvsdbUtils.createQoSForOvsdbNode(dataBroker, updatedUni); - } - UniUtils.updateUniNode(LogicalDatastoreType.CONFIGURATION, - updated.getKey(), - updatedUni, - ovsdbNode, - dataBroker); - } else { - // This should never happen, because on creation, - // the UNI is assigned and OVSDB node - LOG.error("OVSDB node not found for UNI {}", uniKey); - return; - } + final UniAugmentation updatedUni = dataObject.getRootNode().getDataAfter().getAugmentation(UniAugmentation.class); + final UniAugmentation formerUni = dataObject.getRootNode().getDataBefore().getAugmentation(UniAugmentation.class); + if (formerUni != null) { + final String formerUniIp = formerUni.getIpAddress().getIpv4Address().getValue(); + final String updatedUniIp = updatedUni.getIpAddress().getIpv4Address().getValue(); + final InstanceIdentifier updatedUniIid = dataObject.getRootPath().getRootIdentifier(); + final String uniKey = updatedUniIid.firstKeyOf(Node.class).toString(); + Preconditions.checkArgument(formerUniIp.equals(updatedUniIp), + "Can't update UNI with a different IP address. Former IP was %s" + + "Updated IP is %s. Please create a UNI instead of updating this one %s", + formerUniIp, updatedUniIp, uniKey); + Node ovsdbNode; + if (updatedUni.getOvsdbNodeRef() != null) { + LOG.info("OVSDB NODE ref retreive for updated UNI {}", updatedUni.getOvsdbNodeRef()); + final OvsdbNodeRef ovsdbNodeRef = updatedUni.getOvsdbNodeRef(); + final Optional optOvsdbNode = MdsalUtils.readNode(dataBroker,LogicalDatastoreType.OPERATIONAL, ovsdbNodeRef.getValue()); + if(optOvsdbNode.isPresent()) { + ovsdbNode= optOvsdbNode.get(); + LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId()); + // Update QoS entries to ovsdb if speed is configured to UNI node + if (updatedUni.getSpeed() != null) { + OvsdbUtils.createQoSForOvsdbNode(dataBroker, updatedUni); } - LOG.info("UNI {} updated", uniKey); - - final InstanceIdentifier uniIID = UnimgrMapper.getUniIid(dataBroker, updatedUni.getIpAddress(), LogicalDatastoreType.OPERATIONAL); - MdsalUtils.deleteNode(dataBroker, uniIID, LogicalDatastoreType.OPERATIONAL); - UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniIID, updatedUni, ovsdbNode, dataBroker); + UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, updatedUniIid, updatedUni, + ovsdbNode, dataBroker); + } else { + // This should never happen, because on creation, + // the UNI is assigned and OVSDB node + LOG.error("OVSDB node not found for UNI {}, but got OVSDB ref", uniKey, updatedUni.getOvsdbNodeRef()); + return; } - } - if (updated.getValue() != null - && updated.getValue() instanceof OvsdbNodeAugmentation) { - OvsdbNodeAugmentation ovsdbNodeAugmentation = (OvsdbNodeAugmentation) updated.getValue(); - if (ovsdbNodeAugmentation != null) { - LOG.trace("Received an OVSDB node create {}", - ovsdbNodeAugmentation.getConnectionInfo() - .getRemoteIp().getIpv4Address().getValue()); - final List managedNodeEntries = ovsdbNodeAugmentation.getManagedNodeEntry(); - if (managedNodeEntries != null) { - for (ManagedNodeEntry managedNodeEntry : managedNodeEntries) { - LOG.trace("Received an update from an OVSDB node {}.", managedNodeEntry.getKey()); - // We received a node update from the southbound plugin - // so we have to check if it belongs to the UNI - } + } else { + final Optional optOvsdbNode = OvsdbUtils.findOvsdbNode(dataBroker, updatedUni); + if (optOvsdbNode.isPresent()) { + ovsdbNode = optOvsdbNode.get(); + LOG.info("Retrieved the OVSDB node {}", ovsdbNode.getNodeId()); + // Update QoS entries to ovsdb if speed is configured to UNI node + if (updatedUni.getSpeed() != null) { + OvsdbUtils.createQoSForOvsdbNode(dataBroker, updatedUni); } + UniUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, updatedUniIid, updatedUni, + ovsdbNode, dataBroker); + } else { + // This should never happen, because on creation, + // the UNI is assigned and OVSDB node + LOG.error("OVSDB node not found for UNI {}", uniKey); + return; } } + LOG.info("UNI {} updated", uniKey); } } } diff --git a/impl/src/main/java/org/opendaylight/unimgr/impl/EvcDataTreeChangeListener.java b/impl/src/main/java/org/opendaylight/unimgr/impl/EvcDataTreeChangeListener.java new file mode 100644 index 00000000..697be76b --- /dev/null +++ b/impl/src/main/java/org/opendaylight/unimgr/impl/EvcDataTreeChangeListener.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2016 CableLabs 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.unimgr.impl; + +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.unimgr.api.UnimgrDataTreeChangeListener; +import org.opendaylight.unimgr.command.EvcAddCommand; +import org.opendaylight.unimgr.command.EvcRemoveCommand; +import org.opendaylight.unimgr.command.EvcUpdateCommand; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link; +import org.opendaylight.yangtools.concepts.ListenerRegistration; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EvcDataTreeChangeListener extends UnimgrDataTreeChangeListener { + + private static final Logger LOG = LoggerFactory.getLogger(EvcDataTreeChangeListener.class); + private final ListenerRegistration listener; + + public EvcDataTreeChangeListener(final DataBroker dataBroker) { + super(dataBroker); + final DataTreeIdentifier dataTreeIid = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getEvcTopologyPath()); + listener = dataBroker.registerDataTreeChangeListener(dataTreeIid, this); + LOG.info("EvcDataTreeChangeListener created and registered"); + } + + @Override + public void add(final DataTreeModification newDataObject) { + if(newDataObject.getRootPath() != null && newDataObject.getRootNode() != null) { + LOG.info("evc link {} created", newDataObject.getRootNode().getIdentifier()); + final EvcAddCommand evcAddCmd = new EvcAddCommand(dataBroker, newDataObject); + evcAddCmd.execute(); + } + } + + @Override + public void close() throws Exception { + listener.close(); + } + + private InstanceIdentifier getEvcTopologyPath() { + final InstanceIdentifier evcPath = InstanceIdentifier + .create(NetworkTopology.class) + .child(Topology.class, new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID)) + .child(Link.class); + return evcPath; + } + + @Override + public void remove(final DataTreeModification removedDataObject) { + if(removedDataObject.getRootPath() != null && removedDataObject.getRootNode() != null) { + LOG.info("evc link {} deleted", removedDataObject.getRootNode().getIdentifier()); + final EvcRemoveCommand evcRemovedCmd = new EvcRemoveCommand(dataBroker, removedDataObject); + evcRemovedCmd.execute(); + } + } + + @Override + public void update(final DataTreeModification modifiedDataObject) { + if(modifiedDataObject.getRootPath() != null && modifiedDataObject.getRootNode() != null) { + LOG.info("evc link {} updated", modifiedDataObject.getRootNode().getIdentifier()); + final EvcUpdateCommand evcUpdateCmd = new EvcUpdateCommand(dataBroker, modifiedDataObject); + evcUpdateCmd.execute(); + } + } +} diff --git a/impl/src/main/java/org/opendaylight/unimgr/impl/OvsNodeDataTreeChangeListener.java b/impl/src/main/java/org/opendaylight/unimgr/impl/OvsNodeDataTreeChangeListener.java new file mode 100644 index 00000000..57b0d49f --- /dev/null +++ b/impl/src/main/java/org/opendaylight/unimgr/impl/OvsNodeDataTreeChangeListener.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2016 CableLabs 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.unimgr.impl; + +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.unimgr.api.UnimgrDataTreeChangeListener; +import org.opendaylight.unimgr.command.OvsNodeAddCommand; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; +import org.opendaylight.yangtools.concepts.ListenerRegistration; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class OvsNodeDataTreeChangeListener extends UnimgrDataTreeChangeListener { + + private static final Logger LOG = LoggerFactory.getLogger(OvsNodeDataTreeChangeListener.class); + private final ListenerRegistration listener; + + public OvsNodeDataTreeChangeListener(final DataBroker dataBroker) { + super(dataBroker); + final InstanceIdentifier nodePath = getOvsNodeTopologyPath(); + final DataTreeIdentifier dataTreeIid = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, nodePath); + listener = dataBroker.registerDataTreeChangeListener(dataTreeIid, this); + LOG.info("ovsNodeDataTreeChangeListener created and registered"); + } + + @Override + public void add(final DataTreeModification newDataObject) { + if (newDataObject.getRootPath() != null && newDataObject.getRootNode() != null) { + LOG.info("ovs node {} created", newDataObject.getRootNode().getIdentifier()); + final OvsNodeAddCommand ovsNodeAddCmd = new OvsNodeAddCommand(dataBroker, newDataObject); + ovsNodeAddCmd.execute(); + } + } + + @Override + public void close() throws Exception { + listener.close(); + } + + private InstanceIdentifier getOvsNodeTopologyPath() { + final InstanceIdentifier topoPath = InstanceIdentifier + .create(NetworkTopology.class) + .child(Topology.class, + new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID)) + .child(Node.class); + return topoPath; + } + + @Override + public void remove(final DataTreeModification removedDataObject) { + // TODO Auto-generated method stub + } + + @Override + public void update(final DataTreeModification modifiedDataObject) { + // TODO Auto-generated method stub + } + +} diff --git a/impl/src/main/java/org/opendaylight/unimgr/impl/UniDataTreeChangeListener.java b/impl/src/main/java/org/opendaylight/unimgr/impl/UniDataTreeChangeListener.java new file mode 100644 index 00000000..cff24964 --- /dev/null +++ b/impl/src/main/java/org/opendaylight/unimgr/impl/UniDataTreeChangeListener.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2016 CableLabs 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.unimgr.impl; + +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.unimgr.api.UnimgrDataTreeChangeListener; +import org.opendaylight.unimgr.command.UniAddCommand; +import org.opendaylight.unimgr.command.UniRemoveCommand; +import org.opendaylight.unimgr.command.UniUpdateCommand; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; +import org.opendaylight.yangtools.concepts.ListenerRegistration; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UniDataTreeChangeListener extends UnimgrDataTreeChangeListener { + + private static final Logger LOG = LoggerFactory.getLogger(UniDataTreeChangeListener.class); + private final ListenerRegistration listener; + + + public UniDataTreeChangeListener(final DataBroker dataBroker) { + super(dataBroker); + final InstanceIdentifier uniPath = getUniTopologyPath(); + final DataTreeIdentifier dataTreeIid = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, uniPath); + listener = dataBroker.registerDataTreeChangeListener(dataTreeIid, this); + LOG.info("UniDataTreeChangeListener created and registered"); + } + + @Override + public void add(final DataTreeModification newDataObject) { + if (newDataObject.getRootPath() != null && newDataObject.getRootNode() != null) { + LOG.info("uni node {} created", newDataObject.getRootNode().getIdentifier()); + final UniAddCommand uniAddCmd = new UniAddCommand(dataBroker, newDataObject); + uniAddCmd.execute(); + } + } + + @Override + public void close() throws Exception { + listener.close(); + } + + private InstanceIdentifier getUniTopologyPath() { + final InstanceIdentifier nodePath = InstanceIdentifier + .create(NetworkTopology.class) + .child(Topology.class, + new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID)) + .child(Node.class); + return nodePath; + } + + @Override + public void remove(final DataTreeModification removedDataObject) { + if (removedDataObject.getRootPath() != null && removedDataObject.getRootNode() != null) { + LOG.info("uni node {} deleted", removedDataObject.getRootNode().getIdentifier()); + final UniRemoveCommand uniRemoveCmd = new UniRemoveCommand(dataBroker, removedDataObject); + uniRemoveCmd.execute(); + } + } + + @Override + public void update(final DataTreeModification modifiedDataObject) { + if (modifiedDataObject.getRootPath() != null && modifiedDataObject.getRootNode() != null) { + LOG.info("uni node {} created", modifiedDataObject.getRootNode().getIdentifier()); + final UniUpdateCommand uniUpdateCmd = new UniUpdateCommand(dataBroker, modifiedDataObject); + uniUpdateCmd.execute(); + } + } + +} diff --git a/impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrDataChangeListener.java b/impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrDataChangeListener.java deleted file mode 100644 index 559ac6c2..00000000 --- a/impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrDataChangeListener.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (c) 2015 CableLabs 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, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.unimgr.impl; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.binding.api.DataChangeListener; -import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope; -import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.unimgr.api.IUnimgrDataChangeListener; -import org.opendaylight.unimgr.command.Command; -import org.opendaylight.unimgr.command.EvcCreateCommand; -import org.opendaylight.unimgr.command.EvcDeleteCommand; -import org.opendaylight.unimgr.command.EvcUpdateCommand; -import org.opendaylight.unimgr.command.TransactionInvoker; -import org.opendaylight.unimgr.command.UniCreateCommand; -import org.opendaylight.unimgr.command.UniDeleteCommand; -import org.opendaylight.unimgr.command.UniUpdateCommand; -import org.opendaylight.yangtools.concepts.ListenerRegistration; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class UnimgrDataChangeListener implements IUnimgrDataChangeListener { - - private static final Logger LOG = LoggerFactory.getLogger(UnimgrDataChangeListener.class); - private Set> listeners; - private DataBroker dataBroker; - private TransactionInvoker invoker; - - public UnimgrDataChangeListener(DataBroker dataBroker, TransactionInvoker invoker) { - this.dataBroker = dataBroker; - this.invoker = invoker; - listeners = new HashSet>(); - listeners.add(dataBroker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, - UnimgrMapper.getUniTopologyIid(), - this, - DataChangeScope.SUBTREE)); - listeners.add(dataBroker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, - UnimgrMapper.getEvcTopologyIid(), - this, - DataChangeScope.SUBTREE)); - listeners.add(dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, - UnimgrMapper.getOvsdbTopologyIid(), - this, - DataChangeScope.SUBTREE)); - LOG.info("UnimgrDataChangeListener initialized"); - } - - @Override - public void onDataChanged(AsyncDataChangeEvent, - DataObject> change) { - create(change.getCreatedData()); - update(change.getUpdatedData()); - delete(change); - } - - @Override - public void create(Map, - DataObject> changes) { - if (changes != null) { - List commands = new ArrayList(); - Command uniCreate = new UniCreateCommand(dataBroker, changes); - Command evcCreate = new EvcCreateCommand(dataBroker, changes); - commands.add(uniCreate); - commands.add(evcCreate); - invoker.setCommands(commands); - invoker.invoke(); - } - } - - @Override - public void update(Map, - DataObject> changes) { - if (changes != null) { - List commands = new ArrayList(); - Command uniUpdate = new UniUpdateCommand(dataBroker, changes); - Command evcUpdate = new EvcUpdateCommand(dataBroker, changes); - commands.add(uniUpdate); - commands.add(evcUpdate); - invoker.setCommands(commands); - invoker.invoke(); - } - } - - @Override - public void delete(AsyncDataChangeEvent, - DataObject> changes) { - if (changes != null) { - List commands = new ArrayList(); - Command uniDelete = new UniDeleteCommand(dataBroker, changes); - Command evcDelete = new EvcDeleteCommand(dataBroker, changes); - commands.add(uniDelete); - commands.add(evcDelete); - invoker.setCommands(commands); - invoker.invoke(); - } - } - - @Override - public void close() throws Exception { - LOG.info("UnimgrDataChangeListener stopped."); - for (ListenerRegistration listener : listeners) { - if (listener != null) { - listener.close(); - } - } - } -} diff --git a/impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrProvider.java b/impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrProvider.java index 9e0c7ace..fd970124 100755 --- a/impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrProvider.java +++ b/impl/src/main/java/org/opendaylight/unimgr/impl/UnimgrProvider.java @@ -7,7 +7,6 @@ */ package org.opendaylight.unimgr.impl; - import java.util.List; import org.opendaylight.controller.md.sal.binding.api.DataBroker; @@ -17,7 +16,6 @@ import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext; import org.opendaylight.controller.sal.binding.api.BindingAwareProvider; import org.opendaylight.unimgr.api.IUnimgrConsoleProvider; -import org.opendaylight.unimgr.command.TransactionInvoker; import org.opendaylight.unimgr.utils.EvcUtils; import org.opendaylight.unimgr.utils.MdsalUtils; import org.opendaylight.unimgr.utils.OvsdbUtils; @@ -50,11 +48,10 @@ import com.google.common.util.concurrent.CheckedFuture; public class UnimgrProvider implements BindingAwareProvider, AutoCloseable, IUnimgrConsoleProvider { private static final Logger LOG = LoggerFactory.getLogger(UnimgrProvider.class); - - private UnimgrDataChangeListener listener; - private TransactionInvoker invoker; - private DataBroker dataBroker; + private EvcDataTreeChangeListener evcListener; + private OvsNodeDataTreeChangeListener ovsListener; + private UniDataTreeChangeListener uniListener; private ServiceRegistration unimgrConsoleRegistration; public UnimgrProvider() { @@ -62,42 +59,41 @@ public class UnimgrProvider implements BindingAwareProvider, AutoCloseable, IUni } @Override - public void onSessionInitiated(ProviderContext session) { - LOG.info("UnimgrProvider Session Initiated"); - - // Retrieve the data broker to create transactions - dataBroker = session.getSALService(DataBroker.class); - invoker = new TransactionInvoker(); - - // Register the unimgr OSGi CLI - final BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); - unimgrConsoleRegistration = context.registerService(IUnimgrConsoleProvider.class, - this, - null); - - // Register the uni data change listener - listener = new UnimgrDataChangeListener(dataBroker, invoker); + public boolean addEvc(final EvcAugmentation evc) { + // TODO Auto-generated method stub + return false; + } - // Initialize operational and default config data in MD-SAL data store - initDatastore(LogicalDatastoreType.CONFIGURATION, - UnimgrConstants.UNI_TOPOLOGY_ID); - initDatastore(LogicalDatastoreType.OPERATIONAL, - UnimgrConstants.UNI_TOPOLOGY_ID); - initDatastore(LogicalDatastoreType.CONFIGURATION, - UnimgrConstants.EVC_TOPOLOGY_ID); - initDatastore(LogicalDatastoreType.OPERATIONAL, - UnimgrConstants.EVC_TOPOLOGY_ID); + @Override + public boolean addUni(final UniAugmentation uniAug) { + if ((uniAug == null) || (uniAug.getIpAddress() == null) || (uniAug.getMacAddress() == null)) { + return false; + } + return UniUtils.createUniNode(dataBroker, uniAug); } @Override public void close() throws Exception { LOG.info("UnimgrProvider Closed"); unimgrConsoleRegistration.unregister(); - listener.close(); + uniListener.close(); + evcListener.close(); + ovsListener.close(); + } + + @Override + public Evc getEvc(final String uuid) { + // TODO Auto-generated method stub + return null; + } + + @Override + public UniAugmentation getUni(final IpAddress ipAddress) { + return UniUtils.getUni(dataBroker, LogicalDatastoreType.OPERATIONAL, ipAddress); } protected void initDatastore(final LogicalDatastoreType type, - TopologyId topoId) { + final TopologyId topoId) { final InstanceIdentifier path = InstanceIdentifier .create(NetworkTopology.class) .child(Topology.class, @@ -120,7 +116,7 @@ public class UnimgrProvider implements BindingAwareProvider, AutoCloseable, IUni } } - private void initializeTopology(LogicalDatastoreType type) { + private void initializeTopology(final LogicalDatastoreType type) { final ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction(); final InstanceIdentifier path = InstanceIdentifier.create(NetworkTopology.class); final CheckedFuture, ReadFailedException> topology = transaction.read(type,path); @@ -138,55 +134,54 @@ public class UnimgrProvider implements BindingAwareProvider, AutoCloseable, IUni } @Override - public boolean addUni(UniAugmentation uniAug) { - if ((uniAug == null) || (uniAug.getIpAddress() == null) || (uniAug.getMacAddress() == null)) { - return false; - } - return UniUtils.createUniNode(dataBroker, uniAug); - } - - @Override - public boolean removeUni(IpAddress ipAddress) { - final InstanceIdentifier iidUni = UnimgrMapper.getUniIid(dataBroker, ipAddress, LogicalDatastoreType.CONFIGURATION); - if (iidUni == null) { - return false; - } - - return MdsalUtils.deleteNode(dataBroker, iidUni, LogicalDatastoreType.CONFIGURATION); - } - - @Override - public List listUnis(LogicalDatastoreType dataStoreType) { + public List listUnis(final LogicalDatastoreType dataStoreType) { return UniUtils.getUnis(dataBroker, dataStoreType); } @Override - public UniAugmentation getUni(IpAddress ipAddress) { - return UniUtils.getUni(dataBroker, LogicalDatastoreType.OPERATIONAL, ipAddress); - } + public void onSessionInitiated(final ProviderContext session) { + LOG.info("UnimgrProvider Session Initiated"); + + // Retrieve the data broker to create transactions + dataBroker = session.getSALService(DataBroker.class); + // Register the unimgr OSGi CLI + final BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); + unimgrConsoleRegistration = context.registerService(IUnimgrConsoleProvider.class,this, null); + // Register the data trees change listener + uniListener = new UniDataTreeChangeListener(dataBroker); + evcListener = new EvcDataTreeChangeListener(dataBroker); + ovsListener = new OvsNodeDataTreeChangeListener(dataBroker); - @Override - public boolean removeEvc(String uuid) { - // TODO Auto-generated method stub - return false; + // Initialize operational and default config data in MD-SAL data store + initDatastore(LogicalDatastoreType.CONFIGURATION, + UnimgrConstants.UNI_TOPOLOGY_ID); + initDatastore(LogicalDatastoreType.OPERATIONAL, + UnimgrConstants.UNI_TOPOLOGY_ID); + initDatastore(LogicalDatastoreType.CONFIGURATION, + UnimgrConstants.EVC_TOPOLOGY_ID); + initDatastore(LogicalDatastoreType.OPERATIONAL, + UnimgrConstants.EVC_TOPOLOGY_ID); } @Override - public boolean addEvc(EvcAugmentation evc) { + public boolean removeEvc(final String uuid) { // TODO Auto-generated method stub return false; } @Override - public Evc getEvc(String uuid) { - // TODO Auto-generated method stub - return null; + public boolean removeUni(final IpAddress ipAddress) { + final InstanceIdentifier iidUni = UnimgrMapper.getUniIid(dataBroker, ipAddress, LogicalDatastoreType.CONFIGURATION); + if (iidUni == null) { + return false; + } + return MdsalUtils.deleteNode(dataBroker, iidUni, LogicalDatastoreType.CONFIGURATION); } @Override - public boolean updateEvc(InstanceIdentifier evcKey, EvcAugmentation evc, UniSource uniSource, - UniDest uniDest) { + public boolean updateEvc(final InstanceIdentifier evcKey, final EvcAugmentation evc, final UniSource uniSource, + final UniDest uniDest) { final InstanceIdentifier sourceUniIid = uniSource.getUni(); final InstanceIdentifier destinationUniIid = uniDest.getUni(); return EvcUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION, evcKey, evc, sourceUniIid, @@ -194,7 +189,7 @@ public class UnimgrProvider implements BindingAwareProvider, AutoCloseable, IUni } @Override - public boolean updateUni(UniAugmentation uni) { + public boolean updateUni(final UniAugmentation uni) { // Remove the old UNI with IpAdress and create a new one with updated informations if (uni != null) { LOG.trace("UNI updated {}.", uni.getIpAddress().getIpv4Address()); diff --git a/impl/src/main/java/org/opendaylight/unimgr/utils/EvcUtils.java b/impl/src/main/java/org/opendaylight/unimgr/utils/EvcUtils.java index 41f1be62..8fae6430 100644 --- a/impl/src/main/java/org/opendaylight/unimgr/utils/EvcUtils.java +++ b/impl/src/main/java/org/opendaylight/unimgr/utils/EvcUtils.java @@ -43,16 +43,12 @@ public class EvcUtils { private static final Logger LOG = LoggerFactory.getLogger(EvcUtils.class); - private EvcUtils() { - throw new AssertionError("Instantiating utility class."); - } - /** * Delete EVC data from configuration datastore * @param dataBroker The dataBroker instance to create transactions * @param optionalUni Optional Uni Node */ - public static void deleteEvcData(DataBroker dataBroker, Optional optionalUni) { + public static void deleteEvcData(final DataBroker dataBroker, final Optional optionalUni) { if (optionalUni.isPresent()) { final UniAugmentation uniAugmentation = optionalUni @@ -97,7 +93,7 @@ public class EvcUtils { * @param dataBroker The dataBroker instance to create transactions * @return A list of Links retrieved from the Operational DataStore */ - public static List getEvcLinks(DataBroker dataBroker) { + public static List getEvcLinks(final DataBroker dataBroker) { final List evcLinks = new ArrayList<>(); final InstanceIdentifier evcTopology = UnimgrMapper.getEvcTopologyIid(); final Topology topology = MdsalUtils.read(dataBroker, @@ -124,18 +120,19 @@ public class EvcUtils { * @param dataBroker The dataBroker instance to create transactions * @return true if evc is updated */ - public static boolean updateEvcNode(LogicalDatastoreType dataStore, - InstanceIdentifier evcKey, - EvcAugmentation evcAugmentation, - InstanceIdentifier sourceUniIid, - InstanceIdentifier destinationUniIid, - DataBroker dataBroker) { + public static boolean updateEvcNode(final LogicalDatastoreType dataStore, + final InstanceIdentifier evcKey, + final EvcAugmentation evcAugmentation, + final InstanceIdentifier sourceUniIid, + final InstanceIdentifier destinationUniIid, + final DataBroker dataBroker) { final EvcAugmentationBuilder updatedEvcBuilder = new EvcAugmentationBuilder(evcAugmentation); if ((sourceUniIid != null) && (destinationUniIid != null)) { final List sourceList = new ArrayList(); - final UniSourceKey sourceKey = evcAugmentation.getUniSource().iterator().next().getKey(); - final short sourceOrder = evcAugmentation.getUniSource().iterator().next().getOrder(); - final IpAddress sourceIp = evcAugmentation.getUniSource().iterator().next().getIpAddress(); + final UniSource evcUniSource = evcAugmentation.getUniSource().iterator().next(); + final UniSourceKey sourceKey = evcUniSource.getKey(); + final short sourceOrder = evcUniSource.getOrder(); + final IpAddress sourceIp = evcUniSource.getIpAddress(); final UniSource uniSource = new UniSourceBuilder() .setOrder(sourceOrder) .setKey(sourceKey) @@ -146,9 +143,10 @@ public class EvcUtils { updatedEvcBuilder.setUniSource(sourceList); final List destinationList = new ArrayList(); - final UniDestKey destKey = evcAugmentation.getUniDest().iterator().next().getKey(); - final short destOrder = evcAugmentation.getUniDest().iterator().next().getOrder(); - final IpAddress destIp = evcAugmentation.getUniDest().iterator().next().getIpAddress(); + final UniDest evcUniDest = evcAugmentation.getUniDest().iterator().next(); + final UniDestKey destKey = evcUniDest.getKey(); + final short destOrder = evcUniDest.getOrder(); + final IpAddress destIp = evcUniDest.getIpAddress(); final UniDest uniDest = new UniDestBuilder() .setIpAddress(destIp) .setOrder(destOrder) @@ -180,4 +178,8 @@ public class EvcUtils { } return false; } + + private EvcUtils() { + throw new AssertionError("Instantiating utility class."); + } } diff --git a/impl/src/main/java/org/opendaylight/unimgr/utils/OvsdbUtils.java b/impl/src/main/java/org/opendaylight/unimgr/utils/OvsdbUtils.java index aecd3cb8..207a8d4e 100644 --- a/impl/src/main/java/org/opendaylight/unimgr/utils/OvsdbUtils.java +++ b/impl/src/main/java/org/opendaylight/unimgr/utils/OvsdbUtils.java @@ -899,7 +899,7 @@ public class OvsdbUtils { public static List getOvsdbNodes(DataBroker dataBroker) { final List ovsdbNodes = new ArrayList<>(); final InstanceIdentifier ovsdbTopoIdentifier = UnimgrMapper.getOvsdbTopologyIid(); - final Topology topology = MdsalUtils.read(dataBroker, + Topology topology = MdsalUtils.read(dataBroker, LogicalDatastoreType.OPERATIONAL, ovsdbTopoIdentifier); if ((topology != null) && (topology.getNode() != null)) { @@ -909,6 +909,16 @@ public class OvsdbUtils { ovsdbNodes.add(node); } } + } else { + topology = MdsalUtils.read(dataBroker, LogicalDatastoreType.CONFIGURATION, ovsdbTopoIdentifier); + if ((topology != null) && (topology.getNode() != null)) { + for (final Node node : topology.getNode()) { + final OvsdbNodeAugmentation ovsdbNodeAugmentation = node.getAugmentation(OvsdbNodeAugmentation.class); + if (ovsdbNodeAugmentation != null) { + ovsdbNodes.add(node); + } + } + } } return ovsdbNodes; } diff --git a/impl/src/test/java/org/opendaylight/unimgr/command/DataTreeModificationHelper.java b/impl/src/test/java/org/opendaylight/unimgr/command/DataTreeModificationHelper.java new file mode 100644 index 00000000..d0dc2cc7 --- /dev/null +++ b/impl/src/test/java/org/opendaylight/unimgr/command/DataTreeModificationHelper.java @@ -0,0 +1,194 @@ +/* + * Copyright (c) 2016 CableLabs 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.unimgr.command; + +import java.util.Collection; + +import org.opendaylight.controller.md.sal.binding.api.DataObjectModification; +import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey; +import org.opendaylight.yangtools.yang.binding.Augmentation; +import org.opendaylight.yangtools.yang.binding.ChildOf; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.Identifiable; +import org.opendaylight.yangtools.yang.binding.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument; + +/** + * DataTreeModificationHelper help to get a dummy value of DataTreeModification object as DataTreeModification + * class is a final class. + */ +public final class DataTreeModificationHelper { + + public static final DataTreeModification getUniNode(final Node node) { + final DataTreeIdentifier uniDataTreeIid = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getUniIid("10.10.1.3")); + final DataObjectModification uniDataTreeObj = new DataObjectModification() { + @Override + public Collection> getModifiedChildren() { + // TODO Auto-generated method stub + return null; + } + @Override + public & ChildOf, K extends Identifier> DataObjectModification getModifiedChildListItem( + Class arg0, K arg1) { + // TODO Auto-generated method stub + return null; + } + @Override + public > DataObjectModification getModifiedChildContainer(Class arg0) { + // TODO Auto-generated method stub + return null; + } + @Override + public DataObjectModification getModifiedChild(PathArgument arg0) { + // TODO Auto-generated method stub + return null; + } + @Override + public & DataObject> DataObjectModification getModifiedAugmentation( + Class arg0) { + // TODO Auto-generated method stub + return null; + } + @Override + public ModificationType getModificationType() { + // TODO Auto-generated method stub + return null; + } + @Override + public PathArgument getIdentifier() { + // TODO Auto-generated method stub + return null; + } + @Override + public Class getDataType() { + // TODO Auto-generated method stub + return null; + } + @Override + public Node getDataBefore() { + return node; + } + @Override + public Node getDataAfter() { + return node; + } + }; + DataTreeModification uniNode = new DataTreeModification() { + @Override + public DataTreeIdentifier getRootPath() { + return uniDataTreeIid; + } + @Override + public DataObjectModification getRootNode() { + return uniDataTreeObj; + } + }; + return uniNode; + } + + private static final InstanceIdentifier getUniIid(String nodeId) { + NodeId uniNodeId = new NodeId(new NodeId(nodeId)); + InstanceIdentifier uniNodeIid = InstanceIdentifier + .create(NetworkTopology.class) + .child(Topology.class, new TopologyKey(new TopologyId(new Uri("unimgr:uni")))) + .child(Node.class, new NodeKey(uniNodeId)); + return uniNodeIid; + } + + public static final DataTreeModification getEvcLink(final Link link) { + final DataTreeIdentifier evcDataTreeIid = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getEvcLinkIid("1")); + final DataObjectModification evcDataTreeObj = new DataObjectModification() { + @Override + public Collection> getModifiedChildren() { + // TODO Auto-generated method stub + return null; + } + @Override + public & ChildOf, K extends Identifier> DataObjectModification getModifiedChildListItem( + Class arg0, K arg1) { + // TODO Auto-generated method stub + return null; + } + @Override + public > DataObjectModification getModifiedChildContainer(Class arg0) { + // TODO Auto-generated method stub + return null; + } + @Override + public DataObjectModification getModifiedChild(PathArgument arg0) { + // TODO Auto-generated method stub + return null; + } + @Override + public & DataObject> DataObjectModification getModifiedAugmentation( + Class arg0) { + // TODO Auto-generated method stub + return null; + } + @Override + public ModificationType getModificationType() { + // TODO Auto-generated method stub + return null; + } + @Override + public PathArgument getIdentifier() { + // TODO Auto-generated method stub + return null; + } + @Override + public Class getDataType() { + // TODO Auto-generated method stub + return null; + } + @Override + public Link getDataBefore() { + return link; + } + @Override + public Link getDataAfter() { + return link; + } + }; + DataTreeModification evcLink = new DataTreeModification() { + + @Override + public DataTreeIdentifier getRootPath() { + return evcDataTreeIid; + } + + @Override + public DataObjectModification getRootNode() { + return evcDataTreeObj; + } + }; + return evcLink; + } + + private static InstanceIdentifier getEvcLinkIid(String linkId) { + LinkId evcLinkId = new LinkId(new LinkId("evc://" + linkId)); + InstanceIdentifier linkPath = InstanceIdentifier + .create(NetworkTopology.class) + .child(Topology.class,new TopologyKey(new TopologyId(new Uri("unimgr:evc")))) + .child(Link.class, new LinkKey(evcLinkId)); + return linkPath; + } +} diff --git a/impl/src/test/java/org/opendaylight/unimgr/command/EvcCreateCommandTest.java b/impl/src/test/java/org/opendaylight/unimgr/command/EvcAddCommandTest.java similarity index 85% rename from impl/src/test/java/org/opendaylight/unimgr/command/EvcCreateCommandTest.java rename to impl/src/test/java/org/opendaylight/unimgr/command/EvcAddCommandTest.java index 779973e4..9d6c4f6e 100644 --- a/impl/src/test/java/org/opendaylight/unimgr/command/EvcCreateCommandTest.java +++ b/impl/src/test/java/org/opendaylight/unimgr/command/EvcAddCommandTest.java @@ -13,23 +13,19 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.unimgr.impl.UnimgrConstants; import org.opendaylight.unimgr.impl.UnimgrMapper; import org.opendaylight.unimgr.utils.EvcUtils; import org.opendaylight.unimgr.utils.MdsalUtils; import org.opendaylight.unimgr.utils.OvsdbUtils; -import org.opendaylight.unimgr.utils.UniUtils; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef; @@ -41,9 +37,9 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology. import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey; -import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -53,13 +49,15 @@ import com.google.common.base.Optional; @RunWith(PowerMockRunner.class) @PrepareForTest({EvcUtils.class, MdsalUtils.class, OvsdbUtils.class, UnimgrMapper.class}) -public class EvcCreateCommandTest { +public class EvcAddCommandTest { private static final NodeId OVSDB_NODE_ID = new NodeId("ovsdb://7011db35-f44b-4aab-90f6-d89088caf9d8"); - private EvcCreateCommand evcCreateCommand; - private Map, DataObject> changes; + private EvcAddCommand evcAddCommand; + private DataTreeModification evcLink; private DataBroker dataBroker; + private Link link; + private Optional optLinks; @SuppressWarnings("unchecked") @Before @@ -68,13 +66,15 @@ public class EvcCreateCommandTest { PowerMockito.mockStatic(EvcUtils.class); PowerMockito.mockStatic(OvsdbUtils.class); PowerMockito.mockStatic(UnimgrMapper.class); - changes = mock(Map.class); dataBroker = mock(DataBroker.class); - evcCreateCommand = new EvcCreateCommand(dataBroker, changes); + link = mock(Link.class); + optLinks = mock(Optional.class); + evcLink = DataTreeModificationHelper.getEvcLink(link); + evcAddCommand = new EvcAddCommand(dataBroker, evcLink); } /** - * Test method for {@link org.opendaylight.unimgr.command.EvcCreateCommand#execute()}. + * Test method for {@link org.opendaylight.unimgr.command.evcAddCommand#execute()}. * @throws Exception */ @SuppressWarnings({ "rawtypes", "unchecked" }) @@ -95,21 +95,11 @@ public class EvcCreateCommandTest { .create(NetworkTopology.class) .child(Topology.class, new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID)) .child(Node.class, new NodeKey(OVSDB_NODE_ID)); - final Set, DataObject>> setCreated = new HashSet,DataObject>>(); - final Entry, DataObject> created = new Entry, DataObject>() { - @Override - public DataObject setValue(DataObject value) { return null; } - @Override - public DataObject getValue() { return evcAugmentation; } - @Override - public InstanceIdentifier getKey() { return evcKey; } - }; - - setCreated.add(created); unisSource.add(uniSource); unisDest.add(uniDest); - when(changes.entrySet()).thenReturn(setCreated); + when(link.getAugmentation(EvcAugmentation.class)).thenReturn(evcAugmentation); + when(optLinks.isPresent()).thenReturn(false); when(uniAugmentation.getOvsdbNodeRef()).thenReturn(ovsNodedRef); when(evcAugmentation.getUniSource()).thenReturn(unisSource); when(evcAugmentation.getUniDest()).thenReturn(unisDest); @@ -125,6 +115,8 @@ public class EvcCreateCommandTest { when(node.getAugmentation(any(Class.class))).thenReturn(uniAugmentation); when(ovsNodedRef.getValue()).thenReturn(evcKey); + when(MdsalUtils.readLink(any(DataBroker.class), any(LogicalDatastoreType.class), + any(InstanceIdentifier.class))).thenReturn(optLinks); when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(optionalOvsdbNode); PowerMockito.doNothing().when(OvsdbUtils.class, "updateMaxRate", dataBroker, uniAugmentation, @@ -139,7 +131,7 @@ public class EvcCreateCommandTest { when(UnimgrMapper.getUniIid(any(DataBroker.class), any(IpAddress.class), any(LogicalDatastoreType.class))).thenReturn(evcKey); - evcCreateCommand.execute(); + evcAddCommand.execute(); PowerMockito.verifyStatic(times(2)); OvsdbUtils.createTerminationPointNode(any(DataBroker.class), any(UniAugmentation.class), any(Node.class), any(String.class), any(String.class)); @@ -150,7 +142,6 @@ public class EvcCreateCommandTest { EvcUtils.updateEvcNode(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(EvcAugmentation.class), any(InstanceIdentifier.class), any(InstanceIdentifier.class), any(DataBroker.class)); - } } diff --git a/impl/src/test/java/org/opendaylight/unimgr/command/EvcDeleteCommandTest.java b/impl/src/test/java/org/opendaylight/unimgr/command/EvcRemoveCommandTest.java similarity index 79% rename from impl/src/test/java/org/opendaylight/unimgr/command/EvcDeleteCommandTest.java rename to impl/src/test/java/org/opendaylight/unimgr/command/EvcRemoveCommandTest.java index daabff7c..98a86d86 100644 --- a/impl/src/test/java/org/opendaylight/unimgr/command/EvcDeleteCommandTest.java +++ b/impl/src/test/java/org/opendaylight/unimgr/command/EvcRemoveCommandTest.java @@ -13,15 +13,12 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.unimgr.impl.UnimgrMapper; import org.opendaylight.unimgr.utils.EvcUtils; @@ -30,8 +27,8 @@ import org.opendaylight.unimgr.utils.OvsdbUtils; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; -import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -41,10 +38,11 @@ import com.google.common.base.Optional; @RunWith(PowerMockRunner.class) @PrepareForTest({EvcUtils.class, MdsalUtils.class, OvsdbUtils.class, UnimgrMapper.class}) -public class EvcDeleteCommandTest { +public class EvcRemoveCommandTest { - private EvcDeleteCommand evcDeleteCommand; - private AsyncDataChangeEvent, DataObject> changes; + private EvcRemoveCommand evcRemoveCommand; + private DataTreeModification evcLink; + private Link link; private DataBroker dataBroker; @SuppressWarnings("unchecked") @@ -54,19 +52,18 @@ public class EvcDeleteCommandTest { PowerMockito.mockStatic(EvcUtils.class); PowerMockito.mockStatic(OvsdbUtils.class); PowerMockito.mockStatic(UnimgrMapper.class); - changes = mock(AsyncDataChangeEvent.class); dataBroker = mock(DataBroker.class); - evcDeleteCommand = new EvcDeleteCommand(dataBroker, changes); + link = mock(Link.class); + evcLink = DataTreeModificationHelper.getEvcLink(link); + evcRemoveCommand = new EvcRemoveCommand(dataBroker, evcLink); } /** - * Test method for {@link org.opendaylight.unimgr.command.EvcDeleteCommand#EvcDeleteCommand(org.opendaylight.controller.md.sal.binding.api.DataBroker, org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent)}. + * Test method for {@link org.opendaylight.unimgr.command.EvcRemoveCommand#execute(). * @throws Exception */ @SuppressWarnings({ "unchecked", "rawtypes" }) @Test - public void testEvcDeleteCommand() throws Exception { - final Set> removedEvcs = - new HashSet>(); + public void testEvcRemoveCommand() throws Exception { final List unisSource = new ArrayList(); final UniSource uniSource = mock(UniSource.class); final List unisDest = new ArrayList(); @@ -74,16 +71,14 @@ public class EvcDeleteCommandTest { final EvcAugmentation evcAugmentation = mock(EvcAugmentation.class); final Optional optionalNode = mock(Optional.class); final InstanceIdentifier instanceOfNode = mock(InstanceIdentifier.class); - removedEvcs.add(mock(InstanceIdentifier.class)); unisSource.add(uniSource); unisDest.add(uniDest); + when(link.getAugmentation(EvcAugmentation.class)).thenReturn(evcAugmentation); when(evcAugmentation.getUniSource()).thenReturn(unisSource); when(evcAugmentation.getUniDest()).thenReturn(unisDest); when(uniSource.getUni()).thenReturn(instanceOfNode); when(uniDest.getUni()).thenReturn(instanceOfNode); - when(OvsdbUtils.extractRemoved(any(AsyncDataChangeEvent.class), any(Class.class))) - .thenReturn(removedEvcs); when(MdsalUtils.read(any(DataBroker.class), any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(evcAugmentation); when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class), @@ -93,7 +88,7 @@ public class EvcDeleteCommandTest { when(MdsalUtils.deleteNode(any(DataBroker.class), any(InstanceIdentifier.class), any(LogicalDatastoreType.class))).thenReturn(true); - evcDeleteCommand.execute(); + evcRemoveCommand.execute(); PowerMockito.verifyStatic(times(2)); EvcUtils.deleteEvcData(any(DataBroker.class), any(Optional.class)); PowerMockito.verifyStatic(times(1)); @@ -101,4 +96,4 @@ public class EvcDeleteCommandTest { any(LogicalDatastoreType.class)); } -} +} \ No newline at end of file diff --git a/impl/src/test/java/org/opendaylight/unimgr/command/EvcUpdateCommandTest.java b/impl/src/test/java/org/opendaylight/unimgr/command/EvcUpdateCommandTest.java index 758faec1..e68f7bcb 100644 --- a/impl/src/test/java/org/opendaylight/unimgr/command/EvcUpdateCommandTest.java +++ b/impl/src/test/java/org/opendaylight/unimgr/command/EvcUpdateCommandTest.java @@ -13,20 +13,15 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; import org.opendaylight.controller.md.sal.binding.api.ReadTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; import org.opendaylight.unimgr.impl.UnimgrConstants; import org.opendaylight.unimgr.impl.UnimgrMapper; import org.opendaylight.unimgr.utils.EvcUtils; @@ -44,11 +39,10 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology. import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey; -import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.common.RpcError; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -62,7 +56,8 @@ public class EvcUpdateCommandTest { private static final NodeId OVSDB_NODE_ID = new NodeId("ovsdb://7011db35-f44b-4aab-90f6-d89088caf9d8"); private EvcUpdateCommand evcUpdateCommand; - private Map, DataObject> changes; + private Link link; + private DataTreeModification evcLink; private DataBroker dataBroker; @SuppressWarnings("unchecked") @@ -73,16 +68,17 @@ public class EvcUpdateCommandTest { PowerMockito.mockStatic(EvcUtils.class); PowerMockito.mockStatic(OvsdbUtils.class); PowerMockito.mockStatic(UnimgrMapper.class); - changes = mock(Map.class); dataBroker = mock(DataBroker.class); - evcUpdateCommand = new EvcUpdateCommand(dataBroker, changes); + link = mock(Link.class); + evcLink = DataTreeModificationHelper.getEvcLink(link); + evcUpdateCommand = new EvcUpdateCommand(dataBroker, evcLink); } /** * Test method for {@link org.opendaylight.unimgr.command.EvcUpdateCommand#execute()}. * @throws Exception */ @SuppressWarnings({ "unchecked", "rawtypes" }) - @Test//(expected = ReadFailedException.class) + @Test public void testExecute() throws Exception { final ReadTransaction readTransac = mock(ReadOnlyTransaction.class); final CheckedFuture retFormerEvc = mock(CheckedFuture.class); @@ -113,17 +109,6 @@ public class EvcUpdateCommandTest { .create(NetworkTopology.class) .child(Topology.class, new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID)) .child(Node.class, new NodeKey(OVSDB_NODE_ID)); - final Set, DataObject>> setCreated = new HashSet,DataObject>>(); - final Entry, DataObject> created = new Entry, DataObject>() { - @Override - public DataObject setValue(DataObject value) { return null; } - @Override - public DataObject getValue() { return evcAugmentation; } - @Override - public InstanceIdentifier getKey() { return evcKey; } - }; - - setCreated.add(created); unisSource.add(uniSource); unisDest.add(uniDest); formerUnisSource.add(formerUniSource); @@ -131,7 +116,7 @@ public class EvcUpdateCommandTest { // Case EVC1 : formerUni1ip.equals(laterUni1Ip), formerUni2ip.equals(laterUni2Ip) // iidSource != null, iidDest != null - when(changes.entrySet()).thenReturn(setCreated); + when(link.getAugmentation(EvcAugmentation.class)).thenReturn(evcAugmentation); when(dataBroker.newReadOnlyTransaction()).thenReturn((ReadOnlyTransaction) readTransac); when(readTransac.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))) .thenReturn(retFormerEvc); @@ -177,19 +162,12 @@ public class EvcUpdateCommandTest { any(LogicalDatastoreType.class))).thenReturn(evcKey); verifyExecute(0, 0, 6, 2); - - // Case EVC2 : formerUni1ip.equals(laterUni1Ip), formerUni2ip.equals(laterUni2Ip) - // iidSource != null, iidDest != null + // iidSource != null, iidDest != null when(formerUniSource.getIpAddress()).thenReturn(formerIpAddressSource); when(formerUniDest.getIpAddress()).thenReturn(formerIpAddressDest); when(uniSource.getUni()).thenReturn(null); when(uniDest.getUni()).thenReturn(null); - verifyExecute(4, 2, 14, 4); - - // Case EVC3 : EVC2 with ReadFailedError - when(retFormerEvc.checkedGet()).thenThrow( - new ReadFailedException("Test ReadFailedException", (RpcError[]) null)); - verifyExecute(6, 2, 20, 6); + verifyExecute(2, 0, 12, 4); } @SuppressWarnings("unchecked") @@ -217,4 +195,4 @@ public class EvcUpdateCommandTest { any(InstanceIdentifier.class), any(DataBroker.class)); } -} +} \ No newline at end of file diff --git a/impl/src/test/java/org/opendaylight/unimgr/command/UniCreateCommandTest.java b/impl/src/test/java/org/opendaylight/unimgr/command/UniAddCommandTest.java similarity index 75% rename from impl/src/test/java/org/opendaylight/unimgr/command/UniCreateCommandTest.java rename to impl/src/test/java/org/opendaylight/unimgr/command/UniAddCommandTest.java index 5e58501b..687d3150 100644 --- a/impl/src/test/java/org/opendaylight/unimgr/command/UniCreateCommandTest.java +++ b/impl/src/test/java/org/opendaylight/unimgr/command/UniAddCommandTest.java @@ -13,16 +13,12 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.unimgr.impl.UnimgrConstants; import org.opendaylight.unimgr.impl.UnimgrMapper; @@ -42,7 +38,6 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology. import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey; -import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -52,13 +47,15 @@ import com.google.common.base.Optional; @RunWith(PowerMockRunner.class) @PrepareForTest({UniUtils.class, OvsdbUtils.class, MdsalUtils.class, UnimgrMapper.class}) -public class UniCreateCommandTest { +public class UniAddCommandTest { private static final NodeId OVSDB_NODE_ID = new NodeId("ovsdb://7011db35-f44b-4aab-90f6-d89088caf9d8"); - private UniCreateCommand uniCreateCommand; - private Map, DataObject> changes; + private UniAddCommand uniAddCommand; + private DataTreeModification uni; private DataBroker dataBroker; + private Node uniNode; + private Optional optUniNode; @SuppressWarnings("unchecked") @Before @@ -67,13 +64,15 @@ public class UniCreateCommandTest { PowerMockito.mockStatic(OvsdbUtils.class); PowerMockito.mockStatic(MdsalUtils.class); PowerMockito.mockStatic(UnimgrMapper.class); - changes = mock(Map.class); dataBroker = mock(DataBroker.class); - uniCreateCommand = new UniCreateCommand(dataBroker, changes); + uniNode = mock(Node.class); + optUniNode = mock(Optional.class); + uni = DataTreeModificationHelper.getUniNode(uniNode); + uniAddCommand = new UniAddCommand(dataBroker, uni); } /** - * Test method for {@link org.opendaylight.unimgr.command.UniCreateCommand#execute()}. + * Test method for {@link org.opendaylight.unimgr.command.uniAddCommandTest#execute()}. * @throws Exception */ @SuppressWarnings({ "unchecked", "rawtypes" }) @@ -93,20 +92,13 @@ public class UniCreateCommandTest { .create(NetworkTopology.class) .child(Topology.class, new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID)) .child(Node.class, new NodeKey(OVSDB_NODE_ID)); - final Set, DataObject>> setCreated = new HashSet,DataObject>>(); - Entry, DataObject> created = new Entry, DataObject>() { - @Override - public DataObject setValue(DataObject value) { return null; } - @Override - public DataObject getValue() { return uniAugmentation; } - @Override - public InstanceIdentifier getKey() { return uniKey; } - }; nodes.add(node); - setCreated.add(created); // Case UNI1 : uni.getOvsdbNodeRef() != null, !optionalNode.isPresent() - when(changes.entrySet()).thenReturn(setCreated); + when(uniNode.getAugmentation(UniAugmentation.class)).thenReturn(uniAugmentation); + when(optUniNode.isPresent()).thenReturn(false); + when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class), + any(InstanceIdentifier.class))).thenReturn(optUniNode).thenReturn(optionalOvsdbNode); when(optionalOvsdbNode.isPresent()).thenReturn(false).thenReturn(true); when(optionalOvsdbNode.get()).thenReturn(node); when(uniAugmentation.getIpAddress()).thenReturn(ipAddress); @@ -121,8 +113,6 @@ public class UniCreateCommandTest { when(node.getAugmentation(any(Class.class))).thenReturn(uniAugmentation); when(node.getNodeId()).thenReturn(nodeId); when(nodeId.toString()).thenReturn("ovsdbNodeId_test"); - when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class), - any(InstanceIdentifier.class))).thenReturn(optionalOvsdbNode); when(OvsdbUtils.findOvsdbNode(any(DataBroker.class), any(UniAugmentation.class))) .thenReturn(optionalOvsdbNode); when(OvsdbUtils.createQoSForOvsdbNode(any(DataBroker.class), any(UniAugmentation.class))) @@ -150,52 +140,19 @@ public class UniCreateCommandTest { // Case UNI3 : uni.getOvsdbNodeRef() == null, optionalNode.isPresent() when(uniAugmentation.getOvsdbNodeRef()).thenReturn(null); + when(optionalOvsdbNode.isPresent()).thenReturn(true); + when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class), + any(InstanceIdentifier.class))).thenReturn(optUniNode).thenReturn(optionalOvsdbNode); verifyExecute(2, 1, 3, 0); // Case UNI4 : uni.getOvsdbNodeRef() == null, !optionalNode.isPresent() when(optionalOvsdbNode.isPresent()).thenReturn(false); verifyExecute(2, 1, 4, 0); - - // Case OVSDB0 ovsdbNodeAugmentation == null - created = new Entry, DataObject>() { - @Override - public DataObject setValue(DataObject value) { return null; } - @Override - public DataObject getValue() { return null; } - @Override - public InstanceIdentifier getKey() { return uniKey; } - }; - setCreated.clear(); - setCreated.add(created); - verifyExecute(2, 1, 4, 0); - - // Case OVSDB1 : uniNodes != null && !uniNodes.isEmpty(), - // uniAugmentation.getOvsdbNodeRef() != null && - // uniAugmentation.getOvsdbNodeRef().getValue() != null - created = new Entry, DataObject>() { - @Override - public DataObject setValue(DataObject value) { return null; } - @Override - public DataObject getValue() { return ovsdbNodeAugmentation; } - @Override - public InstanceIdentifier getKey() { return uniKey; } - }; - setCreated.clear(); - setCreated.add(created); - - when(uniAugmentation.getOvsdbNodeRef()).thenReturn(ovsNodedRef); - when(optionalOvsdbNode.isPresent()).thenReturn(true); - verifyExecute(3, 2, 4, 1); - - // Case OVSDB2 : uniNodes != null && !uniNodes.isEmpty(), - // ovsdbNodeAugmentation.getConnectionInfo().getRemoteIp() - // .equals(uniAugmentation.getIpAddress()) - when(uniAugmentation.getOvsdbNodeRef()).thenReturn(null); - verifyExecute(3, 3, 4, 2); } + @SuppressWarnings("unchecked") private void verifyExecute(int qosTimes, int bridgeTimes, int updateNodeTime, int updateIIDTimes){ - uniCreateCommand.execute(); + uniAddCommand.execute(); PowerMockito.verifyStatic(times(qosTimes)); OvsdbUtils.createQoSForOvsdbNode(any(DataBroker.class), any(UniAugmentation.class)); PowerMockito.verifyStatic(times(bridgeTimes)); @@ -208,5 +165,4 @@ public class UniCreateCommandTest { UniUtils.updateUniNode(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(UniAugmentation.class), any(InstanceIdentifier.class), any(DataBroker.class)); } - -} +} \ No newline at end of file diff --git a/impl/src/test/java/org/opendaylight/unimgr/command/UniDeleteCommandTest.java b/impl/src/test/java/org/opendaylight/unimgr/command/UniRemoveCommandTest.java similarity index 78% rename from impl/src/test/java/org/opendaylight/unimgr/command/UniDeleteCommandTest.java rename to impl/src/test/java/org/opendaylight/unimgr/command/UniRemoveCommandTest.java index e0a187ee..40d94986 100644 --- a/impl/src/test/java/org/opendaylight/unimgr/command/UniDeleteCommandTest.java +++ b/impl/src/test/java/org/opendaylight/unimgr/command/UniRemoveCommandTest.java @@ -7,26 +7,19 @@ */ package org.opendaylight.unimgr.command; -import static org.junit.Assert.*; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.doNothing; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.powermock.api.support.membermodification.MemberMatcher.method; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mockito; import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.unimgr.impl.UnimgrConstants; import org.opendaylight.unimgr.impl.UnimgrMapper; @@ -34,7 +27,6 @@ import org.opendaylight.unimgr.utils.MdsalUtils; import org.opendaylight.unimgr.utils.OvsdbUtils; import org.opendaylight.unimgr.utils.UniUtils; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; 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.OvsdbNodeRef; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries; @@ -42,20 +34,14 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.re 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.QueuesKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentationBuilder; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; -import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey; -import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder; import org.powermock.api.mockito.PowerMockito; -import org.powermock.api.support.membermodification.MemberMatcher; -import org.powermock.api.support.membermodification.MemberModifier; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -64,14 +50,13 @@ import com.google.common.base.Optional; @RunWith(PowerMockRunner.class) @PrepareForTest({UniUtils.class, OvsdbUtils.class, MdsalUtils.class, UnimgrMapper.class}) -public class UniDeleteCommandTest { +public class UniRemoveCommandTest { private static final NodeId OVSDB_NODE_ID = new NodeId("ovsdb://7011db35-f44b-4aab-90f6-d89088caf9d8"); - private static final String NODE_ID = "uni://10.0.0.1"; - - private UniDeleteCommand uniDeleteCommand; - private AsyncDataChangeEvent, DataObject> changes; + private UniRemoveCommand uniRemoveCommand; + private DataTreeModification uni; private DataBroker dataBroker; + private Node uniNode; @SuppressWarnings("unchecked") @Before @@ -80,20 +65,18 @@ public class UniDeleteCommandTest { PowerMockito.mockStatic(OvsdbUtils.class); PowerMockito.mockStatic(MdsalUtils.class); PowerMockito.mockStatic(UnimgrMapper.class); - changes = mock(AsyncDataChangeEvent.class); dataBroker = mock(DataBroker.class); - uniDeleteCommand = new UniDeleteCommand(dataBroker, changes); + uniNode = mock(Node.class); + uni = DataTreeModificationHelper.getUniNode(uniNode); + uniRemoveCommand = new UniRemoveCommand(dataBroker, uni); } /** - * Test method for {@link org.opendaylight.unimgr.command.UniDeleteCommand#execute()}. + * Test method for {@link org.opendaylight.unimgr.command.UniRemoveCommand#execute()}. */ @SuppressWarnings({ "unchecked", "rawtypes"}) @Test public void testExecute() { - final Set> removedUnis = - new HashSet>(); - removedUnis.add(mock(InstanceIdentifier.class)); final UniAugmentation uniAugmentation = mock(UniAugmentation.class); final OvsdbNodeRef ovsNodedRef = mock(OvsdbNodeRef.class); final Optional optionalNode = mock(Optional.class); @@ -106,7 +89,7 @@ public class UniDeleteCommandTest { final OvsdbNodeAugmentation ovsdbNodeAugmentation = mock(OvsdbNodeAugmentation.class); final QosEntries qosEntries = mock(QosEntries.class); final Queues queues = mock(Queues.class); - + when(uniNode.getAugmentation(UniAugmentation.class)).thenReturn(uniAugmentation); when(uniAugmentation.getIpAddress()).thenReturn(mock(IpAddress.class)); when(uniAugmentation.getOvsdbNodeRef()).thenReturn(ovsNodedRef); when(ovsNodedRef.getValue()).thenReturn(uniKey); @@ -121,8 +104,6 @@ public class UniDeleteCommandTest { when(queues.getKey()).thenReturn(mock(QueuesKey.class)); queuesList.add(queues); when(ovsdbNodeAugmentation.getQueues()).thenReturn(queuesList); - when(OvsdbUtils.extractRemoved(any(AsyncDataChangeEvent.class), any(Class.class))) - .thenReturn(removedUnis); when(MdsalUtils.read(any(DataBroker.class), any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(uniAugmentation); when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class), @@ -138,11 +119,10 @@ public class UniDeleteCommandTest { when(UnimgrMapper.getOvsdbQueuesIid(any(Node.class), any(QueuesKey.class))).thenReturn(instanceOfNode); when(MdsalUtils.deleteNode(any(DataBroker.class), any(InstanceIdentifier.class), any(LogicalDatastoreType.class))).thenReturn(true); - uniDeleteCommand.execute(); - - PowerMockito.verifyStatic(times(5)); + uniRemoveCommand.execute(); + PowerMockito.verifyStatic(times(4)); MdsalUtils.deleteNode(any(DataBroker.class), any(InstanceIdentifier.class), any(LogicalDatastoreType.class)); } -} +} \ No newline at end of file diff --git a/impl/src/test/java/org/opendaylight/unimgr/command/UniUpdateCommandTest.java b/impl/src/test/java/org/opendaylight/unimgr/command/UniUpdateCommandTest.java index f00347b5..0bd992e0 100644 --- a/impl/src/test/java/org/opendaylight/unimgr/command/UniUpdateCommandTest.java +++ b/impl/src/test/java/org/opendaylight/unimgr/command/UniUpdateCommandTest.java @@ -7,24 +7,19 @@ */ package org.opendaylight.unimgr.command; -import static org.junit.Assert.*; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.Map.Entry; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.unimgr.impl.UnimgrConstants; import org.opendaylight.unimgr.impl.UnimgrMapper; @@ -37,7 +32,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.re import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef; 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.ManagedNodeEntry; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.uni.Speed; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology; @@ -46,7 +40,6 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology. import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey; -import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -61,9 +54,9 @@ public class UniUpdateCommandTest { private static final NodeId OVSDB_NODE_ID = new NodeId("ovsdb://7011db35-f44b-4aab-90f6-d89088caf9d8"); private UniUpdateCommand uniUpdateCommand; - private Map, DataObject> changes; + private DataTreeModification uni; private DataBroker dataBroker; - private ManagedNodeEntry managedNodeEntry; + private Node uniNode; @SuppressWarnings("unchecked") @Before @@ -72,9 +65,10 @@ public class UniUpdateCommandTest { PowerMockito.mockStatic(MdsalUtils.class); PowerMockito.mockStatic(OvsdbUtils.class); PowerMockito.mockStatic(UnimgrMapper.class); - changes = mock(Map.class); dataBroker = mock(DataBroker.class); - uniUpdateCommand = new UniUpdateCommand(dataBroker, changes); + uniNode = mock(Node.class); + uni = DataTreeModificationHelper.getUniNode(uniNode); + uniUpdateCommand = new UniUpdateCommand(dataBroker, uni); } /** @@ -86,8 +80,6 @@ public class UniUpdateCommandTest { final Optional optionalOvsdbNode = mock(Optional.class); final UniAugmentation uniAugmentation = mock(UniAugmentation.class); final OvsdbNodeAugmentation ovsdbNodeAugmentation = mock(OvsdbNodeAugmentation.class); - managedNodeEntry = mock(ManagedNodeEntry.class); - final ManagedNodeEntryKey managedNodeEntryKey = mock(ManagedNodeEntryKey.class); final List managedNodeEntries = new ArrayList(); final ConnectionInfo connectionInfo = mock(ConnectionInfo.class); final IpAddress ipAddress = mock(IpAddress.class); @@ -99,20 +91,9 @@ public class UniUpdateCommandTest { .create(NetworkTopology.class) .child(Topology.class, new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID)) .child(Node.class, new NodeKey(OVSDB_NODE_ID)); - final Set, DataObject>> setCreated = new HashSet,DataObject>>(); - Entry, DataObject> created = new Entry, DataObject>() { - @Override - public DataObject setValue(DataObject value) { return null; } - @Override - public DataObject getValue() { return uniAugmentation; } - @Override - public InstanceIdentifier getKey() { return uniKey; } - }; - setCreated.add(created); - managedNodeEntries.add(managedNodeEntry); // Case UNI1 : updatedUni.getOvsdbNodeRef() != null, optOvsdbNode.isPresent() - when(changes.entrySet()).thenReturn(setCreated); + when(uniNode.getAugmentation(UniAugmentation.class)).thenReturn(uniAugmentation); when(optionalOvsdbNode.isPresent()).thenReturn(true); when(optionalOvsdbNode.get()).thenReturn(node); when(uniAugmentation.getIpAddress()).thenReturn(ipAddress); @@ -120,7 +101,6 @@ public class UniUpdateCommandTest { when(uniAugmentation.getOvsdbNodeRef()).thenReturn(ovsNodedRef); when(ovsdbNodeAugmentation.getConnectionInfo()).thenReturn(connectionInfo); when(ovsdbNodeAugmentation.getManagedNodeEntry()).thenReturn(managedNodeEntries); - when(managedNodeEntry.getKey()).thenReturn(managedNodeEntryKey); when(connectionInfo.getRemoteIp()).thenReturn(ipAddress); when(ipAddress.getIpv4Address()).thenReturn(ipv4Address); when(ipv4Address.toString()).thenReturn("ipv4Address_test"); @@ -129,7 +109,6 @@ public class UniUpdateCommandTest { when(node.getAugmentation(any(Class.class))).thenReturn(uniAugmentation); when(node.getNodeId()).thenReturn(nodeId); when(nodeId.toString()).thenReturn("ovsdbNodeId_test"); - when(UniUtils.getUni(any(DataBroker.class), any(LogicalDatastoreType.class), any(IpAddress.class))) .thenReturn(uniAugmentation); when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class), @@ -144,48 +123,20 @@ public class UniUpdateCommandTest { any(LogicalDatastoreType.class))).thenReturn(true); when(UnimgrMapper.getUniIid(any(DataBroker.class), any(IpAddress.class), any(LogicalDatastoreType.class))).thenReturn(uniKey); - verifyExecute(1, 1, 2, 0); - - // Case UNI2 : updatedUni.getOvsdbNodeRef() != null, !optOvsdbNode.isPresent() - when(optionalOvsdbNode.isPresent()).thenReturn(false); - verifyExecute(1, 1, 2, 0); + verifyExecute(1, 1); - // Case UNI3 : updatedUni.getOvsdbNodeRef() == null, !optOvsdbNode.isPresent() - when(uniAugmentation.getOvsdbNodeRef()).thenReturn(null); - verifyExecute(1, 1, 2, 0); - - // Case UNI4 : updatedUni.getOvsdbNodeRef() == null, optOvsdbNode.isPresent() - when(optionalOvsdbNode.isPresent()).thenReturn(true); - verifyExecute(2, 2, 4, 0); - - // Case OVSDB - created = new Entry, DataObject>() { - @Override - public DataObject setValue(DataObject value) { return null; } - @Override - public DataObject getValue() { return ovsdbNodeAugmentation; } - @Override - public InstanceIdentifier getKey() { return uniKey; } - }; - setCreated.clear(); - setCreated.add(created); - - when(uniAugmentation.getOvsdbNodeRef()).thenReturn(ovsNodedRef); + // Case 2 : updatedUni.getOvsdbNodeRef() == null, optOvsdbNode.isPresent() when(optionalOvsdbNode.isPresent()).thenReturn(true); - verifyExecute(2, 2, 4, 1); + verifyExecute(2, 2); } - private void verifyExecute(int qosTimes, int deleteTimes, int updateNodeTime, int getKeyTimes){ + private void verifyExecute(int qosTimes, int updateNodeTime){ uniUpdateCommand.execute(); PowerMockito.verifyStatic(times(qosTimes)); OvsdbUtils.createQoSForOvsdbNode(any(DataBroker.class), any(UniAugmentation.class)); - PowerMockito.verifyStatic(times(deleteTimes)); - MdsalUtils.deleteNode(any(DataBroker.class), any(InstanceIdentifier.class), - any(LogicalDatastoreType.class)); PowerMockito.verifyStatic(times(updateNodeTime)); UniUtils.updateUniNode(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(UniAugmentation.class), any(Node.class), any(DataBroker.class)); - verify(managedNodeEntry, times(getKeyTimes)).getKey(); } -} +} \ No newline at end of file diff --git a/impl/src/test/java/org/opendaylight/unimgr/impl/EvcDataTreeChangeListenerTest.java b/impl/src/test/java/org/opendaylight/unimgr/impl/EvcDataTreeChangeListenerTest.java new file mode 100644 index 00000000..c3bbe9eb --- /dev/null +++ b/impl/src/test/java/org/opendaylight/unimgr/impl/EvcDataTreeChangeListenerTest.java @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2016 CableLabs 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.unimgr.impl; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.opendaylight.controller.md.sal.binding.api.DataObjectModification; +import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType; +import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link; +import org.opendaylight.yangtools.yang.binding.Augmentation; +import org.opendaylight.yangtools.yang.binding.ChildOf; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.Identifiable; +import org.opendaylight.yangtools.yang.binding.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class EvcDataTreeChangeListenerTest { + + private EvcDataTreeChangeListener evcDataTreeChangeListener; + + @Before + public void setUp() throws Exception { + evcDataTreeChangeListener = mock(EvcDataTreeChangeListener.class, Mockito.CALLS_REAL_METHODS); + } + + @SuppressWarnings("unchecked") + @Test + public void testEvcmgrDataTreeChangeListener() { + Collection> collection = new ArrayList>(); + DataTreeModification evc = getDataTreeLink(ModificationType.WRITE); + collection.add(evc); + evc = getDataTreeLink(ModificationType.DELETE); + collection.add(evc); + evc = getDataTreeLink(ModificationType.SUBTREE_MODIFIED); + collection.add(evc); + evcDataTreeChangeListener.onDataTreeChanged(collection); + verify(evcDataTreeChangeListener, times(1)).add(any(DataTreeModification.class)); + verify(evcDataTreeChangeListener, times(1)).remove(any(DataTreeModification.class)); + verify(evcDataTreeChangeListener, times(1)).update(any(DataTreeModification.class)); + } + + private DataTreeModification getDataTreeLink(final ModificationType modificationType) { + final DataObjectModification evcDataObjModification = new DataObjectModification() { + @Override + public Collection> getModifiedChildren() { + // TODO Auto-generated method stub + return null; + } + @Override + public & ChildOf, K extends Identifier> DataObjectModification getModifiedChildListItem( + Class arg0, K arg1) { + // TODO Auto-generated method stub + return null; + } + @Override + public > DataObjectModification getModifiedChildContainer(Class arg0) { + // TODO Auto-generated method stub + return null; + } + @Override + public DataObjectModification getModifiedChild(PathArgument arg0) { + // TODO Auto-generated method stub + return null; + } + @Override + public & DataObject> DataObjectModification getModifiedAugmentation( + Class arg0) { + // TODO Auto-generated method stub + return null; + } + @Override + public ModificationType getModificationType() { + return modificationType; + } + @Override + public PathArgument getIdentifier() { + // TODO Auto-generated method stub + return null; + } + @Override + public Class getDataType() { + // TODO Auto-generated method stub + return null; + } + @Override + public Link getDataBefore() { + // TODO Auto-generated method stub + return null; + } + @Override + public Link getDataAfter() { + // TODO Auto-generated method stub + return null; + } + }; + DataTreeModification modifiedEvc = new DataTreeModification() { + @Override + public DataTreeIdentifier getRootPath() { + return null; + } + @Override + public DataObjectModification getRootNode() { + return evcDataObjModification; + } + }; + return modifiedEvc; + } + +} diff --git a/impl/src/test/java/org/opendaylight/unimgr/impl/UniDataTreeChangeListenerTest.java b/impl/src/test/java/org/opendaylight/unimgr/impl/UniDataTreeChangeListenerTest.java new file mode 100644 index 00000000..40bf5716 --- /dev/null +++ b/impl/src/test/java/org/opendaylight/unimgr/impl/UniDataTreeChangeListenerTest.java @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2016 CableLabs 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, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.unimgr.impl; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.opendaylight.controller.md.sal.binding.api.DataObjectModification; +import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType; +import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; +import org.opendaylight.yangtools.yang.binding.Augmentation; +import org.opendaylight.yangtools.yang.binding.ChildOf; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.Identifiable; +import org.opendaylight.yangtools.yang.binding.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument; +import org.powermock.modules.junit4.PowerMockRunner; + + +@RunWith(PowerMockRunner.class) +public class UniDataTreeChangeListenerTest { + + private UniDataTreeChangeListener uniDataTreeChangeListener; + + @Before + public void setUp() throws Exception { + uniDataTreeChangeListener = mock(UniDataTreeChangeListener.class, Mockito.CALLS_REAL_METHODS); + } + + @SuppressWarnings("unchecked") + @Test + public void testUnimgrDataTreeChangeListener() { + Collection> collection = new ArrayList>(); + DataTreeModification uni = getDataTreeNode(ModificationType.WRITE); + collection.add(uni); + uni = getDataTreeNode(ModificationType.DELETE); + collection.add(uni); + uni = getDataTreeNode(ModificationType.SUBTREE_MODIFIED); + collection.add(uni); + uniDataTreeChangeListener.onDataTreeChanged(collection); + verify(uniDataTreeChangeListener, times(1)).add(any(DataTreeModification.class)); + verify(uniDataTreeChangeListener, times(1)).remove(any(DataTreeModification.class)); + verify(uniDataTreeChangeListener, times(1)).update(any(DataTreeModification.class)); + } + + private DataTreeModification getDataTreeNode(final ModificationType modificationType) { + final DataObjectModification uniDataObjModification = new DataObjectModification() { + @Override + public Collection> getModifiedChildren() { + // TODO Auto-generated method stub + return null; + } + @Override + public & ChildOf, K extends Identifier> DataObjectModification getModifiedChildListItem( + Class arg0, K arg1) { + // TODO Auto-generated method stub + return null; + } + @Override + public > DataObjectModification getModifiedChildContainer(Class arg0) { + // TODO Auto-generated method stub + return null; + } + @Override + public DataObjectModification getModifiedChild(PathArgument arg0) { + // TODO Auto-generated method stub + return null; + } + @Override + public & DataObject> DataObjectModification getModifiedAugmentation( + Class arg0) { + // TODO Auto-generated method stub + return null; + } + @Override + public ModificationType getModificationType() { + return modificationType; + } + @Override + public PathArgument getIdentifier() { + // TODO Auto-generated method stub + return null; + } + @Override + public Class getDataType() { + // TODO Auto-generated method stub + return null; + } + @Override + public Node getDataBefore() { + // TODO Auto-generated method stub + return null; + } + @Override + public Node getDataAfter() { + // TODO Auto-generated method stub + return null; + } + }; + DataTreeModification modifiedUni = new DataTreeModification() { + @Override + public DataTreeIdentifier getRootPath() { + return null; + } + @Override + public DataObjectModification getRootNode() { + return uniDataObjModification; + } + }; + return modifiedUni; + } +} diff --git a/impl/src/test/java/org/opendaylight/unimgr/impl/UnimgrDataChangeListenerTest.java b/impl/src/test/java/org/opendaylight/unimgr/impl/UnimgrDataChangeListenerTest.java deleted file mode 100644 index be800d5c..00000000 --- a/impl/src/test/java/org/opendaylight/unimgr/impl/UnimgrDataChangeListenerTest.java +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright (c) 2016 CableLabs 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, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.unimgr.impl; - -import static org.junit.Assert.assertFalse; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.atLeast; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Map; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.binding.api.DataChangeListener; -import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope; -import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.unimgr.command.Command; -import org.opendaylight.unimgr.command.EvcCreateCommand; -import org.opendaylight.unimgr.command.EvcDeleteCommand; -import org.opendaylight.unimgr.command.EvcUpdateCommand; -import org.opendaylight.unimgr.command.TransactionInvoker; -import org.opendaylight.unimgr.command.UniCreateCommand; -import org.opendaylight.unimgr.command.UniDeleteCommand; -import org.opendaylight.unimgr.command.UniUpdateCommand; -import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; -import org.opendaylight.yangtools.concepts.ListenerRegistration; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.api.support.membermodification.MemberModifier; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({UnimgrMapper.class, - UnimgrDataChangeListener.class}) -public class UnimgrDataChangeListenerTest { - - @Mock private UnimgrDataChangeListener unimgrDataChangeListener; - @Mock private HashSet> listeners; - @Mock private DataBroker dataBroker; - @Mock private TransactionInvoker invoker; - - @Before - public void setUp() throws Exception { - unimgrDataChangeListener = mock(UnimgrDataChangeListener.class, Mockito.CALLS_REAL_METHODS); - MemberModifier.field(UnimgrDataChangeListener.class, "invoker").set(unimgrDataChangeListener, invoker); - } - - @SuppressWarnings({ "unchecked", "resource" }) - @Test - public void testUnimgrDataChangeListener() throws Exception { - final InstanceIdentifier path = mock(InstanceIdentifier.class); - final ListenerRegistration listenerRegistration = mock(ListenerRegistration.class); - PowerMockito.mockStatic(UnimgrMapper.class); - PowerMockito.when(UnimgrMapper.getUniTopologyIid()).thenReturn(path); - PowerMockito.when(UnimgrMapper.getEvcTopologyIid()).thenReturn(path); - PowerMockito.when(UnimgrMapper.getOvsdbTopologyIid()).thenReturn(path); - PowerMockito.whenNew(HashSet.class).withNoArguments().thenReturn(listeners); - when(listeners.add(any(ListenerRegistration.class))).thenReturn(true); - when(dataBroker.registerDataChangeListener(any(LogicalDatastoreType.class), - any(InstanceIdentifier.class), - any(UnimgrDataChangeListener.class), - eq(DataChangeScope.SUBTREE))).thenReturn(listenerRegistration); - new UnimgrDataChangeListener(dataBroker, invoker); - assertFalse(listeners.isEmpty()); - verify(listeners, atLeast(3)).add(any(ListenerRegistration.class)); - verify(dataBroker, atLeast(3)).registerDataChangeListener(any(LogicalDatastoreType.class), - any(InstanceIdentifier.class), - any(UnimgrDataChangeListener.class), - eq(DataChangeScope.SUBTREE)); - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Test - public void testOnDataChanged() { - final AsyncDataChangeEvent change = mock(AsyncDataChangeEvent.class); - final Map map = mock(Map.class); - when(change.getCreatedData()).thenReturn(map); - doNothing().when(unimgrDataChangeListener).create(any(Map.class)); - doNothing().when(unimgrDataChangeListener).update(any(Map.class)); - doNothing().when(unimgrDataChangeListener).delete(any(AsyncDataChangeEvent.class)); - unimgrDataChangeListener.onDataChanged(change); - verify(unimgrDataChangeListener).create(any(Map.class)); - verify(unimgrDataChangeListener).update(any(Map.class)); - verify(unimgrDataChangeListener).delete(any(AsyncDataChangeEvent.class)); - } - - @SuppressWarnings("unchecked") - @Test - public void testCreate() throws Exception { - // False case - unimgrDataChangeListener.create(null); - - // True case - final Map, DataObject> changes = mock(Map.class); - final UniCreateCommand uniCreate = mock(UniCreateCommand.class); - final EvcCreateCommand evcCreate = mock(EvcCreateCommand.class); - final List commands = mock(ArrayList.class); - PowerMockito.whenNew(ArrayList.class).withNoArguments() - .thenReturn((ArrayList) commands); - PowerMockito.whenNew(UniCreateCommand.class).withArguments(any(DataBroker.class), - any(Map.class)).thenReturn(uniCreate); - PowerMockito.whenNew(EvcCreateCommand.class).withArguments(any(DataBroker.class), - any(Map.class)).thenReturn(evcCreate); - when(commands.add(any(Command.class))).thenReturn(true); - doNothing().when(invoker).setCommands(any(ArrayList.class)); - doNothing().when(invoker).invoke(); - unimgrDataChangeListener.create(changes); - verify(invoker, times(1)).setCommands(any(List.class)); - verify(invoker, times(1)).invoke(); - } - - @SuppressWarnings("unchecked") - @Test - public void testUpdate() throws Exception { - // False case - unimgrDataChangeListener.update(null); - - // True case - final Map, DataObject> changes = mock(Map.class); - final UniUpdateCommand uniUpdate = mock(UniUpdateCommand.class); - final EvcUpdateCommand evcUpdate = mock(EvcUpdateCommand.class); - final List commands = mock(ArrayList.class); - PowerMockito.whenNew(ArrayList.class).withNoArguments() - .thenReturn((ArrayList) commands); - PowerMockito.whenNew(UniUpdateCommand.class).withArguments(any(DataBroker.class), - any(Map.class)).thenReturn(uniUpdate); - PowerMockito.whenNew(EvcUpdateCommand.class).withArguments(any(DataBroker.class), - any(Map.class)).thenReturn(evcUpdate); - when(commands.add(any(Command.class))).thenReturn(true); - doNothing().when(invoker).setCommands(any(ArrayList.class)); - doNothing().when(invoker).invoke(); - unimgrDataChangeListener.update(changes); - verify(invoker, times(1)).setCommands(any(List.class)); - verify(invoker, times(1)).invoke(); - } - - @SuppressWarnings("unchecked") - @Test - public void testDelete() throws Exception { - // False case - unimgrDataChangeListener.delete(null); - - // True case - final AsyncDataChangeEvent, DataObject> changes = mock(AsyncDataChangeEvent.class); - final UniDeleteCommand uniDelete = mock(UniDeleteCommand.class); - final EvcDeleteCommand evcDelete = mock(EvcDeleteCommand.class); - final List commands = mock(ArrayList.class); - PowerMockito.whenNew(ArrayList.class).withNoArguments() - .thenReturn((ArrayList) commands); - PowerMockito.whenNew(UniDeleteCommand.class).withArguments(any(DataBroker.class), - any(AsyncDataChangeEvent.class)).thenReturn(uniDelete); - PowerMockito.whenNew(EvcDeleteCommand.class).withArguments(any(DataBroker.class), - any(AsyncDataChangeEvent.class)).thenReturn(evcDelete); - when(commands.add(any(Command.class))).thenReturn(true); - doNothing().when(invoker).setCommands(any(ArrayList.class)); - doNothing().when(invoker).invoke(); - unimgrDataChangeListener.delete(changes); - verify(invoker, times(1)).setCommands(any(List.class)); - verify(invoker, times(1)).invoke(); - } -} diff --git a/impl/src/test/java/org/opendaylight/unimgr/impl/UnimgrProviderTest.java b/impl/src/test/java/org/opendaylight/unimgr/impl/UnimgrProviderTest.java index bc390df5..57b612b7 100755 --- a/impl/src/test/java/org/opendaylight/unimgr/impl/UnimgrProviderTest.java +++ b/impl/src/test/java/org/opendaylight/unimgr/impl/UnimgrProviderTest.java @@ -7,17 +7,16 @@ */ package org.opendaylight.unimgr.impl; +import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.isNull; +import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; -import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.mockito.Mockito.isNull; - -import static org.junit.Assert.assertEquals; import java.util.Dictionary; import java.util.List; @@ -33,7 +32,6 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext; import org.opendaylight.unimgr.api.IUnimgrConsoleProvider; -import org.opendaylight.unimgr.command.TransactionInvoker; import org.opendaylight.unimgr.utils.MdsalUtils; import org.opendaylight.unimgr.utils.OvsdbUtils; import org.opendaylight.unimgr.utils.UniUtils; @@ -77,74 +75,66 @@ import com.google.common.util.concurrent.CheckedFuture; UnimgrMapper.class}) public class UnimgrProviderTest { - @Mock private UnimgrDataChangeListener listener; - @Mock private DataBroker dataBroker; - @Mock private UnimgrProvider unimgrProvider; @Mock private IUnimgrConsoleProvider console; + @Mock private UniDataTreeChangeListener uniListener; + @Mock private EvcDataTreeChangeListener evcListener; + @Mock private OvsNodeDataTreeChangeListener ovsListener; + @Mock private DataBroker dataBroker; @Mock private ServiceRegistration mockUnimgrConsoleRegistration; + @Mock private UnimgrProvider unimgrProvider; @Before public void setUp() throws Exception { unimgrProvider = PowerMockito.mock(UnimgrProvider.class, Mockito.CALLS_REAL_METHODS); - MemberModifier.field(UnimgrProvider.class, "listener").set(unimgrProvider, listener); MemberModifier.field(UnimgrProvider.class, "dataBroker").set(unimgrProvider, dataBroker); + MemberModifier.field(UnimgrProvider.class, "uniListener").set(unimgrProvider, uniListener); + MemberModifier.field(UnimgrProvider.class, "evcListener").set(unimgrProvider, evcListener); + MemberModifier.field(UnimgrProvider.class, "ovsListener").set(unimgrProvider, ovsListener); console = unimgrProvider; } - @SuppressWarnings("unchecked") @Test - public void testOnSessionInitiated() throws Exception { - final ProviderContext session = mock(ProviderContext.class); - when(session.getSALService(DataBroker.class)).thenReturn(dataBroker); - final BundleContext context = mock(BundleContext.class); - PowerMockito.mockStatic(FrameworkUtil.class); - final Bundle bundle = mock(Bundle.class); - when(bundle.getBundleContext()).thenReturn(context); - PowerMockito.when(FrameworkUtil.getBundle(unimgrProvider.getClass())).thenReturn(bundle); - mockUnimgrConsoleRegistration = mock(ServiceRegistration.class); - when(context.registerService(eq(IUnimgrConsoleProvider.class), - any(IUnimgrConsoleProvider.class), - isNull(Dictionary.class))).thenReturn(mockUnimgrConsoleRegistration); - PowerMockito.whenNew(UnimgrDataChangeListener.class).withArguments(any(DataBroker.class), - any(TransactionInvoker.class)) - .thenReturn(listener); - MemberModifier.suppress(MemberMatcher.method(UnimgrProvider.class, "initDatastore")); - unimgrProvider.onSessionInitiated(session); - verify(unimgrProvider, atLeast(4)).initDatastore(any(LogicalDatastoreType.class), any(TopologyId.class)); + public void testAddEvc() throws Exception { + assertEquals(false, unimgrProvider.addEvc(any(EvcAugmentation.class))); } - @SuppressWarnings("unchecked") @Test - public void testInitializeTopology() throws Exception { - final InstanceIdentifier path = mock(InstanceIdentifier.class); - PowerMockito.mockStatic(InstanceIdentifier.class); - when(InstanceIdentifier.create(NetworkTopology.class)).thenReturn(path); - - final CheckedFuture, ReadFailedException> topology = mock(CheckedFuture.class); - final ReadWriteTransaction transaction = mock(ReadWriteTransaction.class); - when(dataBroker.newReadWriteTransaction()).thenReturn(transaction); - when(transaction.read(any(LogicalDatastoreType.class), - any(InstanceIdentifier.class))).thenReturn(topology); + public void testAddUni() throws Exception { + PowerMockito.mockStatic(UniUtils.class); + assertEquals(unimgrProvider.addUni(null), false); + final UniAugmentation mockUniAug = mock(UniAugmentation.class); + // false case + when(mockUniAug.getIpAddress()).thenReturn(null); + assertEquals(unimgrProvider.addUni(mockUniAug), false); + when(mockUniAug.getMacAddress()).thenReturn(null); + assertEquals(unimgrProvider.addUni(mockUniAug), false); + // true case + when(mockUniAug.getIpAddress()).thenReturn(mock(IpAddress.class)); + when(mockUniAug.getMacAddress()).thenReturn(mock(MacAddress.class)); + final UniAugmentationBuilder uniAugBuilder = new UniAugmentationBuilder() + .setIpAddress(mock(IpAddress.class)) + .setMacAddress(mock(MacAddress.class)); + when(UniUtils.createUniNode(any(DataBroker.class), + any(UniAugmentation.class))) + .thenReturn(true); + assertEquals(true, unimgrProvider.addUni(uniAugBuilder.build())); + } - final Optional optNetTopo = mock(Optional.class); - when(topology.get()).thenReturn(optNetTopo); - when(optNetTopo.isPresent()).thenReturn(false); - final NetworkTopologyBuilder ntb = mock(NetworkTopologyBuilder.class); - PowerMockito.whenNew(NetworkTopologyBuilder.class).withNoArguments().thenReturn(ntb); - final NetworkTopology networkTopology = mock(NetworkTopology.class); - when(ntb.build()).thenReturn(networkTopology); - doNothing().when(transaction).put(any(LogicalDatastoreType.class), - any(InstanceIdentifier.class), - any(NetworkTopology.class)); - when(transaction.submit()).thenReturn(mock(CheckedFuture.class)); + @Test + public void testGetEvc() throws Exception { + assertEquals(null, unimgrProvider.getEvc(any(String.class))); + } - final LogicalDatastoreType type = PowerMockito.mock(LogicalDatastoreType.class); - Whitebox.invokeMethod(unimgrProvider, "initializeTopology", type); - verify(ntb).build(); - verify(transaction).put(any(LogicalDatastoreType.class), - any(InstanceIdentifier.class), - any(NetworkTopology.class)); - verify(transaction).submit(); + @Test + public void testgetUni() throws Exception { + PowerMockito.mockStatic(UniUtils.class); + final UniAugmentation mockUniAug = mock(UniAugmentation.class); + when(UniUtils.getUni(any(DataBroker.class), + any(LogicalDatastoreType.class), + any(IpAddress.class))) + .thenReturn(mockUniAug); + final IpAddress mockIpAddress = mock(IpAddress.class); + assertEquals(mockUniAug, unimgrProvider.getUni(mockIpAddress)); } @SuppressWarnings("unchecked") @@ -195,26 +185,77 @@ public class UnimgrProviderTest { any(LogicalDatastoreType.class)); } + @SuppressWarnings("unchecked") @Test - public void testAddUni() throws Exception { + public void testInitializeTopology() throws Exception { + final InstanceIdentifier path = mock(InstanceIdentifier.class); + PowerMockito.mockStatic(InstanceIdentifier.class); + when(InstanceIdentifier.create(NetworkTopology.class)).thenReturn(path); + + final CheckedFuture, ReadFailedException> topology = mock(CheckedFuture.class); + final ReadWriteTransaction transaction = mock(ReadWriteTransaction.class); + when(dataBroker.newReadWriteTransaction()).thenReturn(transaction); + when(transaction.read(any(LogicalDatastoreType.class), + any(InstanceIdentifier.class))).thenReturn(topology); + + final Optional optNetTopo = mock(Optional.class); + when(topology.get()).thenReturn(optNetTopo); + when(optNetTopo.isPresent()).thenReturn(false); + final NetworkTopologyBuilder ntb = mock(NetworkTopologyBuilder.class); + PowerMockito.whenNew(NetworkTopologyBuilder.class).withNoArguments().thenReturn(ntb); + final NetworkTopology networkTopology = mock(NetworkTopology.class); + when(ntb.build()).thenReturn(networkTopology); + doNothing().when(transaction).put(any(LogicalDatastoreType.class), + any(InstanceIdentifier.class), + any(NetworkTopology.class)); + when(transaction.submit()).thenReturn(mock(CheckedFuture.class)); + + final LogicalDatastoreType type = PowerMockito.mock(LogicalDatastoreType.class); + Whitebox.invokeMethod(unimgrProvider, "initializeTopology", type); + verify(ntb).build(); + verify(transaction).put(any(LogicalDatastoreType.class), + any(InstanceIdentifier.class), + any(NetworkTopology.class)); + verify(transaction).submit(); + } + + @SuppressWarnings("unchecked") + @Test + public void testlistUnis() throws Exception { PowerMockito.mockStatic(UniUtils.class); - assertEquals(unimgrProvider.addUni(null), false); - final UniAugmentation mockUniAug = mock(UniAugmentation.class); - // false case - when(mockUniAug.getIpAddress()).thenReturn(null); - assertEquals(unimgrProvider.addUni(mockUniAug), false); - when(mockUniAug.getMacAddress()).thenReturn(null); - assertEquals(unimgrProvider.addUni(mockUniAug), false); - // true case - when(mockUniAug.getIpAddress()).thenReturn(mock(IpAddress.class)); - when(mockUniAug.getMacAddress()).thenReturn(mock(MacAddress.class)); - final UniAugmentationBuilder uniAugBuilder = new UniAugmentationBuilder() - .setIpAddress(mock(IpAddress.class)) - .setMacAddress(mock(MacAddress.class)); - when(UniUtils.createUniNode(any(DataBroker.class), - any(UniAugmentation.class))) - .thenReturn(true); - assertEquals(true, unimgrProvider.addUni(uniAugBuilder.build())); + final List mockUniList = mock(List.class); + when(UniUtils.getUnis(any(DataBroker.class), + any(LogicalDatastoreType.class))) + .thenReturn(mockUniList); + assertEquals(mockUniList, + unimgrProvider.listUnis(any(LogicalDatastoreType.class))); + } + + @SuppressWarnings("unchecked") + @Test + public void testOnSessionInitiated() throws Exception { + final ProviderContext session = mock(ProviderContext.class); + when(session.getSALService(DataBroker.class)).thenReturn(dataBroker); + final BundleContext context = mock(BundleContext.class); + PowerMockito.mockStatic(FrameworkUtil.class); + final Bundle bundle = mock(Bundle.class); + when(bundle.getBundleContext()).thenReturn(context); + PowerMockito.when(FrameworkUtil.getBundle(unimgrProvider.getClass())).thenReturn(bundle); + mockUnimgrConsoleRegistration = mock(ServiceRegistration.class); + when(context.registerService(eq(IUnimgrConsoleProvider.class), + any(IUnimgrConsoleProvider.class), + isNull(Dictionary.class))).thenReturn(mockUnimgrConsoleRegistration); + PowerMockito.whenNew(UniDataTreeChangeListener.class).withArguments(any(DataBroker.class)).thenReturn(uniListener); + PowerMockito.whenNew(EvcDataTreeChangeListener.class).withArguments(any(DataBroker.class)).thenReturn(evcListener); + PowerMockito.whenNew(OvsNodeDataTreeChangeListener.class).withArguments(any(DataBroker.class)).thenReturn(ovsListener); + MemberModifier.suppress(MemberMatcher.method(UnimgrProvider.class, "initDatastore")); + unimgrProvider.onSessionInitiated(session); + verify(unimgrProvider, atLeast(4)).initDatastore(any(LogicalDatastoreType.class), any(TopologyId.class)); + } + + @Test + public void testRemoveEvc() throws Exception { + assertEquals(false, unimgrProvider.removeEvc(any(String.class))); } @SuppressWarnings("unchecked") @@ -246,45 +287,6 @@ public class UnimgrProviderTest { assertEquals(true, unimgrProvider.removeUni(ipAddress)); } - @SuppressWarnings("unchecked") - @Test - public void testlistUnis() throws Exception { - PowerMockito.mockStatic(UniUtils.class); - final List mockUniList = mock(List.class); - when(UniUtils.getUnis(any(DataBroker.class), - any(LogicalDatastoreType.class))) - .thenReturn(mockUniList); - assertEquals(mockUniList, - unimgrProvider.listUnis(any(LogicalDatastoreType.class))); - } - - @Test - public void testgetUni() throws Exception { - PowerMockito.mockStatic(UniUtils.class); - final UniAugmentation mockUniAug = mock(UniAugmentation.class); - when(UniUtils.getUni(any(DataBroker.class), - any(LogicalDatastoreType.class), - any(IpAddress.class))) - .thenReturn(mockUniAug); - final IpAddress mockIpAddress = mock(IpAddress.class); - assertEquals(mockUniAug, unimgrProvider.getUni(mockIpAddress)); - } - - @Test - public void testRemoveEvc() throws Exception { - assertEquals(false, unimgrProvider.removeEvc(any(String.class))); - } - - @Test - public void testAddEvc() throws Exception { - assertEquals(false, unimgrProvider.addEvc(any(EvcAugmentation.class))); - } - - @Test - public void testGetEvc() throws Exception { - assertEquals(null, unimgrProvider.getEvc(any(String.class))); - } - @Test public void testUpdateEvc() throws Exception { final UniSource uniSource = mock(UniSource.class); diff --git a/it/src/test/java/org/opendaylight/unimgr/it/UnimgrIT.java b/it/src/test/java/org/opendaylight/unimgr/it/UnimgrIT.java index 47b7b9c9..e7741aa3 100644 --- a/it/src/test/java/org/opendaylight/unimgr/it/UnimgrIT.java +++ b/it/src/test/java/org/opendaylight/unimgr/it/UnimgrIT.java @@ -167,30 +167,42 @@ public class UnimgrIT extends AbstractMdsalTestBase { } @Test - public void testCreateEvc() { + public void createAndDeleteUNITest() { + UniAugmentation uni = new UniAugmentationBuilder() + .setMacAddress(new MacAddress(MAC_ADDRESS_1)) + .setMacLayer(MAC_LAYER) + .setMode(MODE) + .setMtuSize(BigInteger.valueOf(Long.valueOf(MTU_SIZE))) + .setPhysicalMedium(PHY_MEDIUM) + .setSpeed(null) + .setType(TYPE) + .setIpAddress(new IpAddress(IP_1.toCharArray())) + .build(); + + InstanceIdentifier nodePath = createUniNode(MAC_ADDRESS_1, IP_1); + Assert.assertTrue(validateUni(true, nodePath)); + + InstanceIdentifier deletedNodePath = deleteNode(MAC_ADDRESS_1, IP_1); + Assert.assertTrue(validateUni(false, deletedNodePath)); + } + + @Test + public void testCreateAndDeleteEvc() { LOG.info("Test for create Evc"); // Create an evc between the two Uni nodes - InstanceIdentifier evcIid = createOrDeleteEvcLink(IP_1, MAC_ADDRESS_1, IP_2, MAC_ADDRESS_2, EVC_ID_1, true); + InstanceIdentifier evcIid = createEvcLink(IP_1, MAC_ADDRESS_1, IP_2, MAC_ADDRESS_2, EVC_ID_1); Assert.assertNotNull(evcIid); // Validate Evc create operation boolean status = validateEvc(true, EVC_ID_1); Assert.assertTrue(status); - } - - @Test - public void testDeleteEvc() { - LOG.info("Test for delete Evc"); - // Create an evc between the two Uni nodes - InstanceIdentifier evcIid = createOrDeleteEvcLink(IP_1, MAC_ADDRESS_1, IP_2, MAC_ADDRESS_2, EVC_ID_1, true); - Assert.assertNotNull(evcIid); //Delete the Evc - evcIid = createOrDeleteEvcLink(IP_1, MAC_ADDRESS_1, IP_2, MAC_ADDRESS_2, EVC_ID_1, false); + evcIid = deleteEvc(EVC_ID_1); Assert.assertNotNull(evcIid); // Validate Evc delete operation - boolean status = validateEvc(false, EVC_ID_1); + status = validateEvc(false, EVC_ID_1); Assert.assertTrue(status); } @@ -238,9 +250,26 @@ public class UnimgrIT extends AbstractMdsalTestBase { return uniNodeIid; } - private InstanceIdentifier createOrDeleteEvcLink(String srcUniIp, String srcMac, - String dstUniIp, String dstMac, String evcId, - boolean isCreate) { + private InstanceIdentifier deleteEvc(String evcId) { + LinkId evcLinkId = new LinkId(new LinkId("evc://" + evcId)); + InstanceIdentifier evcLinkIid = null; + evcLinkIid = getEvcLinkIid(evcId); + if (evcLinkIid != null) { + WriteTransaction transaction = dataBroker.newWriteOnlyTransaction(); + transaction.delete(LogicalDatastoreType.CONFIGURATION, evcLinkIid); + CheckedFuture future = transaction.submit(); + try { + future.checkedGet(); + } catch (TransactionCommitFailedException e) { + LOG.error("Error while deleting Evc {}", evcLinkIid); + } + LOG.info("Deleted an Evc link {}", evcLinkId); + } + return evcLinkIid; + } + + private InstanceIdentifier createEvcLink(String srcUniIp, String srcMac, + String dstUniIp, String dstMac, String evcId) { // Create two Uni nodes before creating an Evc InstanceIdentifier uniIid = createUniNode(srcMac, srcUniIp); Assert.assertNotNull(uniIid); @@ -278,35 +307,23 @@ public class UnimgrIT extends AbstractMdsalTestBase { InstanceIdentifier evcLinkIid = null; try { evcLinkIid = getEvcLinkIid(evcId); - if(isCreate) { - LinkKey evcLinkKey = new LinkKey(evcLinkId); - Source mandatorySrcNode = new SourceBuilder().setSourceNode(new NodeId("uni://" + srcUniIp)).build(); - Destination mandatoryDstNode = new DestinationBuilder().setDestNode(new NodeId("uni://" + dstUniIp)).build(); - Link linkData = new LinkBuilder() + LinkKey evcLinkKey = new LinkKey(evcLinkId); + Source mandatorySrcNode = new SourceBuilder().setSourceNode(new NodeId("uni://" + srcUniIp)).build(); + Destination mandatoryDstNode = new DestinationBuilder().setDestNode(new NodeId("uni://" + dstUniIp)).build(); + Link linkData = new LinkBuilder() .setKey(evcLinkKey) .setSource(mandatorySrcNode) .setDestination(mandatoryDstNode) .setLinkId(evcLinkId) .addAugmentation(EvcAugmentation.class, evc) .build(); - WriteTransaction transaction = dataBroker.newWriteOnlyTransaction(); - transaction.put(LogicalDatastoreType.CONFIGURATION, evcLinkIid, linkData); - CheckedFuture future = transaction.submit(); - future.checkedGet(); - LOG.info("Created and submitted a new Evc link {}", evcLinkId); - } else { - WriteTransaction transaction = dataBroker.newWriteOnlyTransaction(); - transaction.delete(LogicalDatastoreType.CONFIGURATION, evcLinkIid); - CheckedFuture future = transaction.submit(); - future.checkedGet(); - LOG.info("Deleted an Evc link {}", evcLinkId); - } + WriteTransaction transaction = dataBroker.newWriteOnlyTransaction(); + transaction.put(LogicalDatastoreType.CONFIGURATION, evcLinkIid, linkData); + CheckedFuture future = transaction.submit(); + future.checkedGet(); + LOG.info("Created and submitted a new Evc link {}", evcLinkId); } catch (Exception e) { - if (isCreate) { - LOG.error("Exception while creating Evc " + "Evc link Id: {}, {}", evcLinkId, e); - } else { - LOG.error("Exception while deleting Evc " + "Evc link Id: {}, {}", evcLinkId, e); - } + LOG.error("Exception while creating Evc " + "Evc link Id: {}, {}", evcLinkId, e); return null; } return evcLinkIid; @@ -351,26 +368,6 @@ public class UnimgrIT extends AbstractMdsalTestBase { return result; } - @Test - public void createAndDeleteUNITest() { - UniAugmentation uni = new UniAugmentationBuilder() - .setMacAddress(new MacAddress(MAC_ADDRESS_1)) - .setMacLayer(MAC_LAYER) - .setMode(MODE) - .setMtuSize(BigInteger.valueOf(Long.valueOf(MTU_SIZE))) - .setPhysicalMedium(PHY_MEDIUM) - .setSpeed(null) - .setType(TYPE) - .setIpAddress(new IpAddress(IP_1.toCharArray())) - .build(); - - InstanceIdentifier nodePath = createUniNode(MAC_ADDRESS_1, IP_1); - Assert.assertTrue(validateUni(true, nodePath)); - - InstanceIdentifier deletedNodePath = deleteNode(MAC_ADDRESS_1, IP_1); - Assert.assertTrue(validateUni(false, deletedNodePath)); - } - private boolean validateUni(boolean forCreate, InstanceIdentifier iid) { Node uni = read(LogicalDatastoreType.CONFIGURATION, iid); if (forCreate && uni != null) { -- 2.36.6