Bug 3540: Implementation of FlowIds based on match data
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / ExternalMapper.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.applyActionIns;
12 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.instructions;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17
18 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
19 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;
20 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf.Action;
21 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf.AllowAction;
22 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf.SubjectFeatures;
23 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Manage the table that assigns source endpoint group, bridge domain, and
36  * router domain to registers to be used by other tables.
37  */
38 public class ExternalMapper extends FlowTable {
39
40     protected static final Logger LOG = LoggerFactory.getLogger(ExternalMapper.class);
41
42     public static short TABLE_ID;
43
44     public ExternalMapper(OfContext ctx, short tableId) {
45         super(ctx);
46         TABLE_ID = tableId;
47     }
48
49     @Override
50     public short getTableId() {
51         return TABLE_ID;
52     }
53
54     @Override
55     public void sync(NodeId nodeId, PolicyInfo policyInfo, FlowMap flowMap) throws Exception {
56
57         if (ctx.getSwitchManager().getExternalPorts(nodeId) == null) {
58             LOG.trace("No external ports found for node: {}", nodeId);
59             return;
60         }
61         // Default drop all
62         flowMap.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(1), null, TABLE_ID));
63
64         // Drop IP traffic that doesn't match a source IP rule
65         flowMap.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(2), FlowUtils.ARP, TABLE_ID));
66         flowMap.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(2), FlowUtils.IPv4, TABLE_ID));
67         flowMap.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(2), FlowUtils.IPv6, TABLE_ID));
68         l3flow(flowMap,nodeId, 100, true);
69         l3flow(flowMap,nodeId, 200, false);
70     }
71
72     private void l3flow(FlowMap flowMap, NodeId nodeId, Integer priority, boolean arp) {
73
74         List<ActionBuilder> actionBuilderList = new ArrayList<ActionBuilder>();
75
76         Action action = SubjectFeatures.getAction(AllowAction.DEFINITION.getId());
77         actionBuilderList = action.updateAction(actionBuilderList, new HashMap<String, Object>(), 0, null);
78
79         Layer3Match m = null;
80         Long etherType = null;
81
82         if (arp) {
83             etherType = FlowUtils.ARP;
84         } else {
85             etherType = FlowUtils.IPv4;
86         }
87
88         Match match = new MatchBuilder().setEthernetMatch(FlowUtils.ethernetMatch(null, null, etherType))
89                 .setLayer3Match(m)
90                 .build();
91         FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "ExternalMapper", match);
92         Flow flow = base().setPriority(priority)
93             .setId(flowid)
94             .setMatch(match)
95             .setInstructions(instructions(applyActionIns(actionBuilderList)))
96             .build();
97
98         flowMap.writeFlow(nodeId, TABLE_ID, flow);
99     }
100 }