CORD-339 Network config host provider

* Implement new application org.onosproject.netcfghost
* Implement BasicHostConfig to include IP and location information
    - Update network-cfg.json to add host config example
    - Add network-cfg-2x2-leaf-spine.json for 2x2 leaf-spine network in SegmentRouting
* Update Segment Rounting
    - Punt ARP packets
      (which is done by HostLocationProvider previously)
    - Check existing hosts when device connected or configured

Change-Id: I03986ddc8203d740b5bf26903e3dbf866d4d4600
diff --git a/providers/netcfghost/pom.xml b/providers/netcfghost/pom.xml
new file mode 100644
index 0000000..0784d90
--- /dev/null
+++ b/providers/netcfghost/pom.xml
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<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.4.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>onos-netcfg-host-provider</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>
+        Host provider that uses network config service to discover hosts.
+    </description>
+    <url>http://onosproject.org</url>
+
+    <properties>
+        <onos.version>1.4.0-SNAPSHOT</onos.version>
+        <onos.app.name>org.onosproject.netcfghostprovider</onos.app.name>
+        <onos.app.origin>ON.Lab</onos.app.origin>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-api</artifactId>
+            <version>${onos.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onlab-osgi</artifactId>
+            <version>${onos.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.11</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-api</artifactId>
+            <version>${onos.version}</version>
+            <scope>test</scope>
+            <classifier>tests</classifier>
+        </dependency>
+
+        <dependency>
+            <groupId>org.easymock</groupId>
+            <artifactId>easymock</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+</project>
diff --git a/providers/netcfghost/src/main/java/org/onosproject/provider/netcfghost/NetworkConfigHostProvider.java b/providers/netcfghost/src/main/java/org/onosproject/provider/netcfghost/NetworkConfigHostProvider.java
new file mode 100644
index 0000000..767cfb7
--- /dev/null
+++ b/providers/netcfghost/src/main/java/org/onosproject/provider/netcfghost/NetworkConfigHostProvider.java
@@ -0,0 +1,196 @@
+/*
+ * 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.onosproject.provider.netcfghost;
+
+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.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.Host;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
+import org.onosproject.net.config.NetworkConfigRegistry;
+import org.onosproject.net.config.basics.BasicHostConfig;
+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.provider.AbstractProvider;
+import org.onosproject.net.provider.ProviderId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import java.util.Set;
+
+/**
+ * Host provider that uses network config service to discover hosts.
+ */
+@Component(immediate = true)
+public class NetworkConfigHostProvider extends AbstractProvider implements HostProvider {
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected HostProviderRegistry providerRegistry;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetworkConfigRegistry networkConfigRegistry;
+
+    private static final String APP_NAME = "org.onosproject.provider.netcfghost";
+    private ApplicationId appId;
+    protected HostProviderService providerService;
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final InternalNetworkConfigListener networkConfigListener =
+            new InternalNetworkConfigListener();
+
+    /**
+     * Creates an network config host location provider.
+     */
+    public NetworkConfigHostProvider() {
+        super(new ProviderId("host", APP_NAME));
+    }
+
+    @Activate
+    protected void activate() {
+        appId = coreService.registerApplication(APP_NAME);
+        providerService = providerRegistry.register(this);
+        networkConfigRegistry.addListener(networkConfigListener);
+        readInitialConfig();
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        networkConfigRegistry.removeListener(networkConfigListener);
+        providerRegistry.unregister(this);
+        providerService = null;
+        log.info("Stopped");
+    }
+
+    @Override
+    public void triggerProbe(Host host) {
+        /*
+         * Note: In CORD deployment, we assume that all hosts are configured.
+         * Therefore no probe is required.
+         */
+    }
+
+    /**
+     * Adds host information.
+     * IP information will be appended if host exists.
+     *
+     * @param mac MAC address of the host
+     * @param vlan VLAN ID of the host
+     * @param hloc Location of the host
+     * @param ips Set of IP addresses of the host
+     */
+    protected void addHost(MacAddress mac, VlanId vlan, HostLocation hloc, Set<IpAddress> ips) {
+        HostId hid = HostId.hostId(mac, vlan);
+        HostDescription desc = new DefaultHostDescription(mac, vlan, hloc, ips);
+        providerService.hostDetected(hid, desc, false);
+    }
+
+    /**
+     * Updates host information.
+     * IP information will be replaced if host exists.
+     *
+     * @param mac MAC address of the host
+     * @param vlan VLAN ID of the host
+     * @param hloc Location of the host
+     * @param ips Set of IP addresses of the host
+     */
+    protected void updateHost(MacAddress mac, VlanId vlan, HostLocation hloc, Set<IpAddress> ips) {
+        HostId hid = HostId.hostId(mac, vlan);
+        HostDescription desc = new DefaultHostDescription(mac, vlan, hloc, ips);
+        providerService.hostDetected(hid, desc, true);
+    }
+
+    /**
+     * Removes host information.
+     *
+     * @param mac MAC address of the host
+     * @param vlan VLAN ID of the host
+     */
+    protected void removeHost(MacAddress mac, VlanId vlan) {
+        HostId hid = HostId.hostId(mac, vlan);
+        providerService.hostVanished(hid);
+    }
+
+    private void readInitialConfig() {
+        networkConfigRegistry.getSubjects(HostId.class).forEach(hostId -> {
+            MacAddress mac = hostId.mac();
+            VlanId vlan = hostId.vlanId();
+            BasicHostConfig hostConfig =
+                    networkConfigRegistry.getConfig(hostId, BasicHostConfig.class);
+            Set<IpAddress> ipAddresses = hostConfig.ipAddresses();
+            ConnectPoint location = hostConfig.location();
+            HostLocation hloc = new HostLocation(location, System.currentTimeMillis());
+            addHost(mac, vlan, hloc, ipAddresses);
+        });
+    }
+
+    private class InternalNetworkConfigListener implements NetworkConfigListener {
+        @Override
+        public void event(NetworkConfigEvent event) {
+            // Do not process non-host, register and unregister events
+            if (!event.configClass().equals(BasicHostConfig.class) ||
+                    event.type() == NetworkConfigEvent.Type.CONFIG_REGISTERED ||
+                    event.type() == NetworkConfigEvent.Type.CONFIG_UNREGISTERED) {
+                return;
+            }
+
+            HostId hostId = (HostId) event.subject();
+            MacAddress mac = hostId.mac();
+            VlanId vlan = hostId.vlanId();
+            BasicHostConfig hostConfig =
+                    networkConfigRegistry.getConfig(hostId, BasicHostConfig.class);
+            Set<IpAddress> ipAddresses = null;
+            HostLocation hloc = null;
+
+            // Note: There will be no config presented in the CONFIG_REMOVE case
+            if (hostConfig != null) {
+                ipAddresses = hostConfig.ipAddresses();
+                ConnectPoint location = hostConfig.location();
+                hloc = new HostLocation(location, System.currentTimeMillis());
+            }
+
+            switch (event.type()) {
+                case CONFIG_ADDED:
+                    addHost(mac, vlan, hloc, ipAddresses);
+                    break;
+                case CONFIG_UPDATED:
+                    updateHost(mac, vlan, hloc, ipAddresses);
+                    break;
+                case CONFIG_REMOVED:
+                    removeHost(mac, vlan);
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+}
diff --git a/providers/netcfghost/src/main/java/org/onosproject/provider/netcfghost/package-info.java b/providers/netcfghost/src/main/java/org/onosproject/provider/netcfghost/package-info.java
new file mode 100644
index 0000000..a56b9a8
--- /dev/null
+++ b/providers/netcfghost/src/main/java/org/onosproject/provider/netcfghost/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Host provider that uses network config service to discover hosts.
+ */
+package org.onosproject.provider.netcfghost;
diff --git a/providers/netcfghost/src/test/java/org/onosproject/provider/netcfghost/NetworkConfigHostProviderTest.java b/providers/netcfghost/src/test/java/org/onosproject/provider/netcfghost/NetworkConfigHostProviderTest.java
new file mode 100644
index 0000000..a4f057c
--- /dev/null
+++ b/providers/netcfghost/src/test/java/org/onosproject/provider/netcfghost/NetworkConfigHostProviderTest.java
@@ -0,0 +1,132 @@
+/*
+ * 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.onosproject.provider.netcfghost;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.host.DefaultHostDescription;
+import org.onosproject.net.host.HostDescription;
+import org.onosproject.net.host.HostProvider;
+import org.onosproject.net.host.HostProviderService;
+import org.onosproject.net.provider.AbstractProviderService;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Set of tests of the host location provider for CORD.
+ */
+public class NetworkConfigHostProviderTest {
+    private NetworkConfigHostProvider provider = new NetworkConfigHostProvider();
+    private MockHostProviderService providerService = new MockHostProviderService(provider);
+
+    private MacAddress mac = MacAddress.valueOf("c0:ff:ee:c0:ff:ee");
+    private VlanId vlan = VlanId.vlanId(VlanId.UNTAGGED);
+    private DeviceId deviceId = DeviceId.deviceId("of:0000000000000001");
+    private PortNumber port = PortNumber.portNumber(5);
+    private HostLocation hloc = new HostLocation(deviceId, port, 100);
+    private Set<IpAddress> ips = new HashSet<>();
+    private HostId hostId = HostId.hostId(mac, vlan);
+    private HostDescription hostDescription;
+
+    @Before
+    public void setUp() {
+        provider.providerService = providerService;
+
+        // Initialize test variables
+        ips.add(IpAddress.valueOf("10.0.0.1"));
+        ips.add(IpAddress.valueOf("192.168.0.1"));
+        hostDescription = new DefaultHostDescription(mac, vlan, hloc, ips);
+    }
+
+    @Test
+    public void testAddHost() throws Exception {
+        provider.addHost(mac, vlan, hloc, ips);
+        assertThat(providerService.hostId, is(hostId));
+        assertThat(providerService.hostDescription, is(hostDescription));
+        assertThat(providerService.event, is("hostDetected"));
+        providerService.clear();
+    }
+
+    @Test
+    public void testUpdateHost() throws Exception {
+        provider.updateHost(mac, vlan, hloc, ips);
+        assertThat(providerService.hostId, is(hostId));
+        assertThat(providerService.hostDescription, is(hostDescription));
+        assertThat(providerService.event, is("hostDetected"));
+        providerService.clear();
+    }
+
+    @Test
+    public void testRemoveHost() throws Exception {
+        provider.removeHost(mac, vlan);
+        assertThat(providerService.hostId, is(hostId));
+        assertNull(providerService.hostDescription);
+        assertThat(providerService.event, is("hostVanished"));
+        providerService.clear();
+    }
+
+    /**
+     * Mock HostProviderService.
+     */
+    private class MockHostProviderService
+            extends AbstractProviderService<HostProvider>
+            implements HostProviderService {
+        private HostId hostId = null;
+        private HostDescription hostDescription = null;
+        private String event = null;
+
+        public MockHostProviderService(HostProvider provider) {
+            super(provider);
+        }
+
+        @Override
+        public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) {
+            this.hostId = hostId;
+            this.hostDescription = hostDescription;
+            this.event = "hostDetected";
+        }
+
+        @Override
+        public void hostVanished(HostId hostId) {
+            this.hostId = hostId;
+            this.event = "hostVanished";
+        }
+
+        @Override
+        public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
+            // Note: This method is never used.
+        }
+
+        public void clear() {
+            this.hostId = null;
+            this.hostDescription = null;
+            this.event = null;
+        }
+    }
+}