Remove netvirt from SouthboundIT since netvirt has it now.
[ovsdb.git] / southbound / southbound-it / src / test / java / org / opendaylight / ovsdb / southbound / it / AbstractConfigTestBase.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 package org.opendaylight.ovsdb.southbound.it;
9
10 import static org.ops4j.pax.exam.CoreOptions.maven;
11 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
12 //import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
13 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
14 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
15
16 import com.google.common.collect.ObjectArrays;
17
18 import java.io.File;
19 import java.lang.management.ManagementFactory;
20 import java.util.Calendar;
21
22 import javax.management.InstanceNotFoundException;
23
24 import org.junit.Rule;
25 import org.junit.internal.AssumptionViolatedException;
26 import org.junit.rules.TestRule;
27 import org.junit.rules.TestWatcher;
28 import org.junit.runner.Description;
29 import org.opendaylight.controller.config.api.ConfigRegistry;
30 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
31 import org.ops4j.pax.exam.Configuration;
32 import org.ops4j.pax.exam.Option;
33 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
34 import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
35 import org.ops4j.pax.exam.options.MavenUrlReference;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public abstract class AbstractConfigTestBase {
40     private static final Logger LOG = LoggerFactory.getLogger(AbstractConfigTestBase.class);
41
42     /*
43      * Wait up to 10s for our configured module to come up
44      */
45     private static final int MODULE_TIMEOUT = 10000;
46     private static int configTimes = 0;
47
48     public abstract String getModuleName();
49
50     public abstract String getInstanceName();
51
52     public abstract MavenUrlReference getFeatureRepo();
53
54     public abstract String getFeatureName();
55
56     public Option[] getLoggingOptions() {
57         Option[] options = new Option[] {
58                 editConfigurationFilePut(SouthboundITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
59                         logConfiguration(AbstractConfigTestBase.class),
60                         LogLevel.INFO.name())
61         };
62         return options;
63     }
64
65     public String logConfiguration(Class<?> klazz) {
66         return "log4j.logger." + klazz.getPackage().getName();
67     }
68
69     public Option[] getFeaturesOptions() {
70         return new Option[]{};
71     }
72
73     public Option[] getPropertiesOptions() {
74         return new Option[]{};
75     }
76
77     public MavenArtifactUrlReference getKarafDistro() {
78         /*MavenArtifactUrlReference karafUrl = maven()
79                 .groupId("org.opendaylight.controller")
80                 .artifactId("opendaylight-karaf-empty")
81                 .version("1.5.0-SNAPSHOT")
82                 .type("zip");*/
83         MavenArtifactUrlReference karafUrl = maven()
84                 .groupId("org.opendaylight.ovsdb")
85                 .artifactId("southbound-karaf")
86                 .version("1.1.0-SNAPSHOT")
87                 .type("zip");
88         return karafUrl;
89     }
90
91     @Configuration
92     public Option[] config() {
93         LOG.info("Calling config, configTimes: {}", configTimes);
94         configTimes++;
95         Option[] options = new Option[] {
96                 // KarafDistributionOption.debugConfiguration("5005", true),
97                 karafDistributionConfiguration()
98                         .frameworkUrl(getKarafDistro())
99                         .unpackDirectory(new File("target/exam"))
100                         .useDeployFolder(false),
101                 keepRuntimeFolder(),
102                 //features(getFeatureRepo() , getFeatureName())
103         };
104         options = ObjectArrays.concat(options, getFeaturesOptions(), Option.class);
105         options = ObjectArrays.concat(options, getLoggingOptions(), Option.class);
106         options = ObjectArrays.concat(options, getPropertiesOptions(), Option.class);
107         return options;
108     }
109
110     public void setup() throws Exception {
111         LOG.info("Module: {} Instance: {} attempting to configure.",
112                 getModuleName(),getInstanceName());
113         Calendar start = Calendar.getInstance();
114         ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory
115                 .getPlatformMBeanServer());
116         for (int timer = 0;timer < MODULE_TIMEOUT;timer++) {
117             try {
118                 configRegistryClient.lookupConfigBean(getModuleName(), getInstanceName());
119                 Thread.sleep(1);
120             } catch (InstanceNotFoundException e) {
121                 if (timer >= MODULE_TIMEOUT) {
122                     throw e;
123                 }
124             } catch (InterruptedException e) {
125                 LOG.error("Exception: ",e);
126             }
127         }
128         Calendar stop = Calendar.getInstance();
129         LOG.info("Module: {} Instance: {} configured after {} ms",
130                 getModuleName(),getInstanceName(),
131                 stop.getTimeInMillis() - start.getTimeInMillis());
132     }
133
134     @Rule
135     public TestRule watcher = new TestWatcher() {
136         @Override
137         protected void starting(Description description) {
138             LOG.info("TestWatcher: Starting test:\n{}", description.getDisplayName());
139         }
140
141         @Override
142         protected void finished(Description description) {
143             LOG.info("TestWatcher: Finished test:\n{}", description.getDisplayName());
144         }
145
146         @Override
147         protected void succeeded(Description description) {
148             LOG.info("TestWatcher: Test succeeded:\n{}", description.getDisplayName());
149         }
150
151         @Override
152         protected void failed(Throwable ex, Description description) {
153             LOG.info("TestWatcher: Test failed:\n{} ", description.getDisplayName(), ex);
154         }
155
156         @Override
157         protected void skipped(AssumptionViolatedException ex, Description description) {
158             LOG.info("TestWatcher: Test skipped:\n{} ", description.getDisplayName(), ex);
159         }
160     };
161 }