+ private 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);
+ if (dataObject == null) {
+ return null;
+ }
+ return fromMd(dataObject);
+ }
+
+ @Override
+ public S get(String uuid) {
+ try (ReadOnlyTransaction tx = getDataBroker().newReadOnlyTransaction()) {
+ return get(uuid, tx);
+ }
+ }
+
+ protected abstract List<T> getDataObjectList(U dataObjects);
+
+ private List<S> getAll(ReadTransaction tx) {
+ Preconditions.checkNotNull(tx);
+ final Set<S> allNeutronObjects = new HashSet<S>();
+ final U dataObjects = readMd(createInstanceIdentifier(), tx);
+ if (dataObjects != null) {
+ for (final T dataObject: getDataObjectList(dataObjects)) {
+ allNeutronObjects.add(fromMd(dataObject));
+ }
+ }
+ LOGGER.debug("Exiting _getAll, Found {} OpenStackFirewall", allNeutronObjects.size());
+ final List<S> ans = new ArrayList<S>();
+ ans.addAll(allNeutronObjects);
+ return ans;
+ }
+
+ @Override
+ public List<S> getAll() {
+ try (ReadOnlyTransaction tx = getDataBroker().newReadOnlyTransaction()) {
+ return getAll(tx);
+ }
+ }
+
+ private boolean add(S input, ReadWriteTransaction tx) throws InterruptedException, ExecutionException {
+ Preconditions.checkNotNull(tx);
+ if (exists(input.getID(), tx)) {
+ tx.cancel();
+ return false;
+ }
+ addMd(input, tx);
+ return true;
+ }
+
+ @Override
+ public boolean add(S input) {
+ int retries = RETRY_MAX;
+ while (retries-- >= 0) {
+ final ReadWriteTransaction tx = getDataBroker().newReadWriteTransaction();
+ try {
+ return add(input, tx);
+ } catch (InterruptedException | ExecutionException e) {
+ if (e.getCause() instanceof OptimisticLockFailedException) {
+ LOGGER.warn("Got OptimisticLockFailedException - {} {}", input, retries);
+ continue;
+ }
+ // TODO: rethrow exception. don't mask exception
+ LOGGER.error("Transaction failed", e);
+ }
+ break;
+ }
+ return false;
+ }
+
+ private boolean remove(String uuid, ReadWriteTransaction tx) throws InterruptedException, ExecutionException {
+ Preconditions.checkNotNull(tx);
+ if (!exists(uuid, tx)) {
+ tx.cancel();
+ return false;
+ }
+ removeMd(toMd(uuid), tx);
+ return true;
+ }
+
+ @Override
+ public boolean remove(String uuid) {
+ int retries = RETRY_MAX;
+ while (retries-- >= 0) {
+ final ReadWriteTransaction tx = getDataBroker().newReadWriteTransaction();
+ try {
+ return remove(uuid, tx);
+ } catch (InterruptedException | ExecutionException e) {
+ if (e.getCause() instanceof OptimisticLockFailedException) {
+ LOGGER.warn("Got OptimisticLockFailedException - {} {}", uuid, retries);
+ continue;
+ }
+ // TODO: rethrow exception. don't mask exception
+ LOGGER.error("Transaction failed", e);
+ }
+ break;
+ }
+ return false;
+ }
+
+ private boolean update(String uuid, S delta, ReadWriteTransaction tx) throws InterruptedException, ExecutionException {
+ Preconditions.checkNotNull(tx);
+ if (!exists(uuid, tx)) {
+ tx.cancel();
+ return false;
+ }
+ updateMd(delta, tx);
+ return true;
+ }
+
+ @Override
+ public boolean update(String uuid, S delta) {
+ int retries = RETRY_MAX;
+ while (retries-- >= 0) {
+ final ReadWriteTransaction tx = getDataBroker().newReadWriteTransaction();
+ try {
+ return update(uuid, delta, tx);
+ } catch (InterruptedException | ExecutionException e) {
+ if (e.getCause() instanceof OptimisticLockFailedException) {
+ LOGGER.warn("Got OptimisticLockFailedException - {} {} {}", uuid, delta, retries);
+ continue;
+ }
+ // TODO: rethrow exception. don't mask exception
+ LOGGER.error("Transaction failed", e);
+ }
+ break;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean inUse(String uuid) {
+ return false;
+ }