2 * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.utils.ovsdb.it.utils;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
14 import java.util.List;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
18 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
19 import org.opendaylight.ovsdb.utils.mdsal.utils.NotifyingDataChangeListener;
20 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * This class contains various utility methods used in OVSDB integration tests (IT).
31 public class OvsdbItUtils {
32 private static final Logger LOG = LoggerFactory.getLogger(OvsdbItUtils.class);
33 MdsalUtils mdsalUtils;
34 SouthboundUtils southboundUtils;
35 DataBroker dataBroker;
38 * Create a new OvsdbItUtils instance.
39 * @param dataBroker md-sal data broker
41 public OvsdbItUtils(DataBroker dataBroker) {
42 this.dataBroker = dataBroker;
43 mdsalUtils = new MdsalUtils(dataBroker);
44 southboundUtils = new SouthboundUtils(mdsalUtils);
48 * Get a NodeInfo instance initialized with this ItUtil's DataBroker.
49 * @param connectionInfo ConnectionInfo for the OVSDB server
50 * @param waitList For tracking outstanding md-sal events notifications
51 * @return a new NodeInfo object
53 public NodeInfo createNodeInfo(ConnectionInfo connectionInfo, List<NotifyingDataChangeListener> waitList) {
54 return new NodeInfo(connectionInfo, this, waitList);
58 * Checks whether the OVSDB controller is connected. This method will retry 10 times and will through an
59 * AssertionError for any number of unexpected states.
60 * @param connectionInfo where to connect to
61 * @return true if connected
62 * @throws InterruptedException if interrupted while waiting for connection to appear
64 public boolean isControllerConnected(ConnectionInfo connectionInfo) throws InterruptedException {
65 LOG.info("isControllerConnected enter");
66 ControllerEntry controllerEntry;
67 Node ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
68 assertNotNull("ovsdb node not found", ovsdbNode);
70 String controllerTarget = southboundUtils.getControllersFromOvsdbNode(ovsdbNode).get(0);
71 assertNotNull("Failed to get controller target", controllerTarget);
73 for (int i = 0; i < 10; i++) {
74 LOG.info("isControllerConnected try {}: looking for controller: {}", i, controllerTarget);
75 OvsdbBridgeAugmentation bridge =
76 southboundUtils.getBridge(connectionInfo, "br-int");
77 if (bridge != null && bridge.getControllerEntry() != null) {
78 controllerEntry = bridge.getControllerEntry().iterator().next();
79 assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
80 if (controllerEntry.isIsConnected()) {
81 LOG.info("isControllerConnected exit: true {}", controllerTarget);
87 LOG.info("isControllerConnected exit: false {}", controllerTarget);
92 * Get a DataBroker and assert that it is not null.
93 * @param providerContext ProviderContext from which to retrieve the DataBroker
94 * @return the Databroker
96 public static DataBroker getDatabroker(BindingAwareBroker.ProviderContext providerContext) {
97 DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
98 assertNotNull("dataBroker should not be null", dataBroker);