Infra for running ovs dockers
[ovsdb.git] / utils / ovsdb-it-utils / src / main / java / org / opendaylight / ovsdb / utils / ovsdb / it / utils / NodeInfo.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.assertNotEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15
16 import java.util.List;
17
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
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.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.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Utility class for connections to an OVSDB node. Contains various info for the node
29  * as public data members.
30  */
31 public class NodeInfo {
32     private static final Logger LOG = LoggerFactory.getLogger(NodeInfo.class);
33     public static final String INTEGRATION_BRIDGE_NAME = "br-int";
34
35     private ConnectionInfo connectionInfo;
36     private InstanceIdentifier<Node> ovsdbIid;
37     InstanceIdentifier<Node> bridgeIid;
38     public long datapathId;
39     public Node ovsdbNode;
40     public Node bridgeNode;
41     NotifyingDataChangeListener ovsdbWaiter;
42     NotifyingDataChangeListener bridgeWaiter;
43     List<NotifyingDataChangeListener> waitList;
44     OvsdbItUtils itUtils;
45
46     /**
47      * Create a new NodeInfo object.
48      * @param connectionInfo of the OVSDB node
49      * @param itUtils OvsdbItUtils instance
50      * @param waitList for tracking outstanding md-sal events
51      */
52     NodeInfo(ConnectionInfo connectionInfo, OvsdbItUtils itUtils, List<NotifyingDataChangeListener> waitList) {
53         this.connectionInfo = connectionInfo;
54         this.itUtils = itUtils;
55         this.waitList = waitList;
56         ovsdbIid = SouthboundUtils.createInstanceIdentifier(connectionInfo);
57         bridgeIid = SouthboundUtils.createInstanceIdentifier(connectionInfo, INTEGRATION_BRIDGE_NAME);
58     }
59
60     /**
61      * Connect to the OVSDB node, wait for the connection to be established and for the integration bridge
62      * to be successfully created. Contains assertions for unexpected states
63      * @throws InterruptedException if interrupted while waiting for connection
64      */
65     public void connect() throws InterruptedException {
66         ovsdbWaiter = new NotifyingDataChangeListener(LogicalDatastoreType.OPERATIONAL, ovsdbIid, waitList);
67         ovsdbWaiter.registerDataChangeListener(itUtils.dataBroker);
68         bridgeWaiter = new NotifyingDataChangeListener(LogicalDatastoreType.OPERATIONAL, bridgeIid, waitList);
69         bridgeWaiter.registerDataChangeListener(itUtils.dataBroker);
70
71         assertNotNull("connection failed", itUtils.southboundUtils.addOvsdbNode(connectionInfo, 0));
72
73         ovsdbWaiter.waitForCreation();
74         ovsdbNode = itUtils.southboundUtils.getOvsdbNode(connectionInfo);
75         assertNotNull("node is not connected", ovsdbNode);
76
77         bridgeWaiter.waitForCreation();
78         assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
79                 + " is not connected", itUtils.isControllerConnected(connectionInfo));
80
81         bridgeNode = itUtils.southboundUtils.getBridgeNode(ovsdbNode, INTEGRATION_BRIDGE_NAME);
82         assertNotNull("bridge " + INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
83         datapathId = itUtils.southboundUtils.getDataPathId(bridgeNode);
84         String datapathIdString = itUtils.southboundUtils.getDatapathId(bridgeNode);
85         LOG.info("NodeInfo.connect: bridgeNode: {}, datapathId: {} - {}", bridgeNode, datapathIdString, datapathId);
86         assertNotEquals("datapathId was not found", datapathId, 0);
87     }
88
89     /**
90      * Remove integration bridge and teardown connection. Contains assertions for unexpected states.
91      * @throws InterruptedException if interrupted while waiting for disconnect to complete
92      */
93     public void disconnect() throws InterruptedException {
94         assertTrue(itUtils.southboundUtils.deleteBridge(connectionInfo, INTEGRATION_BRIDGE_NAME, 0));
95         bridgeWaiter.waitForDeletion();
96         Node bridgeNode = itUtils.mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeIid);
97         assertNull("Bridge should not be found", bridgeNode);
98         assertTrue(itUtils.southboundUtils.disconnectOvsdbNode(connectionInfo, 0));
99         ovsdbWaiter.waitForDeletion();
100         Node ovsdbNode = itUtils.mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, ovsdbIid);
101         assertNull("Ovsdb node should not be found", ovsdbNode);
102     }
103
104 }