Search in sources :

Example 6 with EffectiveModelContextFactory

use of org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory in project yangtools by opendaylight.

the class YangTextSchemaContextResolver method getEffectiveModelContext.

/**
 * Try to parse all currently available yang files and build new schema context depending on specified parsing mode.
 *
 * @param statementParserMode mode of statement parser
 * @return new schema context iif there is at least 1 yang file registered and
 *         new schema context was successfully built.
 */
public Optional<? extends EffectiveModelContext> getEffectiveModelContext(final StatementParserMode statementParserMode) {
    final EffectiveModelContextFactory factory = repository.createEffectiveModelContextFactory(config(statementParserMode));
    Optional<EffectiveModelContext> sc;
    Object ver;
    do {
        // Spin get stable context version
        Object cv;
        do {
            cv = contextVersion;
            sc = currentSchemaContext.get();
            if (version == cv) {
                return sc;
            }
        } while (cv != contextVersion);
        // Version has been updated
        Collection<SourceIdentifier> sources;
        do {
            ver = version;
            sources = ImmutableSet.copyOf(requiredSources);
        } while (ver != version);
        while (true) {
            final ListenableFuture<EffectiveModelContext> f = factory.createEffectiveModelContext(sources);
            try {
                sc = Optional.of(f.get());
                break;
            } catch (final InterruptedException e) {
                throw new IllegalStateException("Interrupted while assembling schema context", e);
            } catch (final ExecutionException e) {
                LOG.info("Failed to fully assemble schema context for {}", sources, e);
                final Throwable cause = e.getCause();
                Verify.verify(cause instanceof SchemaResolutionException);
                sources = ((SchemaResolutionException) cause).getResolvedSources();
            }
        }
        LOG.debug("Resolved schema context for {}", sources);
        synchronized (this) {
            if (contextVersion == cv) {
                currentSchemaContext.set(sc);
                contextVersion = ver;
            }
        }
    } while (version == ver);
    return sc;
}
Also used : SchemaResolutionException(org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException) EffectiveModelContextFactory(org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) RevisionSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier) ExecutionException(java.util.concurrent.ExecutionException) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 7 with EffectiveModelContextFactory

use of org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory in project yangtools by opendaylight.

the class OpenconfigVerSharedSchemaRepositoryTest method testSharedSchemaRepository.

