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