1c2e1ad5127fc2d59395fe52399d0e80d3070eb8
[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.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 import com.google.common.base.Objects;
54 import com.google.common.base.Optional;
55 import com.google.common.collect.Ordering;
56
57 /**
58  * Manage the group tables for handling broadcast/multicast
59  *
60  */
61
62 public class GroupTable extends OfTable {
63     private static final Logger LOG =
64             LoggerFactory.getLogger(GroupTable.class);
65
66     public GroupTable(OfContext ctx) {
67         super(ctx);
68     }
69
70     // @Override
71     @Override
72     public void update(NodeId nodeId, PolicyInfo policyInfo, FlowMap flowMap)
73             throws Exception {
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);
80         Optional<Node> r =
81                 t.read(LogicalDatastoreType.CONFIGURATION, niid).get();
82         if (!r.isPresent())
83             return;
84         FlowCapableNode fcn = r.get().getAugmentation(FlowCapableNode.class);
85         if (fcn == null)
86             return;
87
88         HashMap<GroupId, GroupCtx> groupMap = new HashMap<>();
89
90         if (fcn.getGroup() != null) {
91             for (Group g : fcn.getGroup()) {
92                 GroupCtx gctx = new GroupCtx(g.getGroupId());
93                 groupMap.put(g.getGroupId(), gctx);
94
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));
99                     }
100             }
101         }
102
103         // sync(nodeId, policyInfo, dirty, groupMap);
104         sync(nodeId, policyInfo, groupMap);
105
106         WriteTransaction wt = ctx.getDataBroker().newWriteOnlyTransaction();
107         boolean wrote = syncGroupToStore(wt, nodeId, groupMap);
108         if (wrote)
109             wt.submit().get();
110     }
111
112     protected boolean syncGroupToStore(WriteTransaction wt,
113             NodeId nodeId,
114             HashMap<GroupId, GroupCtx> groupMap) {
115         boolean wrote = false;
116         for (GroupCtx gctx : groupMap.values()) {
117             InstanceIdentifier<Group> giid =
118                     createGroupPath(nodeId, gctx.groupId);
119             if (!gctx.visited) {
120                 // Remove group table
121                 wrote = true;
122                 wt.delete(LogicalDatastoreType.CONFIGURATION, giid);
123             } else {
124                 ArrayList<Bucket> buckets = new ArrayList<>();
125
126                 // update group table
127                 for (BucketCtx bctx : gctx.bucketMap.values()) {
128                     BucketId bid;
129                     if (bctx.b != null)
130                         bid = bctx.b.getBucketId();
131                     else
132                         bid = bctx.newb.getBucketId();
133                     InstanceIdentifier<Bucket> biid =
134                             createBucketPath(nodeId,
135                                     gctx.groupId,
136                                     bid);
137                     if (!bctx.visited) {
138                         // remove bucket
139                         wrote = true;
140                         wt.delete(LogicalDatastoreType.CONFIGURATION, biid);
141                     } else if (bctx.b == null) {
142                         // new bucket
143                         buckets.add(bctx.newb);
144                     } else if (!Objects.equal(bctx.newb.getAction(),
145                             Ordering.from(ActionComparator.INSTANCE)
146                                     .sortedCopy(bctx.b.getAction()))) {
147                         // update bucket
148                         buckets.add(bctx.newb);
149                     }
150                 }
151                 if (buckets.size() > 0) {
152                     GroupBuilder gb = new GroupBuilder()
153                             .setGroupId(gctx.groupId)
154                             .setGroupType(GroupTypes.GroupAll)
155                             .setBuckets(new BucketsBuilder()
156                                     .setBucket(buckets)
157                                     .build());
158                     wrote = true;
159                     wt.merge(LogicalDatastoreType.CONFIGURATION,
160                             giid, gb.build());
161                 }
162             }
163         }
164         return wrote;
165     }
166
167     protected void sync(NodeId nodeId, PolicyInfo policyInfo, HashMap<GroupId, GroupCtx> groupMap) throws Exception {
168
169
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);
174                 if (gctx == null) {
175                     groupMap.put(gid, gctx = new GroupCtx(gid));
176                 }
177                 gctx.visited = true;
178
179                 for (EgKey epg : ctx.getEndpointManager().getGroupsForNode(nodeId)) {
180
181
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))
186                             continue;
187
188                         long bucketId = OrdinalFactory.getContextOrdinal(destNode);
189                         bucketId |= 1L << 31;
190
191                         IpAddress tunDst =
192                                 ctx.getSwitchManager().getTunnelIP(destNode);
193                         NodeConnectorId tunPort =
194                                 ctx.getSwitchManager().getTunnelPort(nodeId);
195                         if (tunDst == null || tunPort == null)
196                             continue;
197                         Action tundstAction = null;
198                         if (tunDst.getIpv4Address() != null) {
199                             String nextHop = tunDst.getIpv4Address().getValue();
200                             tundstAction = nxLoadTunIPv4Action(nextHop, true);
201                         } else {
202                             LOG.error("IPv6 tunnel destination {} for {} not supported",
203                                     tunDst.getIpv6Address().getValue(),
204                                     destNode);
205                             continue;
206                         }
207
208                         BucketBuilder bb = new BucketBuilder()
209                                 .setBucketId(new BucketId(Long.valueOf(bucketId)))
210                                 .setAction(actionList(tundstAction,
211                                         outputAction(tunPort)));
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 }