blob: d079b23eb605738bfe1d836a18c68a75d8396f18 [file] [log] [blame]
Thomas Vachuska4407a7e2016-07-02 12:38:01 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Thomas Vachuska4407a7e2016-07-02 12:38:01 +02003 *
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
Thomas Vachuskab1906d22018-02-14 16:50:16 -080019import com.google.common.collect.ImmutableSet;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020020import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
22import org.onlab.packet.IpAddress;
Thomas Vachuska84b4fcc2018-03-22 14:29:06 -070023import org.onlab.util.Tools;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.HostId;
27import org.onosproject.net.HostLocation;
28import org.onosproject.net.config.NetworkConfigService;
29import org.onosproject.net.config.basics.BasicHostConfig;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020030import org.onosproject.provider.nil.CustomTopologySimulator;
31import org.onosproject.provider.nil.NullProviders;
32import org.onosproject.provider.nil.TopologySimulator;
33
Thomas Vachuskab1906d22018-02-14 16:50:16 -080034import java.util.Set;
35
36import static java.util.Objects.requireNonNull;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020037
38/**
39 * Adds a simulated end-station host to the custom topology simulation.
40 */
41@Command(scope = "onos", name = "null-create-host",
42 description = "Adds a simulated end-station host to the custom topology simulation")
Thomas Vachuskab1906d22018-02-14 16:50:16 -080043public class CreateNullHost extends CreateNullEntity {
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020044
Thomas Vachuskab1906d22018-02-14 16:50:16 -080045 @Argument(index = 0, name = "deviceNames", description = "Name of device where host is attached; comma-separated",
46 required = true)
47 String deviceNames = null;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020048
Thomas Vachuskab1906d22018-02-14 16:50:16 -080049 @Argument(index = 1, name = "hostIps", description = "Host IP addresses; comma-separated",
50 required = true)
51 String hostIps = null;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020052
Simon Hunteb3cf542017-02-10 13:18:41 -080053 @Argument(index = 2, name = "latOrY",
54 description = "Geo latitude / Grid y-coord",
Thomas Vachuskab1906d22018-02-14 16:50:16 -080055 required = false)
Simon Hunteb3cf542017-02-10 13:18:41 -080056 Double latOrY = null;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020057
Simon Hunteb3cf542017-02-10 13:18:41 -080058 @Argument(index = 3, name = "longOrX",
59 description = "Geo longitude / Grid x-coord",
Thomas Vachuskab1906d22018-02-14 16:50:16 -080060 required = false)
Simon Hunteb3cf542017-02-10 13:18:41 -080061 Double longOrX = null;
62
63 @Argument(index = 4, name = "locType", description = "Location type {geo|grid}",
Thomas Vachuskab1906d22018-02-14 16:50:16 -080064 required = false)
Simon Hunteb3cf542017-02-10 13:18:41 -080065 String locType = GEO;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020066
67 @Override
68 protected void execute() {
69 NullProviders service = get(NullProviders.class);
70 NetworkConfigService cfgService = get(NetworkConfigService.class);
71
72 TopologySimulator simulator = service.currentSimulator();
Thomas Vachuskab1906d22018-02-14 16:50:16 -080073 if (!validateSimulator(simulator) || !validateLocType(locType)) {
Simon Hunteb3cf542017-02-10 13:18:41 -080074 return;
75 }
76
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020077 CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
Thomas Vachuska83da9b72016-07-02 12:52:23 +020078 HostId id = sim.nextHostId();
Thomas Vachuskab1906d22018-02-14 16:50:16 -080079 Set<HostLocation> locations = getLocations(sim, deviceNames);
80 Set<IpAddress> ips = getIps(hostIps);
81
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020082 BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class);
Thomas Vachuskab1906d22018-02-14 16:50:16 -080083 setUiCoordinates(cfg, locType, latOrY, longOrX);
Simon Hunteb3cf542017-02-10 13:18:41 -080084
Thomas Vachuska84b4fcc2018-03-22 14:29:06 -070085 Tools.delay(10);
Thomas Vachuskab1906d22018-02-14 16:50:16 -080086 sim.createHost(id, locations, ips);
87 }
Simon Hunteb3cf542017-02-10 13:18:41 -080088
Thomas Vachuskab1906d22018-02-14 16:50:16 -080089 private Set<IpAddress> getIps(String hostIps) {
90 ImmutableSet.Builder<IpAddress> ips = ImmutableSet.builder();
91 String[] csv = hostIps.split(",");
92 for (String s : csv) {
93 ips.add(IpAddress.valueOf(s));
Simon Hunteb3cf542017-02-10 13:18:41 -080094 }
Thomas Vachuskab1906d22018-02-14 16:50:16 -080095 return ips.build();
96 }
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020097
Thomas Vachuskab1906d22018-02-14 16:50:16 -080098 private Set<HostLocation> getLocations(CustomTopologySimulator sim, String deviceNames) {
99 ImmutableSet.Builder<HostLocation> locations = ImmutableSet.builder();
100 String[] csv = deviceNames.split(",");
101 for (String s : csv) {
102 locations.add(requireNonNull(findAvailablePort(sim.deviceId(s))));
103 }
104 return locations.build();
Thomas Vachuska4407a7e2016-07-02 12:38:01 +0200105 }
106
107 // Finds an available connect point among edge ports of the specified device
108 private HostLocation findAvailablePort(DeviceId deviceId) {
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800109 ConnectPoint point = findAvailablePort(deviceId, null);
110 return point == null ? null : new HostLocation(point, System.currentTimeMillis());
Thomas Vachuska4407a7e2016-07-02 12:38:01 +0200111 }
112
113}