ce55b582f6f5189e2d743a9f542572eb2c6bfebd
[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.OfWriter;
12 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
13 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
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.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Base class for managing flow tables
25  * @author readams
26  */
27 public abstract class FlowTable extends OfTable {
28     protected static final Logger LOG =
29             LoggerFactory.getLogger(FlowTable.class);
30
31     public FlowTable(OfContext ctx) {
32         super(ctx);
33     }
34
35     // *******
36     // OfTable
37     // *******
38
39     @Override
40     public void update(NodeId nodeId, PolicyInfo policyInfo,
41                        OfWriter ofWriter) throws Exception {
42
43         sync(nodeId, policyInfo, ofWriter);
44
45     }
46
47     // *********
48     // FlowTable
49     // *********
50
51     /**
52      * Sync flow state using the flow map
53      *
54      * @param nodeId the node id
55      * @param policyInfo the current policy snapshot
56      * @param ofWriter the {@link OfWriter}
57      * @throws Exception throws all exception
58      */
59     public abstract void sync(NodeId nodeId, PolicyInfo policyInfo, OfWriter ofWriter) throws Exception;
60
61     /**
62      * Get the table ID being manipulated
63      *
64      * @return the table id
65      */
66     public abstract short getTableId();
67
68     // ***************
69     // Utility methods
70     // ***************
71
72     /**
73      * Get a base flow builder with some common features already set
74      *
75      * @return {@link FlowBuilder}
76      */
77     protected FlowBuilder base() {
78         return new FlowBuilder()
79             .setTableId(getTableId())
80             .setBarrier(false)
81             .setHardTimeout(0)
82             .setIdleTimeout(0);
83     }
84
85     /**
86      * Write a drop flow for the given ethertype at the given priority.
87      * If the ethertype is null, then drop all traffic
88      *
89      * @param priority the priority
90      * @param etherType the ethertype
91      * @param tableId the table id
92      * @return a drop flow for the given ethertype at the given priority.
93      */
94     public Flow dropFlow(Integer priority, Long etherType, Short tableId) {
95         FlowId flowid;
96         FlowBuilder flowb = base()
97                 .setPriority(priority)
98                 .setInstructions(FlowUtils.dropInstructions());
99         if (etherType != null) {
100             MatchBuilder mb = new MatchBuilder()
101                     .setEthernetMatch(
102                             FlowUtils.ethernetMatch(null, null, etherType));
103             Match match = mb.build();
104             flowid = FlowIdUtils.newFlowId(tableId, "drop", match);
105             flowb.setMatch(match);
106         } else {
107             flowid = FlowIdUtils.newFlowId("dropAll");
108         }
109         flowb.setId(flowid);
110         return flowb.build();
111     }
112
113 }