Disable host learning feature.

This patch gives us the ability to disable
host learning in certain ports via the ports
configuration.

We have the following cases :
        - Discover a host with no learning configuration, this
          results to learned host (missing config assumes learning is on)
        - Discover a host with learning configuration set to false,
          do not learn that host.
        - Update the learning configuration of a CP to false. Then,
          fetch all host at that location, if these hosts are not
          configured statically at this location, remove them

Change-Id: I2a05ce5f9a890bb4049fd7856f95d78f467e3330
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/HostLearningConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/HostLearningConfig.java
new file mode 100644
index 0000000..7a4a5a4
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/HostLearningConfig.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2016-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.net.config.basics;
+
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.config.Config;
+
+/**
+ * Configuration for enabling/disabling host learning.
+ */
+public class HostLearningConfig extends Config<ConnectPoint> {
+    private static final String ENABLED = "enabled";
+
+    @Override
+    public boolean isValid() {
+        return isBoolean(ENABLED, FieldPresence.MANDATORY);
+    }
+
+    public Boolean hostLearningEnabled() {
+        return get(ENABLED, true);
+    }
+
+    public void setEnabled(String boolString) {
+        object.put(ENABLED, boolString);
+    }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/config/basics/HostLearningConfigTest.java b/core/api/src/test/java/org/onosproject/net/config/basics/HostLearningConfigTest.java
new file mode 100644
index 0000000..21467c5
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/config/basics/HostLearningConfigTest.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2016-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.net.config.basics;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.Beta;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.config.Config;
+import org.onosproject.net.config.ConfigApplyDelegate;
+import org.onosproject.net.config.InvalidFieldException;
+
+import java.io.InputStream;
+import java.net.URI;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests for class {@link HostLearningConfig}.
+ */
+@Beta
+public class HostLearningConfigTest {
+    private ConnectPoint cp;
+    private HostLearningConfig config;
+    private HostLearningConfig invalidConfig;
+
+    /**
+     * Initialize test related variables.
+     *
+     * @throws Exception
+     */
+    @Before
+    public void setUp() throws Exception {
+        InputStream jsonStream = HostLearningConfigTest.class
+                .getResourceAsStream("/host-learning-config.json");
+        InputStream invalidJsonStream = HostLearningConfigTest.class
+                .getResourceAsStream("/host-learning-config-invalid.json");
+
+        cp = new ConnectPoint(DeviceId.deviceId(new URI("of:0000000000000202")), PortNumber.portNumber((long) 5));
+        ConnectPoint subject = cp;
+        String key = CoreService.CORE_APP_NAME;
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode jsonNode = mapper.readTree(jsonStream);
+        JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream);
+        ConfigApplyDelegate delegate = new MockDelegate();
+
+        config = new HostLearningConfig();
+        config.init(subject, key, jsonNode, mapper, delegate);
+        invalidConfig = new HostLearningConfig();
+        invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate);
+    }
+
+    /**
+     * Tests config validity.
+     *
+     */
+    @Test(expected = InvalidFieldException.class)
+    public void isValid() {
+        assertTrue(config.isValid());
+        assertFalse(invalidConfig.isValid());
+    }
+
+    /**
+     * Tests enabled setter.
+     *
+     */
+    @Test
+    public void testEnableLearning() {
+        config.setEnabled("false");
+        assertTrue(!config.hostLearningEnabled());
+        config.setEnabled("true");
+        assertTrue(config.hostLearningEnabled());
+    }
+
+    private class MockDelegate implements ConfigApplyDelegate {
+        @Override
+        public void onApply(Config config) {
+        }
+    }
+}
diff --git a/core/api/src/test/resources/host-learning-config-invalid.json b/core/api/src/test/resources/host-learning-config-invalid.json
new file mode 100644
index 0000000..414b7c2
--- /dev/null
+++ b/core/api/src/test/resources/host-learning-config-invalid.json
@@ -0,0 +1,3 @@
+{
+  "not-enabled" : false
+}
\ No newline at end of file
diff --git a/core/api/src/test/resources/host-learning-config.json b/core/api/src/test/resources/host-learning-config.json
new file mode 100644
index 0000000..4ff6266
--- /dev/null
+++ b/core/api/src/test/resources/host-learning-config.json
@@ -0,0 +1,3 @@
+{
+  "enabled" : true
+}
\ No newline at end of file