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