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