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