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