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.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 import com.google.common.base.Objects;
54 import com.google.common.base.Optional;
55 import com.google.common.collect.Ordering;
58 * Manage the group tables for handling broadcast/multicast
62 public class GroupTable extends OfTable {
63 private static final Logger LOG =
64 LoggerFactory.getLogger(GroupTable.class);
66 public GroupTable(OfContext ctx) {
72 public void update(NodeId nodeId, PolicyInfo policyInfo, FlowMap flowMap)
74 // there appears to be no way of getting only the existing group
75 // tables unfortunately, so we have to get the whole goddamned node.
76 // Since this is happening concurrently with other things that are
77 // working in subtrees of nodes, we have to do two transactions
78 ReadOnlyTransaction t = ctx.getDataBroker().newReadOnlyTransaction();
79 InstanceIdentifier<Node> niid = createNodePath(nodeId);
81 t.read(LogicalDatastoreType.CONFIGURATION, niid).get();
84 FlowCapableNode fcn = r.get().getAugmentation(FlowCapableNode.class);
88 HashMap<GroupId, GroupCtx> groupMap = new HashMap<>();
90 if (fcn.getGroup() != null) {
91 for (Group g : fcn.getGroup()) {
92 GroupCtx gctx = new GroupCtx(g.getGroupId());
93 groupMap.put(g.getGroupId(), gctx);
95 Buckets bs = g.getBuckets();
96 if (bs != null && bs.getBucket() != null)
97 for (Bucket b : bs.getBucket()) {
98 gctx.bucketMap.put(b.getBucketId(), new BucketCtx(b));
103 // sync(nodeId, policyInfo, dirty, groupMap);
104 sync(nodeId, policyInfo, groupMap);
106 WriteTransaction wt = ctx.getDataBroker().newWriteOnlyTransaction();
107 boolean wrote = syncGroupToStore(wt, nodeId, groupMap);
112 protected boolean syncGroupToStore(WriteTransaction wt,
114 HashMap<GroupId, GroupCtx> groupMap) {
115 boolean wrote = false;
116 for (GroupCtx gctx : groupMap.values()) {
117 InstanceIdentifier<Group> giid =
118 createGroupPath(nodeId, gctx.groupId);
120 // Remove group table
122 wt.delete(LogicalDatastoreType.CONFIGURATION, giid);
124 ArrayList<Bucket> buckets = new ArrayList<>();
126 // update group table
127 for (BucketCtx bctx : gctx.bucketMap.values()) {
130 bid = bctx.b.getBucketId();
132 bid = bctx.newb.getBucketId();
133 InstanceIdentifier<Bucket> biid =
134 createBucketPath(nodeId,
140 wt.delete(LogicalDatastoreType.CONFIGURATION, biid);
141 } else if (bctx.b == null) {
143 buckets.add(bctx.newb);
144 } else if (!Objects.equal(bctx.newb.getAction(),
145 Ordering.from(ActionComparator.INSTANCE)
146 .sortedCopy(bctx.b.getAction()))) {
148 buckets.add(bctx.newb);
151 if (buckets.size() > 0) {
152 GroupBuilder gb = new GroupBuilder()
153 .setGroupId(gctx.groupId)
154 .setGroupType(GroupTypes.GroupAll)
155 .setBuckets(new BucketsBuilder()
159 wt.merge(LogicalDatastoreType.CONFIGURATION,
167 protected void sync(NodeId nodeId, PolicyInfo policyInfo, HashMap<GroupId, GroupCtx> groupMap) throws Exception {
170 for (Endpoint localEp : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
171 EndpointFwdCtxOrdinals localEpFwdCtxOrds = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, policyInfo, localEp);
172 GroupId gid = new GroupId(Long.valueOf(localEpFwdCtxOrds.getFdId()));
173 GroupCtx gctx = groupMap.get(gid);
175 groupMap.put(gid, gctx = new GroupCtx(gid));
179 for (EgKey epg : ctx.getEndpointManager().getGroupsForNode(nodeId)) {
182 // we'll use the fdId with the high bit set for remote bucket
183 // and just the local port number for local bucket
184 for (NodeId destNode : ctx.getEndpointManager().getNodesForGroup(epg)) {
185 if (nodeId.equals(destNode))
188 long bucketId = OrdinalFactory.getContextOrdinal(destNode);
189 bucketId |= 1L << 31;
192 ctx.getSwitchManager().getTunnelIP(destNode);
193 NodeConnectorId tunPort =
194 ctx.getSwitchManager().getTunnelPort(nodeId);
195 if (tunDst == null || tunPort == null)
197 Action tundstAction = null;
198 if (tunDst.getIpv4Address() != null) {
199 String nextHop = tunDst.getIpv4Address().getValue();
200 tundstAction = nxLoadTunIPv4Action(nextHop, true);
202 LOG.error("IPv6 tunnel destination {} for {} not supported",
203 tunDst.getIpv6Address().getValue(),
208 BucketBuilder bb = new BucketBuilder()
209 .setBucketId(new BucketId(Long.valueOf(bucketId)))
210 .setAction(actionList(tundstAction,
211 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;