Added range type to subject-feature-definition/parameter
[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 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         if (fcn.getGroup() != null) {
82             for (Group g : fcn.getGroup()) {
83                 GroupCtx gctx = new GroupCtx(g.getGroupId());
84                 groupMap.put(g.getGroupId(), gctx);
85
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));
90                 }
91             }
92         }
93
94         sync(nodeId, policyInfo, dirty, groupMap);
95
96         WriteTransaction wt = ctx.dataBroker.newWriteOnlyTransaction();
97         boolean wrote = syncGroupToStore(wt, nodeId, groupMap);
98         if (wrote)
99             wt.submit().get();
100     }
101
102     protected boolean syncGroupToStore(WriteTransaction wt,
103                                        NodeId nodeId,
104                                        HashMap<GroupId, GroupCtx> groupMap) {
105         boolean wrote = false;
106         for (GroupCtx gctx : groupMap.values()) {
107             InstanceIdentifier<Group> giid =
108                     createGroupPath(nodeId, gctx.groupId);
109             if (!gctx.visited) {
110                 // Remove group table
111                 wrote = true;
112                 wt.delete(LogicalDatastoreType.CONFIGURATION, giid);
113             } else {
114                 ArrayList<Bucket> buckets = new ArrayList<>();
115
116                 // update group table
117                 for (BucketCtx bctx : gctx.bucketMap.values()) {
118                     BucketId bid;
119                     if (bctx.b != null) bid = bctx.b.getBucketId();
120                     else bid = bctx.newb.getBucketId();
121                     InstanceIdentifier<Bucket> biid =
122                             createBucketPath(nodeId,
123                                              gctx.groupId,
124                                              bid);
125                     if (!bctx.visited) {
126                         // remove bucket
127                         wrote = true;
128                         wt.delete(LogicalDatastoreType.CONFIGURATION, biid);
129                     } else if (bctx.b == null) {
130                         // new bucket
131                         buckets.add(bctx.newb);
132                     } else if (!Objects.equal(bctx.newb.getAction(),
133                                               Ordering.from(ActionComparator.INSTANCE)
134                                                   .sortedCopy(bctx.b.getAction()))) {
135                         // update bucket
136                         buckets.add(bctx.newb);
137                     }
138                 }
139                 if (buckets.size() > 0) {
140                     GroupBuilder gb = new GroupBuilder()
141                         .setGroupId(gctx.groupId)
142                         .setGroupType(GroupTypes.GroupAll)
143                         .setBuckets(new BucketsBuilder()
144                         .setBucket(buckets)
145                         .build());
146                     wrote = true;
147                     wt.merge(LogicalDatastoreType.CONFIGURATION,
148                              giid, gb.build());
149                 }
150             }
151         }
152         return wrote;
153     }
154
155     protected void sync(NodeId nodeId, PolicyInfo policyInfo, Dirty dirty,
156                         HashMap<GroupId, GroupCtx> groupMap) throws Exception {
157
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;
165
166             int fdId = ctx.policyManager.getContextOrdinal(epg.getTenantId(),
167                                                            fd.getId());
168             GroupId gid = new GroupId(Long.valueOf(fdId));
169             GroupCtx gctx = groupMap.get(gid);
170             if (gctx == null) {
171                 groupMap.put(gid, gctx = new GroupCtx(gid));
172             }
173             gctx.visited = true;
174
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;
179
180                 long bucketId = ctx.policyManager
181                         .getContextOrdinal(destNode.getValue());
182                 bucketId |= 1L << 31;
183
184                 IpAddress tunDst =
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);
193                 } else {
194                     LOG.error("IPv6 tunnel destination {} for {} not supported",
195                               tunDst.getIpv6Address().getValue(),
196                               destNode);
197                     continue;
198                 }
199
200                 BucketBuilder bb = new BucketBuilder()
201                     .setBucketId(new BucketId(Long.valueOf(bucketId)))
202                     .setAction(actionList(tundstAction,
203                                           outputAction(tunPort)));
204                 updateBucket(gctx, bb);
205             }
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())))
211                     continue;
212
213                 long bucketId;
214                 try {
215                     bucketId = getOfPortNum(ofc.getNodeConnectorId());
216                 } catch (NumberFormatException e) {
217                     LOG.warn("Could not parse port number {}",
218                              ofc.getNodeConnectorId(), e);
219                     continue;
220                 }
221
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);
227             }
228         }
229     }
230
231     private static void updateBucket(GroupCtx gctx, BucketBuilder bb) {
232         BucketCtx bctx = gctx.bucketMap.get(bb.getBucketId());
233         if (bctx == null) {
234             gctx.bucketMap.put(bb.getBucketId(),
235                                bctx = new BucketCtx(null));
236         }
237         bctx.visited = true;
238         bctx.newb = bb.build();
239     }
240
241     protected static class BucketCtx {
242         Bucket b;
243         Bucket newb;
244         boolean visited = false;
245
246         public BucketCtx(Bucket b) {
247             super();
248             this.b = b;
249         }
250     }
251
252     protected static class GroupCtx {
253         GroupId groupId;
254         Map<BucketId, BucketCtx> bucketMap = new HashMap<>();
255         boolean visited = false;
256
257         public GroupCtx(GroupId groupId) {
258             super();
259             this.groupId = groupId;
260         }
261     }
262
263 }