Fix mdsal-it-base with JDK9+
[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.ops4j.pax.exam.Configuration;
24 import org.ops4j.pax.exam.MavenUtils;
25 import org.ops4j.pax.exam.Option;
26 import org.ops4j.pax.exam.OptionUtils;
27 import org.ops4j.pax.exam.karaf.container.internal.JavaVersionUtil;
28 import org.ops4j.pax.exam.karaf.options.KarafDistributionOption;
29 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
30 import org.ops4j.pax.exam.options.MavenUrlReference;
31 import org.ops4j.pax.exam.options.extra.VMOption;
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 {
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
50     /*
51      * Default values for karaf distro type, groupId, and artifactId
52      */
53     private static final String KARAF_DISTRO_TYPE = "zip";
54     private static final String KARAF_DISTRO_ARTIFACTID = "opendaylight-karaf-empty";
55     private static final String KARAF_DISTRO_GROUPID = "org.opendaylight.odlparent";
56
57     /*
58      * Property names to override defaults for karaf distro artifactId, groupId,
59      * version, and type
60      */
61     private static final String KARAF_DISTRO_TYPE_PROP = "karaf.distro.type";
62     private static final String KARAF_DISTRO_ARTIFACTID_PROP = "karaf.distro.artifactId";
63     private static final String KARAF_DISTRO_GROUPID_PROP = "karaf.distro.groupId";
64
65     public static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
66
67     @Inject @Filter(timeout = 60000)
68     private BundleContext context;
69
70     public abstract MavenUrlReference getFeatureRepo();
71
72     public abstract String getFeatureName();
73
74     @Before
75     public void setup() throws Exception {
76     }
77
78     public Option getLoggingOption() {
79         Option option = editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
80                 "log4j2.logger.mdsal-it-base.name",
81                 AbstractMdsalTestBase.class.getPackage().getName());
82         option = composite(option, editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
83                 "log4j2.logger.mdsal-it-base.level",
84                 LogLevel.INFO.name()));
85         return option;
86     }
87
88     /**
89      * Override this method to provide more options to config.
90      *
91      * @return An array of additional config options
92      */
93     protected Option[] getAdditionalOptions() {
94         return null;
95     }
96
97     /**
98      * Returns a Log4J logging configuration property name for the given class's package name of the form
99      * "log4j.logger.package_name".
100      *
101      * @deprecated The karaf logging provider is now Log4J2 so logging configurations must conform to the Log4J2 style.
102      *     This method is kept for compilation backwards compatibility but will be removed in a future release.
103      */
104     @Deprecated
105     public String logConfiguration(final 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 type = System.getProperty(KARAF_DISTRO_TYPE_PROP, KARAF_DISTRO_TYPE);
113
114         return maven().groupId(groupId).artifactId(artifactId).versionAsInProject().type(type).getURL();
115     }
116
117     protected Option mvnLocalRepoOption() {
118         String mvnRepoLocal = System.getProperty(MAVEN_REPO_LOCAL, "");
119         LOG.info("mvnLocalRepo \"{}\"", mvnRepoLocal);
120         return editConfigurationFilePut(ETC_ORG_OPS4J_PAX_URL_MVN_CFG, ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY,
121                 mvnRepoLocal);
122     }
123
124     @Configuration
125     public Option[] config() {
126         Option[] options = new Option[] {
127                 when(Boolean.getBoolean(KARAF_DEBUG_PROP))
128                         .useOptions(KarafDistributionOption.debugConfiguration(KARAF_DEBUG_PORT, true)),
129                 karafDistributionConfiguration().frameworkUrl(getKarafDistro())
130                         .unpackDirectory(new File(PAX_EXAM_UNPACK_DIRECTORY)).useDeployFolder(false),
131                 when(Boolean.getBoolean(KEEP_UNPACK_DIRECTORY_PROP)).useOptions(keepRuntimeFolder()),
132                 features(getFeatureRepo(), getFeatureName()),
133                 //mavenBundle("org.apache.aries.quiesce", "org.apache.aries.quiesce.api", "1.0.0"), getLoggingOption(),
134                 mvnLocalRepoOption(),
135                 configureConsole().ignoreLocalConsole().ignoreRemoteShell(),
136                 editConfigurationFilePut(ETC_ORG_OPS4J_PAX_LOGGING_CFG, "log4j2.rootLogger.level", "INFO") };
137
138         if (JavaVersionUtil.getMajorVersion() >= 9) {
139             final String karafVersion = MavenUtils.getArtifactVersion("org.apache.karaf.features",
140                 "org.apache.karaf.features.core");
141             options = OptionUtils.combine(options, new VMOption[] {
142                     new VMOption("--add-reads=java.xml=java.logging"),
143                     new VMOption("--add-exports=java.base/org.apache.karaf.specs.locator=java.xml,ALL-UNNAMED"),
144                     new VMOption("--patch-module"),
145                     new VMOption("java.base=lib/endorsed/org.apache.karaf.specs.locator-" + karafVersion + ".jar"),
146                     new VMOption("--patch-module"),
147                     new VMOption("java.xml=lib/endorsed/org.apache.karaf.specs.java.xml-" + karafVersion + ".jar"),
148                     new VMOption("--add-opens"),
149                     new VMOption("java.base/java.security=ALL-UNNAMED"),
150                     new VMOption("--add-opens"),
151                     new VMOption("java.base/java.net=ALL-UNNAMED"),
152                     new VMOption("--add-opens"),
153                     new VMOption("java.base/java.lang=ALL-UNNAMED"),
154                     new VMOption("--add-opens"),
155                     new VMOption("java.base/java.util=ALL-UNNAMED"),
156                     new VMOption("--add-opens"),
157                     new VMOption("java.naming/javax.naming.spi=ALL-UNNAMED"),
158                     new VMOption("--add-opens"),
159                     new VMOption("java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED"),
160                     new VMOption("--add-exports=java.base/sun.net.www.protocol.http=ALL-UNNAMED"),
161                     new VMOption("--add-exports=java.base/sun.net.www.protocol.https=ALL-UNNAMED"),
162                     new VMOption("--add-exports=java.base/sun.net.www.protocol.jar=ALL-UNNAMED"),
163                     new VMOption("--add-exports=jdk.naming.rmi/com.sun.jndi.url.rmi=ALL-UNNAMED"),
164                     new VMOption("-classpath"),
165                     new VMOption("lib/jdk9plus/*" + File.pathSeparator + "lib/boot/*")
166             });
167         }
168
169         return OptionUtils.combine(options, getAdditionalOptions());
170     }
171 }