XMPP as SBI support: implementation of core XMPP and Xmpp Device Provider

State machine handled by XmppSession interface, most of tests implemented

XmppDeviceFactory re-designed, tests updated

pom and BUCK files updated

Change-Id: I4c6955e091169c945415084cbb000c61b474c0fc
diff --git a/providers/pom.xml b/providers/pom.xml
index bed5a3d..79cdcb1 100644
--- a/providers/pom.xml
+++ b/providers/pom.xml
@@ -54,6 +54,7 @@
         -->
         <module>tl1</module>
         <module>general</module>
+        <module>xmpp</module>
     </modules>
 
     <dependencies>
diff --git a/providers/xmpp/device/BUCK b/providers/xmpp/device/BUCK
new file mode 100644
index 0000000..265d928
--- /dev/null
+++ b/providers/xmpp/device/BUCK
@@ -0,0 +1,26 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//lib:tinder-xmpp',
+    '//protocols/xmpp/core/api:onos-protocols-xmpp-core-api',
+    '//protocols/xmpp/core/ctl:onos-protocols-xmpp-core-ctl',
+]
+
+TEST_DEPS = [
+    '//lib:TEST_ADAPTERS',
+]
+
+osgi_jar_with_tests (
+    deps = COMPILE_DEPS,
+    test_deps = TEST_DEPS,
+)
+
+onos_app (
+    app_name = 'org.onosproject.xmpp.device',
+    title = 'XMPP Device Provider',
+    category = 'Provider',
+    url = 'https://wiki.onosproject.org/display/ONOS/XMPP+as+SBI',
+    description = 'XMPP protocol southbound provider.',
+    required_apps = [
+        'org.onosproject.protocols.xmpp',
+    ]
+)
\ No newline at end of file
diff --git a/providers/xmpp/device/pom.xml b/providers/xmpp/device/pom.xml
new file mode 100644
index 0000000..ec84565
--- /dev/null
+++ b/providers/xmpp/device/pom.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.onosproject</groupId>
+        <artifactId>onos-xmpp-providers</artifactId>
+        <version>1.13.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>onos-xmpp-provider-device</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>ONOS XMPP protocol Device provider</description>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-api</artifactId>
+            <classifier>tests</classifier>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.igniterealtime</groupId>
+            <artifactId>tinder</artifactId>
+            <version>1.3.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-xmpp-core-api</artifactId>
+            <version>1.13.0-SNAPSHOT</version>
+        </dependency>
+    </dependencies>
+</project>
\ No newline at end of file
diff --git a/providers/xmpp/device/src/main/java/org/onosproject/provider/xmpp/device/impl/XmppDeviceProvider.java b/providers/xmpp/device/src/main/java/org/onosproject/provider/xmpp/device/impl/XmppDeviceProvider.java
new file mode 100644
index 0000000..069fc05
--- /dev/null
+++ b/providers/xmpp/device/src/main/java/org/onosproject/provider/xmpp/device/impl/XmppDeviceProvider.java
@@ -0,0 +1,185 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.xmpp.device.impl;
+
+import com.google.common.base.Preconditions;
+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.ChassisId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.MastershipRole;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.device.DefaultDeviceDescription;
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.DeviceProvider;
+import org.onosproject.net.device.DeviceProviderRegistry;
+import org.onosproject.net.device.DeviceProviderService;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.provider.AbstractProvider;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.xmpp.core.XmppController;
+import org.onosproject.xmpp.core.XmppDeviceId;
+import org.onosproject.xmpp.core.XmppDeviceListener;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.xmpp.packet.JID;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Provider which will try to fetch the details of XMPP devices from the core and run a capability discovery on each of
+ * the device.
+ */
+@Component(immediate = true)
+public class XmppDeviceProvider extends AbstractProvider implements DeviceProvider {
+
+    private final Logger logger = getLogger(getClass());
+
+    private static final String PROVIDER = "org.onosproject.provider.xmpp.device";
+    private static final String APP_NAME = "org.onosproject.xmpp";
+    private static final String XMPP = "xmpp";
+    private static final String ADDRESS = "address";
+
+    private static final String HARDWARE_VERSION = "XMPP Device";
+    private static final String SOFTWARE_VERSION = "1.0";
+    private static final String SERIAL_NUMBER = "unknown";
+    private static final String IS_NULL_MSG = "XMPP device info is null";
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceProviderRegistry providerRegistry;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected XmppController controller;
+
+    protected DeviceProviderService providerService;
+
+    protected ApplicationId appId;
+
+    private XmppDeviceListener deviceListener = new InternalXmppDeviceListener();
+
+    public XmppDeviceProvider() {
+        super(new ProviderId(XMPP, PROVIDER));
+    }
+
+    @Activate
+    public void activate(ComponentContext context) {
+        providerService = providerRegistry.register(this);
+        appId = coreService.registerApplication(APP_NAME);
+        controller.addXmppDeviceListener(deviceListener);
+        logger.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        controller.removeXmppDeviceListener(deviceListener);
+        providerRegistry.unregister(this);
+        providerService = null;
+    }
+
+    @Override
+    public void triggerProbe(DeviceId deviceId) {
+
+    }
+
+    @Override
+    public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
+
+    }
+
+    @Override
+    public boolean isReachable(DeviceId deviceId) {
+        String id = deviceId.uri().getSchemeSpecificPart();
+        JID jid = new JID(id);
+        XmppDeviceId xmppDeviceId = new XmppDeviceId(jid);
+        return controller.getDevice(xmppDeviceId) != null;
+    }
+
+    @Override
+    public void changePortState(DeviceId deviceId, PortNumber portNumber, boolean enable) {
+
+    }
+
+    private void connectDevice(XmppDeviceId xmppDeviceId) {
+        DeviceId deviceId = DeviceId.deviceId(xmppDeviceId.id());
+        String ipAddress = controller.getDevice(xmppDeviceId).getIpAddress().getAddress().getHostAddress();
+        // Assumption: manufacturer is uniquely identified by domain part of JID
+        String manufacturer = xmppDeviceId.getJid().getDomain();
+
+        ChassisId cid = new ChassisId();
+
+        SparseAnnotations annotations = DefaultAnnotations.builder()
+                .set(AnnotationKeys.PROTOCOL, XMPP.toUpperCase())
+                .set("IpAddress", ipAddress)
+                .build();
+        DeviceDescription deviceDescription = new DefaultDeviceDescription(
+                deviceId.uri(),
+                Device.Type.OTHER,
+                manufacturer, HARDWARE_VERSION,
+                SOFTWARE_VERSION, SERIAL_NUMBER,
+                cid, true,
+                annotations);
+
+        if (deviceService.getDevice(deviceId) == null) {
+            providerService.deviceConnected(deviceId, deviceDescription);
+        }
+    }
+
+    private void disconnectDevice(XmppDeviceId xmppDeviceId) {
+        Preconditions.checkNotNull(xmppDeviceId, IS_NULL_MSG);
+
+        DeviceId deviceId = DeviceId.deviceId(xmppDeviceId.id());
+        if (deviceService.getDevice(deviceId) != null) {
+            providerService.deviceDisconnected(deviceId);
+            logger.info("XMPP device {} removed from XMPP controller", deviceId);
+        } else {
+            logger.warn("XMPP device {} does not exist in the store, " +
+                    "or it may already have been removed", deviceId);
+        }
+    }
+
+    private class InternalXmppDeviceListener implements XmppDeviceListener {
+
+        @Override
+        public void deviceConnected(XmppDeviceId deviceId) {
+            logger.info("NOTIFICATION: device {} connected", deviceId);
+            connectDevice(deviceId);
+        }
+
+        @Override
+        public void deviceDisconnected(XmppDeviceId deviceId) {
+            logger.info("NOTIFICATION: device {} disconnected", deviceId);
+            disconnectDevice(deviceId);
+        }
+    }
+
+
+}
diff --git a/providers/xmpp/device/src/main/java/org/onosproject/provider/xmpp/device/impl/package-info.java b/providers/xmpp/device/src/main/java/org/onosproject/provider/xmpp/device/impl/package-info.java
new file mode 100644
index 0000000..d9908ce
--- /dev/null
+++ b/providers/xmpp/device/src/main/java/org/onosproject/provider/xmpp/device/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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 XMPP as a means of device discovery.
+ */
+package org.onosproject.provider.xmpp.device.impl;
\ No newline at end of file
diff --git a/providers/xmpp/device/src/test/java/org/onosproject/provider/xmpp/device/impl/XmppDeviceProviderTest.java b/providers/xmpp/device/src/test/java/org/onosproject/provider/xmpp/device/impl/XmppDeviceProviderTest.java
new file mode 100644
index 0000000..f5ca6b6
--- /dev/null
+++ b/providers/xmpp/device/src/test/java/org/onosproject/provider/xmpp/device/impl/XmppDeviceProviderTest.java
@@ -0,0 +1,317 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.xmpp.device.impl;
+
+import org.dom4j.Document;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.net.DefaultDevice;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceProvider;
+import org.onosproject.net.device.DeviceProviderRegistry;
+import org.onosproject.net.device.DeviceProviderRegistryAdapter;
+import org.onosproject.net.device.DeviceProviderService;
+import org.onosproject.net.device.DeviceProviderServiceAdapter;
+import org.onosproject.net.device.DeviceServiceAdapter;
+import org.onosproject.net.device.DeviceStoreAdapter;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.xmpp.core.XmppController;
+import org.onosproject.xmpp.core.XmppDevice;
+import org.onosproject.xmpp.core.XmppDeviceId;
+import org.onosproject.xmpp.core.XmppDeviceListener;
+import org.onosproject.xmpp.core.XmppIqListener;
+import org.onosproject.xmpp.core.XmppMessageListener;
+import org.onosproject.xmpp.core.XmppPresenceListener;
+import org.onosproject.xmpp.core.XmppSession;
+import org.xmpp.packet.JID;
+import org.xmpp.packet.Packet;
+import org.xmpp.packet.PacketError;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
+
+/**
+ * Testing class for XmppDeviceProvider.
+ */
+public class XmppDeviceProviderTest {
+
+
+    private final XmppDeviceProvider provider = new XmppDeviceProvider();
+    XmppControllerAdapter xmppController = new XmppControllerAdapter();
+
+    //Provider Mock
+    private final DeviceProviderRegistry deviceRegistry = new MockDeviceProviderRegistry();
+    private final DeviceProviderService providerService = new MockDeviceProviderService();
+    private final DeviceServiceAdapter deviceService = new DeviceServiceAdapter();
+    private final MockDeviceStore deviceStore = new MockDeviceStore();
+
+    //Provider related classes
+    private CoreService coreService;
+    private final ApplicationId appId =
+            new DefaultApplicationId(100, APP_NAME);
+    private static final String APP_NAME = "org.onosproject.xmpp";
+
+    private final HashMap<DeviceId, Device> devices = new HashMap<>();
+
+    private final String agentOneId = "agent1@test.org";
+    private final XmppDeviceId agentOneXmppId = new XmppDeviceId(new JID(agentOneId));
+
+    @Before
+    public void setUp() throws IOException {
+        coreService = createMock(CoreService.class);
+        expect(coreService.registerApplication(APP_NAME))
+                .andReturn(appId).anyTimes();
+        replay(coreService);
+        provider.coreService = coreService;
+        provider.providerRegistry = deviceRegistry;
+        provider.deviceService = deviceService;
+        provider.providerService = providerService;
+        provider.controller = xmppController;
+        provider.activate(null);
+        devices.clear();
+    }
+
+    @Test
+    public void activate() throws Exception {
+        assertTrue("Provider should be registered", deviceRegistry.getProviders().contains(provider.id()));
+        assertEquals("Incorrect device service", deviceService, provider.deviceService);
+        assertEquals("Incorrect provider service", providerService, provider.providerService);
+        assertEquals("Incorrent application id", appId, provider.appId);
+        assertNotNull("XMPP device listener should be added", xmppController.listener);
+    }
+
+    @Test
+    public void deactivate() throws Exception {
+        provider.deactivate();
+        assertNull("Device listener should be removed", xmppController.listener);
+        assertFalse("Provider should not be registered", deviceRegistry.getProviders().contains(provider));
+        assertNull("Provider service should be null", provider.providerService);
+    }
+
+    @Test
+    public void testDeviceAdded() {
+        xmppController.listener.deviceConnected(agentOneXmppId);
+        assertEquals("XMPP device added", 1, devices.size());
+    }
+
+    @Test
+    public void testDeviceRemoved() {
+        xmppController.listener.deviceDisconnected(agentOneXmppId);
+        assertEquals("XMPP device removed", 0, devices.size());
+    }
+
+    @Test
+    public void testIsReachable() {
+        assertTrue(provider.isReachable(DeviceId.deviceId("reachable@xmpp.org")));
+        assertFalse(provider.isReachable(DeviceId.deviceId("non-reachable@xmpp.org")));
+    }
+
+
+    private class MockDeviceProviderRegistry extends DeviceProviderRegistryAdapter {
+
+        final Set<ProviderId> providers = new HashSet<>();
+
+        @Override
+        public DeviceProviderService register(DeviceProvider provider) {
+            providers.add(provider.id());
+            return providerService;
+        }
+
+        @Override
+        public void unregister(DeviceProvider provider) {
+            providers.remove(provider.id());
+        }
+
+        @Override
+        public Set<ProviderId> getProviders() {
+            return providers;
+        }
+
+    }
+
+    private class MockDeviceProviderService extends DeviceProviderServiceAdapter {
+
+        @Override
+        public void deviceConnected(DeviceId deviceId, DeviceDescription desc) {
+            assertNotNull("DeviceId should be not null", deviceId);
+            assertNotNull("DeviceDescription should be not null", desc);
+            deviceStore.createOrUpdateDevice(ProviderId.NONE, deviceId, desc);
+        }
+
+
+        @Override
+        public void deviceDisconnected(DeviceId deviceId) {
+            deviceStore.removeDevice(deviceId);
+        }
+
+    }
+
+    private class MockDeviceStore extends DeviceStoreAdapter {
+
+        @Override
+        public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
+                                                DeviceDescription desc) {
+
+            devices.put(deviceId, new DefaultDevice(providerId, deviceId, desc.type(),
+                                                    desc.manufacturer(), desc.hwVersion(),
+                                                    desc.swVersion(), desc.serialNumber(),
+                                                    desc.chassisId(), desc.annotations()));
+            return null;
+        }
+
+        @Override
+        public DeviceEvent removeDevice(DeviceId deviceId) {
+            devices.remove(deviceId);
+            return null;
+        }
+
+        @Override
+        public Device getDevice(DeviceId deviceId) {
+            return devices.get(deviceId);
+        }
+
+        @Override
+        public int getDeviceCount() {
+            return devices.size();
+        }
+
+    }
+
+    private class XmppControllerAdapter implements XmppController {
+
+        XmppDeviceListener listener = null;
+        Map<XmppDeviceId, XmppDevice> xmppDevices = new HashMap();
+
+        XmppControllerAdapter() {
+            XmppDeviceAdapter reachable = new XmppDeviceAdapter("reachable@xmpp.org", "127.0.0.1", 54333);
+            xmppDevices.put(reachable.xmppDeviceId, reachable);
+            XmppDeviceAdapter testDevice = new XmppDeviceAdapter(agentOneId, "127.0.0.1", 54334);
+            xmppDevices.put(testDevice.xmppDeviceId, testDevice);
+        }
+
+        @java.lang.Override
+        public XmppDevice getDevice(XmppDeviceId xmppDeviceId) {
+            return xmppDevices.get(xmppDeviceId);
+        }
+
+        @java.lang.Override
+        public void addXmppDeviceListener(XmppDeviceListener deviceListener) {
+            this.listener = deviceListener;
+        }
+
+        @java.lang.Override
+        public void removeXmppDeviceListener(XmppDeviceListener deviceListener) {
+            this.listener = null;
+        }
+
+        @java.lang.Override
+        public void addXmppIqListener(XmppIqListener iqListener) {
+
+        }
+
+        @java.lang.Override
+        public void removeXmppIqListener(XmppIqListener iqListener) {
+
+        }
+
+        @java.lang.Override
+        public void addXmppMessageListener(XmppMessageListener messageListener) {
+
+        }
+
+        @java.lang.Override
+        public void removeXmppMessageListener(XmppMessageListener messageListener) {
+
+        }
+
+        @java.lang.Override
+        public void addXmppPresenceListener(XmppPresenceListener presenceListener) {
+
+        }
+
+        @java.lang.Override
+        public void removeXmppPresenceListener(XmppPresenceListener presenceListener) {
+
+        }
+    }
+
+    private class XmppDeviceAdapter implements XmppDevice {
+
+        InetSocketAddress testAddress;
+        XmppDeviceId xmppDeviceId;
+
+        public XmppDeviceAdapter(String jid, String address, int port) {
+            testAddress = new InetSocketAddress(address, port);
+            this.xmppDeviceId = new XmppDeviceId(new JID(jid));
+        }
+
+        @Override
+        public XmppSession getSession() {
+            return null;
+        }
+
+        @Override
+        public InetSocketAddress getIpAddress() {
+            return testAddress;
+        }
+
+        @Override
+        public void registerConnectedDevice() {
+
+        }
+
+        @Override
+        public void disconnectDevice() {
+
+        }
+
+        @Override
+        public void sendPacket(Packet packet) {
+
+        }
+
+        @Override
+        public void writeRawXml(Document document) {
+
+        }
+
+        @Override
+        public void handlePacket(Packet packet) {
+
+        }
+
+        @Override
+        public void sendError(PacketError packetError) {
+
+        }
+
+    }
+
+}
diff --git a/providers/xmpp/pom.xml b/providers/xmpp/pom.xml
new file mode 100644
index 0000000..3ce40d1
--- /dev/null
+++ b/providers/xmpp/pom.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>onos-providers</artifactId>
+        <groupId>org.onosproject</groupId>
+        <version>1.13.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>onos-xmpp-providers</artifactId>
+
+    <modules>
+        <module>onos-xmpp-provider-device</module>
+    </modules>
+
+</project>
\ No newline at end of file