af47668193a8ee5cae56badbef9e628a9c706d9a
[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
18 import com.google.common.base.Stopwatch;
19
20 import java.io.File;
21 import java.lang.management.ManagementFactory;
22 import java.util.concurrent.TimeUnit;
23
24 import javax.management.ObjectName;
25
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.internal.AssumptionViolatedException;
29 import org.junit.rules.TestRule;
30 import org.junit.rules.TestWatcher;
31 import org.junit.runner.Description;
32 import org.opendaylight.controller.config.api.ConfigRegistry;
33 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
34 import org.ops4j.pax.exam.Configuration;
35 import org.ops4j.pax.exam.Option;
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
45     private static final String PAX_EXAM_UNPACK_DIRECTORY = "target/exam";
46     private static final String KARAF_DEBUG_PORT = "5005";
47     private static final String KARAF_DEBUG_PROP = "karaf.debug";
48     private static final String KEEP_UNPACK_DIRECTORY_PROP = "karaf.keep.unpack";
49     private static final Logger LOG = LoggerFactory.getLogger(AbstractConfigTestBase.class);
50     public static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
51
52     /*
53      * Default values for karaf distro version, type, groupId, and artifactId
54      */
55     private static final String KARAF_DISTRO_VERSION = "3.0.4";
56     private static final String KARAF_DISTRO_TYPE = "zip";
57     private static final String KARAF_DISTRO_ARTIFACTID = "apache-karaf";
58     private static final String KARAF_DISTRO_GROUPID = "org.apache.karaf";
59
60     /*
61      * Property names to override defaults for karaf distro artifactId, groupId, version, and type
62      */
63     private static final String KARAF_DISTRO_VERSION_PROP = "karaf.distro.version";
64     private static final String KARAF_DISTRO_TYPE_PROP = "karaf.distro.type";
65     private static final String KARAF_DISTRO_ARTIFACTID_PROP = "karaf.distro.artifactId";
66     private static final String KARAF_DISTRO_GROUPID_PROP = "karaf.distro.groupId";
67
68     /*
69      * Wait up to 10s for our configured module to come up
70      */
71     private static final int MODULE_TIMEOUT_MILLIS = 60000;
72
73     public abstract String getModuleName();
74
75     public abstract String getInstanceName();
76
77     public abstract MavenUrlReference getFeatureRepo();
78
79     public abstract String getFeatureName();
80
81     public Option getLoggingOption() {
82         Option option = editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
83                         logConfiguration(AbstractConfigTestBase.class),
84                         LogLevel.INFO.name());
85         return option;
86     }
87
88     public String logConfiguration(Class<?> klazz) {
89         return "log4j.logger." + klazz.getPackage().getName();
90     }
91
92     public String getKarafDistro() {
93         String groupId = System.getProperty(KARAF_DISTRO_GROUPID_PROP,KARAF_DISTRO_GROUPID);
94         String artifactId = System.getProperty(KARAF_DISTRO_ARTIFACTID_PROP,KARAF_DISTRO_ARTIFACTID);
95         String version = System.getProperty(KARAF_DISTRO_VERSION_PROP,KARAF_DISTRO_VERSION);
96         String type = System.getProperty(KARAF_DISTRO_TYPE_PROP,KARAF_DISTRO_TYPE);
97         MavenArtifactUrlReference karafUrl = maven()
98                 .groupId(groupId)
99                 .artifactId(artifactId)
100                 .version(version)
101                 .type(type);
102         return karafUrl.getURL();
103     }
104
105     @Configuration
106     public Option[] config() {
107         Option[] options = new Option[] {
108                 when(Boolean.getBoolean(KARAF_DEBUG_PROP))
109                         .useOptions(KarafDistributionOption.debugConfiguration(KARAF_DEBUG_PORT, true)),
110                 karafDistributionConfiguration().frameworkUrl(getKarafDistro())
111                         .unpackDirectory(new File(PAX_EXAM_UNPACK_DIRECTORY))
112                         .useDeployFolder(false),
113                 when(Boolean.getBoolean(KEEP_UNPACK_DIRECTORY_PROP)).useOptions(keepRuntimeFolder()),
114                 features(getFeatureRepo(), getFeatureName()),
115                 getLoggingOption()};
116         return options;
117     }
118
119     @Before
120     public void setup() throws Exception {
121         LOG.info("Module: {} Instance: {} attempting to configure.",
122                 getModuleName(),getInstanceName());
123         Stopwatch stopWatch = Stopwatch.createStarted();
124         ObjectName objectName = null;
125         for(int i = 0;i<MODULE_TIMEOUT_MILLIS;i++) {
126             try {
127                 ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory
128                         .getPlatformMBeanServer());
129                 objectName = configRegistryClient.lookupConfigBean(getModuleName(), getInstanceName());
130                 LOG.info("Module: {} Instance: {} ObjectName: {}.",
131                         getModuleName(),getInstanceName(),objectName);
132                 break;
133             } catch (Exception e) {
134                 if(i<MODULE_TIMEOUT_MILLIS) {
135                     Thread.sleep(1);
136                     continue;
137                 } else {
138                     throw e;
139                 }
140             }
141         }
142         if(objectName != null) {
143             LOG.info("Module: {} Instance: {} configured after {} ms",
144                 getModuleName(),getInstanceName(),
145                 stopWatch.elapsed(TimeUnit.MILLISECONDS));
146         } else {
147             throw new RuntimeException("NOT FOUND Module: " +getModuleName() + " Instance: " + getInstanceName() +
148                     " configured after " + stopWatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
149         }
150     }
151
152     @Rule
153     public TestRule watcher = new TestWatcher() {
154
155         @Override
156         protected void starting(Description description) {
157             LOG.info("TestWatcher: Starting test: {}", description.getDisplayName());
158         }
159
160         @Override
161         protected void finished(Description description) {
162             LOG.info("TestWatcher: Finished test: {}", description.getDisplayName());
163         }
164
165         @Override
166         protected void succeeded(Description description) {
167             LOG.info("TestWatcher: Test succeeded: {}", description.getDisplayName());
168         }
169
170         @Override
171         protected void failed(Throwable ex, Description description) {
172             LOG.info("TestWatcher: Test failed: {}", description.getDisplayName(), ex);
173         }
174
175         @Override
176         protected void skipped(AssumptionViolatedException ex, Description description) {
177             LOG.info("TestWatcher: Test skipped: {} ", description.getDisplayName(), ex);
178         }
179     };
180
181 }

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