522935ea005044b80ec0adbbd31715c4c49a9666
[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.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;
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                        FlowMap flowMap) throws Exception {
42
43         sync(nodeId, policyInfo, flowMap);
44
45     }
46
47     // *********
48     // FlowTable
49     // *********
50
51     /**
52      * Sync flow state using the flow map
53      * @throws Exception
54      */
55     public abstract void sync(NodeId nodeId, PolicyInfo policyInfo, FlowMap flowMap) throws Exception;
56
57     /**
58      * Get the table ID being manipulated
59      */
60     public abstract short getTableId();
61
62     // ***************
63     // Utility methods
64     // ***************
65
66     /**
67      * Get a base flow builder with some common features already set
68      */
69     protected FlowBuilder base() {
70         return new FlowBuilder()
71             .setTableId(getTableId())
72             .setBarrier(false)
73             .setHardTimeout(0)
74             .setIdleTimeout(0);
75     }
76
77     /**
78      * Write a drop flow for the given ethertype at the given priority.
79      * If the ethertype is null, then drop all traffic
80      */
81     public Flow dropFlow(Integer priority, Long etherType, Short tableId) {
82         FlowId flowid;
83         FlowBuilder flowb = base()
84                 .setPriority(priority)
85                 .setInstructions(FlowUtils.dropInstructions());
86         if (etherType != null) {
87             MatchBuilder mb = new MatchBuilder()
88                     .setEthernetMatch(
89                             FlowUtils.ethernetMatch(null, null, etherType));
90             Match match = mb.build();
91             flowid = FlowIdUtils.newFlowId(tableId, "drop", match);
92             flowb.setMatch(match);
93         } else {
94             flowid = FlowIdUtils.newFlowId("dropAll");
95         }
96         flowb.setId(flowid);
97         return flowb.build();
98     }
99
100 }