Cleanup use of deprecated constructs
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / handlers / SchemaContextHandler.java
index ee29d0afe2c611ff463bbf84a054ca9509d43e65..daeb0c1443e1329748fee3d739dd4ba753770352 100644 (file)
@@ -7,13 +7,22 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.handlers;
 
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.Throwables;
 import java.util.Collection;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.atomic.AtomicInteger;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.apache.aries.blueprint.annotation.service.Reference;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.IetfYangLibrary;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.MonitoringModule;
@@ -25,16 +34,15 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgum
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * Implementation of {@link SchemaContextHandler}.
- *
  */
+@Singleton
 @SuppressWarnings("checkstyle:FinalClass")
 public class SchemaContextHandler implements SchemaContextListenerHandler, AutoCloseable {
 
@@ -44,31 +52,29 @@ public class SchemaContextHandler implements SchemaContextListenerHandler, AutoC
 
     private final TransactionChainHandler transactionChainHandler;
     private final DOMSchemaService domSchemaService;
-    private ListenerRegistration<SchemaContextListener> listenerRegistration;
+    private ListenerRegistration<?> listenerRegistration;
 
-    private volatile SchemaContext schemaContext;
+    private volatile EffectiveModelContext schemaContext;
 
     /**
      * Constructor.
      *
      * @param transactionChainHandler Transaction chain handler
      */
-    private SchemaContextHandler(final TransactionChainHandler transactionChainHandler,
-            final DOMSchemaService domSchemaService) {
+    @Inject
+    public SchemaContextHandler(final TransactionChainHandler transactionChainHandler,
+            final @Reference DOMSchemaService domSchemaService) {
         this.transactionChainHandler = transactionChainHandler;
         this.domSchemaService = domSchemaService;
     }
 
-    public static SchemaContextHandler newInstance(TransactionChainHandler transactionChainHandler,
-            DOMSchemaService domSchemaService) {
-        return new SchemaContextHandler(transactionChainHandler, domSchemaService);
-    }
-
+    @PostConstruct
     public void init() {
         listenerRegistration = domSchemaService.registerSchemaContextListener(this);
     }
 
     @Override
+    @PreDestroy
     public void close() {
         if (listenerRegistration != null) {
             listenerRegistration.close();
@@ -77,9 +83,8 @@ public class SchemaContextHandler implements SchemaContextListenerHandler, AutoC
 
     @Override
     @SuppressWarnings("checkstyle:hiddenField")
-    public void onGlobalContextUpdated(final SchemaContext context) {
-        Preconditions.checkNotNull(context);
-        schemaContext = context;
+    public void onModelContextUpdated(final EffectiveModelContext context) {
+        schemaContext = requireNonNull(context);
 
         final Module ietfYangLibraryModule =
                 context.findModule(IetfYangLibrary.MODULE_QNAME).orElse(null);
@@ -100,32 +105,37 @@ public class SchemaContextHandler implements SchemaContextListenerHandler, AutoC
     }
 
     @Override
-    public SchemaContext get() {
+    public EffectiveModelContext get() {
         return schemaContext;
     }
 
     private void putData(
             final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode) {
-        final DOMDataWriteTransaction wTx = this.transactionChainHandler.get().newWriteOnlyTransaction();
+        final DOMTransactionChain transactionChain = this.transactionChainHandler.get();
+        final DOMDataTreeWriteTransaction wTx = transactionChain.newWriteOnlyTransaction();
         wTx.put(LogicalDatastoreType.OPERATIONAL,
                 YangInstanceIdentifier.create(NodeIdentifier.create(normNode.getNodeType())), normNode);
         try {
-            wTx.submit().checkedGet();
-        } catch (final TransactionCommitFailedException e) {
-            if (!(e.getCause() instanceof ConflictingModificationAppliedException)) {
-                throw new RestconfDocumentedException("Problem occurred while putting data to DS.", e);
+            wTx.commit().get();
+        } catch (InterruptedException e) {
+            throw new RestconfDocumentedException("Problem occurred while putting data to DS.", e);
+        } catch (ExecutionException e) {
+            final TransactionCommitFailedException failure = Throwables.getCauseAs(e,
+                TransactionCommitFailedException.class);
+            if (failure.getCause() instanceof ConflictingModificationAppliedException) {
+                /*
+                 * Ignore error when another cluster node is already putting the same data to DS.
+                 * We expect that cluster is homogeneous and that node was going to write the same data
+                 * (that means no retry is needed). Transaction chain reset must be invoked to be able
+                 * to continue writing data with another transaction after failed transaction.
+                 * This is workaround for bug https://bugs.opendaylight.org/show_bug.cgi?id=7728
+                 */
+                LOG.warn("Ignoring that another cluster node is already putting the same data to DS.", e);
+            } else {
+                throw new RestconfDocumentedException("Problem occurred while putting data to DS.", failure);
             }
-
-            /*
-              Ignore error when another cluster node is already putting the same data to DS.
-              We expect that cluster is homogeneous and that node was going to write the same data
-              (that means no retry is needed). Transaction chain reset must be invoked to be able
-              to continue writing data with another transaction after failed transaction.
-              This is workaround for bug:
-              https://bugs.opendaylight.org/show_bug.cgi?id=7728
-            */
-            LOG.warn("Ignoring that another cluster node is already putting the same data to DS.", e);
-            this.transactionChainHandler.reset();
+        } finally {
+            transactionChain.close();
         }
     }
 }