Merge "Bug 5326 - endpoint can reach nodes outside internal domain"
[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.ArrayList;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.concurrent.ExecutionException;
22
23 import org.antlr.v4.runtime.misc.Array2DHashSet;
24 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.groupbasedpolicy.dto.EgKey;
27 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
28 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
29 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
30 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.Segmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 import com.google.common.base.Optional;
50
51 /**
52  * Manage the group tables for handling broadcast/multicast
53  */
54
55 public class GroupTable extends OfTable {
56
57     private static final Logger LOG = LoggerFactory.getLogger(GroupTable.class);
58
59     public GroupTable(OfContext ctx) {
60         super(ctx);
61     }
62
63     FlowCapableNode getFCNodeFromDatastore(NodeId nodeId)
64             throws ExecutionException, InterruptedException {
65         FlowCapableNode fcn = null;
66         ReadOnlyTransaction t = ctx.getDataBroker().newReadOnlyTransaction();
67         InstanceIdentifier<FlowCapableNode> fcniid = createNodePath(nodeId).builder()
68                 .augmentation(FlowCapableNode.class).build();
69
70         Optional<FlowCapableNode> r = t.read(LogicalDatastoreType.OPERATIONAL, fcniid).get();
71         if (!r.isPresent()) {
72             LOG.warn("Node {} is not present", fcniid);
73             return null;
74         }
75         fcn = r.get();
76         t.close();
77         return fcn;
78     }
79
80     @Override
81     public void sync(NodeId nodeId, OfWriter ofWriter) throws Exception {
82         // there appears to be no way of getting only the existing group
83         // tables unfortunately, so we have to get the whole goddamned node.
84         // Since this is happening concurrently with other things that are
85         // working in subtrees of nodes, we have to do two transactions
86         FlowCapableNode fcn = getFCNodeFromDatastore(nodeId);
87         if (fcn == null)
88             return;
89
90         for (Endpoint localEp : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
91             EndpointFwdCtxOrdinals localEpFwdCtxOrds =
92                     OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, localEp);
93             if (localEpFwdCtxOrds == null) {
94                 LOG.info("getEndpointFwdCtxOrdinals is null for EP {}", localEp);
95                 continue;
96             }
97
98             GroupId gid = new GroupId(Long.valueOf(localEpFwdCtxOrds.getFdId()));
99             if (!ofWriter.groupExists(nodeId, gid.getValue())) {
100                 LOG.info("createGroup {} {}", nodeId, gid);
101                 ofWriter.writeGroup(nodeId, gid);
102             }
103
104             for (EgKey epg : ctx.getEndpointManager().getGroupsForNode(nodeId)) {
105
106                 // we'll use the fdId with the high bit set for remote bucket
107                 // and just the local port number for local bucket
108                 for (NodeId destNode : findPeerNodesForGroup(epg)) {
109                     if (nodeId.equals(destNode))
110                         continue;
111
112                     if(isFloodDomainOnNode(localEpFwdCtxOrds.getFdId(), destNode)) {
113                         long bucketId = OrdinalFactory.getContextOrdinal(destNode);
114                         bucketId |= 1L << 31;
115
116                         IpAddress tunDst = ctx.getSwitchManager().getTunnelIP(destNode, TunnelTypeVxlan.class);
117                         NodeConnectorId tunPort = ctx.getSwitchManager().getTunnelPort(nodeId, TunnelTypeVxlan.class);
118                         if (tunDst == null || tunPort == null)
119                             continue;
120                         Action tundstAction = null;
121                         if (tunDst.getIpv4Address() != null) {
122                             String nextHop = tunDst.getIpv4Address().getValue();
123                             tundstAction = nxLoadTunIPv4Action(nextHop, true);
124                         } else {
125                             LOG.error("IPv6 tunnel destination {} for {} not supported", tunDst.getIpv6Address().getValue(),
126                                     destNode);
127                             continue;
128                         }
129                         BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId)))
130                                 .setAction(actionList(tundstAction, outputAction(tunPort)));
131                         ofWriter.writeBucket(nodeId, gid, bb.build());
132                     }
133                 }
134                 // TODO broadcasts are not separated by EPG between endpoints on the same node
135                 OfOverlayContext ofc = localEp.getAugmentation(OfOverlayContext.class);
136                 if (EndpointManager.isExternal(localEp, ctx.getTenant(localEp.getTenant()).getExternalImplicitGroups()))
137                     continue;
138
139                 long bucketId;
140                 try {
141                     bucketId = getOfPortNum(ofc.getNodeConnectorId());
142                 } catch (NumberFormatException e) {
143                     LOG.warn("Could not parse port number {}", ofc.getNodeConnectorId(), e);
144                     continue;
145                 }
146                 Action output = outputAction(ofc.getNodeConnectorId());
147                 BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId))).setAction(
148                         FlowUtils.actionList(output));
149                 ofWriter.writeBucket(nodeId, gid, bb.build());
150
151                 // if boradcast exceeds internal domain
152                 for (Endpoint extEp : ctx.getEndpointManager().getExtEpsNoLocForGroup(epg)) {
153                     if (extEp.getNetworkContainment() != null
154                             && extEp.getNetworkContainment().equals(localEp.getNetworkContainment())) {
155                         L2FloodDomain l2Fd = ctx.getTenant(extEp.getTenant()).resolveL2FloodDomain(
156                                 extEp.getNetworkContainment());
157                         if (l2Fd != null) {
158                             Segmentation segmentation = l2Fd.getAugmentation(Segmentation.class);
159                             // external endpoints do not have location augmentation
160                             // however they are beyond external ports
161                             for (NodeConnectorId extNcId : ctx.getSwitchManager().getExternalPorts(nodeId)) {
162                                 try {
163                                     bucketId = getOfPortNum(extNcId);
164                                 } catch (NumberFormatException e) {
165                                     LOG.warn("Could not parse external port number {}", extNcId, e);
166                                     continue;
167                                 }
168                                 ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder>
169                                     actionList = new ArrayList<>();
170                                 if (segmentation != null) {
171                                     Integer vlanId = segmentation.getSegmentationId();
172                                     actionList.addAll(FlowUtils.pushVlanActions(vlanId));
173                                     actionList.add(new ActionBuilder().setOrder(2).setAction(outputAction(extNcId)));
174                                 } else {
175                                     actionList.add(new ActionBuilder().setOrder(0).setAction(outputAction(extNcId)));
176                                 }
177                                 bb.setBucketId(new BucketId(Long.valueOf(bucketId))).setAction(
178                                         FlowUtils.actionList(actionList));
179                                 ofWriter.writeBucket(nodeId, gid, bb.build());
180                             }
181                         }
182                     }
183                 }
184             }
185         }
186     }
187
188     /**
189      * @param sourceEpgKey a key of source group
190      * @return all the nodes on which endpoints are either in groups that have policy with source
191      *         group, or are in the source group
192      */
193     private Set<NodeId> findPeerNodesForGroup(EgKey sourceEpgKey) {
194         Set<NodeId> nodes = new HashSet<NodeId>();
195         nodes.addAll(ctx.getEndpointManager().getNodesForGroup(sourceEpgKey));
196         for (EgKey dstEpgs : ctx.getCurrentPolicy().getPeers(sourceEpgKey)) {
197             nodes.addAll(ctx.getEndpointManager().getNodesForGroup(dstEpgs));
198         }
199         return nodes;
200     }
201
202     private boolean isFloodDomainOnNode(int fdId, NodeId node) throws Exception {
203         for (Endpoint ep : ctx.getEndpointManager().getEndpointsForNode(node)) {
204             int epFdId = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, ep).getFdId();
205             if (fdId == epFdId) {
206                 return true;
207             }
208         }
209         return false;
210     }
211 }