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