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.concurrent.ExecutionException;
20 import com.google.common.base.Optional;
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.renderer.ofoverlay.OfContext;
24 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
25 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
26 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
27 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
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.Bucket;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
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 update(NodeId nodeId, PolicyInfo policyInfo, 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 sync(nodeId, policyInfo, ofWriter);
87 public void sync(NodeId nodeId, PolicyInfo policyInfo, OfWriter ofWriter) throws Exception {
89 for (Endpoint localEp : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
90 EndpointFwdCtxOrdinals localEpFwdCtxOrds =
91 OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, policyInfo, localEp);
92 if (localEpFwdCtxOrds == null) {
93 LOG.debug("getEndpointFwdCtxOrdinals is null for EP {}", localEp);
97 GroupId gid = new GroupId(Long.valueOf(localEpFwdCtxOrds.getFdId()));
98 if (!ofWriter.groupExists(nodeId, gid.getValue())) {
99 LOG.info("createGroup {} {}", nodeId, gid);
100 ofWriter.writeGroup(nodeId, gid);
103 for (EgKey epg : ctx.getEndpointManager().getGroupsForNode(nodeId)) {
105 // we'll use the fdId with the high bit set for remote bucket
106 // and just the local port number for local bucket
107 for (NodeId destNode : ctx.getEndpointManager().getNodesForGroup(epg)) {
108 if (nodeId.equals(destNode))
111 long bucketId = OrdinalFactory.getContextOrdinal(destNode);
112 bucketId |= 1L << 31;
114 IpAddress tunDst = ctx.getSwitchManager().getTunnelIP(destNode, TunnelTypeVxlan.class);
115 NodeConnectorId tunPort = ctx.getSwitchManager().getTunnelPort(nodeId, TunnelTypeVxlan.class);
116 if (tunDst == null || tunPort == null)
118 Action tundstAction = null;
119 if (tunDst.getIpv4Address() != null) {
120 String nextHop = tunDst.getIpv4Address().getValue();
121 tundstAction = nxLoadTunIPv4Action(nextHop, true);
123 LOG.error("IPv6 tunnel destination {} for {} not supported", tunDst.getIpv6Address().getValue(),
127 BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId)))
128 .setAction(actionList(tundstAction, outputAction(tunPort)));
130 ofWriter.writeBucket(nodeId, gid, bb.build());
132 OfOverlayContext ofc = localEp.getAugmentation(OfOverlayContext.class);
133 if (ofc == null || ofc.getNodeConnectorId() == null ||
134 (LocationType.External.equals(ofc.getLocationType())))
139 bucketId = getOfPortNum(ofc.getNodeConnectorId());
140 } catch (NumberFormatException e) {
141 LOG.warn("Could not parse port number {}", ofc.getNodeConnectorId(), e);
145 Action output = outputAction(ofc.getNodeConnectorId());
146 BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId)))
147 .setAction(actionList(output));
148 ofWriter.writeBucket(nodeId, gid, bb.build());