@Test
public void testSharedSchemaRepository() throws Exception {
    final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("shared-schema-repo-test");
    final SettableSchemaProvider<IRSchemaSource> bar = getImmediateYangSourceProviderFromResource("/openconfig-version/shared-schema-repository/bar@2016-01-01.yang");
    bar.register(sharedSchemaRepository);
    bar.setResult();
    final SettableSchemaProvider<IRSchemaSource> foo = getImmediateYangSourceProviderFromResource("/openconfig-version/shared-schema-repository/foo.yang");
    foo.register(sharedSchemaRepository);
    foo.setResult();
    final SettableSchemaProvider<IRSchemaSource> semVer = getImmediateYangSourceProviderFromResource("/openconfig-version/shared-schema-repository/openconfig-extensions.yang");
    semVer.register(sharedSchemaRepository);
    semVer.setResult();
    final EffectiveModelContextFactory fact = sharedSchemaRepository.createEffectiveModelContextFactory();
    final ListenableFuture<EffectiveModelContext> inetAndTopologySchemaContextFuture = fact.createEffectiveModelContext(bar.getId(), foo.getId(), semVer.getId());
    assertTrue(inetAndTopologySchemaContextFuture.isDone());
    assertSchemaContext(inetAndTopologySchemaContextFuture.get(), 3);
    final ListenableFuture<EffectiveModelContext> barSchemaContextFuture = fact.createEffectiveModelContext(bar.getId(), semVer.getId());
    assertTrue(barSchemaContextFuture.isDone());
    assertSchemaContext(barSchemaContextFuture.get(), 2);
}
Also used : IRSchemaSource(org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource) EffectiveModelContextFactory(org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Example 8 with EffectiveModelContextFactory

use of org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory in project netconf by opendaylight.

the class NetconfNodeActorTest method testRegisterAndUnregisterMountPoint.

@Test
public void testRegisterAndUnregisterMountPoint() throws Exception {
    ActorRef slaveRef = registerSlaveMountPoint();
    // Unregister
    slaveRef.tell(new UnregisterSlaveMountPoint(), testKit.getRef());
    verify(mockMountPointReg, timeout(5000)).close();
    verify(mockSchemaSourceReg1, timeout(1000)).close();
    verify(mockSchemaSourceReg2, timeout(1000)).close();
    // Test registration with another interleaved registration that completes while the first registration
    // is resolving the schema context.
    reset(mockSchemaSourceReg1, mockRegistry, mockSchemaRepository);
    resetMountPointMocks();
    doReturn(mockSchemaSourceReg1).when(mockRegistry).registerSchemaSource(any(), withSourceId(SOURCE_IDENTIFIER1));
    final SchemaSourceRegistration<?> newMockSchemaSourceReg = mock(SchemaSourceRegistration.class);
    final EffectiveModelContextFactory newMockSchemaContextFactory = mock(EffectiveModelContextFactory.class);
    doReturn(Futures.immediateFuture(mockSchemaContext)).when(newMockSchemaContextFactory).createEffectiveModelContext(anyCollection());
    doAnswer(unused -> {
        SettableFuture<SchemaContext> future = SettableFuture.create();
        new Thread(() -> {
            doReturn(newMockSchemaSourceReg).when(mockRegistry).registerSchemaSource(any(), withSourceId(SOURCE_IDENTIFIER1));
            doReturn(newMockSchemaContextFactory).when(mockSchemaRepository).createEffectiveModelContextFactory();
            slaveRef.tell(new RegisterMountPoint(ImmutableList.of(SOURCE_IDENTIFIER1), masterRef), testKit.getRef());
            future.set(mockSchemaContext);
        }).start();
        return future;
    }).when(mockSchemaContextFactory).createEffectiveModelContext(anyCollection());
    doReturn(mockSchemaContextFactory).when(mockSchemaRepository).createEffectiveModelContextFactory();
    slaveRef.tell(new RegisterMountPoint(ImmutableList.of(SOURCE_IDENTIFIER1), masterRef), testKit.getRef());
    verify(mockMountPointBuilder, timeout(5000)).register();
    verify(mockMountPointBuilder, after(500)).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(mockMountPointBuilder).addService(eq(DOMSchemaService.class), any());
    verify(mockSchemaSourceReg1).close();
    verify(mockRegistry, times(2)).registerSchemaSource(any(), withSourceId(SOURCE_IDENTIFIER1));
    verify(mockSchemaRepository, times(2)).createEffectiveModelContextFactory();
    verifyNoMoreInteractions(mockMountPointBuilder, newMockSchemaSourceReg);
    // Stop the slave actor and verify schema source registrations are closed.
    final Future<Boolean> stopFuture = Patterns.gracefulStop(slaveRef, TIMEOUT.duration());
    Await.result(stopFuture, TIMEOUT.duration());
    verify(mockMountPointReg).close();
    verify(newMockSchemaSourceReg).close();
}
Also used : DOMRpcService(org.opendaylight.mdsal.dom.api.DOMRpcService) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) UnregisterSlaveMountPoint(org.opendaylight.netconf.topology.singleton.messages.UnregisterSlaveMountPoint) EffectiveModelContextFactory(org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory) DOMNotificationService(org.opendaylight.mdsal.dom.api.DOMNotificationService) DOMActionService(org.opendaylight.mdsal.dom.api.DOMActionService) RegisterMountPoint(org.opendaylight.netconf.topology.singleton.messages.RegisterMountPoint) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) DOMDataBroker(org.opendaylight.mdsal.dom.api.DOMDataBroker) DOMSchemaService(org.opendaylight.mdsal.dom.api.DOMSchemaService) Test(org.junit.Test)

Example 9 with EffectiveModelContextFactory

use of org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory in project netconf by opendaylight.

the class NetconfDeviceTest method testNetconfDeviceAvailableCapabilitiesBuilding.

