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 if (fcn.getGroup() != null) {
82 for (Group g : fcn.getGroup()) {
83 GroupCtx gctx = new GroupCtx(g.getGroupId());
84 groupMap.put(g.getGroupId(), gctx);
86 Buckets bs = g.getBuckets();
87 if (bs != null && bs.getBucket() != null)
88 for (Bucket b : bs.getBucket()) {
89 gctx.bucketMap.put(b.getBucketId(), new BucketCtx(b));
94 sync(nodeId, policyInfo, dirty, groupMap);
96 WriteTransaction wt = ctx.dataBroker.newWriteOnlyTransaction();
97 boolean wrote = syncGroupToStore(wt, nodeId, groupMap);
102 protected boolean syncGroupToStore(WriteTransaction wt,
104 HashMap<GroupId, GroupCtx> groupMap) {
105 boolean wrote = false;
106 for (GroupCtx gctx : groupMap.values()) {
107 InstanceIdentifier<Group> giid =
108 createGroupPath(nodeId, gctx.groupId);
110 // Remove group table
112 wt.delete(LogicalDatastoreType.CONFIGURATION, giid);
114 ArrayList<Bucket> buckets = new ArrayList<>();
116 // update group table
117 for (BucketCtx bctx : gctx.bucketMap.values()) {
119 if (bctx.b != null) bid = bctx.b.getBucketId();
120 else bid = bctx.newb.getBucketId();
121 InstanceIdentifier<Bucket> biid =
122 createBucketPath(nodeId,
128 wt.delete(LogicalDatastoreType.CONFIGURATION, biid);
129 } else if (bctx.b == null) {
131 buckets.add(bctx.newb);
132 } else if (!Objects.equal(bctx.newb.getAction(),
133 Ordering.from(ActionComparator.INSTANCE)
134 .sortedCopy(bctx.b.getAction()))) {
136 buckets.add(bctx.newb);
139 if (buckets.size() > 0) {
140 GroupBuilder gb = new GroupBuilder()
141 .setGroupId(gctx.groupId)
142 .setGroupType(GroupTypes.GroupAll)
143 .setBuckets(new BucketsBuilder()
147 wt.merge(LogicalDatastoreType.CONFIGURATION,
155 protected void sync(NodeId nodeId, PolicyInfo policyInfo, Dirty dirty,
156 HashMap<GroupId, GroupCtx> groupMap) throws Exception {
158 for (EgKey epg : ctx.epManager.getGroupsForNode(nodeId)) {
159 IndexedTenant it = ctx.policyResolver.getTenant(epg.getTenantId());
160 if (it == null) continue;
161 EndpointGroup eg = it.getEndpointGroup(epg.getEgId());
162 if (eg == null || eg.getNetworkDomain() == null) continue;
163 L2FloodDomain fd = it.resolveL2FloodDomain(eg.getNetworkDomain());
164 if (fd == null) continue;
166 int fdId = ctx.policyManager.getContextOrdinal(epg.getTenantId(),
168 GroupId gid = new GroupId(Long.valueOf(fdId));
169 GroupCtx gctx = groupMap.get(gid);
171 groupMap.put(gid, gctx = new GroupCtx(gid));
175 // we'll use the fdId with the high bit set for remote bucket
176 // and just the local port number for local bucket
177 for (NodeId destNode : ctx.epManager.getNodesForGroup(epg)) {
178 if (nodeId.equals(destNode)) continue;
180 long bucketId = ctx.policyManager
181 .getContextOrdinal(destNode.getValue());
182 bucketId |= 1L << 31;
185 ctx.switchManager.getTunnelIP(destNode);
186 NodeConnectorId tunPort =
187 ctx.switchManager.getTunnelPort(nodeId);
188 if (tunDst == null || tunPort == null) continue;
189 Action tundstAction = null;
190 if (tunDst.getIpv4Address() != null) {
191 String nextHop = tunDst.getIpv4Address().getValue();
192 tundstAction = nxLoadTunIPv4Action(nextHop, true);
194 LOG.error("IPv6 tunnel destination {} for {} not supported",
195 tunDst.getIpv6Address().getValue(),
200 BucketBuilder bb = new BucketBuilder()
201 .setBucketId(new BucketId(Long.valueOf(bucketId)))
202 .setAction(actionList(tundstAction,
203 outputAction(tunPort)));
204 updateBucket(gctx, bb);
206 for (Endpoint localEp : ctx.epManager.getEPsForNode(nodeId, epg)) {
207 OfOverlayContext ofc =
208 localEp.getAugmentation(OfOverlayContext.class);
209 if (ofc == null || ofc.getNodeConnectorId() == null ||
210 (LocationType.External.equals(ofc.getLocationType())))
215 bucketId = getOfPortNum(ofc.getNodeConnectorId());
216 } catch (NumberFormatException e) {
217 LOG.warn("Could not parse port number {}",
218 ofc.getNodeConnectorId(), e);
222 Action output = outputAction(ofc.getNodeConnectorId());
223 BucketBuilder bb = new BucketBuilder()
224 .setBucketId(new BucketId(Long.valueOf(bucketId)))
225 .setAction(actionList(output));
226 updateBucket(gctx, bb);
231 private static void updateBucket(GroupCtx gctx, BucketBuilder bb) {
232 BucketCtx bctx = gctx.bucketMap.get(bb.getBucketId());
234 gctx.bucketMap.put(bb.getBucketId(),
235 bctx = new BucketCtx(null));
238 bctx.newb = bb.build();
241 protected static class BucketCtx {
244 boolean visited = false;
246 public BucketCtx(Bucket b) {
252 protected static class GroupCtx {
254 Map<BucketId, BucketCtx> bucketMap = new HashMap<>();
255 boolean visited = false;
257 public GroupCtx(GroupId groupId) {
259 this.groupId = groupId;