2da4f4a5f05022585aa56e0ebd450ef7ec5bc5ab
[sfc.git] /
1 /**
2  * Copyright (c) 2018 Inocybe 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.sfc.statistics.testutils;
10
11 import static com.google.common.base.Preconditions.checkState;
12
13 import com.google.common.collect.ImmutableSet;
14
15 import java.io.IOException;
16 import java.util.Set;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
23 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
24 import org.opendaylight.sfc.provider.api.SfcDataStoreAPI;
25 import org.opendaylight.sfc.provider.api.SfcInstanceIdentifiers;
26 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.service.path.id.rev150804.service.path.ids.ServicePathId;
27 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePath;
28 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunction;
29 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.state.ServiceFunctionState;
30 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.service.function.chain.grouping.ServiceFunctionChain;
31 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.ovs.rev140701.SffOvsBridgeAugmentationBuilder;
32 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder;
33 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfg.rev150214.service.function.groups.ServiceFunctionGroup;
34 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
35 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sft.rev140701.ServiceFunctionTypes;
36 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.sfc.of.renderer.rev151123.SfcOfTableOffsets;
37 import org.opendaylight.yang.gen.v1.urn.intel.params.xml.ns.sf.desc.mon.rev141201.ServiceFunctionState1;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
41 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
42
43 public class AbstractDataStoreManager extends AbstractConcurrentDataBrokerTest {
44     protected DataBroker dataBroker;
45     protected SfcInstanceIdentifiers sfcIids;
46     protected static ExecutorService executor = Executors.newFixedThreadPool(5);
47
48     // initial sfc setup, executor is set only once, new data broker
49     // is created before every set, it ensures empty data store
50     protected void setupSfc() {
51         dataBroker = getDataBroker();
52         SfcDataStoreAPI.setDataProviderAux(dataBroker);
53         sfcIids = new SfcInstanceIdentifiers();
54     }
55
56     protected void close() throws ExecutionException, InterruptedException {
57         if (sfcIids != null) {
58             // Deletes everything from SFC that was created in the datastore
59             sfcIids.close();
60         }
61     }
62
63     /*
64      * loads only SFC YANG modules - increased performance Specify a class from
65      * YANG which should be loaded
66      */
67     @Override
68     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
69         ImmutableSet.Builder<YangModuleInfo> moduleInfoSet = ImmutableSet.<YangModuleInfo>builder();
70         loadModuleInfos(Nodes.class, moduleInfoSet);
71         loadModuleInfos(FlowCapableNode.class, moduleInfoSet);
72         loadModuleInfos(FlowStatisticsData.class, moduleInfoSet);
73         loadModuleInfos(ServiceFunction.class, moduleInfoSet);
74         loadModuleInfos(ServiceFunctionForwarder.class, moduleInfoSet);
75         loadModuleInfos(SffOvsBridgeAugmentationBuilder.class, moduleInfoSet);
76         loadModuleInfos(SfcOfTableOffsets.class, moduleInfoSet);
77         loadModuleInfos(ServiceFunctionChain.class, moduleInfoSet);
78         loadModuleInfos(ServiceFunctionPath.class, moduleInfoSet);
79         loadModuleInfos(ServiceFunctionGroup.class, moduleInfoSet);
80         loadModuleInfos(ServicePathId.class, moduleInfoSet);
81         loadModuleInfos(RenderedServicePath.class, moduleInfoSet);
82         loadModuleInfos(ServiceFunctionState.class, moduleInfoSet);
83         loadModuleInfos(ServiceFunctionState1.class, moduleInfoSet);
84         loadModuleInfos(ServiceFunctionTypes.class, moduleInfoSet);
85         return moduleInfoSet.build();
86     }
87
88     public static void loadModuleInfos(Class<?> clazzFromModule, ImmutableSet.Builder<YangModuleInfo> moduleInfoSet)
89             throws Exception {
90         YangModuleInfo moduleInfo = BindingReflections.getModuleInfo(clazzFromModule);
91         checkState(moduleInfo != null, "Module Info for %s is not available.", clazzFromModule);
92         collectYangModuleInfo(moduleInfo, moduleInfoSet);
93     }
94
95     private static void collectYangModuleInfo(final YangModuleInfo moduleInfo,
96             final ImmutableSet.Builder<YangModuleInfo> moduleInfoSet) throws IOException {
97         moduleInfoSet.add(moduleInfo);
98         for (YangModuleInfo dependency : moduleInfo.getImportedModules()) {
99             collectYangModuleInfo(dependency, moduleInfoSet);
100         }
101     }
102 }