Polling link provider
The provider is capable of polling the environment using the
device driver behaviour (LinkDiscovery)
Change-Id: Ia09f866299d38c4de9b13201c9cf63b03909164a
diff --git a/providers/link/BUCK b/providers/link/BUCK
new file mode 100644
index 0000000..c8a288f
--- /dev/null
+++ b/providers/link/BUCK
@@ -0,0 +1,21 @@
+COMPILE_DEPS = [
+ '//lib:CORE_DEPS',
+]
+
+TEST_DEPS = [
+ '//lib:TEST_ADAPTERS',
+ '//utils/osgi:onlab-osgi-tests',
+]
+
+osgi_jar_with_tests (
+ deps = COMPILE_DEPS,
+ test_deps = TEST_DEPS,
+)
+
+onos_app (
+ app_name = 'org.onosproject.linkdiscovery',
+ title = 'Link Discovery Provider',
+ category = 'Provider',
+ url = 'http://onosproject.org',
+ description = 'ONOS link discovery provider.',
+)
\ No newline at end of file
diff --git a/providers/link/app.xml b/providers/link/app.xml
new file mode 100644
index 0000000..da36ba8
--- /dev/null
+++ b/providers/link/app.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ ~ Copyright 2016-present 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.
+ -->
+<app name="org.onosproject.linkdiscovery" origin="acino.eu" version="${project.version}"
+ category="Provider" url="http://onosproject.org" title="ONOS link discovery provider"
+ featuresRepo="mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features"
+ features="${project.artifactId}">
+ <description>${project.description}</description>
+ <artifact>mvn:${project.groupId}/onos-linkdiscovery-provider/${project.version}</artifact>
+</app>
diff --git a/providers/link/features.xml b/providers/link/features.xml
new file mode 100644
index 0000000..bffae0e
--- /dev/null
+++ b/providers/link/features.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+
+<!--
+ ~ Copyright 2016-present 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}">
+ <feature name="${project.artifactId}" version="${project.version}"
+ description="${project.description}">
+ <feature>onos-api</feature>
+ <bundle>mvn:${project.groupId}/onos-linkdiscovery-provider/${project.version}</bundle>
+ </feature>
+</features>
+
diff --git a/providers/link/pom.xml b/providers/link/pom.xml
new file mode 100644
index 0000000..3af585b
--- /dev/null
+++ b/providers/link/pom.xml
@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2016-present 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-providers</artifactId>
+ <version>1.7.0-SNAPSHOT</version>
+ </parent>
+
+
+ <artifactId>onos-linkdiscovery-provider</artifactId>
+ <packaging>bundle</packaging>
+
+ <properties>
+ <onos.app.name>org.onosproject.linkdiscovery</onos.app.name>
+ <onos.app.title>Link Discovery Provider</onos.app.title>
+ <onos.app.origin>acino.eu</onos.app.origin>
+ <onos.app.category>Provider</onos.app.category>
+ <onos.app.readme>ONOS link discovery provider.</onos.app.readme>
+ </properties>
+
+ <description>ONOS link discovery provider</description>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onlab-osgi</artifactId>
+ <version>${project.version}</version>
+ <classifier>tests</classifier>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onlab-junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymock</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-api</artifactId>
+ <classifier>tests</classifier>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/providers/link/src/main/java/org/onosproject/provider/linkdiscovery/impl/LinkDiscoveryProvider.java b/providers/link/src/main/java/org/onosproject/provider/linkdiscovery/impl/LinkDiscoveryProvider.java
new file mode 100644
index 0000000..a5fc042
--- /dev/null
+++ b/providers/link/src/main/java/org/onosproject/provider/linkdiscovery/impl/LinkDiscoveryProvider.java
@@ -0,0 +1,281 @@
+/*
+ * Copyright 2016-present 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.onosproject.provider.linkdiscovery.impl;
+
+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.Modified;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Link;
+import org.onosproject.net.behaviour.LinkDiscovery;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceListener;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.link.DefaultLinkDescription;
+import org.onosproject.net.link.LinkDescription;
+import org.onosproject.net.link.LinkProvider;
+import org.onosproject.net.link.LinkProviderRegistry;
+import org.onosproject.net.link.LinkProviderService;
+import org.onosproject.net.link.LinkService;
+import org.onosproject.net.provider.AbstractProvider;
+import org.onosproject.net.provider.ProviderId;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.Strings.isNullOrEmpty;
+import static java.util.concurrent.Executors.newScheduledThreadPool;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.onlab.util.Tools.get;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Link provider capable of polling the environment using the device driver
+ * {@link LinkDiscovery} behaviour.
+ */
+@Component(immediate = true)
+public class LinkDiscoveryProvider extends AbstractProvider
+ implements LinkProvider {
+
+ protected static final String APP_NAME = "org.onosproject.linkdiscovery";
+ protected static final String SCHEME_NAME = "linkdiscovery";
+ private static final String LINK_PROVIDER_PACKAGE = "org.onosproject.provider.linkdiscovery";
+ private final Logger log = getLogger(getClass());
+ private static final int DEFAULT_POLL_DELAY_SECONDS = 20;
+ @Property(name = "linkPollDelaySeconds", intValue = DEFAULT_POLL_DELAY_SECONDS,
+ label = "Initial delay (in seconds) for polling link discovery")
+ protected static int linkPollDelaySeconds = DEFAULT_POLL_DELAY_SECONDS;
+ private static final int DEFAULT_POLL_FREQUENCY_SECONDS = 10;
+ @Property(name = "linkPollFrequencySeconds", intValue = DEFAULT_POLL_FREQUENCY_SECONDS,
+ label = "Frequency (in seconds) for polling link discovery")
+ protected static int linkPollFrequencySeconds = DEFAULT_POLL_FREQUENCY_SECONDS;
+
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected LinkProviderRegistry providerRegistry;
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected LinkService linkService;
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected MastershipService mastershipService;
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected DeviceService deviceService;
+ protected ScheduledExecutorService executor =
+ newScheduledThreadPool(2, groupedThreads("onos/netconf-link",
+ "discovery-%d"));
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected ComponentConfigService cfgService;
+
+ protected LinkProviderService providerService;
+ private InternalDeviceListener deviceListener = new InternalDeviceListener();
+ private ApplicationId appId;
+ private ScheduledFuture<?> scheduledTask;
+
+ /**
+ * Creates a provider with the supplied identifier.
+ */
+ public LinkDiscoveryProvider() {
+ super(new ProviderId(SCHEME_NAME, LINK_PROVIDER_PACKAGE));
+ }
+
+ @Activate
+ public void activate(ComponentContext context) {
+ providerService = providerRegistry.register(this);
+ appId = coreService.registerApplication(APP_NAME);
+ deviceService.addListener(deviceListener);
+ cfgService.registerProperties(getClass());
+
+ if (context == null) {
+ linkPollFrequencySeconds = DEFAULT_POLL_FREQUENCY_SECONDS;
+ log.info("No component configuration");
+ } else {
+ Dictionary<?, ?> properties = context.getProperties();
+ linkPollFrequencySeconds =
+ getNewPollFrequency(properties, linkPollFrequencySeconds);
+ }
+ scheduledTask = schedulePolling();
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ cfgService.unregisterProperties(getClass(), false);
+ deviceService.removeListener(deviceListener);
+ providerRegistry.unregister(this);
+ providerService = null;
+ scheduledTask.cancel(true);
+ executor.shutdown();
+ log.info("Stopped");
+ }
+
+ @Modified
+ public void modified(ComponentContext context) {
+ if (context == null) {
+ log.info("No component configuration");
+ return;
+ } else {
+ Dictionary<?, ?> properties = context.getProperties();
+
+ int newPollFrequency = getNewPollFrequency(properties, linkPollFrequencySeconds);
+ int newPollDelay = getNewPollDealy(properties, linkPollDelaySeconds);
+ if (newPollFrequency != linkPollFrequencySeconds ||
+ newPollDelay != linkPollDelaySeconds) {
+ linkPollFrequencySeconds = newPollFrequency;
+ linkPollDelaySeconds = newPollDelay;
+ //stops the old scheduled task
+ scheduledTask.cancel(true);
+ //schedules new task at the new polling rate
+ scheduledTask = schedulePolling();
+ }
+ }
+ log.info("Modified");
+ }
+
+ private int getNewPollFrequency(Dictionary<?, ?> properties, int pollFrequency) {
+ int newPollFrequency;
+ try {
+ String s = get(properties, "linkPollFrequencySeconds");
+ newPollFrequency = isNullOrEmpty(s) ? pollFrequency : Integer.parseInt(s.trim());
+ } catch (NumberFormatException | ClassCastException e) {
+ newPollFrequency = DEFAULT_POLL_FREQUENCY_SECONDS;
+ }
+ return newPollFrequency;
+ }
+
+ private int getNewPollDealy(Dictionary<?, ?> properties, int pollDelay) {
+ int newPollFrequency;
+ try {
+ String s = get(properties, "linkPollDelaySeconds");
+ newPollFrequency = isNullOrEmpty(s) ? pollDelay : Integer.parseInt(s.trim());
+ } catch (NumberFormatException | ClassCastException e) {
+ newPollFrequency = DEFAULT_POLL_DELAY_SECONDS;
+ }
+ return newPollFrequency;
+ }
+
+ private ScheduledFuture schedulePolling() {
+ return executor.scheduleAtFixedRate(this::discoverLinksTasks,
+ linkPollDelaySeconds,
+ linkPollFrequencySeconds,
+ SECONDS);
+ }
+
+ private void discoverLinksTasks() {
+ deviceService.getAvailableDevices().forEach(device -> {
+ if (isSupported(device)) {
+ evaluateLinks(device.id(), device.as(LinkDiscovery.class).getLinks());
+ }
+ });
+ }
+
+ private void evaluateLinks(DeviceId deviceId, Set<LinkDescription> discoveredLinksDesc) {
+ //The provider will get only existing links related to LinkDiscovery
+ Set<Link> storedLinks = linkService.getDeviceEgressLinks(deviceId)
+ .stream()
+ .filter(link -> {
+ String value = link.annotations().value(AnnotationKeys.PROTOCOL);
+ if (value != null && value.equals(SCHEME_NAME.toUpperCase())) {
+ return true;
+ }
+ return false;
+ })
+ .collect(Collectors.toSet());
+
+ //Convert Link to LinkDescription for comparison
+ Set<LinkDescription> storedLinkDescs = new HashSet<>();
+ storedLinks.forEach(link -> storedLinkDescs
+ .add(new DefaultLinkDescription(
+ link.src(), link.dst(), link.type(), link.isExpected(),
+ DefaultAnnotations.builder().putAll(link.annotations()).build())));
+ log.debug("Current stored links provider related {}", storedLinks);
+
+ //Add the correct annotation for comparison
+ Set<LinkDescription> discoveredLinkDescsAnn = new HashSet<>();
+
+ discoveredLinksDesc.forEach(linkDesc -> discoveredLinkDescsAnn
+ .add(new DefaultLinkDescription(
+ linkDesc.src(), linkDesc.dst(), linkDesc.type(), false,
+ DefaultAnnotations.builder().putAll(linkDesc.annotations())
+ .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase())
+ .build())));
+
+ Set<LinkDescription> linkDescsToBeRemoved = new HashSet<>(storedLinkDescs);
+ linkDescsToBeRemoved.removeAll(discoveredLinkDescsAnn);
+ log.debug("Links to be removed {}", linkDescsToBeRemoved);
+ linkDescsToBeRemoved.forEach(linkDesc ->
+ providerService.linkVanished(linkDesc));
+
+ Set<LinkDescription> linksToBeAdded = new HashSet<>(discoveredLinkDescsAnn);
+ linksToBeAdded.removeAll(storedLinkDescs);
+ log.debug("Links to be added {}", linksToBeAdded);
+ linksToBeAdded.forEach(linkDesc -> providerService.linkDetected(linkDesc)
+ );
+ }
+
+ protected boolean isSupported(Device device) {
+ boolean supported = mastershipService.isLocalMaster(device.id())
+ && device.is(LinkDiscovery.class);
+ if (!supported) {
+ log.debug("Device {} does not support LinkDiscovery", device);
+ }
+ return supported;
+ }
+
+ /**
+ * Listener for core device events.
+ */
+ private class InternalDeviceListener implements DeviceListener {
+ @Override
+ public void event(DeviceEvent event) {
+ if ((event.type() == DeviceEvent.Type.DEVICE_ADDED)) {
+ executor.execute(() -> event.subject().as(LinkDiscovery.class).getLinks()
+ .forEach(linkDesc -> {
+ providerService.linkDetected(new DefaultLinkDescription(
+ linkDesc.src(), linkDesc.dst(), linkDesc.type(), false,
+ DefaultAnnotations.builder()
+ .putAll(linkDesc.annotations())
+ .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase())
+ .build()));
+ }));
+ }
+ }
+
+ @Override
+ public boolean isRelevant(DeviceEvent event) {
+ return isSupported(event.subject());
+ }
+ }
+}
diff --git a/providers/link/src/main/java/org/onosproject/provider/linkdiscovery/impl/package-info.java b/providers/link/src/main/java/org/onosproject/provider/linkdiscovery/impl/package-info.java
new file mode 100644
index 0000000..e3dcae2
--- /dev/null
+++ b/providers/link/src/main/java/org/onosproject/provider/linkdiscovery/impl/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2016-present 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.
+ */
+
+/**
+ * Provider that uses LinkDiscovery {@link org.onosproject.net.behaviour.LinkDiscovery}
+ * to poll the network environment as a means of link discovery.
+ */
+package org.onosproject.provider.linkdiscovery.impl;
\ No newline at end of file
diff --git a/providers/link/src/test/java/org/onosproject/provider/linkdiscovery/impl/LinkDiscoveryProviderTest.java b/providers/link/src/test/java/org/onosproject/provider/linkdiscovery/impl/LinkDiscoveryProviderTest.java
new file mode 100644
index 0000000..0f272fb
--- /dev/null
+++ b/providers/link/src/test/java/org/onosproject/provider/linkdiscovery/impl/LinkDiscoveryProviderTest.java
@@ -0,0 +1,305 @@
+/*
+ * Copyright 2016-present 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.onosproject.provider.linkdiscovery.impl;
+
+import com.google.common.collect.ImmutableSet;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ComponentContextAdapter;
+import org.onlab.packet.ChassisId;
+import org.onosproject.cfg.ComponentConfigAdapter;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.mastership.MastershipService;
+import org.onosproject.mastership.MastershipServiceAdapter;
+import org.onosproject.net.AbstractProjectableModel;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.Annotations;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DefaultDevice;
+import org.onosproject.net.DefaultLink;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Link;
+import org.onosproject.net.LinkKey;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.LinkDiscovery;
+import org.onosproject.net.device.DeviceListener;
+import org.onosproject.net.device.DeviceServiceAdapter;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.Behaviour;
+import org.onosproject.net.driver.Driver;
+import org.onosproject.net.driver.DriverAdapter;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.net.driver.DriverServiceAdapter;
+import org.onosproject.net.link.DefaultLinkDescription;
+import org.onosproject.net.link.LinkDescription;
+import org.onosproject.net.link.LinkProviderRegistryAdapter;
+import org.onosproject.net.link.LinkProviderServiceAdapter;
+import org.onosproject.net.link.LinkServiceAdapter;
+import org.onosproject.net.provider.ProviderId;
+
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Set;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
+import static org.onlab.junit.TestTools.assertAfter;
+import static org.onosproject.provider.linkdiscovery.impl.LinkDiscoveryProvider.APP_NAME;
+import static org.onosproject.provider.linkdiscovery.impl.LinkDiscoveryProvider.SCHEME_NAME;
+
+/**
+ * Test for polling mechanism of the NetconfLinkProvider.
+ */
+public class LinkDiscoveryProviderTest {
+
+ private static final ComponentContextAdapter CONTEXT =
+ new ComponentContextAdapter() {
+ @Override
+ public Dictionary getProperties() {
+ Hashtable<String, Integer> props = new Hashtable<>();
+ props.put("linkPollFrequencySeconds", 2);
+ props.put("linkPollDelaySeconds", 1);
+ return props;
+ }
+ };
+ // Network Mocks
+ private static final DeviceId DEVICE_ID_1 = DeviceId.deviceId("netconf:1.1.1.1");
+ private static final DeviceId DEVICE_ID_2 = DeviceId.deviceId("netconf:1.1.1.2");
+ private static final ConnectPoint CP11 = new ConnectPoint(DEVICE_ID_1, PortNumber.portNumber(1));
+ private static final ConnectPoint CP12 = new ConnectPoint(DEVICE_ID_1, PortNumber.portNumber(2));
+ private static final ConnectPoint CP13 = new ConnectPoint(DEVICE_ID_1, PortNumber.portNumber(3));
+ private static final ConnectPoint CP14 = new ConnectPoint(DEVICE_ID_1, PortNumber.portNumber(4));
+ private static final ConnectPoint CP21 = new ConnectPoint(DEVICE_ID_2, PortNumber.portNumber(1));
+ private static final ConnectPoint CP22 = new ConnectPoint(DEVICE_ID_2, PortNumber.portNumber(2));
+ private static final ConnectPoint CP23 = new ConnectPoint(DEVICE_ID_2, PortNumber.portNumber(3));
+ private static final ConnectPoint CP24 = new ConnectPoint(DEVICE_ID_2, PortNumber.portNumber(4));
+ private static final DefaultAnnotations DEVICE_ANNOTATIONS = DefaultAnnotations.builder()
+ .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()).build();
+ private static final DefaultAnnotations LINK_ANNOTATIONS = DefaultAnnotations.builder()
+ .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()).build();
+ private static final LinkKey LINKKEY1 = LinkKey.linkKey(CP11, CP21);
+ private static final LinkDescription LINK1 = new DefaultLinkDescription(CP11, CP21,
+ Link.Type.DIRECT,
+ LINK_ANNOTATIONS);
+ private static final LinkKey LINKKEY2 = LinkKey.linkKey(CP12, CP22);
+ private static final LinkDescription LINK2 = new DefaultLinkDescription(CP12, CP22,
+ Link.Type.DIRECT,
+ LINK_ANNOTATIONS);
+ private static final LinkKey LINKKEY3 = LinkKey.linkKey(CP13, CP23);
+ private static final LinkDescription LINK3 = new DefaultLinkDescription(CP13, CP23,
+ Link.Type.DIRECT,
+ DefaultAnnotations.builder()
+ .build());
+ private static final LinkKey LINKKEY4 = LinkKey.linkKey(CP14, CP24);
+ private static final LinkDescription LINK4 = new DefaultLinkDescription(CP14, CP24,
+ Link.Type.DIRECT,
+ DefaultAnnotations.builder().build());
+ //Service Mocks
+ private final MockDeviceService deviceService = new MockDeviceService();
+ private final LinkProviderRegistryAdapter linkRegistry = new LinkProviderRegistryAdapter();
+ private final MastershipService mastershipService = new MockMastershipService();
+ private final MockLinkService linkService = new MockLinkService();
+ private final Driver driver = new MockDriver();
+ //Provider related classes
+ private LinkProviderServiceAdapter providerService;
+ private CoreService coreService;
+ private LinkDiscoveryProvider provider = new LinkDiscoveryProvider();
+ private final Device device1 = new MockDevice(provider.id(), DEVICE_ID_1, Device.Type.SWITCH,
+ "foo.inc", "0", "0", "0", new ChassisId(),
+ DEVICE_ANNOTATIONS);
+ private Set<DeviceListener> deviceListeners = new HashSet<>();
+ private ApplicationId appId =
+ new DefaultApplicationId(100, APP_NAME);
+ private TestLink testLink = new TestLink();
+
+ @Before
+ public void setUp() {
+ coreService = createMock(CoreService.class);
+ expect(coreService.registerApplication(appId.name()))
+ .andReturn(appId).anyTimes();
+ replay(coreService);
+ provider.coreService = coreService;
+ provider.providerRegistry = linkRegistry;
+ provider.deviceService = deviceService;
+ provider.mastershipService = mastershipService;
+ provider.linkService = linkService;
+ provider.cfgService = new ComponentConfigAdapter();
+ AbstractProjectableModel.setDriverService(null, new DriverServiceAdapter());
+ provider.activate(null);
+ providerService = linkRegistry.registeredProvider();
+ }
+
+ @Test
+ public void activate() throws Exception {
+ assertFalse("Provider should be registered", linkRegistry.getProviders().contains(provider));
+ assertEquals("Device service should be registered", provider.deviceService, deviceService);
+ assertEquals("Device listener should be added", 1, deviceListeners.size());
+ assertNotNull("Registration expected", providerService);
+ assertEquals("Incorrect provider", provider, providerService.provider());
+ assertFalse("Executor should be running", provider.executor.isShutdown());
+ assertFalse("Executor should be running", provider.executor.isTerminated());
+ assertEquals("Incorrect polling frequency, should be default", 10,
+ provider.linkPollFrequencySeconds);
+ assertEquals("Incorrect polling delay , should be default", 20,
+ provider.linkPollDelaySeconds);
+ }
+
+ @Test
+ public void modified() throws Exception {
+ provider.modified(CONTEXT);
+ assertEquals("Incorrect polling frequency, should be default", 2,
+ provider.linkPollFrequencySeconds);
+ assertEquals("Incorrect polling delay , should be default", 1,
+ provider.linkPollDelaySeconds);
+
+ }
+
+
+ @Test
+ public void deactivate() throws Exception {
+ provider.deactivate();
+ assertEquals("Device listener should be removed", 0, deviceListeners.size());
+ assertFalse("Provider should not be registered", linkRegistry.getProviders().contains(provider));
+ assertTrue(provider.executor.isShutdown());
+ assertNull(provider.providerService);
+ }
+
+
+ @Test
+ public void linksTestForStoredDevice() {
+ provider.modified(CONTEXT);
+ providerService.discoveredLinkDescriptions().put(LINKKEY1, LINK1);
+ providerService.discoveredLinkDescriptions().put(LINKKEY2, LINK2);
+ providerService.discoveredLinkDescriptions().put(LINKKEY4, LINK4);
+ testLink.addLinkDesc(LINK2);
+ testLink.addLinkDesc(LINK3);
+ assertAfter(1100, () -> {
+ assertEquals("Total number of link must be 3", 3, providerService.discoveredLinkDescriptions().size());
+ assertFalse("Link1 should be removed",
+ providerService.discoveredLinkDescriptions().containsKey(LINKKEY1));
+ assertTrue("Link2 should be present",
+ providerService.discoveredLinkDescriptions().containsKey(LINKKEY2));
+ assertTrue("Link3 should be added",
+ providerService.discoveredLinkDescriptions().containsKey(LINKKEY3));
+ assertEquals("Link3 should be annotated", SCHEME_NAME.toUpperCase(),
+ providerService.discoveredLinkDescriptions()
+ .get(LINKKEY3).annotations().value(AnnotationKeys.PROTOCOL));
+ assertTrue("Link4 should be present because it is not related to the LinkDiscovery",
+ providerService.discoveredLinkDescriptions().containsKey(LINKKEY4));
+
+ });
+ clear();
+ }
+
+ private void clear() {
+ testLink.clearLinkDesc();
+ providerService.discoveredLinkDescriptions().clear();
+ providerService.discoveredLinks().clear();
+ }
+
+ private class MockDeviceService extends DeviceServiceAdapter {
+
+ @Override
+ public Iterable<Device> getAvailableDevices() {
+ return ImmutableSet.of(device1);
+ }
+
+ @Override
+ public void addListener(DeviceListener listener) {
+ deviceListeners.add(listener);
+ }
+
+ @Override
+ public void removeListener(DeviceListener listener) {
+ deviceListeners.remove(listener);
+ }
+ }
+
+ private class MockMastershipService extends MastershipServiceAdapter {
+
+ @Override
+ public boolean isLocalMaster(DeviceId deviceId) {
+ return true;
+ }
+ }
+
+ private class MockLinkService extends LinkServiceAdapter {
+ @Override
+ public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
+ Set<Link> links = new HashSet<>();
+ providerService.discoveredLinkDescriptions().values()
+ .forEach(x -> links.add(DefaultLink.builder()
+ .providerId(provider.id())
+ .src(x.src())
+ .dst(x.dst())
+ .type(x.type())
+ .isExpected(x.isExpected())
+ .annotations(x.annotations()).build()));
+ return ImmutableSet.copyOf(links);
+ }
+ }
+
+ private class MockDevice extends DefaultDevice {
+
+ public MockDevice(ProviderId providerId, DeviceId id, Type type,
+ String manufacturer, String hwVersion, String swVersion,
+ String serialNumber, ChassisId chassisId, Annotations... annotations) {
+ super(providerId, id, type, manufacturer, hwVersion, swVersion, serialNumber,
+ chassisId, annotations);
+ }
+
+ @Override
+ protected Driver locateDriver() {
+ return driver;
+ }
+
+ @Override
+ public Driver driver() {
+ return driver;
+ }
+ }
+
+ private class MockDriver extends DriverAdapter {
+ @Override
+ public <T extends Behaviour> T createBehaviour(DriverHandler handler, Class<T> behaviourClass) {
+
+ return (T) testLink;
+ }
+ }
+
+ private class TestLink extends AbstractHandlerBehaviour implements LinkDiscovery {
+ Set<LinkDescription> linkDescriptions = new HashSet<>();
+
+ @Override
+ public Set<LinkDescription> getLinks() {
+ return ImmutableSet.copyOf(linkDescriptions);
+ }
+
+ private void addLinkDesc(LinkDescription link) {
+ linkDescriptions.add(link);
+ }
+
+ private void clearLinkDesc() {
+ linkDescriptions.clear();
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/providers/link/src/test/java/org/onosproject/provider/linkdiscovery/impl/package-info.java b/providers/link/src/test/java/org/onosproject/provider/linkdiscovery/impl/package-info.java
new file mode 100644
index 0000000..7e71928
--- /dev/null
+++ b/providers/link/src/test/java/org/onosproject/provider/linkdiscovery/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present 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.
+ */
+
+/**
+ * Test package for the Link Discovery Provider.
+ */
+package org.onosproject.provider.linkdiscovery.impl;
\ No newline at end of file
diff --git a/providers/pom.xml b/providers/pom.xml
index 5ec5a89..8b71910 100644
--- a/providers/pom.xml
+++ b/providers/pom.xml
@@ -49,6 +49,7 @@
<module>isis</module>
<module>lisp</module>
<module>ospf</module>
+ <module>link</module>
</modules>
<dependencies>