Bump yangtools to 13.0.0
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / test / AbstractSchemaAwareTest.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;
9
10 import com.google.common.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.ServiceLoader;
15 import java.util.Set;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.junit.Before;
18 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
19 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
20 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
21 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23
24 public abstract class AbstractSchemaAwareTest {
25     private static final LoadingCache<Set<YangModuleInfo>, BindingRuntimeContext> RUNTIME_CONTEXT_CACHE =
26             CacheBuilder.newBuilder().weakValues().build(
27                 new CacheLoader<Set<YangModuleInfo>, BindingRuntimeContext>() {
28                     @Override
29                     public BindingRuntimeContext load(final Set<YangModuleInfo> key) {
30                         return BindingRuntimeHelpers.createRuntimeContext(key);
31                     }
32                 });
33     private static final LoadingCache<ClassLoader, ImmutableSet<YangModuleInfo>> MODULE_INFO_CACHE =
34         CacheBuilder.newBuilder().weakKeys().weakValues().build(
35             new CacheLoader<ClassLoader, ImmutableSet<YangModuleInfo>>() {
36                 @Override
37                 public ImmutableSet<YangModuleInfo> load(final ClassLoader key) {
38                     return BindingRuntimeHelpers.loadModuleInfos(key);
39                 }
40             });
41
42
43     @Before
44     public final void setup() throws Exception {
45         setupWithRuntimeContext(getRuntimeContext());
46     }
47
48     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
49         return cacheModuleInfos(Thread.currentThread().getContextClassLoader());
50     }
51
52     protected BindingRuntimeContext getRuntimeContext() throws Exception {
53         return RUNTIME_CONTEXT_CACHE.getUnchecked(getModuleInfos());
54     }
55
56     protected EffectiveModelContext modelContext() throws Exception {
57         return getRuntimeContext().modelContext();
58     }
59
60     protected void setupWithRuntimeContext(final BindingRuntimeContext runtimeContext) {
61         setupWithSchema(runtimeContext.modelContext());
62     }
63
64     /**
65      * Setups test with EffectiveModelContext.
66      *
67      * @param context schema context
68      */
69     protected void setupWithSchema(final EffectiveModelContext context) {
70         // No-op
71     }
72
73     /**
74      * Loads {@link YangModuleInfo} instances available on supplied {@link ClassLoader}, assuming the set of available
75      * information does not change. Subsequent accesses may return cached values.
76      *
77      * <p>
78      * {@link YangModuleInfo} are discovered using {@link ServiceLoader} for {@link YangModelBindingProvider}.
79      * {@link YangModelBindingProvider} are simple classes which holds only pointers to actual instance
80      * {@link YangModuleInfo}.
81      *
82      * <p>
83      * When {@link YangModuleInfo} is available, all dependencies are recursively collected into returning set by
84      * collecting results of {@link YangModuleInfo#getImportedModules()}.
85      *
86      * @param loader Class loader for which {@link YangModuleInfo} should be retrieved.
87      * @return Set of {@link YangModuleInfo} available for supplied classloader.
88      */
89     protected static final @NonNull ImmutableSet<YangModuleInfo> cacheModuleInfos(final ClassLoader loader) {
90         return MODULE_INFO_CACHE.getUnchecked(loader);
91     }
92 }