blob: e1eb5dd1ce37f9139c77b4e954dfc4847933233f [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 Vachuska4a315162018-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 Vachuska4407a7e2016-07-02 12:38:01 +020023import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.HostId;
26import org.onosproject.net.HostLocation;
27import org.onosproject.net.config.NetworkConfigService;
28import org.onosproject.net.config.basics.BasicHostConfig;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020029import org.onosproject.provider.nil.CustomTopologySimulator;
30import org.onosproject.provider.nil.NullProviders;
31import org.onosproject.provider.nil.TopologySimulator;
32
Thomas Vachuska4a315162018-02-14 16:50:16 -080033import java.util.Set;
34
35import static java.util.Objects.requireNonNull;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020036
37/**
38 * Adds a simulated end-station host to the custom topology simulation.
39 */
40@Command(scope = "onos", name = "null-create-host",
41 description = "Adds a simulated end-station host to the custom topology simulation")
Thomas Vachuska4a315162018-02-14 16:50:16 -080042public class CreateNullHost extends CreateNullEntity {
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020043
Thomas Vachuska4a315162018-02-14 16:50:16 -080044 @Argument(index = 0, name = "deviceNames", description = "Name of device where host is attached; comma-separated",
45 required = true)
46 String deviceNames = null;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020047
Thomas Vachuska4a315162018-02-14 16:50:16 -080048 @Argument(index = 1, name = "hostIps", description = "Host IP addresses; comma-separated",
49 required = true)
50 String hostIps = null;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020051
Simon Hunteb3cf542017-02-10 13:18:41 -080052 @Argument(index = 2, name = "latOrY",
53 description = "Geo latitude / Grid y-coord",
Thomas Vachuska4a315162018-02-14 16:50:16 -080054 required = false)
Simon Hunteb3cf542017-02-10 13:18:41 -080055 Double latOrY = null;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020056
Simon Hunteb3cf542017-02-10 13:18:41 -080057 @Argument(index = 3, name = "longOrX",
58 description = "Geo longitude / Grid x-coord",
Thomas Vachuska4a315162018-02-14 16:50:16 -080059 required = false)
Simon Hunteb3cf542017-02-10 13:18:41 -080060 Double longOrX = null;
61
62 @Argument(index = 4, name = "locType", description = "Location type {geo|grid}",
Thomas Vachuska4a315162018-02-14 16:50:16 -080063 required = false)
Simon Hunteb3cf542017-02-10 13:18:41 -080064 String locType = GEO;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020065
66 @Override
67 protected void execute() {
68 NullProviders service = get(NullProviders.class);
69 NetworkConfigService cfgService = get(NetworkConfigService.class);
70
71 TopologySimulator simulator = service.currentSimulator();
Thomas Vachuska4a315162018-02-14 16:50:16 -080072 if (!validateSimulator(simulator) || !validateLocType(locType)) {
Simon Hunteb3cf542017-02-10 13:18:41 -080073 return;
74 }
75
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020076 CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
Thomas Vachuska83da9b72016-07-02 12:52:23 +020077 HostId id = sim.nextHostId();
Thomas Vachuska4a315162018-02-14 16:50:16 -080078 Set<HostLocation> locations = getLocations(sim, deviceNames);
79 Set<IpAddress> ips = getIps(hostIps);
80
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020081 BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class);
Thomas Vachuska4a315162018-02-14 16:50:16 -080082 setUiCoordinates(cfg, locType, latOrY, longOrX);
Simon Hunteb3cf542017-02-10 13:18:41 -080083
Thomas Vachuska4a315162018-02-14 16:50:16 -080084 sim.createHost(id, locations, ips);
85 }
Simon Hunteb3cf542017-02-10 13:18:41 -080086
Thomas Vachuska4a315162018-02-14 16:50:16 -080087 private Set<IpAddress> getIps(String hostIps) {
88 ImmutableSet.Builder<IpAddress> ips = ImmutableSet.builder();
89 String[] csv = hostIps.split(",");
90 for (String s : csv) {
91 ips.add(IpAddress.valueOf(s));
Simon Hunteb3cf542017-02-10 13:18:41 -080092 }
Thomas Vachuska4a315162018-02-14 16:50:16 -080093 return ips.build();
94 }
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020095
Thomas Vachuska4a315162018-02-14 16:50:16 -080096 private Set<HostLocation> getLocations(CustomTopologySimulator sim, String deviceNames) {
97 ImmutableSet.Builder<HostLocation> locations = ImmutableSet.builder();
98 String[] csv = deviceNames.split(",");
99 for (String s : csv) {
100 locations.add(requireNonNull(findAvailablePort(sim.deviceId(s))));
101 }
102 return locations.build();
Thomas Vachuska4407a7e2016-07-02 12:38:01 +0200103 }
104
105 // Finds an available connect point among edge ports of the specified device
106 private HostLocation findAvailablePort(DeviceId deviceId) {
Thomas Vachuska4a315162018-02-14 16:50:16 -0800107 ConnectPoint point = findAvailablePort(deviceId, null);
108 return point == null ? null : new HostLocation(point, System.currentTimeMillis());
Thomas Vachuska4407a7e2016-07-02 12:38:01 +0200109 }
110
111}