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 com.google.common.base.Optional;
23 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
26 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
27 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
28 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
29 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Manage the group tables for handling broadcast/multicast
50 public class GroupTable extends OfTable {
52 private static final Logger LOG = LoggerFactory.getLogger(GroupTable.class);
54 public GroupTable(OfContext ctx) {
58 FlowCapableNode getFCNodeFromDatastore(NodeId nodeId)
59 throws ExecutionException, InterruptedException {
60 FlowCapableNode fcn = null;
61 ReadOnlyTransaction t = ctx.getDataBroker().newReadOnlyTransaction();
62 InstanceIdentifier<FlowCapableNode> fcniid = createNodePath(nodeId).builder()
63 .augmentation(FlowCapableNode.class).build();
65 Optional<FlowCapableNode> r = t.read(LogicalDatastoreType.OPERATIONAL, fcniid).get();
67 LOG.warn("Node {} is not present", fcniid);
76 public void update(NodeId nodeId, PolicyInfo policyInfo, OfWriter ofWriter) throws Exception {
77 // there appears to be no way of getting only the existing group
78 // tables unfortunately, so we have to get the whole goddamned node.
79 // Since this is happening concurrently with other things that are
80 // working in subtrees of nodes, we have to do two transactions
81 FlowCapableNode fcn = getFCNodeFromDatastore(nodeId);
85 sync(nodeId, policyInfo, ofWriter);
88 public void sync(NodeId nodeId, PolicyInfo policyInfo, OfWriter ofWriter) throws Exception {
90 for (Endpoint localEp : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
91 EndpointFwdCtxOrdinals localEpFwdCtxOrds =
92 OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, policyInfo, localEp);
93 if (localEpFwdCtxOrds == null) {
94 LOG.debug("getEndpointFwdCtxOrdinals is null for EP {}", localEp);
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);
104 for (EgKey epg : ctx.getEndpointManager().getGroupsForNode(nodeId)) {
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(policyInfo, epg)) {
109 if (nodeId.equals(destNode))
112 if(isFloodDomainOnNode(localEpFwdCtxOrds.getFdId(), destNode, policyInfo)) {
113 long bucketId = OrdinalFactory.getContextOrdinal(destNode);
114 bucketId |= 1L << 31;
116 IpAddress tunDst = ctx.getSwitchManager().getTunnelIP(destNode, TunnelTypeVxlan.class);
117 NodeConnectorId tunPort = ctx.getSwitchManager().getTunnelPort(nodeId, TunnelTypeVxlan.class);
118 if (tunDst == null || tunPort == null)
120 Action tundstAction = null;
121 if (tunDst.getIpv4Address() != null) {
122 String nextHop = tunDst.getIpv4Address().getValue();
123 tundstAction = nxLoadTunIPv4Action(nextHop, true);
125 LOG.error("IPv6 tunnel destination {} for {} not supported", tunDst.getIpv6Address().getValue(),
129 BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId)))
130 .setAction(actionList(tundstAction, outputAction(tunPort)));
131 ofWriter.writeBucket(nodeId, gid, bb.build());
134 OfOverlayContext ofc = localEp.getAugmentation(OfOverlayContext.class);
135 if (ofc == null || ofc.getNodeConnectorId() == null ||
136 (LocationType.External.equals(ofc.getLocationType())))
141 bucketId = getOfPortNum(ofc.getNodeConnectorId());
142 } catch (NumberFormatException e) {
143 LOG.warn("Could not parse port number {}", ofc.getNodeConnectorId(), e);
147 Action output = outputAction(ofc.getNodeConnectorId());
148 BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId)))
149 .setAction(actionList(output));
150 ofWriter.writeBucket(nodeId, gid, bb.build());
156 * @param policyInfo to read peer groups for a group
157 * @param sourceEpgKey a key of source group
158 * @return all the nodes on which endpoints are either in groups that have policy with source
159 * group, or are in the source group
161 private Set<NodeId> findPeerNodesForGroup(PolicyInfo policyInfo, EgKey sourceEpgKey) {
162 Set<NodeId> nodes = new HashSet<NodeId>();
163 nodes.addAll(ctx.getEndpointManager().getNodesForGroup(sourceEpgKey));
164 for (EgKey dstEpgs : policyInfo.getPeers(sourceEpgKey)) {
165 nodes.addAll(ctx.getEndpointManager().getNodesForGroup(dstEpgs));
170 private boolean isFloodDomainOnNode(int fdId, NodeId node, PolicyInfo policyInfo) throws Exception {
171 for (Endpoint ep : ctx.getEndpointManager().getEndpointsForNode(node)) {
172 int epFdId = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, policyInfo, ep).getFdId();
173 if (fdId == epFdId) {