Merge "Scripts and Vagrantfile to run sFlow on gbp1"
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / IngressNatMapper.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.ARP;
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.nxLoadArpOpAction;
17 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadArpShaAction;
18 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadArpSpaAction;
19 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadRegAction;
20 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadTunIdAction;
21 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxMoveArpShaToArpThaAction;
22 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxMoveArpSpaToArpTpaAction;
23 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxMoveEthSrcToEthDstAction;
24 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.outputAction;
25 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.setDlDstAction;
26 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.setDlSrcAction;
27 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.setIpv4DstAction;
28 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.setIpv6DstAction;
29
30 import java.math.BigInteger;
31 import java.util.Collection;
32
33 import org.opendaylight.groupbasedpolicy.dto.EpKey;
34 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
35 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
36 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.l3endpoint.rev151217.NatAddress;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 /**
64  * Manage the table that assigns source endpoint group, bridge domain, and
65  * router domain to registers to be used by other tables.
66  */
67 public class IngressNatMapper extends FlowTable {
68
69     protected static final Logger LOG = LoggerFactory.getLogger(IngressNatMapper.class);
70
71     // TODO Li alagalah Improve UT coverage for this class.
72     public static short TABLE_ID;
73
74     public IngressNatMapper(OfContext ctx, short tableId) {
75         super(ctx);
76         TABLE_ID = tableId;
77     }
78
79     @Override
80     public short getTableId() {
81         return TABLE_ID;
82     }
83
84     @Override
85     public void sync(NodeId nodeId, OfWriter ofWriter) throws Exception {
86
87         ofWriter.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(1), null, TABLE_ID));
88
89         // TODO Bug 3546 - Difficult: External port is unrelated to Tenant, L3C, L2BD..
90
91         Collection<Endpoint> endpointsForNode = ctx.getEndpointManager().getEndpointsForNode(nodeId);
92         Collection<EndpointL3> l3Endpoints = ctx.getEndpointManager().getL3EndpointsWithNat();
93         for (EndpointL3 l3Ep : l3Endpoints) {
94             if (l3Ep.getL2Context() != null && l3Ep.getMacAddress() !=null ) {
95                 Endpoint ep = ctx.getEndpointManager().getEndpoint(new EpKey(l3Ep.getL2Context(), l3Ep.getMacAddress()));
96                 if (endpointsForNode.contains(ep)) {
97                     createNatFlow(l3Ep, nodeId, ofWriter);
98                 }
99             }
100         }
101     }
102
103     private void createNatFlow(EndpointL3 l3Ep, NodeId nodeId, OfWriter ofWriter) throws Exception {
104         NatAddress natAugL3Endpoint = l3Ep.getAugmentation(NatAddress.class);
105         // Match on L3 Nat Augmentation in Destination, set to IPAddress/Mac, send to SourceMapper
106         if (natAugL3Endpoint == null) {
107             return;
108         }
109         Endpoint ep = ctx.getEndpointManager().getEndpoint(new EpKey(l3Ep.getL2Context(), l3Ep.getMacAddress()));
110         EndpointFwdCtxOrdinals epFwdCtxOrds = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, ep);
111         if (epFwdCtxOrds == null) {
112             LOG.info("getEndpointFwdCtxOrdinals is null for EP {}", ep);
113             return;
114         }
115         Flow flow = buildNatFlow(natAugL3Endpoint.getNatAddress(), l3Ep.getIpAddress(), l3Ep.getMacAddress(), epFwdCtxOrds);
116         if (flow != null) {
117             ofWriter.writeFlow(nodeId, TABLE_ID, flow);
118         }
119         flow = createOutsideArpFlow(natAugL3Endpoint.getNatAddress(), l3Ep.getMacAddress(), nodeId);
120         if (flow != null) {
121             ofWriter.writeFlow(nodeId, TABLE_ID, flow);
122         }
123     }
124
125     private Flow buildNatFlow(IpAddress outsideDestAddress, IpAddress insideDestAddress, MacAddress toMac,
126             EndpointFwdCtxOrdinals epFwdCtxOrds) {
127         // TODO Auto-generated method stub
128         MatchBuilder mb = new MatchBuilder();
129         Action setDestIp;
130         String outsideIpMatch;
131         Layer3Match m;
132
133         Action setDestMac = setDlDstAction(toMac);
134         FlowId flowid = new FlowId(new StringBuilder().append("IngressNat")
135             .append("|")
136             .append(outsideDestAddress)
137             .append("|")
138             .append(insideDestAddress)
139             .append("|")
140             .append(toMac)
141             .toString());
142         if (insideDestAddress.getIpv4Address() != null) {
143             setDestIp = setIpv4DstAction(insideDestAddress.getIpv4Address());
144
145             outsideIpMatch = outsideDestAddress.getIpv4Address().getValue() + "/32";
146             m = new Ipv4MatchBuilder().setIpv4Destination(new Ipv4Prefix(outsideIpMatch)).build();
147             mb.setEthernetMatch(ethernetMatch(null, null, FlowUtils.IPv4)).setLayer3Match(m);
148         } else if (insideDestAddress.getIpv6Address() != null) {
149             setDestIp = setIpv6DstAction(insideDestAddress.getIpv6Address());
150             outsideIpMatch = outsideDestAddress.getIpv6Address().getValue() + "/128";
151             m = new Ipv6MatchBuilder().setIpv6Destination(new Ipv6Prefix(outsideIpMatch)).build();
152             mb.setEthernetMatch(ethernetMatch(null, null, FlowUtils.IPv6)).setLayer3Match(m);
153         } else {
154             return null;
155         }
156
157         int egId = epFwdCtxOrds.getEpgId();
158         int bdId = epFwdCtxOrds.getBdId();
159         int fdId = epFwdCtxOrds.getFdId();
160         int l3Id = epFwdCtxOrds.getL3Id();
161         int cgId = epFwdCtxOrds.getCgId();
162         int tunnelId = epFwdCtxOrds.getTunnelId();
163         Action segReg = nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(egId));
164         Action scgReg = nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(cgId));
165         Action bdReg = nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(bdId));
166         Action fdReg = nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(fdId));
167         Action vrfReg = nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(l3Id));
168         Action tunIdAction = nxLoadTunIdAction(BigInteger.valueOf(tunnelId), false);
169
170         FlowBuilder flowb = base().setPriority(Integer.valueOf(100))
171             .setId(flowid)
172             .setMatch(mb.build())
173             .setInstructions(
174                     instructions(
175                             applyActionIns(setDestIp, setDestMac, segReg, scgReg, bdReg, fdReg, vrfReg, tunIdAction),
176                             gotoTableIns(ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER())));
177         return flowb.build();
178     }
179
180     private Flow createOutsideArpFlow(IpAddress outsideDestAddress, MacAddress toMac, NodeId nodeId) {
181
182         String ikey = outsideDestAddress.getIpv4Address().getValue();
183         BigInteger intMac = new BigInteger(1, bytesFromHexString(toMac.getValue()));
184
185         FlowId flowId = new FlowId(new StringBuffer().append("outside-ip-arp|").append(ikey).toString());
186         MatchBuilder mb = new MatchBuilder().setEthernetMatch(ethernetMatch(null, null, ARP)).setLayer3Match(
187                 new ArpMatchBuilder().setArpOp(Integer.valueOf(1))
188                     .setArpTargetTransportAddress(new Ipv4Prefix(ikey + "/32"))
189                     .build());
190
191         FlowBuilder flowb = base().setPriority(150)
192             .setId(flowId)
193             .setMatch(mb.build())
194             .setInstructions(
195                     instructions(applyActionIns(nxMoveEthSrcToEthDstAction(), setDlSrcAction(toMac),
196                             nxLoadArpOpAction(BigInteger.valueOf(2L)), nxMoveArpShaToArpThaAction(),
197                             nxLoadArpShaAction(intMac), nxMoveArpSpaToArpTpaAction(), nxLoadArpSpaAction(ikey),
198                             outputAction(new NodeConnectorId(nodeId.getValue() + ":INPORT")))));
199         return flowb.build();
200     }
201
202     static byte[] bytesFromHexString(String values) {
203         String target = "";
204         if (values != null) {
205             target = values;
206         }
207         String[] octets = target.split(":");
208
209         byte[] ret = new byte[octets.length];
210         for (int i = 0; i < octets.length; i++) {
211             ret[i] = Integer.valueOf(octets[i], 16).byteValue();
212         }
213         return ret;
214     }
215 }