From ac69968554ceb35025026b0068e7d9c9e003c7d6 Mon Sep 17 00:00:00 2001 From: Michael Vorburger Date: Wed, 6 Jun 2018 16:28:36 +0200 Subject: [PATCH] add transaction to exists() make AbstractTranscriberInterface exists() public This allows one *Interface implementation (say NeutronSecurityRuleInterface) to check for the existence of others (say NeutronSecurityGroupInterface) in dependency checks. JIRA: NEUTRON-158 Change-Id: I351c219f6784180aa918c818fd10bd1cca85afc3 Signed-off-by: Michael Vorburger --- neutron-spi/pom.xml | 4 ++++ .../neutron/spi/INeutronCRUD.java | 11 +++++----- .../NeutronLoadBalancerPoolNorthbound.java | 21 ++++++++++++++----- .../AbstractTranscriberInterface.java | 17 ++++++--------- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/neutron-spi/pom.xml b/neutron-spi/pom.xml index 1fca14418..fc384504e 100644 --- a/neutron-spi/pom.xml +++ b/neutron-spi/pom.xml @@ -41,6 +41,10 @@ org.opendaylight.mdsal.model yang-ext + + org.opendaylight.controller + sal-binding-api + commons-net commons-net diff --git a/neutron-spi/src/main/java/org/opendaylight/neutron/spi/INeutronCRUD.java b/neutron-spi/src/main/java/org/opendaylight/neutron/spi/INeutronCRUD.java index b3f1a3a29..e41642925 100644 --- a/neutron-spi/src/main/java/org/opendaylight/neutron/spi/INeutronCRUD.java +++ b/neutron-spi/src/main/java/org/opendaylight/neutron/spi/INeutronCRUD.java @@ -8,6 +8,7 @@ package org.opendaylight.neutron.spi; import java.util.List; +import org.opendaylight.controller.md.sal.binding.api.ReadTransaction; /** * This interface defines the methods for CRUD of NB neutron objects. @@ -15,14 +16,14 @@ import java.util.List; public interface INeutronCRUD> { /** - * Applications call this interface method to determine if a particular - * Neutron object exists. + * Applications call this interface method to determine if a particular Neutron + * object exists. * - * @param uuid - * UUID of the Neutron object + * @param uuid UUID of the Neutron object + * @param tx the ReadTransaction within which to perform the check * @return boolean */ - boolean exists(String uuid); + boolean exists(String uuid, ReadTransaction tx); /** * Applications call this interface method to return if a particular diff --git a/northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/NeutronLoadBalancerPoolNorthbound.java b/northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/NeutronLoadBalancerPoolNorthbound.java index 83ec1f3d4..310c59a50 100644 --- a/northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/NeutronLoadBalancerPoolNorthbound.java +++ b/northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/NeutronLoadBalancerPoolNorthbound.java @@ -25,6 +25,8 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.codehaus.enunciate.jaxrs.ResponseCode; import org.codehaus.enunciate.jaxrs.StatusCodes; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; import org.opendaylight.neutron.spi.INeutronLoadBalancerPoolCRUD; import org.opendaylight.neutron.spi.NeutronLoadBalancerPool; import org.opendaylight.neutron.spi.NeutronLoadBalancerPoolMember; @@ -43,9 +45,14 @@ public final class NeutronLoadBalancerPoolNorthbound extends AbstractNeutronNort private static final String RESOURCE_NAME = "LoadBalancerPool"; + private final DataBroker dataBroker; + @Inject - public NeutronLoadBalancerPoolNorthbound(@OsgiService INeutronLoadBalancerPoolCRUD neutronCRUD) { + public NeutronLoadBalancerPoolNorthbound( + @OsgiService INeutronLoadBalancerPoolCRUD neutronCRUD, + @OsgiService DataBroker dataBroker) { super(neutronCRUD); + this.dataBroker = dataBroker; } @Override @@ -195,8 +202,10 @@ public final class NeutronLoadBalancerPoolNorthbound extends AbstractNeutronNort // sorting not supported ) { INeutronLoadBalancerPoolCRUD loadBalancerPoolInterface = getNeutronCRUD(); - if (!loadBalancerPoolInterface.exists(loadBalancerPoolUUID)) { - throw new ResourceNotFoundException(uuidNoExist()); + try (ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) { + if (!loadBalancerPoolInterface.exists(loadBalancerPoolUUID, tx)) { + throw new ResourceNotFoundException(uuidNoExist()); + } } List members = loadBalancerPoolInterface.get(loadBalancerPoolUUID) .getLoadBalancerPoolMembers(); @@ -241,8 +250,10 @@ public final class NeutronLoadBalancerPoolNorthbound extends AbstractNeutronNort @QueryParam("fields") List fields) { INeutronLoadBalancerPoolCRUD loadBalancerPoolInterface = getNeutronCRUD(); - if (!loadBalancerPoolInterface.exists(loadBalancerPoolUUID)) { - throw new ResourceNotFoundException(uuidNoExist()); + try (ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) { + if (!loadBalancerPoolInterface.exists(loadBalancerPoolUUID, tx)) { + throw new ResourceNotFoundException(uuidNoExist()); + } } List members = loadBalancerPoolInterface.get(loadBalancerPoolUUID) .getLoadBalancerPoolMembers(); diff --git a/transcriber/src/main/java/org/opendaylight/neutron/transcriber/AbstractTranscriberInterface.java b/transcriber/src/main/java/org/opendaylight/neutron/transcriber/AbstractTranscriberInterface.java index 9f31fd81a..12aa6f6af 100644 --- a/transcriber/src/main/java/org/opendaylight/neutron/transcriber/AbstractTranscriberInterface.java +++ b/transcriber/src/main/java/org/opendaylight/neutron/transcriber/AbstractTranscriberInterface.java @@ -77,8 +77,6 @@ import org.slf4j.LoggerFactory; * @param key type to indentify T * @param Neutron-spi class * @param parent of U - * - * */ public abstract class AbstractTranscriberInterface< T extends DataObject & Identifiable & ChildOf, @@ -86,6 +84,9 @@ public abstract class AbstractTranscriberInterface< K extends Identifier, S extends INeutronObject, V extends DataObject> implements AutoCloseable, INeutronCRUD { + + private static final Logger LOG = LoggerFactory.getLogger(AbstractTranscriberInterface.class); + // T extends DataObject & Identifiable & ChildOf as 0th type argument private static final int MD_LIST_CLASS_TYPE_INDEX = 0; // U extends ChildOf & Augmentable as 1st type argument @@ -95,7 +96,6 @@ public abstract class AbstractTranscriberInterface< // S extends INeutronObject as 3rd type argument private static final int NEUTRON_OBJECT_TYPE_INDEX = 3; - private static final Logger LOG = LoggerFactory.getLogger(AbstractTranscriberInterface.class); private static final int DEDASHED_UUID_LENGTH = 32; private static final int DEDASHED_UUID_START = 0; private static final int DEDASHED_UUID_DIV1 = 8; @@ -428,19 +428,13 @@ public abstract class AbstractTranscriberInterface< public void close() throws Exception { } - private boolean exists(String uuid, ReadTransaction tx) { + @Override + public boolean exists(String uuid, ReadTransaction tx) { Preconditions.checkNotNull(tx); final T dataObject = readMd(createInstanceIdentifier(toMd(uuid)), tx); return dataObject != null; } - @Override - public boolean exists(String uuid) { - try (ReadOnlyTransaction tx = getDataBroker().newReadOnlyTransaction()) { - return exists(uuid, tx); - } - } - private S get(String uuid, ReadTransaction tx) { Preconditions.checkNotNull(tx); final T dataObject = readMd(createInstanceIdentifier(toMd(uuid)), tx); @@ -499,6 +493,7 @@ public abstract class AbstractTranscriberInterface< try { return add(input, tx); } catch (InterruptedException | ExecutionException e) { + // TODO replace all this with org.opendaylight.genius.infra.RetryingManagedNewTransactionRunner if (e.getCause() instanceof OptimisticLockFailedException) { LOG.warn("Got OptimisticLockFailedException - {} {}", input, retries); continue; -- 2.36.6