Merge branch 'master' into topic/netvirtsbmerge
[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(boolean extras) {
78         MavenArtifactUrlReference karafUrl;
79         if (!extras) {
80             karafUrl = maven()
81                     // karaf-empty is busted
82                     //.groupId("org.opendaylight.controller")
83                     //.artifactId("opendaylight-karaf-empty")
84                     //.version("1.5.0-SNAPSHOT")
85                     .groupId("org.opendaylight.ovsdb")
86                     .artifactId("southbound-karaf")
87                     .version("1.1.0-SNAPSHOT")
88                     .type("zip");
89         } else {
90             karafUrl = maven()
91                     .groupId("org.opendaylight.ovsdb")
92                     .artifactId("karaf")
93                     .version("1.1.0-SNAPSHOT")
94                     .type("zip");
95         }
96         return karafUrl;
97     }
98
99     @Configuration
100     public Option[] config() {
101         LOG.info("Calling config, configTimes: {}", configTimes);
102         configTimes++;
103         Option[] options = new Option[] {
104                 //KarafDistributionOption.debugConfiguration("5005", true),
105                 karafDistributionConfiguration()
106                         .frameworkUrl(getKarafDistro(extras))
107                         .unpackDirectory(new File("target/exam"))
108                         .useDeployFolder(false),
109                 keepRuntimeFolder(),
110                 //features(getFeatureRepo() , getFeatureName())
111         };
112         options = ObjectArrays.concat(options, getFeaturesOptions(), Option.class);
113         options = ObjectArrays.concat(options, getLoggingOptions(), Option.class);
114         options = ObjectArrays.concat(options, getPropertiesOptions(), Option.class);
115         return options;
116     }
117
118     public void setup() throws Exception {
119         LOG.info("Module: {} Instance: {} attempting to configure.",
120                 getModuleName(),getInstanceName());
121         Calendar start = Calendar.getInstance();
122         ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory
123                 .getPlatformMBeanServer());
124         for (int timer = 0;timer < MODULE_TIMEOUT;timer++) {
125             try {
126                 configRegistryClient.lookupConfigBean(getModuleName(), getInstanceName());
127                 Thread.sleep(1);
128             } catch (InstanceNotFoundException e) {
129                 if (timer >= MODULE_TIMEOUT) {
130                     throw e;
131                 }
132             } catch (InterruptedException e) {
133                 LOG.error("Exception: ",e);
134             }
135         }
136         Calendar stop = Calendar.getInstance();
137         LOG.info("Module: {} Instance: {} configured after {} ms",
138                 getModuleName(),getInstanceName(),
139                 stop.getTimeInMillis() - start.getTimeInMillis());
140     }
141
142     @Rule
143     public TestRule watcher = new TestWatcher() {
144         @Override
145         protected void starting(Description description) {
146             LOG.info("TestWatcher: Starting test:\n{}", description.getDisplayName());
147         }
148
149         @Override
150         protected void finished(Description description) {
151             LOG.info("TestWatcher: Finished test:\n{}", description.getDisplayName());
152         }
153
154         @Override
155         protected void succeeded(Description description) {
156             LOG.info("TestWatcher: Test succeeded:\n{}", description.getDisplayName());
157         }
158
159         @Override
160         protected void failed(Throwable ex, Description description) {
161             LOG.info("TestWatcher: Test failed:\n{} ", description.getDisplayName(), ex);
162         }
163
164         @Override
165         protected void skipped(AssumptionViolatedException ex, Description description) {
166             LOG.info("TestWatcher: Test skipped:\n{} ", description.getDisplayName(), ex);
167         }
168     };
169 }