Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.provider.nil.cli; |
| 18 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.HostId; |
| 26 | import org.onosproject.net.HostLocation; |
| 27 | import org.onosproject.net.config.NetworkConfigService; |
| 28 | import org.onosproject.net.config.basics.BasicHostConfig; |
| 29 | import org.onosproject.net.edge.EdgePortService; |
| 30 | import org.onosproject.net.host.HostService; |
| 31 | import org.onosproject.provider.nil.CustomTopologySimulator; |
| 32 | import org.onosproject.provider.nil.NullProviders; |
| 33 | import org.onosproject.provider.nil.TopologySimulator; |
| 34 | |
| 35 | import 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") |
| 42 | public 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 Vachuska | 83da9b7 | 2016-07-02 12:52:23 +0200 | [diff] [blame] | 48 | @Argument(index = 1, name = "hostIp", description = "Host IP address", |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 49 | required = true, multiValued = false) |
| 50 | String hostIp = null; |
| 51 | |
Thomas Vachuska | 83da9b7 | 2016-07-02 12:52:23 +0200 | [diff] [blame] | 52 | @Argument(index = 2, name = "latitude", description = "Geo latitude", |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 53 | required = true, multiValued = false) |
| 54 | Double latitude = null; |
| 55 | |
Thomas Vachuska | 83da9b7 | 2016-07-02 12:52:23 +0200 | [diff] [blame] | 56 | @Argument(index = 3, name = "longitude", description = "Geo longitude", |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 57 | 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 Vachuska | 83da9b7 | 2016-07-02 12:52:23 +0200 | [diff] [blame] | 73 | HostId id = sim.nextHostId(); |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 74 | HostLocation location = findAvailablePort(deviceId); |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 75 | 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 | } |