X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fnetconf-topology-singleton%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Ftopology%2Fsingleton%2Fimpl%2FNetconfNodeActorTest.java;h=6e6d3d6f5ec11288fa33e97c2200089eceb5cdee;hb=52caea7f0226b11307aee7983b721124d2e8d25f;hp=4f79e46c1a9468a8ae5d2f9c7eda8cc09a8640f7;hpb=efca5e72d8c1e7b805ed28d1313375dc7d8594f1;p=netconf.git diff --git a/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/NetconfNodeActorTest.java b/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/NetconfNodeActorTest.java index 4f79e46c1a..6e6d3d6f5e 100644 --- a/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/NetconfNodeActorTest.java +++ b/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/NetconfNodeActorTest.java @@ -12,6 +12,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyCollection; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.after; @@ -25,6 +26,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.MockitoAnnotations.initMocks; +import static org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY; import akka.actor.ActorRef; import akka.actor.ActorSystem; @@ -58,7 +60,12 @@ import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.opendaylight.controller.cluster.schema.provider.impl.YangTextSchemaSourceSerializationProxy; +import org.opendaylight.mdsal.common.api.LogicalDatastoreType; +import org.opendaylight.mdsal.dom.api.DOMActionException; +import org.opendaylight.mdsal.dom.api.DOMActionResult; +import org.opendaylight.mdsal.dom.api.DOMActionService; import org.opendaylight.mdsal.dom.api.DOMDataBroker; +import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier; import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction; import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction; import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction; @@ -69,8 +76,11 @@ 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.spi.DefaultDOMRpcResult; +import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult; +import org.opendaylight.netconf.sal.connect.netconf.NetconfDevice.SchemaResourcesDTO; import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId; import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor; +import org.opendaylight.netconf.topology.singleton.impl.utils.ClusteringActionException; import org.opendaylight.netconf.topology.singleton.impl.utils.ClusteringRpcException; import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup; import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup.NetconfTopologySetupBuilder; @@ -87,17 +97,18 @@ import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory; import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException; import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier; -import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory; import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository; import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException; -import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter; import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource; @@ -128,6 +139,9 @@ public class NetconfNodeActorTest { @Mock private DOMRpcService mockDOMRpcService; + @Mock + private DOMActionService mockDOMActionService; + @Mock private DOMMountPointService mockMountPointService; @@ -150,13 +164,16 @@ public class NetconfNodeActorTest { private SchemaSourceRegistry mockRegistry; @Mock - private SchemaContextFactory mockSchemaContextFactory; + private EffectiveModelContextFactory mockSchemaContextFactory; @Mock private SchemaRepository mockSchemaRepository; @Mock - private SchemaContext mockSchemaContext; + private EffectiveModelContext mockSchemaContext; + + @Mock + private SchemaResourcesDTO schemaResourceDTO; @Before public void setup() { @@ -168,11 +185,12 @@ public class NetconfNodeActorTest { masterSchemaRepository.registerSchemaSourceListener( TextToASTTransformer.create(masterSchemaRepository, masterSchemaRepository)); + doReturn(masterSchemaRepository).when(schemaResourceDTO).getSchemaRepository(); + doReturn(mockRegistry).when(schemaResourceDTO).getSchemaRegistry(); final NetconfTopologySetup setup = NetconfTopologySetupBuilder.create().setActorSystem(system) - .setIdleTimeout(Duration.apply(1, TimeUnit.SECONDS)).build(); + .setIdleTimeout(Duration.apply(1, TimeUnit.SECONDS)).setSchemaResourceDTO(schemaResourceDTO).build(); - final Props props = NetconfNodeActor.props(setup, remoteDeviceId, masterSchemaRepository, - masterSchemaRepository, TIMEOUT, mockMountPointService); + final Props props = NetconfNodeActor.props(setup, remoteDeviceId, TIMEOUT, mockMountPointService); masterRef = TestActorRef.create(system, props, "master_messages"); @@ -184,7 +202,7 @@ public class NetconfNodeActorTest { doReturn(mockSchemaSourceReg2).when(mockRegistry).registerSchemaSource(any(), withSourceId(SOURCE_IDENTIFIER2)); doReturn(mockSchemaContextFactory).when(mockSchemaRepository) - .createSchemaContextFactory(any(SchemaSourceFilter.class)); + .createEffectiveModelContextFactory(); } @After @@ -205,7 +223,8 @@ public class NetconfNodeActorTest { final RemoteDeviceId newRemoteDeviceId = new RemoteDeviceId("netconf-topology2", new InetSocketAddress(InetAddresses.forString("127.0.0.2"), 9999)); - final NetconfTopologySetup newSetup = NetconfTopologySetupBuilder.create().setActorSystem(system).build(); + final NetconfTopologySetup newSetup = NetconfTopologySetupBuilder.create() + .setSchemaResourceDTO(schemaResourceDTO).setActorSystem(system).build(); masterRef.tell(new RefreshSetupMasterActorData(newSetup, newRemoteDeviceId), testKit.getRef()); @@ -258,13 +277,13 @@ public class NetconfNodeActorTest { doReturn(mockSchemaSourceReg1).when(mockRegistry).registerSchemaSource(any(), withSourceId(SOURCE_IDENTIFIER1)); doReturn(mockSchemaContextFactory).when(mockSchemaRepository) - .createSchemaContextFactory(any(SchemaSourceFilter.class)); + .createEffectiveModelContextFactory(); final SchemaSourceRegistration newMockSchemaSourceReg = mock(SchemaSourceRegistration.class); - final SchemaContextFactory newMockSchemaContextFactory = mock(SchemaContextFactory.class); + final EffectiveModelContextFactory newMockSchemaContextFactory = mock(EffectiveModelContextFactory.class); doReturn(Futures.immediateFuture(mockSchemaContext)) - .when(newMockSchemaContextFactory).createSchemaContext(any()); + .when(newMockSchemaContextFactory).createEffectiveModelContext(anyCollection()); doAnswer(unused -> { SettableFuture future = SettableFuture.create(); @@ -273,7 +292,7 @@ public class NetconfNodeActorTest { withSourceId(SOURCE_IDENTIFIER1)); doReturn(newMockSchemaContextFactory).when(mockSchemaRepository) - .createSchemaContextFactory(any(SchemaSourceFilter.class)); + .createEffectiveModelContextFactory(); slaveRef.tell(new RegisterMountPoint(ImmutableList.of(SOURCE_IDENTIFIER1), masterRef), testKit.getRef()); @@ -281,10 +300,10 @@ public class NetconfNodeActorTest { future.set(mockSchemaContext); }).start(); return future; - }).when(mockSchemaContextFactory).createSchemaContext(any()); + }).when(mockSchemaContextFactory).createEffectiveModelContext(anyCollection()); doReturn(mockSchemaContextFactory).when(mockSchemaRepository) - .createSchemaContextFactory(any(SchemaSourceFilter.class)); + .createEffectiveModelContextFactory(); slaveRef.tell(new RegisterMountPoint(ImmutableList.of(SOURCE_IDENTIFIER1), masterRef), testKit.getRef()); @@ -292,10 +311,11 @@ public class NetconfNodeActorTest { verify(mockMountPointBuilder, after(500)).addInitialSchemaContext(mockSchemaContext); verify(mockMountPointBuilder).addService(eq(DOMDataBroker.class), any()); verify(mockMountPointBuilder).addService(eq(DOMRpcService.class), any()); + verify(mockMountPointBuilder).addService(eq(DOMActionService.class), any()); verify(mockMountPointBuilder).addService(eq(DOMNotificationService.class), any()); verify(mockSchemaSourceReg1).close(); verify(mockRegistry, times(2)).registerSchemaSource(any(), withSourceId(SOURCE_IDENTIFIER1)); - verify(mockSchemaRepository, times(2)).createSchemaContextFactory(any(SchemaSourceFilter.class)); + verify(mockSchemaRepository, times(2)).createEffectiveModelContextFactory(); verifyNoMoreInteractions(mockMountPointBuilder, newMockSchemaSourceReg); // Stop the slave actor and verify schema source registrations are closed. @@ -310,15 +330,19 @@ public class NetconfNodeActorTest { @SuppressWarnings("unchecked") @Test public void testRegisterMountPointWithSchemaFailures() throws Exception { - final NetconfTopologySetup setup = NetconfTopologySetupBuilder.create().setActorSystem(system).build(); + SchemaResourcesDTO schemaResourceDTO2 = mock(SchemaResourcesDTO.class); + doReturn(mockRegistry).when(schemaResourceDTO2).getSchemaRegistry(); + doReturn(mockSchemaRepository).when(schemaResourceDTO2).getSchemaRepository(); + final NetconfTopologySetup setup = NetconfTopologySetupBuilder.create().setSchemaResourceDTO(schemaResourceDTO2) + .setActorSystem(system).build(); - final ActorRef slaveRef = system.actorOf(NetconfNodeActor.props(setup, remoteDeviceId, mockRegistry, - mockSchemaRepository, TIMEOUT, mockMountPointService)); + final ActorRef slaveRef = system.actorOf(NetconfNodeActor.props(setup, remoteDeviceId, TIMEOUT, + mockMountPointService)); // Test unrecoverable failure. doReturn(Futures.immediateFailedFuture(new SchemaResolutionException("mock"))) - .when(mockSchemaContextFactory).createSchemaContext(any()); + .when(mockSchemaContextFactory).createEffectiveModelContext(anyCollection()); slaveRef.tell(new RegisterMountPoint(ImmutableList.of(SOURCE_IDENTIFIER1, SOURCE_IDENTIFIER2), masterRef), testKit.getRef()); @@ -339,7 +363,7 @@ public class NetconfNodeActorTest { doReturn(Futures.immediateFailedFuture(new SchemaResolutionException("mock", new AskTimeoutException("timeout")))) .doReturn(Futures.immediateFuture(mockSchemaContext)) - .when(mockSchemaContextFactory).createSchemaContext(any()); + .when(mockSchemaContextFactory).createEffectiveModelContext(anyCollection()); slaveRef.tell(new RegisterMountPoint(ImmutableList.of(SOURCE_IDENTIFIER1, SOURCE_IDENTIFIER2), masterRef), testKit.getRef()); @@ -355,15 +379,15 @@ public class NetconfNodeActorTest { reset(mockSchemaSourceReg1, mockSchemaSourceReg2, mockSchemaRepository, mockSchemaContextFactory); resetMountPointMocks(); - final SchemaContextFactory mockSchemaContextFactorySuccess = mock(SchemaContextFactory.class); + final EffectiveModelContextFactory mockSchemaContextFactorySuccess = mock(EffectiveModelContextFactory.class); doReturn(Futures.immediateFuture(mockSchemaContext)) - .when(mockSchemaContextFactorySuccess).createSchemaContext(any()); + .when(mockSchemaContextFactorySuccess).createEffectiveModelContext(anyCollection()); doAnswer(unused -> { SettableFuture future = SettableFuture.create(); new Thread(() -> { doReturn(mockSchemaContextFactorySuccess).when(mockSchemaRepository) - .createSchemaContextFactory(any(SchemaSourceFilter.class)); + .createEffectiveModelContextFactory(); slaveRef.tell(new RegisterMountPoint(ImmutableList.of(SOURCE_IDENTIFIER1, SOURCE_IDENTIFIER2), masterRef), testKit.getRef()); @@ -371,16 +395,35 @@ public class NetconfNodeActorTest { future.setException(new SchemaResolutionException("mock", new AskTimeoutException("timeout"))); }).start(); return future; - }).when(mockSchemaContextFactory).createSchemaContext(any()); + }).when(mockSchemaContextFactory).createEffectiveModelContext(anyCollection()); - doReturn(mockSchemaContextFactory).when(mockSchemaRepository) - .createSchemaContextFactory(any(SchemaSourceFilter.class)); + doReturn(mockSchemaContextFactory).when(mockSchemaRepository).createEffectiveModelContextFactory(); slaveRef.tell(new RegisterMountPoint(ImmutableList.of(SOURCE_IDENTIFIER1, SOURCE_IDENTIFIER2), masterRef), testKit.getRef()); verify(mockMountPointBuilder, timeout(5000)).register(); - verify(mockSchemaRepository, times(2)).createSchemaContextFactory(any(SchemaSourceFilter.class)); + verify(mockSchemaRepository, times(2)).createEffectiveModelContextFactory(); + } + + @Test(expected = MissingSchemaSourceException.class) + public void testMissingSchemaSourceOnMissingProvider() throws Exception { + SchemaResourcesDTO schemaResourceDTO2 = mock(SchemaResourcesDTO.class); + doReturn(DEFAULT_SCHEMA_REPOSITORY).when(schemaResourceDTO2).getSchemaRegistry(); + doReturn(DEFAULT_SCHEMA_REPOSITORY).when(schemaResourceDTO2).getSchemaRepository(); + final NetconfTopologySetup setup = NetconfTopologySetupBuilder.create().setActorSystem(system) + .setSchemaResourceDTO(schemaResourceDTO2).setIdleTimeout(Duration.apply(1, TimeUnit.SECONDS)).build(); + final Props props = NetconfNodeActor.props(setup, remoteDeviceId, TIMEOUT, mockMountPointService); + ActorRef actor = TestActorRef.create(system, props, "master_messages_2"); + + final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("testID"); + + final ProxyYangTextSourceProvider proxyYangProvider = + new ProxyYangTextSourceProvider(actor, system.dispatcher(), TIMEOUT); + + final Future resolvedSchemaFuture = + proxyYangProvider.getYangTextSchemaSource(sourceIdentifier); + Await.result(resolvedSchemaFuture, TIMEOUT.duration()); } @Test @@ -495,6 +538,60 @@ public class NetconfNodeActorTest { } } + @Test + @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"}) + public void testSlaveInvokeAction() throws Throwable { + final List sourceIdentifiers = Lists + .newArrayList(RevisionSourceIdentifier.create("testActionID")); + initializeMaster(sourceIdentifiers); + registerSlaveMountPoint(); + + ArgumentCaptor domActionServiceCaptor = ArgumentCaptor.forClass(DOMActionService.class); + verify(mockMountPointBuilder).addService(eq(DOMActionService.class), domActionServiceCaptor.capture()); + + final DOMActionService slaveDomActionService = domActionServiceCaptor.getValue(); + assertTrue(slaveDomActionService instanceof ProxyDOMActionService); + + final QName testQName = QName.create("test", "2019-08-16", "TestActionQname"); + final SchemaPath schemaPath = SchemaPath.create(true, testQName); + + final YangInstanceIdentifier yangIIdPath = YangInstanceIdentifier + .create(new YangInstanceIdentifier.NodeIdentifier(testQName)); + + final DOMDataTreeIdentifier domDataTreeIdentifier = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, + yangIIdPath); + + final ContainerNode outputNode = ImmutableContainerNodeBuilder.create() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(testQName)) + .withChild(ImmutableNodes.leafNode(testQName, "foo")).build(); + + // Action with no response output. + doReturn(FluentFutures.immediateNullFluentFuture()).when(mockDOMActionService) + .invokeAction(any(), any(), any()); + DOMActionResult result = slaveDomActionService.invokeAction(schemaPath, domDataTreeIdentifier, outputNode) + .get(2, TimeUnit.SECONDS); + assertEquals(null, result); + + // Action with response output. + doReturn(FluentFutures.immediateFluentFuture(new SimpleDOMActionResult(outputNode))).when(mockDOMActionService) + .invokeAction(any(), any(), any()); + result = slaveDomActionService.invokeAction(schemaPath, domDataTreeIdentifier, outputNode) + .get(2, TimeUnit.SECONDS); + + assertEquals(outputNode, result.getOutput().get()); + assertTrue(result.getErrors().isEmpty()); + + // Action failure. + exception.expect(DOMActionException.class); + doReturn(FluentFutures.immediateFailedFluentFuture(new ClusteringActionException("mock"))) + .when(mockDOMActionService).invokeAction(any(), any(), any()); + try { + slaveDomActionService.invokeAction(schemaPath, domDataTreeIdentifier, outputNode).get(2, TimeUnit.SECONDS); + } catch (ExecutionException e) { + throw e.getCause(); + } + } + @Test public void testSlaveNewTransactionRequests() { @@ -522,12 +619,15 @@ public class NetconfNodeActorTest { } private ActorRef registerSlaveMountPoint() { + SchemaResourcesDTO schemaResourceDTO2 = mock(SchemaResourcesDTO.class); + doReturn(mockRegistry).when(schemaResourceDTO2).getSchemaRegistry(); + doReturn(mockSchemaRepository).when(schemaResourceDTO2).getSchemaRepository(); final ActorRef slaveRef = system.actorOf(NetconfNodeActor.props( - NetconfTopologySetupBuilder.create().setActorSystem(system).build(), remoteDeviceId, mockRegistry, - mockSchemaRepository, TIMEOUT, mockMountPointService)); + NetconfTopologySetupBuilder.create().setSchemaResourceDTO(schemaResourceDTO2).setActorSystem(system) + .build(), remoteDeviceId, TIMEOUT, mockMountPointService)); doReturn(Futures.immediateFuture(mockSchemaContext)) - .when(mockSchemaContextFactory).createSchemaContext(any()); + .when(mockSchemaContextFactory).createEffectiveModelContext(anyCollection()); slaveRef.tell(new RegisterMountPoint(ImmutableList.of(SOURCE_IDENTIFIER1, SOURCE_IDENTIFIER2), masterRef), testKit.getRef()); @@ -545,7 +645,7 @@ public class NetconfNodeActorTest { private void initializeMaster(final List sourceIdentifiers) { masterRef.tell(new CreateInitialMasterActorData(mockDOMDataBroker, sourceIdentifiers, - mockDOMRpcService), testKit.getRef()); + mockDOMRpcService, mockDOMActionService), testKit.getRef()); testKit.expectMsgClass(MasterActorDataInitialized.class); }