Use SchemaContextHandler non-statically 31/71131/2
authorTom Pantelis <tompantelis@gmail.com>
Thu, 19 Apr 2018 22:16:03 +0000 (18:16 -0400)
committerTom Pantelis <tompantelis@gmail.com>
Fri, 20 Apr 2018 00:54:07 +0000 (20:54 -0400)
Removed the static SchemaContext field and made the SchemaContextHandler
instance static temporarily for the RestconfApplication but converts the
rest of the code to reference it non-statically. The static SchemaContextHandler
will be removed in a subsequent patch.

Change-Id: I9763ad472acdefbade511c47ee7e2f140f9d6ca1
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
30 files changed:
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/RestConnectorProvider.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/RestconfApplication.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/handlers/SchemaContextHandler.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/JsonNormalizedNodeBodyReader.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/XmlNormalizedNodeBodyReader.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/AbstractToPatchBodyReader.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonToPatchBodyReader.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlToPatchBodyReader.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/spi/AbstractIdentifierAwareJaxRsProvider.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/spi/AbstractNormalizedNodeBodyReader.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/JSONRestconfServiceRfc8040Impl.java
restconf/restconf-nb-rfc8040/src/main/resources/org/opendaylight/blueprint/restconf-bp.xml
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/RestConnectorProviderTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/TestUtils.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/handlers/SchemaContextHandlerTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/XmlBodyReaderMountPointTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReaderMountPointTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/JsonPatchBodyReaderTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReaderMountPointTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/patch/XmlPatchBodyReaderTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/test/AbstractBodyReaderTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/test/JsonBodyReaderTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/test/XmlBodyReaderTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/JSONRestconfServiceRfc8040ImplTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImplTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfInvokeOperationsServiceImplTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfStreamsSubscriptionServiceImplTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/services/simple/impl/RestconfImplTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/services/simple/impl/RestconfOperationsServiceTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/ListenerAdapterTest.java

index 4cf3aa1636e4b2e0bddf49608a20ee53df7660a2..e13a2f691ed2a04769715eacf8144168cf451431 100644 (file)
@@ -22,8 +22,6 @@ import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
 import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServiceWrapper;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -44,15 +42,13 @@ public class RestConnectorProvider<T extends ServiceWrapper> implements Restconf
     private final DOMSchemaService domSchemaService;
     private final TransactionChainHandler transactionChainHandler;
     private final DOMDataBroker dataBroker;
-
-    private ListenerRegistration<SchemaContextListener> listenerRegistration;
-    private SchemaContextHandler schemaCtxHandler;
+    private final SchemaContextHandler schemaCtxHandler;
     private final T wrapperServices;
 
     public RestConnectorProvider(final DOMDataBroker domDataBroker, final DOMSchemaService domSchemaService,
             final DOMRpcService rpcService, final DOMNotificationService notificationService,
             final DOMMountPointService mountPointService, final TransactionChainHandler transactionChainHandler,
-            final T wrapperServices) {
+            final SchemaContextHandler schemaCtxHandler, final T wrapperServices) {
         this.wrapperServices = wrapperServices;
         this.domSchemaService = Preconditions.checkNotNull(domSchemaService);
         this.rpcService = Preconditions.checkNotNull(rpcService);
@@ -60,6 +56,7 @@ public class RestConnectorProvider<T extends ServiceWrapper> implements Restconf
         this.mountPointService = Preconditions.checkNotNull(mountPointService);
         this.transactionChainHandler = Preconditions.checkNotNull(transactionChainHandler);
         this.dataBroker = Preconditions.checkNotNull(domDataBroker);
+        this.schemaCtxHandler = Preconditions.checkNotNull(schemaCtxHandler);
     }
 
     public synchronized void start() {
@@ -67,9 +64,6 @@ public class RestConnectorProvider<T extends ServiceWrapper> implements Restconf
 
         final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
 
-        this.schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
-        this.listenerRegistration = domSchemaService.registerSchemaContextListener(this.schemaCtxHandler);
-
         final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
 
         final NotificationServiceHandler notificationServiceHandler =
@@ -95,12 +89,7 @@ public class RestConnectorProvider<T extends ServiceWrapper> implements Restconf
     }
 
     @Override
-    public void close() throws Exception {
-        // close registration
-        if (this.listenerRegistration != null) {
-            this.listenerRegistration.close();
-        }
-
+    public void close() {
         mountPointServiceHandler = null;
     }
 }
index e779281483c80b1a1c3b0e48c4e8e422af8e9b65..3c386490a4d3681974d93093f32e2bfa14354791 100644 (file)
@@ -11,6 +11,7 @@ import com.google.common.collect.ImmutableSet;
 import java.util.HashSet;
 import java.util.Set;
 import javax.ws.rs.core.Application;
