Thomas Vachuska | c13e67a | 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; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.config.basics.BasicElementConfig; |
| 24 | import org.onosproject.net.edge.EdgePortService; |
| 25 | import org.onosproject.net.host.HostService; |
| 26 | import org.onosproject.provider.nil.CustomTopologySimulator; |
| 27 | import org.onosproject.provider.nil.TopologySimulator; |
| 28 | |
| 29 | import java.util.List; |
| 30 | import java.util.Objects; |
| 31 | |
| 32 | /** |
| 33 | * Base command for adding simulated entities to the custom topology simulation. |
| 34 | */ |
| 35 | public 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 | } |