From a5842de0021edda1ff05b2cde62a3013f21fa53f Mon Sep 17 00:00:00 2001 From: Xu Rong Date: Tue, 19 Jul 2016 21:16:44 +0800 Subject: [PATCH] Bug 6198 - Use sal-netconf-connector to connet device costs too much time Filter missing sources Identifiers before createSchemaContext in class RecursiveSchemaSetup. Change-Id: Ic41d4803df0807d799a1e80ceb6639877f006e42 Signed-off-by: Xu Rong (cherry picked from commit 57f2e5cc5a34fbecd7a492f3180e9a54d9cd8d17) --- .../topology/AbstractNetconfTopology.java | 7 ++-- .../netconf/NetconfConnectorModule.java | 7 ++-- .../sal/connect/netconf/NetconfDevice.java | 36 ++++++++++++++++++- .../connect/netconf/NetconfDeviceTest.java | 24 ++++++++++--- .../cli/NetconfDeviceConnectionManager.java | 2 +- 5 files changed, 63 insertions(+), 13 deletions(-) diff --git a/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/AbstractNetconfTopology.java b/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/AbstractNetconfTopology.java index 9c3dc6d6fa..f0c56f3ca7 100644 --- a/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/AbstractNetconfTopology.java +++ b/netconf/netconf-topology/src/main/java/org/opendaylight/netconf/topology/AbstractNetconfTopology.java @@ -148,7 +148,7 @@ public abstract class AbstractNetconfTopology implements NetconfTopology, Bindin // directory cache/schema is used. static { schemaResourcesDTOs.put(DEFAULT_CACHE_DIRECTORY, - new NetconfDevice.SchemaResourcesDTO(DEFAULT_SCHEMA_REPOSITORY, + new NetconfDevice.SchemaResourcesDTO(DEFAULT_SCHEMA_REPOSITORY, DEFAULT_SCHEMA_REPOSITORY, DEFAULT_SCHEMA_CONTEXT_FACTORY, new NetconfStateSchemasResolverImpl())); DEFAULT_SCHEMA_REPOSITORY.registerSchemaSourceListener(DEFAULT_CACHE); @@ -166,6 +166,7 @@ public abstract class AbstractNetconfTopology implements NetconfTopology, Bindin protected final SharedSchemaRepository sharedSchemaRepository; protected SchemaSourceRegistry schemaRegistry = DEFAULT_SCHEMA_REPOSITORY; + protected SchemaRepository schemaRepository = DEFAULT_SCHEMA_REPOSITORY; protected SchemaContextFactory schemaContextFactory = DEFAULT_SCHEMA_CONTEXT_FACTORY; protected DOMMountPointService mountPointService = null; @@ -371,7 +372,7 @@ public abstract class AbstractNetconfTopology implements NetconfTopology, Bindin } if (schemaResourcesDTO == null) { - schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(schemaRegistry, schemaContextFactory, + schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(schemaRegistry, schemaRepository, schemaContextFactory, new NetconfStateSchemasResolverImpl()); } @@ -393,7 +394,7 @@ public abstract class AbstractNetconfTopology implements NetconfTopology, Bindin final FilesystemSchemaSourceCache deviceCache = createDeviceFilesystemCache(moduleSchemaCacheDirectory); repository.registerSchemaSourceListener(deviceCache); - return new NetconfDevice.SchemaResourcesDTO(repository, schemaContextFactory, + return new NetconfDevice.SchemaResourcesDTO(repository, repository, schemaContextFactory, new NetconfStateSchemasResolverImpl()); } diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/controller/config/yang/md/sal/connector/netconf/NetconfConnectorModule.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/controller/config/yang/md/sal/connector/netconf/NetconfConnectorModule.java index 46d03feb48..6ca1436e95 100644 --- a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/controller/config/yang/md/sal/connector/netconf/NetconfConnectorModule.java +++ b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/controller/config/yang/md/sal/connector/netconf/NetconfConnectorModule.java @@ -132,7 +132,7 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co // directory cache/schema is used. static { schemaResourcesDTOs.put(DEFAULT_CACHE_DIRECTORY, - new NetconfDevice.SchemaResourcesDTO(DEFAULT_SCHEMA_REPOSITORY, + new NetconfDevice.SchemaResourcesDTO(DEFAULT_SCHEMA_REPOSITORY, DEFAULT_SCHEMA_REPOSITORY, DEFAULT_SCHEMA_CONTEXT_FACTORY, new NetconfStateSchemasResolverImpl())); DEFAULT_SCHEMA_REPOSITORY.registerSchemaSourceListener(DEFAULT_CACHE); @@ -143,6 +143,7 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co private BundleContext bundleContext; private Optional userCapabilities; private SchemaSourceRegistry schemaRegistry = DEFAULT_SCHEMA_REPOSITORY; + private SchemaRepository schemaRepository = DEFAULT_SCHEMA_REPOSITORY; private SchemaContextFactory schemaContextFactory = DEFAULT_SCHEMA_CONTEXT_FACTORY; private Broker domRegistry; @@ -283,7 +284,7 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co } if (schemaResourcesDTO == null) { - schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(schemaRegistry, schemaContextFactory, + schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(schemaRegistry, schemaRepository, schemaContextFactory, new NetconfStateSchemasResolverImpl()); } @@ -329,7 +330,7 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co final FilesystemSchemaSourceCache deviceCache = createDeviceFilesystemCache(moduleSchemaCacheDirectory); repository.registerSchemaSourceListener(deviceCache); - return new NetconfDevice.SchemaResourcesDTO(repository, schemaContextFactory, + return new NetconfDevice.SchemaResourcesDTO(repository, repository, schemaContextFactory, new NetconfStateSchemasResolverImpl()); } diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDevice.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDevice.java index 69efd2858e..3edbaf1018 100644 --- a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDevice.java +++ b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDevice.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; +import java.util.stream.Collectors; import org.opendaylight.controller.md.sal.dom.api.DOMNotification; import org.opendaylight.controller.md.sal.dom.api.DOMRpcException; import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult; @@ -54,7 +55,9 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; 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.SchemaSourceException; import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation; import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; @@ -62,6 +65,7 @@ import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource; import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider; import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration; import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry; +import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -87,6 +91,7 @@ public class NetconfDevice implements RemoteDevice salFacade; private final ListeningExecutorService processingExecutor; protected final SchemaSourceRegistry schemaRegistry; + protected final SchemaRepository schemaRepository; private final NetconfDeviceSchemasResolver stateSchemasResolver; private final NotificationHandler notificationHandler; protected final List> sourceRegistrations = Lists.newArrayList(); @@ -110,6 +115,7 @@ public class NetconfDevice implements RemoteDevice requiredSources = deviceSources.getRequiredSources(); + final Collection missingSources = filterMissingSources(requiredSources); + + capabilities.addUnresolvedCapabilities(getQNameFromSourceIdentifiers(missingSources), + UnavailableCapability.FailureReason.MissingSource); + + requiredSources.removeAll(missingSources); + setUpSchema(requiredSources); + } + + private Collection filterMissingSources(final Collection requiredSources) { + + return requiredSources.parallelStream().filter(sourceIdentifier -> { + boolean remove = false; + try { + schemaRepository.getSchemaSource(sourceIdentifier, ASTSchemaSource.class).checkedGet(); + } catch (SchemaSourceException e) { + remove = true; + } + return remove; + }).collect(Collectors.toList()); } /** diff --git a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDeviceTest.java b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDeviceTest.java index c8be625f0b..c47060cdb8 100644 --- a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDeviceTest.java +++ b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDeviceTest.java @@ -10,6 +10,7 @@ package org.opendaylight.netconf.sal.connect.netconf; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyCollectionOf; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; @@ -62,14 +63,17 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath; 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.SourceIdentifier; +import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation; import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource; import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration; import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline; +import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource; public class NetconfDeviceTest { @@ -119,6 +123,7 @@ public class NetconfDeviceTest { final NetconfDeviceCommunicator listener = getListener(); final SchemaContextFactory schemaFactory = getSchemaFactory(); + final SchemaRepository schemaRepository = getSchemaRepository(); // Make fallback attempt to fail due to empty resolved sources final SchemaResolutionException schemaResolutionException @@ -129,7 +134,7 @@ public class NetconfDeviceTest { .when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class)); final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO - = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaFactory, stateSchemasResolver); + = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver); final NetconfDevice device = new NetconfDeviceBuilder() .setReconnectOnSchemasChange(true) .setSchemaResourcesDTO(schemaResourcesDTO) @@ -154,9 +159,11 @@ public class NetconfDeviceTest { final SchemaContext schema = getSchema(); final SchemaContextFactory schemaFactory = getSchemaFactory(); + final SchemaRepository schemaRepository = getSchemaRepository(); // Make fallback attempt to fail due to empty resolved sources final MissingSchemaSourceException schemaResolutionException = new MissingSchemaSourceException("fail first", TEST_SID); + doReturn(Futures.immediateFailedCheckedFuture(schemaResolutionException)).when(schemaRepository).getSchemaSource(eq(TEST_SID), eq(ASTSchemaSource.class)); doAnswer(new Answer() { @Override public Object answer(final InvocationOnMock invocation) throws Throwable { @@ -180,7 +187,7 @@ public class NetconfDeviceTest { }; final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO - = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaFactory, stateSchemasResolver); + = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver); final NetconfDevice device = new NetconfDeviceBuilder() .setReconnectOnSchemasChange(true) @@ -194,7 +201,7 @@ public class NetconfDeviceTest { device.onRemoteSessionUp(sessionCaps, listener); Mockito.verify(facade, Mockito.timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class)); - Mockito.verify(schemaFactory, times(2)).createSchemaContext(anyCollectionOf(SourceIdentifier.class)); + Mockito.verify(schemaFactory, times(1)).createSchemaContext(anyCollectionOf(SourceIdentifier.class)); } private SchemaSourceRegistry getSchemaRegistry() { @@ -205,13 +212,20 @@ public class NetconfDeviceTest { return mock; } + private SchemaRepository getSchemaRepository() { + final SchemaRepository mock = mock(SchemaRepository.class); + final SchemaSourceRepresentation mockRep = mock(SchemaSourceRepresentation.class); + doReturn(Futures.immediateCheckedFuture(mockRep)).when(mock).getSchemaSource(any(SourceIdentifier.class), eq(ASTSchemaSource.class)); + return mock; + } + @Test public void testNotificationBeforeSchema() throws Exception { final RemoteDeviceHandler facade = getFacade(); final NetconfDeviceCommunicator listener = getListener(); final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO - = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaFactory(), stateSchemasResolver); + = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(), getSchemaFactory(), stateSchemasResolver); final NetconfDevice device = new NetconfDeviceBuilder() .setReconnectOnSchemasChange(true) .setSchemaResourcesDTO(schemaResourcesDTO) @@ -246,7 +260,7 @@ public class NetconfDeviceTest { final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory(); final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO - = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaContextProviderFactory, stateSchemasResolver); + = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, stateSchemasResolver); final NetconfDevice device = new NetconfDeviceBuilder() .setReconnectOnSchemasChange(true) .setSchemaResourcesDTO(schemaResourcesDTO) diff --git a/netconf/tools/netconf-cli/src/main/java/org/opendaylight/netconf/cli/NetconfDeviceConnectionManager.java b/netconf/tools/netconf-cli/src/main/java/org/opendaylight/netconf/cli/NetconfDeviceConnectionManager.java index a3e98b31da..6e95ccf3a4 100644 --- a/netconf/tools/netconf-cli/src/main/java/org/opendaylight/netconf/cli/NetconfDeviceConnectionManager.java +++ b/netconf/tools/netconf-cli/src/main/java/org/opendaylight/netconf/cli/NetconfDeviceConnectionManager.java @@ -84,7 +84,7 @@ public class NetconfDeviceConnectionManager implements Closeable { final FilesystemSchemaSourceCache cache = new FilesystemSchemaSourceCache<>(repository, YangTextSchemaSource.class, new File(CACHE)); repository.registerSchemaSourceListener(cache); repository.registerSchemaSourceListener(TextToASTTransformer.create(repository, repository)); - final SchemaResourcesDTO schemaResourcesDTO = new SchemaResourcesDTO(repository, schemaContextFactory, new NetconfStateSchemasResolverImpl()); + final SchemaResourcesDTO schemaResourcesDTO = new SchemaResourcesDTO(repository, repository, schemaContextFactory, new NetconfStateSchemasResolverImpl()); device = new NetconfDeviceBuilder() .setReconnectOnSchemasChange(true) -- 2.36.6