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 | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 25 | import java.util.HashSet; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 26 | import java.util.Set; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 27 | |
| 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 29 | |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Represents a region. |
| 32 | */ |
| 33 | public class UiRegion extends UiNode { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 34 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 35 | // loose bindings to things in this region |
| 36 | private final Set<DeviceId> deviceIds = new HashSet<>(); |
| 37 | private final Set<HostId> hostIds = new HashSet<>(); |
| 38 | private final Set<UiLinkId> uiLinkIds = new HashSet<>(); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 39 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 40 | private final UiTopology topology; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 41 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 42 | private final Region region; |
| 43 | |
| 44 | /** |
| 45 | * Constructs a UI region, with a reference to the specified backing region. |
| 46 | * |
| 47 | * @param topology parent topology |
| 48 | * @param region backing region |
| 49 | */ |
| 50 | public UiRegion(UiTopology topology, Region region) { |
| 51 | this.topology = topology; |
| 52 | this.region = region; |
| 53 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 54 | |
| 55 | @Override |
| 56 | protected void destroy() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 57 | deviceIds.clear(); |
| 58 | hostIds.clear(); |
| 59 | uiLinkIds.clear(); |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Returns the identity of the region. |
| 64 | * |
| 65 | * @return region ID |
| 66 | */ |
| 67 | public RegionId id() { |
| 68 | return region.id(); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public String idAsString() { |
| 73 | return id().toString(); |
| 74 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 75 | |
| 76 | @Override |
| 77 | public String name() { |
| 78 | return region.name(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the region instance backing this UI region. |
| 83 | * |
| 84 | * @return the backing region instance |
| 85 | */ |
| 86 | public Region backingRegion() { |
| 87 | return region; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Make sure we have only these devices in the region. |
| 92 | * |
| 93 | * @param devices devices in the region |
| 94 | */ |
| 95 | public void reconcileDevices(Set<DeviceId> devices) { |
| 96 | deviceIds.clear(); |
| 97 | deviceIds.addAll(devices); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public String toString() { |
| 102 | return toStringHelper(this) |
| 103 | .add("id", id()) |
| 104 | .add("name", name()) |
| 105 | .add("devices", deviceIds) |
| 106 | .add("#hosts", hostIds.size()) |
| 107 | .add("#links", uiLinkIds.size()) |
| 108 | .toString(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns the region's type. |
| 113 | * |
| 114 | * @return region type |
| 115 | */ |
| 116 | public Region.Type type() { |
| 117 | return region.type(); |
| 118 | } |
| 119 | |
| 120 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame^] | 121 | * Returns the set of device identifiers for this region. |
| 122 | * |
| 123 | * @return device identifiers for this region |
| 124 | */ |
| 125 | public Set<DeviceId> deviceIds() { |
| 126 | return ImmutableSet.copyOf(deviceIds); |
| 127 | } |
| 128 | |
| 129 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 130 | * Returns the devices in this region. |
| 131 | * |
| 132 | * @return the devices in this region |
| 133 | */ |
| 134 | public Set<UiDevice> devices() { |
| 135 | return topology.deviceSet(deviceIds); |
| 136 | } |
| 137 | |
| 138 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame^] | 139 | * Returns the set of host identifiers for this region. |
| 140 | * |
| 141 | * @return host identifiers for this region |
| 142 | */ |
| 143 | public Set<HostId> hostIds() { |
| 144 | return ImmutableSet.copyOf(hostIds); |
| 145 | } |
| 146 | |
| 147 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 148 | * Returns the hosts in this region. |
| 149 | * |
| 150 | * @return the hosts in this region |
| 151 | */ |
| 152 | public Set<UiHost> hosts() { |
| 153 | return topology.hostSet(hostIds); |
| 154 | } |
| 155 | |
| 156 | /** |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame^] | 157 | * Returns the set of link identifiers for this region. |
| 158 | * |
| 159 | * @return link identifiers for this region |
| 160 | */ |
| 161 | public Set<UiLinkId> linkIds() { |
| 162 | return ImmutableSet.copyOf(uiLinkIds); |
| 163 | } |
| 164 | |
| 165 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 166 | * Returns the links in this region. |
| 167 | * |
| 168 | * @return the links in this region |
| 169 | */ |
| 170 | public Set<UiLink> links() { |
| 171 | return topology.linkSet(uiLinkIds); |
| 172 | } |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 173 | } |