Fixed NAT in OFOverlay based on EIG
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / GroupTable.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.actionList;
12 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.createNodePath;
13 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.getOfPortNum;
14 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadTunIPv4Action;
15 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.outputAction;
16
17 import java.util.HashSet;
18 import java.util.Set;
19 import java.util.concurrent.ExecutionException;
20
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.groupbasedpolicy.dto.EgKey;
24 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
25 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
26 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
27 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.google.common.base.Optional;
44
45 /**
46  * Manage the group tables for handling broadcast/multicast
47  */
48
49 public class GroupTable extends OfTable {
50
51     private static final Logger LOG = LoggerFactory.getLogger(GroupTable.class);
52
53     public GroupTable(OfContext ctx) {
54         super(ctx);
55     }
56
57     FlowCapableNode getFCNodeFromDatastore(NodeId nodeId)
58             throws ExecutionException, InterruptedException {
59         FlowCapableNode fcn = null;
60         ReadOnlyTransaction t = ctx.getDataBroker().newReadOnlyTransaction();
61         InstanceIdentifier<FlowCapableNode> fcniid = createNodePath(nodeId).builder()
62                 .augmentation(FlowCapableNode.class).build();
63
64         Optional<FlowCapableNode> r = t.read(LogicalDatastoreType.OPERATIONAL, fcniid).get();
65         if (!r.isPresent()) {
66             LOG.warn("Node {} is not present", fcniid);
67             return null;
68         }
69         fcn = r.get();
70         t.close();
71         return fcn;
72     }
73
74     @Override
75     public void sync(NodeId nodeId, OfWriter ofWriter) throws Exception {
76         // there appears to be no way of getting only the existing group
77         // tables unfortunately, so we have to get the whole goddamned node.
78         // Since this is happening concurrently with other things that are
79         // working in subtrees of nodes, we have to do two transactions
80         FlowCapableNode fcn = getFCNodeFromDatastore(nodeId);
81         if (fcn == null)
82             return;
83
84         for (Endpoint localEp : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
85             EndpointFwdCtxOrdinals localEpFwdCtxOrds =
86                     OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, localEp);
87             if (localEpFwdCtxOrds == null) {
88                 LOG.debug("getEndpointFwdCtxOrdinals is null for EP {}", localEp);
89                 continue;
90             }
91
92             GroupId gid = new GroupId(Long.valueOf(localEpFwdCtxOrds.getFdId()));
93             if (!ofWriter.groupExists(nodeId, gid.getValue())) {
94                 LOG.info("createGroup {} {}", nodeId, gid);
95                 ofWriter.writeGroup(nodeId, gid);
96             }
97
98             for (EgKey epg : ctx.getEndpointManager().getGroupsForNode(nodeId)) {
99
100                 // we'll use the fdId with the high bit set for remote bucket
101                 // and just the local port number for local bucket
102                 for (NodeId destNode : findPeerNodesForGroup(epg)) {
103                     if (nodeId.equals(destNode))
104                         continue;
105
106                     if(isFloodDomainOnNode(localEpFwdCtxOrds.getFdId(), destNode)) {
107                         long bucketId = OrdinalFactory.getContextOrdinal(destNode);
108                         bucketId |= 1L << 31;
109
110                         IpAddress tunDst = ctx.getSwitchManager().getTunnelIP(destNode, TunnelTypeVxlan.class);
111                         NodeConnectorId tunPort = ctx.getSwitchManager().getTunnelPort(nodeId, TunnelTypeVxlan.class);
112                         if (tunDst == null || tunPort == null)
113                             continue;
114                         Action tundstAction = null;
115                         if (tunDst.getIpv4Address() != null) {
116                             String nextHop = tunDst.getIpv4Address().getValue();
117                             tundstAction = nxLoadTunIPv4Action(nextHop, true);
118                         } else {
119                             LOG.error("IPv6 tunnel destination {} for {} not supported", tunDst.getIpv6Address().getValue(),
120                                     destNode);
121                             continue;
122                         }
123                         BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId)))
124                                 .setAction(actionList(tundstAction, outputAction(tunPort)));
125                         ofWriter.writeBucket(nodeId, gid, bb.build());
126                     }
127                 }
128                 OfOverlayContext ofc = localEp.getAugmentation(OfOverlayContext.class);
129                 if (EndpointManager.isExternal(localEp, ctx.getTenant(localEp.getTenant()).getExternalImplicitGroups()))
130                     continue;
131
132                 long bucketId;
133                 try {
134                     bucketId = getOfPortNum(ofc.getNodeConnectorId());
135                 } catch (NumberFormatException e) {
136                     LOG.warn("Could not parse port number {}", ofc.getNodeConnectorId(), e);
137                     continue;
138                 }
139
140                 Action output = outputAction(ofc.getNodeConnectorId());
141                 BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId)))
142                         .setAction(actionList(output));
143                 ofWriter.writeBucket(nodeId, gid, bb.build());
144             }
145         }
146     }
147
148     /**
149      * @param sourceEpgKey a key of source group
150      * @return all the nodes on which endpoints are either in groups that have policy with source
151      *         group, or are in the source group
152      */
153     private Set<NodeId> findPeerNodesForGroup(EgKey sourceEpgKey) {
154         Set<NodeId> nodes = new HashSet<NodeId>();
155         nodes.addAll(ctx.getEndpointManager().getNodesForGroup(sourceEpgKey));
156         for (EgKey dstEpgs : ctx.getCurrentPolicy().getPeers(sourceEpgKey)) {
157             nodes.addAll(ctx.getEndpointManager().getNodesForGroup(dstEpgs));
158         }
159         return nodes;
160     }
161
162     private boolean isFloodDomainOnNode(int fdId, NodeId node) throws Exception {
163         for (Endpoint ep : ctx.getEndpointManager().getEndpointsForNode(node)) {
164             int epFdId = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, ep).getFdId();
165             if (fdId == epFdId) {
166                 return true;
167             }
168         }
169         return false;
170     }
171 }