blob: 1ea55a4b93c25d2d0f960593d73a9ae31bedde2b [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();
Sean Condon8d338b52019-03-21 20:20:58 +000081 Set<HostLocation> locations;
82 try {
83 locations = getLocations(sim, deviceNames);
84 } catch (NoLocationException e) {
85 error("\u001B[1;31mHost not created - no location (free port) available on %s\u001B[0m", e.getMessage());
86 return;
87 }
Thomas Vachuskab1906d22018-02-14 16:50:16 -080088 Set<IpAddress> ips = getIps(hostIps);
89
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020090 BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class);
Thomas Vachuskab1906d22018-02-14 16:50:16 -080091 setUiCoordinates(cfg, locType, latOrY, longOrX);
Simon Hunteb3cf542017-02-10 13:18:41 -080092
Thomas Vachuska84b4fcc2018-03-22 14:29:06 -070093 Tools.delay(10);
Thomas Vachuskab1906d22018-02-14 16:50:16 -080094 sim.createHost(id, locations, ips);
95 }
Simon Hunteb3cf542017-02-10 13:18:41 -080096
Thomas Vachuskab1906d22018-02-14 16:50:16 -080097 private Set<IpAddress> getIps(String hostIps) {
98 ImmutableSet.Builder<IpAddress> ips = ImmutableSet.builder();
99 String[] csv = hostIps.split(",");
100 for (String s : csv) {
101 ips.add(IpAddress.valueOf(s));
Simon Hunteb3cf542017-02-10 13:18:41 -0800102 }
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800103 return ips.build();
104 }
Thomas Vachuska4407a7e2016-07-02 12:38:01 +0200105
Sean Condon8d338b52019-03-21 20:20:58 +0000106 private Set<HostLocation> getLocations(CustomTopologySimulator sim, String deviceNames)
107 throws NoLocationException {
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800108 ImmutableSet.Builder<HostLocation> locations = ImmutableSet.builder();
109 String[] csv = deviceNames.split(",");
110 for (String s : csv) {
Sean Condon8d338b52019-03-21 20:20:58 +0000111 HostLocation loc = findAvailablePort(sim.deviceId(s));
112 if (loc == null) {
113 throw new NoLocationException(deviceNames);
114 }
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800115 locations.add(requireNonNull(findAvailablePort(sim.deviceId(s))));
116 }
117 return locations.build();
Thomas Vachuska4407a7e2016-07-02 12:38:01 +0200118 }
119
120 // Finds an available connect point among edge ports of the specified device
121 private HostLocation findAvailablePort(DeviceId deviceId) {
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800122 ConnectPoint point = findAvailablePort(deviceId, null);
123 return point == null ? null : new HostLocation(point, System.currentTimeMillis());
Thomas Vachuska4407a7e2016-07-02 12:38:01 +0200124 }
125
126}