blob: 1b609cf27349a1e84d874a94f2601999d8ed23fc [file] [log] [blame]
Thomas Vachuska5c6ed222016-06-29 11:02:22 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Thomas Vachuska5c6ed222016-06-29 11:02:22 +02003 *
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;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020027import org.onosproject.net.host.HostService;
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020028import org.onosproject.provider.nil.CustomTopologySimulator;
29import org.onosproject.provider.nil.NullProviders;
30import org.onosproject.provider.nil.TopologySimulator;
31
32import java.util.Iterator;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020033import java.util.Objects;
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020034
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")
40public 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 Vachuska4407a7e2016-07-02 12:38:01 +020069 ConnectPoint one = findAvailablePort(sim.deviceId(src), null);
70 ConnectPoint two = findAvailablePort(sim.deviceId(dst), one);
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020071 sim.createLink(one, two, Link.Type.valueOf(type.toUpperCase()), !unidirectional);
72 }
73
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020074 // Finds an available connect point among edge ports of the specified device
75 private ConnectPoint findAvailablePort(DeviceId deviceId, ConnectPoint otherPoint) {
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020076 EdgePortService eps = get(EdgePortService.class);
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020077 HostService hs = get(HostService.class);
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020078 Iterator<ConnectPoint> points = eps.getEdgePoints(deviceId).iterator();
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020079
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 Vachuska5c6ed222016-06-29 11:02:22 +020087 }
88
89}