/*
 * Copyright (c) 2016 Red Hat, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.ovsdb.utils.ovsdb.it.utils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.List;

import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
import org.opendaylight.ovsdb.utils.mdsal.utils.NotifyingDataChangeListener;
import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class contains various utility methods used in OVSDB integration tests (IT).
 */
public class OvsdbItUtils {
    private static final Logger LOG = LoggerFactory.getLogger(OvsdbItUtils.class);
    MdsalUtils mdsalUtils;
    SouthboundUtils southboundUtils;
    DataBroker dataBroker;

    /**
     * Create a new OvsdbItUtils instance.
     * @param dataBroker  md-sal data broker
     */
    public OvsdbItUtils(DataBroker dataBroker) {
        this.dataBroker = dataBroker;
        mdsalUtils = new MdsalUtils(dataBroker);
        southboundUtils = new SouthboundUtils(mdsalUtils);
    }

    /**
     * Get a NodeInfo instance initialized with this ItUtil's DataBroker.
     * @param connectionInfo ConnectionInfo for the OVSDB server
     * @param waitList For tracking outstanding md-sal events notifications
     * @return a new NodeInfo object
     */
    public NodeInfo createNodeInfo(ConnectionInfo connectionInfo, List<NotifyingDataChangeListener> waitList) {
        return new NodeInfo(connectionInfo, this, waitList);
    }

    /**
     * Checks whether the OVSDB controller is connected. This method will retry 10 times and will through an
     * AssertionError for any number of unexpected states.
     * @param connectionInfo where to connect to
     * @return true if connected
     * @throws InterruptedException if interrupted while waiting for connection to appear
     */
    public boolean isControllerConnected(ConnectionInfo connectionInfo) throws InterruptedException {
        LOG.info("isControllerConnected enter");
        ControllerEntry controllerEntry;
        Node ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
        assertNotNull("ovsdb node not found", ovsdbNode);

        String controllerTarget = southboundUtils.getControllersFromOvsdbNode(ovsdbNode).get(0);
        assertNotNull("Failed to get controller target", controllerTarget);

        for (int i = 0; i < 10; i++) {
            LOG.info("isControllerConnected try {}: looking for controller: {}", i, controllerTarget);
            OvsdbBridgeAugmentation bridge =
                    southboundUtils.getBridge(connectionInfo, "br-int");
            if (bridge != null && bridge.getControllerEntry() != null) {
                controllerEntry = bridge.getControllerEntry().iterator().next();
                assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
                if (controllerEntry.isIsConnected()) {
                    LOG.info("isControllerConnected exit: true {}", controllerTarget);
                    return true;
                }
            }
            Thread.sleep(1000);
        }
        LOG.info("isControllerConnected exit: false {}", controllerTarget);
        return false;
    }

    /**
     * Get a DataBroker and assert that it is not null.
     * @param providerContext ProviderContext from which to retrieve the DataBroker
     * @return the Databroker
     */
    public static DataBroker getDatabroker(BindingAwareBroker.ProviderContext providerContext) {
        DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
        assertNotNull("dataBroker should not be null", dataBroker);
        return dataBroker;
    }

}

@@ -536,79 +536,8 @@
 er;%0A
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;%0A
 impo
@@ -4048,473 +4048,6 @@
   %7D%0A
-%0A    /**%0A     * Get a DataBroker and assert that it is not null.%0A     * @param providerContext ProviderContext from which to retrieve the DataBroker%0A     * @return the Databroker%0A     */%0A    public static DataBroker getDatabroker(BindingAwareBroker.ProviderContext providerContext) %7B%0A        DataBroker dataBroker = providerContext.getSALService(DataBroker.class);%0A        assertNotNull(%22dataBroker should not be null%22, dataBroker);%0A        return dataBroker;%0A    %7D%0A%0A
 %7D%0A

Remove unused references to BindingAwareBroker