Luca Prete
Committed by Gerrit Code Review

Adding VLAN to PeerConnectivityManager

Change-Id: I695087c108dc4d9d2da61992019d8fa3d08c61c1
......@@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Sets;
import org.onlab.packet.IpAddress;
import org.onlab.packet.VlanId;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.config.Config;
......@@ -41,6 +42,7 @@ public class BgpConfig extends Config<ApplicationId> {
public static final String CONNECT_POINT = "connectPoint";
public static final String NAME = "name";
public static final String PEERS = "peers";
public static final String VLAN = "vlan";
/**
* Gets the set of configured BGP speakers.
......@@ -69,7 +71,10 @@ public class BgpConfig extends Config<ApplicationId> {
name = Optional.of(jsonNode.get(NAME).asText());
}
VlanId vlan = getVlan(jsonNode);
speakers.add(new BgpSpeakerConfig(name,
vlan,
ConnectPoint.deviceConnectPoint(jsonNode.path(CONNECT_POINT).asText()),
listenAddresses));
});
......@@ -77,6 +82,15 @@ public class BgpConfig extends Config<ApplicationId> {
return speakers;
}
// If configured, it retreives a VLAN Id from a BGP speaker node
private VlanId getVlan(JsonNode node) {
VlanId vlan = VlanId.NONE;
if (!node.path(VLAN).isMissingNode()) {
vlan = VlanId.vlanId(node.path(VLAN).asText());
}
return vlan;
}
/**
* Examines whether a name of BGP speaker exists in configuration.
*
......@@ -102,6 +116,8 @@ public class BgpConfig extends Config<ApplicationId> {
speakerNode.put(NAME, speaker.name().get());
speakerNode.put(VLAN, speaker.vlan().toString());
speakerNode.put(CONNECT_POINT, speaker.connectPoint().elementId().toString()
+ "/" + speaker.connectPoint().port().toString());
......@@ -157,8 +173,8 @@ public class BgpConfig extends Config<ApplicationId> {
/**
* Finds BGP speaker peering with a given external peer.
*
* @param peerAddress peering address to be removed
* @return speaker
* @param peerAddress BGP peer address
* @return BGP speaker
*/
public BgpSpeakerConfig getSpeakerFromPeer(IpAddress peerAddress) {
for (BgpConfig.BgpSpeakerConfig speaker : bgpSpeakers()) {
......@@ -207,12 +223,14 @@ public class BgpConfig extends Config<ApplicationId> {
public static class BgpSpeakerConfig {
private Optional<String> name;
private VlanId vlanId;
private ConnectPoint connectPoint;
private Set<IpAddress> peers;
public BgpSpeakerConfig(Optional<String> name, ConnectPoint connectPoint,
Set<IpAddress> peers) {
public BgpSpeakerConfig(Optional<String> name, VlanId vlanId,
ConnectPoint connectPoint, Set<IpAddress> peers) {
this.name = checkNotNull(name);
this.vlanId = checkNotNull(vlanId);
this.connectPoint = checkNotNull(connectPoint);
this.peers = checkNotNull(peers);
}
......@@ -221,6 +239,10 @@ public class BgpConfig extends Config<ApplicationId> {
return name;
}
public VlanId vlan() {
return vlanId;
}
public ConnectPoint connectPoint() {
return connectPoint;
}
......@@ -252,6 +274,7 @@ public class BgpConfig extends Config<ApplicationId> {
if (obj instanceof BgpSpeakerConfig) {
final BgpSpeakerConfig that = (BgpSpeakerConfig) obj;
return Objects.equals(this.name, that.name) &&
Objects.equals(this.vlanId, that.vlanId) &&
Objects.equals(this.connectPoint, that.connectPoint) &&
Objects.equals(this.peers, that.peers);
}
......@@ -260,7 +283,7 @@ public class BgpConfig extends Config<ApplicationId> {
@Override
public int hashCode() {
return Objects.hash(name, connectPoint, peers);
return Objects.hash(name, vlanId, connectPoint, peers);
}
}
}
......
......@@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import org.onlab.packet.VlanId;
import org.onosproject.TestApplicationId;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.ConnectPoint;
......@@ -51,6 +52,8 @@ public class BgpConfigTest {
private static final IpAddress IP5 = IpAddress.valueOf("10.0.201.1");
public static final IpAddress IP_NON_EXIST = IpAddress.valueOf("10.101.1.1");
public static final VlanId NO_VLAN = VlanId.NONE;
public static final ConnectPoint CONNECT_POINT1 = ConnectPoint.
deviceConnectPoint("of:0000000000000001/1");
public static final ConnectPoint CONNECT_POINT2 = ConnectPoint.
......@@ -230,7 +233,8 @@ public class BgpConfigTest {
ConnectPoint connectPoint = CONNECT_POINT1;
Set<IpAddress> connectedPeers = new HashSet<>(Arrays.asList(IP1, IP2, IP3));
return new BgpConfig.BgpSpeakerConfig(speakerName, connectPoint, connectedPeers);
return new BgpConfig.BgpSpeakerConfig(speakerName, NO_VLAN,
connectPoint, connectedPeers);
}
private BgpConfig.BgpSpeakerConfig createNewSpeaker() {
......@@ -239,6 +243,7 @@ public class BgpConfigTest {
Set<IpAddress> connectedPeers = new HashSet<>(
Arrays.asList(IP4, IP5));
return new BgpConfig.BgpSpeakerConfig(speakerName, connectPoint, connectedPeers);
return new BgpConfig.BgpSpeakerConfig(speakerName, NO_VLAN,
connectPoint, connectedPeers);
}
}
......
......@@ -19,6 +19,7 @@ package org.onosproject.routing.cli;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onlab.packet.IpAddress;
import org.onlab.packet.VlanId;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
......@@ -40,12 +41,18 @@ public class AddSpeakerCommand extends AbstractShellCommand {
@Argument(index = 0, name = "name",
description = "Name of the internal BGP speaker",
required = true, multiValued = false)
String name = null;
private String name = null;
@Argument(index = 1, name = "connectionPoint",
description = "Interface to the BGP speaker",
required = true, multiValued = false)
String connectionPoint = null;
private String connectionPoint = null;
@Argument(index = 2, name = "vlanId",
description = "VLAN Id of the internal BGP speaker",
required = false, multiValued = false)
private String vlanId = null;
private VlanId vlanIdObj = null;
private static final String SPEAKER_ADD_SUCCESS = "Speaker Successfully Added.";
......@@ -57,11 +64,19 @@ public class AddSpeakerCommand extends AbstractShellCommand {
BgpConfig config = configService.addConfig(appId, BgpConfig.class);
if (name != null) {
BgpConfig.BgpSpeakerConfig speaker = config.getSpeakerWithName(name);
if (speaker != null) {
log.debug("Speaker already exists: {}", name);
return;
}
}
if (vlanId == null || vlanId.isEmpty()) {
vlanIdObj = VlanId.NONE;
} else {
vlanIdObj = VlanId.vlanId(Short.valueOf(vlanId));
}
addSpeakerToConf(config);
configService.applyConfig(appId, BgpConfig.class, config.node());
......@@ -85,6 +100,7 @@ public class AddSpeakerCommand extends AbstractShellCommand {
ConnectPoint connectPoint = ConnectPoint.
deviceConnectPoint(connectionPoint);
return new BgpConfig.BgpSpeakerConfig(Optional.ofNullable(name),
vlanIdObj,
connectPoint, new HashSet<IpAddress>());
}
}
......
......@@ -37,7 +37,7 @@ import java.util.List;
description = "Lists all BGP speakers")
public class BgpSpeakersListCommand extends AbstractShellCommand {
private static final String FORMAT = "port=%s/%s, peers=%s";
private static final String FORMAT = "port=%s/%s, vlan=%s, peers=%s";
private static final String NAME_FORMAT = "%s: " + FORMAT;
private static final Comparator<BgpConfig.BgpSpeakerConfig> SPEAKERS_COMPARATOR = (s1, s2) ->
......@@ -67,10 +67,10 @@ public class BgpSpeakersListCommand extends AbstractShellCommand {
s -> {
if (s.name().isPresent()) {
print(NAME_FORMAT, s.name().get(), s.connectPoint().deviceId(),
s.connectPoint().port(), s.peers());
s.connectPoint().port(), s.vlan(), s.peers());
} else {
print(FORMAT, s.connectPoint().deviceId(),
s.connectPoint().port(), s.peers());
s.connectPoint().port(), s.vlan(), s.peers());
}
});
}
......
......@@ -21,6 +21,7 @@ import org.onlab.packet.IPv6;
import org.onlab.packet.IpAddress;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.TpPort;
import org.onlab.packet.VlanId;
import org.onosproject.core.ApplicationId;
import org.onosproject.incubator.net.intf.Interface;
import org.onosproject.incubator.net.intf.InterfaceEvent;
......@@ -166,6 +167,9 @@ public class PeerConnectivityManager {
private Collection<PointToPointIntent> buildSpeakerIntents(BgpConfig.BgpSpeakerConfig speaker) {
List<PointToPointIntent> intents = new ArrayList<>();
// Get the BGP Speaker VLAN Id
VlanId bgpSpeakerVlanId = speaker.vlan();
for (IpAddress peerAddress : speaker.peers()) {
Interface peeringInterface = interfaceService.getMatchingInterface(peerAddress);
......@@ -175,18 +179,23 @@ public class PeerConnectivityManager {
continue;
}
IpAddress peeringAddress = null;
for (InterfaceIpAddress address : peeringInterface.ipAddresses()) {
IpAddress bgpSpeakerAddress = null;
for (InterfaceIpAddress address : peeringInterface.ipAddressesList()) {
if (address.subnetAddress().contains(peerAddress)) {
peeringAddress = address.ipAddress();
bgpSpeakerAddress = address.ipAddress();
break;
}
}
checkNotNull(peeringAddress);
checkNotNull(bgpSpeakerAddress);
VlanId peerVlanId = peeringInterface.vlan();
intents.addAll(buildIntents(speaker.connectPoint(), peeringAddress,
peeringInterface.connectPoint(), peerAddress));
intents.addAll(buildIntents(speaker.connectPoint(), bgpSpeakerVlanId,
bgpSpeakerAddress,
peeringInterface.connectPoint(),
peerVlanId,
peerAddress));
}
return intents;
......@@ -197,19 +206,25 @@ public class PeerConnectivityManager {
* IP addresses.
*
* @param portOne the first connect point
* @param vlanOne the ingress VLAN
* @param ipOne the first IP address
* @param portTwo the second connect point
* @param vlanTwo the egress VLAN
* @param ipTwo the second IP address
* @return the intents to install
*/
private Collection<PointToPointIntent> buildIntents(ConnectPoint portOne,
VlanId vlanOne,
IpAddress ipOne,
ConnectPoint portTwo,
VlanId vlanTwo,
IpAddress ipTwo) {
List<PointToPointIntent> intents = new ArrayList<>();
TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
TrafficTreatment.Builder treatmentToPeer = DefaultTrafficTreatment.builder();
TrafficTreatment.Builder treatmentToSpeaker = DefaultTrafficTreatment.builder();
TrafficSelector selector;
Key key;
......@@ -224,8 +239,15 @@ public class PeerConnectivityManager {
icmpProtocol = IPv6.PROTOCOL_ICMP6;
}
// Add treatment for VLAN for traffic going from BGP speaker to BGP peer
if (!vlanTwo.equals(VlanId.NONE)) {
treatmentToPeer.setVlanId(vlanTwo);
}
// Path from BGP speaker to BGP peer matching destination TCP port 179
selector = buildSelector(tcpProtocol,
vlanOne,
ipOne,
ipTwo,
null,
......@@ -237,7 +259,7 @@ public class PeerConnectivityManager {
.appId(appId)
.key(key)
.selector(selector)
.treatment(treatment)
.treatment(treatmentToPeer.build())
.ingressPoint(portOne)
.egressPoint(portTwo)
.priority(PRIORITY_OFFSET)
......@@ -245,6 +267,7 @@ public class PeerConnectivityManager {
// Path from BGP speaker to BGP peer matching source TCP port 179
selector = buildSelector(tcpProtocol,
vlanOne,
ipOne,
ipTwo,
BGP_PORT,
......@@ -256,71 +279,80 @@ public class PeerConnectivityManager {
.appId(appId)
.key(key)
.selector(selector)
.treatment(treatment)
.treatment(treatmentToPeer.build())
.ingressPoint(portOne)
.egressPoint(portTwo)
.priority(PRIORITY_OFFSET)
.build());
// Path from BGP peer to BGP speaker matching destination TCP port 179
selector = buildSelector(tcpProtocol,
ipTwo,
// ICMP path from BGP speaker to BGP peer
selector = buildSelector(icmpProtocol,
vlanOne,
ipOne,
ipTwo,
null,
BGP_PORT);
null);
key = buildKey(ipTwo, ipOne, SUFFIX_DST);
key = buildKey(ipOne, ipTwo, SUFFIX_ICMP);
intents.add(PointToPointIntent.builder()
.appId(appId)
.key(key)
.selector(selector)
.treatment(treatment)
.ingressPoint(portTwo)
.egressPoint(portOne)
.treatment(treatmentToPeer.build())
.ingressPoint(portOne)
.egressPoint(portTwo)
.priority(PRIORITY_OFFSET)
.build());
// Path from BGP peer to BGP speaker matching source TCP port 179
// Add treatment for VLAN for traffic going from BGP peer to BGP speaker
if (!vlanOne.equals(VlanId.NONE)) {
treatmentToSpeaker.setVlanId(vlanOne);
}
// Path from BGP peer to BGP speaker matching destination TCP port 179
selector = buildSelector(tcpProtocol,
vlanTwo,
ipTwo,
ipOne,
BGP_PORT,
null);
null,
BGP_PORT);
key = buildKey(ipTwo, ipOne, SUFFIX_SRC);
key = buildKey(ipTwo, ipOne, SUFFIX_DST);
intents.add(PointToPointIntent.builder()
.appId(appId)
.key(key)
.selector(selector)
.treatment(treatment)
.treatment(treatmentToSpeaker.build())
.ingressPoint(portTwo)
.egressPoint(portOne)
.priority(PRIORITY_OFFSET)
.build());
// ICMP path from BGP speaker to BGP peer
selector = buildSelector(icmpProtocol,
ipOne,
// Path from BGP peer to BGP speaker matching source TCP port 179
selector = buildSelector(tcpProtocol,
vlanTwo,
ipTwo,
null,
ipOne,
BGP_PORT,
null);
key = buildKey(ipOne, ipTwo, SUFFIX_ICMP);
key = buildKey(ipTwo, ipOne, SUFFIX_SRC);
intents.add(PointToPointIntent.builder()
.appId(appId)
.key(key)
.selector(selector)
.treatment(treatment)
.ingressPoint(portOne)
.egressPoint(portTwo)
.treatment(treatmentToSpeaker.build())
.ingressPoint(portTwo)
.egressPoint(portOne)
.priority(PRIORITY_OFFSET)
.build());
// ICMP path from BGP peer to BGP speaker
selector = buildSelector(icmpProtocol,
vlanTwo,
ipTwo,
ipOne,
null,
......@@ -332,7 +364,7 @@ public class PeerConnectivityManager {
.appId(appId)
.key(key)
.selector(selector)
.treatment(treatment)
.treatment(treatmentToSpeaker.build())
.ingressPoint(portTwo)
.egressPoint(portOne)
.priority(PRIORITY_OFFSET)
......@@ -351,11 +383,17 @@ public class PeerConnectivityManager {
* @param dstTcpPort destination TCP port, or null if shouldn't be set
* @return the new traffic selector
*/
private TrafficSelector buildSelector(byte ipProto, IpAddress srcIp,
private TrafficSelector buildSelector(byte ipProto, VlanId ingressVlanId,
IpAddress srcIp,
IpAddress dstIp, Short srcTcpPort,
Short dstTcpPort) {
TrafficSelector.Builder builder = DefaultTrafficSelector.builder().matchIPProtocol(ipProto);
// Match on any VLAN Id if a VLAN Id configured on the ingress interface
if (!ingressVlanId.equals(VlanId.NONE)) {
builder.matchVlanId(VlanId.ANY);
}
if (dstIp.isIp4()) {
builder.matchEthType(Ethernet.TYPE_IPV4)
.matchIPSrc(IpPrefix.valueOf(srcIp, IpPrefix.MAX_INET_MASK_LENGTH))
......