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