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
diff --git a/providers/host/src/main/java/org/onosproject/provider/host/impl/HostLocationProvider.java b/providers/host/src/main/java/org/onosproject/provider/host/impl/HostLocationProvider.java
index 40e67b5..edc5ef5 100644
--- a/providers/host/src/main/java/org/onosproject/provider/host/impl/HostLocationProvider.java
+++ b/providers/host/src/main/java/org/onosproject/provider/host/impl/HostLocationProvider.java
@@ -54,6 +54,14 @@
import org.onosproject.cfg.ComponentConfigService;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
+import org.onosproject.net.config.NetworkConfigRegistry;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.net.config.basics.BasicHostConfig;
+import org.onosproject.net.config.basics.HostLearningConfig;
+import org.onosproject.net.config.basics.SubjectFactories;
import org.onosproject.net.intf.InterfaceService;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.Device;
@@ -130,10 +138,14 @@
protected ComponentConfigService cfgService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected NetworkConfigRegistry registry;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected InterfaceService interfaceService;
private final InternalHostProvider processor = new InternalHostProvider();
private final DeviceListener deviceListener = new InternalDeviceListener();
+ private final InternalConfigListener cfgListener = new InternalConfigListener();
private ApplicationId appId;
@@ -173,6 +185,19 @@
private ExecutorService probeEventHandler;
private ExecutorService packetHandler;
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected NetworkConfigService netcfgService;
+
+ private ConfigFactory<ConnectPoint, HostLearningConfig> hostLearningConfig =
+ new ConfigFactory<ConnectPoint, HostLearningConfig>(
+ SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY,
+ HostLearningConfig.class, "hostLearning") {
+ @Override
+ public HostLearningConfig createConfig() {
+ return new HostLearningConfig();
+ }
+ };
+
/**
* Creates an OpenFlow host provider.
*/
@@ -193,8 +218,9 @@
providerService = providerRegistry.register(this);
packetService.addProcessor(processor, PacketProcessor.advisor(1));
deviceService.addListener(deviceListener);
-
+ registry.registerConfigFactory(hostLearningConfig);
modified(context);
+ netcfgService.addListener(cfgListener);
log.info("Started with Application ID {}", appId.id());
}
@@ -212,6 +238,8 @@
probeEventHandler.shutdown();
packetHandler.shutdown();
providerService = null;
+ registry.unregisterConfigFactory(hostLearningConfig);
+ netcfgService.removeListener(cfgListener);
log.info("Stopped");
}
@@ -517,6 +545,13 @@
return;
}
+ HostLearningConfig cfg = netcfgService.getConfig(heardOn, HostLearningConfig.class);
+ // if learning is disabled bail out.
+ if ((cfg != null) && (!cfg.hostLearningEnabled())) {
+ log.debug("Learning disabled for {}, abort.", heardOn);
+ return;
+ }
+
// ARP: possible new hosts, update both location and IP
if (eth.getEtherType() == Ethernet.TYPE_ARP) {
ARP arp = (ARP) eth.getPayload();
@@ -775,4 +810,45 @@
);
}
+
+ private class InternalConfigListener implements NetworkConfigListener {
+
+ @Override
+ public void event(NetworkConfigEvent event) {
+ switch (event.type()) {
+ case CONFIG_ADDED:
+ case CONFIG_UPDATED:
+ log.debug("HostLearningConfig event of type {}", event.type());
+ // if learning enabled do nothing
+ HostLearningConfig learningConfig = (HostLearningConfig) event.config().get();
+ if (learningConfig.hostLearningEnabled()) {
+ return;
+ }
+
+ // if host learning is disable remove this location from existing, learnt hosts
+ ConnectPoint connectPoint = learningConfig.subject();
+ Set<Host> connectedHosts = hostService.getConnectedHosts(connectPoint);
+ for (Host host : connectedHosts) {
+ BasicHostConfig hostConfig = netcfgService.getConfig(host.id(), BasicHostConfig.class);
+
+ if ((hostConfig == null) || (!hostConfig.locations().contains(connectPoint))) {
+ // timestamp shoud not matter for comparing HostLocation and ConnectPoint
+ providerService.removeLocationFromHost(host.id(), new HostLocation(connectPoint, 1));
+ }
+ }
+ break;
+ case CONFIG_REMOVED:
+ default:
+ break;
+ }
+ }
+
+ @Override
+ public boolean isRelevant(NetworkConfigEvent event) {
+ if (!event.configClass().equals(HostLearningConfig.class)) {
+ return false;
+ }
+ return true;
+ }
+ }
}
diff --git a/providers/host/src/test/java/org/onosproject/provider/host/impl/HostLocationProviderTest.java b/providers/host/src/test/java/org/onosproject/provider/host/impl/HostLocationProviderTest.java
index 89a6ef8..9971921 100644
--- a/providers/host/src/test/java/org/onosproject/provider/host/impl/HostLocationProviderTest.java
+++ b/providers/host/src/test/java/org/onosproject/provider/host/impl/HostLocationProviderTest.java
@@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
import com.google.common.util.concurrent.MoreExecutors;
import org.junit.After;
import org.junit.Before;
@@ -52,6 +53,9 @@
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.net.config.Config;
+import org.onosproject.net.config.NetworkConfigRegistryAdapter;
+import org.onosproject.net.config.NetworkConfigServiceAdapter;
import org.onosproject.net.intf.Interface;
import org.onosproject.net.intf.InterfaceListener;
import org.onosproject.net.intf.InterfaceService;
@@ -209,6 +213,8 @@
private final TestHostService hostService = new TestHostService();
private final TestPacketService packetService = new TestPacketService();
private final TestInterfaceService interfaceService = new TestInterfaceService();
+ private final TestNetworkConfigRegistryAdapter registryAdapter = new TestNetworkConfigRegistryAdapter();
+ private final TestNetworkConfigService netcfgService = new TestNetworkConfigService();
private PacketProcessor testProcessor;
private CoreService coreService;
@@ -233,7 +239,8 @@
provider.deviceService = deviceService;
provider.hostService = hostService;
provider.interfaceService = interfaceService;
-
+ provider.registry = registryAdapter;
+ provider.netcfgService = netcfgService;
provider.activate(CTX_FOR_NO_REMOVE);
provider.deviceEventHandler = MoreExecutors.newDirectExecutorService();
@@ -1244,4 +1251,20 @@
return false;
}
}
+
+ private class TestNetworkConfigRegistryAdapter extends NetworkConfigRegistryAdapter {
+ private Set<Config> configs = Sets.newHashSet();
+
+ @Override
+ public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
+ Config c = configs.stream()
+ .filter(config -> subject.equals(config.subject()))
+ .filter(config -> configClass.equals(config.getClass()))
+ .findFirst().orElse(null);
+ return (C) c;
+ }
+ }
+
+ private class TestNetworkConfigService extends NetworkConfigServiceAdapter {
+ }
}