2 * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.utils.netvirt.it.utils;
11 import static org.junit.Assert.assertNotNull;
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;
34 * This class contains various utility methods used in netvirt integration tests (IT).
36 public class NetvirtItUtils {
37 private static final Logger LOG = LoggerFactory.getLogger(NetvirtItUtils.class);
38 MdsalUtils mdsalUtils;
39 SouthboundUtils southboundUtils;
40 DataBroker dataBroker;
43 * Create a new NetvirtItUtils instance.
44 * @param dataBroker md-sal data broker
46 public NetvirtItUtils(DataBroker dataBroker) {
47 this.dataBroker = dataBroker;
48 mdsalUtils = new MdsalUtils(dataBroker);
49 southboundUtils = new SouthboundUtils(mdsalUtils);
53 * Check that the netvirt topology is in the operational mdsal.
54 * @return true if the netvirt topology was successfully retrieved
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,
63 waitForIt.registerDataChangeListener(dataBroker);
65 waitForIt.waitForCreation(60 * 1000);
66 } catch (InterruptedException e) {
67 LOG.info("getNetvirtTopology: InterruptedException while wait(ing)ForCreation");
70 boolean found = null != mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
72 LOG.info("getNetvirtTopology: found {} == {}", ItConstants.NETVIRT_TOPOLOGY_ID, found);
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
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);
92 NotifyingDataChangeListener waitForIt = new NotifyingDataChangeListener(LogicalDatastoreType.CONFIGURATION,
94 waitForIt.registerDataChangeListener(dataBroker);
95 waitForIt.waitForCreation(10000);
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);
101 waitForIt = new NotifyingDataChangeListener(LogicalDatastoreType.OPERATIONAL, iid, null);
102 waitForIt.registerDataChangeListener(dataBroker);
103 waitForIt.waitForCreation(10000);
105 flow = FlowUtils.getFlow(flowBuilder, nodeBuilder,
106 dataBroker.newReadOnlyTransaction(), LogicalDatastoreType.OPERATIONAL);
107 assertNotNull("Could not find flow in operational: " + flowBuilder.build() + "--" + nodeBuilder.build(),
112 * Log the flows in a given table.
113 * @param datapathId dpid
114 * @param tableNum table number
115 * @param store configuration or operational
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);
122 LOG.info("logFlows: Could not find table {} in {}", tableNum, store);
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);
131 * Log the flows in a given table.
132 * @param datapathId dpid
133 * @param table table number
135 public void logFlows(long datapathId, short table) {
136 logFlows(datapathId, table, LogicalDatastoreType.CONFIGURATION);
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
144 public static DataBroker getDatabroker(BindingAwareBroker.ProviderContext providerContext) {
145 DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
146 assertNotNull("dataBroker should not be null", dataBroker);