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