@Test
public void testNetconfDeviceAvailableCapabilitiesBuilding() throws Exception {
    final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
    final NetconfDeviceCommunicator listener = getListener();
    final EffectiveModelContextFactory schemaContextProviderFactory = getSchemaFactory();
    final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
    final NetconfDevice device = new NetconfDeviceBuilder().setReconnectOnSchemasChange(true).setSchemaResourcesDTO(schemaResourcesDTO).setGlobalProcessingExecutor(getExecutor()).setId(getId()).setSalFacade(facade).setBaseSchemas(BASE_SCHEMAS).build();
    final NetconfDevice netconfSpy = spy(device);
    final NetconfSessionPreferences sessionCaps = getSessionCaps(true, Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
    final Map<QName, AvailableCapability.CapabilityOrigin> moduleBasedCaps = new HashMap<>();
    moduleBasedCaps.putAll(sessionCaps.getModuleBasedCapsOrigin());
    moduleBasedCaps.put(QName.create("(test:qname:side:loading)test"), AvailableCapability.CapabilityOrigin.UserDefined);
    netconfSpy.onRemoteSessionUp(sessionCaps.replaceModuleCaps(moduleBasedCaps), listener);
    final ArgumentCaptor<NetconfSessionPreferences> argument = ArgumentCaptor.forClass(NetconfSessionPreferences.class);
    verify(facade, timeout(5000)).onDeviceConnected(any(MountPointContext.class), argument.capture(), any(DOMRpcService.class), isNull());
    final NetconfDeviceCapabilities netconfDeviceCaps = argument.getValue().getNetconfDeviceCapabilities();
    netconfDeviceCaps.getResolvedCapabilities().forEach(entry -> assertEquals("Builded 'AvailableCapability' schemas should match input capabilities.", moduleBasedCaps.get(QName.create(entry.getCapability())).getName(), entry.getCapabilityOrigin().getName()));
}
Also used : NetconfDeviceCommunicator(org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator) DOMRpcService(org.opendaylight.mdsal.dom.api.DOMRpcService) HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName) EffectiveModelContextFactory(org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory) NetconfSessionPreferences(org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences) MountPointContext(org.opendaylight.yangtools.rfc8528.data.api.MountPointContext) NetconfDeviceCapabilities(org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities) Test(org.junit.Test)

Example 10 with EffectiveModelContextFactory

use of org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory in project netconf by opendaylight.

the class NetconfDeviceTest method testNetconfDeviceNotificationsCapabilityIsNotPresent.

