f8b9676773608deb4ca55c187a378baccf95d768
[ovsdb.git] / utils / it-utils / src / main / java / org / opendaylight / ovsdb / utils / it / utils / ItUtils.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.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.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
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.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This class contains various utility methods used in OVSDB integration tests (IT).
36  */
37 public class ItUtils {
38     private static final Logger LOG = LoggerFactory.getLogger(ItUtils.class);
39     MdsalUtils mdsalUtils;
40     SouthboundUtils southboundUtils;
41     DataBroker dataBroker;
42
43     /**
44      * Create a new ItUtils instance
45      * @param dataBroker  md-sal data broker
46      */
47     public ItUtils(DataBroker dataBroker) {
48         this.dataBroker = dataBroker;
49         mdsalUtils = new MdsalUtils(dataBroker);
50         southboundUtils = new SouthboundUtils(mdsalUtils);
51     }
52
53     /**
54      * Get a NodeInfo instance initialized with this ItUtil's DataBroker
55      * @param connectionInfo ConnectionInfo for the OVSDB server
56      * @param waitList For tracking outstanding md-sal events notifications
57      * @return
58      */
59     public NodeInfo createNodeInfo(ConnectionInfo connectionInfo, List<NotifyingDataChangeListener> waitList) {
60         return new NodeInfo(connectionInfo, this, waitList);
61     }
62
63     /**
64      * Checks whether the OVSDB controller is connected. This method will retry 10 times and will through an
65      * AssertionError for any number of unexpected states.
66      * @param connectionInfo
67      * @return true if connected
68      * @throws InterruptedException
69      */
70     public boolean isControllerConnected(ConnectionInfo connectionInfo) throws InterruptedException {
71         LOG.info("isControllerConnected enter");
72         ControllerEntry controllerEntry;
73         Node ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
74         assertNotNull("ovsdb node not found", ovsdbNode);
75
76         String controllerTarget = southboundUtils.getControllersFromOvsdbNode(ovsdbNode).get(0);
77         assertNotNull("Failed to get controller target", controllerTarget);
78
79         for (int i = 0; i < 10; i++) {
80             LOG.info("isControllerConnected try {}: looking for controller: {}", i, controllerTarget);
81             OvsdbBridgeAugmentation bridge =
82                     southboundUtils.getBridge(connectionInfo, "br-int");
83             if (bridge != null && bridge.getControllerEntry() != null) {
84                 controllerEntry = bridge.getControllerEntry().iterator().next();
85                 assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
86                 if (controllerEntry.isIsConnected()) {
87                     LOG.info("isControllerConnected exit: true {}", controllerTarget);
88                     return true;
89                 }
90             }
91             Thread.sleep(1000);
92         }
93         LOG.info("isControllerConnected exit: false {}", controllerTarget);
94         return false;
95     }
96
97     public static DataBroker getDatabroker(BindingAwareBroker.ProviderContext providerContext) {
98         DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
99         assertNotNull("dataBroker should not be null", dataBroker);
100         return dataBroker;
101     }
102
103     public Boolean getNetvirtTopology() {
104         LOG.info("getNetvirtTopology: looking for {}...", ItConstants.NETVIRT_TOPOLOGY_ID);
105         Boolean found = false;
106         final TopologyId topologyId = new TopologyId(new Uri(ItConstants.NETVIRT_TOPOLOGY_ID));
107         InstanceIdentifier<Topology> path =
108                 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, new TopologyKey(topologyId));
109         for (int i = 0; i < 60; i++) {
110             Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
111             if (topology != null) {
112                 LOG.info("getNetvirtTopology: found {}...", ItConstants.NETVIRT_TOPOLOGY_ID);
113                 found = true;
114                 break;
115             } else {
116                 LOG.info("getNetvirtTopology: still looking (try {})...", i);
117                 try {
118                     Thread.sleep(1000);
119                 } catch (InterruptedException e) {
120                     LOG.warn("Interrupted while waiting for {}", ItConstants.NETVIRT_TOPOLOGY_ID, e);
121                 }
122             }
123         }
124         return found;
125     }
126 }