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