Most basic features now working, including:
[groupbasedpolicy.git] / groupbasedpolicy / 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 java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.Map;
14
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;
45
46 import com.google.common.base.Objects;
47 import com.google.common.base.Optional;
48 import com.google.common.collect.Ordering;
49
50 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.*;
51
52 /**
53  * Manage the group tables for handling broadcast/multicast
54  * @author readams
55  */
56 public class GroupTable extends OfTable {
57     private static final Logger LOG = 
58             LoggerFactory.getLogger(GroupTable.class);
59
60     public GroupTable(OfTableCtx ctx) {
61         super(ctx);
62     }
63     
64     @Override
65     public void update(NodeId nodeId, PolicyInfo policyInfo, Dirty dirty)
66             throws Exception {
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);
73         Optional<Node> r =
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;
78
79         HashMap<GroupId, GroupCtx> groupMap = new HashMap<>();
80
81         for (Group g : fcn.getGroup()) {
82             GroupCtx gctx = new GroupCtx(g.getGroupId());
83             groupMap.put(g.getGroupId(), gctx);
84
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));
89             }
90         }
91         
92         sync(nodeId, policyInfo, dirty, groupMap);
93
94         WriteTransaction wt = ctx.dataBroker.newWriteOnlyTransaction();
95         boolean wrote = syncGroupToStore(wt, nodeId, groupMap);
96         if (wrote)
97             wt.submit().get();
98     }
99     
100     protected boolean syncGroupToStore(WriteTransaction wt,
101                                        NodeId nodeId, 
102                                        HashMap<GroupId, GroupCtx> groupMap) {
103         boolean wrote = false;
104         for (GroupCtx gctx : groupMap.values()) {
105             InstanceIdentifier<Group> giid = 
106                     createGroupPath(nodeId, gctx.groupId);
107             if (!gctx.visited) {
108                 // Remove group table
109                 wrote = true;
110                 wt.delete(LogicalDatastoreType.CONFIGURATION, giid);
111             } else {
112                 ArrayList<Bucket> buckets = new ArrayList<>();
113                 
114                 // update group table
115                 for (BucketCtx bctx : gctx.bucketMap.values()) {
116                     BucketId bid;
117                     if (bctx.b != null) bid = bctx.b.getBucketId();
118                     else bid = bctx.newb.getBucketId();
119                     InstanceIdentifier<Bucket> biid = 
120                             createBucketPath(nodeId,
121                                              gctx.groupId, 
122                                              bid);
123                     if (!bctx.visited) {
124                         // remove bucket
125                         wrote = true;
126                         wt.delete(LogicalDatastoreType.CONFIGURATION, biid);
127                     } else if (bctx.b == null) {
128                         // new bucket
129                         buckets.add(bctx.newb);
130                     } else if (!Objects.equal(bctx.newb.getAction(), 
131                                               Ordering.from(ActionComparator.INSTANCE)
132                                                   .sortedCopy(bctx.b.getAction()))) {
133                         // update bucket
134                         buckets.add(bctx.newb);
135                     }
136                 }
137                 if (buckets.size() > 0) {
138                     GroupBuilder gb = new GroupBuilder()
139                         .setGroupId(gctx.groupId)
140                         .setGroupType(GroupTypes.GroupAll)
141                         .setBuckets(new BucketsBuilder()
142                         .setBucket(buckets)
143                         .build());
144                     wrote = true;
145                     wt.merge(LogicalDatastoreType.CONFIGURATION, 
146                              giid, gb.build());
147                 }
148             }
149         }
150         return wrote;
151     }
152     
153     protected void sync(NodeId nodeId, PolicyInfo policyInfo, Dirty dirty,
154                         HashMap<GroupId, GroupCtx> groupMap) throws Exception {
155
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;
163
164             int fdId = ctx.policyManager.getContextOrdinal(epg.getTenantId(),
165                                                            fd.getId());
166             GroupId gid = new GroupId(Long.valueOf(fdId));
167             GroupCtx gctx = groupMap.get(gid);
168             if (gctx == null) {
169                 groupMap.put(gid, gctx = new GroupCtx(gid)); 
170             }
171             gctx.visited = true;
172             
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;
177
178                 long bucketId = (long)ctx.policyManager
179                         .getContextOrdinal(destNode.getValue());
180                 bucketId |= 1L << 31;
181
182                 IpAddress tunDst = 
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);
191                 } else {
192                     LOG.error("IPv6 tunnel destination {} for {} not supported",
193                               tunDst.getIpv6Address().getValue(),
194                               destNode);
195                     continue;
196                 }
197
198                 BucketBuilder bb = new BucketBuilder()
199                     .setBucketId(new BucketId(Long.valueOf(bucketId)))
200                     .setAction(actionList(tundstAction,
201                                           outputAction(tunPort)));
202                 updateBucket(gctx, bb);
203             }
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())))
209                     continue;
210
211                 long bucketId;
212                 try {
213                     bucketId = getOfPortNum(ofc.getNodeConnectorId());
214                 } catch (NumberFormatException e) {
215                     LOG.warn("Could not parse port number {}", 
216                              ofc.getNodeConnectorId(), e);
217                     continue;
218                 }
219
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);
225             }
226         }
227     }
228
229     private static void updateBucket(GroupCtx gctx, BucketBuilder bb) {
230         BucketCtx bctx = gctx.bucketMap.get(bb.getBucketId());
231         if (bctx == null) {
232             gctx.bucketMap.put(bb.getBucketId(), 
233                                bctx = new BucketCtx(null));
234         }
235         bctx.visited = true;
236         bctx.newb = bb.build();        
237     }
238     
239     protected static class BucketCtx {
240         Bucket b;
241         Bucket newb;
242         boolean visited = false;
243
244         public BucketCtx(Bucket b) {
245             super();
246             this.b = b;
247         }
248     }
249     
250     protected static class GroupCtx {
251         GroupId groupId;
252         Map<BucketId, BucketCtx> bucketMap = new HashMap<>();
253         boolean visited = false;
254
255         public GroupCtx(GroupId groupId) {
256             super();
257             this.groupId = groupId;
258         }
259     }
260     
261 }