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 java.util.function.Consumer;
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.spi.BindingDOMCodecFactory;
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.yangtools.concepts.Registration;
24 import org.opendaylight.yangtools.util.ObjectRegistry;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 public final class MockSchemaService implements DOMSchemaService, AdapterContext {
28 // Codec has some amount of non-trivial state, such as generated classes. Its operation should not be affected by
29 // anything except BindingRuntimeContext, hence we should be able to reuse it.
30 private static final LoadingCache<BindingRuntimeContext, BindingDOMCodecServices> CODEC_CACHE =
31 CacheBuilder.newBuilder().weakKeys().weakValues().build(
32 new CacheLoader<BindingRuntimeContext, BindingDOMCodecServices>() {
34 public BindingDOMCodecServices load(final BindingRuntimeContext key) {
35 return ServiceLoader.load(BindingDOMCodecFactory.class)
36 .findFirst().orElseThrow().createBindingDOMCodec(key);
40 private EffectiveModelContext modelContext;
41 private CurrentAdapterSerializer serializer;
43 final ObjectRegistry<Consumer<EffectiveModelContext>> listeners = ObjectRegistry.createConcurrent("mock schema");
46 public synchronized EffectiveModelContext getGlobalContext() {
51 public Registration registerSchemaContextListener(final Consumer<EffectiveModelContext> listener) {
52 return listeners.register(listener);
55 public synchronized void changeSchema(final BindingRuntimeContext newContext) {
56 serializer = new CurrentAdapterSerializer(CODEC_CACHE.getUnchecked(newContext));
57 modelContext = newContext.modelContext();
58 listeners.streamObjects().forEach(listener -> listener.accept(modelContext));
62 public synchronized CurrentAdapterSerializer currentSerializer() {
63 return verifyNotNull(serializer);