74fea32479bf269bc184ff771c0d5a82cb35227b
[netvirt.git] / utils / mdsal-openflow / src / main / java / org / opendaylight / ovsdb / utils / mdsal / openflow / FlowUtils.java
1 /*
2  * Copyright (c) 2015 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 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class FlowUtils {
36     private static final Logger LOG = LoggerFactory.getLogger(FlowUtils.class);
37     private static final String OPENFLOW = "openflow";
38     public final static long REG_VALUE_FROM_LOCAL = 0x1L;
39     public final static long REG_VALUE_FROM_REMOTE = 0x2L;
40     public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
41     public static final int ARP_OP_REQUEST = 0x1;
42     public static final int ARP_OP_REPLY = 0x2;
43
44
45     public static String getNodeName(long dpidLong) {
46         return OPENFLOW + ":" + dpidLong;
47     }
48
49     public static NodeConnectorId getNodeConnectorId(long ofPort, String nodeName) {
50         return new NodeConnectorId(nodeName + ":" + ofPort);
51     }
52
53     public static NodeConnectorId getSpecialNodeConnectorId(long dpidLong, String portName) {
54         return new NodeConnectorId(getNodeName(dpidLong) + ":" + portName);
55     }
56
57     public static NodeConnectorId getNodeConnectorId(long dpidLong, long ofPort) {
58         return getNodeConnectorId(ofPort, getNodeName(dpidLong));
59     }
60
61     public static NodeBuilder createNodeBuilder(String nodeId) {
62         NodeBuilder builder = new NodeBuilder();
63         builder.setId(new NodeId(nodeId));
64         builder.setKey(new NodeKey(builder.getId()));
65         return builder;
66     }
67
68     public static NodeBuilder createNodeBuilder(long dpidLong) {
69         return createNodeBuilder(getNodeName(dpidLong));
70     }
71
72     public static InstanceIdentifier<Flow> createFlowPath(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
73         return InstanceIdentifier.builder(Nodes.class)
74                 .child(Node.class, nodeBuilder.getKey())
75                 .augmentation(FlowCapableNode.class)
76                 .child(Table.class, new TableKey(flowBuilder.getTableId()))
77                 .child(Flow.class, flowBuilder.getKey()).build();
78     }
79
80     public static InstanceIdentifier<Node> createNodePath(NodeBuilder nodeBuilder) {
81         return InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeBuilder.getKey()).build();
82     }
83
84     public static Flow getFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder,
85                                ReadOnlyTransaction readTx, final LogicalDatastoreType store) {
86         try {
87             Optional<Flow> data = readTx.read(store, createFlowPath(flowBuilder, nodeBuilder)).get();
88             if (data.isPresent()) {
89                 return data.get();
90             }
91         } catch (InterruptedException|ExecutionException e) {
92             LOG.error(e.getMessage(), e);
93         }
94
95         LOG.info("Cannot find data for Flow {} in {}", flowBuilder.getFlowName(), store);
96         return null;
97     }
98
99     public static FlowBuilder getPipelineFlow(short table, short gotoTable) {
100         FlowBuilder flowBuilder = new FlowBuilder();
101         flowBuilder.setMatch(new MatchBuilder().build());
102
103         String flowName = "DEFAULT_PIPELINE_FLOW_" + table;
104         return initFlowBuilder(flowBuilder, flowName, table)
105                 .setPriority(0);
106     }
107
108     /**
109      * Sets up common defaults for the given flow builder: a flow identifier and key based on the given flow name,
110      * strict, no barrier, the given table identifier, no hard timeout and no idle timeout.
111      *
112      * @param flowBuilder The flow builder.
113      * @param flowName The flow name.
114      * @param table The table.
115      * @return The flow builder.
116      */
117     public static FlowBuilder initFlowBuilder(FlowBuilder flowBuilder, String flowName, short table) {
118         final FlowId flowId = new FlowId(flowName);
119         flowBuilder
120                 .setId(flowId)
121                 .setStrict(true)
122                 .setBarrier(false)
123                 .setTableId(table)
124                 .setKey(new FlowKey(flowId))
125                 .setFlowName(flowName)
126                 .setHardTimeout(0)
127                 .setIdleTimeout(0);
128         return flowBuilder;
129     }
130 }