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 org.opendaylight.mdsal.binding.dom.adapter.AdapterContext;
18 import org.opendaylight.mdsal.binding.dom.adapter.CurrentAdapterSerializer;
19 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingCodecContext;
20 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
21 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
22 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
23 import org.opendaylight.mdsal.dom.api.DOMSchemaServiceExtension;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.util.ListenerRegistry;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
30 public final class MockSchemaService implements DOMSchemaService, EffectiveModelContextProvider, AdapterContext {
31 // Codec has some amount of non-trivial state, such as generated classes. Its operation should not be affected by
32 // anything except BindingRuntimeContext, hence we should be able to reuse it.
33 private static final LoadingCache<BindingRuntimeContext, BindingDOMCodecServices> CODEC_CACHE =
34 CacheBuilder.newBuilder().weakKeys().weakValues().build(
35 new CacheLoader<BindingRuntimeContext, BindingDOMCodecServices>() {
37 public BindingDOMCodecServices load(final BindingRuntimeContext key) {
38 return new BindingCodecContext(key);
42 private EffectiveModelContext schemaContext;
43 private CurrentAdapterSerializer serializer;
45 final ListenerRegistry<EffectiveModelContextListener> listeners = ListenerRegistry.create();
48 public synchronized EffectiveModelContext getGlobalContext() {
53 public ListenerRegistration<EffectiveModelContextListener> registerSchemaContextListener(
54 final EffectiveModelContextListener listener) {
55 return listeners.register(listener);
59 public synchronized EffectiveModelContext getEffectiveModelContext() {
64 public ClassToInstanceMap<DOMSchemaServiceExtension> getExtensions() {
65 return ImmutableClassToInstanceMap.of();
68 public synchronized void changeSchema(final BindingRuntimeContext newContext) {
69 serializer = new CurrentAdapterSerializer(CODEC_CACHE.getUnchecked(newContext));
70 schemaContext = newContext.getEffectiveModelContext();
71 listeners.streamListeners().forEach(listener -> listener.onModelContextUpdated(schemaContext));
75 public synchronized CurrentAdapterSerializer currentSerializer() {
76 return verifyNotNull(serializer);