First commit for circuits app
Change-Id: I4b00370a458ae64a903a6798c247ecc7acb3fefe
diff --git a/vpls/features.xml b/vpls/features.xml
new file mode 100644
index 0000000..d9f5033
--- /dev/null
+++ b/vpls/features.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+ ~ Copyright 2015 Open Networking Laboratory
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
+ <repository>mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features</repository>
+ <feature name="${project.artifactId}" version="${project.version}"
+ description="${project.description}">
+ <feature>onos-api</feature>
+ <bundle>mvn:${project.groupId}/onos-app-routing-api/${project.version}</bundle>
+ <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version}</bundle>
+ </feature>
+</features>
diff --git a/vpls/pom.xml b/vpls/pom.xml
new file mode 100644
index 0000000..2bd66fa
--- /dev/null
+++ b/vpls/pom.xml
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2015 Open Networking Laboratory
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-app-samples</artifactId>
+ <version>1.4.0-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+
+ <artifactId>onos-app-vpls</artifactId>
+ <packaging>bundle</packaging>
+
+ <description>Application to create L2 broadcast network using VLAN</description>
+
+ <properties>
+ <onos.app.name>org.onosproject.vpls</onos.app.name>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-incubator-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-app-routing-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-app-sdnip</artifactId>
+ <version>1.4.0-SNAPSHOT</version>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/vpls/src/main/java/org/onos/vpls/IntentInstaller.java b/vpls/src/main/java/org/onos/vpls/IntentInstaller.java
new file mode 100644
index 0000000..f864502
--- /dev/null
+++ b/vpls/src/main/java/org/onos/vpls/IntentInstaller.java
@@ -0,0 +1,213 @@
+package org.onos.vpls;
+
+import com.google.common.collect.SetMultimap;
+import javafx.util.Pair;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.intent.Intent;
+import org.onosproject.net.intent.Key;
+import org.onosproject.net.intent.MultiPointToSinglePointIntent;
+import org.onosproject.net.intent.SinglePointToMultiPointIntent;
+import org.onosproject.sdnip.IntentSynchronizer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Synchronizes intents between the in-memory intent store and the
+ * IntentService.
+ */
+public class IntentInstaller {
+ private static final Logger log = LoggerFactory.getLogger(
+ IntentInstaller.class);
+
+ private static final int PRIORITY_OFFSET = 1000;
+
+ private static final String PREFIX_BROADCAST = "brc";
+ private static final String PREFIX_UNICAST = "uni";
+
+ private final ApplicationId appId;
+ private final IntentSynchronizer intentSynchronizer;
+
+ /**
+ * Class constructor.
+ *
+ * @param appId the Application ID
+ * @param intentSynchronizer the intent service
+ */
+ public IntentInstaller(ApplicationId appId, IntentSynchronizer intentSynchronizer) {
+ this.appId = appId;
+ this.intentSynchronizer = intentSynchronizer;
+ }
+
+ /**
+ * Formats the requests for creating and submit intents.
+ * Single Points to Multi Point intents are created for all the conigured
+ * Connect Points. Multi Point to Single Point intents are created for
+ * Connect Points configured that have hosts attached.
+ *
+ * @param confHostPresentCPoint A map of Connect Points with the eventual
+ * MAC address of the host attached, by VLAN
+ */
+ protected void installIntents(SetMultimap<VlanId,
+ Pair<ConnectPoint,
+ MacAddress>> confHostPresentCPoint) {
+ List<Intent> intents = new ArrayList<>();
+
+ confHostPresentCPoint.keys()
+ .forEach(vlanId -> {
+ List<Pair<ConnectPoint, MacAddress>> cPoints =
+ confHostPresentCPoint.get(vlanId).stream().collect(Collectors.toList());
+
+ if (!cPoints.isEmpty()) {
+ for (int i = 0; i < cPoints.size(); i++) {
+ ConnectPoint src = cPoints.get(i).getKey();
+ Set<ConnectPoint> dsts = new HashSet<>();
+ MacAddress mac = cPoints.get(i).getValue();
+ for (int j = 0; j < cPoints.size(); j++) {
+ ConnectPoint dst = cPoints.get(j).getKey();
+ if (!dst.equals(src)) {
+ dsts.add(dst);
+ }
+ }
+ Collection<SinglePointToMultiPointIntent> brcIntents =
+ buildBrcIntents(src, dsts, vlanId);
+ intents.addAll(brcIntents);
+ if (mac != null) {
+ Collection<MultiPointToSinglePointIntent> uniIntents =
+ buildUniIntents(dsts, src, vlanId, mac);
+ intents.addAll(uniIntents);
+ }
+ }
+ }
+ });
+ submitIntents(intents);
+ }
+
+ /**
+ * Requests to install the intents passed as argument to the Intent Service.
+ *
+ * @param intents intents to be submitted
+ */
+ private void submitIntents(Collection<Intent> intents) {
+ log.debug("Submitting intents to the IntentSynchronizer");
+
+ for (Intent intent : intents) {
+ intentSynchronizer.submit(intent);
+ }
+ }
+
+ /**
+ * Builds a set of Single Point to Multi Point intents.
+ *
+ * @param src The source Connect Point
+ * @param dsts The destination Connect Points
+ * @return Single Point to Multi Point intents generated.
+ */
+ private Collection<SinglePointToMultiPointIntent> buildBrcIntents(ConnectPoint src,
+ Set<ConnectPoint> dsts,
+ VlanId vlanId) {
+ log.debug("Building p-2-mp intent from {}", src);
+
+ List<SinglePointToMultiPointIntent> intents = new ArrayList<>();
+
+ TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
+
+
+ TrafficSelector.Builder builder = DefaultTrafficSelector.builder()
+ .matchEthDst(MacAddress.BROADCAST)
+ .matchVlanId(vlanId);
+
+ TrafficSelector selector = builder.build();
+
+ Key key = buildKey(PREFIX_BROADCAST, src, vlanId);
+
+ intents.add(SinglePointToMultiPointIntent.builder()
+ .appId(appId)
+ .key(key)
+ .selector(selector)
+ .treatment(treatment)
+ .ingressPoint(src)
+ .egressPoints(dsts)
+ .priority(PRIORITY_OFFSET)
+ .build());
+ return intents;
+ }
+
+ /**
+ * Builds a set of Multi Point to Single Point intents.
+ *
+ * @param srcs The source Connect Points
+ * @param dst The destination Connect Point
+ * @return Multi Point to Single Point intents generated.
+ */
+ private Collection<MultiPointToSinglePointIntent> buildUniIntents(Set<ConnectPoint> srcs,
+ ConnectPoint dst,
+ VlanId vlanId,
+ MacAddress mac) {
+ log.debug("Building mp-2-p intent to {}", dst);
+
+ List<MultiPointToSinglePointIntent> intents = new ArrayList<>();
+
+ TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
+
+
+ TrafficSelector.Builder builder = DefaultTrafficSelector.builder()
+ .matchEthDst(mac)
+ .matchVlanId(vlanId);
+
+ TrafficSelector selector = builder.build();
+
+ Key key = buildKey(PREFIX_UNICAST, dst, vlanId);
+
+ intents.add(MultiPointToSinglePointIntent.builder()
+ .appId(appId)
+ .key(key)
+ .selector(selector)
+ .treatment(treatment)
+ .ingressPoints(srcs)
+ .egressPoint(dst)
+ .priority(PRIORITY_OFFSET)
+ .build());
+ return intents;
+ }
+
+ /**
+ * Builds an intent Key for either for a Single Point to Multi Point or
+ * Multi Point to Single Point intent, based on a prefix that defines
+ * the type of intent, the single connection point representing the source
+ * or the destination and the vlan id representing the network.
+ *
+ * @param cPoint the source or destination connect point
+ * @param vlanId the network vlan id
+ * @param prefix prefix string
+ * @return
+ */
+ private Key buildKey(String prefix, ConnectPoint cPoint, VlanId vlanId) {
+ String keyString = new StringBuilder()
+ .append(prefix)
+ .append("-")
+ .append(cPoint.deviceId())
+ .append("-")
+ .append(cPoint.port())
+ .append("-")
+ .append(vlanId)
+
+ .toString();
+
+ return Key.of(keyString, appId);
+ }
+
+}
diff --git a/vpls/src/main/java/org/onos/vpls/Vpls.java b/vpls/src/main/java/org/onos/vpls/Vpls.java
new file mode 100644
index 0000000..c2c8611
--- /dev/null
+++ b/vpls/src/main/java/org/onos/vpls/Vpls.java
@@ -0,0 +1,248 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onos.vpls;
+
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.SetMultimap;
+import javafx.util.Pair;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.ControllerNode;
+import org.onosproject.cluster.LeadershipEvent;
+import org.onosproject.cluster.LeadershipEventListener;
+import org.onosproject.cluster.LeadershipService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.incubator.net.intf.InterfaceService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.Host;
+import org.onosproject.net.host.HostEvent;
+import org.onosproject.net.host.HostListener;
+import org.onosproject.net.host.HostService;
+import org.onosproject.net.intent.IntentService;
+import org.onosproject.sdnip.IntentSynchronizer;
+import org.slf4j.Logger;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Application to create L2 broadcast overlay networks using VLAN.
+ */
+@Component(immediate = true)
+public class Vpls {
+ private static final String VPLS_APP = "org.onosproject.vpls";
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected ClusterService clusterService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected HostService hostService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected IntentService intentService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected InterfaceService interfaceService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected LeadershipService leadershipService;
+
+ private final HostListener hostListener = new InternalHostListener();
+
+ private IntentInstaller intentInstaller;
+
+ private IntentSynchronizer intentSynchronizer;
+
+ private ApplicationId appId;
+
+ private LeadershipEventListener leadershipEventListener =
+ new InnerLeadershipEventListener();
+ private ControllerNode localControllerNode;
+
+ @Activate
+ protected void activate() {
+ appId = coreService.registerApplication(VPLS_APP);
+
+ localControllerNode = clusterService.getLocalNode();
+
+ intentSynchronizer = new IntentSynchronizer(appId, intentService);
+ intentSynchronizer.start();
+
+ intentInstaller = new IntentInstaller(appId, intentSynchronizer);
+
+ leadershipService.addListener(leadershipEventListener);
+ leadershipService.runForLeadership(appId.name());
+
+ hostService.addListener(hostListener);
+
+ setupConnectivity();
+
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ leadershipService.withdraw(appId.name());
+ leadershipService.removeListener(leadershipEventListener);
+
+ log.info("Stopped");
+ }
+
+ private void setupConnectivity() {
+ /*
+ * Parse Configuration and get Connect Point by VlanId.
+ */
+ SetMultimap<VlanId, ConnectPoint> confCPointsByVlan = getConfigCPoints();
+
+ /*
+ * Check that configured Connect Points have hosts attached and
+ * associate their Mac Address to the Connect Points configured.
+ */
+ SetMultimap<VlanId, Pair<ConnectPoint, MacAddress>> confHostPresentCPoint =
+ pairAvailableHosts(confCPointsByVlan);
+
+ /*
+ * Create and submit intents between the Connect Points.
+ * Intents for broadcast between all the configured Connect Points.
+ * Intents for unicast between all the configured Connect Points with
+ * hosts attached.
+ */
+ intentInstaller.installIntents(confHostPresentCPoint);
+ }
+
+ /**
+ * Computes the list of configured interfaces with a VLAN Id.
+ *
+ * @return the interfaces grouped by vlan id
+ */
+ private SetMultimap<VlanId, ConnectPoint> getConfigCPoints() {
+ log.debug("Checking interface configuration");
+
+ SetMultimap<VlanId, ConnectPoint> confCPointsByVlan =
+ HashMultimap.create();
+
+ interfaceService.getInterfaces()
+ .forEach(intf -> confCPointsByVlan.put(intf.vlan(),
+ intf.connectPoint()));
+ return confCPointsByVlan;
+ }
+
+ /**
+ * Checks if for any ConnectPoint configured there's an host present
+ * and in case it associate them together.
+ *
+ * @param confCPointsByVlan the configured ConnectPoints grouped by vlan id
+ * @return the configured ConnectPoints with eventual hosts associated.
+ */
+ private SetMultimap<VlanId, Pair<ConnectPoint, MacAddress>> pairAvailableHosts(
+ SetMultimap<VlanId, ConnectPoint> confCPointsByVlan) {
+ log.debug("Binding connected hosts mac addresses");
+
+ SetMultimap<VlanId, Pair<ConnectPoint, MacAddress>> confHostPresentCPoint =
+ HashMultimap.create();
+
+ confCPointsByVlan.entries()
+ .forEach(e -> bindMacAddr(e, confHostPresentCPoint));
+
+ return confHostPresentCPoint;
+ }
+
+ private void bindMacAddr(Map.Entry<VlanId, ConnectPoint> e,
+ SetMultimap<VlanId, Pair<ConnectPoint, MacAddress>> confHostPresentCPoint) {
+ VlanId vlanId = e.getKey();
+ ConnectPoint cp = e.getValue();
+ Set<Host> connectedHosts = hostService.getConnectedHosts(cp);
+ if (!connectedHosts.isEmpty()) {
+ connectedHosts.forEach(host -> {
+ if (host.vlan().equals(vlanId)) {
+ confHostPresentCPoint.put(vlanId, new Pair<>(cp, host.mac()));
+ } else {
+ confHostPresentCPoint.put(vlanId, new Pair<>(cp, null));
+ }
+ });
+ } else {
+ confHostPresentCPoint.put(vlanId, new Pair<>(cp, null));
+ }
+ }
+
+ /**
+ * A listener for Leadership Events.
+ */
+ private class InnerLeadershipEventListener
+ implements LeadershipEventListener {
+
+ @Override
+ public void event(LeadershipEvent event) {
+ log.debug("Leadership Event: time = {} type = {} event = {}",
+ event.time(), event.type(), event);
+
+ if (!event.subject().topic().equals(appId.name())) {
+ return; // Not our topic: ignore
+ }
+ if (!Objects.equals(event.subject().leader(), localControllerNode.id())) {
+ return; // The event is not about this instance: ignore
+ }
+
+ switch (event.type()) {
+ case LEADER_ELECTED:
+ log.info("Leader Elected");
+ intentSynchronizer.leaderChanged(true);
+ break;
+ case LEADER_BOOTED:
+ log.info("Leader Lost Election");
+ intentSynchronizer.leaderChanged(false);
+ break;
+ case LEADER_REELECTED:
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ /**
+ * Listener for host events.
+ */
+ class InternalHostListener implements HostListener {
+ @Override
+ public void event(HostEvent event) {
+ log.debug("Received HostEvent {}", event);
+ switch (event.type()) {
+ case HOST_ADDED:
+ case HOST_UPDATED:
+ case HOST_REMOVED:
+ setupConnectivity();
+ break;
+ default:
+ break;
+ }
+ }
+ }
+}
diff --git a/vpls/src/main/java/org/onos/vpls/package-info.java b/vpls/src/main/java/org/onos/vpls/package-info.java
new file mode 100644
index 0000000..6a6a0a1
--- /dev/null
+++ b/vpls/src/main/java/org/onos/vpls/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Application to create L2 broadcast network using VLAN.
+ */
+package org.onos.vpls;