Merge "Tests for neutron-ovsdb"
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / mapper / egressnat / EgressNatMapper.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.mapper.egressnat;
10
11 import org.opendaylight.groupbasedpolicy.dto.EpKey;
12 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
13 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
14 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowTable;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTable;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
23
24 import java.util.Collection;
25
26 /**
27  * <h1>Manage the table that assigns source endpoint group, bridge domain, and
28  * router domain to registers to be used by other tables</h1>
29  *
30  * <i>NAT flow</i><br>
31  * Priority = 100<br>
32  * Matches:<br>
33  *      - ipv4/ipv6 inside address<br>
34  *      - ethernet type<br>
35  *      - Reg6 {@link NxmNxReg6}<br>
36  * Actions:<br>
37  *      - set_src ip address<br>
38  *      - {@link GoToTable} EXTERNAL MAPPER table<br>
39  */
40 public class EgressNatMapper extends FlowTable {
41     // Priorities
42     private static final Integer DROP = 1;
43     private static final Integer NAT = 100;
44     private final short tableId;
45
46     public EgressNatMapper(OfContext ctx, short tableId) {
47         super(ctx);
48         this.tableId = tableId;
49     }
50
51     @Override
52     public short getTableId() {
53         return tableId;
54     }
55
56     @Override
57     public void sync(Endpoint endpoint, OfWriter ofWriter) throws Exception {
58         NodeId endpointNodeId = ctx.getEndpointManager().getEndpointNodeId(endpoint);
59         if (endpointNodeId == null) {
60             LOG.warn("Endpoint {} has no location specified, skipped", endpoint);
61             return;
62         }
63         syncFlows(new EgressNatMapperFlows(endpointNodeId, tableId), endpoint, ofWriter);
64     }
65
66     void syncFlows(EgressNatMapperFlows flows, Endpoint endpoint, OfWriter ofWriter) {
67
68         // Drop
69         flows.dropFlow(DROP, null, ofWriter);
70
71         // NAT flows
72         short externalMapperId = ctx.getPolicyManager().getTABLEID_EXTERNAL_MAPPER();
73         Collection<EndpointL3> l3Endpoints = ctx.getEndpointManager().getL3EndpointsWithNat();
74         EndpointKey endpointKey = endpoint.getKey();
75         for (EndpointL3 l3Endpoint : l3Endpoints) {
76             L2BridgeDomainId l2Context = l3Endpoint.getL2Context();
77             MacAddress macAddress = l3Endpoint.getMacAddress();
78             if (l2Context != null && macAddress != null) {
79                 Endpoint l2EpFromL3Ep = ctx.getEndpointManager().getEndpoint(new EpKey(l2Context, macAddress));
80                 if(endpointKey.equals(l2EpFromL3Ep.getKey())) {
81                     flows.natFlows(externalMapperId, l3Endpoint, NAT, ofWriter);
82                     // L3 Endpoint found, end of loop
83                     break;
84                 }
85             }
86         }
87     }
88 }