Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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; |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 22 | import org.onlab.util.Tools; |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.HostId; |
| 25 | import org.onosproject.net.HostLocation; |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 26 | import org.onosproject.net.config.NetworkConfigService; |
| 27 | import org.onosproject.net.config.basics.BasicHostConfig; |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 28 | import org.onosproject.provider.nil.CustomTopologySimulator; |
| 29 | import org.onosproject.provider.nil.NullProviders; |
| 30 | import org.onosproject.provider.nil.TopologySimulator; |
| 31 | |
| 32 | import java.util.List; |
| 33 | |
| 34 | /** |
| 35 | * Adds a simulated end-station host to the custom topology simulation. |
| 36 | */ |
| 37 | @Command(scope = "onos", name = "null-create-hosts", |
| 38 | description = "Adds a simulated end-station host to the custom topology simulation") |
| 39 | public class CreateNullHosts extends CreateNullEntity { |
| 40 | |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 41 | private static final double NONE = -99999999.9999; |
| 42 | |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 43 | @Argument(index = 0, name = "deviceName", description = "Name of device where hosts are attached", |
| 44 | required = true) |
| 45 | String deviceName = null; |
| 46 | |
| 47 | @Argument(index = 1, name = "hostIpPattern", description = "Host IP pattern", |
| 48 | required = true) |
| 49 | String hostIpPattern = null; |
| 50 | |
| 51 | @Argument(index = 2, name = "hostCount", description = "Number of hosts to create", |
| 52 | required = true) |
| 53 | int hostCount = 0; |
| 54 | |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 55 | @Argument(index = 3, name = "gridY", description = "Grid y-coord for top of host block") |
| 56 | double gridY = NONE; |
| 57 | |
| 58 | @Argument(index = 4, name = "gridX", description = "Grid X-coord for center of host block") |
| 59 | double gridX = NONE; |
| 60 | |
| 61 | @Argument(index = 5, name = "hostsPerRow", description = "Number of hosts to render per row in block") |
| 62 | int hostsPerRow = 5; |
| 63 | |
| 64 | @Argument(index = 6, name = "rowGap", description = "Y gap between rows") |
| 65 | double rowGap = 70; |
| 66 | |
| 67 | @Argument(index = 7, name = "colGap", description = "X gap between rows") |
| 68 | double colGap = 50; |
| 69 | |
| 70 | |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 71 | @Override |
| 72 | protected void execute() { |
| 73 | NullProviders service = get(NullProviders.class); |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 74 | NetworkConfigService cfgService = get(NetworkConfigService.class); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 75 | |
| 76 | TopologySimulator simulator = service.currentSimulator(); |
| 77 | if (!validateSimulator(simulator)) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | CustomTopologySimulator sim = (CustomTopologySimulator) simulator; |
| 82 | |
| 83 | List<ConnectPoint> points = findAvailablePorts(sim.deviceId(deviceName)); |
| 84 | String pattern = hostIpPattern.replace("*", "%d"); |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 85 | double yStep = rowGap / hostsPerRow; |
| 86 | double y = gridY; |
| 87 | double x = gridX - (colGap * (hostsPerRow - 1)) / 2; |
| 88 | |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 89 | for (int h = 0; h < hostCount; h++) { |
| 90 | HostLocation location = new HostLocation(points.get(h), System.currentTimeMillis()); |
| 91 | IpAddress ip = IpAddress.valueOf(String.format(pattern, h)); |
| 92 | HostId id = sim.nextHostId(); |
Thomas Vachuska | fa61549 | 2018-03-19 09:11:51 -0700 | [diff] [blame] | 93 | |
| 94 | if (gridY != NONE) { |
| 95 | BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class); |
| 96 | setUiCoordinates(cfg, GRID, y, x); |
| 97 | if (((h + 1) % hostsPerRow) == 0) { |
| 98 | x = gridX - (colGap * (hostsPerRow - 1)) / 2; |
| 99 | } else { |
| 100 | x += colGap; |
| 101 | y += yStep; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | Tools.delay(10); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 106 | sim.createHost(id, location, ip); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | } |