2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.dom.adapter.test.util;
10 import static com.google.common.base.Verify.verifyNotNull;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import com.google.common.collect.ClassToInstanceMap;
16 import com.google.common.collect.ImmutableClassToInstanceMap;
17 import java.util.ServiceLoader;
18 import org.opendaylight.mdsal.binding.dom.adapter.AdapterContext;
19 import org.opendaylight.mdsal.binding.dom.adapter.CurrentAdapterSerializer;
20 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecFactory;
21 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
22 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
23 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
24 import org.opendaylight.mdsal.dom.api.DOMSchemaServiceExtension;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.util.ListenerRegistry;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
31 public final class MockSchemaService implements DOMSchemaService, EffectiveModelContextProvider, AdapterContext {
32 // Codec has some amount of non-trivial state, such as generated classes. Its operation should not be affected by
33 // anything except BindingRuntimeContext, hence we should be able to reuse it.
34 private static final LoadingCache<BindingRuntimeContext, BindingDOMCodecServices> CODEC_CACHE =
35 CacheBuilder.newBuilder().weakKeys().weakValues().build(
36 new CacheLoader<BindingRuntimeContext, BindingDOMCodecServices>() {
38 public BindingDOMCodecServices load(final BindingRuntimeContext key) {
39 return ServiceLoader.load(BindingDOMCodecFactory.class)
40 .findFirst().orElseThrow().createBindingDOMCodec(key);
44 private EffectiveModelContext schemaContext;
45 private CurrentAdapterSerializer serializer;
47 final ListenerRegistry<EffectiveModelContextListener> listeners = ListenerRegistry.create();
50 public synchronized EffectiveModelContext getGlobalContext() {
55 public ListenerRegistration<EffectiveModelContextListener> registerSchemaContextListener(
56 final EffectiveModelContextListener listener) {
57 return listeners.register(listener);
61 public synchronized EffectiveModelContext getEffectiveModelContext() {
66 public ClassToInstanceMap<DOMSchemaServiceExtension> getExtensions() {
67 return ImmutableClassToInstanceMap.of();
70 public synchronized void changeSchema(final BindingRuntimeContext newContext) {
71 serializer = new CurrentAdapterSerializer(CODEC_CACHE.getUnchecked(newContext));
72 schemaContext = newContext.getEffectiveModelContext();
73 listeners.streamListeners().forEach(listener -> listener.onModelContextUpdated(schemaContext));
77 public synchronized CurrentAdapterSerializer currentSerializer() {
78 return verifyNotNull(serializer);