Bug 3302: fix for GroupTable
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / SourceMapper.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.addNxTunIdMatch;
12 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.applyActionIns;
13 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.ethernetMatch;
14 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.gotoTableIns;
15 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.instructions;
16 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadRegAction;
17 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadTunIdAction;
18
19 import java.math.BigInteger;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.Set;
23
24 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
25 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
26 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
27 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
28 import org.opendaylight.groupbasedpolicy.resolver.IndexedTenant;
29 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 import com.google.common.collect.Sets;
52
53 /**
54  * Manage the table that assigns source endpoint group, bridge domain, and
55  * router domain to registers to be used by other tables.
56  */
57 public class SourceMapper extends FlowTable {
58
59     protected static final Logger LOG = LoggerFactory.getLogger(SourceMapper.class);
60
61     // TODO Li alagalah Improve UT coverage for this class.
62     public static short TABLE_ID;
63
64     public SourceMapper(OfContext ctx, short tableId) {
65         super(ctx);
66         TABLE_ID = tableId;
67     }
68
69     @Override
70     public short getTableId() {
71         return TABLE_ID;
72     }
73
74     @Override
75     public void sync(NodeId nodeId, PolicyInfo policyInfo, OfWriter ofWriter) throws Exception {
76
77         ofWriter.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(1), null, TABLE_ID));
78
79         // Handle case where packets from from External
80         for (Endpoint ep : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
81             OfOverlayContext ofc = ep.getAugmentation(OfOverlayContext.class);
82
83             IndexedTenant tenant = ctx.getPolicyResolver().getTenant(ep.getTenant());
84             if (tenant == null)
85                 continue;
86
87             EndpointFwdCtxOrdinals epFwdCtxOrds = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, policyInfo, ep);
88             if (epFwdCtxOrds == null) {
89                 LOG.debug("getEndpointFwdCtxOrdinals is null for EP {}", ep);
90                 continue;
91             }
92
93             createRemoteTunnels(ofWriter, nodeId, ep, policyInfo, epFwdCtxOrds);
94
95             if (ep.getTenant() == null || (ep.getEndpointGroup() == null && ep.getEndpointGroups() == null)) {
96                 continue;
97             }
98
99             if (ofc != null && ofc.getNodeConnectorId() != null
100                     && (ofc.getLocationType() == null || LocationType.Internal.equals(ofc.getLocationType()))) {
101                 /**
102                  * Sync the local EP information.
103                  */
104                 syncEP(ofWriter, nodeId, ep, ofc.getNodeConnectorId(), epFwdCtxOrds);
105             }
106         }
107     }
108
109     private void createRemoteTunnels(OfWriter ofWriter, NodeId nodeId, Endpoint ep, PolicyInfo policyInfo,
110             EndpointFwdCtxOrdinals epFwdCtxOrds) throws Exception {
111         Set<EgKey> epgs = new HashSet<>();
112
113         // Get EPGs and add to Set to remove duplicates
114         // TODO alagalah Li: test EndpointManager.getEgKeys
115         if (ep.getEndpointGroup() != null) {
116             epgs.add(new EgKey(ep.getTenant(), ep.getEndpointGroup()));
117         }
118         if (ep.getEndpointGroups() != null) {
119             for (EndpointGroupId epgId : ep.getEndpointGroups()) {
120                 epgs.add(new EgKey(ep.getTenant(), epgId));
121             }
122         }
123
124         // Create tunnels on remote Nodes that may talk to us.
125         for (EgKey epg : epgs) {
126             Set<EgKey> peers = Sets.union(Collections.singleton(epg), policyInfo.getPeers(epg));
127             for (EgKey peer : peers) {
128                 for (NodeId remoteNodeId : ctx.getEndpointManager().getNodesForGroup(peer)) {
129
130                     // Please do not check for remote v local nodeID, we need local to local tunnels
131                     // in the case of chaining - The Great Dr Sunal.
132                     NodeConnectorId tunPort = ctx.getSwitchManager().getTunnelPort(remoteNodeId, TunnelTypeVxlan.class);
133                     if (tunPort == null) {
134                         LOG.trace("No tunnel port for tunnel in SourceMapper between local:{} and remote:{}",
135                                 nodeId.getValue(), remoteNodeId.getValue());
136                         continue;
137                     }
138                     ofWriter.writeFlow(remoteNodeId, TABLE_ID, createTunnelFlow(tunPort, epFwdCtxOrds));
139                     ofWriter.writeFlow(remoteNodeId, TABLE_ID, createBroadcastFlow(tunPort, epFwdCtxOrds));
140                 }
141             }
142         }
143     }
144
145     private Flow createBroadcastFlow(NodeConnectorId tunPort, EndpointFwdCtxOrdinals epFwdCtxOrds) {
146
147         int fdId = epFwdCtxOrds.getFdId();
148
149         MatchBuilder mb = new MatchBuilder().setInPort(tunPort);
150         addNxTunIdMatch(mb, fdId);
151
152         // set condition group register to all ones to
153         // bypass
154         // policy enforcement
155         /*
156          * TODO: This breaks distributed policy enforcement
157          * especially wrt multi-action. BAD. Must be addressed
158          * (this is why we can't have nice things).
159          * This can be fixed with new tunnelId ordinal in
160          * Ordinal Factory.
161          */
162
163         Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
164
165         Match match = mb.build();
166         FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "tunnelFdId", match);
167         FlowBuilder flowb = base().setId(flowid)
168             .setPriority(Integer.valueOf(150))
169             .setMatch(match)
170             .setInstructions(instructions(applyActionIns(fdReg), gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
171         return flowb.build();
172     }
173
174     private Flow createTunnelFlow(NodeConnectorId tunPort, EndpointFwdCtxOrdinals epFwdCtxOrds) {
175         // ... this is a remote node.
176
177         int egId = epFwdCtxOrds.getEpgId();
178         int bdId = epFwdCtxOrds.getBdId();
179         int fdId = epFwdCtxOrds.getFdId();
180         int l3Id = epFwdCtxOrds.getL3Id();
181         int tunnelId = epFwdCtxOrds.getTunnelId();
182
183         MatchBuilder mb = new MatchBuilder().setInPort(tunPort);
184         addNxTunIdMatch(mb, tunnelId);
185
186         Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
187         // set condition group register to all ones to
188         // bypass
189         // policy enforcement
190         /*
191          * TODO: This breaks distributed policy enforcement
192          * especially wrt multi-action. BAD. Must be addressed
193          * (this is why we can't have nice things).
194          * This can be fixed with new tunnelId ordinal in
195          * Ordinal Factory.
196          */
197         Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(0xffffff));
198         Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
199         Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
200         Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
201         Match match = mb.build();
202         FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "tunnel", match);
203         FlowBuilder flowb = base().setId(flowid)
204             .setPriority(Integer.valueOf(150))
205             .setMatch(match)
206             .setInstructions(
207                     instructions(applyActionIns(segReg, scgReg, bdReg, fdReg, vrfReg),
208                             gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
209         return flowb.build();
210     }
211
212     private void syncEP(OfWriter ofWriter, NodeId nodeId, Endpoint ep, NodeConnectorId ncId, EndpointFwdCtxOrdinals epFwdCtxOrds) throws Exception {
213
214         // TODO alagalah Li/Be: We should also match on EndpointL3 with the appropriate
215         // network containment. This would solve a lot of problems and prepare for EndpointL3 RPC.
216
217         int egId = epFwdCtxOrds.getEpgId();
218         int bdId = epFwdCtxOrds.getBdId();
219         int fdId = epFwdCtxOrds.getFdId();
220         int l3Id = epFwdCtxOrds.getL3Id();
221         int cgId = epFwdCtxOrds.getCgId();
222         int tunnelId = epFwdCtxOrds.getTunnelId();
223
224         Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
225         Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(cgId));
226         Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
227         Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
228         Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
229         Action tunIdAction = nxLoadTunIdAction(BigInteger.valueOf(tunnelId), false);
230
231         Match match = new MatchBuilder().setEthernetMatch(ethernetMatch(ep.getMacAddress(), null, null))
232                 .setInPort(ncId)
233                 .build();
234         FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "ep", match);
235         FlowBuilder flowb = base().setPriority(Integer.valueOf(100))
236             .setId(flowid)
237             .setMatch(match)
238             .setInstructions(
239                     instructions(applyActionIns(segReg, scgReg, bdReg, fdReg, vrfReg,tunIdAction),
240                             gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
241         ofWriter.writeFlow(nodeId, TABLE_ID, flowb.build());
242     }
243
244 }