blob: 0bcaa580e7ac0234e5d8104241abf9bfb2692cb0 [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 Huntc0f20c12016-05-09 09:30:20 -070019import org.onosproject.net.DeviceId;
20import org.onosproject.net.HostId;
Simon Hunt23fb1352016-04-11 12:15:19 -070021import org.onosproject.net.region.Region;
Simon Hunt642bc452016-05-04 19:34:45 -070022import org.onosproject.net.region.RegionId;
Simon Hunt23fb1352016-04-11 12:15:19 -070023
Simon Huntc0f20c12016-05-09 09:30:20 -070024import java.util.HashSet;
Simon Hunt23fb1352016-04-11 12:15:19 -070025import java.util.Set;
Simon Huntc0f20c12016-05-09 09:30:20 -070026
27import static com.google.common.base.MoreObjects.toStringHelper;
Simon Hunt23fb1352016-04-11 12:15:19 -070028
Simon Hunt5f6dbf82016-03-30 08:53:33 -070029/**
30 * Represents a region.
31 */
32public class UiRegion extends UiNode {
Simon Hunt23fb1352016-04-11 12:15:19 -070033
Simon Huntc0f20c12016-05-09 09:30:20 -070034 // 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 Hunt23fb1352016-04-11 12:15:19 -070038
Simon Huntc0f20c12016-05-09 09:30:20 -070039 private final UiTopology topology;
Simon Hunt23fb1352016-04-11 12:15:19 -070040
Simon Huntc0f20c12016-05-09 09:30:20 -070041 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 Hunt23fb1352016-04-11 12:15:19 -070053
54 @Override
55 protected void destroy() {
Simon Huntc0f20c12016-05-09 09:30:20 -070056 deviceIds.clear();
57 hostIds.clear();
58 uiLinkIds.clear();
Simon Hunt23fb1352016-04-11 12:15:19 -070059 }
60
Simon Hunt642bc452016-05-04 19:34:45 -070061 /**
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 Huntc0f20c12016-05-09 09:30:20 -070074
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 Hunt5f6dbf82016-03-30 08:53:33 -0700145}