Merge "Use MD-SAL events to react to OVSDB changes"
[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.OutputPortValues;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class FlowUtils {
37     private static final Logger LOG = LoggerFactory.getLogger(FlowUtils.class);
38     private static final String OPENFLOW = "openflow";
39     public final static long REG_VALUE_FROM_LOCAL = 0x1L;
40     public final static long REG_VALUE_FROM_REMOTE = 0x2L;
41     public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
42     public static final int ARP_OP_REQUEST = 0x1;
43     public static final int ARP_OP_REPLY = 0x2;
44
45
46     public static String getNodeName(long dpidLong) {
47         return OPENFLOW + ":" + dpidLong;
48     }
49
50     public static NodeConnectorId getNodeConnectorId(long ofPort, String nodeName) {
51         return new NodeConnectorId(nodeName + ":" + ofPort);
52     }
53
54     public static NodeConnectorId getSpecialNodeConnectorId(long dpidLong, String portName) {
55         return new NodeConnectorId(getNodeName(dpidLong) + ":" + portName);
56     }
57
58     public static NodeConnectorId getNodeConnectorId(long dpidLong, long ofPort) {
59         return getNodeConnectorId(ofPort, getNodeName(dpidLong));
60     }
61
62     public static NodeBuilder createNodeBuilder(String nodeId) {
63         NodeBuilder builder = new NodeBuilder();
64         builder.setId(new NodeId(nodeId));
65         builder.setKey(new NodeKey(builder.getId()));
66         return builder;
67     }
68
69     public static NodeBuilder createNodeBuilder(long dpidLong) {
70         return createNodeBuilder(getNodeName(dpidLong));
71     }
72
73     public static InstanceIdentifier<Flow> createFlowPath(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
74         return InstanceIdentifier.builder(Nodes.class)
75                 .child(Node.class, nodeBuilder.getKey())
76                 .augmentation(FlowCapableNode.class)
77                 .child(Table.class, new TableKey(flowBuilder.getTableId()))
78                 .child(Flow.class, flowBuilder.getKey()).build();
79     }
80
81     public static InstanceIdentifier<Node> createNodePath(NodeBuilder nodeBuilder) {
82         return InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeBuilder.getKey()).build();
83     }
84
85     public static Flow getFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder,
86                                ReadOnlyTransaction readTx, final LogicalDatastoreType store) {
87         try {
88             Optional<Flow> data = readTx.read(store, createFlowPath(flowBuilder, nodeBuilder)).get();
89             if (data.isPresent()) {
90                 return data.get();
91             }
92         } catch (InterruptedException|ExecutionException e) {
93             LOG.error(e.getMessage(), e);
94         }
95
96         LOG.info("Cannot find data for Flow {} in {}", flowBuilder.getFlowName(), store);
97         return null;
98     }
99
100     public static FlowBuilder getPipelineFlow(short table, short gotoTable) {
101         FlowBuilder flowBuilder = new FlowBuilder();
102         flowBuilder.setMatch(new MatchBuilder().build());
103
104         String flowId = "DEFAULT_PIPELINE_FLOW_" + table;
105         flowBuilder.setId(new FlowId(flowId));
106         FlowKey key = new FlowKey(new FlowId(flowId));
107         flowBuilder.setStrict(true);
108         flowBuilder.setBarrier(false);
109         flowBuilder.setTableId(table);
110         flowBuilder.setKey(key);
111         flowBuilder.setFlowName(flowId);
112         flowBuilder.setHardTimeout(0);
113         flowBuilder.setIdleTimeout(0);
114         flowBuilder.setPriority(0);
115         return flowBuilder;
116     }
117 }