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