d553fde041ef944a1f1dbfea5294d09b65904caa
[controller.git] / opendaylight / md-sal / mdsal-it-base / src / main / java / org / opendaylight / controller / mdsal / it / base / AbstractMdsalTestBase.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.mdsal.it.base;
10
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;
19
20 import java.io.File;
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;
36
37 public abstract class AbstractMdsalTestBase implements BindingAwareProvider {
38
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";
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 int REGISTRATION_TIMEOUT = 70000;
50
51     /*
52      * Default values for karaf distro type, groupId, and artifactId
53      */
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";
57
58     /*
59      * Property names to override defaults for karaf distro artifactId, groupId,
60      * version, and type
61      */
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";
66
67     public static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
68
69     @Inject @Filter(timeout = 60000)
70     private BundleContext context;
71     @Inject @Filter(timeout = 60000)
72     private BindingAwareBroker broker;
73     private ProviderContext session = null;
74
75     public ProviderContext getSession() {
76         return session;
77     }
78
79     public abstract MavenUrlReference getFeatureRepo();
80
81     public abstract String getFeatureName();
82
83     @Override
84     public void onSessionInitiated(ProviderContext session) {
85         LOG.info("Session Initiated: {}",session);
86         this.session = session;
87     }
88
89     @Before
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",
97                         session,
98                         stop - start);
99                 return;
100             } else {
101                 Thread.sleep(1);
102             }
103         }
104         throw new RuntimeException("Session not initiated after " + REGISTRATION_TIMEOUT + " ms");
105     }
106
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()));
114         return option;
115     }
116
117     /**
118      * Override this method to provide more options to config.
119      *
120      * @return An array of additional config options
121      */
122     protected Option[] getAdditionalOptions() {
123         return null;
124     }
125
126     /**
127      * Returns a Log4J logging configuration property name for the given class's package name of the form
128      * "log4j.logger.package_name".
129      *
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.
132      */
133     @Deprecated
134     public String logConfiguration(final Class<?> klazz) {
135         return "log4j.logger." + klazz.getPackage().getName();
136     }
137
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);
142
143         return maven().groupId(groupId).artifactId(artifactId).versionAsInProject().type(type).getURL();
144     }
145
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,
150                 mvnRepoLocal);
151     }
152
153     @Configuration
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());
167     }
168 }

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