41dcb05e58b926e498d57d70fef8cb7d434eb4bd
[netvirt.git] / utils / netvirt-it-utils / src / main / java / org / opendaylight / netvirt / utils / netvirt / it / utils / NetvirtItUtils.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.netvirt.utils.netvirt.it.utils;
10
11 import static org.junit.Assert.assertNotNull;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
16 import org.opendaylight.netvirt.utils.mdsal.openflow.FlowUtils;
17 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
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.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * This class contains various utility methods used in netvirt integration tests (IT).
35  */
36 public class NetvirtItUtils {
37     private static final Logger LOG = LoggerFactory.getLogger(NetvirtItUtils.class);
38     MdsalUtils mdsalUtils;
39     SouthboundUtils southboundUtils;
40     DataBroker dataBroker;
41
42     /**
43      * Create a new NetvirtItUtils instance.
44      * @param dataBroker  md-sal data broker
45      */
46     public NetvirtItUtils(DataBroker dataBroker) {
47         this.dataBroker = dataBroker;
48         mdsalUtils = new MdsalUtils(dataBroker);
49         southboundUtils = new SouthboundUtils(mdsalUtils);
50     }
51
52     /**
53      * Check that the netvirt topology is in the operational mdsal.
54      * @return true if the netvirt topology was successfully retrieved
55      */
56     public Boolean getNetvirtTopology() {
57         LOG.info("getNetvirtTopology: looking for {}...", ItConstants.NETVIRT_TOPOLOGY_ID);
58         final TopologyId topologyId = new TopologyId(new Uri(ItConstants.NETVIRT_TOPOLOGY_ID));
59         InstanceIdentifier<Topology> path =
60                 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, new TopologyKey(topologyId));
61         NotifyingDataChangeListener waitForIt = new NotifyingDataChangeListener(LogicalDatastoreType.OPERATIONAL,
62                 path, null);
63         waitForIt.registerDataChangeListener(dataBroker);
64         try {
65             waitForIt.waitForCreation(60 * 1000);
66         } catch (InterruptedException e) {
67             LOG.info("getNetvirtTopology: InterruptedException while wait(ing)ForCreation");
68         }
69
70         boolean found = null != mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
71
72         LOG.info("getNetvirtTopology: found {} == {}", ItConstants.NETVIRT_TOPOLOGY_ID, found);
73
74         return found;
75     }
76
77     /**
78      * Verify that the given flow was installed in a table. This method will wait 10 seconds for the flows
79      * to appear in each of the md-sal CONFIGURATION and OPERATIONAL data stores
80      * @param datapathId dpid where flow is installed
81      * @param flowId The "name" of the flow, e.g., "TunnelFloodOut_100"
82      * @param table integer value of table
83      * @throws InterruptedException if interrupted while waiting for flow to appear in mdsal
84      */
85     public void verifyFlow(long datapathId, String flowId, short table) throws InterruptedException {
86         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder =
87                 FlowUtils.createNodeBuilder(datapathId);
88         FlowBuilder flowBuilder =
89                 FlowUtils.initFlowBuilder(new FlowBuilder(), flowId, table);
90         InstanceIdentifier<Flow> iid = FlowUtils.createFlowPath(flowBuilder, nodeBuilder);
91
92         NotifyingDataChangeListener waitForIt = new NotifyingDataChangeListener(LogicalDatastoreType.CONFIGURATION,
93                 iid, null);
94         waitForIt.registerDataChangeListener(dataBroker);
95         waitForIt.waitForCreation(10000);
96
97         Flow flow = FlowUtils.getFlow(flowBuilder, nodeBuilder,
98                         dataBroker.newReadOnlyTransaction(), LogicalDatastoreType.CONFIGURATION);
99         assertNotNull("Could not find flow in config: " + flowBuilder.build() + "--" + nodeBuilder.build(), flow);
100
101         waitForIt = new NotifyingDataChangeListener(LogicalDatastoreType.OPERATIONAL, iid, null);
102         waitForIt.registerDataChangeListener(dataBroker);
103         waitForIt.waitForCreation(10000);
104
105         flow = FlowUtils.getFlow(flowBuilder, nodeBuilder,
106                         dataBroker.newReadOnlyTransaction(), LogicalDatastoreType.OPERATIONAL);
107         assertNotNull("Could not find flow in operational: " + flowBuilder.build() + "--" + nodeBuilder.build(),
108                 flow);
109     }
110
111     /**
112      * Log the flows in a given table.
113      * @param datapathId dpid
114      * @param tableNum table number
115      * @param store configuration or operational
116      */
117     public void logFlows(long datapathId, short tableNum, LogicalDatastoreType store) {
118         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder =
119                 FlowUtils.createNodeBuilder(datapathId);
120         Table table = FlowUtils.getTable(nodeBuilder, tableNum, dataBroker.newReadOnlyTransaction(), store);
121         if (table == null) {
122             LOG.info("logFlows: Could not find table {} in {}", tableNum, store);
123         }
124         //TBD: Log table and store in one line, flows in following lines
125         for (Flow flow : table.getFlow()) {
126             LOG.info("logFlows: table {} flow {} in {}", tableNum, flow.getFlowName(), store);
127         }
128     }
129
130     /**
131      * Log the flows in a given table.
132      * @param datapathId dpid
133      * @param table table number
134      */
135     public void logFlows(long datapathId, short table) {
136         logFlows(datapathId, table, LogicalDatastoreType.CONFIGURATION);
137     }
138
139     /**
140      * Get a DataBroker and assert that it is not null.
141      * @param providerContext ProviderContext from which to retrieve the DataBroker
142      * @return the Databroker
143      */
144     public static DataBroker getDatabroker(BindingAwareBroker.ProviderContext providerContext) {
145         DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
146         assertNotNull("dataBroker should not be null", dataBroker);
147         return dataBroker;
148     }
149 }