Fixing null provider custom topo simulator
- properly deal with device mastership
- allow creation of multi-homed hosts
- made UI location parameters optional
- added a simulated fabric script
Change-Id: I8558cc06aa4c323fab898b02fba9659b202c5392
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullDevice.java b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullDevice.java
index b6c668f..4d081f8 100644
--- a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullDevice.java
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullDevice.java
@@ -17,7 +17,6 @@
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
-import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.config.NetworkConfigService;
@@ -31,34 +30,32 @@
*/
@Command(scope = "onos", name = "null-create-device",
description = "Adds a simulated device to the custom topology simulation")
-public class CreateNullDevice extends AbstractShellCommand {
- private static final String GEO = "geo";
- private static final String GRID = "grid";
+public class CreateNullDevice extends CreateNullEntity {
@Argument(index = 0, name = "type", description = "Device type, e.g. switch, roadm",
- required = true, multiValued = false)
+ required = true)
String type = null;
@Argument(index = 1, name = "name", description = "Device name",
- required = true, multiValued = false)
+ required = true)
String name = null;
@Argument(index = 2, name = "portCount", description = "Port count",
- required = true, multiValued = false)
+ required = true)
Integer portCount = null;
@Argument(index = 3, name = "latOrY",
description = "Geo latitude / Grid y-coord",
- required = true, multiValued = false)
+ required = false)
Double latOrY = null;
@Argument(index = 4, name = "longOrX",
description = "Geo longitude / Grid x-coord",
- required = true, multiValued = false)
+ required = false)
Double longOrX = null;
@Argument(index = 5, name = "locType", description = "Location type {geo|grid}",
- required = false, multiValued = false)
+ required = false)
String locType = GEO;
@Override
@@ -67,28 +64,15 @@
NetworkConfigService cfgService = get(NetworkConfigService.class);
TopologySimulator simulator = service.currentSimulator();
- if (!(simulator instanceof CustomTopologySimulator)) {
- error("Custom topology simulator is not active.");
- return;
- }
-
- if (!(GEO.equals(locType) || GRID.equals(locType))) {
- error("locType must be 'geo' or 'grid'.");
+ if (!validateSimulator(simulator) || !validateLocType(locType)) {
return;
}
CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
DeviceId deviceId = sim.nextDeviceId();
BasicDeviceConfig cfg = cfgService.addConfig(deviceId, BasicDeviceConfig.class);
- cfg.name(name)
- .locType(locType);
-
- if (GEO.equals(locType)) {
- cfg.latitude(latOrY).longitude(longOrX);
- } else {
- cfg.gridX(longOrX).gridY(latOrY);
- }
- cfg.apply();
+ cfg.name(name);
+ setUiCoordinates(cfg, locType, latOrY, longOrX);
sim.createDevice(deviceId, name, Device.Type.valueOf(type.toUpperCase()), portCount);
}
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullEntity.java b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullEntity.java
new file mode 100644
index 0000000..ed68dfc
--- /dev/null
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullEntity.java
@@ -0,0 +1,106 @@
+/*
+ * 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.nil.cli;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.primitives.Longs;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.config.basics.BasicElementConfig;
+import org.onosproject.net.edge.EdgePortService;
+import org.onosproject.net.host.HostService;
+import org.onosproject.provider.nil.CustomTopologySimulator;
+import org.onosproject.provider.nil.TopologySimulator;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Base command for adding simulated entities to the custom topology simulation.
+ */
+public abstract class CreateNullEntity extends AbstractShellCommand {
+ protected static final String GEO = "geo";
+ protected static final String GRID = "grid";
+
+ /**
+ * Validates that the simulator is custom.
+ *
+ * @param simulator topology simulator
+ * @return true if valid
+ */
+ protected boolean validateSimulator(TopologySimulator simulator) {
+ if (!(simulator instanceof CustomTopologySimulator)) {
+ error("Custom topology simulator is not active.");
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Validates that the location type is valid.
+ *
+ * @param locType location type
+ * @return true if valid
+ */
+ protected boolean validateLocType(String locType) {
+ if (!(GEO.equals(locType) || GRID.equals(locType))) {
+ error("locType must be 'geo' or 'grid'.");
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Sets the UI location coordinates appropriately.
+ *
+ * @param cfg element config
+ * @param locType location type
+ * @param latOrY latitude or Y grid
+ * @param longOrX longitude or X grid
+ */
+ protected void setUiCoordinates(BasicElementConfig cfg,
+ String locType, Double latOrY, Double longOrX) {
+ if (latOrY != null && longOrX != null) {
+ cfg.locType(locType);
+ if (GEO.equals(locType)) {
+ cfg.latitude(latOrY).longitude(longOrX);
+ } else {
+ cfg.gridX(longOrX).gridY(latOrY);
+ }
+ }
+ cfg.apply();
+ }
+
+ /**
+ * Finds an available connect point among edge ports of the specified device.
+ *
+ * @param deviceId device identifier
+ * @param otherPoint optional other point to be excluded from search
+ * @return connect point available for link or host attachment
+ */
+ protected ConnectPoint findAvailablePort(DeviceId deviceId, ConnectPoint otherPoint) {
+ EdgePortService eps = get(EdgePortService.class);
+ HostService hs = get(HostService.class);
+
+ List<ConnectPoint> points = ImmutableList
+ .sortedCopyOf((l, r) -> Longs.compare(l.port().toLong(), r.port().toLong()),
+ eps.getEdgePoints(deviceId));
+ return points.stream()
+ .filter(p -> !Objects.equals(p, otherPoint) && hs.getConnectedHosts(p).isEmpty())
+ .findFirst().orElse(null);
+ }
+}
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullHost.java b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullHost.java
index 5197e79..e1eb5dd 100644
--- a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullHost.java
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullHost.java
@@ -16,54 +16,51 @@
package org.onosproject.provider.nil.cli;
+import com.google.common.collect.ImmutableSet;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onlab.packet.IpAddress;
-import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.HostId;
import org.onosproject.net.HostLocation;
import org.onosproject.net.config.NetworkConfigService;
import org.onosproject.net.config.basics.BasicHostConfig;
-import org.onosproject.net.edge.EdgePortService;
-import org.onosproject.net.host.HostService;
import org.onosproject.provider.nil.CustomTopologySimulator;
import org.onosproject.provider.nil.NullProviders;
import org.onosproject.provider.nil.TopologySimulator;
-import java.util.HashSet;
-import java.util.Iterator;
+import java.util.Set;
+
+import static java.util.Objects.requireNonNull;
/**
* Adds a simulated end-station host to the custom topology simulation.
*/
@Command(scope = "onos", name = "null-create-host",
description = "Adds a simulated end-station host to the custom topology simulation")
-public class CreateNullHost extends AbstractShellCommand {
- private static final String GEO = "geo";
- private static final String GRID = "grid";
+public class CreateNullHost extends CreateNullEntity {
- @Argument(index = 0, name = "deviceName", description = "Name of device where host is attached",
- required = true, multiValued = false)
- String deviceName = null;
+ @Argument(index = 0, name = "deviceNames", description = "Name of device where host is attached; comma-separated",
+ required = true)
+ String deviceNames = null;
- @Argument(index = 1, name = "hostIp", description = "Host IP address",
- required = true, multiValued = false)
- String hostIp = null;
+ @Argument(index = 1, name = "hostIps", description = "Host IP addresses; comma-separated",
+ required = true)
+ String hostIps = null;
@Argument(index = 2, name = "latOrY",
description = "Geo latitude / Grid y-coord",
- required = true, multiValued = false)
+ required = false)
Double latOrY = null;
@Argument(index = 3, name = "longOrX",
description = "Geo longitude / Grid x-coord",
- required = true, multiValued = false)
+ required = false)
Double longOrX = null;
@Argument(index = 4, name = "locType", description = "Location type {geo|grid}",
- required = false, multiValued = false)
+ required = false)
String locType = GEO;
@Override
@@ -72,48 +69,43 @@
NetworkConfigService cfgService = get(NetworkConfigService.class);
TopologySimulator simulator = service.currentSimulator();
- if (!(simulator instanceof CustomTopologySimulator)) {
- error("Custom topology simulator is not active.");
- return;
- }
-
- if (!(GEO.equals(locType) || GRID.equals(locType))) {
- error("locType must be 'geo' or 'grid'.");
+ if (!validateSimulator(simulator) || !validateLocType(locType)) {
return;
}
CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
- DeviceId deviceId = sim.deviceId(deviceName);
HostId id = sim.nextHostId();
- HostLocation location = findAvailablePort(deviceId);
+ Set<HostLocation> locations = getLocations(sim, deviceNames);
+ Set<IpAddress> ips = getIps(hostIps);
+
BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class);
+ setUiCoordinates(cfg, locType, latOrY, longOrX);
- cfg.locType(locType);
- cfg.setLocations(new HashSet<HostLocation>() {{ add(location); }});
+ sim.createHost(id, locations, ips);
+ }
- if (GEO.equals(locType)) {
- cfg.latitude(latOrY).longitude(longOrX);
- } else {
- cfg.gridX(longOrX).gridY(latOrY);
+ private Set<IpAddress> getIps(String hostIps) {
+ ImmutableSet.Builder<IpAddress> ips = ImmutableSet.builder();
+ String[] csv = hostIps.split(",");
+ for (String s : csv) {
+ ips.add(IpAddress.valueOf(s));
}
- cfg.apply();
+ return ips.build();
+ }
- sim.createHost(id, location, IpAddress.valueOf(hostIp));
+ private Set<HostLocation> getLocations(CustomTopologySimulator sim, String deviceNames) {
+ ImmutableSet.Builder<HostLocation> locations = ImmutableSet.builder();
+ String[] csv = deviceNames.split(",");
+ for (String s : csv) {
+ locations.add(requireNonNull(findAvailablePort(sim.deviceId(s))));
+ }
+ return locations.build();
}
// Finds an available connect point among edge ports of the specified device
private HostLocation findAvailablePort(DeviceId deviceId) {
- EdgePortService eps = get(EdgePortService.class);
- HostService hs = get(HostService.class);
- Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator();
-
- while (points.hasNext()) {
- ConnectPoint point = points.next();
- if (hs.getConnectedHosts(point).isEmpty()) {
- return new HostLocation(point, System.currentTimeMillis());
- }
- }
- return null;
+ ConnectPoint point = findAvailablePort(deviceId, null);
+ return point == null ? null : new HostLocation(point, System.currentTimeMillis());
}
}
diff --git a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullLink.java b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullLink.java
index 1b609cf..72e1781 100644
--- a/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullLink.java
+++ b/providers/null/src/main/java/org/onosproject/provider/nil/cli/CreateNullLink.java
@@ -19,40 +19,32 @@
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.apache.karaf.shell.commands.Option;
-import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.net.ConnectPoint;
-import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
-import org.onosproject.net.edge.EdgePortService;
-import org.onosproject.net.host.HostService;
import org.onosproject.provider.nil.CustomTopologySimulator;
import org.onosproject.provider.nil.NullProviders;
import org.onosproject.provider.nil.TopologySimulator;
-import java.util.Iterator;
-import java.util.Objects;
-
/**
* Adds a simulated link to the custom topology simulation.
*/
@Command(scope = "onos", name = "null-create-link",
description = "Adds a simulated link to the custom topology simulation")
-public class CreateNullLink extends AbstractShellCommand {
+public class CreateNullLink extends CreateNullEntity {
@Argument(index = 0, name = "type", description = "Link type, e.g. direct, indirect, optical",
- required = true, multiValued = false)
+ required = true)
String type = null;
@Argument(index = 1, name = "src", description = "Source device name",
- required = true, multiValued = false)
+ required = true)
String src = null;
@Argument(index = 2, name = "dst", description = "Destination device name",
- required = true, multiValued = false)
+ required = true)
String dst = null;
- @Option(name = "-u", aliases = "--unidirectional", description = "Unidirectional link only",
- required = false, multiValued = false)
+ @Option(name = "-u", aliases = "--unidirectional", description = "Unidirectional link only")
private boolean unidirectional = false;
@Override
@@ -71,19 +63,4 @@
sim.createLink(one, two, Link.Type.valueOf(type.toUpperCase()), !unidirectional);
}
- // Finds an available connect point among edge ports of the specified device
- private ConnectPoint findAvailablePort(DeviceId deviceId, ConnectPoint otherPoint) {
- EdgePortService eps = get(EdgePortService.class);
- HostService hs = get(HostService.class);
- Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator();
-
- while (points.hasNext()) {
- ConnectPoint point = points.next();
- if (!Objects.equals(point, otherPoint) && hs.getConnectedHosts(point).isEmpty()) {
- return point;
- }
- }
- return null;
- }
-
}