+import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyReader;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.NormalizedNodeJsonBodyWriter;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.NormalizedNodeXmlBodyWriter;
@@ -24,14 +25,13 @@ import org.opendaylight.restconf.nb.rfc8040.jersey.providers.schema.SchemaExport
 import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServicesWrapperImpl;
 
 public class RestconfApplication extends Application {
+    private final SchemaContextHandler schemaContextHandler = SchemaContextHandler.instance();
 
     @Override
     public Set<Class<?>> getClasses() {
         return ImmutableSet.<Class<?>>builder()
                 .add(NormalizedNodeJsonBodyWriter.class).add(NormalizedNodeXmlBodyWriter.class)
-                .add(JsonNormalizedNodeBodyReader.class).add(XmlNormalizedNodeBodyReader.class)
                 .add(SchemaExportContentYinBodyWriter.class).add(SchemaExportContentYangBodyWriter.class)
-                .add(JsonToPatchBodyReader.class).add(XmlToPatchBodyReader.class)
                 .add(PatchJsonBodyWriter.class).add(PatchXmlBodyWriter.class)
                 .build();
     }
@@ -40,6 +40,10 @@ public class RestconfApplication extends Application {
     public Set<Object> getSingletons() {
         final Set<Object> singletons = new HashSet<>();
         singletons.add(ServicesWrapperImpl.getInstance());
+        singletons.add(new JsonNormalizedNodeBodyReader(schemaContextHandler));
+        singletons.add(new JsonToPatchBodyReader(schemaContextHandler));
+        singletons.add(new XmlNormalizedNodeBodyReader(schemaContextHandler));
+        singletons.add(new XmlToPatchBodyReader(schemaContextHandler));
         return singletons;
     }
 }
index 65534bf16fad720bf29b703cde7b70a3edaeabe2..aecbf8d6aed547fe4a773075446606168c40caef 100644 (file)
@@ -8,16 +8,17 @@
 package org.opendaylight.restconf.nb.rfc8040.handlers;
 
 import com.google.common.base.Preconditions;
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.Collection;
 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 org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.IetfYangLibrary;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.MonitoringModule;
 import org.opendaylight.restconf.nb.rfc8040.utils.mapping.RestconfMappingNodeUtil;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
@@ -26,6 +27,7 @@ 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.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;
 
@@ -33,25 +35,57 @@ import org.slf4j.LoggerFactory;
  * Implementation of {@link SchemaContextHandler}.
  *
  */
-public class SchemaContextHandler implements SchemaContextListenerHandler {
+@SuppressWarnings("checkstyle:FinalClass")
+public class SchemaContextHandler implements SchemaContextListenerHandler, AutoCloseable {
 
     private static final Logger LOG = LoggerFactory.getLogger(SchemaContextHandler.class);
 
-    @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
-    private static volatile SchemaContext schemaContext;
-
-    private final TransactionChainHandler transactionChainHandler;
+    private static final SchemaContextHandler INSTANCE = new SchemaContextHandler();
 
     private final AtomicInteger moduleSetId = new AtomicInteger(0);
 
+    private TransactionChainHandler transactionChainHandler;
+    private DOMSchemaService domSchemaService;
+    private ListenerRegistration<SchemaContextListener> listenerRegistration;
+
+    private volatile SchemaContext schemaContext;
+
     /**
      * Constructor.
      *
      * @param transactionChainHandler Transaction chain handler
      */
-    public SchemaContextHandler(final TransactionChainHandler transactionChainHandler) {
+    private SchemaContextHandler(final TransactionChainHandler transactionChainHandler,
+            final DOMSchemaService domSchemaService) {
         this.transactionChainHandler = transactionChainHandler;
-        schemaContext = null;
+        this.domSchemaService = domSchemaService;
+    }
+
+    @Deprecated
+    private SchemaContextHandler() {
+    }
+
+    @Deprecated
+    public static SchemaContextHandler instance() {
+        return INSTANCE;
+    }
+
+    public static SchemaContextHandler newInstance(TransactionChainHandler transactionChainHandler,
+            DOMSchemaService domSchemaService) {
+        INSTANCE.transactionChainHandler = transactionChainHandler;
+        INSTANCE.domSchemaService = domSchemaService;
+        return INSTANCE;
+    }
+
+    public void init() {
+        listenerRegistration = domSchemaService.registerSchemaContextListener(this);
+    }
+
+    @Override
+    public void close() {
+        if (listenerRegistration != null) {
+            listenerRegistration.close();
+        }
     }
 
     @Override
@@ -62,15 +96,20 @@ public class SchemaContextHandler implements SchemaContextListenerHandler {
 
         final Module ietfYangLibraryModule =
                 context.findModule(IetfYangLibrary.MODULE_QNAME).orElse(null);
-        NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
-                RestconfMappingNodeUtil.mapModulesByIetfYangLibraryYang(context.getModules(), ietfYangLibraryModule,
-                        context, String.valueOf(this.moduleSetId.incrementAndGet()));
-        putData(normNode);
+        if (ietfYangLibraryModule != null) {
+            NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
+                    RestconfMappingNodeUtil.mapModulesByIetfYangLibraryYang(context.getModules(), ietfYangLibraryModule,
+                            context, String.valueOf(this.moduleSetId.incrementAndGet()));
+            putData(normNode);
+        }
 
         final Module monitoringModule =
                 schemaContext.findModule(MonitoringModule.MODULE_QNAME).orElse(null);
-        normNode = RestconfMappingNodeUtil.mapCapabilites(monitoringModule);
-        putData(normNode);
+        if (monitoringModule != null) {
+            NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
+                    RestconfMappingNodeUtil.mapCapabilites(monitoringModule);
+            putData(normNode);
+        }
     }
 
     @Override
@@ -78,14 +117,6 @@ public class SchemaContextHandler implements SchemaContextListenerHandler {
         return schemaContext;
     }
 
-    public static SchemaContext getSchemaContext() {
-        return schemaContext;
-    }
-
-    public static void setSchemaContext(final SchemaContext context) {
-        schemaContext = context;
-    }
-
     private void putData(
             final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode) {
         final DOMDataWriteTransaction wTx = this.transactionChainHandler.get().newWriteOnlyTransaction();
index 83b927a7bbadcb498222c4be93c45ebc9f5cbb81..e03f60c805cf7abcf0b60bc12fb4861451550601 100644 (file)
@@ -25,6 +25,7 @@ import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040;
+import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi.AbstractNormalizedNodeBodyReader;
 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -50,9 +51,12 @@ import org.slf4j.LoggerFactory;
 @Provider
 @Consumes({ Rfc8040.MediaTypes.DATA + RestconfConstants.JSON, MediaType.APPLICATION_JSON })
 public class JsonNormalizedNodeBodyReader extends AbstractNormalizedNodeBodyReader {
-
     private static final Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class);
 
+    public JsonNormalizedNodeBodyReader(SchemaContextHandler schemaContextHandler) {
+        super(schemaContextHandler);
+    }
+
     @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
     protected NormalizedNodeContext readBody(final InstanceIdentifierContext<?> path, final InputStream entityStream)
index 54ea4fe3680acc2214e053e6ff62227435b19a89..a2e873a067927d82b880261e10f2dc303828ca03 100644 (file)
@@ -30,6 +30,7 @@ import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040;
+import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi.AbstractNormalizedNodeBodyReader;
 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
 import org.opendaylight.yangtools.util.xml.UntrustedXML;
@@ -63,6 +64,10 @@ import org.xml.sax.SAXException;
 public class XmlNormalizedNodeBodyReader extends AbstractNormalizedNodeBodyReader {
     private static final Logger LOG = LoggerFactory.getLogger(XmlNormalizedNodeBodyReader.class);
 
+    public XmlNormalizedNodeBodyReader(SchemaContextHandler schemaContextHandler) {
+        super(schemaContextHandler);
+    }
+
     @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
     protected NormalizedNodeContext readBody(final InstanceIdentifierContext<?> path, final InputStream entityStream)
index e2e7fdc21d9ffcd891794ce0b3fb26b0ae9c521f..963194001e7c8fad1164b3ca0fc653b7b0c79712 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
 
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.patch.PatchContext;
+import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi.AbstractIdentifierAwareJaxRsProvider;
 
 /**
@@ -17,6 +18,9 @@ import org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi.AbstractIdentif
  * @author Robert Varga
  */
 abstract class AbstractToPatchBodyReader extends AbstractIdentifierAwareJaxRsProvider<PatchContext> {
+    protected AbstractToPatchBodyReader(SchemaContextHandler schemaContextHandler) {
+        super(schemaContextHandler);
+    }
 
     @Override
     protected final PatchContext emptyBody(final InstanceIdentifierContext<?> path) {
index 9985f7686124ab7d7ef81050ee47ef6846632d17..489f8e0e3277bb1c635a5207d3a6a592c9076d82 100644 (file)
@@ -21,6 +21,7 @@ import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
+import java.util.concurrent.atomic.AtomicReference;
 import javax.annotation.Nonnull;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.WebApplicationException;
@@ -57,7 +58,9 @@ import org.slf4j.LoggerFactory;
 public class JsonToPatchBodyReader extends AbstractToPatchBodyReader {
     private static final Logger LOG = LoggerFactory.getLogger(JsonToPatchBodyReader.class);
 
-    private String patchId;
+    public JsonToPatchBodyReader(SchemaContextHandler schemaContextHandler) {
+        super(schemaContextHandler);
+    }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
@@ -73,10 +76,11 @@ public class JsonToPatchBodyReader extends AbstractToPatchBodyReader {
     private PatchContext readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream)
             throws IOException {
         final JsonReader jsonReader = new JsonReader(new InputStreamReader(entityStream, StandardCharsets.UTF_8));
-        final List<PatchEntity> resultList = read(jsonReader, path);
+        AtomicReference<String> patchId = new AtomicReference<>();
+        final List<PatchEntity> resultList = read(jsonReader, path, patchId);
         jsonReader.close();
 
-        return new PatchContext(path, resultList, patchId);
+        return new PatchContext(path, resultList, patchId.get());
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
@@ -84,7 +88,7 @@ public class JsonToPatchBodyReader extends AbstractToPatchBodyReader {
             RestconfDocumentedException {
         try {
             return readFrom(
-                    ParserIdentifier.toInstanceIdentifier(uriPath, SchemaContextHandler.getSchemaContext(),
+                    ParserIdentifier.toInstanceIdentifier(uriPath, getSchemaContext(),
                             Optional.of(RestConnectorProvider.getMountPointService())), entityStream);
         } catch (final Exception e) {
             propagateExceptionAs(e);
@@ -106,7 +110,8 @@ public class JsonToPatchBodyReader extends AbstractToPatchBodyReader {
                 ErrorTag.MALFORMED_MESSAGE, exception);
     }
 
-    private List<PatchEntity> read(final JsonReader in, final InstanceIdentifierContext<?> path) throws IOException {
+    private List<PatchEntity> read(final JsonReader in, final InstanceIdentifierContext<?> path,
+            final AtomicReference<String> patchId) throws IOException {
         final List<PatchEntity> resultCollection = new ArrayList<>();
         final StringModuleInstanceIdentifierCodec codec = new StringModuleInstanceIdentifierCodec(
                 path.getSchemaContext());
@@ -133,7 +138,7 @@ public class JsonToPatchBodyReader extends AbstractToPatchBodyReader {
                 case END_DOCUMENT:
                     break;
                 case NAME:
-                    parseByName(in.nextName(), edit, in, path, codec, resultCollection);
+                    parseByName(in.nextName(), edit, in, path, codec, resultCollection, patchId);
                     break;
                 case END_OBJECT:
                     in.endObject();
@@ -164,7 +169,8 @@ public class JsonToPatchBodyReader extends AbstractToPatchBodyReader {
     private void parseByName(@Nonnull final String name, @Nonnull final PatchEdit edit,
                              @Nonnull final JsonReader in, @Nonnull final InstanceIdentifierContext<?> path,
                              @Nonnull final StringModuleInstanceIdentifierCodec codec,
-                             @Nonnull final List<PatchEntity> resultCollection) throws IOException {
+                             @Nonnull final List<PatchEntity> resultCollection,
+                             @Nonnull final AtomicReference<String> patchId) throws IOException {
         switch (name) {
             case "edit":
                 if (in.peek() == JsonToken.BEGIN_ARRAY) {
@@ -185,7 +191,7 @@ public class JsonToPatchBodyReader extends AbstractToPatchBodyReader {
 
                 break;
             case "patch-id":
-                this.patchId = in.nextString();
+                patchId.set(in.nextString());
                 break;
             default:
                 break;
index 7af6e56b32c14e2b35de0c58eebf5aeea42f02cd..338c605867e34178127e0376982b2fcd26387332 100644 (file)
@@ -34,6 +34,7 @@ import org.opendaylight.restconf.common.patch.PatchEditOperation;
 import org.opendaylight.restconf.common.patch.PatchEntity;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040;
 import org.opendaylight.restconf.nb.rfc8040.codecs.StringModuleInstanceIdentifierCodec;
+import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
 import org.opendaylight.yangtools.util.xml.UntrustedXML;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -66,6 +67,10 @@ public class XmlToPatchBodyReader extends AbstractToPatchBodyReader {
     private static final Logger LOG = LoggerFactory.getLogger(XmlToPatchBodyReader.class);
     private static final Splitter SLASH_SPLITTER = Splitter.on('/');
 
+    public XmlToPatchBodyReader(SchemaContextHandler schemaContextHandler) {
+        super(schemaContextHandler);
+    }
+
     @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
     protected PatchContext readBody(final InstanceIdentifierContext<?> path, final InputStream entityStream)
index 8028ee94d8cd5c1bd2ccc386f74feda47d654662..a3398aa7b2af47f56f815a62eaf69b752e02436b 100644 (file)
@@ -27,6 +27,7 @@ import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
 public abstract class AbstractIdentifierAwareJaxRsProvider<T> implements MessageBodyReader<T> {
 
@@ -36,6 +37,12 @@ public abstract class AbstractIdentifierAwareJaxRsProvider<T> implements Message
     @Context
     private Request request;
 
+    private final SchemaContextHandler schemaContextHandler;
+
+    protected AbstractIdentifierAwareJaxRsProvider(SchemaContextHandler schemaContextHandler) {
+        this.schemaContextHandler = schemaContextHandler;
+    }
+
     @Override
     public final boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
             final MediaType mediaType) {
@@ -78,9 +85,7 @@ public abstract class AbstractIdentifierAwareJaxRsProvider<T> implements Message
     }
 
     private InstanceIdentifierContext<?> getInstanceIdentifierContext() {
-        return ParserIdentifier.toInstanceIdentifier(
-                getIdentifier(),
-                SchemaContextHandler.getSchemaContext(),
+        return ParserIdentifier.toInstanceIdentifier(getIdentifier(), getSchemaContext(),
                 Optional.of(RestConnectorProvider.getMountPointService()));
     }
 
@@ -88,6 +93,10 @@ public abstract class AbstractIdentifierAwareJaxRsProvider<T> implements Message
         return this.uriInfo;
     }
 
+    protected SchemaContext getSchemaContext() {
+        return schemaContextHandler.get();
+    }
+
     protected boolean isPost() {
         return HttpMethod.POST.equals(this.request.getMethod());
     }
index 61147543e22849d4a641013869e857acdae6b87c..3e2862ee7107816d123a585909daf5daa3f1d8c0 100644 (file)
@@ -12,6 +12,7 @@ import javax.ws.rs.core.Request;
 import javax.ws.rs.core.UriInfo;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
+import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 
 /**
  * Common superclass for readers producing {@link NormalizedNodeContext}.
@@ -21,6 +22,9 @@ import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 @Beta
 public abstract class AbstractNormalizedNodeBodyReader
         extends AbstractIdentifierAwareJaxRsProvider<NormalizedNodeContext> {
+    protected AbstractNormalizedNodeBodyReader(SchemaContextHandler schemaContextHandler) {
+        super(schemaContextHandler);
+    }
 
     public final void injectParams(final UriInfo uriInfo, final Request request) {
         setUriInfo(uriInfo);
index 8ec497f31e49f302ed3b9c819f3e7fa83e93e6df..52664062ec135d7091da5424a0a155b413aa7812 100644 (file)
@@ -60,11 +60,14 @@ public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, Auto
 
     private final TransactionServicesWrapper services;
     private final DOMMountPointServiceHandler mountPointServiceHandler;
+    private final SchemaContextHandler schemaContextHandler;
 
     public JSONRestconfServiceRfc8040Impl(final TransactionServicesWrapper services,
-            final DOMMountPointServiceHandler mountPointServiceHandler) {
+            final DOMMountPointServiceHandler mountPointServiceHandler,
+            final SchemaContextHandler schemaContextHandler) {
         this.services = services;
         this.mountPointServiceHandler = mountPointServiceHandler;
+        this.schemaContextHandler = schemaContextHandler;
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
@@ -192,7 +195,7 @@ public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, Auto
 
         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
 
-        JsonToPatchBodyReader jsonToPatchBodyReader = new JsonToPatchBodyReader();
+        JsonToPatchBodyReader jsonToPatchBodyReader = new JsonToPatchBodyReader(schemaContextHandler);
         final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream);
 
         LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
@@ -214,8 +217,7 @@ public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, Auto
     private NormalizedNodeContext toNormalizedNodeContext(final String uriPath, @Nullable final String payload,
             final boolean isPost) throws OperationFailedException {
         final InstanceIdentifierContext<?> instanceIdentifierContext = ParserIdentifier.toInstanceIdentifier(
-                uriPath, SchemaContextHandler.getSchemaContext(),
-                Optional.of(mountPointServiceHandler.get()));
+                uriPath, schemaContextHandler.get(), Optional.of(mountPointServiceHandler.get()));
 
         if (payload == null) {
             return new NormalizedNodeContext(instanceIdentifierContext, null);
index acf12a81193b605180716ad2f18c2cacf3a7e682..943639892e9fcc8c2f474de6f77c704bb85162a0 100644 (file)
     <argument ref="domDataBroker"/>
   </bean>
 
+  <bean id="schemaContextHandler" class="org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler"
+      factory-method="newInstance" init-method="init" destroy-method="close">
+    <argument ref="transactionChainHandler"/>
+    <argument ref="domSchemaService"/>
+  </bean>
+
   <bean id="restconfProvider" class="org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider"
           init-method="start" destroy-method="close">
     <argument ref="domDataBroker"/>
@@ -52,6 +58,7 @@
     <argument ref="domNotificationService"/>
     <argument ref="domMountPointService"/>
     <argument ref="transactionChainHandler"/>
+    <argument ref="schemaContextHandler"/>
     <argument ref="wrapper"/>
   </bean>
 
@@ -66,6 +73,7 @@
     <argument>
       <bean factory-ref="restconfProvider" factory-method="getMountPointServiceHandler" />
     </argument>
+    <argument ref="schemaContextHandler"/>
   </bean>
 
   <service ref="jsonRestconfService" odl:type="rfc8040"
index 40152962518021f1a733f515c1252262c1e37208..767f94e468f4b221babb0fd797a17e9ce6517188 100644 (file)
@@ -57,9 +57,11 @@ public class RestConnectorProviderTest {
         doReturn(mockRegistration).when(domSchemaService).registerSchemaContextListener(
                 Mockito.any(SchemaContextHandler.class));
 
+        final TransactionChainHandler transactionChainHandler = new TransactionChainHandler(mockDataBroker);
         this.connectorProvider = new RestConnectorProvider<>(mockDataBroker, domSchemaService, mockRpcService,
-                mockNotificationService, mockMountPointService,
-                new TransactionChainHandler(mockDataBroker), ServicesWrapperImpl.getInstance());
+                mockNotificationService, mockMountPointService, transactionChainHandler,
+                SchemaContextHandler.newInstance(transactionChainHandler, domSchemaService),
+                ServicesWrapperImpl.getInstance());
     }
 
     /**
@@ -72,7 +74,6 @@ public class RestConnectorProviderTest {
 
         // verify interactions
         verify(mockDataBroker).createTransactionChain(Mockito.any());
-        verify(domSchemaService).registerSchemaContextListener(Mockito.any(SchemaContextHandler.class));
     }
 
     /**
@@ -93,8 +94,5 @@ public class RestConnectorProviderTest {
 
         // close
         this.connectorProvider.close();
-
-        // verify interaction
-        verify(this.mockRegistration).close();
     }
 }
index ab7ad9bf87acdff984bf85995e8e00e461ef86bc..be8b993ae3a107068f3913dc4a4b34310a714657 100644 (file)
@@ -8,8 +8,12 @@
 package org.opendaylight.restconf.nb.rfc8040;
 
 import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 
 import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.Futures;
 import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
@@ -35,6 +39,13 @@ import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
+import org.mockito.Mockito;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
+import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
 import org.opendaylight.yangtools.util.xml.UntrustedXML;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.Revision;
@@ -263,4 +274,18 @@ public final class TestUtils {
 
         return mapEntryNode.build();
     }
+
+    public static SchemaContextHandler newSchemaContextHandler(SchemaContext schemaContext) {
+        DOMDataBroker mockDataBroker = mock(DOMDataBroker.class);
+        DOMTransactionChain mockChain = mock(DOMTransactionChain.class);
+        DOMDataWriteTransaction mockTx = mock(DOMDataWriteTransaction.class);
+        doReturn(Futures.immediateCheckedFuture(null)).when(mockTx).submit();
+        doReturn(mockTx).when(mockChain).newWriteOnlyTransaction();
+
+        doReturn(mockChain).when(mockDataBroker).createTransactionChain(any());
+        SchemaContextHandler schemaContextHandler = SchemaContextHandler.newInstance(
+                new TransactionChainHandler(mockDataBroker), Mockito.mock(DOMSchemaService.class));
+        schemaContextHandler.onGlobalContextUpdated(schemaContext);
+        return schemaContextHandler;
+    }
 }
index ef4c63b985053c1123c290b761b72e8eb7194b38..8ea9b45275659b45c14407c8e9d291ed1e1c7fc6 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.restconf.nb.rfc8040.handlers;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertNotNull;
 
 import com.google.common.util.concurrent.CheckedFuture;
 import org.junit.Before;
@@ -19,7 +18,9 @@ import org.mockito.Mockito;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
@@ -33,6 +34,7 @@ public class SchemaContextHandlerTest {
 
     private SchemaContextHandler schemaContextHandler;
     private SchemaContext schemaContext;
+    private final DOMSchemaService mockDOMSchemaService = Mockito.mock(DOMSchemaService.class);
 
     @Before
     public void setup() throws Exception {
@@ -44,7 +46,9 @@ public class SchemaContextHandlerTest {
         final CheckedFuture<Void,TransactionCommitFailedException> checked = Mockito.mock(CheckedFuture.class);
         Mockito.when(wTx.submit()).thenReturn(checked);
         Mockito.when(checked.checkedGet()).thenReturn(null);
-        this.schemaContextHandler = new SchemaContextHandler(txHandler);
+
+
+        this.schemaContextHandler = SchemaContextHandler.newInstance(txHandler, mockDOMSchemaService);
 
         this.schemaContext =
                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_ACTUAL_SCHEMA_CONTEXT));
@@ -52,11 +56,21 @@ public class SchemaContextHandlerTest {
     }
 
     /**
-     * Testing init of {@link SchemaContextHandler}.
+     * Testing init and close.
      */
     @Test
-    public void schemaContextHandlerImplInitTest() {
-        assertNotNull("Handler should be created and not null", this.schemaContextHandler);
+    public void testInitAndClose() {
+        ListenerRegistration<?> mockListenerReg = Mockito.mock(ListenerRegistration.class);
+        Mockito.doReturn(mockListenerReg).when(mockDOMSchemaService)
+            .registerSchemaContextListener(schemaContextHandler);
+
+        schemaContextHandler.init();
+
+        Mockito.verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler);
+
+        schemaContextHandler.close();
+
+        Mockito.verify(mockListenerReg).close();
     }
 
     /**
index 6290961e1ddf73b792c4c7cd054bf8760ca50553..aa33375b5701febaf6d7f8be19ae6852bb0188a4 100644 (file)
@@ -30,7 +30,6 @@ import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
-import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -54,7 +53,8 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
         URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
 
     public XmlBodyReaderMountPointTest() throws Exception {
-        this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
+        super(schemaContext);
+        this.xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler);
     }
 
     @Override
@@ -74,7 +74,6 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
         when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mountPointService);
         when(mountPointService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountPoint));
         when(mountPoint.getSchemaContext()).thenReturn(schemaContext);
-        SchemaContextHandler.setSchemaContext(schemaContext);
     }
 
     @Test
index fa0d482fde4f6bfae5036127bb42198c5da12a6c..1453027ed571b2692d0c3ec2815d186dbbc53ec1 100644 (file)
@@ -24,7 +24,6 @@ import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.patch.PatchContext;
-import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -37,7 +36,8 @@ public class JsonPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
     private final JsonToPatchBodyReader jsonToPatchBodyReader;
 
     public JsonPatchBodyReaderMountPointTest() throws Exception {
-        jsonToPatchBodyReader = new JsonToPatchBodyReader();
+        super(schemaContext);
+        jsonToPatchBodyReader = new JsonToPatchBodyReader(schemaContextHandler);
     }
 
     @Override
@@ -55,7 +55,6 @@ public class JsonPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
         when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mountPointService);
         when(mountPointService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountPoint));
         when(mountPoint.getSchemaContext()).thenReturn(schemaContext);
-        SchemaContextHandler.setSchemaContext(schemaContext);
     }
 
     @Test
index e5824c9217cffdf98267b068a4a25a1bbdb03079..770746ce81f8df7ba6d6307ff3ed18356c85f312 100644 (file)
@@ -21,7 +21,6 @@ import org.junit.Test;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.patch.PatchContext;
-import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
@@ -32,7 +31,8 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
     private static SchemaContext schemaContext;
 
     public JsonPatchBodyReaderTest() throws Exception {
-        jsonToPatchBodyReader = new JsonToPatchBodyReader();
+        super(schemaContext);
+        jsonToPatchBodyReader = new JsonToPatchBodyReader(schemaContextHandler);
     }
 
     @Override
@@ -44,7 +44,6 @@ public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
     public static void initialization() {
         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
         when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
-        SchemaContextHandler.setSchemaContext(schemaContext);
     }
 
     @Test
index 00c200ea273a93ab8cfea3bcbfb207396ee743dd..c6383310979ff181c28fe8219632e033c267cf68 100644 (file)
@@ -23,7 +23,6 @@ import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.patch.PatchContext;
-import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -36,7 +35,8 @@ public class XmlPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
     private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
 
     public XmlPatchBodyReaderMountPointTest() throws Exception {
-        xmlToPatchBodyReader = new XmlToPatchBodyReader();
+        super(schemaContext);
+        xmlToPatchBodyReader = new XmlToPatchBodyReader(schemaContextHandler);
     }
 
     @Override
@@ -54,7 +54,6 @@ public class XmlPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
         when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mountPointService);
         when(mountPointService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountPoint));
         when(mountPoint.getSchemaContext()).thenReturn(schemaContext);
-        SchemaContextHandler.setSchemaContext(schemaContext);
     }
 
     @Test
index aee4a976639234f7cc10aa3a08554330bb069291..f98f576a9581daa1ccc91b652f650b6b7e36bcfd 100644 (file)
@@ -20,7 +20,6 @@ import org.junit.Test;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.patch.PatchContext;
-import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
@@ -31,7 +30,8 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
     private static SchemaContext schemaContext;
 
     public XmlPatchBodyReaderTest() throws Exception {
-        xmlToPatchBodyReader = new XmlToPatchBodyReader();
+        super(schemaContext);
+        xmlToPatchBodyReader = new XmlToPatchBodyReader(schemaContextHandler);
     }
 
     @Override
@@ -43,7 +43,6 @@ public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
     public static void initialization() {
         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
         when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
-        SchemaContextHandler.setSchemaContext(schemaContext);
     }
 
     @Test
index 990340edaeec69bb9178174615cbfd717dffe066..a112fd00152a87710f7c079736daf0af30679995 100644 (file)
@@ -23,7 +23,9 @@ import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.patch.PatchContext;
 import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
+import org.opendaylight.restconf.nb.rfc8040.TestUtils;
 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
+import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi.AbstractIdentifierAwareJaxRsProvider;
 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
@@ -34,14 +36,17 @@ public abstract class AbstractBodyReaderTest {
             mock(DOMMountPointServiceHandler.class);
 
     protected final MediaType mediaType;
+    protected final SchemaContextHandler schemaContextHandler;
 
-    protected AbstractBodyReaderTest() throws NoSuchFieldException, IllegalAccessException {
+    protected AbstractBodyReaderTest(SchemaContext schemaContext) throws NoSuchFieldException, IllegalAccessException {
         mediaType = getMediaType();
 
         final Field mountPointServiceHandlerField =
                 RestConnectorProvider.class.getDeclaredField("mountPointServiceHandler");
         mountPointServiceHandlerField.setAccessible(true);
         mountPointServiceHandlerField.set(RestConnectorProvider.class, MOUNT_POINT_SERVICE_HANDLER);
+
+        schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
     }
 
     protected abstract MediaType getMediaType();
index 41283b63f89677cfe21d784e4977dccd45962fb6..b372915132bfa326a1a7d5c42a69f66ad1f2e9e5 100644 (file)
@@ -25,7 +25,6 @@ import org.junit.Test;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
-import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyReader;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
@@ -49,7 +48,8 @@ public class JsonBodyReaderTest extends AbstractBodyReaderTest {
         URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
 
     public JsonBodyReaderTest() throws Exception {
-        this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
+        super(schemaContext);
+        this.jsonBodyReader = new JsonNormalizedNodeBodyReader(schemaContextHandler);
     }
 
     @Override
@@ -64,7 +64,6 @@ public class JsonBodyReaderTest extends AbstractBodyReaderTest {
         testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
         schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
         when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
-        SchemaContextHandler.setSchemaContext(schemaContext);
     }
 
     @Test
index d3529d0ab07f6bf233e5a47718eb61ed553fd3a1..ead68d4848ae4dcfb15072067760149f848648ad 100644 (file)
@@ -29,7 +29,6 @@ import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
-import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.XmlNormalizedNodeBodyReader;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
@@ -53,7 +52,8 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
         URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
 
     public XmlBodyReaderTest() throws Exception {
-        this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
+        super(schemaContext);
+        this.xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler);
     }
 
     @Override
@@ -68,7 +68,6 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
         testFiles.addAll(TestRestconfUtils.loadFiles("/foo-xml-test/yang"));
         schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
         when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
-        SchemaContextHandler.setSchemaContext(schemaContext);
     }
 
     @Test
index c8821e8e7b842040e22b2fa051c051791941687d..681dd4db76ab9856ac32df822e6d541f299957bf 100644 (file)
@@ -128,9 +128,6 @@ public class JSONRestconfServiceRfc8040ImplTest {
     @Mock
     private DOMMountPointService mockMountPointService;
 
-    @Mock
-    private SchemaContextHandler mockSchemaContextHandler;
-
     @Mock
     private DOMDataBroker mockDOMDataBroker;
 
@@ -142,10 +139,11 @@ public class JSONRestconfServiceRfc8040ImplTest {
 
     private JSONRestconfServiceRfc8040Impl service;
 
+    private final SchemaContextHandler schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
+
     @BeforeClass
     public static void init() throws IOException, ReactorException {
         schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
-        SchemaContextHandler.setSchemaContext(schemaContext);
     }
 
     @SuppressWarnings("resource")
@@ -177,25 +175,22 @@ public class JSONRestconfServiceRfc8040ImplTest {
 
         doReturn(mockTxChain).when(mockDOMDataBroker).createTransactionChain(any());
 
-        doReturn(schemaContext).when(mockSchemaContextHandler).get();
-
         final TransactionChainHandler txChainHandler = new TransactionChainHandler(mockDOMDataBroker);
 
         final DOMMountPointServiceHandler mountPointServiceHandler =
                 new DOMMountPointServiceHandler(mockMountPointService);
 
         final DOMNotificationService mockNotificationService = mock(DOMNotificationService.class);
-        ServicesWrapperImpl.getInstance().setHandlers(mockSchemaContextHandler, mountPointServiceHandler,
+        ServicesWrapperImpl.getInstance().setHandlers(schemaContextHandler, mountPointServiceHandler,
                 txChainHandler, new DOMDataBrokerHandler(mockDOMDataBroker),
                 new RpcServiceHandler(mockRpcService),
                 new NotificationServiceHandler(mockNotificationService), domSchemaService);
 
-        service = new JSONRestconfServiceRfc8040Impl(ServicesWrapperImpl.getInstance(), mountPointServiceHandler);
+        service = new JSONRestconfServiceRfc8040Impl(ServicesWrapperImpl.getInstance(), mountPointServiceHandler,
+                schemaContextHandler);
 
         new RestConnectorProvider<>(mockDOMDataBroker, domSchemaService, mockRpcService, mockNotificationService,
-                mockMountPointService, txChainHandler, null).start();
-
-        SchemaContextHandler.setSchemaContext(schemaContext);
+                mockMountPointService, txChainHandler, schemaContextHandler, null).start();
     }
 
     private static String loadData(final String path) throws IOException {
index de78c34c9d831d46ecee4ddc61f389c3d30dd963..37e62b0bbc7f31e2af9eba94bfec3911e64bc090 100644 (file)
@@ -45,6 +45,7 @@ import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
@@ -184,7 +185,8 @@ public class RestconfDataServiceImplTest {
 
         transactionChainHandler = new TransactionChainHandler(mockDataBroker);
 
-        final SchemaContextHandler schemaContextHandler = new SchemaContextHandler(transactionChainHandler);
+        final SchemaContextHandler schemaContextHandler = SchemaContextHandler.newInstance(transactionChainHandler,
+                Mockito.mock(DOMSchemaService.class));
 
         schemaContextHandler.onGlobalContextUpdated(this.contextRef.get());
         this.dataService = new RestconfDataServiceImpl(schemaContextHandler, this.transactionChainHandler,
index 87998c82aa2bb8db233090e01c9d775242183c93..f8ab905c799167ef32ae7f05a5d795bfe16f92a7 100644 (file)
@@ -23,6 +23,7 @@ import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
@@ -61,7 +62,8 @@ public class RestconfInvokeOperationsServiceImplTest {
         final CheckedFuture<Void, TransactionCommitFailedException> checked = Mockito.mock(CheckedFuture.class);
         Mockito.when(wTx.submit()).thenReturn(checked);
         Mockito.when(checked.checkedGet()).thenReturn(null);
-        final SchemaContextHandler schemaContextHandler = new SchemaContextHandler(txHandler);
+        final SchemaContextHandler schemaContextHandler =
+                SchemaContextHandler.newInstance(txHandler, Mockito.mock(DOMSchemaService.class));
         schemaContextHandler.onGlobalContextUpdated(contextRef.get());
         this.invokeOperationsService =
                 new RestconfInvokeOperationsServiceImpl(this.rpcServiceHandler, schemaContextHandler);
index bda60dd71de79ba8079e15971582182b5d91f86e..b7d476fa075baa28298beb86238fe4360dc7c123 100644 (file)
@@ -42,6 +42,7 @@ import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.util.SimpleUriInfo;
@@ -70,9 +71,8 @@ public class RestconfStreamsSubscriptionServiceImplTest {
     private UriInfo uriInfo;
     @Mock
     private NotificationServiceHandler notificationServiceHandler;
-    @Mock
-    private TransactionChainHandler transactionHandler;
 
+    private TransactionChainHandler transactionHandler;
     private SchemaContextHandler schemaHandler;
 
     @SuppressWarnings("unchecked")
@@ -80,10 +80,7 @@ public class RestconfStreamsSubscriptionServiceImplTest {
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
 
-        final TransactionChainHandler txHandler = mock(TransactionChainHandler.class);
         final DOMTransactionChain domTx = mock(DOMTransactionChain.class);
-        Mockito.when(this.transactionHandler.get()).thenReturn(domTx);
-        Mockito.when(txHandler.get()).thenReturn(domTx);
         final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
         Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
         final DOMDataReadWriteTransaction rwTx = Mockito.mock(DOMDataReadWriteTransaction.class);
@@ -96,9 +93,12 @@ public class RestconfStreamsSubscriptionServiceImplTest {
         final CheckedFuture<Void, TransactionCommitFailedException> checked = mock(CheckedFuture.class);
         Mockito.when(wTx.submit()).thenReturn(checked);
         Mockito.when(checked.checkedGet()).thenReturn(null);
-        this.schemaHandler = new SchemaContextHandler(txHandler);
 
         final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
+        doReturn(domTx).when(dataBroker).createTransactionChain(any());
+
+        transactionHandler = new TransactionChainHandler(dataBroker);
+        schemaHandler = SchemaContextHandler.newInstance(transactionHandler, Mockito.mock(DOMSchemaService.class));
 
         DOMDataTreeChangeService dataTreeChangeService = mock(DOMDataTreeChangeService.class);
         doReturn(mock(ListenerRegistration.class)).when(dataTreeChangeService)
index dcfa41a80568166421d8b4e56779fcc6fc6ad751..bec5c7be12ec25c6f6295a294aa13807a21ade40 100644 (file)
@@ -7,18 +7,13 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.services.simple.impl;
 
-import com.google.common.util.concurrent.CheckedFuture;
 import org.junit.Assert;
 import org.junit.Test;
-import org.mockito.Mockito;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
-import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.IetfYangLibrary;
 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
+import org.opendaylight.restconf.nb.rfc8040.TestUtils;
 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
-import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
@@ -29,18 +24,7 @@ public class RestconfImplTest {
     public void restImplTest() throws Exception {
         final SchemaContext schemaContext =
                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/restconf/impl"));
-
-        final TransactionChainHandler txHandler = Mockito.mock(TransactionChainHandler.class);
-        final DOMTransactionChain domTx = Mockito.mock(DOMTransactionChain.class);
-        Mockito.when(txHandler.get()).thenReturn(domTx);
-        final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
-        Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
-        final CheckedFuture<Void, TransactionCommitFailedException> checked = Mockito.mock(CheckedFuture.class);
-        Mockito.when(wTx.submit()).thenReturn(checked);
-        Mockito.when(checked.checkedGet()).thenReturn(null);
-        final SchemaContextHandler schemaContextHandler = new SchemaContextHandler(txHandler);
-        schemaContextHandler.onGlobalContextUpdated(schemaContext);
-
+        final SchemaContextHandler schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
         final RestconfImpl restconfImpl = new RestconfImpl(schemaContextHandler);
         final NormalizedNodeContext libraryVersion = restconfImpl.getLibraryVersion();
         final LeafNode<?> value = (LeafNode<?>) libraryVersion.getData();
index 562a1f2bbec8d5698c1e64f7c2e63d8ff6c4dc92..078f27534b25a44ad7c3d9ee2a761b9fda3f532b 100644 (file)
@@ -11,23 +11,19 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 import com.google.common.collect.ImmutableSet;
-import com.google.common.util.concurrent.Futures;
 import java.net.URI;
 import java.util.Set;
 import javax.ws.rs.core.UriInfo;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
-import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
-import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
+import org.opendaylight.restconf.nb.rfc8040.TestUtils;
 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
-import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
 import org.opendaylight.yangtools.yang.common.Empty;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
@@ -55,15 +51,7 @@ public class RestconfOperationsServiceTest {
     public void init() throws Exception {
         MockitoAnnotations.initMocks(this);
         this.schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/modules"));
-
-        final TransactionChainHandler txHandler = Mockito.mock(TransactionChainHandler.class);
-        final DOMTransactionChain domTx = Mockito.mock(DOMTransactionChain.class);
-        Mockito.when(txHandler.get()).thenReturn(domTx);
-        final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
-        Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
-        Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
-        this.schemaContextHandler = new SchemaContextHandler(txHandler);
-        this.schemaContextHandler.onGlobalContextUpdated(this.schemaContext);
+        this.schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
 
         this.domMountPointServiceHandler = new DOMMountPointServiceHandler(this.domMountPointService);
 
index d80a86f004d8f8004606138476695f03f8628e0d..5d1d3353800486bc92546339475a8c14e533273f 100644 (file)
@@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
 import org.json.JSONObject;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Mockito;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
@@ -31,6 +32,7 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
 import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.PatchCont;
@@ -72,8 +74,9 @@ public class ListenerAdapterTest extends AbstractConcurrentDataBrokerTest {
                 "/instanceidentifier/yang/instance-identifier-patch-module.yang");
 
         transactionChainHandler = new TransactionChainHandler(domDataBroker);
-        schemaContextHandler = new SchemaContextHandler(transactionChainHandler);
-        SchemaContextHandler.setSchemaContext(sc);
+        schemaContextHandler = SchemaContextHandler.newInstance(transactionChainHandler,
+                Mockito.mock(DOMSchemaService.class));
+        schemaContextHandler.onGlobalContextUpdated(sc);
     }
 
     class ListenerAdapterTester extends ListenerAdapter {