Update MRI projects for Aluminium
[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 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.ovsdb.utils.mdsal.utils.ControllerMdsalUtils;
17 import org.opendaylight.ovsdb.utils.mdsal.utils.ControllerNotifyingDataChangeListener;
18 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * This class contains various utility methods used in OVSDB integration tests (IT).
28  */
29 public class OvsdbItUtils {
30     private static final Logger LOG = LoggerFactory.getLogger(OvsdbItUtils.class);
31     ControllerMdsalUtils mdsalUtils;
32     SouthboundUtils southboundUtils;
33     DataBroker dataBroker;
34
35     /**
36      * Create a new OvsdbItUtils instance.
37      * @param dataBroker  md-sal data broker
38      */
39     public OvsdbItUtils(DataBroker dataBroker) {
40         this.dataBroker = dataBroker;
41         mdsalUtils = new ControllerMdsalUtils(dataBroker);
42         southboundUtils = new SouthboundUtils(mdsalUtils);
43     }
44
45     /**
46      * Get a NodeInfo instance initialized with this ItUtil's DataBroker.
47      * @param connectionInfo ConnectionInfo for the OVSDB server
48      * @param waitList For tracking outstanding md-sal events notifications
49      * @return a new NodeInfo object
50      */
51     public NodeInfo createNodeInfo(ConnectionInfo connectionInfo,
52             List<ControllerNotifyingDataChangeListener> waitList) {
53         return new NodeInfo(connectionInfo, this, waitList);
54     }
55
56     /**
57      * Checks whether the OVSDB controller is connected. This method will retry 10 times and will through an
58      * AssertionError for any number of unexpected states.
59      * @param connectionInfo where to connect to
60      * @return true if connected
61      * @throws InterruptedException if interrupted while waiting for connection to appear
62      */
63     public boolean isControllerConnected(ConnectionInfo connectionInfo) throws InterruptedException {
64         LOG.info("isControllerConnected enter");
65         ControllerEntry controllerEntry;
66         Node ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
67         assertNotNull("ovsdb node not found", ovsdbNode);
68
69         String controllerTarget = southboundUtils.getControllersFromOvsdbNode(ovsdbNode).get(0);
70         assertNotNull("Failed to get controller target", controllerTarget);
71
72         for (int i = 0; i < 10; i++) {
73             LOG.info("isControllerConnected try {}: looking for controller: {}", i, controllerTarget);
74             OvsdbBridgeAugmentation bridge =
75                     southboundUtils.getBridge(connectionInfo, "br-int");
76             if (bridge != null && bridge.getControllerEntry() != null) {
77                 controllerEntry = bridge.getControllerEntry().values().iterator().next();
78                 assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
79                 if (controllerEntry.isIsConnected()) {
80                     LOG.info("isControllerConnected exit: true {}", controllerTarget);
81                     return true;
82                 }
83             }
84             Thread.sleep(1000);
85         }
86         LOG.info("isControllerConnected exit: false {}", controllerTarget);
87         return false;
88     }
89 }