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 java.util.ServiceLoader;
16 import org.opendaylight.mdsal.binding.dom.adapter.AdapterContext;
17 import org.opendaylight.mdsal.binding.dom.adapter.CurrentAdapterSerializer;
18 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecFactory;
19 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
20 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
21 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.util.ListenerRegistry;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
28 public final class MockSchemaService implements DOMSchemaService, EffectiveModelContextProvider, AdapterContext {
29 // Codec has some amount of non-trivial state, such as generated classes. Its operation should not be affected by
30 // anything except BindingRuntimeContext, hence we should be able to reuse it.
31 private static final LoadingCache<BindingRuntimeContext, BindingDOMCodecServices> CODEC_CACHE =
32 CacheBuilder.newBuilder().weakKeys().weakValues().build(
33 new CacheLoader<BindingRuntimeContext, BindingDOMCodecServices>() {
35 public BindingDOMCodecServices load(final BindingRuntimeContext key) {
36 return ServiceLoader.load(BindingDOMCodecFactory.class)
37 .findFirst().orElseThrow().createBindingDOMCodec(key);
41 private EffectiveModelContext schemaContext;
42 private CurrentAdapterSerializer serializer;
44 final ListenerRegistry<EffectiveModelContextListener> listeners = ListenerRegistry.create();
47 public synchronized EffectiveModelContext getGlobalContext() {
52 public ListenerRegistration<EffectiveModelContextListener> registerSchemaContextListener(
53 final EffectiveModelContextListener listener) {
54 return listeners.register(listener);
58 public synchronized EffectiveModelContext getEffectiveModelContext() {
62 public synchronized void changeSchema(final BindingRuntimeContext newContext) {
63 serializer = new CurrentAdapterSerializer(CODEC_CACHE.getUnchecked(newContext));
64 schemaContext = newContext.getEffectiveModelContext();
65 listeners.streamListeners().forEach(listener -> listener.onModelContextUpdated(schemaContext));
69 public synchronized CurrentAdapterSerializer currentSerializer() {
70 return verifyNotNull(serializer);