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 java.util.ArrayList;
12 import java.util.HashMap;
15 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.Dirty;
19 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
20 import org.opendaylight.groupbasedpolicy.resolver.IndexedTenant;
21 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.BucketsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2FloodDomain;
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.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import com.google.common.base.Objects;
47 import com.google.common.base.Optional;
48 import com.google.common.collect.Ordering;
50 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.*;
53 * Manage the group tables for handling broadcast/multicast
56 public class GroupTable extends OfTable {
57 private static final Logger LOG =
58 LoggerFactory.getLogger(GroupTable.class);
60 public GroupTable(OfTableCtx ctx) {
65 public void update(NodeId nodeId, PolicyInfo policyInfo, Dirty dirty)
67 // there appears to be no way of getting only the existing group
68 // tables unfortunately, so we have to get the whole goddamned node.
69 // Since this is happening concurrently with other things that are
70 // working in subtrees of nodes, we have to do two transactions
71 ReadOnlyTransaction t = ctx.dataBroker.newReadOnlyTransaction();
72 InstanceIdentifier<Node> niid = createNodePath(nodeId);
74 t.read(LogicalDatastoreType.CONFIGURATION, niid).get();
75 if (!r.isPresent()) return;
76 FlowCapableNode fcn = r.get().getAugmentation(FlowCapableNode.class);
77 if (fcn == null) return;
79 HashMap<GroupId, GroupCtx> groupMap = new HashMap<>();
81 for (Group g : fcn.getGroup()) {
82 GroupCtx gctx = new GroupCtx(g.getGroupId());
83 groupMap.put(g.getGroupId(), gctx);
85 Buckets bs = g.getBuckets();
86 if (bs != null && bs.getBucket() != null)
87 for (Bucket b : bs.getBucket()) {
88 gctx.bucketMap.put(b.getBucketId(), new BucketCtx(b));
92 sync(nodeId, policyInfo, dirty, groupMap);
94 WriteTransaction wt = ctx.dataBroker.newWriteOnlyTransaction();
95 boolean wrote = syncGroupToStore(wt, nodeId, groupMap);
100 protected boolean syncGroupToStore(WriteTransaction wt,
102 HashMap<GroupId, GroupCtx> groupMap) {
103 boolean wrote = false;
104 for (GroupCtx gctx : groupMap.values()) {
105 InstanceIdentifier<Group> giid =
106 createGroupPath(nodeId, gctx.groupId);
108 // Remove group table
110 wt.delete(LogicalDatastoreType.CONFIGURATION, giid);
112 ArrayList<Bucket> buckets = new ArrayList<>();
114 // update group table
115 for (BucketCtx bctx : gctx.bucketMap.values()) {
117 if (bctx.b != null) bid = bctx.b.getBucketId();
118 else bid = bctx.newb.getBucketId();
119 InstanceIdentifier<Bucket> biid =
120 createBucketPath(nodeId,
126 wt.delete(LogicalDatastoreType.CONFIGURATION, biid);
127 } else if (bctx.b == null) {
129 buckets.add(bctx.newb);
130 } else if (!Objects.equal(bctx.newb.getAction(),
131 Ordering.from(ActionComparator.INSTANCE)
132 .sortedCopy(bctx.b.getAction()))) {
134 buckets.add(bctx.newb);
137 if (buckets.size() > 0) {
138 GroupBuilder gb = new GroupBuilder()
139 .setGroupId(gctx.groupId)
140 .setGroupType(GroupTypes.GroupAll)
141 .setBuckets(new BucketsBuilder()
145 wt.merge(LogicalDatastoreType.CONFIGURATION,
153 protected void sync(NodeId nodeId, PolicyInfo policyInfo, Dirty dirty,
154 HashMap<GroupId, GroupCtx> groupMap) throws Exception {
156 for (EgKey epg : ctx.epManager.getGroupsForNode(nodeId)) {
157 IndexedTenant it = ctx.policyResolver.getTenant(epg.getTenantId());
158 if (it == null) continue;
159 EndpointGroup eg = it.getEndpointGroup(epg.getEgId());
160 if (eg == null || eg.getNetworkDomain() == null) continue;
161 L2FloodDomain fd = it.resolveL2FloodDomain(eg.getNetworkDomain());
162 if (fd == null) continue;
164 int fdId = ctx.policyManager.getContextOrdinal(epg.getTenantId(),
166 GroupId gid = new GroupId(Long.valueOf(fdId));
167 GroupCtx gctx = groupMap.get(gid);
169 groupMap.put(gid, gctx = new GroupCtx(gid));
173 // we'll use the fdId with the high bit set for remote bucket
174 // and just the local port number for local bucket
175 for (NodeId destNode : ctx.epManager.getNodesForGroup(epg)) {
176 if (nodeId.equals(destNode)) continue;
178 long bucketId = (long)ctx.policyManager
179 .getContextOrdinal(destNode.getValue());
180 bucketId |= 1L << 31;
183 ctx.switchManager.getTunnelIP(destNode);
184 NodeConnectorId tunPort =
185 ctx.switchManager.getTunnelPort(nodeId);
186 if (tunDst == null || tunPort == null) continue;
187 Action tundstAction = null;
188 if (tunDst.getIpv4Address() != null) {
189 String nextHop = tunDst.getIpv4Address().getValue();
190 tundstAction = nxLoadTunIPv4Action(nextHop, true);
192 LOG.error("IPv6 tunnel destination {} for {} not supported",
193 tunDst.getIpv6Address().getValue(),
198 BucketBuilder bb = new BucketBuilder()
199 .setBucketId(new BucketId(Long.valueOf(bucketId)))
200 .setAction(actionList(tundstAction,
201 outputAction(tunPort)));
202 updateBucket(gctx, bb);
204 for (Endpoint localEp : ctx.epManager.getEPsForNode(nodeId, epg)) {
205 OfOverlayContext ofc =
206 localEp.getAugmentation(OfOverlayContext.class);
207 if (ofc == null || ofc.getNodeConnectorId() == null ||
208 (LocationType.External.equals(ofc.getLocationType())))
213 bucketId = getOfPortNum(ofc.getNodeConnectorId());
214 } catch (NumberFormatException e) {
215 LOG.warn("Could not parse port number {}",
216 ofc.getNodeConnectorId(), e);
220 Action output = outputAction(ofc.getNodeConnectorId());
221 BucketBuilder bb = new BucketBuilder()
222 .setBucketId(new BucketId(Long.valueOf(bucketId)))
223 .setAction(actionList(output));
224 updateBucket(gctx, bb);
229 private static void updateBucket(GroupCtx gctx, BucketBuilder bb) {
230 BucketCtx bctx = gctx.bucketMap.get(bb.getBucketId());
232 gctx.bucketMap.put(bb.getBucketId(),
233 bctx = new BucketCtx(null));
236 bctx.newb = bb.build();
239 protected static class BucketCtx {
242 boolean visited = false;
244 public BucketCtx(Bucket b) {
250 protected static class GroupCtx {
252 Map<BucketId, BucketCtx> bucketMap = new HashMap<>();
253 boolean visited = false;
255 public GroupCtx(GroupId groupId) {
257 this.groupId = groupId;