Thomas Vachuska

Adding ability to add simulated hosts to the null provider.

Change-Id: Iaf96caf904e2e8a724064be39211087bf681dad0
...@@ -17,11 +17,14 @@ ...@@ -17,11 +17,14 @@
17 package org.onosproject.provider.nil; 17 package org.onosproject.provider.nil;
18 18
19 import com.google.common.collect.Maps; 19 import com.google.common.collect.Maps;
20 +import org.onlab.packet.IpAddress;
20 import org.onlab.packet.MacAddress; 21 import org.onlab.packet.MacAddress;
21 import org.onlab.packet.VlanId; 22 import org.onlab.packet.VlanId;
22 import org.onosproject.net.Device; 23 import org.onosproject.net.Device;
23 import org.onosproject.net.DeviceId; 24 import org.onosproject.net.DeviceId;
24 import org.onosproject.net.HostId; 25 import org.onosproject.net.HostId;
26 +import org.onosproject.net.HostLocation;
27 +import org.onosproject.net.host.DefaultHostDescription;
25 28
26 import java.util.Map; 29 import java.util.Map;
27 30
...@@ -80,6 +83,19 @@ public class CustomTopologySimulator extends TopologySimulator { ...@@ -80,6 +83,19 @@ public class CustomTopologySimulator extends TopologySimulator {
80 nameToId.put(name, id); 83 nameToId.put(name, id);
81 } 84 }
82 85
86 + /**
87 + * Creates a simulated host.
88 + *
89 + * @param hostId host identifier
90 + * @param location host location
91 + * @param hostIp host IP address
92 + */
93 + public void createHost(HostId hostId, HostLocation location, IpAddress hostIp) {
94 + DefaultHostDescription description =
95 + new DefaultHostDescription(hostId.mac(), hostId.vlanId(), location, hostIp);
96 + hostProviderService.hostDetected(hostId, description, false);
97 + }
98 +
83 @Override 99 @Override
84 protected void createDevices() { 100 protected void createDevices() {
85 } 101 }
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.provider.nil.cli;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.onlab.packet.IpAddress;
22 +import org.onosproject.cli.AbstractShellCommand;
23 +import org.onosproject.net.ConnectPoint;
24 +import org.onosproject.net.DeviceId;
25 +import org.onosproject.net.HostId;
26 +import org.onosproject.net.HostLocation;
27 +import org.onosproject.net.config.NetworkConfigService;
28 +import org.onosproject.net.config.basics.BasicHostConfig;
29 +import org.onosproject.net.edge.EdgePortService;
30 +import org.onosproject.net.host.HostService;
31 +import org.onosproject.provider.nil.CustomTopologySimulator;
32 +import org.onosproject.provider.nil.NullProviders;
33 +import org.onosproject.provider.nil.TopologySimulator;
34 +
35 +import java.util.Iterator;
36 +
37 +/**
38 + * Adds a simulated end-station host to the custom topology simulation.
39 + */
40 +@Command(scope = "onos", name = "null-create-host",
41 + description = "Adds a simulated end-station host to the custom topology simulation")
42 +public class CreateNullHost extends AbstractShellCommand {
43 +
44 + @Argument(index = 0, name = "deviceName", description = "Name of device where host is attached",
45 + required = true, multiValued = false)
46 + String deviceName = null;
47 +
48 + @Argument(index = 1, name = "hostId", description = "Host identifier",
49 + required = true, multiValued = false)
50 + String hostId = null;
51 +
52 + @Argument(index = 2, name = "hostIp", description = "Host IP address",
53 + required = true, multiValued = false)
54 + String hostIp = null;
55 +
56 + @Argument(index = 3, name = "latitude", description = "Geo latitude",
57 + required = true, multiValued = false)
58 + Double latitude = null;
59 +
60 + @Argument(index = 4, name = "longitude", description = "Geo longitude",
61 + required = true, multiValued = false)
62 + Double longitude = null;
63 +
64 + @Override
65 + protected void execute() {
66 + NullProviders service = get(NullProviders.class);
67 + NetworkConfigService cfgService = get(NetworkConfigService.class);
68 +
69 + TopologySimulator simulator = service.currentSimulator();
70 + if (!(simulator instanceof CustomTopologySimulator)) {
71 + error("Custom topology simulator is not active.");
72 + return;
73 + }
74 +
75 + CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
76 + DeviceId deviceId = sim.deviceId(deviceName);
77 + HostLocation location = findAvailablePort(deviceId);
78 + HostId id = HostId.hostId(hostId);
79 + BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class);
80 + cfg.latitude(latitude);
81 + cfg.longitude(longitude);
82 + cfg.apply();
83 +
84 + sim.createHost(id, location, IpAddress.valueOf(hostIp));
85 + }
86 +
87 + // Finds an available connect point among edge ports of the specified device
88 + private HostLocation findAvailablePort(DeviceId deviceId) {
89 + EdgePortService eps = get(EdgePortService.class);
90 + HostService hs = get(HostService.class);
91 + Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator();
92 +
93 + while (points.hasNext()) {
94 + ConnectPoint point = points.next();
95 + if (hs.getConnectedHosts(point).isEmpty()) {
96 + return new HostLocation(point, System.currentTimeMillis());
97 + }
98 + }
99 + return null;
100 + }
101 +
102 +}
...@@ -24,11 +24,13 @@ import org.onosproject.net.ConnectPoint; ...@@ -24,11 +24,13 @@ import org.onosproject.net.ConnectPoint;
24 import org.onosproject.net.DeviceId; 24 import org.onosproject.net.DeviceId;
25 import org.onosproject.net.Link; 25 import org.onosproject.net.Link;
26 import org.onosproject.net.edge.EdgePortService; 26 import org.onosproject.net.edge.EdgePortService;
27 +import org.onosproject.net.host.HostService;
27 import org.onosproject.provider.nil.CustomTopologySimulator; 28 import org.onosproject.provider.nil.CustomTopologySimulator;
28 import org.onosproject.provider.nil.NullProviders; 29 import org.onosproject.provider.nil.NullProviders;
29 import org.onosproject.provider.nil.TopologySimulator; 30 import org.onosproject.provider.nil.TopologySimulator;
30 31
31 import java.util.Iterator; 32 import java.util.Iterator;
33 +import java.util.Objects;
32 34
33 /** 35 /**
34 * Adds a simulated link to the custom topology simulation. 36 * Adds a simulated link to the custom topology simulation.
...@@ -64,15 +66,24 @@ public class CreateNullLink extends AbstractShellCommand { ...@@ -64,15 +66,24 @@ public class CreateNullLink extends AbstractShellCommand {
64 } 66 }
65 67
66 CustomTopologySimulator sim = (CustomTopologySimulator) simulator; 68 CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
67 - ConnectPoint one = findAvailablePort(sim.deviceId(src)); 69 + ConnectPoint one = findAvailablePort(sim.deviceId(src), null);
68 - ConnectPoint two = findAvailablePort(sim.deviceId(dst)); 70 + ConnectPoint two = findAvailablePort(sim.deviceId(dst), one);
69 sim.createLink(one, two, Link.Type.valueOf(type.toUpperCase()), !unidirectional); 71 sim.createLink(one, two, Link.Type.valueOf(type.toUpperCase()), !unidirectional);
70 } 72 }
71 73
72 - private ConnectPoint findAvailablePort(DeviceId deviceId) { 74 + // Finds an available connect point among edge ports of the specified device
75 + private ConnectPoint findAvailablePort(DeviceId deviceId, ConnectPoint otherPoint) {
73 EdgePortService eps = get(EdgePortService.class); 76 EdgePortService eps = get(EdgePortService.class);
77 + HostService hs = get(HostService.class);
74 Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator(); 78 Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator();
75 - return points.hasNext() ? points.next() : null; 79 +
80 + while (points.hasNext()) {
81 + ConnectPoint point = points.next();
82 + if (!Objects.equals(point, otherPoint) && hs.getConnectedHosts(point).isEmpty()) {
83 + return point;
84 + }
85 + }
86 + return null;
76 } 87 }
77 88
78 } 89 }
......
...@@ -47,6 +47,9 @@ ...@@ -47,6 +47,9 @@
47 <command> 47 <command>
48 <action class="org.onosproject.provider.nil.cli.CreateNullLink"/> 48 <action class="org.onosproject.provider.nil.cli.CreateNullLink"/>
49 </command> 49 </command>
50 + <command>
51 + <action class="org.onosproject.provider.nil.cli.CreateNullHost"/>
52 + </command>
50 </command-bundle> 53 </command-bundle>
51 54
52 <bean id="startStopCompleter" class="org.onosproject.cli.StartStopCompleter"/> 55 <bean id="startStopCompleter" class="org.onosproject.cli.StartStopCompleter"/>
......