Bug 3173 - group flows rendering fix.
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / GroupTable.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
10
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;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.Map;
22
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;
53
54 import com.google.common.base.Objects;
55 import com.google.common.base.Optional;
56 import com.google.common.collect.Ordering;
57
58 /**
59  * Manage the group tables for handling broadcast/multicast
60  *
61  */
62
63 public class GroupTable extends OfTable {
64     private static final Logger LOG =
65             LoggerFactory.getLogger(GroupTable.class);
66
67     public GroupTable(OfContext ctx) {
68         super(ctx);
69     }
70
71     // @Override
72     @Override
73     public void update(NodeId nodeId, PolicyInfo policyInfo, FlowMap flowMap)
74             throws Exception {
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);
81         Optional<Node> r =
82                 t.read(LogicalDatastoreType.OPERATIONAL, niid).get();
83         if (!r.isPresent())
84             return;
85         FlowCapableNode fcn = r.get().getAugmentation(FlowCapableNode.class);
86         if (fcn == null)
87             return;
88
89         HashMap<GroupId, GroupCtx> groupMap = new HashMap<>();
90
91         if (fcn.getGroup() != null) {
92             for (Group g : fcn.getGroup()) {
93                 GroupCtx gctx = new GroupCtx(g.getGroupId());
94                 groupMap.put(g.getGroupId(), gctx);
95
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));
100                     }
101             }
102         }
103
104         // sync(nodeId, policyInfo, dirty, groupMap);
105         sync(nodeId, policyInfo, groupMap);
106
107         WriteTransaction wt = ctx.getDataBroker().newWriteOnlyTransaction();
108         boolean wrote = syncGroupToStore(wt, nodeId, groupMap);
109         if (wrote)
110             wt.submit().get();
111     }
112
113     protected boolean syncGroupToStore(WriteTransaction wt,
114             NodeId nodeId,
115             HashMap<GroupId, GroupCtx> groupMap) {
116         boolean wrote = false;
117         for (GroupCtx gctx : groupMap.values()) {
118             InstanceIdentifier<Group> giid =
119                     createGroupPath(nodeId, gctx.groupId);
120             if (!gctx.visited) {
121                 // Remove group table
122                 wrote = true;
123                 wt.delete(LogicalDatastoreType.CONFIGURATION, giid);
124             } else {
125                 ArrayList<Bucket> buckets = new ArrayList<>();
126
127                 // update group table
128                 for (BucketCtx bctx : gctx.bucketMap.values()) {
129                     BucketId bid;
130                     if (bctx.b != null)
131                         bid = bctx.b.getBucketId();
132                     else
133                         bid = bctx.newb.getBucketId();
134                     InstanceIdentifier<Bucket> biid =
135                             createBucketPath(nodeId,
136                                     gctx.groupId,
137                                     bid);
138                     if (!bctx.visited) {
139                         // remove bucket
140                         wrote = true;
141                         wt.delete(LogicalDatastoreType.CONFIGURATION, biid);
142                     } else if (bctx.b == null) {
143                         // new bucket
144                         buckets.add(bctx.newb);
145                     } else if (!Objects.equal(bctx.newb.getAction(),
146                             Ordering.from(ActionComparator.INSTANCE)
147                                     .sortedCopy(bctx.b.getAction()))) {
148                         // update bucket
149                         buckets.add(bctx.newb);
150                     }
151                 }
152                 if (buckets.size() > 0) {
153                     GroupBuilder gb = new GroupBuilder()
154                             .setGroupId(gctx.groupId)
155                             .setGroupType(GroupTypes.GroupAll)
156                             .setBuckets(new BucketsBuilder()
157                                     .setBucket(buckets)
158                                     .build());
159                     wrote = true;
160                     wt.merge(LogicalDatastoreType.CONFIGURATION,
161                             giid, gb.build(), true);
162                 }
163             }
164         }
165         return wrote;
166     }
167
168     protected void sync(NodeId nodeId, PolicyInfo policyInfo, HashMap<GroupId, GroupCtx> groupMap) throws Exception {
169
170
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);
175                 if (gctx == null) {
176                     groupMap.put(gid, gctx = new GroupCtx(gid));
177                 }
178                 gctx.visited = true;
179
180                 for (EgKey epg : ctx.getEndpointManager().getGroupsForNode(nodeId)) {
181
182
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))
187                             continue;
188
189                         long bucketId = OrdinalFactory.getContextOrdinal(destNode);
190                         bucketId |= 1L << 31;
191
192                         IpAddress tunDst =
193                                 ctx.getSwitchManager().getTunnelIP(destNode, TunnelTypeVxlan.class);
194                         NodeConnectorId tunPort =
195                                 ctx.getSwitchManager().getTunnelPort(nodeId, TunnelTypeVxlan.class);
196                         if (tunDst == null || tunPort == null)
197                             continue;
198                         Action tundstAction = null;
199                         if (tunDst.getIpv4Address() != null) {
200                             String nextHop = tunDst.getIpv4Address().getValue();
201                             tundstAction = nxLoadTunIPv4Action(nextHop, true);
202                         } else {
203                             LOG.error("IPv6 tunnel destination {} for {} not supported",
204                                     tunDst.getIpv6Address().getValue(),
205                                     destNode);
206                             continue;
207                         }
208                     BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId))).setAction(actionList(tundstAction, outputAction(tunPort)));
209
210
211
212                         updateBucket(gctx, bb);
213                     }
214                 OfOverlayContext ofc =
215                         localEp.getAugmentation(OfOverlayContext.class);
216                 if (ofc == null || ofc.getNodeConnectorId() == null ||
217                         (LocationType.External.equals(ofc.getLocationType())))
218                     continue;
219
220                 long bucketId;
221                 try {
222                     bucketId = getOfPortNum(ofc.getNodeConnectorId());
223                 } catch (NumberFormatException e) {
224                     LOG.warn("Could not parse port number {}",
225                             ofc.getNodeConnectorId(), e);
226                     continue;
227                 }
228
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);
234             }
235         }
236     }
237
238     private static void updateBucket(GroupCtx gctx, BucketBuilder bb) {
239         BucketCtx bctx = gctx.bucketMap.get(bb.getBucketId());
240         if (bctx == null) {
241             gctx.bucketMap.put(bb.getBucketId(),
242                     bctx = new BucketCtx(null));
243         }
244         bctx.visited = true;
245         bctx.newb = bb.build();
246     }
247
248     protected static class BucketCtx {
249         Bucket b;
250         Bucket newb;
251         boolean visited = false;
252
253         public BucketCtx(Bucket b) {
254             super();
255             this.b = b;
256         }
257     }
258
259     protected static class GroupCtx {
260         GroupId groupId;
261         Map<BucketId, BucketCtx> bucketMap = new HashMap<>();
262         boolean visited = false;
263
264         public GroupCtx(GroupId groupId) {
265             super();
266             this.groupId = groupId;
267         }
268     }
269 }