Bug 5617: Added UT for GroupTable and ChainActionFlows
[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 com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Optional;
13 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.groupbasedpolicy.dto.EgKey;
16 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
17 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
18 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
19 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.Segmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import java.util.ArrayList;
39 import java.util.HashSet;
40 import java.util.Set;
41 import java.util.concurrent.ExecutionException;
42
43 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.*;
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     @Override
58     public void sync(Endpoint endpoint, OfWriter ofWriter) throws Exception {
59         NodeId endpointNodeId = ctx.getEndpointManager().getEndpointNodeId(endpoint);
60         if (endpointNodeId == null) {
61             LOG.warn("Endpoint {} has no location specified, skipped", endpoint);
62             return;
63         }
64
65         // there appears to be no way of getting only the existing group
66         // tables unfortunately, so we have to get the whole node.
67         // Since this is happening concurrently with other things that are
68         // working in subtrees of nodes, we have to do two transactions
69         FlowCapableNode fcn = getFCNodeFromDatastore(endpointNodeId);
70         if (fcn == null)
71             return;
72         EndpointFwdCtxOrdinals ordinals = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, endpoint);
73         if (ordinals == null) {
74             LOG.info("getEndpointFwdCtxOrdinals is null for EP {}", endpoint);
75             return;
76         }
77         GroupId groupId = new GroupId(Long.valueOf(ordinals.getFdId()));
78         if (!ofWriter.groupExists(endpointNodeId, groupId.getValue())) {
79             LOG.info("createGroup {} {}", endpointNodeId, groupId);
80             ofWriter.writeGroup(endpointNodeId, groupId);
81         }
82         syncGroups(endpointNodeId, ordinals, endpoint, groupId, ofWriter);
83     }
84
85     @VisibleForTesting
86     void syncGroups(NodeId nodeId, EndpointFwdCtxOrdinals ordinals, Endpoint endpoint, GroupId groupId,
87                             OfWriter ofWriter) throws Exception {
88         for (EgKey endpointGroupKey : ctx.getEndpointManager().getGroupsForNode(nodeId)) {
89             // we'll use the fdId with the high bit set for remote bucket
90             // and just the local port number for local bucket
91             for (NodeId destinationNode : findPeerNodesForGroup(endpointGroupKey)) {
92                 if (nodeId.equals(destinationNode))
93                     continue;
94                 if (isFloodDomainOnNode(ordinals.getFdId(), destinationNode)) {
95                     Long bucketId;
96                     try {
97                         bucketId = (long) OrdinalFactory.getContextOrdinal(destinationNode);
98                     } catch (Exception e) {
99                         LOG.error("Error during getting of context ordinal, node: {}", destinationNode);
100                         continue;
101                     }
102                     bucketId |= 1L << 31;
103                     IpAddress tunDst = ctx.getSwitchManager().getTunnelIP(destinationNode, TunnelTypeVxlan.class);
104                     NodeConnectorId tunPort = ctx.getSwitchManager().getTunnelPort(nodeId, TunnelTypeVxlan.class);
105                     if (tunDst == null || tunPort == null)
106                         continue;
107                     Action tunDstAction;
108                     if (tunDst.getIpv4Address() != null) {
109                         String nextHop = tunDst.getIpv4Address().getValue();
110                         tunDstAction = nxLoadTunIPv4Action(nextHop, true);
111                     } else {
112                         LOG.error("IPv6 tunnel destination {} for {} not supported", tunDst.getIpv6Address().getValue(),
113                                 destinationNode);
114                         continue;
115                     }
116                     BucketBuilder bucketBuilder = new BucketBuilder().setBucketId(new BucketId(bucketId))
117                             .setAction(actionList(tunDstAction, outputAction(tunPort)));
118                     ofWriter.writeBucket(nodeId, groupId, bucketBuilder.build());
119                 }
120             }
121             // TODO broadcasts are not separated by EPG between endpoints on the same node
122             OfOverlayContext ofc = endpoint.getAugmentation(OfOverlayContext.class);
123             if (EndpointManager.isExternal(endpoint, ctx.getTenant(endpoint.getTenant()).getExternalImplicitGroups()))
124                 continue;
125             long bucketId;
126             try {
127                 bucketId = getOfPortNum(ofc.getNodeConnectorId());
128             } catch (NumberFormatException e) {
129                 LOG.warn("Could not parse port number {}", ofc.getNodeConnectorId(), e);
130                 continue;
131             }
132             Action output = outputAction(ofc.getNodeConnectorId());
133             BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(bucketId)).setAction(
134                     FlowUtils.actionList(output));
135             ofWriter.writeBucket(nodeId, groupId, bb.build());
136             // if broadcast exceeds internal domain
137             for (Endpoint extEp : ctx.getEndpointManager().getExtEpsNoLocForGroup(endpointGroupKey)) {
138                 if (extEp.getNetworkContainment() != null
139                         && extEp.getNetworkContainment().equals(endpoint.getNetworkContainment())) {
140                     L2FloodDomain l2Fd = ctx.getTenant(extEp.getTenant())
141                             .resolveL2FloodDomain(extEp.getNetworkContainment());
142                     if (l2Fd != null) {
143                         Segmentation segmentation = l2Fd.getAugmentation(Segmentation.class);
144                         // external endpoints do not have location augmentation
145                         // however they are beyond external ports
146                         for (NodeConnectorId extNcId : ctx.getSwitchManager().getExternalPorts(nodeId)) {
147                             try {
148                                 bucketId = getOfPortNum(extNcId);
149                             } catch (NumberFormatException e) {
150                                 LOG.warn("Could not parse external port number {}", extNcId, e);
151                                 continue;
152                             }
153                             ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder>
154                                     actionList = new ArrayList<>();
155                             if (segmentation != null) {
156                                 Integer vlanId = segmentation.getSegmentationId();
157                                 actionList.addAll(FlowUtils.pushVlanActions(vlanId));
158                                 actionList.add(new ActionBuilder().setOrder(2).setAction(outputAction(extNcId)));
159                             } else {
160                                 actionList.add(new ActionBuilder().setOrder(0).setAction(outputAction(extNcId)));
161                             }
162                             bb.setBucketId(new BucketId(bucketId)).setAction(
163                                     FlowUtils.actionList(actionList));
164                             ofWriter.writeBucket(nodeId, groupId, bb.build());
165                         }
166                     }
167                 }
168             }
169         }
170
171     }
172
173     /**
174      * @param sourceEpgKey a key of source group
175      * @return all the nodes on which endpoints are either in groups that have policy with source
176      * group, or are in the source group
177      */
178     private Set<NodeId> findPeerNodesForGroup(EgKey sourceEpgKey) {
179         Set<NodeId> nodes = new HashSet<>();
180         nodes.addAll(ctx.getEndpointManager().getNodesForGroup(sourceEpgKey));
181         for (EgKey dstEpGroups : ctx.getCurrentPolicy().getPeers(sourceEpgKey)) {
182             nodes.addAll(ctx.getEndpointManager().getNodesForGroup(dstEpGroups));
183         }
184         return nodes;
185     }
186
187     private boolean isFloodDomainOnNode(int fdId, NodeId node) throws Exception {
188         for (Endpoint endpoint : ctx.getEndpointManager().getEndpointsForNode(node)) {
189             EndpointFwdCtxOrdinals endpointFwdCtxOrdinals = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, endpoint);
190             if (endpointFwdCtxOrdinals == null) {
191                 continue;
192             }
193             int epFdId = endpointFwdCtxOrdinals.getFdId();
194             if (fdId == epFdId) {
195                 return true;
196             }
197         }
198         return false;
199     }
200
201     private FlowCapableNode getFCNodeFromDatastore(NodeId nodeId)
202             throws ExecutionException, InterruptedException {
203         ReadOnlyTransaction t = ctx.getDataBroker().newReadOnlyTransaction();
204         InstanceIdentifier<FlowCapableNode> fcnIid = createNodePath(nodeId).builder()
205                 .augmentation(FlowCapableNode.class).build();
206
207         Optional<FlowCapableNode> r = t.read(LogicalDatastoreType.OPERATIONAL, fcnIid).get();
208         if (!r.isPresent()) {
209             LOG.warn("Node {} is not present", fcnIid);
210             return null;
211         }
212         FlowCapableNode fcn = r.get();
213         t.close();
214         return fcn;
215     }
216 }