blob: 245b1106fe5aaa95aabfabc3ba8e9bb285989c89 [file] [log] [blame]
Thomas Vachuska4407a7e2016-07-02 12:38:01 +02001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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
48 @Argument(index = 1, name = "hostId", description = "Host identifier",
49 required = true, multiValued = false)
50 String hostId = null;
51
52 @Argument(index = 2, name = "hostIp", description = "Host IP address",
53 required = true, multiValued = false)
54 String hostIp = null;
55
56 @Argument(index = 3, name = "latitude", description = "Geo latitude",
57 required = true, multiValued = false)
58 Double latitude = null;
59
60 @Argument(index = 4, name = "longitude", description = "Geo longitude",
61 required = true, multiValued = false)
62 Double longitude = null;
63
64 @Override
65 protected void execute() {
66 NullProviders service = get(NullProviders.class);
67 NetworkConfigService cfgService = get(NetworkConfigService.class);
68
69 TopologySimulator simulator = service.currentSimulator();
70 if (!(simulator instanceof CustomTopologySimulator)) {
71 error("Custom topology simulator is not active.");
72 return;
73 }
74
75 CustomTopologySimulator sim = (CustomTopologySimulator) simulator;
76 DeviceId deviceId = sim.deviceId(deviceName);
77 HostLocation location = findAvailablePort(deviceId);
78 HostId id = HostId.hostId(hostId);
79 BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class);
80 cfg.latitude(latitude);
81 cfg.longitude(longitude);
82 cfg.apply();
83
84 sim.createHost(id, location, IpAddress.valueOf(hostIp));
85 }
86
87 // Finds an available connect point among edge ports of the specified device
88 private HostLocation findAvailablePort(DeviceId deviceId) {
89 EdgePortService eps = get(EdgePortService.class);
90 HostService hs = get(HostService.class);
91 Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator();
92
93 while (points.hasNext()) {
94 ConnectPoint point = points.next();
95 if (hs.getConnectedHosts(point).isEmpty()) {
96 return new HostLocation(point, System.currentTimeMillis());
97 }
98 }
99 return null;
100 }
101
102}