add transaction to exists() 28/72728/5
authorMichael Vorburger <vorburger@redhat.com>
Wed, 6 Jun 2018 14:28:36 +0000 (16:28 +0200)
committerMichael Vorburger <vorburger@redhat.com>
Mon, 11 Jun 2018 15:06:18 +0000 (17:06 +0200)
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 <vorburger@redhat.com>
neutron-spi/pom.xml
neutron-spi/src/main/java/org/opendaylight/neutron/spi/INeutronCRUD.java
northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/NeutronLoadBalancerPoolNorthbound.java
transcriber/src/main/java/org/opendaylight/neutron/transcriber/AbstractTranscriberInterface.java

index 1fca14418a25fff6e9e34c5f4d10c60e34d5a053..fc384504e3dfd3638befed19cd7698149bfc3c08 100644 (file)
       <groupId>org.opendaylight.mdsal.model</groupId>
       <artifactId>yang-ext</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.opendaylight.controller</groupId>
+      <artifactId>sal-binding-api</artifactId>
+    </dependency>
     <dependency>
       <groupId>commons-net</groupId>
       <artifactId>commons-net</artifactId>
index b3f1a3a298d551089363c2d1e76d15c855cc1a88..e41642925d4210d199a5b6bb20046faea06aed44 100644 (file)
@@ -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<T extends INeutronObject<T>> {
 
     /**
-     * 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
index 83ec1f3d46d0bdbd4e220cf5a7786f78d6930cb6..310c59a5062d1310ea9ccc2771573fe2ba1847b4 100644 (file)
@@ -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<NeutronLoadBalancerPoolMember> members = loadBalancerPoolInterface.get(loadBalancerPoolUUID)
                 .getLoadBalancerPoolMembers();
@@ -241,8 +250,10 @@ public final class NeutronLoadBalancerPoolNorthbound extends AbstractNeutronNort
             @QueryParam("fields") List<String> 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<NeutronLoadBalancerPoolMember> members = loadBalancerPoolInterface.get(loadBalancerPoolUUID)
                 .getLoadBalancerPoolMembers();
index 9f31fd81a35fcbd6f1e27ed8a5d37e052976e1c8..12aa6f6afbdb01a5d14e39d2db4f5eeafe03b89f 100644 (file)
@@ -77,8 +77,6 @@ import org.slf4j.LoggerFactory;
  * @param <K> key type to indentify T
  * @param <S> Neutron-spi class
  * @param <V> parent of U
- *
- *
  */
 public abstract class AbstractTranscriberInterface<
         T extends DataObject & Identifiable<K> & ChildOf<? super U>,
@@ -86,6 +84,9 @@ public abstract class AbstractTranscriberInterface<
         K extends Identifier<T>, S extends INeutronObject<S>,
         V extends DataObject>
         implements AutoCloseable, INeutronCRUD<S> {
+
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractTranscriberInterface.class);
+
     // T extends DataObject & Identifiable<K> & ChildOf<? super U> as 0th type argument
     private static final int MD_LIST_CLASS_TYPE_INDEX = 0;
     // U extends ChildOf<? super Neutron> & Augmentable<U> as 1st type argument
@@ -95,7 +96,6 @@ public abstract class AbstractTranscriberInterface<
     // S extends INeutronObject<S> 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;