1ba0fc5b92c64b3dd36517fd966377c7e42d2b8b
[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                 if (localEpFwdCtxOrds == null) {
174                     LOG.debug("getEndpointFwdCtxOrdinals is null for EP {}", localEp);
175                     continue;
176                 }
177
178                 GroupId gid = new GroupId(Long.valueOf(localEpFwdCtxOrds.getFdId()));
179                 GroupCtx gctx = groupMap.get(gid);
180                 if (gctx == null) {
181                     groupMap.put(gid, gctx = new GroupCtx(gid));
182                 }
183                 gctx.visited = true;
184
185                 for (EgKey epg : ctx.getEndpointManager().getGroupsForNode(nodeId)) {
186
187
188                     // we'll use the fdId with the high bit set for remote bucket
189                     // and just the local port number for local bucket
190                     for (NodeId destNode : ctx.getEndpointManager().getNodesForGroup(epg)) {
191                         if (nodeId.equals(destNode))
192                             continue;
193
194                         long bucketId = OrdinalFactory.getContextOrdinal(destNode);
195                         bucketId |= 1L << 31;
196
197                         IpAddress tunDst =
198                                 ctx.getSwitchManager().getTunnelIP(destNode, TunnelTypeVxlan.class);
199                         NodeConnectorId tunPort =
200                                 ctx.getSwitchManager().getTunnelPort(nodeId, TunnelTypeVxlan.class);
201                         if (tunDst == null || tunPort == null)
202                             continue;
203                         Action tundstAction = null;
204                         if (tunDst.getIpv4Address() != null) {
205                             String nextHop = tunDst.getIpv4Address().getValue();
206                             tundstAction = nxLoadTunIPv4Action(nextHop, true);
207                         } else {
208                             LOG.error("IPv6 tunnel destination {} for {} not supported",
209                                     tunDst.getIpv6Address().getValue(),
210                                     destNode);
211                             continue;
212                         }
213                     BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId))).setAction(actionList(tundstAction, outputAction(tunPort)));
214
215
216
217                         updateBucket(gctx, bb);
218                     }
219                 OfOverlayContext ofc =
220                         localEp.getAugmentation(OfOverlayContext.class);
221                 if (ofc == null || ofc.getNodeConnectorId() == null ||
222                         (LocationType.External.equals(ofc.getLocationType())))
223                     continue;
224
225                 long bucketId;
226                 try {
227                     bucketId = getOfPortNum(ofc.getNodeConnectorId());
228                 } catch (NumberFormatException e) {
229                     LOG.warn("Could not parse port number {}",
230                             ofc.getNodeConnectorId(), e);
231                     continue;
232                 }
233
234                 Action output = outputAction(ofc.getNodeConnectorId());
235                 BucketBuilder bb = new BucketBuilder()
236                         .setBucketId(new BucketId(Long.valueOf(bucketId)))
237                         .setAction(actionList(output));
238                 updateBucket(gctx, bb);
239             }
240         }
241     }
242
243     private static void updateBucket(GroupCtx gctx, BucketBuilder bb) {
244         BucketCtx bctx = gctx.bucketMap.get(bb.getBucketId());
245         if (bctx == null) {
246             gctx.bucketMap.put(bb.getBucketId(),
247                     bctx = new BucketCtx(null));
248         }
249         bctx.visited = true;
250         bctx.newb = bb.build();
251     }
252
253     protected static class BucketCtx {
254         Bucket b;
255         Bucket newb;
256         boolean visited = false;
257
258         public BucketCtx(Bucket b) {
259             super();
260             this.b = b;
261         }
262     }
263
264     protected static class GroupCtx {
265         GroupId groupId;
266         Map<BucketId, BucketCtx> bucketMap = new HashMap<>();
267         boolean visited = false;
268
269         public GroupCtx(GroupId groupId) {
270             super();
271             this.groupId = groupId;
272         }
273     }
274 }