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