c541725e4358e4c0643f60cc977945dd269681df
[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.MatchBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
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     // OfTable
36     // *******
37
38     @Override
39     public void update(NodeId nodeId, PolicyInfo policyInfo,
40                        FlowMap flowMap) throws Exception {
41
42         sync(nodeId, policyInfo, flowMap);
43
44     }
45
46     // *********
47     // FlowTable
48     // *********
49
50     /**
51      * Sync flow state using the flow map
52      * @throws Exception
53      */
54     public abstract void sync(NodeId nodeId, PolicyInfo policyInfo, FlowMap flowMap) throws Exception;
55
56     /**
57      * Get the table ID being manipulated
58      */
59     public abstract short getTableId();
60
61     // ***************
62     // Utility methods
63     // ***************
64
65     /**
66      * Get a base flow builder with some common features already set
67      */
68     protected FlowBuilder base() {
69         return new FlowBuilder()
70             .setTableId(getTableId())
71             .setBarrier(false)
72             .setHardTimeout(0)
73             .setIdleTimeout(0);
74     }
75
76     /**
77      * Write a drop flow for the given ethertype at the given priority.
78      * If the ethertype is null, then drop all traffic
79      */
80     public Flow dropFlow(Integer priority, Long etherType) {
81         FlowId flowid = new FlowId(new StringBuilder()
82                 .append("drop|")
83                 .append(etherType)
84                 .toString());
85
86         FlowBuilder flowb = base()
87                 .setId(flowid)
88                 .setPriority(priority)
89                 .setInstructions(FlowUtils.dropInstructions());
90         if (etherType != null)
91             flowb.setMatch(new MatchBuilder()
92                     .setEthernetMatch(FlowUtils.ethernetMatch(null, null,
93                             etherType))
94                     .build());
95         return flowb.build();
96     }
97
98 }