blob: c9b9301964d948e0e235b0ab7f5d55f9ca2c7843 [file] [log] [blame]
Thomas Vachuskaa8e74772018-02-26 11:33:35 -08001/*
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
17package org.onosproject.provider.nil.cli;
18
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.onlab.packet.IpAddress;
Thomas Vachuskafa615492018-03-19 09:11:51 -070022import org.onlab.util.Tools;
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080023import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.HostId;
25import org.onosproject.net.HostLocation;
Thomas Vachuskafa615492018-03-19 09:11:51 -070026import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.net.config.basics.BasicHostConfig;
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080028import org.onosproject.provider.nil.CustomTopologySimulator;
29import org.onosproject.provider.nil.NullProviders;
30import org.onosproject.provider.nil.TopologySimulator;
31
32import 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")
39public class CreateNullHosts extends CreateNullEntity {
40
Thomas Vachuskafa615492018-03-19 09:11:51 -070041 private static final double NONE = -99999999.9999;
42
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080043 @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 Vachuskafa615492018-03-19 09:11:51 -070055 @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 Vachuskaa8e74772018-02-26 11:33:35 -080071 @Override
72 protected void execute() {
73 NullProviders service = get(NullProviders.class);
Thomas Vachuskafa615492018-03-19 09:11:51 -070074 NetworkConfigService cfgService = get(NetworkConfigService.class);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080075
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 Vachuskafa615492018-03-19 09:11:51 -070085 double yStep = rowGap / hostsPerRow;
86 double y = gridY;
87 double x = gridX - (colGap * (hostsPerRow - 1)) / 2;
88
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080089 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 Vachuskafa615492018-03-19 09:11:51 -070093
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 Vachuskaa8e74772018-02-26 11:33:35 -0800106 sim.createHost(id, location, ip);
107 }
108 }
109
110}