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;
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;
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;
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>() {
29 public BindingRuntimeContext load(final Set<YangModuleInfo> key) {
30 return BindingRuntimeHelpers.createRuntimeContext(key);
33 private static final LoadingCache<ClassLoader, ImmutableSet<YangModuleInfo>> MODULE_INFO_CACHE =
34 CacheBuilder.newBuilder().weakKeys().weakValues().build(
35 new CacheLoader<ClassLoader, ImmutableSet<YangModuleInfo>>() {
37 public ImmutableSet<YangModuleInfo> load(final ClassLoader key) {
38 return BindingRuntimeHelpers.loadModuleInfos(key);
44 public final void setup() throws Exception {
45 setupWithRuntimeContext(getRuntimeContext());
48 protected Set<YangModuleInfo> getModuleInfos() throws Exception {
49 return cacheModuleInfos(Thread.currentThread().getContextClassLoader());
52 protected BindingRuntimeContext getRuntimeContext() throws Exception {
53 return RUNTIME_CONTEXT_CACHE.getUnchecked(getModuleInfos());
56 protected EffectiveModelContext modelContext() throws Exception {
57 return getRuntimeContext().modelContext();
60 protected void setupWithRuntimeContext(final BindingRuntimeContext runtimeContext) {
61 setupWithSchema(runtimeContext.modelContext());
65 * Setups test with EffectiveModelContext.
67 * @param context schema context
69 protected void setupWithSchema(final EffectiveModelContext context) {
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.
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}.
83 * When {@link YangModuleInfo} is available, all dependencies are recursively collected into returning set by
84 * collecting results of {@link YangModuleInfo#getImportedModules()}.
86 * @param loader Class loader for which {@link YangModuleInfo} should be retrieved.
87 * @return Set of {@link YangModuleInfo} available for supplied classloader.
89 protected static final @NonNull ImmutableSet<YangModuleInfo> cacheModuleInfos(final ClassLoader loader) {
90 return MODULE_INFO_CACHE.getUnchecked(loader);