2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.mdsal.it.base;
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.when;
14 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
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;
21 import javax.inject.Inject;
22 import org.junit.Before;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
26 import org.ops4j.pax.exam.Configuration;
27 import org.ops4j.pax.exam.Option;
28 import org.ops4j.pax.exam.OptionUtils;
29 import org.ops4j.pax.exam.karaf.options.KarafDistributionOption;
30 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
31 import org.ops4j.pax.exam.options.MavenUrlReference;
32 import org.ops4j.pax.exam.util.Filter;
33 import org.osgi.framework.BundleContext;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public abstract class AbstractMdsalTestBase implements BindingAwareProvider {
39 private static final Logger LOG = LoggerFactory.getLogger(AbstractMdsalTestBase.class);
40 private static final String MAVEN_REPO_LOCAL = "maven.repo.local";
41 private static final String ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY = "org.ops4j.pax.url.mvn.localRepository";
42 private static final String ETC_ORG_OPS4J_PAX_URL_MVN_CFG = "etc/org.ops4j.pax.url.mvn.cfg";
43 private static final String ETC_ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
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 int REGISTRATION_TIMEOUT = 70000;
52 * Default values for karaf distro type, groupId, and artifactId
54 private static final String KARAF_DISTRO_TYPE = "zip";
55 private static final String KARAF_DISTRO_ARTIFACTID = "opendaylight-karaf-empty";
56 private static final String KARAF_DISTRO_GROUPID = "org.opendaylight.odlparent";
59 * Property names to override defaults for karaf distro artifactId, groupId,
62 private static final String KARAF_DISTRO_VERSION_PROP = "karaf.distro.version";
63 private static final String KARAF_DISTRO_TYPE_PROP = "karaf.distro.type";
64 private static final String KARAF_DISTRO_ARTIFACTID_PROP = "karaf.distro.artifactId";
65 private static final String KARAF_DISTRO_GROUPID_PROP = "karaf.distro.groupId";
67 public static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
69 @Inject @Filter(timeout = 60000)
70 private BundleContext context;
71 @Inject @Filter(timeout = 60000)
72 private BindingAwareBroker broker;
73 private ProviderContext session = null;
75 public ProviderContext getSession() {
79 public abstract MavenUrlReference getFeatureRepo();
81 public abstract String getFeatureName();
84 public void onSessionInitiated(ProviderContext session) {
85 LOG.info("Session Initiated: {}",session);
86 this.session = session;
90 public void setup() throws Exception {
91 long start = System.nanoTime();
92 broker.registerProvider(this);
93 for (int i = 0; i < REGISTRATION_TIMEOUT; i++) {
94 if (session != null) {
95 long stop = System.nanoTime();
96 LOG.info("Registered session {} with the MD-SAL after {} ns",
104 throw new RuntimeException("Session not initiated after " + REGISTRATION_TIMEOUT + " ms");
107 public Option getLoggingOption() {
108 Option option = editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
109 "log4j2.logger.mdsal-it-base.name",
110 AbstractMdsalTestBase.class.getPackage().getName());
111 option = composite(option, editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
112 "log4j2.logger.mdsal-it-base.level",
113 LogLevel.INFO.name()));
118 * Override this method to provide more options to config.
120 * @return An array of additional config options
122 protected Option[] getAdditionalOptions() {
127 * Returns a Log4J logging configuration property name for the given class's package name of the form
128 * "log4j.logger.package_name".
130 * @deprecated The karaf logging provider is now Log4J2 so logging configurations must conform to the Log4J2 style.
131 * This method is kept for compilation backwards compatibility but will be removed in a future release.
134 public String logConfiguration(final Class<?> klazz) {
135 return "log4j.logger." + klazz.getPackage().getName();
138 public String getKarafDistro() {
139 String groupId = System.getProperty(KARAF_DISTRO_GROUPID_PROP, KARAF_DISTRO_GROUPID);
140 String artifactId = System.getProperty(KARAF_DISTRO_ARTIFACTID_PROP, KARAF_DISTRO_ARTIFACTID);
141 String type = System.getProperty(KARAF_DISTRO_TYPE_PROP, KARAF_DISTRO_TYPE);
143 return maven().groupId(groupId).artifactId(artifactId).versionAsInProject().type(type).getURL();
146 protected Option mvnLocalRepoOption() {
147 String mvnRepoLocal = System.getProperty(MAVEN_REPO_LOCAL, "");
148 LOG.info("mvnLocalRepo \"{}\"", mvnRepoLocal);
149 return editConfigurationFilePut(ETC_ORG_OPS4J_PAX_URL_MVN_CFG, ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY,
154 public Option[] config() {
155 Option[] options = new Option[] {
156 when(Boolean.getBoolean(KARAF_DEBUG_PROP))
157 .useOptions(KarafDistributionOption.debugConfiguration(KARAF_DEBUG_PORT, true)),
158 karafDistributionConfiguration().frameworkUrl(getKarafDistro())
159 .unpackDirectory(new File(PAX_EXAM_UNPACK_DIRECTORY)).useDeployFolder(false),
160 when(Boolean.getBoolean(KEEP_UNPACK_DIRECTORY_PROP)).useOptions(keepRuntimeFolder()),
161 features(getFeatureRepo(), getFeatureName()),
162 //mavenBundle("org.apache.aries.quiesce", "org.apache.aries.quiesce.api", "1.0.0"), getLoggingOption(),
163 mvnLocalRepoOption(),
164 configureConsole().ignoreLocalConsole().ignoreRemoteShell(),
165 editConfigurationFilePut(ETC_ORG_OPS4J_PAX_LOGGING_CFG, "log4j2.rootLogger.level", "INFO") };
166 return OptionUtils.combine(options, getAdditionalOptions());