blob: 97762162e136efea1d6224a6e30b20aa60cfb3ac [file] [log] [blame]
Simon Hunt5f6dbf82016-03-30 08:53:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Simon Hunt5f6dbf82016-03-30 08:53:33 -07003 *
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
17package org.onosproject.ui.model.topo;
18
Simon Hunt58a0dd02016-05-17 11:54:23 -070019import com.google.common.collect.ImmutableSet;
Simon Huntc0f20c12016-05-09 09:30:20 -070020import org.onosproject.net.DeviceId;
21import org.onosproject.net.HostId;
Simon Hunt23fb1352016-04-11 12:15:19 -070022import org.onosproject.net.region.Region;
Simon Hunt642bc452016-05-04 19:34:45 -070023import org.onosproject.net.region.RegionId;
Simon Hunt23fb1352016-04-11 12:15:19 -070024
Simon Huntc0f20c12016-05-09 09:30:20 -070025import java.util.HashSet;
Simon Hunt23fb1352016-04-11 12:15:19 -070026import java.util.Set;
Simon Huntc0f20c12016-05-09 09:30:20 -070027
28import static com.google.common.base.MoreObjects.toStringHelper;
Simon Hunt23fb1352016-04-11 12:15:19 -070029
Simon Hunt5f6dbf82016-03-30 08:53:33 -070030/**
31 * Represents a region.
32 */
33public class UiRegion extends UiNode {
Simon Hunt23fb1352016-04-11 12:15:19 -070034
Simon Huntc0f20c12016-05-09 09:30:20 -070035 // 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 Hunt23fb1352016-04-11 12:15:19 -070039
Simon Huntc0f20c12016-05-09 09:30:20 -070040 private final UiTopology topology;
Simon Hunt23fb1352016-04-11 12:15:19 -070041
Simon Huntc0f20c12016-05-09 09:30:20 -070042 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 Hunt23fb1352016-04-11 12:15:19 -070054
55 @Override
56 protected void destroy() {
Simon Huntc0f20c12016-05-09 09:30:20 -070057 deviceIds.clear();
58 hostIds.clear();
59 uiLinkIds.clear();
Simon Hunt23fb1352016-04-11 12:15:19 -070060 }
61
Simon Hunt642bc452016-05-04 19:34:45 -070062 /**
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 Huntc0f20c12016-05-09 09:30:20 -070075
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 Hunt58a0dd02016-05-17 11:54:23 -0700121 * 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 Huntc0f20c12016-05-09 09:30:20 -0700130 * 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 Hunt58a0dd02016-05-17 11:54:23 -0700139 * 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 Huntc0f20c12016-05-09 09:30:20 -0700148 * 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 Hunt58a0dd02016-05-17 11:54:23 -0700157 * 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 Huntc0f20c12016-05-09 09:30:20 -0700166 * 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 Hunt5f6dbf82016-03-30 08:53:33 -0700173}