Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.provider.nil.cli; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.primitives.Longs; |
Thomas Vachuska | cab29d2 | 2018-02-21 15:47:29 -0800 | [diff] [blame] | 20 | import org.onlab.util.Tools; |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.net.ConnectPoint; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.config.basics.BasicElementConfig; |
| 25 | import org.onosproject.net.edge.EdgePortService; |
| 26 | import org.onosproject.net.host.HostService; |
| 27 | import org.onosproject.provider.nil.CustomTopologySimulator; |
| 28 | import org.onosproject.provider.nil.TopologySimulator; |
| 29 | |
| 30 | import java.util.List; |
| 31 | import java.util.Objects; |
| 32 | |
| 33 | /** |
| 34 | * Base command for adding simulated entities to the custom topology simulation. |
| 35 | */ |
| 36 | public abstract class CreateNullEntity extends AbstractShellCommand { |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 37 | |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 38 | protected static final String GEO = "geo"; |
| 39 | protected static final String GRID = "grid"; |
| 40 | |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 41 | protected static final int MAX_EDGE_PORT_TRIES = 5; |
| 42 | |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 43 | /** |
| 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 Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 100 | HostService hs = get(HostService.class); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 101 | 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 Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 114 | |
Thomas Vachuska | cab29d2 | 2018-02-21 15:47:29 -0800 | [diff] [blame] | 115 | // As there may be a slight delay in edge service getting updated, retry a few times |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 116 | for (int i = 0; i < MAX_EDGE_PORT_TRIES; i++) { |
Thomas Vachuska | cab29d2 | 2018-02-21 15:47:29 -0800 | [diff] [blame] | 117 | List<ConnectPoint> points = ImmutableList |
| 118 | .sortedCopyOf((l, r) -> Longs.compare(l.port().toLong(), r.port().toLong()), |
| 119 | eps.getEdgePoints(deviceId)); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 120 | if (!points.isEmpty()) { |
| 121 | return points; |
Thomas Vachuska | cab29d2 | 2018-02-21 15:47:29 -0800 | [diff] [blame] | 122 | } |
| 123 | Tools.delay(100); |
| 124 | } |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 125 | return ImmutableList.of(); |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 126 | } |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 127 | |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 128 | } |