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