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