blob: 575319d187230bb19a433d278eb5387fdcaa22c1 [file] [log] [blame]
Thomas Vachuska5c6ed222016-06-29 11:02:22 +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.apache.karaf.shell.commands.Option;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Link;
26import org.onosproject.net.edge.EdgePortService;
27import org.onosproject.provider.nil.CustomTopologySimulator;
28import org.onosproject.provider.nil.NullProviders;
29import org.onosproject.provider.nil.TopologySimulator;
30
31import 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")
38public 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}