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.ArrayList;
18 import java.util.HashSet;
19 import java.util.List;
21 import java.util.concurrent.ExecutionException;
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;
49 import com.google.common.base.Optional;
52 * Manage the group tables for handling broadcast/multicast
55 public class GroupTable extends OfTable {
57 private static final Logger LOG = LoggerFactory.getLogger(GroupTable.class);
59 public GroupTable(OfContext ctx) {
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();
70 Optional<FlowCapableNode> r = t.read(LogicalDatastoreType.OPERATIONAL, fcniid).get();
72 LOG.warn("Node {} is not present", fcniid);
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);
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);
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(epg)) {
109 if (nodeId.equals(destNode))
112 if(isFloodDomainOnNode(localEpFwdCtxOrds.getFdId(), destNode)) {
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 // 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()))
141 bucketId = getOfPortNum(ofc.getNodeConnectorId());
142 } catch (NumberFormatException e) {
143 LOG.warn("Could not parse port number {}", ofc.getNodeConnectorId(), e);
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());
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());
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)) {
163 bucketId = getOfPortNum(extNcId);
164 } catch (NumberFormatException e) {
165 LOG.warn("Could not parse external port number {}", extNcId, e);
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)));
175 actionList.add(new ActionBuilder().setOrder(0).setAction(outputAction(extNcId)));
177 bb.setBucketId(new BucketId(Long.valueOf(bucketId))).setAction(
178 FlowUtils.actionList(actionList));
179 ofWriter.writeBucket(nodeId, gid, bb.build());
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
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));
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) {