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