Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +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 | 5c6ed22 | 2016-06-29 11:02:22 +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.apache.karaf.shell.commands.Option; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Link; |
| 26 | import org.onosproject.net.edge.EdgePortService; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 27 | import org.onosproject.net.host.HostService; |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 28 | import org.onosproject.provider.nil.CustomTopologySimulator; |
| 29 | import org.onosproject.provider.nil.NullProviders; |
| 30 | import org.onosproject.provider.nil.TopologySimulator; |
| 31 | |
| 32 | import java.util.Iterator; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 33 | import java.util.Objects; |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Adds a simulated link to the custom topology simulation. |
| 37 | */ |
| 38 | @Command(scope = "onos", name = "null-create-link", |
| 39 | description = "Adds a simulated link to the custom topology simulation") |
| 40 | public class CreateNullLink extends AbstractShellCommand { |
| 41 | |
| 42 | @Argument(index = 0, name = "type", description = "Link type, e.g. direct, indirect, optical", |
| 43 | required = true, multiValued = false) |
| 44 | String type = null; |
| 45 | |
| 46 | @Argument(index = 1, name = "src", description = "Source device name", |
| 47 | required = true, multiValued = false) |
| 48 | String src = null; |
| 49 | |
| 50 | @Argument(index = 2, name = "dst", description = "Destination device name", |
| 51 | required = true, multiValued = false) |
| 52 | String dst = null; |
| 53 | |
| 54 | @Option(name = "-u", aliases = "--unidirectional", description = "Unidirectional link only", |
| 55 | required = false, multiValued = false) |
| 56 | private boolean unidirectional = false; |
| 57 | |
| 58 | @Override |
| 59 | protected void execute() { |
| 60 | NullProviders service = get(NullProviders.class); |
| 61 | |
| 62 | TopologySimulator simulator = service.currentSimulator(); |
| 63 | if (!(simulator instanceof CustomTopologySimulator)) { |
| 64 | error("Custom topology simulator is not active."); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | CustomTopologySimulator sim = (CustomTopologySimulator) simulator; |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 69 | ConnectPoint one = findAvailablePort(sim.deviceId(src), null); |
| 70 | ConnectPoint two = findAvailablePort(sim.deviceId(dst), one); |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 71 | sim.createLink(one, two, Link.Type.valueOf(type.toUpperCase()), !unidirectional); |
| 72 | } |
| 73 | |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 74 | // Finds an available connect point among edge ports of the specified device |
| 75 | private ConnectPoint findAvailablePort(DeviceId deviceId, ConnectPoint otherPoint) { |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 76 | EdgePortService eps = get(EdgePortService.class); |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 77 | HostService hs = get(HostService.class); |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 78 | Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator(); |
Thomas Vachuska | 4407a7e | 2016-07-02 12:38:01 +0200 | [diff] [blame] | 79 | |
| 80 | while (points.hasNext()) { |
| 81 | ConnectPoint point = points.next(); |
| 82 | if (!Objects.equals(point, otherPoint) && hs.getConnectedHosts(point).isEmpty()) { |
| 83 | return point; |
| 84 | } |
| 85 | } |
| 86 | return null; |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } |