Deprecate all MD-SAL APIs
[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 @Deprecated
22 public abstract class AbstractSchemaAwareTest {
23     private static final LoadingCache<ClassLoader, Set<YangModuleInfo>> MODULE_INFO_CACHE = CacheBuilder.newBuilder()
24             .weakKeys().weakValues().build(new CacheLoader<ClassLoader, Set<YangModuleInfo>>() {
25                 @Override
26                 public Set<YangModuleInfo> load(final ClassLoader key) {
27                     return BindingReflections.loadModuleInfos(key);
28                 }
29             });
30     private static final LoadingCache<Set<YangModuleInfo>, SchemaContext> SCHEMA_CONTEXT_CACHE =
31             CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Set<YangModuleInfo>, SchemaContext>() {
32                 @Override
33                 public SchemaContext load(final Set<YangModuleInfo> key) {
34                     final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
35                     moduleContext.addModuleInfos(key);
36                     return moduleContext.tryToCreateSchemaContext().get();
37                 }
38             });
39
40     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
41         return MODULE_INFO_CACHE.getUnchecked(Thread.currentThread().getContextClassLoader());
42     }
43
44     protected SchemaContext getSchemaContext() throws Exception {
45         // ImmutableSet guarantees non-null
46         return SCHEMA_CONTEXT_CACHE.getUnchecked(ImmutableSet.copyOf(getModuleInfos()));
47     }
48
49     @Before
50     public final void setup() throws Exception {
51         setupWithSchema(getSchemaContext());
52     }
53
54     /**
55      * Setups test with Schema context.
56      * This method is called before {@link #setupWithSchemaService(SchemaService)}
57      */
58     protected abstract void setupWithSchema(SchemaContext context);
59
60 }