Bump upstreams
[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 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.ovsdb.utils.mdsal.utils.NotifyingDataChangeListener;
19 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Utility class for connections to an OVSDB node. Contains various info for the node
28  * as public data members.
29  */
30 public class NodeInfo {
31     private static final Logger LOG = LoggerFactory.getLogger(NodeInfo.class);
32     public static final String INTEGRATION_BRIDGE_NAME = "br-int";
33
34     private final ConnectionInfo connectionInfo;
35     private final InstanceIdentifier<Node> ovsdbIid;
36     private final InstanceIdentifier<Node> bridgeIid;
37     private final List<NotifyingDataChangeListener> waitList;
38     private final OvsdbItUtils itUtils;
39
40     private long datapathId;
41     private Node ovsdbNode;
42     private Node bridgeNode;
43     private NotifyingDataChangeListener ovsdbWaiter;
44     private NotifyingDataChangeListener bridgeWaiter;
45
46
47     /**
48      * Create a new NodeInfo object.
49      * @param connectionInfo of the OVSDB node
50      * @param itUtils OvsdbItUtils instance
51      * @param waitList for tracking outstanding md-sal events
52      */
53     NodeInfo(final ConnectionInfo connectionInfo, final OvsdbItUtils itUtils,
54              final List<NotifyingDataChangeListener> waitList) {
55         this.connectionInfo = connectionInfo;
56         this.itUtils = itUtils;
57         this.waitList = waitList;
58         ovsdbIid = SouthboundUtils.createInstanceIdentifier(connectionInfo);
59         bridgeIid = SouthboundUtils.createInstanceIdentifier(connectionInfo, INTEGRATION_BRIDGE_NAME);
60     }
61
62     private void addWaiters() {
63         ovsdbWaiter = new NotifyingDataChangeListener(LogicalDatastoreType.OPERATIONAL,
64             NotifyingDataChangeListener.BIT_CREATE, ovsdbIid, waitList);
65         ovsdbWaiter.registerDataChangeListener(itUtils.dataBroker);
66         bridgeWaiter = new NotifyingDataChangeListener(LogicalDatastoreType.OPERATIONAL,
67             NotifyingDataChangeListener.BIT_CREATE, bridgeIid, waitList);
68         bridgeWaiter.registerDataChangeListener(itUtils.dataBroker);
69     }
70
71     private void closeWaiters() throws Exception {
72         ovsdbWaiter.close();
73         bridgeWaiter.close();
74     }
75
76     /**
77      * Connect to the OVSDB node, wait for the connection to be established and for the integration bridge
78      * to be successfully created. Contains assertions for unexpected states
79      * @throws InterruptedException if interrupted while waiting for connection
80      */
81     public void connect() throws Exception {
82         addWaiters();
83
84         assertNotNull("connection failed", itUtils.southboundUtils.addOvsdbNode(connectionInfo, 0));
85
86         ovsdbWaiter.waitForCreation();
87         ovsdbNode = itUtils.southboundUtils.getOvsdbNode(connectionInfo);
88         assertNotNull("node is not connected", ovsdbNode);
89
90         bridgeWaiter.waitForCreation();
91         assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
92                 + " is not connected", itUtils.isControllerConnected(connectionInfo));
93
94         bridgeNode = itUtils.southboundUtils.getBridgeNode(ovsdbNode, INTEGRATION_BRIDGE_NAME);
95         assertNotNull("bridge " + INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
96         datapathId = itUtils.southboundUtils.getDataPathId(bridgeNode);
97         String datapathIdString = itUtils.southboundUtils.getDatapathId(bridgeNode);
98         LOG.info("NodeInfo.connect: bridgeNode: {}, datapathId: {} - {}", bridgeNode, datapathIdString, datapathId);
99         assertNotEquals("datapathId was not found", datapathId, 0);
100     }
101
102     /**
103      * Remove integration bridge and teardown connection. Contains assertions for unexpected states.
104      * @throws InterruptedException if interrupted while waiting for disconnect to complete
105      */
106     public void disconnect() throws Exception {
107         ovsdbWaiter.setMask(NotifyingDataChangeListener.BIT_DELETE);
108         bridgeWaiter.setMask(NotifyingDataChangeListener.BIT_DELETE);
109         assertTrue(itUtils.southboundUtils.deleteBridge(connectionInfo, INTEGRATION_BRIDGE_NAME, 0));
110         bridgeWaiter.waitForDeletion();
111         assertNull("Bridge should not be found", itUtils.mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeIid));
112         assertTrue(itUtils.southboundUtils.disconnectOvsdbNode(connectionInfo, 0));
113         ovsdbWaiter.waitForDeletion();
114         assertNull("Ovsdb node should not be found",
115                 itUtils.mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, ovsdbIid));
116         closeWaiters();
117     }
118 }