Cache reflection operations in AbstractSchemaAwareTest
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / 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.controller.md.sal.binding.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.Set;
15 import org.junit.Before;
16 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
17 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
18 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20
21 public abstract class AbstractSchemaAwareTest {
22     private static final LoadingCache<ClassLoader, Set<YangModuleInfo>> MODULE_INFO_CACHE = CacheBuilder.newBuilder()
23             .weakKeys().weakValues().build(new CacheLoader<ClassLoader, Set<YangModuleInfo>>() {
24                 @Override
25                 public Set<YangModuleInfo> load(final ClassLoader key) {
26                     return BindingReflections.loadModuleInfos(key);
27                 }
28             });
29     private static final LoadingCache<Set<YangModuleInfo>, SchemaContext> SCHEMA_CONTEXT_CACHE =
30             CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Set<YangModuleInfo>, SchemaContext>() {
31                 @Override
32                 public SchemaContext load(final Set<YangModuleInfo> key) {
33                     final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
34                     moduleContext.addModuleInfos(key);
35                     return moduleContext.tryToCreateSchemaContext().get();
36                 }
37             });
38
39     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
40         return MODULE_INFO_CACHE.getUnchecked(Thread.currentThread().getContextClassLoader());
41     }
42
43     protected SchemaContext getSchemaContext() throws Exception {
44         // ImmutableSet guarantees non-null
45         return SCHEMA_CONTEXT_CACHE.getUnchecked(ImmutableSet.copyOf(getModuleInfos()));
46     }
47
48     @Before
49     public final void setup() throws Exception {
50         setupWithSchema(getSchemaContext());
51     }
52
53     /**
54      * Setups test with Schema context.
55      * This method is called before {@link #setupWithSchemaService(SchemaService)}
56      */
57     protected abstract void setupWithSchema(SchemaContext context);
58
59 }