5277fb96546d7ac565b50d8ac33aa9ba16b789da
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / FlowUtils.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, 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.groupbasedpolicy.renderer.ofoverlay.flow;
10
11 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
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.FlowKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActionsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39
40 import com.google.common.collect.ImmutableList;
41
42 /**
43  * Utilities for constructing OpenFlow flows
44  */
45 public final class FlowUtils {
46
47     /**
48      * Creates an Instance Identifier (path) for node with specified id
49      *
50      * @param nodeId
51      * @return
52      */
53     public static final InstanceIdentifier<Node> 
54         createNodePath(final NodeId nodeId) {
55         return InstanceIdentifier.builder(Nodes.class)
56                 .child(Node.class, new NodeKey(nodeId))
57                 .build();
58     }
59
60     /**
61      * Shorten's node child path to node path.
62      *
63      * @param nodeChild child of node, from which we want node path.
64      * @return
65      */
66     public static final InstanceIdentifier<Node> 
67         getNodePath(final InstanceIdentifier<?> nodeChild) {
68         return nodeChild.firstIdentifierOf(Node.class);
69     }
70
71
72     /**
73      * Creates a table path by appending table specific location to node path
74      *
75      * @param nodePath
76      * @param tableKey
77      * @return
78      */
79     public static final InstanceIdentifier<Table> 
80         createTablePath(final InstanceIdentifier<Node> nodePath, 
81                         final TableKey tableKey) {
82         return nodePath.builder()
83                 .augmentation(FlowCapableNode.class)
84                 .child(Table.class, tableKey)
85                 .build();
86     }
87
88     /**
89      * Creates a table path from a node ID and table ID
90      *
91      * @param nodePath
92      * @param tableKey
93      * @return
94      */
95     public static final InstanceIdentifier<Table> 
96         createTablePath(final NodeId nodeId, 
97                         final short tableId) {
98         return createNodePath(nodeId).builder()
99                 .augmentation(FlowCapableNode.class)
100                 .child(Table.class, new TableKey(tableId))
101                 .build();
102     }
103
104     /**
105      * Creates a path for particular flow, by appending flow-specific information
106      * to table path.
107      *
108      * @param table
109      * @param flowKey
110      * @return
111      */
112     public static InstanceIdentifier<Flow> 
113             createFlowPath(final InstanceIdentifier<Table> table, 
114                            final FlowKey flowKey) {
115         return table.child(Flow.class, flowKey);
116     }
117     
118     /**
119      * Creates a path for particular flow, by appending flow-specific information
120      * to table path.
121      *
122      * @param table
123      * @param flowId
124      * @return
125      */
126     public static InstanceIdentifier<Flow> 
127             createFlowPath(final InstanceIdentifier<Table> table, 
128                            final FlowId flowId) {
129         return createFlowPath(table, new FlowKey(flowId));
130     }
131
132     public static Instructions gotoTable(short tableId) {
133         return new InstructionsBuilder()
134         .setInstruction(ImmutableList.of(new InstructionBuilder()
135             .setOrder(Integer.valueOf(0))
136             .setInstruction(new GoToTableCaseBuilder()
137                 .setGoToTable(new GoToTableBuilder()
138                     .setTableId(tableId)
139                     .build())
140                 .build())
141             .build()))
142         .build();
143     }
144     
145     public static Instructions dropInstructions() {
146         return new InstructionsBuilder()
147             .setInstruction(ImmutableList.of(new InstructionBuilder()
148                 .setOrder(Integer.valueOf(0))
149                 .setInstruction(new WriteActionsCaseBuilder()
150                     .setWriteActions(new WriteActionsBuilder()
151                         .setAction(ImmutableList.of(new ActionBuilder()
152                             .setOrder(Integer.valueOf(0))
153                             .setAction(new DropActionCaseBuilder()
154                                 .setDropAction(new DropActionBuilder()
155                                     .build())
156                                 .build())
157                             .build()))
158                         .build())
159                     .build())
160                 .build()))
161             .build();
162     }
163     
164     public static EthernetMatch ethernetMatch(MacAddress srcMac, 
165                                               MacAddress dstMac,
166                                               Long etherType) {
167         EthernetMatchBuilder emb = new  EthernetMatchBuilder();
168         if (srcMac != null)
169             emb.setEthernetSource(new EthernetSourceBuilder()
170                 .setAddress(srcMac)
171                 .build());
172         if (dstMac != null)
173             emb.setEthernetDestination(new EthernetDestinationBuilder()
174                 .setAddress(dstMac)
175                 .build());
176         if (etherType != null)
177             emb.setEthernetType(new EthernetTypeBuilder()
178                 .setType(new EtherType(etherType))
179                 .build());
180         return emb.build();
181     }
182 }
183