blob: 2eb3fe3327160e8274eb3bcba6d617d3ea443636 [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;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Argument;
21import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020023import org.onlab.packet.IpAddress;
Thomas Vachuska84b4fcc2018-03-22 14:29:06 -070024import org.onlab.util.Tools;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020025import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.HostId;
28import org.onosproject.net.HostLocation;
29import org.onosproject.net.config.NetworkConfigService;
30import org.onosproject.net.config.basics.BasicHostConfig;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020031import org.onosproject.provider.nil.CustomTopologySimulator;
32import org.onosproject.provider.nil.NullProviders;
33import org.onosproject.provider.nil.TopologySimulator;
34
Thomas Vachuskab1906d22018-02-14 16:50:16 -080035import java.util.Set;
36
37import static java.util.Objects.requireNonNull;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020038
39/**
40 * Adds a simulated end-station host to the custom topology simulation.
41 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070042@Service
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020043@Command(scope = "onos", name = "null-create-host",
44 description = "Adds a simulated end-station host to the custom topology simulation")
Thomas Vachuskab1906d22018-02-14 16:50:16 -080045public class CreateNullHost extends CreateNullEntity {
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020046
Thomas Vachuskab1906d22018-02-14 16:50:16 -080047 @Argument(index = 0, name = "deviceNames", description = "Name of device where host is attached; comma-separated",
48 required = true)
49 String deviceNames = null;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020050
Thomas Vachuskab1906d22018-02-14 16:50:16 -080051 @Argument(index = 1, name = "hostIps", description = "Host IP addresses; comma-separated",
52 required = true)
53 String hostIps = null;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020054
Simon Hunteb3cf542017-02-10 13:18:41 -080055 @Argument(index = 2, name = "latOrY",
56 description = "Geo latitude / Grid y-coord",
Thomas Vachuskab1906d22018-02-14 16:50:16 -080057 required = false)
Simon Hunteb3cf542017-02-10 13:18:41 -080058 Double latOrY = null;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020059
Simon Hunteb3cf542017-02-10 13:18:41 -080060 @Argument(index = 3, name = "longOrX",
61 description = "Geo longitude / Grid x-coord",
Thomas Vachuskab1906d22018-02-14 16:50:16 -080062 required = false)
Simon Hunteb3cf542017-02-10 13:18:41 -080063 Double longOrX = null;
64
65 @Argument(index = 4, name = "locType", description = "Location type {geo|grid}",
Thomas Vachuskab1906d22018-02-14 16:50:16 -080066 required = false)
Simon Hunteb3cf542017-02-10 13:18:41 -080067 String locType = GEO;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020068
69 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070070 protected void doExecute() {
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020071 NullProviders service = get(NullProviders.class);
72 NetworkConfigService cfgService = get(NetworkConfigService.class);
73
74 TopologySimulator simulator = service.currentSimulator();
Thomas Vachuskab1906d22018-02-14 16:50:16 -080075 if (!validateSimulator(simulator) || !validateLocType(locType)) {
Simon Hunteb3cf542017-02-10 13:18:41 -080076 return;
77 }
78
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020079 CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
Thomas Vachuska83da9b72016-07-02 12:52:23 +020080 HostId id = sim.nextHostId();
Thomas Vachuskab1906d22018-02-14 16:50:16 -080081 Set<HostLocation> locations = getLocations(sim, deviceNames);
82 Set<IpAddress> ips = getIps(hostIps);
83
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020084 BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class);
Thomas Vachuskab1906d22018-02-14 16:50:16 -080085 setUiCoordinates(cfg, locType, latOrY, longOrX);
Simon Hunteb3cf542017-02-10 13:18:41 -080086
Thomas Vachuska84b4fcc2018-03-22 14:29:06 -070087 Tools.delay(10);
Thomas Vachuskab1906d22018-02-14 16:50:16 -080088 sim.createHost(id, locations, ips);
89 }
Simon Hunteb3cf542017-02-10 13:18:41 -080090
Thomas Vachuskab1906d22018-02-14 16:50:16 -080091 private Set<IpAddress> getIps(String hostIps) {
92 ImmutableSet.Builder<IpAddress> ips = ImmutableSet.builder();
93 String[] csv = hostIps.split(",");
94 for (String s : csv) {
95 ips.add(IpAddress.valueOf(s));
Simon Hunteb3cf542017-02-10 13:18:41 -080096 }
Thomas Vachuskab1906d22018-02-14 16:50:16 -080097 return ips.build();
98 }
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020099
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800100 private Set<HostLocation> getLocations(CustomTopologySimulator sim, String deviceNames) {
101 ImmutableSet.Builder<HostLocation> locations = ImmutableSet.builder();
102 String[] csv = deviceNames.split(",");
103 for (String s : csv) {
104 locations.add(requireNonNull(findAvailablePort(sim.deviceId(s))));
105 }
106 return locations.build();
Thomas Vachuska4407a7e2016-07-02 12:38:01 +0200107 }
108
109 // Finds an available connect point among edge ports of the specified device
110 private HostLocation findAvailablePort(DeviceId deviceId) {
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800111 ConnectPoint point = findAvailablePort(deviceId, null);
112 return point == null ? null : new HostLocation(point, System.currentTimeMillis());
Thomas Vachuska4407a7e2016-07-02 12:38:01 +0200113 }
114
115}