2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
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;
17 import java.util.HashSet;
19 import java.util.concurrent.ExecutionException;
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;
43 import com.google.common.base.Optional;
46 * Manage the group tables for handling broadcast/multicast
49 public class GroupTable extends OfTable {
51 private static final Logger LOG = LoggerFactory.getLogger(GroupTable.class);
53 public GroupTable(OfContext ctx) {
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();
64 Optional<FlowCapableNode> r = t.read(LogicalDatastoreType.OPERATIONAL, fcniid).get();
66 LOG.warn("Node {} is not present", fcniid);
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);
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);
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);
98 for (EgKey epg : ctx.getEndpointManager().getGroupsForNode(nodeId)) {
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))
106 if(isFloodDomainOnNode(localEpFwdCtxOrds.getFdId(), destNode)) {
107 long bucketId = OrdinalFactory.getContextOrdinal(destNode);
108 bucketId |= 1L << 31;
110 IpAddress tunDst = ctx.getSwitchManager().getTunnelIP(destNode, TunnelTypeVxlan.class);
111 NodeConnectorId tunPort = ctx.getSwitchManager().getTunnelPort(nodeId, TunnelTypeVxlan.class);
112 if (tunDst == null || tunPort == null)
114 Action tundstAction = null;
115 if (tunDst.getIpv4Address() != null) {
116 String nextHop = tunDst.getIpv4Address().getValue();
117 tundstAction = nxLoadTunIPv4Action(nextHop, true);
119 LOG.error("IPv6 tunnel destination {} for {} not supported", tunDst.getIpv6Address().getValue(),
123 BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId)))
124 .setAction(actionList(tundstAction, outputAction(tunPort)));
125 ofWriter.writeBucket(nodeId, gid, bb.build());
128 OfOverlayContext ofc = localEp.getAugmentation(OfOverlayContext.class);
129 if (EndpointManager.isExternal(localEp, ctx.getTenant(localEp.getTenant()).getExternalImplicitGroups()))
134 bucketId = getOfPortNum(ofc.getNodeConnectorId());
135 } catch (NumberFormatException e) {
136 LOG.warn("Could not parse port number {}", ofc.getNodeConnectorId(), e);
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());
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
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));
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) {