blob: ee4c835872d7ab751acaea46a67adf61351237af [file] [log] [blame]
Thomas Vachuska4407a7e2016-07-02 12:38:01 +02001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
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
35import java.util.Iterator;
36
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")
42public class CreateNullHost extends AbstractShellCommand {
Simon Hunteb3cf542017-02-10 13:18:41 -080043 private static final String GEO = "geo";
44 private static final String GRID = "grid";
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020045
46 @Argument(index = 0, name = "deviceName", description = "Name of device where host is attached",
47 required = true, multiValued = false)
48 String deviceName = null;
49
Thomas Vachuska83da9b72016-07-02 12:52:23 +020050 @Argument(index = 1, name = "hostIp", description = "Host IP address",
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020051 required = true, multiValued = false)
52 String hostIp = null;
53
Simon Hunteb3cf542017-02-10 13:18:41 -080054 @Argument(index = 2, name = "latOrY",
55 description = "Geo latitude / Grid y-coord",
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020056 required = true, multiValued = false)
Simon Hunteb3cf542017-02-10 13:18:41 -080057 Double latOrY = null;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020058
Simon Hunteb3cf542017-02-10 13:18:41 -080059 @Argument(index = 3, name = "longOrX",
60 description = "Geo longitude / Grid x-coord",
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020061 required = true, multiValued = false)
Simon Hunteb3cf542017-02-10 13:18:41 -080062 Double longOrX = null;
63
64 @Argument(index = 4, name = "locType", description = "Location type {geo|grid}",
65 required = false, multiValued = false)
66 String locType = GEO;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020067
68 @Override
69 protected void execute() {
70 NullProviders service = get(NullProviders.class);
71 NetworkConfigService cfgService = get(NetworkConfigService.class);
72
73 TopologySimulator simulator = service.currentSimulator();
74 if (!(simulator instanceof CustomTopologySimulator)) {
75 error("Custom topology simulator is not active.");
76 return;
77 }
78
Simon Hunteb3cf542017-02-10 13:18:41 -080079 if (!(GEO.equals(locType) || GRID.equals(locType))) {
80 error("locType must be 'geo' or 'grid'.");
81 return;
82 }
83
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020084 CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
85 DeviceId deviceId = sim.deviceId(deviceName);
Thomas Vachuska83da9b72016-07-02 12:52:23 +020086 HostId id = sim.nextHostId();
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020087 HostLocation location = findAvailablePort(deviceId);
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020088 BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class);
Simon Hunteb3cf542017-02-10 13:18:41 -080089
90 cfg.locType(locType);
91
92 if (GEO.equals(locType)) {
93 cfg.latitude(latOrY).longitude(longOrX);
94 } else {
95 cfg.gridX(longOrX).gridY(latOrY);
96 }
97 cfg.apply();
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020098
99 sim.createHost(id, location, IpAddress.valueOf(hostIp));
100 }
101
102 // Finds an available connect point among edge ports of the specified device
103 private HostLocation findAvailablePort(DeviceId deviceId) {
104 EdgePortService eps = get(EdgePortService.class);
105 HostService hs = get(HostService.class);
106 Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator();
107
108 while (points.hasNext()) {
109 ConnectPoint point = points.next();
110 if (hs.getConnectedHosts(point).isEmpty()) {
111 return new HostLocation(point, System.currentTimeMillis());
112 }
113 }
114 return null;
115 }
116
117}