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