Update .gitreview for new repo
[netvirt.git] / utils / mdsal-openflow / src / main / java / org / opendaylight / ovsdb / utils / mdsal / openflow / FlowUtils.java
1 /*
2  * Copyright (c) 2015 - 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.mdsal.openflow;
10
11 import com.google.common.base.Optional;
12
13 import java.util.concurrent.ExecutionException;
14
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;
36
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;
45
46
47     public static String getNodeName(long dpidLong) {
48         return OPENFLOW + ":" + dpidLong;
49     }
50
51     public static NodeConnectorId getNodeConnectorId(long ofPort, String nodeName) {
52         return new NodeConnectorId(nodeName + ":" + ofPort);
53     }
54
55     public static NodeConnectorId getSpecialNodeConnectorId(long dpidLong, String portName) {
56         return new NodeConnectorId(getNodeName(dpidLong) + ":" + portName);
57     }
58
59     public static NodeConnectorId getNodeConnectorId(long dpidLong, long ofPort) {
60         return getNodeConnectorId(ofPort, getNodeName(dpidLong));
61     }
62
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()));
67         return builder;
68     }
69
70     public static NodeBuilder createNodeBuilder(long dpidLong) {
71         return createNodeBuilder(getNodeName(dpidLong));
72     }
73
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();
80     }
81
82     public static InstanceIdentifier<Node> createNodePath(NodeBuilder nodeBuilder) {
83         return InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeBuilder.getKey()).build();
84     }
85
86     public static Flow getFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder,
87                                ReadOnlyTransaction readTx, final LogicalDatastoreType store) {
88         try {
89             Optional<Flow> data = readTx.read(store, createFlowPath(flowBuilder, nodeBuilder)).get();
90             if (data.isPresent()) {
91                 return data.get();
92             }
93         } catch (InterruptedException|ExecutionException e) {
94             LOG.error("Failed to get flow {}", flowBuilder.getFlowName(), e);
95         }
96
97         LOG.info("Cannot find data for Flow {} in {}", flowBuilder.getFlowName(), store);
98         return null;
99     }
100
101     public static FlowBuilder getPipelineFlow(short table, short gotoTable) {
102         FlowBuilder flowBuilder = new FlowBuilder();
103         flowBuilder.setMatch(new MatchBuilder().build());
104
105         String flowName = "DEFAULT_PIPELINE_FLOW_" + table;
106         return initFlowBuilder(flowBuilder, flowName, table)
107                 .setPriority(0);
108     }
109
110     /**
111      * Creates a flowBuilder.
112      * @param flowName the flow name
113      * @param priority the priority
114      * @param matchBuilder the match builder
115      * @param tableNo the table no to which flow needs to be inserted.
116      * @return the created flow builder.
117      */
118     public static FlowBuilder createFlowBuilder(String flowName,Integer priority,
119                                                 MatchBuilder matchBuilder, short tableNo) {
120         FlowBuilder flowBuilder = new FlowBuilder();
121         flowBuilder.setMatch(matchBuilder.build());
122         initFlowBuilder(flowBuilder, flowName, tableNo).setPriority(priority);
123         return flowBuilder;
124     }
125     /**
126      * Sets up common defaults for the given flow builder: a flow identifier and key based on the given flow name,
127      * strict, no barrier, the given table identifier, no hard timeout and no idle timeout.
128      *
129      * @param flowBuilder The flow builder.
130      * @param flowName The flow name.
131      * @param table The table.
132      * @return The flow builder.
133      */
134     public static FlowBuilder initFlowBuilder(FlowBuilder flowBuilder, String flowName, short table) {
135         final FlowId flowId = new FlowId(flowName);
136         flowBuilder
137                 .setId(flowId)
138                 .setStrict(true)
139                 .setBarrier(false)
140                 .setTableId(table)
141                 .setKey(new FlowKey(flowId))
142                 .setFlowName(flowName)
143                 .setHardTimeout(0)
144                 .setIdleTimeout(0);
145         return flowBuilder;
146     }
147 }