apply checkstyle check during build for neutron-mapper
[groupbasedpolicy.git] / neutron-mapper / src / test / java / org / opendaylight / groupbasedpolicy / neutron / mapper / test / CustomDataBrokerTest.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.groupbasedpolicy.neutron.mapper.test;
10
11 import static com.google.common.base.Preconditions.checkState;
12
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.ImmutableSet.Builder;
15
16 import java.io.IOException;
17 import java.util.Collection;
18
19 import javax.annotation.Nonnull;
20
21 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
22 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
23 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
24
25 /**
26  * Loads only modules of GBP and it's dependencies for data broker.
27  * <br>
28  * Therefore this implementation is faster than {@link AbstractDataBrokerTest}
29  */
30 public abstract class CustomDataBrokerTest extends AbstractDataBrokerTest {
31
32     @Override
33     protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
34         Builder<YangModuleInfo> moduleInfoSet = ImmutableSet.<YangModuleInfo>builder();
35         for (Class<?> clazz : getClassesFromModules()) {
36             loadModuleInfos(clazz, moduleInfoSet);
37         }
38         return moduleInfoSet.build();
39     }
40
41     /**
42      * Used to get classes from yang modules.
43      *
44      * @return a class from every yang module which needs to be loaded. Cannot return {@code null}
45      *         or empty collection.
46      */
47     public abstract @Nonnull Collection<Class<?>> getClassesFromModules();
48
49     public static void loadModuleInfos(Class<?> clazzFromModule, Builder<YangModuleInfo> moduleInfoSet)
50             throws Exception {
51         YangModuleInfo moduleInfo = BindingReflections.getModuleInfo(clazzFromModule);
52         checkState(moduleInfo != null, "Module Info for %s is not available.", clazzFromModule);
53         collectYangModuleInfo(moduleInfo, moduleInfoSet);
54     }
55
56     private static void collectYangModuleInfo(final YangModuleInfo moduleInfo,
57             final Builder<YangModuleInfo> moduleInfoSet) throws IOException {
58         moduleInfoSet.add(moduleInfo);
59         for (YangModuleInfo dependency : moduleInfo.getImportedModules()) {
60             collectYangModuleInfo(dependency, moduleInfoSet);
61         }
62     }
63 }