4d964eec607135d39ce62e5ed82df1e1a8f090cb
[controller.git] / opendaylight / config / config-it-base / src / main / java / org / opendaylight / controller / config / it / base / AbstractConfigTestBase.java
1 /*
2  * Copyright (c) 2015, 2017 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
9 package org.opendaylight.controller.config.it.base;
10
11 import static org.ops4j.pax.exam.CoreOptions.maven;
12 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
13 import static org.ops4j.pax.exam.CoreOptions.when;
14 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
15 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
16 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
17 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
18
19 import com.google.common.base.Stopwatch;
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.lang.management.ManagementFactory;
24 import java.util.Properties;
25 import java.util.concurrent.TimeUnit;
26 import javax.management.ObjectName;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.internal.AssumptionViolatedException;
30 import org.junit.rules.TestRule;
31 import org.junit.rules.TestWatcher;
32 import org.junit.runner.Description;
33 import org.opendaylight.controller.config.api.ConfigRegistry;
34 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
35 import org.ops4j.pax.exam.Configuration;
36 import org.ops4j.pax.exam.Option;
37 import org.ops4j.pax.exam.OptionUtils;
38 import org.ops4j.pax.exam.karaf.options.KarafDistributionOption;
39 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
40 import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
41 import org.ops4j.pax.exam.options.MavenUrlReference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public abstract class AbstractConfigTestBase {
46     private static final String MAVEN_REPO_LOCAL = "maven.repo.local";
47     private static final String ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY = "org.ops4j.pax.url.mvn.localRepository";
48     private static final String ETC_ORG_OPS4J_PAX_URL_MVN_CFG = "etc/org.ops4j.pax.url.mvn.cfg";
49     private static final String ETC_ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
50
51     private static final String PAX_EXAM_UNPACK_DIRECTORY = "target/exam";
52     private static final String KARAF_DEBUG_PORT = "5005";
53     private static final String KARAF_DEBUG_PROP = "karaf.debug";
54     private static final String KEEP_UNPACK_DIRECTORY_PROP = "karaf.keep.unpack";
55     private static final Logger LOG = LoggerFactory.getLogger(AbstractConfigTestBase.class);
56     public static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
57
58     /*
59      * Default values for karaf distro type, groupId, and artifactId
60      */
61     private static final String KARAF_DISTRO_TYPE = "zip";
62     private static final String KARAF_DISTRO_ARTIFACTID = "opendaylight-karaf-empty";
63     private static final String KARAF_DISTRO_GROUPID = "org.opendaylight.odlparent";
64
65     /*
66      * Property names to override defaults for karaf distro artifactId, groupId,
67      * version, and type
68      */
69     private static final String KARAF_DISTRO_VERSION_PROP = "karaf.distro.version";
70     private static final String KARAF_DISTRO_TYPE_PROP = "karaf.distro.type";
71     private static final String KARAF_DISTRO_ARTIFACTID_PROP = "karaf.distro.artifactId";
72     private static final String KARAF_DISTRO_GROUPID_PROP = "karaf.distro.groupId";
73
74     /**
75      * Property file used to store the Karaf distribution version.
76      */
77     private static final String PROPERTIES_FILENAME = "abstractconfigtestbase.properties";
78
79     /*
80      * Wait up to 10s for our configured module to come up
81      */
82     private static final int MODULE_TIMEOUT_MILLIS = 60000;
83
84     /**
85      * This method need only be overridden if using the config system.
86      *
87      * @deprecated
88      *
89      * @return the config module name
90      */
91     @Deprecated
92     public String getModuleName() {
93         return null;
94     }
95
96     /**
97      * This method need only be overridden if using the config system.
98      *
99      * @deprecated
100      *
101      * @return the config module instance name
102      */
103     @Deprecated
104     public String getInstanceName() {
105         return null;
106     }
107
108     public abstract MavenUrlReference getFeatureRepo();
109
110     public abstract String getFeatureName();
111
112     public Option getLoggingOption() {
113         return editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG, logConfiguration(AbstractConfigTestBase.class),
114                 LogLevel.INFO.name());
115     }
116
117     /**
118      * Override this method to provide more options to config.
119      *
120      * @return An array of additional config options
121      */
122     protected Option[] getAdditionalOptions() {
123         return null;
124     }
125
126     public String logConfiguration(final Class<?> klazz) {
127         return "log4j.logger." + klazz.getPackage().getName();
128     }
129
130     public String getKarafDistro() {
131         String groupId = System.getProperty(KARAF_DISTRO_GROUPID_PROP, KARAF_DISTRO_GROUPID);
132         String artifactId = System.getProperty(KARAF_DISTRO_ARTIFACTID_PROP, KARAF_DISTRO_ARTIFACTID);
133         String version = System.getProperty(KARAF_DISTRO_VERSION_PROP);
134         String type = System.getProperty(KARAF_DISTRO_TYPE_PROP, KARAF_DISTRO_TYPE);
135         if (version == null) {
136             // We use a properties file to retrieve ${karaf.version}, instead of
137             // .versionAsInProject()
138             // This avoids forcing all users to depend on Karaf in their POMs
139             Properties abstractConfigTestBaseProps = new Properties();
140             try (InputStream abstractConfigTestBaseInputStream = Thread.currentThread().getContextClassLoader()
141                     .getResourceAsStream(PROPERTIES_FILENAME)) {
142                 abstractConfigTestBaseProps.load(abstractConfigTestBaseInputStream);
143             } catch (final IOException e) {
144                 LOG.error("Unable to load {} to determine the Karaf version", PROPERTIES_FILENAME, e);
145             }
146             version = abstractConfigTestBaseProps.getProperty(KARAF_DISTRO_VERSION_PROP);
147         }
148         MavenArtifactUrlReference karafUrl = maven().groupId(groupId).artifactId(artifactId).version(version)
149                 .type(type);
150         return karafUrl.getURL();
151     }
152
153     protected Option mvnLocalRepoOption() {
154         String mvnRepoLocal = System.getProperty(MAVEN_REPO_LOCAL, "");
155         LOG.info("mvnLocalRepo \"{}\"", mvnRepoLocal);
156         return editConfigurationFilePut(ETC_ORG_OPS4J_PAX_URL_MVN_CFG, ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY,
157                 mvnRepoLocal);
158     }
159
160     @Configuration
161     public Option[] config() {
162         Option[] options = new Option[] {
163                 when(Boolean.getBoolean(KARAF_DEBUG_PROP))
164                         .useOptions(KarafDistributionOption.debugConfiguration(KARAF_DEBUG_PORT, true)),
165                 karafDistributionConfiguration().frameworkUrl(getKarafDistro())
166                         .unpackDirectory(new File(PAX_EXAM_UNPACK_DIRECTORY)).useDeployFolder(false),
167                 when(Boolean.getBoolean(KEEP_UNPACK_DIRECTORY_PROP)).useOptions(keepRuntimeFolder()),
168                 features(getFeatureRepo(), getFeatureName()),
169                 mavenBundle("org.apache.aries.quiesce", "org.apache.aries.quiesce.api", "1.0.0"), getLoggingOption(),
170                 mvnLocalRepoOption(),
171                 editConfigurationFilePut(ETC_ORG_OPS4J_PAX_LOGGING_CFG, "log4j.rootLogger", "INFO, stdout, osgi:*") };
172         return OptionUtils.combine(options, getAdditionalOptions());
173     }
174
175     @Before
176     @SuppressWarnings("IllegalCatch")
177     public void setup() throws Exception {
178         String moduleName = getModuleName();
179         String instanceName = getInstanceName();
180         if (moduleName == null || instanceName == null) {
181             return;
182         }
183
184         LOG.info("Module: {} Instance: {} attempting to configure.", moduleName, instanceName);
185         Stopwatch stopWatch = Stopwatch.createStarted();
186         ObjectName objectName = null;
187         for (int i = 0; i < MODULE_TIMEOUT_MILLIS; i++) {
188             try {
189                 ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(
190                         ManagementFactory.getPlatformMBeanServer());
191                 objectName = configRegistryClient.lookupConfigBean(moduleName, instanceName);
192                 LOG.info("Module: {} Instance: {} ObjectName: {}.", moduleName, instanceName, objectName);
193                 break;
194             } catch (final Exception e) {
195                 if (i < MODULE_TIMEOUT_MILLIS) {
196                     Thread.sleep(1);
197                     continue;
198                 } else {
199                     throw e;
200                 }
201             }
202         }
203         if (objectName != null) {
204             LOG.info("Module: {} Instance: {} configured after {} ms", moduleName, instanceName,
205                     stopWatch.elapsed(TimeUnit.MILLISECONDS));
206         } else {
207             throw new RuntimeException("NOT FOUND Module: " + moduleName + " Instance: " + instanceName
208                     + " configured after " + stopWatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
209         }
210     }
211
212     @Rule
213     public TestRule watcher = new TestWatcher() {
214         @Override
215         protected void starting(final Description description) {
216             LOG.info("TestWatcher: Starting test: {}", description.getDisplayName());
217         }
218
219         @Override
220         protected void finished(final Description description) {
221             LOG.info("TestWatcher: Finished test: {}", description.getDisplayName());
222         }
223
224         @Override
225         protected void succeeded(final Description description) {
226             LOG.info("TestWatcher: Test succeeded: {}", description.getDisplayName());
227         }
228
229         @Override
230         protected void failed(final Throwable ex, final Description description) {
231             LOG.info("TestWatcher: Test failed: {}", description.getDisplayName(), ex);
232         }
233
234         @Override
235         protected void skipped(final AssumptionViolatedException ex, final Description description) {
236             LOG.info("TestWatcher: Test skipped: {} ", description.getDisplayName(), ex);
237         }
238     };
239 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.