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