Do not re-acquire global schema context 76/100276/4
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 25 Mar 2022 23:44:17 +0000 (00:44 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 26 Mar 2022 14:04:00 +0000 (15:04 +0100)
The context is bound at NormalizedNodeContext creation time, let's
make sure we do not change that.

JIRA: NETCONF-818
Change-Id: I7ec6296c2703b3689aa0c464874ede37695d9065
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfInvokeOperationsServiceImpl.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfInvokeOperationsServiceImplTest.java

index b340007aede85c2c28fd45dfa776cf9f938b0759..c8affc6873a550f9f2039bfc06afb3c5c0e988f8 100644 (file)
@@ -16,7 +16,6 @@ import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
-import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import javax.ws.rs.Path;
 import javax.ws.rs.WebApplicationException;
@@ -28,7 +27,6 @@ import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.mdsal.dom.api.DOMRpcException;
 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
 import org.opendaylight.mdsal.dom.api.DOMRpcService;
-import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
@@ -74,20 +72,19 @@ public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperat
     @Override
     public void invokeRpc(final String identifier, final NormalizedNodePayload payload, final UriInfo uriInfo,
             final AsyncResponse ar) {
-        final SchemaNode schema = payload.getInstanceIdentifierContext().getSchemaNode();
+        final InstanceIdentifierContext context = payload.getInstanceIdentifierContext();
+        final EffectiveModelContext schemaContext = context.getSchemaContext();
+        final DOMMountPoint mountPoint = context.getMountPoint();
+        final SchemaNode schema = context.getSchemaNode();
         final QName rpcName = schema.getQName();
-        final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
 
         final ListenableFuture<? extends DOMRpcResult> future;
-        final EffectiveModelContext schemaContextRef;
         if (mountPoint == null) {
-            schemaContextRef = schemaContextHandler.get();
-
             // FIXME: this really should be a normal RPC invocation service which has its own interface with JAX-RS
             if (SAL_REMOTE_NAMESPACE.equals(rpcName.getNamespace())) {
                 if (identifier.contains("create-data-change-event-subscription")) {
                     future = Futures.immediateFuture(
-                        CreateStreamUtil.createDataChangeNotifiStream(payload, schemaContextRef));
+                        CreateStreamUtil.createDataChangeNotifiStream(payload, schemaContext));
                 } else {
                     future = Futures.immediateFailedFuture(new RestconfDocumentedException("Unsupported operation",
                         ErrorType.RPC, ErrorTag.OPERATION_NOT_SUPPORTED));
@@ -96,7 +93,6 @@ public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperat
                 future = invokeRpc(payload.getData(), rpcName, rpcService);
             }
         } else {
-            schemaContextRef = modelContext(mountPoint);
             future = invokeRpc(payload.getData(), rpcName, mountPoint);
         }
 
@@ -114,8 +110,7 @@ public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperat
                 if (resultData == null || ((ContainerNode) resultData).isEmpty()) {
                     ar.resume(new WebApplicationException(Status.NO_CONTENT));
                 } else {
-                    ar.resume(NormalizedNodePayload.of(new InstanceIdentifierContext(null, schema,
-                        mountPoint, schemaContextRef), resultData));
+                    ar.resume(NormalizedNodePayload.of(context, resultData));
                 }
             }
 
@@ -178,10 +173,4 @@ public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperat
             throw new RestconfDocumentedException("Invocation failed", e);
         }
     }
-
-    private static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
-        return mountPoint.getService(DOMSchemaService.class)
-            .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))
-            .orElse(null);
-    }
 }
index 229b4f79ffc1bb5eb6c12b866511c50280c813c4..33d1a85d7e46d1c2b5b539073845b3cbc658d006 100644 (file)
@@ -29,6 +29,7 @@ import javax.ws.rs.container.AsyncResponse;
 import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.UriInfo;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
@@ -42,8 +43,8 @@ import org.opendaylight.mdsal.dom.api.DOMRpcException;
 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
 import org.opendaylight.mdsal.dom.api.DOMRpcService;
-import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
@@ -62,8 +63,6 @@ import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 @RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class RestconfInvokeOperationsServiceImplTest {
-    private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/invoke-rpc";
-
     private static final QName RPC = QName.create("ns", "2015-02-28", "test-rpc");
     private static final ContainerNode INPUT = Builders.containerBuilder()
         .withNodeIdentifier(new NodeIdentifier(QName.create(RPC, "input")))
@@ -74,23 +73,28 @@ public class RestconfInvokeOperationsServiceImplTest {
         .withChild(ImmutableNodes.leafNode(QName.create(RPC, "content"), "operation result"))
         .build();
 
+    private static EffectiveModelContext CONTEXT;
+
     @Mock
     private DOMRpcService rpcService;
     @Mock
     private DOMMountPoint mountPoint;
     private RestconfInvokeOperationsServiceImpl invokeOperationsService;
 
+    @BeforeClass
+    public static void beforeClass() throws Exception {
+        CONTEXT = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/invoke-rpc"));
+    }
+
     @Before
-    public void setup() throws Exception {
-        final EffectiveModelContext contextRef =
-                YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
+    public void setup() {
         final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
         final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
         doReturn(wTx).when(dataBroker).newWriteOnlyTransaction();
         doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
         final SchemaContextHandler schemaContextHandler = new SchemaContextHandler(dataBroker,
-            mock(DOMSchemaService.class));
-        schemaContextHandler.onModelContextUpdated(contextRef);
+            FixedDOMSchemaService.of(CONTEXT));
+        schemaContextHandler.init();
         invokeOperationsService =
                 new RestconfInvokeOperationsServiceImpl(rpcService, schemaContextHandler);
     }
@@ -183,6 +187,6 @@ public class RestconfInvokeOperationsServiceImplTest {
         doReturn(immediateFluentFuture(domRpcResult)).when(rpcService).invokeRpc(qname, data);
         doReturn(result).when(domRpcResult).getResult();
         return NormalizedNodePayload.of(
-            InstanceIdentifierContext.ofLocalRpc(mock(EffectiveModelContext.class), schemaNode), data);
+            InstanceIdentifierContext.ofLocalRpc(CONTEXT, schemaNode), data);
     }
 }