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