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