3335586bef872a71628083a3f8451e991f1ba55f
[netvirt.git] /
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.netvirt.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<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();
87     }
88
89     public static InstanceIdentifier<Node> createNodePath(NodeBuilder nodeBuilder) {
90         return InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeBuilder.getKey()).build();
91     }
92
93     public static Flow getFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder,
94                                ReadOnlyTransaction readTx, final LogicalDatastoreType store) {
95         try {
96             Optional<Flow> data = readTx.read(store, createFlowPath(flowBuilder, nodeBuilder)).get();
97             if (data.isPresent()) {
98                 return data.get();
99             }
100         } catch (InterruptedException|ExecutionException e) {
101             LOG.error("Failed to get flow {}", flowBuilder.getFlowName(), e);
102         }
103
104         LOG.info("Cannot find data for Flow {} in {}", flowBuilder.getFlowName(), store);
105         return null;
106     }
107
108     public static Table getTable(NodeBuilder nodeBuilder, short table,
109                                     ReadOnlyTransaction readTx, final LogicalDatastoreType store) {
110         try {
111             Optional<Table> data = readTx.read(store, createTablePath(nodeBuilder, table)).get();
112             if (data.isPresent()) {
113                 return data.get();
114             }
115         } catch (InterruptedException|ExecutionException e) {
116             LOG.error("Failed to get table {}", table, e);
117         }
118
119         LOG.info("Cannot find data for table {} in {}", table, store);
120         return null;
121     }
122
123     public static FlowBuilder getPipelineFlow(short table, short gotoTable) {
124         FlowBuilder flowBuilder = new FlowBuilder();
125         flowBuilder.setMatch(new MatchBuilder().build());
126
127         String flowName = "DEFAULT_PIPELINE_FLOW_" + table;
128         return initFlowBuilder(flowBuilder, flowName, table)
129                 .setPriority(0);
130     }
131
132     /**
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.
139      */
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);
145         return flowBuilder;
146     }
147     /**
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.
150      *
151      * @param flowBuilder The flow builder.
152      * @param flowName The flow name.
153      * @param table The table.
154      * @return The flow builder.
155      */
156     public static FlowBuilder initFlowBuilder(FlowBuilder flowBuilder, String flowName, short table) {
157         final FlowId flowId = new FlowId(flowName);
158         flowBuilder
159                 .setId(flowId)
160                 .setStrict(true)
161                 .setBarrier(false)
162                 .setTableId(table)
163                 .setKey(new FlowKey(flowId))
164                 .setFlowName(flowName)
165                 .setHardTimeout(0)
166                 .setIdleTimeout(0);
167         return flowBuilder;
168     }
169 }