import java.util.stream.Collectors;
import org.junit.Test;
import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractSchemaAwareTest;
-import org.opendaylight.mdsal.binding.dom.adapter.test.util.MockAdapterContext;
+import org.opendaylight.mdsal.binding.dom.codec.impl.BindingCodecContext;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
private static final YangInstanceIdentifier BI_TOP_LEVEL_LIST = YangInstanceIdentifier.builder().node(Top.QNAME)
.node(TopLevelList.QNAME).nodeWithKey(TopLevelList.QNAME, NAME_QNAME, TOP_FOO_KEY.getName()).build();
- private MockAdapterContext codec;
- private EffectiveModelContext context;
+ private CurrentAdapterSerializer serializer;
@Override
- protected void setupWithSchema(final EffectiveModelContext schemaContext) {
- this.context = schemaContext;
- this.codec = new MockAdapterContext();
+ protected void setupWithRuntimeContext(final BindingRuntimeContext runtimeContext) {
+ serializer = new CurrentAdapterSerializer(new BindingCodecContext(runtimeContext));
}
@Test
public void testComplexAugmentationSerialization() {
- codec.onModelContextUpdated(context);
- final PathArgument lastArg = codec.currentSerializer().toYangInstanceIdentifier(BA_TREE_COMPLEX_USES)
- .getLastPathArgument();
+ final PathArgument lastArg = serializer.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES).getLastPathArgument();
assertTrue(lastArg instanceof AugmentationIdentifier);
}
@Test
public void testLeafOnlyAugmentationSerialization() {
- codec.onModelContextUpdated(context);
- final PathArgument leafOnlyLastArg = codec.currentSerializer().toYangInstanceIdentifier(BA_TREE_LEAF_ONLY)
+ final PathArgument leafOnlyLastArg = serializer.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY)
.getLastPathArgument();
assertTrue(leafOnlyLastArg instanceof AugmentationIdentifier);
assertTrue(((AugmentationIdentifier) leafOnlyLastArg).getPossibleChildNames().contains(SIMPLE_VALUE_QNAME));
@Test
public void testGetRpcMethodToQName() {
- codec.onModelContextUpdated(context);
- final List<String> retMap = codec.currentSerializer()
+ final List<String> retMap = serializer
.getRpcMethodToQName(OpendaylightTestRpcServiceService.class).keySet().stream()
.map(Method::getName)
.collect(Collectors.toList());
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
+import com.google.common.util.concurrent.ListenableFuture;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map.Entry;
import org.junit.Test;
-import org.opendaylight.mdsal.binding.dom.adapter.test.util.TestingModuleInfoSnapshot;
import org.opendaylight.mdsal.binding.dom.codec.impl.BindingCodecContext;
import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
import org.opendaylight.mdsal.binding.runtime.api.DefaultBindingRuntimeContext;
+import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
public class CurrentAdapterSerializerTest {
"urn:test", "2017-01-01", "cont")));
return codec.fromNormalizedNode(path, data);
}
+
+ private static final class TestingModuleInfoSnapshot implements ModuleInfoSnapshot {
+ static final TestingModuleInfoSnapshot INSTANCE = new TestingModuleInfoSnapshot();
+
+ private TestingModuleInfoSnapshot() {
+ // Hidden on purpose
+ }
+
+ @Override
+ public EffectiveModelContext getEffectiveModelContext() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public ListenableFuture<? extends YangTextSchemaSource> getSource(SourceIdentifier sourceIdentifier) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> Class<T> loadClass(String fullyQualifiedName) throws ClassNotFoundException {
+ return (Class<T>) Class.forName(fullyQualifiedName);
+ }
+ }
}
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
import org.opendaylight.mdsal.dom.api.DOMDataBroker;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
public abstract class AbstractBaseDataBrokerTest extends AbstractSchemaAwareTest {
private static final int ASSERT_COMMIT_DEFAULT_TIMEOUT = 5000;
}
@Override
- protected void setupWithSchema(final EffectiveModelContext context) {
+ protected void setupWithRuntimeContext(final BindingRuntimeContext runtimeContext) {
testCustomizer = createDataBrokerTestCustomizer();
dataBroker = testCustomizer.createDataBroker();
domBroker = testCustomizer.getDOMDataBroker();
- testCustomizer.updateSchema(context);
+ testCustomizer.updateSchema(runtimeContext);
+ super.setupWithRuntimeContext(runtimeContext);
}
public DataBroker getDataBroker() {
import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMDataBrokerAdapter;
import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMNotificationPublishServiceAdapter;
import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMNotificationServiceAdapter;
-import org.opendaylight.mdsal.binding.dom.adapter.test.util.MockAdapterContext;
import org.opendaylight.mdsal.binding.dom.adapter.test.util.MockSchemaService;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.mdsal.dom.api.DOMDataBroker;
import org.opendaylight.mdsal.dom.api.DOMSchemaService;
import org.opendaylight.mdsal.dom.broker.SerializedDOMDataBroker;
import org.opendaylight.mdsal.dom.spi.store.DOMStore;
import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
public abstract class AbstractDataBrokerTestCustomizer {
+ private final DOMNotificationRouter domNotificationRouter = DOMNotificationRouter.create(16);
+ private final MockSchemaService schemaService = new MockSchemaService();
private DOMDataBroker domDataBroker;
- private final DOMNotificationRouter domNotificationRouter;
- private final MockSchemaService schemaService;
private ImmutableMap<LogicalDatastoreType, DOMStore> datastores;
- private final MockAdapterContext adapterContext;
public ImmutableMap<LogicalDatastoreType, DOMStore> createDatastores() {
return ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
.build();
}
- public AbstractDataBrokerTestCustomizer() {
- schemaService = new MockSchemaService();
- adapterContext = new MockAdapterContext();
- schemaService.registerSchemaContextListener(adapterContext);
- domNotificationRouter = DOMNotificationRouter.create(16);
- }
-
public DOMStore createConfigurationDatastore() {
final InMemoryDOMDataStore store = new InMemoryDOMDataStore("CFG", getDataTreeChangeListenerExecutor());
schemaService.registerSchemaContextListener(store);
}
public NotificationService createNotificationService() {
- return new BindingDOMNotificationServiceAdapter(adapterContext, domNotificationRouter);
+ return new BindingDOMNotificationServiceAdapter(schemaService, domNotificationRouter);
}
public NotificationPublishService createNotificationPublishService() {
- return new BindingDOMNotificationPublishServiceAdapter(adapterContext, domNotificationRouter);
+ return new BindingDOMNotificationPublishServiceAdapter(schemaService, domNotificationRouter);
}
public abstract ListeningExecutorService getCommitCoordinatorExecutor();
}
public DataBroker createDataBroker() {
- return new BindingDOMDataBrokerAdapter(adapterContext, getDOMDataBroker());
+ return new BindingDOMDataBrokerAdapter(schemaService, getDOMDataBroker());
}
public AdapterContext getAdapterContext() {
- return adapterContext;
+ return schemaService;
}
public DOMSchemaService getSchemaService() {
return datastores;
}
- public void updateSchema(final EffectiveModelContext ctx) {
+ public void updateSchema(final BindingRuntimeContext ctx) {
schemaService.changeSchema(ctx);
}
import org.opendaylight.mdsal.binding.api.NotificationPublishService;
import org.opendaylight.mdsal.binding.api.NotificationService;
import org.opendaylight.mdsal.binding.dom.adapter.AdapterContext;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
import org.opendaylight.mdsal.dom.broker.DOMNotificationRouter;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
public class AbstractNotificationBrokerTest extends AbstractSchemaAwareTest {
private AdapterContext bindingToNormalizedNodeCodec;
@Override
- protected void setupWithSchema(final EffectiveModelContext context) {
+ protected void setupWithRuntimeContext(final BindingRuntimeContext runtimeContext) {
final DataBrokerTestCustomizer testCustomizer = createDataBrokerTestCustomizer();
domNotificationRouter = testCustomizer.getDomNotificationRouter();
notificationService = testCustomizer.createNotificationService();
notificationPublishService = testCustomizer.createNotificationPublishService();
bindingToNormalizedNodeCodec = testCustomizer.getAdapterContext();
- testCustomizer.updateSchema(context);
+ testCustomizer.updateSchema(runtimeContext);
+ super.setupWithRuntimeContext(runtimeContext);
}
protected DataBrokerTestCustomizer createDataBrokerTestCustomizer() {
import com.google.common.cache.LoadingCache;
import java.util.Set;
import org.junit.Before;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
public abstract class AbstractSchemaAwareTest {
- private static final LoadingCache<Set<YangModuleInfo>, EffectiveModelContext> SCHEMA_CONTEXT_CACHE =
+ private static final LoadingCache<Set<YangModuleInfo>, BindingRuntimeContext> RUNTIME_CONTEXT_CACHE =
CacheBuilder.newBuilder().weakValues().build(
- new CacheLoader<Set<YangModuleInfo>, EffectiveModelContext>() {
+ new CacheLoader<Set<YangModuleInfo>, BindingRuntimeContext>() {
@Override
- public EffectiveModelContext load(final Set<YangModuleInfo> key) {
- return BindingRuntimeHelpers.createEffectiveModel(key);
+ public BindingRuntimeContext load(final Set<YangModuleInfo> key) {
+ return BindingRuntimeHelpers.createRuntimeContext(key);
}
});
@Before
public final void setup() throws Exception {
- setupWithSchema(getSchemaContext());
+ setupWithRuntimeContext(getRuntimeContext());
}
protected Set<YangModuleInfo> getModuleInfos() throws Exception {
return BindingReflections.cacheModuleInfos(Thread.currentThread().getContextClassLoader());
}
+ protected BindingRuntimeContext getRuntimeContext() throws Exception {
+ return RUNTIME_CONTEXT_CACHE.getUnchecked(getModuleInfos());
+ }
+
protected EffectiveModelContext getSchemaContext() throws Exception {
- return SCHEMA_CONTEXT_CACHE.getUnchecked(getModuleInfos());
+ return getRuntimeContext().getEffectiveModelContext();
+ }
+
+ protected void setupWithRuntimeContext(final BindingRuntimeContext runtimeContext) {
+ setupWithSchema(runtimeContext.getEffectiveModelContext());
}
/**
- * Setups test with Schema context.
+ * Setups test with EffectiveModelContext.
*
* @param context schema context
*/
- protected abstract void setupWithSchema(EffectiveModelContext context);
+ protected void setupWithSchema(final EffectiveModelContext context) {
+ // No-op
+ }
}
@Beta
public class BindingTestContext implements AutoCloseable {
- private MockAdapterContext codec;
-
private final ListeningExecutorService executor;
private final boolean startWithSchema;
}
public AdapterContext getCodec() {
- return codec;
+ return mockSchemaService;
}
protected BindingTestContext(final ListeningExecutorService executor, final boolean startWithSchema) {
public void startNewDataBroker() {
checkState(executor != null, "Executor needs to be set");
checkState(newDOMDataBroker != null, "DOM Data Broker must be set");
- dataBroker = new BindingDOMDataBrokerAdapter(codec, newDOMDataBroker);
+ dataBroker = new BindingDOMDataBrokerAdapter(mockSchemaService, newDOMDataBroker);
}
public void startNewDomDataBroker() {
public void startBindingBroker() {
checkState(executor != null, "Executor needs to be set");
- baConsumerRpc = new BindingDOMRpcServiceAdapter(codec, getDomRpcInvoker());
- baProviderRpc = new BindingDOMRpcProviderServiceAdapter(codec, getDomRpcRegistry());
- final MountPointService mountService = new BindingDOMMountPointServiceAdapter(codec, biMountImpl);
+ baConsumerRpc = new BindingDOMRpcServiceAdapter(mockSchemaService, getDomRpcInvoker());
+ baProviderRpc = new BindingDOMRpcProviderServiceAdapter(mockSchemaService, getDomRpcRegistry());
+ final MountPointService mountService = new BindingDOMMountPointServiceAdapter(mockSchemaService, biMountImpl);
}
public void startForwarding() {
}
public void startBindingToDomMappingService() {
- codec = new MockAdapterContext();
- mockSchemaService.registerSchemaContextListener(codec);
+ // No-op, really
}
private void updateYangSchema(final Set<YangModuleInfo> moduleInfos) {
- mockSchemaService.changeSchema(BindingRuntimeHelpers.createEffectiveModel(moduleInfos));
+ mockSchemaService.changeSchema(BindingRuntimeHelpers.createRuntimeContext(moduleInfos));
}
public EffectiveModelContext getContext() {
final DOMNotificationRouter router = DOMNotificationRouter.create(16);
domPublishService = router;
domListenService = router;
- publishService = new BindingDOMNotificationPublishServiceAdapter(codec, domPublishService);
- listenService = new BindingDOMNotificationServiceAdapter(codec, domListenService);
+ publishService = new BindingDOMNotificationPublishServiceAdapter(mockSchemaService, domPublishService);
+ listenService = new BindingDOMNotificationServiceAdapter(mockSchemaService, domListenService);
}
+++ /dev/null
-/*
- * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.mdsal.binding.dom.adapter.test.util;
-
-import static com.google.common.base.Verify.verifyNotNull;
-
-import org.opendaylight.mdsal.binding.dom.adapter.AdapterContext;
-import org.opendaylight.mdsal.binding.dom.adapter.CurrentAdapterSerializer;
-import org.opendaylight.mdsal.binding.dom.codec.impl.BindingCodecContext;
-import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
-import org.opendaylight.mdsal.binding.runtime.api.DefaultBindingRuntimeContext;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
-
-public final class MockAdapterContext implements AdapterContext, EffectiveModelContextListener {
- private volatile CurrentAdapterSerializer serializer = null;
-
- @Override
- public CurrentAdapterSerializer currentSerializer() {
- return verifyNotNull(serializer);
- }
-
- @Override
- public void onModelContextUpdated(final EffectiveModelContext newModelContext) {
- serializer = new CurrentAdapterSerializer(new BindingCodecContext(new DefaultBindingRuntimeContext(
- new DefaultBindingRuntimeGenerator().generateTypeMapping(newModelContext),
- TestingModuleInfoSnapshot.INSTANCE)));
- }
-}
*/
package org.opendaylight.mdsal.binding.dom.adapter.test.util;
+import static com.google.common.base.Verify.verifyNotNull;
+
import com.google.common.collect.ClassToInstanceMap;
import com.google.common.collect.ImmutableClassToInstanceMap;
+import org.opendaylight.mdsal.binding.dom.adapter.AdapterContext;
+import org.opendaylight.mdsal.binding.dom.adapter.CurrentAdapterSerializer;
+import org.opendaylight.mdsal.binding.dom.codec.impl.BindingCodecContext;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
import org.opendaylight.mdsal.dom.api.DOMSchemaService;
import org.opendaylight.mdsal.dom.api.DOMSchemaServiceExtension;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
-public final class MockSchemaService implements DOMSchemaService, EffectiveModelContextProvider {
-
+public final class MockSchemaService implements DOMSchemaService, EffectiveModelContextProvider, AdapterContext {
private EffectiveModelContext schemaContext;
+ private CurrentAdapterSerializer serializer;
final ListenerRegistry<EffectiveModelContextListener> listeners = ListenerRegistry.create();
return ImmutableClassToInstanceMap.of();
}
- public synchronized void changeSchema(final EffectiveModelContext newContext) {
- schemaContext = newContext;
+ public synchronized void changeSchema(final BindingRuntimeContext newContext) {
+ serializer = new CurrentAdapterSerializer(new BindingCodecContext(newContext));
+ schemaContext = newContext.getEffectiveModelContext();
listeners.streamListeners().forEach(listener -> listener.onModelContextUpdated(schemaContext));
}
+
+ @Override
+ public synchronized CurrentAdapterSerializer currentSerializer() {
+ return verifyNotNull(serializer);
+ }
}
+++ /dev/null
-/*
- * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.mdsal.binding.dom.adapter.test.util;
-
-import com.google.common.util.concurrent.ListenableFuture;
-import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
-import org.opendaylight.yangtools.util.ClassLoaderUtils;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
-import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-
-public final class TestingModuleInfoSnapshot implements ModuleInfoSnapshot {
- public static final TestingModuleInfoSnapshot INSTANCE = new TestingModuleInfoSnapshot();
-
- private TestingModuleInfoSnapshot() {
- // Hidden on purpose
- }
-
- @Override
- public EffectiveModelContext getEffectiveModelContext() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public ListenableFuture<? extends YangTextSchemaSource> getSource(SourceIdentifier sourceIdentifier) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public <T> Class<T> loadClass(String fullyQualifiedName) throws ClassNotFoundException {
- return (Class<T>) ClassLoaderUtils.loadClassWithTCCL(fullyQualifiedName);
- }
-}
}
}
+ public static @NonNull BindingRuntimeContext createRuntimeContext(
+ final Collection<? extends YangModuleInfo> infos) {
+ final ModuleInfoSnapshot snapshot;
+
+ try {
+ snapshot = prepareContext(ServiceLoaderState.ParserFactory.INSTANCE, infos);
+ } catch (YangParserException e) {
+ throw new IllegalStateException("Failed to parse models", e);
+ }
+
+ return new DefaultBindingRuntimeContext(
+ ServiceLoaderState.Generator.INSTANCE.generateTypeMapping(snapshot.getEffectiveModelContext()), snapshot);
+ }
+
public static @NonNull BindingRuntimeContext createRuntimeContext(final YangParserFactory parserFactory,
final BindingRuntimeGenerator generator, final Class<?>... classes) throws YangParserException {
return createRuntimeContext(parserFactory, generator, Arrays.asList(classes));