Base projects and parents for IT tests.
[controller.git] / opendaylight / md-sal / mdsal-it-base / src / main / java / org / opendaylight / controller / mdsal / it / base / AbstractMdsalTestBase.java
1 package org.opendaylight.controller.mdsal.it.base;
2
3 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
4 import static org.ops4j.pax.exam.CoreOptions.composite;
5
6 import java.util.Calendar;
7
8 import javax.inject.Inject;
9
10 import org.junit.Before;
11 import org.opendaylight.controller.config.it.base.AbstractConfigTestBase;
12 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
15 import org.ops4j.pax.exam.Option;
16 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
17 import org.ops4j.pax.exam.util.Filter;
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.ServiceReference;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public abstract class AbstractMdsalTestBase extends AbstractConfigTestBase implements BindingAwareProvider {
24
25     private static final Logger LOG = LoggerFactory.getLogger(AbstractMdsalTestBase.class);
26     private static final int REGISTRATION_TIMEOUT = 70000;
27     @Inject @Filter(timeout=60000)
28     private BundleContext context;
29     private ProviderContext session = null;
30
31     public ProviderContext getSession() {
32         return session;
33     }
34
35     @Override
36     public void onSessionInitiated(ProviderContext session) {
37         LOG.info("Session Initiated: {}",session);
38         this.session = session;
39     }
40
41     @Override
42     @Before
43     public void setup() throws Exception {
44         super.setup();
45         Calendar start = Calendar.getInstance();
46         ServiceReference<BindingAwareBroker> serviceReference = context.getServiceReference(BindingAwareBroker.class);
47         if(serviceReference == null) {
48             throw new RuntimeException("BindingAwareBroker not found");
49         }
50         BindingAwareBroker broker = context.getService(serviceReference);
51         broker.registerProvider(this);
52         for(int i=0;i<REGISTRATION_TIMEOUT;i++) {
53             if(session !=null) {
54                 Calendar stop = Calendar.getInstance();
55                 LOG.info("Registered session {} with the MD-SAL after {} ms",
56                         session,
57                         stop.getTimeInMillis() - start.getTimeInMillis());
58                 return;
59             } else {
60                 Thread.sleep(1);
61             }
62         }
63         throw new RuntimeException("Session not initiated after " + REGISTRATION_TIMEOUT + " ms");
64     }
65
66     @Override
67     public Option getLoggingOption() {
68         Option option = editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
69                         logConfiguration(AbstractMdsalTestBase.class),
70                         LogLevel.INFO.name());
71         option = composite(option, super.getLoggingOption());
72         return option;
73     }
74
75 }

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