Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 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 { |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 43 | private static final String GEO = "geo"; |
| 44 | private static final String GRID = "grid"; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 45 | |
| 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 Vachuska | 83da9b7 | 2016-07-02 12:52:23 +0200 | [diff] [blame] | 50 | @Argument(index = 1, name = "hostIp", description = "Host IP address", |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 51 | required = true, multiValued = false) |
| 52 | String hostIp = null; |
| 53 | |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 54 | @Argument(index = 2, name = "latOrY", |
| 55 | description = "Geo latitude / Grid y-coord", |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 56 | required = true, multiValued = false) |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 57 | Double latOrY = null; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 58 | |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 59 | @Argument(index = 3, name = "longOrX", |
| 60 | description = "Geo longitude / Grid x-coord", |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 61 | required = true, multiValued = false) |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 62 | 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 Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 67 | |
| 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 Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 79 | if (!(GEO.equals(locType) || GRID.equals(locType))) { |
| 80 | error("locType must be 'geo' or 'grid'."); |
| 81 | return; |
| 82 | } |
| 83 | |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 84 | CustomTopologySimulator sim = (CustomTopologySimulator) simulator; |
| 85 | DeviceId deviceId = sim.deviceId(deviceName); |
Thomas Vachuska | 83da9b7 | 2016-07-02 12:52:23 +0200 | [diff] [blame] | 86 | HostId id = sim.nextHostId(); |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 87 | HostLocation location = findAvailablePort(deviceId); |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 88 | BasicHostConfig cfg = cfgService.addConfig(id, BasicHostConfig.class); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 89 | |
| 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 Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 98 | |
| 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 | } |