blob: cf769fdbfed30de8c896dd209377a6a53d2afd6a [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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080022import org.onlab.packet.IpAddress;
Thomas Vachuskafa615492018-03-19 09:11:51 -070023import org.onlab.util.Tools;
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.HostId;
26import org.onosproject.net.HostLocation;
Thomas Vachuskafa615492018-03-19 09:11:51 -070027import org.onosproject.net.config.NetworkConfigService;
28import org.onosproject.net.config.basics.BasicHostConfig;
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080029import org.onosproject.provider.nil.CustomTopologySimulator;
30import org.onosproject.provider.nil.NullProviders;
31import org.onosproject.provider.nil.TopologySimulator;
32
33import java.util.List;
34
35/**
36 * Adds a simulated end-station host to the custom topology simulation.
37 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070038@Service
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080039@Command(scope = "onos", name = "null-create-hosts",
40 description = "Adds a simulated end-station host to the custom topology simulation")
41public class CreateNullHosts extends CreateNullEntity {
42
Thomas Vachuskafa615492018-03-19 09:11:51 -070043 private static final double NONE = -99999999.9999;
44
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080045 @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 Vachuskafa615492018-03-19 09:11:51 -070057 @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 Vachuskaa8e74772018-02-26 11:33:35 -080073 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070074 protected void doExecute() {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080075 NullProviders service = get(NullProviders.class);
Thomas Vachuskafa615492018-03-19 09:11:51 -070076 NetworkConfigService cfgService = get(NetworkConfigService.class);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080077
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 Vachuskafa615492018-03-19 09:11:51 -070087 double yStep = rowGap / hostsPerRow;
88 double y = gridY;
89 double x = gridX - (colGap * (hostsPerRow - 1)) / 2;
90
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080091 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 Vachuskafa615492018-03-19 09:11:51 -070095
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 Vachuskaa8e74772018-02-26 11:33:35 -0800108 sim.createHost(id, location, ip);
109 }
110 }
111
112}