Remove plugin dependencies
[ovsdb.git] / openstack / netvirt-it / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / it / 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 package org.opendaylight.ovsdb.openstack.netvirt.it;
9
10 import static org.ops4j.pax.exam.CoreOptions.maven;
11 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
12 import static org.ops4j.pax.exam.CoreOptions.systemTimeout;
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.collect.ObjectArrays;
19
20 import java.io.File;
21 import java.lang.management.ManagementFactory;
22 import java.util.Calendar;
23
24 import javax.management.InstanceNotFoundException;
25
26 import org.junit.Rule;
27 import org.junit.rules.TestRule;
28 import org.junit.rules.TestWatcher;
29 import org.junit.runner.Description;
30 import org.opendaylight.controller.config.api.ConfigRegistry;
31 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
32 import org.ops4j.pax.exam.Configuration;
33 import org.ops4j.pax.exam.Option;
34 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
35 import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
36 import org.ops4j.pax.exam.options.MavenUrlReference;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public abstract class AbstractConfigTestBase {
41
42     private static final Logger LOG = LoggerFactory.getLogger(AbstractConfigTestBase.class);
43     public static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
44     public static final String CUSTOM_PROPERTIES = "etc/custom.properties";
45     private static final String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
46     private static final String SERVER_PORT = "ovsdbserver.port";
47     private static final String CONNECTION_TYPE = "ovsdbserver.connection";
48     private static final String CONNECTION_TYPE_ACTIVE = "active";
49     private static final String CONNECTION_TYPE_PASSIVE = "passive";
50     private static final String DEFAULT_SERVER_IPADDRESS = "127.0.0.1";
51     private static final String DEFAULT_SERVER_PORT = "6640";
52
53     /*
54      * Wait up to 10s for our configured module to come up
55      */
56     private static final int MODULE_TIMEOUT = 30000;
57
58     public abstract String getModuleName();
59
60     public abstract String getInstanceName();
61
62     public abstract MavenUrlReference getFeatureRepo();
63
64     public abstract String getFeatureName();
65
66     public Option[] getLoggingOptions() {
67         Option[] options = new Option[] {
68                 editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
69                         logConfiguration(AbstractConfigTestBase.class),
70                         LogLevel.INFO.name()),
71                 editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
72                         "log4j.logger.org.opendaylight.ovsdb.southbound-impl",
73                         LogLevel.DEBUG.name())
74         };
75         return options;
76     }
77
78     public String logConfiguration(Class<?> klazz) {
79         return "log4j.logger." + klazz.getPackage().getName();
80     }
81
82     public Option[] getPropertiesOptions() {
83         return null;
84     }
85
86     public MavenArtifactUrlReference getKarafDistro() {
87         MavenArtifactUrlReference karafUrl = maven()
88                 .groupId("org.opendaylight.controller")
89                 .artifactId("opendaylight-karaf-empty")
90                 .version("1.5.0-SNAPSHOT")
91                 .type("zip");
92         return karafUrl;
93     }
94
95     @Configuration
96     public Option[] config() {
97         Option[] options = new Option[] {
98                 // KarafDistributionOption.debugConfiguration("5005", true),
99                 karafDistributionConfiguration()
100                         .frameworkUrl(getKarafDistro())
101                         .unpackDirectory(new File("target/exam"))
102                         .useDeployFolder(false),
103                 keepRuntimeFolder(),
104                 systemTimeout(240000),
105                 features(getFeatureRepo() , getFeatureName()),
106                 mavenBundle("org.opendaylight.ovsdb", "openstack.net-virt").versionAsInProject()
107         };
108         options = ObjectArrays.concat(options, getLoggingOptions(), Option.class);
109         options = ObjectArrays.concat(options, getPropertiesOptions(), Option.class);
110         return options;
111     }
112
113     public void setup() throws Exception {
114         LOG.info("Module: {} Instance: {} attempting to configure.",
115                 getModuleName(),getInstanceName());
116         Calendar start = Calendar.getInstance();
117         ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory
118                 .getPlatformMBeanServer());
119         for (int timer = 0;timer < MODULE_TIMEOUT;timer++) {
120             try {
121                 configRegistryClient.lookupConfigBean(getModuleName(), getInstanceName());
122                 Thread.sleep(1);
123             } catch (InstanceNotFoundException e) {
124                 if (timer < MODULE_TIMEOUT) {
125                     continue;
126                 } else {
127                     throw e;
128                 }
129             } catch (InterruptedException e) {
130                 LOG.error("Exception: ",e);
131             }
132         }
133         Calendar stop = Calendar.getInstance();
134         LOG.info("Module: {} Instance: {} configured after {} ms",
135                 getModuleName(),getInstanceName(),
136                 stop.getTimeInMillis() - start.getTimeInMillis());
137     }
138
139     @Rule
140     public TestRule watcher = new TestWatcher() {
141         @Override
142         protected void starting(Description description) {
143             LOG.info("TestWatcher: Starting test: {}",
144                     description.getDisplayName());
145         }
146
147         @Override
148         protected void finished(Description description) {
149             LOG.info("TestWatcher: Finished test: {}", description.getDisplayName());
150         }
151     };
152 }