blob: 93ef2a6f3390dc717e1c04be69acc2e480019320 [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 {
43
44 @Argument(index = 0, name = "deviceName", description = "Name of device where host is attached",
45 required = true, multiValued = false)
46 String deviceName = null;
47
Thomas Vachuska83da9b72016-07-02 12:52:23 +020048 @Argument(index = 1, name = "hostIp", description = "Host IP address",
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020049 required = true, multiValued = false)
50 String hostIp = null;
51
Thomas Vachuska83da9b72016-07-02 12:52:23 +020052 @Argument(index = 2, name = "latitude", description = "Geo latitude",
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020053 required = true, multiValued = false)
54 Double latitude = null;
55
Thomas Vachuska83da9b72016-07-02 12:52:23 +020056 @Argument(index = 3, name = "longitude", description = "Geo longitude",
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020057 required = true, multiValued = false)
58 Double longitude = null;
59
60 @Override
61 protected void execute() {
62 NullProviders service = get(NullProviders.class);
63 NetworkConfigService cfgService = get(NetworkConfigService.class);
64
65 TopologySimulator simulator = service.currentSimulator();
66 if (!(simulator instanceof CustomTopologySimulator)) {
67 error("Custom topology simulator is not active.");
68 return;
69 }
70
71 CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
72 DeviceId deviceId = sim.deviceId(deviceName);
Thomas Vachuska83da9b72016-07-02 12:52:23 +020073 HostId id = sim.nextHostId();
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020074 HostLocation location = findAvailablePort(deviceId);
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020075 BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class);
76 cfg.latitude(latitude);
77 cfg.longitude(longitude);
78 cfg.apply();
79
80 sim.createHost(id, location, IpAddress.valueOf(hostIp));
81 }
82
83 // Finds an available connect point among edge ports of the specified device
84 private HostLocation findAvailablePort(DeviceId deviceId) {
85 EdgePortService eps = get(EdgePortService.class);
86 HostService hs = get(HostService.class);
87 Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator();
88
89 while (points.hasNext()) {
90 ConnectPoint point = points.next();
91 if (hs.getConnectedHosts(point).isEmpty()) {
92 return new HostLocation(point, System.currentTimeMillis());
93 }
94 }
95 return null;
96 }
97
98}