Bug 5617: OfOverlay refactoring - Source Mapper
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / FlowTable.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 static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.gotoTableIns;
12
13 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Base class for managing flow tables
24  * @author readams
25  */
26 public abstract class FlowTable extends OfTable {
27     protected static final Logger LOG =
28             LoggerFactory.getLogger(FlowTable.class);
29
30     public FlowTable(OfContext ctx) {
31         super(ctx);
32     }
33
34     /**
35      * Get the table ID being manipulated
36      *
37      * @return the table id
38      */
39     public abstract short getTableId();
40
41     // ***************
42     // Utility methods
43     // ***************
44
45     /**
46      * Get a base flow builder with some common features already set
47      *
48      * @return {@link FlowBuilder}
49      */
50     protected FlowBuilder base() {
51         return new FlowBuilder()
52             .setTableId(getTableId())
53             .setBarrier(false)
54             .setHardTimeout(0)
55             .setIdleTimeout(0);
56     }
57
58     /**
59      * Write a drop flow for the given ethertype at the given priority.
60      * If the ethertype is null, then drop all traffic
61      *
62      * @param priority the priority
63      * @param etherType the ethertype
64      * @param tableId the table id
65      * @return a drop flow for the given ethertype at the given priority.
66      */
67     public Flow dropFlow(Integer priority, Long etherType, Short tableId) {
68         FlowId flowid;
69         FlowBuilder flowb = base()
70                 .setPriority(priority)
71                 .setInstructions(FlowUtils.dropInstructions());
72         if (etherType != null) {
73             MatchBuilder mb = new MatchBuilder()
74                     .setEthernetMatch(
75                             FlowUtils.ethernetMatch(null, null, etherType));
76             Match match = mb.build();
77             flowid = FlowIdUtils.newFlowId(tableId, "drop", match);
78             flowb.setMatch(match);
79         } else {
80             flowid = FlowIdUtils.newFlowId("dropAll");
81         }
82         flowb.setId(flowid);
83         return flowb.build();
84     }
85
86 }