@Test
public void testNetconfDeviceNotificationsCapabilityIsNotPresent() throws Exception {
    final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
    final NetconfDeviceCommunicator listener = getListener();
    final EffectiveModelContextFactory schemaContextProviderFactory = getSchemaFactory();
    final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
    final NetconfDevice device = new NetconfDeviceBuilder().setSchemaResourcesDTO(schemaResourcesDTO).setGlobalProcessingExecutor(getExecutor()).setId(getId()).setSalFacade(facade).setBaseSchemas(BASE_SCHEMAS).build();
    final NetconfDevice netconfSpy = spy(device);
    final NetconfSessionPreferences sessionCaps = getSessionCaps(false, Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
    netconfSpy.onRemoteSessionUp(sessionCaps, listener);
    final ArgumentCaptor<NetconfSessionPreferences> argument = ArgumentCaptor.forClass(NetconfSessionPreferences.class);
    verify(facade, timeout(5000)).onDeviceConnected(any(MountPointContext.class), argument.capture(), any(DOMRpcService.class), isNull());
    final NetconfDeviceCapabilities netconfDeviceCaps = argument.getValue().getNetconfDeviceCapabilities();
    List<String> notificationModulesName = Arrays.asList(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.$YangModuleInfoImpl.getInstance().getName().toString(), org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.$YangModuleInfoImpl.getInstance().getName().toString());
    assertFalse(netconfDeviceCaps.getResolvedCapabilities().stream().anyMatch(entry -> notificationModulesName.contains(entry.getCapability())));
}
Also used : Arrays(java.util.Arrays) SchemaSourceRegistration(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) EffectiveModelContextFactory(org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory) SettableFuture(com.google.common.util.concurrent.SettableFuture) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) HashMultimap(com.google.common.collect.HashMultimap) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) Mockito.after(org.mockito.Mockito.after) Mockito.doReturn(org.mockito.Mockito.doReturn) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) RemoteDeviceHandler(org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler) DOMActionService(org.opendaylight.mdsal.dom.api.DOMActionService) XmlUtil(org.opendaylight.netconf.api.xml.XmlUtil) Module(org.opendaylight.yangtools.yang.model.api.Module) Collection(java.util.Collection) RemoteDeviceId(org.opendaylight.netconf.sal.connect.util.RemoteDeviceId) DOMNotification(org.opendaylight.mdsal.dom.api.DOMNotification) Set(java.util.Set) Mockito.doNothing(org.mockito.Mockito.doNothing) InetSocketAddress(java.net.InetSocketAddress) Sets(com.google.common.collect.Sets) Executors(java.util.concurrent.Executors) NetconfDeviceCommunicator(org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator) SchemaSourceRepresentation(org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation) List(java.util.List) Revision(org.opendaylight.yangtools.yang.common.Revision) MissingSchemaSourceException(org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException) Assert.assertFalse(org.junit.Assert.assertFalse) PotentialSchemaSource(org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource) SAXException(org.xml.sax.SAXException) NetconfSessionPreferences(org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences) Mockito.mock(org.mockito.Mockito.mock) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) Iterables(com.google.common.collect.Iterables) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) Mockito.timeout(org.mockito.Mockito.timeout) SchemaSourceRegistry(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry) Lists(com.google.common.collect.Lists) ArgumentCaptor(org.mockito.ArgumentCaptor) DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) XmlNetconfConstants(org.opendaylight.netconf.api.xml.XmlNetconfConstants) NetconfDeviceRpc(org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc) DOMRpcResult(org.opendaylight.mdsal.dom.api.DOMRpcResult) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) SchemaRepository(org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository) DOMRpcService(org.opendaylight.mdsal.dom.api.DOMRpcService) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) NetconfMessageTransformUtil(org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil) MountPointContext(org.opendaylight.yangtools.rfc8528.data.api.MountPointContext) QName(org.opendaylight.yangtools.yang.common.QName) Mockito.verify(org.mockito.Mockito.verify) ArgumentMatchers.anyCollection(org.mockito.ArgumentMatchers.anyCollection) Futures(com.google.common.util.concurrent.Futures) NetconfDeviceSchemasResolver(org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemasResolver) RevisionSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier) SchemaResolutionException(org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException) AvailableCapability(org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability) Collections(java.util.Collections) NetconfDeviceCapabilities(org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities) Assert.assertEquals(org.junit.Assert.assertEquals) MessageTransformer(org.opendaylight.netconf.sal.connect.api.MessageTransformer) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) NetconfDeviceCommunicator(org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator) DOMRpcService(org.opendaylight.mdsal.dom.api.DOMRpcService) EffectiveModelContextFactory(org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory) NetconfSessionPreferences(org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences) MountPointContext(org.opendaylight.yangtools.rfc8528.data.api.MountPointContext) NetconfDeviceCapabilities(org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities) Test(org.junit.Test)

Aggregations

EffectiveModelContextFactory (org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory)21 Test (org.junit.Test)19 NetconfDeviceCommunicator (org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator)10 NetconfSessionPreferences (org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences)10 SchemaContext (org.opendaylight.yangtools.yang.model.api.SchemaContext)10 Collection (java.util.Collection)9 ArgumentMatchers.anyCollection (org.mockito.ArgumentMatchers.anyCollection)9 DOMRpcService (org.opendaylight.mdsal.dom.api.DOMRpcService)8 SchemaResolutionException (org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException)8 DOMActionService (org.opendaylight.mdsal.dom.api.DOMActionService)7 MountPointContext (org.opendaylight.yangtools.rfc8528.data.api.MountPointContext)7 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)7 HashMap (java.util.HashMap)6 IRSchemaSource (org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource)6 HashMultimap (com.google.common.collect.HashMultimap)5 Iterables (com.google.common.collect.Iterables)5 Lists (com.google.common.collect.Lists)5 Sets (com.google.common.collect.Sets)5 Futures (com.google.common.util.concurrent.Futures)5 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)5