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.createBucketPath;
13 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.createGroupPath;
14 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.createNodePath;
15 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.getOfPortNum;
16 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadTunIPv4Action;
17 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.outputAction;
19 import java.util.ArrayList;
20 import java.util.HashMap;
23 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
27 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;
28 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
29 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
30 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
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.flow.inventory.rev130819.FlowCapableNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.BucketsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 import com.google.common.base.Objects;
55 import com.google.common.base.Optional;
56 import com.google.common.collect.Ordering;
59 * Manage the group tables for handling broadcast/multicast
63 public class GroupTable extends OfTable {
64 private static final Logger LOG =
65 LoggerFactory.getLogger(GroupTable.class);
67 public GroupTable(OfContext ctx) {
73 public void update(NodeId nodeId, PolicyInfo policyInfo, FlowMap flowMap)
75 // there appears to be no way of getting only the existing group
76 // tables unfortunately, so we have to get the whole goddamned node.
77 // Since this is happening concurrently with other things that are
78 // working in subtrees of nodes, we have to do two transactions
79 ReadOnlyTransaction t = ctx.getDataBroker().newReadOnlyTransaction();
80 InstanceIdentifier<Node> niid = createNodePath(nodeId);
82 t.read(LogicalDatastoreType.OPERATIONAL, niid).get();
85 FlowCapableNode fcn = r.get().getAugmentation(FlowCapableNode.class);
89 HashMap<GroupId, GroupCtx> groupMap = new HashMap<>();
91 if (fcn.getGroup() != null) {
92 for (Group g : fcn.getGroup()) {
93 GroupCtx gctx = new GroupCtx(g.getGroupId());
94 groupMap.put(g.getGroupId(), gctx);
96 Buckets bs = g.getBuckets();
97 if (bs != null && bs.getBucket() != null)
98 for (Bucket b : bs.getBucket()) {
99 gctx.bucketMap.put(b.getBucketId(), new BucketCtx(b));
104 // sync(nodeId, policyInfo, dirty, groupMap);
105 sync(nodeId, policyInfo, groupMap);
107 WriteTransaction wt = ctx.getDataBroker().newWriteOnlyTransaction();
108 boolean wrote = syncGroupToStore(wt, nodeId, groupMap);
113 protected boolean syncGroupToStore(WriteTransaction wt,
115 HashMap<GroupId, GroupCtx> groupMap) {
116 boolean wrote = false;
117 for (GroupCtx gctx : groupMap.values()) {
118 InstanceIdentifier<Group> giid =
119 createGroupPath(nodeId, gctx.groupId);
121 // Remove group table
123 wt.delete(LogicalDatastoreType.CONFIGURATION, giid);
125 ArrayList<Bucket> buckets = new ArrayList<>();
127 // update group table
128 for (BucketCtx bctx : gctx.bucketMap.values()) {
131 bid = bctx.b.getBucketId();
133 bid = bctx.newb.getBucketId();
134 InstanceIdentifier<Bucket> biid =
135 createBucketPath(nodeId,
141 wt.delete(LogicalDatastoreType.CONFIGURATION, biid);
142 } else if (bctx.b == null) {
144 buckets.add(bctx.newb);
145 } else if (!Objects.equal(bctx.newb.getAction(),
146 Ordering.from(ActionComparator.INSTANCE)
147 .sortedCopy(bctx.b.getAction()))) {
149 buckets.add(bctx.newb);
152 if (buckets.size() > 0) {
153 GroupBuilder gb = new GroupBuilder()
154 .setGroupId(gctx.groupId)
155 .setGroupType(GroupTypes.GroupAll)
156 .setBuckets(new BucketsBuilder()
160 wt.merge(LogicalDatastoreType.CONFIGURATION,
161 giid, gb.build(), true);
168 protected void sync(NodeId nodeId, PolicyInfo policyInfo, HashMap<GroupId, GroupCtx> groupMap) throws Exception {
171 for (Endpoint localEp : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
172 EndpointFwdCtxOrdinals localEpFwdCtxOrds = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, policyInfo, localEp);
173 GroupId gid = new GroupId(Long.valueOf(localEpFwdCtxOrds.getFdId()));
174 GroupCtx gctx = groupMap.get(gid);
176 groupMap.put(gid, gctx = new GroupCtx(gid));
180 for (EgKey epg : ctx.getEndpointManager().getGroupsForNode(nodeId)) {
183 // we'll use the fdId with the high bit set for remote bucket
184 // and just the local port number for local bucket
185 for (NodeId destNode : ctx.getEndpointManager().getNodesForGroup(epg)) {
186 if (nodeId.equals(destNode))
189 long bucketId = OrdinalFactory.getContextOrdinal(destNode);
190 bucketId |= 1L << 31;
193 ctx.getSwitchManager().getTunnelIP(destNode, TunnelTypeVxlan.class);
194 NodeConnectorId tunPort =
195 ctx.getSwitchManager().getTunnelPort(nodeId, TunnelTypeVxlan.class);
196 if (tunDst == null || tunPort == null)
198 Action tundstAction = null;
199 if (tunDst.getIpv4Address() != null) {
200 String nextHop = tunDst.getIpv4Address().getValue();
201 tundstAction = nxLoadTunIPv4Action(nextHop, true);
203 LOG.error("IPv6 tunnel destination {} for {} not supported",
204 tunDst.getIpv6Address().getValue(),
208 BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId))).setAction(actionList(tundstAction, outputAction(tunPort)));
212 updateBucket(gctx, bb);
214 OfOverlayContext ofc =
215 localEp.getAugmentation(OfOverlayContext.class);
216 if (ofc == null || ofc.getNodeConnectorId() == null ||
217 (LocationType.External.equals(ofc.getLocationType())))
222 bucketId = getOfPortNum(ofc.getNodeConnectorId());
223 } catch (NumberFormatException e) {
224 LOG.warn("Could not parse port number {}",
225 ofc.getNodeConnectorId(), e);
229 Action output = outputAction(ofc.getNodeConnectorId());
230 BucketBuilder bb = new BucketBuilder()
231 .setBucketId(new BucketId(Long.valueOf(bucketId)))
232 .setAction(actionList(output));
233 updateBucket(gctx, bb);
238 private static void updateBucket(GroupCtx gctx, BucketBuilder bb) {
239 BucketCtx bctx = gctx.bucketMap.get(bb.getBucketId());
241 gctx.bucketMap.put(bb.getBucketId(),
242 bctx = new BucketCtx(null));
245 bctx.newb = bb.build();
248 protected static class BucketCtx {
251 boolean visited = false;
253 public BucketCtx(Bucket b) {
259 protected static class GroupCtx {
261 Map<BucketId, BucketCtx> bucketMap = new HashMap<>();
262 boolean visited = false;
264 public GroupCtx(GroupId groupId) {
266 this.groupId = groupId;