Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.ui.model.topo; |
| 18 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.HostId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 22 | import org.onosproject.net.region.Region; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 23 | import org.onosproject.net.region.RegionId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 24 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 25 | import java.util.ArrayList; |
| 26 | import java.util.Collections; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 27 | import java.util.HashSet; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 28 | import java.util.List; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 29 | import java.util.Set; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 30 | |
| 31 | import static com.google.common.base.MoreObjects.toStringHelper; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 32 | |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Represents a region. |
| 35 | */ |
| 36 | public class UiRegion extends UiNode { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 37 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 38 | // loose bindings to things in this region |
| 39 | private final Set<DeviceId> deviceIds = new HashSet<>(); |
| 40 | private final Set<HostId> hostIds = new HashSet<>(); |
| 41 | private final Set<UiLinkId> uiLinkIds = new HashSet<>(); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 42 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 43 | private final List<String> layerOrder = new ArrayList<>(); |
| 44 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 45 | private final UiTopology topology; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 46 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 47 | private final Region region; |
| 48 | |
| 49 | /** |
| 50 | * Constructs a UI region, with a reference to the specified backing region. |
| 51 | * |
| 52 | * @param topology parent topology |
| 53 | * @param region backing region |
| 54 | */ |
| 55 | public UiRegion(UiTopology topology, Region region) { |
| 56 | this.topology = topology; |
| 57 | this.region = region; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 58 | // unless told otherwise, we'll use a single, default layer |
| 59 | layerOrder.add(UiNode.LAYER_DEFAULT); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 60 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 61 | |
| 62 | @Override |
| 63 | protected void destroy() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 64 | deviceIds.clear(); |
| 65 | hostIds.clear(); |
| 66 | uiLinkIds.clear(); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 69 | /** |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 70 | * Sets the layer order for this region. |
| 71 | * Typically, the {@code UiNode.LAYER_*} constants will be used here. |
| 72 | * |
| 73 | * @param layers the layers |
| 74 | */ |
| 75 | public void setLayerOrder(String... layers) { |
| 76 | layerOrder.clear(); |
| 77 | Collections.addAll(layerOrder, layers); |
| 78 | } |
| 79 | |
| 80 | /** |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 81 | * Returns the identity of the region. |
| 82 | * |
| 83 | * @return region ID |
| 84 | */ |
| 85 | public RegionId id() { |
| 86 | return region.id(); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public String idAsString() { |
| 91 | return id().toString(); |
| 92 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 93 | |
| 94 | @Override |
| 95 | public String name() { |
| 96 | return region.name(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns the region instance backing this UI region. |
| 101 | * |
| 102 | * @return the backing region instance |
| 103 | */ |
| 104 | public Region backingRegion() { |
| 105 | return region; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Make sure we have only these devices in the region. |
| 110 | * |
| 111 | * @param devices devices in the region |
| 112 | */ |
| 113 | public void reconcileDevices(Set<DeviceId> devices) { |
| 114 | deviceIds.clear(); |
| 115 | deviceIds.addAll(devices); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public String toString() { |
| 120 | return toStringHelper(this) |
| 121 | .add("id", id()) |
| 122 | .add("name", name()) |
| 123 | .add("devices", deviceIds) |
| 124 | .add("#hosts", hostIds.size()) |
| 125 | .add("#links", uiLinkIds.size()) |
| 126 | .toString(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns the region's type. |
| 131 | * |
| 132 | * @return region type |
| 133 | */ |
| 134 | public Region.Type type() { |
| 135 | return region.type(); |
| 136 | } |
| 137 | |
| 138 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 139 | * Returns the set of device identifiers for this region. |
| 140 | * |
| 141 | * @return device identifiers for this region |
| 142 | */ |
| 143 | public Set<DeviceId> deviceIds() { |
| 144 | return ImmutableSet.copyOf(deviceIds); |
| 145 | } |
| 146 | |
| 147 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 148 | * Returns the devices in this region. |
| 149 | * |
| 150 | * @return the devices in this region |
| 151 | */ |
| 152 | public Set<UiDevice> devices() { |
| 153 | return topology.deviceSet(deviceIds); |
| 154 | } |
| 155 | |
| 156 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 157 | * Returns the set of host identifiers for this region. |
| 158 | * |
| 159 | * @return host identifiers for this region |
| 160 | */ |
| 161 | public Set<HostId> hostIds() { |
| 162 | return ImmutableSet.copyOf(hostIds); |
| 163 | } |
| 164 | |
| 165 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 166 | * Returns the hosts in this region. |
| 167 | * |
| 168 | * @return the hosts in this region |
| 169 | */ |
| 170 | public Set<UiHost> hosts() { |
| 171 | return topology.hostSet(hostIds); |
| 172 | } |
| 173 | |
| 174 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 175 | * Returns the set of link identifiers for this region. |
| 176 | * |
| 177 | * @return link identifiers for this region |
| 178 | */ |
| 179 | public Set<UiLinkId> linkIds() { |
| 180 | return ImmutableSet.copyOf(uiLinkIds); |
| 181 | } |
| 182 | |
| 183 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 184 | * Returns the links in this region. |
| 185 | * |
| 186 | * @return the links in this region |
| 187 | */ |
| 188 | public Set<UiLink> links() { |
| 189 | return topology.linkSet(uiLinkIds); |
| 190 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 191 | |
| 192 | /** |
| 193 | * Returns the order in which layers should be rendered. Lower layers |
| 194 | * come earlier in the list. For example, to indicate that nodes in the |
| 195 | * optical layer should be rendered "below" nodes in the packet layer, |
| 196 | * this method should return: |
| 197 | * <pre> |
| 198 | * [UiNode.LAYER_OPTICAL, UiNode.LAYER_PACKET] |
| 199 | * </pre> |
| 200 | * |
| 201 | * @return layer ordering |
| 202 | */ |
| 203 | public List<String> layerOrder() { |
| 204 | return Collections.unmodifiableList(layerOrder); |
| 205 | } |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 206 | } |