Bug 3302: fix for GroupTable
[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 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxOutputRegAction;
14
15 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
16 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
17 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg7;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Manage the table that assigns source endpoint group, bridge domain, and
27  * router domain to registers to be used by other tables.
28  */
29 public class ExternalMapper extends FlowTable {
30
31     protected static final Logger LOG = LoggerFactory.getLogger(ExternalMapper.class);
32
33     public static short TABLE_ID;
34
35     public ExternalMapper(OfContext ctx, short tableId) {
36         super(ctx);
37         TABLE_ID = tableId;
38     }
39
40     @Override
41     public short getTableId() {
42         return TABLE_ID;
43     }
44
45     @Override
46     public void sync(NodeId nodeId, PolicyInfo policyInfo, OfWriter ofWriter) throws Exception {
47
48         if (ctx.getSwitchManager().getExternalPorts(nodeId) == null) {
49             LOG.trace("No external ports found for node: {}", nodeId);
50             return;
51         }
52         // Default drop all
53         ofWriter.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(1), null, TABLE_ID));
54
55         /*
56          *  Default Egress flow. Other methods may write to this table to augment egress
57          *  functionality, such as bypassing/utilising the NAT table, or ServiceFunctionChaining
58          */
59         ofWriter.writeFlow(nodeId, TABLE_ID, defaultFlow());
60
61     }
62
63     private Flow defaultFlow() {
64         FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "defaultExternalFlow", null);
65         Flow flow = base().setPriority(100)
66             .setId(flowid)
67             .setInstructions(instructions(applyActionIns(nxOutputRegAction(NxmNxReg7.class))))
68             .build();
69         return flow;
70     }
71 }