blob: a9efa78cd5517ffa39a46c887fb717a8935e863a [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;
Thomas Vachuskacab29d22018-02-21 15:47:29 -080020import org.onlab.util.Tools;
Thomas Vachuskab1906d22018-02-14 16:50:16 -080021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.config.basics.BasicElementConfig;
25import org.onosproject.net.edge.EdgePortService;
26import org.onosproject.net.host.HostService;
27import org.onosproject.provider.nil.CustomTopologySimulator;
28import org.onosproject.provider.nil.TopologySimulator;
29
30import java.util.List;
31import java.util.Objects;
32
33/**
34 * Base command for adding simulated entities to the custom topology simulation.
35 */
36public abstract class CreateNullEntity extends AbstractShellCommand {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080037
Thomas Vachuskab1906d22018-02-14 16:50:16 -080038 protected static final String GEO = "geo";
39 protected static final String GRID = "grid";
40
Thomas Vachuskaa8e74772018-02-26 11:33:35 -080041 protected static final int MAX_EDGE_PORT_TRIES = 5;
42
Thomas Vachuskab1906d22018-02-14 16:50:16 -080043 /**
44 * Validates that the simulator is custom.
45 *
46 * @param simulator topology simulator
47 * @return true if valid
48 */
49 protected boolean validateSimulator(TopologySimulator simulator) {
50 if (!(simulator instanceof CustomTopologySimulator)) {
51 error("Custom topology simulator is not active.");
52 return false;
53 }
54 return true;
55 }
56
57 /**
58 * Validates that the location type is valid.
59 *
60 * @param locType location type
61 * @return true if valid
62 */
63 protected boolean validateLocType(String locType) {
64 if (!(GEO.equals(locType) || GRID.equals(locType))) {
65 error("locType must be 'geo' or 'grid'.");
66 return false;
67 }
68 return true;
69 }
70
71 /**
72 * Sets the UI location coordinates appropriately.
73 *
74 * @param cfg element config
75 * @param locType location type
76 * @param latOrY latitude or Y grid
77 * @param longOrX longitude or X grid
78 */
79 protected void setUiCoordinates(BasicElementConfig cfg,
80 String locType, Double latOrY, Double longOrX) {
81 if (latOrY != null && longOrX != null) {
82 cfg.locType(locType);
83 if (GEO.equals(locType)) {
84 cfg.latitude(latOrY).longitude(longOrX);
85 } else {
86 cfg.gridX(longOrX).gridY(latOrY);
87 }
88 }
89 cfg.apply();
90 }
91
92 /**
93 * Finds an available connect point among edge ports of the specified device.
94 *
95 * @param deviceId device identifier
96 * @param otherPoint optional other point to be excluded from search
97 * @return connect point available for link or host attachment
98 */
99 protected ConnectPoint findAvailablePort(DeviceId deviceId, ConnectPoint otherPoint) {
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800100 HostService hs = get(HostService.class);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800101 return findAvailablePorts(deviceId).stream()
102 .filter(p -> !Objects.equals(p, otherPoint) && hs.getConnectedHosts(p).isEmpty())
103 .findFirst().orElse(null);
104 }
105
106 /**
107 * Finds an available connect points among edge ports of the specified device.
108 *
109 * @param deviceId device identifier
110 * @return list of connect points available for link or host attachments
111 */
112 protected List<ConnectPoint> findAvailablePorts(DeviceId deviceId) {
113 EdgePortService eps = get(EdgePortService.class);
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800114
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800115 // As there may be a slight delay in edge service getting updated, retry a few times
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800116 for (int i = 0; i < MAX_EDGE_PORT_TRIES; i++) {
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800117 List<ConnectPoint> points = ImmutableList
118 .sortedCopyOf((l, r) -> Longs.compare(l.port().toLong(), r.port().toLong()),
119 eps.getEdgePoints(deviceId));
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800120 if (!points.isEmpty()) {
121 return points;
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800122 }
123 Tools.delay(100);
124 }
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800125 return ImmutableList.of();
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800126 }
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800127
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800128}