blob: ed68dfc2eaa4d971534d742f0d159d61584ce889 [file] [log] [blame]
Thomas Vachuska4a315162018-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;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.config.basics.BasicElementConfig;
24import org.onosproject.net.edge.EdgePortService;
25import org.onosproject.net.host.HostService;
26import org.onosproject.provider.nil.CustomTopologySimulator;
27import org.onosproject.provider.nil.TopologySimulator;
28
29import java.util.List;
30import java.util.Objects;
31
32/**
33 * Base command for adding simulated entities to the custom topology simulation.
34 */
35public abstract class CreateNullEntity extends AbstractShellCommand {
36 protected static final String GEO = "geo";
37 protected static final String GRID = "grid";
38
39 /**
40 * Validates that the simulator is custom.
41 *
42 * @param simulator topology simulator
43 * @return true if valid
44 */
45 protected boolean validateSimulator(TopologySimulator simulator) {
46 if (!(simulator instanceof CustomTopologySimulator)) {
47 error("Custom topology simulator is not active.");
48 return false;
49 }
50 return true;
51 }
52
53 /**
54 * Validates that the location type is valid.
55 *
56 * @param locType location type
57 * @return true if valid
58 */
59 protected boolean validateLocType(String locType) {
60 if (!(GEO.equals(locType) || GRID.equals(locType))) {
61 error("locType must be 'geo' or 'grid'.");
62 return false;
63 }
64 return true;
65 }
66
67 /**
68 * Sets the UI location coordinates appropriately.
69 *
70 * @param cfg element config
71 * @param locType location type
72 * @param latOrY latitude or Y grid
73 * @param longOrX longitude or X grid
74 */
75 protected void setUiCoordinates(BasicElementConfig cfg,
76 String locType, Double latOrY, Double longOrX) {
77 if (latOrY != null && longOrX != null) {
78 cfg.locType(locType);
79 if (GEO.equals(locType)) {
80 cfg.latitude(latOrY).longitude(longOrX);
81 } else {
82 cfg.gridX(longOrX).gridY(latOrY);
83 }
84 }
85 cfg.apply();
86 }
87
88 /**
89 * Finds an available connect point among edge ports of the specified device.
90 *
91 * @param deviceId device identifier
92 * @param otherPoint optional other point to be excluded from search
93 * @return connect point available for link or host attachment
94 */
95 protected ConnectPoint findAvailablePort(DeviceId deviceId, ConnectPoint otherPoint) {
96 EdgePortService eps = get(EdgePortService.class);
97 HostService hs = get(HostService.class);
98
99 List<ConnectPoint> points = ImmutableList
100 .sortedCopyOf((l, r) -> Longs.compare(l.port().toLong(), r.port().toLong()),
101 eps.getEdgePoints(deviceId));
102 return points.stream()
103 .filter(p -> !Objects.equals(p, otherPoint) && hs.getConnectedHosts(p).isEmpty())
104 .findFirst().orElse(null);
105 }
106}