Merge "workaround for sfc"
[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
42     public static String getNodeName(long dpidLong) {
43         return OPENFLOW + ":" + dpidLong;
44     }
45
46     public static NodeConnectorId getNodeConnectorId(long ofPort, String nodeName) {
47         return new NodeConnectorId(nodeName + ":" + ofPort);
48     }
49
50     public static NodeConnectorId getNodeConnectorId(long dpidLong, long ofPort) {
51         return getNodeConnectorId(ofPort, getNodeName(dpidLong));
52     }
53
54     public static NodeBuilder createNodeBuilder(String nodeId) {
55         NodeBuilder builder = new NodeBuilder();
56         builder.setId(new NodeId(nodeId));
57         builder.setKey(new NodeKey(builder.getId()));
58         return builder;
59     }
60
61     public static NodeBuilder createNodeBuilder(long dpidLong) {
62         return createNodeBuilder(getNodeName(dpidLong));
63     }
64
65     public static InstanceIdentifier<Flow> createFlowPath(FlowBuilder flowBuilder, NodeBuilder nodeBuilder) {
66         return InstanceIdentifier.builder(Nodes.class)
67                 .child(Node.class, nodeBuilder.getKey())
68                 .augmentation(FlowCapableNode.class)
69                 .child(Table.class, new TableKey(flowBuilder.getTableId()))
70                 .child(Flow.class, flowBuilder.getKey()).build();
71     }
72
73     public static InstanceIdentifier<Node> createNodePath(NodeBuilder nodeBuilder) {
74         return InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeBuilder.getKey()).build();
75     }
76
77     public static Flow getFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder,
78                                ReadOnlyTransaction readTx, final LogicalDatastoreType store) {
79         try {
80             Optional<Flow> data = readTx.read(store, createFlowPath(flowBuilder, nodeBuilder)).get();
81             if (data.isPresent()) {
82                 return data.get();
83             }
84         } catch (InterruptedException|ExecutionException e) {
85             LOG.error(e.getMessage(), e);
86         }
87
88         LOG.info("Cannot find data for Flow {} in {}", flowBuilder.getFlowName(), store);
89         return null;
90     }
91
92     public static FlowBuilder getPipelineFlow(short table, short gotoTable) {
93         FlowBuilder flowBuilder = new FlowBuilder();
94         flowBuilder.setMatch(new MatchBuilder().build());
95
96         String flowId = "DEFAULT_PIPELINE_FLOW_" + table;
97         flowBuilder.setId(new FlowId(flowId));
98         FlowKey key = new FlowKey(new FlowId(flowId));
99         flowBuilder.setStrict(true);
100         flowBuilder.setBarrier(false);
101         flowBuilder.setTableId(table);
102         flowBuilder.setKey(key);
103         flowBuilder.setFlowName(flowId);
104         flowBuilder.setHardTimeout(0);
105         flowBuilder.setIdleTimeout(0);
106         flowBuilder.setPriority(0);
107         return flowBuilder;
108     }
109 }