Infra for running ovs dockers
[ovsdb.git] / utils / ovsdb-it-utils / src / main / java / org / opendaylight / ovsdb / utils / ovsdb / it / utils / OvsdbItUtils.java
1 /*
2  * Copyright (c) 2016 Red Hat, 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.ovsdb.utils.ovsdb.it.utils;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import java.util.List;
15
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;
27
28 /**
29  * This class contains various utility methods used in OVSDB integration tests (IT).
30  */
31 public class OvsdbItUtils {
32     private static final Logger LOG = LoggerFactory.getLogger(OvsdbItUtils.class);
33     MdsalUtils mdsalUtils;
34     SouthboundUtils southboundUtils;
35     DataBroker dataBroker;
36
37     /**
38      * Create a new OvsdbItUtils instance.
39      * @param dataBroker  md-sal data broker
40      */
41     public OvsdbItUtils(DataBroker dataBroker) {
42         this.dataBroker = dataBroker;
43         mdsalUtils = new MdsalUtils(dataBroker);
44         southboundUtils = new SouthboundUtils(mdsalUtils);
45     }
46
47     /**
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
52      */
53     public NodeInfo createNodeInfo(ConnectionInfo connectionInfo, List<NotifyingDataChangeListener> waitList) {
54         return new NodeInfo(connectionInfo, this, waitList);
55     }
56
57     /**
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
63      */
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);
69
70         String controllerTarget = southboundUtils.getControllersFromOvsdbNode(ovsdbNode).get(0);
71         assertNotNull("Failed to get controller target", controllerTarget);
72
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);
82                     return true;
83                 }
84             }
85             Thread.sleep(1000);
86         }
87         LOG.info("isControllerConnected exit: false {}", controllerTarget);
88         return false;
89     }
90
91     /**
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
95      */
96     public static DataBroker getDatabroker(BindingAwareBroker.ProviderContext providerContext) {
97         DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
98         assertNotNull("dataBroker should not be null", dataBroker);
99         return dataBroker;
100     }
101
102 }