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