Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 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 | |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Argument; |
| 21 | import org.apache.karaf.shell.commands.Command; |
| 22 | import org.onlab.packet.IpAddress; |
Thomas Vachuska | 84b4fcc | 2018-03-22 14:29:06 -0700 | [diff] [blame] | 23 | import org.onlab.util.Tools; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.HostId; |
| 27 | import org.onosproject.net.HostLocation; |
| 28 | import org.onosproject.net.config.NetworkConfigService; |
| 29 | import org.onosproject.net.config.basics.BasicHostConfig; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 30 | import org.onosproject.provider.nil.CustomTopologySimulator; |
| 31 | import org.onosproject.provider.nil.NullProviders; |
| 32 | import org.onosproject.provider.nil.TopologySimulator; |
| 33 | |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 34 | import java.util.Set; |
| 35 | |
| 36 | import static java.util.Objects.requireNonNull; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Adds a simulated end-station host to the custom topology simulation. |
| 40 | */ |
| 41 | @Command(scope = "onos", name = "null-create-host", |
| 42 | description = "Adds a simulated end-station host to the custom topology simulation") |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 43 | public class CreateNullHost extends CreateNullEntity { |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 44 | |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 45 | @Argument(index = 0, name = "deviceNames", description = "Name of device where host is attached; comma-separated", |
| 46 | required = true) |
| 47 | String deviceNames = null; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 48 | |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 49 | @Argument(index = 1, name = "hostIps", description = "Host IP addresses; comma-separated", |
| 50 | required = true) |
| 51 | String hostIps = null; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 52 | |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 53 | @Argument(index = 2, name = "latOrY", |
| 54 | description = "Geo latitude / Grid y-coord", |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 55 | required = false) |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 56 | Double latOrY = null; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 57 | |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 58 | @Argument(index = 3, name = "longOrX", |
| 59 | description = "Geo longitude / Grid x-coord", |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 60 | required = false) |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 61 | Double longOrX = null; |
| 62 | |
| 63 | @Argument(index = 4, name = "locType", description = "Location type {geo|grid}", |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 64 | required = false) |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 65 | String locType = GEO; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 66 | |
| 67 | @Override |
| 68 | protected void execute() { |
| 69 | NullProviders service = get(NullProviders.class); |
| 70 | NetworkConfigService cfgService = get(NetworkConfigService.class); |
| 71 | |
| 72 | TopologySimulator simulator = service.currentSimulator(); |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 73 | if (!validateSimulator(simulator) || !validateLocType(locType)) { |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 77 | CustomTopologySimulator sim = (CustomTopologySimulator) simulator; |
Thomas Vachuska | 83da9b7 | 2016-07-02 12:52:23 +0200 | [diff] [blame] | 78 | HostId id = sim.nextHostId(); |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 79 | Set<HostLocation> locations = getLocations(sim, deviceNames); |
| 80 | Set<IpAddress> ips = getIps(hostIps); |
| 81 | |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 82 | BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class); |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 83 | setUiCoordinates(cfg, locType, latOrY, longOrX); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 84 | |
Thomas Vachuska | 84b4fcc | 2018-03-22 14:29:06 -0700 | [diff] [blame] | 85 | Tools.delay(10); |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 86 | sim.createHost(id, locations, ips); |
| 87 | } |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 88 | |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 89 | private Set<IpAddress> getIps(String hostIps) { |
| 90 | ImmutableSet.Builder<IpAddress> ips = ImmutableSet.builder(); |
| 91 | String[] csv = hostIps.split(","); |
| 92 | for (String s : csv) { |
| 93 | ips.add(IpAddress.valueOf(s)); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 94 | } |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 95 | return ips.build(); |
| 96 | } |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 97 | |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 98 | private Set<HostLocation> getLocations(CustomTopologySimulator sim, String deviceNames) { |
| 99 | ImmutableSet.Builder<HostLocation> locations = ImmutableSet.builder(); |
| 100 | String[] csv = deviceNames.split(","); |
| 101 | for (String s : csv) { |
| 102 | locations.add(requireNonNull(findAvailablePort(sim.deviceId(s)))); |
| 103 | } |
| 104 | return locations.build(); |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Finds an available connect point among edge ports of the specified device |
| 108 | private HostLocation findAvailablePort(DeviceId deviceId) { |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 109 | ConnectPoint point = findAvailablePort(deviceId, null); |
| 110 | return point == null ? null : new HostLocation(point, System.currentTimeMillis()); |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | } |