d7c42f58a91387704f3f5a63d2f64b0abde08a02
[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.controller";
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     public abstract String getModuleName();
82
83     public abstract String getInstanceName();
84
85     public abstract MavenUrlReference getFeatureRepo();
86
87     public abstract String getFeatureName();
88
89     public Option getLoggingOption() {
90         Option option = editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
91                         logConfiguration(AbstractConfigTestBase.class),
92                         LogLevel.INFO.name());
93         return option;
94     }
95
96     /**
97      * Override this method to provide more options to config
98      *
99      * @return An array of additional config options
100      */
101     protected Option[] getAdditionalOptions() {
102         return null;
103     }
104
105     public String logConfiguration(Class<?> klazz) {
106         return "log4j.logger." + klazz.getPackage().getName();
107     }
108
109     public String getKarafDistro() {
110         String groupId = System.getProperty(KARAF_DISTRO_GROUPID_PROP,KARAF_DISTRO_GROUPID);
111         String artifactId = System.getProperty(KARAF_DISTRO_ARTIFACTID_PROP,KARAF_DISTRO_ARTIFACTID);
112         String version = System.getProperty(KARAF_DISTRO_VERSION_PROP);
113         String type = System.getProperty(KARAF_DISTRO_TYPE_PROP,KARAF_DISTRO_TYPE);
114         if (version == null) {
115             // We use a properties file to retrieve ${karaf.version}, instead of .versionAsInProject()
116             // This avoids forcing all users to depend on Karaf in their POMs
117             Properties abstractConfigTestBaseProps = new Properties();
118             try (InputStream abstractConfigTestBaseInputStream = Thread.currentThread().getContextClassLoader()
119                     .getResourceAsStream(PROPERTIES_FILENAME)) {
120                 abstractConfigTestBaseProps.load(abstractConfigTestBaseInputStream);
121             } catch (IOException e) {
122                 LOG.error("Unable to load {} to determine the Karaf version", PROPERTIES_FILENAME, e);
123             }
124             version = abstractConfigTestBaseProps.getProperty(KARAF_DISTRO_VERSION_PROP);
125         }
126         MavenArtifactUrlReference karafUrl = maven()
127                 .groupId(groupId)
128                 .artifactId(artifactId)
129                 .version(version)
130                 .type(type);
131         return karafUrl.getURL();
132     }
133
134     protected Option mvnLocalRepoOption() {
135         String mvnRepoLocal = System.getProperty(MAVEN_REPO_LOCAL, "");
136         LOG.info("mvnLocalRepo \"{}\"", mvnRepoLocal);
137         return editConfigurationFilePut(ETC_ORG_OPS4J_PAX_URL_MVN_CFG, ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY,
138                 mvnRepoLocal);
139     }
140
141     @Configuration
142     public Option[] config() {
143         Option[] options = new Option[] {
144                 when(Boolean.getBoolean(KARAF_DEBUG_PROP))
145                         .useOptions(KarafDistributionOption.debugConfiguration(KARAF_DEBUG_PORT, true)),
146                 karafDistributionConfiguration().frameworkUrl(getKarafDistro())
147                         .unpackDirectory(new File(PAX_EXAM_UNPACK_DIRECTORY))
148                         .useDeployFolder(false),
149                 when(Boolean.getBoolean(KEEP_UNPACK_DIRECTORY_PROP)).useOptions(keepRuntimeFolder()),
150                 features(getFeatureRepo(), getFeatureName()),
151                 getLoggingOption(),
152                 mvnLocalRepoOption(),
153                 editConfigurationFilePut(ETC_ORG_OPS4J_PAX_LOGGING_CFG, "log4j.rootLogger", "INFO, stdout, osgi:*")};
154         return OptionUtils.combine(options, getAdditionalOptions());
155     }
156
157     @Before
158     public void setup() throws Exception {
159         LOG.info("Module: {} Instance: {} attempting to configure.",
160                 getModuleName(),getInstanceName());
161         Stopwatch stopWatch = Stopwatch.createStarted();
162         ObjectName objectName = null;
163         for(int i = 0;i<MODULE_TIMEOUT_MILLIS;i++) {
164             try {
165                 ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory
166                         .getPlatformMBeanServer());
167                 objectName = configRegistryClient.lookupConfigBean(getModuleName(), getInstanceName());
168                 LOG.info("Module: {} Instance: {} ObjectName: {}.",
169                         getModuleName(),getInstanceName(),objectName);
170                 break;
171             } catch (Exception e) {
172                 if(i<MODULE_TIMEOUT_MILLIS) {
173                     Thread.sleep(1);
174                     continue;
175                 } else {
176                     throw e;
177                 }
178             }
179         }
180         if(objectName != null) {
181             LOG.info("Module: {} Instance: {} configured after {} ms",
182                 getModuleName(),getInstanceName(),
183                 stopWatch.elapsed(TimeUnit.MILLISECONDS));
184         } else {
185             throw new RuntimeException("NOT FOUND Module: " +getModuleName() + " Instance: " + getInstanceName() +
186                     " configured after " + stopWatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
187         }
188     }
189
190     @Rule
191     public TestRule watcher = new TestWatcher() {
192
193         @Override
194         protected void starting(Description description) {
195             LOG.info("TestWatcher: Starting test: {}", description.getDisplayName());
196         }
197
198         @Override
199         protected void finished(Description description) {
200             LOG.info("TestWatcher: Finished test: {}", description.getDisplayName());
201         }
202
203         @Override
204         protected void succeeded(Description description) {
205             LOG.info("TestWatcher: Test succeeded: {}", description.getDisplayName());
206         }
207
208         @Override
209         protected void failed(Throwable ex, Description description) {
210             LOG.info("TestWatcher: Test failed: {}", description.getDisplayName(), ex);
211         }
212
213         @Override
214         protected void skipped(AssumptionViolatedException ex, Description description) {
215             LOG.info("TestWatcher: Test skipped: {} ", description.getDisplayName(), ex);
216         }
217     };
218
219 }

©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.