Merge "Add constant class for shared strings in SouthboundIT"
[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.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
46     public abstract String getModuleName();
47
48     public abstract String getInstanceName();
49
50     public abstract MavenUrlReference getFeatureRepo();
51
52     public abstract String getFeatureName();
53
54     public Option[] getLoggingOptions() {
55         Option[] options = new Option[] {
56                 editConfigurationFilePut(SouthboundITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
57                         logConfiguration(AbstractConfigTestBase.class),
58                         LogLevel.INFO.name())
59         };
60         return options;
61     }
62
63     public String logConfiguration(Class<?> klazz) {
64         return "log4j.logger." + klazz.getPackage().getName();
65     }
66
67     public Option[] getPropertiesOptions() {
68         return null;
69     }
70
71     public MavenArtifactUrlReference getKarafDistro() {
72         MavenArtifactUrlReference karafUrl = maven()
73                 .groupId("org.opendaylight.controller")
74                 .artifactId("opendaylight-karaf-empty")
75                 .version("1.5.0-SNAPSHOT")
76                 .type("zip");
77         return karafUrl;
78     }
79
80     @Configuration
81     public Option[] config() {
82         Option[] options = new Option[] {
83                 // KarafDistributionOption.debugConfiguration("5005", true),
84                 karafDistributionConfiguration()
85                         .frameworkUrl(getKarafDistro())
86                         .unpackDirectory(new File("target/exam"))
87                         .useDeployFolder(false),
88                 keepRuntimeFolder(),
89                 features(getFeatureRepo() , getFeatureName()),
90         };
91         options = ObjectArrays.concat(options, getLoggingOptions(), Option.class);
92         options = ObjectArrays.concat(options, getPropertiesOptions(), Option.class);
93         return options;
94     }
95
96     public void setup() throws Exception {
97         LOG.info("Module: {} Instance: {} attempting to configure.",
98                 getModuleName(),getInstanceName());
99         Calendar start = Calendar.getInstance();
100         ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory
101                 .getPlatformMBeanServer());
102         for (int timer = 0;timer < MODULE_TIMEOUT;timer++) {
103             try {
104                 configRegistryClient.lookupConfigBean(getModuleName(), getInstanceName());
105                 Thread.sleep(1);
106             } catch (InstanceNotFoundException e) {
107                 if (timer < MODULE_TIMEOUT) {
108                     continue;
109                 } else {
110                     throw e;
111                 }
112             } catch (InterruptedException e) {
113                 LOG.error("Exception: ",e);
114             }
115         }
116         Calendar stop = Calendar.getInstance();
117         LOG.info("Module: {} Instance: {} configured after {} ms",
118                 getModuleName(),getInstanceName(),
119                 stop.getTimeInMillis() - start.getTimeInMillis());
120     }
121
122     @Rule
123     public TestRule watcher = new TestWatcher() {
124         @Override
125         protected void starting(Description description) {
126             LOG.info("TestWatcher: Starting test: {}",
127                     description.getDisplayName());
128         }
129
130         @Override
131         protected void finished(Description description) {
132             LOG.info("TestWatcher: Finished test: {}", description.getDisplayName());
133         }
134     };
135 }