2 * Copyright (c) 2015 - 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.mdsal.openflow;
11 import com.google.common.base.Optional;
13 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class FlowUtils {
38 private static final Logger LOG = LoggerFactory.getLogger(FlowUtils.class);
39 private static final String OPENFLOW = "openflow";
40 public final static long REG_VALUE_FROM_LOCAL = 0x1L;
41 public final static long REG_VALUE_FROM_REMOTE = 0x2L;
42 public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
43 public static final int ARP_OP_REQUEST = 0x1;
44 public static final int ARP_OP_REPLY = 0x2;
47 public static String getNodeName(long dpidLong) {
48 return OPENFLOW + ":" + dpidLong;
51 public static NodeConnectorId getNodeConnectorId(long ofPort, String nodeName) {
52 return new NodeConnectorId(nodeName + ":" + ofPort);
55 public static NodeConnectorId getSpecialNodeConnectorId(long dpidLong, String portName) {
56 return new NodeConnectorId(getNodeName(dpidLong) + ":" + portName);
59 public static NodeConnectorId getNodeConnectorId(long dpidLong, long ofPort) {
60 return getNodeConnectorId(ofPort, getNodeName(dpidLong));
63 public static NodeBuilder createNodeBuilder(String nodeId) {
64 NodeBuilder builder = new NodeBuilder();
65 builder.setId(new NodeId(nodeId));
66 builder.setKey(new NodeKey(builder.getId()));
70 public static NodeBuilder createNodeBuilder(long dpidLong) {
71 return createNodeBuilder(getNodeName(dpidLong));
74 public static InstanceIdentifier<Flow> createFlowPath(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
75 return InstanceIdentifier.builder(Nodes.class)
76 .child(Node.class, nodeBuilder.getKey())
77 .augmentation(FlowCapableNode.class)
78 .child(Table.class, new TableKey(flowBuilder.getTableId()))
79 .child(Flow.class, flowBuilder.getKey()).build();
82 public static InstanceIdentifier<Table> createTablePath(NodeBuilder nodeBuilder, short table) {
83 return InstanceIdentifier.builder(Nodes.class)
84 .child(Node.class, nodeBuilder.getKey())
85 .augmentation(FlowCapableNode.class)
86 .child(Table.class, new TableKey(table)).build();
89 public static InstanceIdentifier<Node> createNodePath(NodeBuilder nodeBuilder) {
90 return InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeBuilder.getKey()).build();
93 public static Flow getFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder,
94 ReadOnlyTransaction readTx, final LogicalDatastoreType store) {
96 Optional<Flow> data = readTx.read(store, createFlowPath(flowBuilder, nodeBuilder)).get();
97 if (data.isPresent()) {
100 } catch (InterruptedException|ExecutionException e) {
101 LOG.error("Failed to get flow {}", flowBuilder.getFlowName(), e);
104 LOG.info("Cannot find data for Flow {} in {}", flowBuilder.getFlowName(), store);
108 public static Table getTable(NodeBuilder nodeBuilder, short table,
109 ReadOnlyTransaction readTx, final LogicalDatastoreType store) {
111 Optional<Table> data = readTx.read(store, createTablePath(nodeBuilder, table)).get();
112 if (data.isPresent()) {
115 } catch (InterruptedException|ExecutionException e) {
116 LOG.error("Failed to get table {}", table, e);
119 LOG.info("Cannot find data for table {} in {}", table, store);
123 public static FlowBuilder getPipelineFlow(short table, short gotoTable) {
124 FlowBuilder flowBuilder = new FlowBuilder();
125 flowBuilder.setMatch(new MatchBuilder().build());
127 String flowName = "DEFAULT_PIPELINE_FLOW_" + table;
128 return initFlowBuilder(flowBuilder, flowName, table)
133 * Creates a flowBuilder.
134 * @param flowName the flow name
135 * @param priority the priority
136 * @param matchBuilder the match builder
137 * @param tableNo the table no to which flow needs to be inserted.
138 * @return the created flow builder.
140 public static FlowBuilder createFlowBuilder(String flowName,Integer priority,
141 MatchBuilder matchBuilder, short tableNo) {
142 FlowBuilder flowBuilder = new FlowBuilder();
143 flowBuilder.setMatch(matchBuilder.build());
144 initFlowBuilder(flowBuilder, flowName, tableNo).setPriority(priority);
148 * Sets up common defaults for the given flow builder: a flow identifier and key based on the given flow name,
149 * strict, no barrier, the given table identifier, no hard timeout and no idle timeout.
151 * @param flowBuilder The flow builder.
152 * @param flowName The flow name.
153 * @param table The table.
154 * @return The flow builder.
156 public static FlowBuilder initFlowBuilder(FlowBuilder flowBuilder, String flowName, short table) {
157 final FlowId flowId = new FlowId(flowName);
163 .setKey(new FlowKey(flowId))
164 .setFlowName(flowName)