2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Optional;
14 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.groupbasedpolicy.dto.EgKey;
17 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
18 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
19 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
20 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
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.group.buckets.BucketBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.Segmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import java.util.ArrayList;
43 import java.util.HashSet;
45 import java.util.concurrent.ExecutionException;
47 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.*;
50 * Manage the group tables for handling broadcast/multicast
53 public class GroupTable extends OfTable {
55 private static final Logger LOG = LoggerFactory.getLogger(GroupTable.class);
57 public GroupTable(OfContext ctx) {
62 public void sync(Endpoint endpoint, OfWriter ofWriter) throws Exception {
63 NodeId endpointNodeId = ctx.getEndpointManager().getEndpointNodeId(endpoint);
64 if (endpointNodeId == null) {
65 LOG.warn("Endpoint {} has no location specified, skipped", endpoint);
69 // there appears to be no way of getting only the existing group
70 // tables unfortunately, so we have to get the whole node.
71 // Since this is happening concurrently with other things that are
72 // working in subtrees of nodes, we have to do two transactions
73 FlowCapableNode fcn = getFCNodeFromDatastore(endpointNodeId);
76 EndpointFwdCtxOrdinals ordinals = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, endpoint);
77 if (ordinals == null) {
78 LOG.info("getEndpointFwdCtxOrdinals is null for EP {}", endpoint);
81 GroupId groupId = new GroupId(Long.valueOf(ordinals.getFdId()));
82 if (!ofWriter.groupExists(endpointNodeId, groupId.getValue())) {
83 LOG.info("createGroup {} {}", endpointNodeId, groupId);
84 ofWriter.writeGroup(endpointNodeId, groupId);
86 syncGroups(endpointNodeId, ordinals, endpoint, groupId, ofWriter);
90 void syncGroups(NodeId nodeId, EndpointFwdCtxOrdinals ordinals, Endpoint endpoint, GroupId groupId,
91 OfWriter ofWriter) throws Exception {
92 for (EgKey endpointGroupKey : ctx.getEndpointManager().getGroupsForNode(nodeId)) {
93 // we'll use the fdId with the high bit set for remote bucket
94 // and just the local port number for local bucket
95 for (NodeId destinationNode : findPeerNodesForGroup(endpointGroupKey)) {
96 if (nodeId.equals(destinationNode))
98 if (isFloodDomainOnNode(ordinals.getFdId(), destinationNode)) {
101 bucketId = (long) OrdinalFactory.getContextOrdinal(destinationNode);
102 } catch (Exception e) {
103 LOG.error("Error during getting of context ordinal, node: {}", destinationNode);
106 bucketId |= 1L << 31;
107 IpAddress tunDst = ctx.getSwitchManager().getTunnelIP(destinationNode, TunnelTypeVxlan.class);
108 NodeConnectorId tunPort = ctx.getSwitchManager().getTunnelPort(nodeId, TunnelTypeVxlan.class);
109 if (tunDst == null || tunPort == null)
112 if (tunDst.getIpv4Address() != null) {
113 String nextHop = tunDst.getIpv4Address().getValue();
114 tunDstAction = nxLoadTunIPv4Action(nextHop, true);
116 LOG.error("IPv6 tunnel destination {} for {} not supported", tunDst.getIpv6Address().getValue(),
120 BucketBuilder bucketBuilder = new BucketBuilder().setBucketId(new BucketId(bucketId))
121 .setAction(actionList(tunDstAction, outputAction(tunPort)));
122 ofWriter.writeBucket(nodeId, groupId, bucketBuilder.build());
125 // TODO broadcasts are not separated by EPG between endpoints on the same node
126 OfOverlayContext ofc = endpoint.getAugmentation(OfOverlayContext.class);
127 if (EndpointManager.isExternal(endpoint, ctx.getTenant(endpoint.getTenant()).getExternalImplicitGroups()))
131 bucketId = getOfPortNum(ofc.getNodeConnectorId());
132 } catch (NumberFormatException e) {
133 LOG.warn("Could not parse port number {}", ofc.getNodeConnectorId(), e);
136 Action output = outputAction(ofc.getNodeConnectorId());
137 BucketBuilder bb = new BucketBuilder().setBucketId(new BucketId(bucketId)).setAction(
138 FlowUtils.actionList(output));
139 ofWriter.writeBucket(nodeId, groupId, bb.build());
140 // if broadcast exceeds internal domain
141 for (Endpoint extEp : ctx.getEndpointManager().getExtEpsNoLocForGroup(endpointGroupKey)) {
142 if (extEp.getNetworkContainment() != null
143 && extEp.getNetworkContainment().equals(endpoint.getNetworkContainment())) {
144 Subnet subnet = ctx.getTenant(extEp.getTenant()).resolveSubnet(new SubnetId(extEp.getNetworkContainment()));
145 L2FloodDomain l2Fd = ctx.getTenant(extEp.getTenant())
146 .resolveL2FloodDomain(new L2FloodDomainId(subnet.getParent().getValue()));
148 Segmentation segmentation = l2Fd.getAugmentation(Segmentation.class);
149 // external endpoints do not have location augmentation
150 // however they are beyond external ports
151 for (NodeConnectorId extNcId : ctx.getSwitchManager().getExternalPorts(nodeId)) {
153 bucketId = getOfPortNum(extNcId);
154 } catch (NumberFormatException e) {
155 LOG.warn("Could not parse external port number {}", extNcId, e);
158 ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder>
159 actionList = new ArrayList<>();
160 if (segmentation != null) {
161 Integer vlanId = segmentation.getSegmentationId();
162 actionList.addAll(FlowUtils.pushVlanActions(vlanId));
163 actionList.add(new ActionBuilder().setOrder(2).setAction(outputAction(extNcId)));
165 actionList.add(new ActionBuilder().setOrder(0).setAction(outputAction(extNcId)));
167 bb.setBucketId(new BucketId(bucketId)).setAction(
168 FlowUtils.actionList(actionList));
169 ofWriter.writeBucket(nodeId, groupId, bb.build());
179 * @param sourceEpgKey a key of source group
180 * @return all the nodes on which endpoints are either in groups that have policy with source
181 * group, or are in the source group
183 private Set<NodeId> findPeerNodesForGroup(EgKey sourceEpgKey) {
184 Set<NodeId> nodes = new HashSet<>();
185 nodes.addAll(ctx.getEndpointManager().getNodesForGroup(sourceEpgKey));
186 for (EgKey dstEpGroups : ctx.getCurrentPolicy().getPeers(sourceEpgKey)) {
187 nodes.addAll(ctx.getEndpointManager().getNodesForGroup(dstEpGroups));
192 private boolean isFloodDomainOnNode(int fdId, NodeId node) throws Exception {
193 for (Endpoint endpoint : ctx.getEndpointManager().getEndpointsForNode(node)) {
194 EndpointFwdCtxOrdinals endpointFwdCtxOrdinals = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, endpoint);
195 if (endpointFwdCtxOrdinals == null) {
198 int epFdId = endpointFwdCtxOrdinals.getFdId();
199 if (fdId == epFdId) {
206 private FlowCapableNode getFCNodeFromDatastore(NodeId nodeId)
207 throws ExecutionException, InterruptedException {
208 ReadOnlyTransaction t = ctx.getDataBroker().newReadOnlyTransaction();
209 InstanceIdentifier<FlowCapableNode> fcnIid = createNodePath(nodeId).builder()
210 .augmentation(FlowCapableNode.class).build();
212 Optional<FlowCapableNode> r = t.read(LogicalDatastoreType.OPERATIONAL, fcnIid).get();
213 if (!r.isPresent()) {
214 LOG.warn("Node {} is not present", fcnIid);
217 FlowCapableNode fcn = r.get();