Cache BindingDOMCodecServices in tests
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / test / util / MockSchemaService.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter.test.util;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
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;
29
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>() {
36                 @Override
37                 public BindingDOMCodecServices load(final BindingRuntimeContext key) {
38                     return new BindingCodecContext(key);
39                 }
40             });
41
42     private EffectiveModelContext schemaContext;
43     private CurrentAdapterSerializer serializer;
44
45     final ListenerRegistry<EffectiveModelContextListener> listeners = ListenerRegistry.create();
46
47     @Override
48     public synchronized EffectiveModelContext getGlobalContext() {
49         return schemaContext;
50     }
51
52     @Override
53     public ListenerRegistration<EffectiveModelContextListener> registerSchemaContextListener(
54             final EffectiveModelContextListener listener) {
55         return listeners.register(listener);
56     }
57
58     @Override
59     public synchronized EffectiveModelContext getEffectiveModelContext() {
60         return schemaContext;
61     }
62
63     @Override
64     public ClassToInstanceMap<DOMSchemaServiceExtension> getExtensions() {
65         return ImmutableClassToInstanceMap.of();
66     }
67
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));
72     }
73
74     @Override
75     public synchronized CurrentAdapterSerializer currentSerializer() {
76         return verifyNotNull(serializer);
77     }
78 }