aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/providers/ovsdb/host
diff options
context:
space:
mode:
authorAshlee Young <ashlee@onosfw.com>2015-09-09 22:15:21 -0700
committerAshlee Young <ashlee@onosfw.com>2015-09-09 22:15:21 -0700
commit13d05bc8458758ee39cb829098241e89616717ee (patch)
tree22a4d1ce65f15952f07a3df5af4b462b4697cb3a /framework/src/onos/providers/ovsdb/host
parent6139282e1e93c2322076de4b91b1c85d0bc4a8b3 (diff)
ONOS checkin based on commit tag e796610b1f721d02f9b0e213cf6f7790c10ecd60
Change-Id: Ife8810491034fe7becdba75dda20de4267bd15cd
Diffstat (limited to 'framework/src/onos/providers/ovsdb/host')
-rw-r--r--framework/src/onos/providers/ovsdb/host/pom.xml39
-rw-r--r--framework/src/onos/providers/ovsdb/host/src/main/java/org/onosproject/ovsdb/provider/host/OvsdbHostProvider.java148
-rw-r--r--framework/src/onos/providers/ovsdb/host/src/main/java/org/onosproject/ovsdb/provider/host/package-info.java21
-rw-r--r--framework/src/onos/providers/ovsdb/host/src/test/java/org/onosproject/ovsdb/provider/host/OvsdbHostProviderTest.java199
4 files changed, 407 insertions, 0 deletions
diff --git a/framework/src/onos/providers/ovsdb/host/pom.xml b/framework/src/onos/providers/ovsdb/host/pom.xml
new file mode 100644
index 00000000..0bd88555
--- /dev/null
+++ b/framework/src/onos/providers/ovsdb/host/pom.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0"?>
+<!--
+ ~ 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
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+ xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-ovsdb-providers</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>onos-ovsdb-provider-host</artifactId>
+ <packaging>bundle</packaging>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-ovsdb-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git a/framework/src/onos/providers/ovsdb/host/src/main/java/org/onosproject/ovsdb/provider/host/OvsdbHostProvider.java b/framework/src/onos/providers/ovsdb/host/src/main/java/org/onosproject/ovsdb/provider/host/OvsdbHostProvider.java
new file mode 100644
index 00000000..8d2fd142
--- /dev/null
+++ b/framework/src/onos/providers/ovsdb/host/src/main/java/org/onosproject/ovsdb/provider/host/OvsdbHostProvider.java
@@ -0,0 +1,148 @@
+/*
+ * 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.
+ */
+package org.onosproject.ovsdb.provider.host;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.toHex;
+import static org.slf4j.LoggerFactory.getLogger;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+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.apache.felix.scr.annotations.Service;
+import org.onlab.packet.VlanId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Host;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.host.DefaultHostDescription;
+import org.onosproject.net.host.HostDescription;
+import org.onosproject.net.host.HostProvider;
+import org.onosproject.net.host.HostProviderRegistry;
+import org.onosproject.net.host.HostProviderService;
+import org.onosproject.net.host.HostService;
+import org.onosproject.net.provider.AbstractProvider;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.ovsdb.controller.EventSubject;
+import org.onosproject.ovsdb.controller.OvsdbController;
+import org.onosproject.ovsdb.controller.OvsdbEvent;
+import org.onosproject.ovsdb.controller.OvsdbEventListener;
+import org.onosproject.ovsdb.controller.OvsdbEventSubject;
+import org.slf4j.Logger;
+
+/**
+ * Provider which uses an ovsdb controller to detect host.
+ */
+@Component(immediate = true)
+@Service
+public class OvsdbHostProvider extends AbstractProvider implements HostProvider {
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected HostProviderRegistry providerRegistry;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected HostService hostService;
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected OvsdbController controller;
+
+ private HostProviderService providerService;
+ private OvsdbEventListener innerEventListener = new InnerOvsdbEventListener();
+
+ @Activate
+ public void activate() {
+ providerService = providerRegistry.register(this);
+ controller.addOvsdbEventListener(innerEventListener);
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ providerRegistry.unregister(this);
+ providerService = null;
+ log.info("Stopped");
+ }
+
+ public OvsdbHostProvider() {
+ super(new ProviderId("ovsdb", "org.onosproject.ovsdb.provider.host"));
+ }
+
+ @Override
+ public void triggerProbe(Host host) {
+ log.info("Triggering probe on host {}", host);
+ }
+
+ private class InnerOvsdbEventListener implements OvsdbEventListener {
+
+ @Override
+ public void handle(OvsdbEvent<EventSubject> event) {
+ OvsdbEventSubject subject = null;
+ if (event.subject() instanceof OvsdbEventSubject) {
+ subject = (OvsdbEventSubject) event.subject();
+ }
+ checkNotNull(subject, "EventSubject is not null");
+ // If ifaceid is null,it indicates this is not a vm port.
+ if (subject.ifaceid() == null) {
+ return;
+ }
+ switch (event.type()) {
+ case PORT_ADDED:
+ HostId hostId = HostId.hostId(subject.hwAddress(), null);
+ DeviceId deviceId = DeviceId.deviceId(uri(subject.dpid().value()));
+ PortNumber portNumber = PortNumber.portNumber(subject
+ .portNumber().value(), subject.portName().value());
+ HostLocation loaction = new HostLocation(deviceId, portNumber,
+ 0L);
+ SparseAnnotations annotations = DefaultAnnotations.builder()
+ .set("ifaceid", subject.ifaceid().value()).build();
+ HostDescription hostDescription = new DefaultHostDescription(
+ subject.hwAddress(),
+ VlanId.vlanId(),
+ loaction,
+ annotations);
+ providerService.hostDetected(hostId, hostDescription);
+ break;
+ case PORT_REMOVED:
+ HostId host = HostId.hostId(subject.hwAddress(), null);
+ providerService.hostVanished(host);
+ break;
+ default:
+ break;
+ }
+
+ }
+
+ }
+
+ public URI uri(String value) {
+ try {
+ return new URI("of", toHex(Long.valueOf(value)), null);
+ } catch (URISyntaxException e) {
+ return null;
+ }
+ }
+}
diff --git a/framework/src/onos/providers/ovsdb/host/src/main/java/org/onosproject/ovsdb/provider/host/package-info.java b/framework/src/onos/providers/ovsdb/host/src/main/java/org/onosproject/ovsdb/provider/host/package-info.java
new file mode 100644
index 00000000..895ae2ec
--- /dev/null
+++ b/framework/src/onos/providers/ovsdb/host/src/main/java/org/onosproject/ovsdb/provider/host/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * 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.
+ */
+
+/**
+ *Provider that uses ovsdb controller as a means of infrastructure host discovery.
+ *
+ */
+package org.onosproject.ovsdb.provider.host; \ No newline at end of file
diff --git a/framework/src/onos/providers/ovsdb/host/src/test/java/org/onosproject/ovsdb/provider/host/OvsdbHostProviderTest.java b/framework/src/onos/providers/ovsdb/host/src/test/java/org/onosproject/ovsdb/provider/host/OvsdbHostProviderTest.java
new file mode 100644
index 00000000..bf18606c
--- /dev/null
+++ b/framework/src/onos/providers/ovsdb/host/src/test/java/org/onosproject/ovsdb/provider/host/OvsdbHostProviderTest.java
@@ -0,0 +1,199 @@
+/*
+ * 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.
+ */
+package org.onosproject.ovsdb.provider.host;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.List;
+import java.util.Set;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.MacAddress;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.host.HostDescription;
+import org.onosproject.net.host.HostProvider;
+import org.onosproject.net.host.HostProviderRegistry;
+import org.onosproject.net.host.HostProviderService;
+import org.onosproject.net.provider.AbstractProviderService;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.ovsdb.controller.DefaultEventSubject;
+import org.onosproject.ovsdb.controller.EventSubject;
+import org.onosproject.ovsdb.controller.OvsdbClientService;
+import org.onosproject.ovsdb.controller.OvsdbController;
+import org.onosproject.ovsdb.controller.OvsdbDatapathId;
+import org.onosproject.ovsdb.controller.OvsdbEvent;
+import org.onosproject.ovsdb.controller.OvsdbEventListener;
+import org.onosproject.ovsdb.controller.OvsdbIfaceId;
+import org.onosproject.ovsdb.controller.OvsdbNodeId;
+import org.onosproject.ovsdb.controller.OvsdbNodeListener;
+import org.onosproject.ovsdb.controller.OvsdbPortName;
+import org.onosproject.ovsdb.controller.OvsdbPortNumber;
+import org.onosproject.ovsdb.controller.OvsdbPortType;
+
+/**
+ * Test for ovsdb host provider.
+ */
+public class OvsdbHostProviderTest {
+ private static final MacAddress MAC = MacAddress
+ .valueOf("00:00:11:00:00:01");
+ private final OvsdbHostProvider provider = new OvsdbHostProvider();
+ private final TestHostRegistry hostRegistry = new TestHostRegistry();
+ protected OvsdbControllerTest controller = new OvsdbControllerTest();
+ private TestHostProviderService providerService;
+
+ @Before
+ public void setUp() {
+ provider.providerRegistry = hostRegistry;
+ provider.controller = controller;
+ provider.activate();
+ }
+
+ @Test
+ public void basics() {
+ assertNotNull("registration expected", providerService);
+ assertEquals("incorrect provider", provider, providerService.provider());
+ }
+
+ @Test
+ public void portAdded() {
+ DefaultEventSubject eventSubject = new DefaultEventSubject(MAC, null,
+ new OvsdbPortName("portName"),
+ new OvsdbPortNumber(0L),
+ new OvsdbDatapathId("10002"),
+ new OvsdbPortType("vxlan"),
+ new OvsdbIfaceId("102345"));
+ controller.ovsdbEventListener
+ .handle(new OvsdbEvent<EventSubject>(
+ OvsdbEvent.Type.PORT_ADDED,
+ eventSubject));
+ assertNotNull("never went throught the provider service",
+ providerService.added);
+
+ }
+
+ @Test
+ public void portRemoved() {
+ DefaultEventSubject eventSubject = new DefaultEventSubject(MAC, null,
+ new OvsdbPortName("portName"),
+ new OvsdbPortNumber(0L),
+ new OvsdbDatapathId("10002"),
+ new OvsdbPortType("vxlan"),
+ new OvsdbIfaceId("102345"));
+ controller.ovsdbEventListener
+ .handle(new OvsdbEvent<EventSubject>(
+ OvsdbEvent.Type.PORT_REMOVED,
+ eventSubject));
+ assertEquals("port status unhandled", 1, providerService.removeCount);
+ }
+
+ @After
+ public void tearDown() {
+ provider.deactivate();
+ provider.coreService = null;
+ provider.providerRegistry = null;
+ }
+
+ private class TestHostRegistry implements HostProviderRegistry {
+
+ @Override
+ public HostProviderService register(HostProvider provider) {
+ providerService = new TestHostProviderService(provider);
+ return providerService;
+ }
+
+ @Override
+ public void unregister(HostProvider provider) {
+ }
+
+ @Override
+ public Set<ProviderId> getProviders() {
+ return null;
+ }
+
+ }
+
+ private class TestHostProviderService
+ extends AbstractProviderService<HostProvider>
+ implements HostProviderService {
+
+ DeviceId added = null;
+ DeviceId moved = null;
+ DeviceId spine = null;
+ public int removeCount;
+
+ protected TestHostProviderService(HostProvider provider) {
+ super(provider);
+ }
+
+ @Override
+ public void hostDetected(HostId hostId, HostDescription hostDescription) {
+ DeviceId descr = hostDescription.location().deviceId();
+ if (added == null) {
+ added = descr;
+ } else if ((moved == null) && !descr.equals(added)) {
+ moved = descr;
+ } else {
+ spine = descr;
+ }
+ }
+
+ @Override
+ public void hostVanished(HostId hostId) {
+ removeCount++;
+ }
+
+ }
+
+ private class OvsdbControllerTest implements OvsdbController {
+ private OvsdbEventListener ovsdbEventListener = null;
+
+ @Override
+ public void addNodeListener(OvsdbNodeListener listener) {
+
+ }
+
+ @Override
+ public void removeNodeListener(OvsdbNodeListener listener) {
+
+ }
+
+ @Override
+ public void addOvsdbEventListener(OvsdbEventListener listener) {
+ ovsdbEventListener = listener;
+
+ }
+
+ @Override
+ public void removeOvsdbEventListener(OvsdbEventListener listener) {
+ ovsdbEventListener = null;
+
+ }
+
+ @Override
+ public List<OvsdbNodeId> getNodeIds() {
+ return null;
+ }
+
+ @Override
+ public OvsdbClientService getOvsdbClient(OvsdbNodeId nodeId) {
+ return null;
+ }
+ }
+}