Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +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.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; |
| 27 | import org.onosproject.provider.nil.CustomTopologySimulator; |
| 28 | import org.onosproject.provider.nil.NullProviders; |
| 29 | import org.onosproject.provider.nil.TopologySimulator; |
| 30 | |
| 31 | import java.util.Iterator; |
| 32 | |
| 33 | /** |
| 34 | * Adds a simulated link to the custom topology simulation. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "null-create-link", |
| 37 | description = "Adds a simulated link to the custom topology simulation") |
| 38 | public class CreateNullLink extends AbstractShellCommand { |
| 39 | |
| 40 | @Argument(index = 0, name = "type", description = "Link type, e.g. direct, indirect, optical", |
| 41 | required = true, multiValued = false) |
| 42 | String type = null; |
| 43 | |
| 44 | @Argument(index = 1, name = "src", description = "Source device name", |
| 45 | required = true, multiValued = false) |
| 46 | String src = null; |
| 47 | |
| 48 | @Argument(index = 2, name = "dst", description = "Destination device name", |
| 49 | required = true, multiValued = false) |
| 50 | String dst = null; |
| 51 | |
| 52 | @Option(name = "-u", aliases = "--unidirectional", description = "Unidirectional link only", |
| 53 | required = false, multiValued = false) |
| 54 | private boolean unidirectional = false; |
| 55 | |
| 56 | @Override |
| 57 | protected void execute() { |
| 58 | NullProviders service = get(NullProviders.class); |
| 59 | |
| 60 | TopologySimulator simulator = service.currentSimulator(); |
| 61 | if (!(simulator instanceof CustomTopologySimulator)) { |
| 62 | error("Custom topology simulator is not active."); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | CustomTopologySimulator sim = (CustomTopologySimulator) simulator; |
| 67 | ConnectPoint one = findAvailablePort(sim.deviceId(src)); |
| 68 | ConnectPoint two = findAvailablePort(sim.deviceId(dst)); |
| 69 | sim.createLink(one, two, Link.Type.valueOf(type.toUpperCase()), !unidirectional); |
| 70 | } |
| 71 | |
| 72 | private ConnectPoint findAvailablePort(DeviceId deviceId) { |
| 73 | EdgePortService eps = get(EdgePortService.class); |
| 74 | Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator(); |
| 75 | return points.hasNext() ? points.next() : null; |
| 76 | } |
| 77 | |
| 78 | } |