blob: 368fa3db00f9053b3bd168b251ca01c1c4441fe4 [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 {
37 protected static final String GEO = "geo";
38 protected static final String GRID = "grid";
39
40 /**
41 * Validates that the simulator is custom.
42 *
43 * @param simulator topology simulator
44 * @return true if valid
45 */
46 protected boolean validateSimulator(TopologySimulator simulator) {
47 if (!(simulator instanceof CustomTopologySimulator)) {
48 error("Custom topology simulator is not active.");
49 return false;
50 }
51 return true;
52 }
53
54 /**
55 * Validates that the location type is valid.
56 *
57 * @param locType location type
58 * @return true if valid
59 */
60 protected boolean validateLocType(String locType) {
61 if (!(GEO.equals(locType) || GRID.equals(locType))) {
62 error("locType must be 'geo' or 'grid'.");
63 return false;
64 }
65 return true;
66 }
67
68 /**
69 * Sets the UI location coordinates appropriately.
70 *
71 * @param cfg element config
72 * @param locType location type
73 * @param latOrY latitude or Y grid
74 * @param longOrX longitude or X grid
75 */
76 protected void setUiCoordinates(BasicElementConfig cfg,
77 String locType, Double latOrY, Double longOrX) {
78 if (latOrY != null && longOrX != null) {
79 cfg.locType(locType);
80 if (GEO.equals(locType)) {
81 cfg.latitude(latOrY).longitude(longOrX);
82 } else {
83 cfg.gridX(longOrX).gridY(latOrY);
84 }
85 }
86 cfg.apply();
87 }
88
89 /**
90 * Finds an available connect point among edge ports of the specified device.
91 *
92 * @param deviceId device identifier
93 * @param otherPoint optional other point to be excluded from search
94 * @return connect point available for link or host attachment
95 */
96 protected ConnectPoint findAvailablePort(DeviceId deviceId, ConnectPoint otherPoint) {
97 EdgePortService eps = get(EdgePortService.class);
98 HostService hs = get(HostService.class);
99
Thomas Vachuskacab29d22018-02-21 15:47:29 -0800100 // As there may be a slight delay in edge service getting updated, retry a few times
101 for (int i = 0; i < 3; i++) {
102 List<ConnectPoint> points = ImmutableList
103 .sortedCopyOf((l, r) -> Longs.compare(l.port().toLong(), r.port().toLong()),
104 eps.getEdgePoints(deviceId));
105 ConnectPoint point = points.stream()
106 .filter(p -> !Objects.equals(p, otherPoint) && hs.getConnectedHosts(p).isEmpty())
107 .findFirst().orElse(null);
108 if (point != null) {
109 return point;
110 }
111 Tools.delay(100);
112 }
113 return null;
Thomas Vachuskab1906d22018-02-14 16:50:16 -0800114 }
115}