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

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