blob: 5197e796ac46dce2fe70167241a0b961435f3b0d [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
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.onlab.packet.IpAddress;
22import org.onosproject.cli.AbstractShellCommand;
23import 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;
29import org.onosproject.net.edge.EdgePortService;
30import org.onosproject.net.host.HostService;
31import org.onosproject.provider.nil.CustomTopologySimulator;
32import org.onosproject.provider.nil.NullProviders;
33import org.onosproject.provider.nil.TopologySimulator;
34
Sean Condonc59d6962017-08-17 12:21:58 +010035import java.util.HashSet;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020036import java.util.Iterator;
37
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")
43public class CreateNullHost extends AbstractShellCommand {
Simon Hunteb3cf542017-02-10 13:18:41 -080044 private static final String GEO = "geo";
45 private static final String GRID = "grid";
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020046
47 @Argument(index = 0, name = "deviceName", description = "Name of device where host is attached",
48 required = true, multiValued = false)
49 String deviceName = null;
50
Thomas Vachuska83da9b72016-07-02 12:52:23 +020051 @Argument(index = 1, name = "hostIp", description = "Host IP address",
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020052 required = true, multiValued = false)
53 String hostIp = null;
54
Simon Hunteb3cf542017-02-10 13:18:41 -080055 @Argument(index = 2, name = "latOrY",
56 description = "Geo latitude / Grid y-coord",
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020057 required = true, multiValued = 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 Vachuska4407a7e2016-07-02 12:38:01 +020062 required = true, multiValued = false)
Simon Hunteb3cf542017-02-10 13:18:41 -080063 Double longOrX = null;
64
65 @Argument(index = 4, name = "locType", description = "Location type {geo|grid}",
66 required = false, multiValued = false)
67 String locType = GEO;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020068
69 @Override
70 protected void execute() {
71 NullProviders service = get(NullProviders.class);
72 NetworkConfigService cfgService = get(NetworkConfigService.class);
73
74 TopologySimulator simulator = service.currentSimulator();
75 if (!(simulator instanceof CustomTopologySimulator)) {
76 error("Custom topology simulator is not active.");
77 return;
78 }
79
Simon Hunteb3cf542017-02-10 13:18:41 -080080 if (!(GEO.equals(locType) || GRID.equals(locType))) {
81 error("locType must be 'geo' or 'grid'.");
82 return;
83 }
84
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020085 CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
86 DeviceId deviceId = sim.deviceId(deviceName);
Thomas Vachuska83da9b72016-07-02 12:52:23 +020087 HostId id = sim.nextHostId();
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020088 HostLocation location = findAvailablePort(deviceId);
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020089 BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class);
Simon Hunteb3cf542017-02-10 13:18:41 -080090
91 cfg.locType(locType);
Sean Condonc59d6962017-08-17 12:21:58 +010092 cfg.setLocations(new HashSet<HostLocation>() {{ add(location); }});
Simon Hunteb3cf542017-02-10 13:18:41 -080093
94 if (GEO.equals(locType)) {
95 cfg.latitude(latOrY).longitude(longOrX);
96 } else {
97 cfg.gridX(longOrX).gridY(latOrY);
98 }
99 cfg.apply();
Thomas Vachuska4407a7e2016-07-02 12:38:01 +0200100
101 sim.createHost(id, location, IpAddress.valueOf(hostIp));
102 }
103
104 // Finds an available connect point among edge ports of the specified device
105 private HostLocation findAvailablePort(DeviceId deviceId) {
106 EdgePortService eps = get(EdgePortService.class);
107 HostService hs = get(HostService.class);
108 Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator();
109
110 while (points.hasNext()) {
111 ConnectPoint point = points.next();
112 if (hs.getConnectedHosts(point).isEmpty()) {
113 return new HostLocation(point, System.currentTimeMillis());
114 }
115 }
116 return null;
117 }
118
119}