blob: 237c79d3f195838d318bc30444aaa9c041c3df68 [file] [log] [blame]
Thomas Vachuskab1906d22018-02-14 16:50:16 -08001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.provider.nil.cli;
17
18import com.google.common.collect.ImmutableList;
19import com.google.common.primitives.Longs;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Thomas Vachuskacab29d22018-02-21 15:47:29 -080021import org.onlab.util.Tools;
Thomas Vachuskab1906d22018-02-14 16:50:16 -080022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.config.basics.BasicElementConfig;
26import org.onosproject.net.edge.EdgePortService;
27import org.onosproject.net.host.HostService;
28import org.onosproject.provider.nil.CustomTopologySimulator;
29import org.onosproject.provider.nil.TopologySimulator;
30
31import java.util.List;
32import java.util.Objects;
33
34/**
35 * Base command for adding simulated entities to the custom topology simulation.
36 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070037@Service
Thomas Vachuskab1906d22018-02-14 16:50:16 -080038public abstract class CreateNullEntity extends AbstractShellCommand {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080039
Thomas Vachuskab1906d22018-02-14 16:50:16 -080040 protected static final String GEO = "geo";
41 protected static final String GRID = "grid";
42
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080043 protected static final int MAX_EDGE_PORT_TRIES = 5;
44
Thomas Vachuskab1906d22018-02-14 16:50:16 -080045 /**
46 * Validates that the simulator is custom.
47 *
48 * @param simulator topology simulator
49 * @return true if valid
50 */
51 protected boolean validateSimulator(TopologySimulator simulator) {
52 if (!(simulator instanceof CustomTopologySimulator)) {
53 error("Custom topology simulator is not active.");
54 return false;
55 }
56 return true;
57 }
58
59 /**
60 * Validates that the location type is valid.
61 *
62 * @param locType location type
63 * @return true if valid
64 */
65 protected boolean validateLocType(String locType) {
66 if (!(GEO.equals(locType) || GRID.equals(locType))) {
67 error("locType must be 'geo' or 'grid'.");
68 return false;
69 }
70 return true;
71 }
72
73 /**
74 * Sets the UI location coordinates appropriately.
75 *
76 * @param cfg element config
77 * @param locType location type
78 * @param latOrY latitude or Y grid
79 * @param longOrX longitude or X grid
80 */
81 protected void setUiCoordinates(BasicElementConfig cfg,
82 String locType, Double latOrY, Double longOrX) {
83 if (latOrY != null && longOrX != null) {
84 cfg.locType(locType);
85 if (GEO.equals(locType)) {
86 cfg.latitude(latOrY).longitude(longOrX);
87 } else {
88 cfg.gridX(longOrX).gridY(latOrY);
89 }
90 }
91 cfg.apply();
92 }
93
94 /**
95 * Finds an available connect point among edge ports of the specified device.
96 *
97 * @param deviceId device identifier
98 * @param otherPoint optional other point to be excluded from search
99 * @return connect point available for link or host attachment
100 */
101 protected ConnectPoint findAvailablePort(DeviceId deviceId, ConnectPoint otherPoint) {
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800102 HostService hs = get(HostService.class);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800103 return findAvailablePorts(deviceId).stream()
104 .filter(p -> !Objects.equals(p, otherPoint) && hs.getConnectedHosts(p).isEmpty())
105 .findFirst().orElse(null);
106 }
107
108 /**
109 * Finds an available connect points among edge ports of the specified device.
110 *
111 * @param deviceId device identifier
112 * @return list of connect points available for link or host attachments
113 */
114 protected List<ConnectPoint> findAvailablePorts(DeviceId deviceId) {
115 EdgePortService eps = get(EdgePortService.class);
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800116
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800117 // As there may be a slight delay in edge service getting updated, retry a few times
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800118 for (int i = 0; i < MAX_EDGE_PORT_TRIES; i++) {
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800119 List<ConnectPoint> points = ImmutableList
120 .sortedCopyOf((l, r) -> Longs.compare(l.port().toLong(), r.port().toLong()),
121 eps.getEdgePoints(deviceId));
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800122 if (!points.isEmpty()) {
123 return points;
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800124 }
125 Tools.delay(100);
126 }
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800127 return ImmutableList.of();
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800128 }
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800129
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800130}