Bug 3302: fix for GroupTable
[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.createNodePath;
13 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.getOfPortNum;
14 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxLoadTunIPv4Action;
15 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.outputAction;
16
17 import java.util.ArrayList;
18 import java.util.concurrent.ExecutionException;
19
20 import com.google.common.base.Optional;
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
24 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
25 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
26 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
27 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Manage the group tables for handling broadcast/multicast
47  */
48
49 public class GroupTable extends OfTable {
50
51     private static final Logger LOG = LoggerFactory.getLogger(GroupTable.class);
52
53     public GroupTable(OfContext ctx) {
54         super(ctx);
55     }
56
57     FlowCapableNode getFCNodeFromDatastore(NodeId nodeId)
58             throws ExecutionException, InterruptedException {
59         FlowCapableNode fcn = null;
60         ReadOnlyTransaction t = ctx.getDataBroker().newReadOnlyTransaction();
61         InstanceIdentifier<FlowCapableNode> fcniid = createNodePath(nodeId).builder()
62                 .augmentation(FlowCapableNode.class).build();
63
64         Optional<FlowCapableNode> r = t.read(LogicalDatastoreType.OPERATIONAL, fcniid).get();
65         if (!r.isPresent()) {
66             LOG.warn("Node {} is not present", fcniid);
67             return null;
68         }
69         fcn = r.get();
70         t.close();
71         return fcn;
72     }
73
74     @Override
75     public void update(NodeId nodeId, PolicyInfo policyInfo, OfWriter ofWriter) throws Exception {
76         // there appears to be no way of getting only the existing group
77         // tables unfortunately, so we have to get the whole goddamned node.
78         // Since this is happening concurrently with other things that are
79         // working in subtrees of nodes, we have to do two transactions
80         FlowCapableNode fcn = getFCNodeFromDatastore(nodeId);
81         if (fcn == null)
82             return;
83
84         sync(nodeId, policyInfo, ofWriter);
85     }
86
87     public void sync(NodeId nodeId, PolicyInfo policyInfo, OfWriter ofWriter) throws Exception {
88
89         for (Endpoint localEp : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
90             EndpointFwdCtxOrdinals localEpFwdCtxOrds =
91                     OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, policyInfo, localEp);
92             if (localEpFwdCtxOrds == null) {
93                 LOG.debug("getEndpointFwdCtxOrdinals is null for EP {}", localEp);
94                 continue;
95             }
96
97             GroupId gid = new GroupId(Long.valueOf(localEpFwdCtxOrds.getFdId()));
98             if (!ofWriter.groupExists(nodeId, gid.getValue())) {
99                 LOG.info("createGroup {} {}", nodeId, gid);
100                 ofWriter.writeGroup(nodeId, gid);
101             }
102
103             for (EgKey epg : ctx.getEndpointManager().getGroupsForNode(nodeId)) {
104
105                 // we'll use the fdId with the high bit set for remote bucket
106                 // and just the local port number for local bucket
107                 for (NodeId destNode : ctx.getEndpointManager().getNodesForGroup(epg)) {
108                     if (nodeId.equals(destNode))
109                         continue;
110
111                     long bucketId = OrdinalFactory.getContextOrdinal(destNode);
112                     bucketId |= 1L << 31;
113
114                     IpAddress tunDst = ctx.getSwitchManager().getTunnelIP(destNode, TunnelTypeVxlan.class);
115                     NodeConnectorId tunPort = ctx.getSwitchManager().getTunnelPort(nodeId, TunnelTypeVxlan.class);
116                     if (tunDst == null || tunPort == null)
117                         continue;
118                     Action tundstAction = null;
119                     if (tunDst.getIpv4Address() != null) {
120                         String nextHop = tunDst.getIpv4Address().getValue();
121                         tundstAction = nxLoadTunIPv4Action(nextHop, true);
122                     } else {
123                         LOG.error("IPv6 tunnel destination {} for {} not supported", tunDst.getIpv6Address().getValue(),
124                                 destNode);
125                         continue;
126                     }
127                     BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId)))
128                             .setAction(actionList(tundstAction, outputAction(tunPort)));
129
130                     ofWriter.writeBucket(nodeId, gid, bb.build());
131                 }
132                 OfOverlayContext ofc = localEp.getAugmentation(OfOverlayContext.class);
133                 if (ofc == null || ofc.getNodeConnectorId() == null ||
134                         (LocationType.External.equals(ofc.getLocationType())))
135                     continue;
136
137                 long bucketId;
138                 try {
139                     bucketId = getOfPortNum(ofc.getNodeConnectorId());
140                 } catch (NumberFormatException e) {
141                     LOG.warn("Could not parse port number {}", ofc.getNodeConnectorId(), e);
142                     continue;
143                 }
144
145                 Action output = outputAction(ofc.getNodeConnectorId());
146                 BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(Long.valueOf(bucketId)))
147                         .setAction(actionList(output));
148                 ofWriter.writeBucket(nodeId, gid, bb.build());
149             }
150         }
151     }
152
153 }