2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
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;
19 import java.math.BigInteger;
20 import java.util.Collections;
21 import java.util.HashSet;
24 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
25 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;
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.MatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 import com.google.common.collect.Sets;
53 * Manage the table that assigns source endpoint group, bridge domain, and
54 * router domain to registers to be used by other tables.
56 public class SourceMapper extends FlowTable {
58 protected static final Logger LOG = LoggerFactory.getLogger(SourceMapper.class);
60 // TODO Li alagalah Improve UT coverage for this class.
61 public static final short TABLE_ID = 1;
63 public SourceMapper(OfContext ctx) {
68 public short getTableId() {
73 public void sync(NodeId nodeId, PolicyInfo policyInfo, FlowMap flowMap) throws Exception {
75 flowMap.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(1), null));
77 for (Endpoint ep : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
78 OfOverlayContext ofc = ep.getAugmentation(OfOverlayContext.class);
79 if (ofc != null && ofc.getNodeConnectorId() != null
80 && (ofc.getLocationType() == null || LocationType.Internal.equals(ofc.getLocationType()))
81 && ep.getTenant() != null && (ep.getEndpointGroup() != null || ep.getEndpointGroups() != null)) {
83 IndexedTenant tenant = ctx.getPolicyResolver().getTenant(ep.getTenant());
87 EndpointFwdCtxOrdinals epFwdCtxOrds = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, policyInfo, ep);
88 EgKey sepg = new EgKey(ep.getTenant(), ep.getEndpointGroup());
90 createRemoteTunnels(flowMap, nodeId, ep, policyInfo, epFwdCtxOrds);
93 * Sync the local EP information.
95 syncEP(flowMap, policyInfo, nodeId, ep, ofc, sepg, epFwdCtxOrds);
100 private void createRemoteTunnels(FlowMap flowMap, NodeId nodeId, Endpoint ep, PolicyInfo policyInfo,
101 EndpointFwdCtxOrdinals epFwdCtxOrds) throws Exception {
102 Set<EgKey> epgs = new HashSet<>();
104 // Get EPGs and add to Set to remove duplicates
105 // TODO alagalah Li: test EndpointManager.getEgKeys
106 if (ep.getEndpointGroup() != null) {
107 epgs.add(new EgKey(ep.getTenant(), ep.getEndpointGroup()));
109 if (ep.getEndpointGroups() != null) {
110 for (EndpointGroupId epgId : ep.getEndpointGroups()) {
111 epgs.add(new EgKey(ep.getTenant(), epgId));
115 // Create tunnels on remote Nodes that may talk to us.
116 for (EgKey epg : epgs) {
117 Set<EgKey> peers = Sets.union(Collections.singleton(epg), policyInfo.getPeers(epg));
118 for (EgKey peer : peers) {
119 for (NodeId remoteNodeId : ctx.getEndpointManager().getNodesForGroup(peer)) {
121 // Please do not check for remote v local nodeID, we need local to local tunnels
122 // in the case of chaining - The Great Dr Sunal.
123 NodeConnectorId tunPort = ctx.getSwitchManager().getTunnelPort(remoteNodeId, TunnelTypeVxlan.class);
124 if (tunPort == null) {
125 LOG.trace("No tunnel port for tunnel in SourceMapper between local:{} and remote:{}",
126 nodeId.getValue(), remoteNodeId.getValue());
129 flowMap.writeFlow(remoteNodeId, TABLE_ID, createTunnelFlow(tunPort, epFwdCtxOrds));
130 flowMap.writeFlow(remoteNodeId, TABLE_ID, createBroadcastFlow(tunPort, epFwdCtxOrds));
136 private Flow createBroadcastFlow(NodeConnectorId tunPort, EndpointFwdCtxOrdinals epFwdCtxOrds) {
138 int fdId = epFwdCtxOrds.getFdId();
140 FlowId flowid = new FlowId(new StringBuilder().append(tunPort.getValue())
146 MatchBuilder mb = new MatchBuilder().setInPort(tunPort);
147 addNxTunIdMatch(mb, fdId);
149 // set condition group register to all ones to
151 // policy enforcement
153 * TODO: This breaks distributed policy enforcement
154 * especially wrt multi-action. BAD. Must be addressed
155 * (this is why we can't have nice things).
156 * This can be fixed with new tunnelId ordinal in
160 Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
162 FlowBuilder flowb = base().setId(flowid)
163 .setPriority(Integer.valueOf(150))
164 .setMatch(mb.build())
165 .setInstructions(instructions(applyActionIns(fdReg), gotoTableIns((short) (TABLE_ID + 1))));
166 return flowb.build();
169 private Flow createTunnelFlow(NodeConnectorId tunPort, EndpointFwdCtxOrdinals epFwdCtxOrds) {
170 // ... this is a remote node.
172 int egId = epFwdCtxOrds.getEpgId();
173 int bdId = epFwdCtxOrds.getBdId();
174 int fdId = epFwdCtxOrds.getFdId();
175 int l3Id = epFwdCtxOrds.getL3Id();
176 int tunnelId = epFwdCtxOrds.getTunnelId();
178 FlowId flowid = new FlowId(new StringBuilder().append(tunPort.getValue())
191 MatchBuilder mb = new MatchBuilder().setInPort(tunPort);
192 addNxTunIdMatch(mb, tunnelId);
193 Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
194 // set condition group register to all ones to
196 // policy enforcement
198 * TODO: This breaks distributed policy enforcement
199 * especially wrt multi-action. BAD. Must be addressed
200 * (this is why we can't have nice things).
201 * This can be fixed with new tunnelId ordinal in
204 Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(0xffffff));
205 Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
206 Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
207 Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
208 FlowBuilder flowb = base().setId(flowid)
209 .setPriority(Integer.valueOf(150))
210 .setMatch(mb.build())
212 instructions(applyActionIns(segReg, scgReg, bdReg, fdReg, vrfReg),
213 gotoTableIns((short) (TABLE_ID + 1))));
214 return flowb.build();
217 private void syncEP(FlowMap flowMap, PolicyInfo policyInfo, NodeId nodeId, Endpoint ep, OfOverlayContext ofc,
218 EgKey egKey, EndpointFwdCtxOrdinals epFwdCtxOrds) throws Exception {
220 // TODO alagalah Li/Be: We should also match on EndpointL3 with the appropriate
221 // network containment. This would solve a lot of problems and prepare for EndpointL3 RPC.
223 int egId = epFwdCtxOrds.getEpgId();
224 int bdId = epFwdCtxOrds.getBdId();
225 int fdId = epFwdCtxOrds.getFdId();
226 int l3Id = epFwdCtxOrds.getL3Id();
227 int cgId = epFwdCtxOrds.getCgId();
228 int tunnelId = epFwdCtxOrds.getTunnelId();
230 FlowId flowid = new FlowId(new StringBuilder().append(ofc.getNodeConnectorId().getValue())
232 .append(ep.getMacAddress().getValue())
244 Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
245 Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(cgId));
246 Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
247 Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
248 Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
249 Action tunIdAction = nxLoadTunIdAction(BigInteger.valueOf(tunnelId), false);
251 FlowBuilder flowb = base().setPriority(Integer.valueOf(100))
254 new MatchBuilder().setEthernetMatch(ethernetMatch(ep.getMacAddress(), null, null))
255 .setInPort(ofc.getNodeConnectorId())
258 instructions(applyActionIns(segReg, scgReg, bdReg, fdReg, vrfReg,tunIdAction),
259 gotoTableIns((short) (TABLE_ID + 1))));
260 flowMap.writeFlow(nodeId, TABLE_ID, flowb.build());