blob: 023300cfbc798fdebb320e42c40bdd1ab6f1d627 [file] [log] [blame]
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ui.model.topo;
import com.google.common.collect.ImmutableSet;
import org.onosproject.net.DeviceId;
import org.onosproject.net.HostId;
import org.onosproject.net.region.Region;
import org.onosproject.net.region.RegionId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* Represents a region.
*/
public class UiRegion extends UiNode {
// loose bindings to things in this region
private final Set<DeviceId> deviceIds = new HashSet<>();
private final Set<HostId> hostIds = new HashSet<>();
private final Set<UiLinkId> uiLinkIds = new HashSet<>();
private final List<String> layerOrder = new ArrayList<>();
private final UiTopology topology;
private final Region region;
/**
* Constructs a UI region, with a reference to the specified backing region.
*
* @param topology parent topology
* @param region backing region
*/
public UiRegion(UiTopology topology, Region region) {
this.topology = topology;
this.region = region;
// unless told otherwise, we'll use a single, default layer
layerOrder.add(UiNode.LAYER_DEFAULT);
}
@Override
protected void destroy() {
deviceIds.clear();
hostIds.clear();
uiLinkIds.clear();
}
/**
* Sets the layer order for this region.
* Typically, the {@code UiNode.LAYER_*} constants will be used here.
*
* @param layers the layers
*/
public void setLayerOrder(String... layers) {
layerOrder.clear();
Collections.addAll(layerOrder, layers);
}
/**
* Returns the identity of the region.
*
* @return region ID
*/
public RegionId id() {
return region.id();
}
@Override
public String idAsString() {
return id().toString();
}
@Override
public String name() {
return region.name();
}
/**
* Returns the region instance backing this UI region.
*
* @return the backing region instance
*/
public Region backingRegion() {
return region;
}
/**
* Make sure we have only these devices in the region.
*
* @param devices devices in the region
*/
public void reconcileDevices(Set<DeviceId> devices) {
deviceIds.clear();
deviceIds.addAll(devices);
}
@Override
public String toString() {
return toStringHelper(this)
.add("id", id())
.add("name", name())
.add("devices", deviceIds)
.add("#hosts", hostIds.size())
.add("#links", uiLinkIds.size())
.toString();
}
/**
* Returns the region's type.
*
* @return region type
*/
public Region.Type type() {
return region.type();
}
/**
* Returns the set of device identifiers for this region.
*
* @return device identifiers for this region
*/
public Set<DeviceId> deviceIds() {
return ImmutableSet.copyOf(deviceIds);
}
/**
* Returns the devices in this region.
*
* @return the devices in this region
*/
public Set<UiDevice> devices() {
return topology.deviceSet(deviceIds);
}
/**
* Returns the set of host identifiers for this region.
*
* @return host identifiers for this region
*/
public Set<HostId> hostIds() {
return ImmutableSet.copyOf(hostIds);
}
/**
* Returns the hosts in this region.
*
* @return the hosts in this region
*/
public Set<UiHost> hosts() {
return topology.hostSet(hostIds);
}
/**
* Returns the set of link identifiers for this region.
*
* @return link identifiers for this region
*/
public Set<UiLinkId> linkIds() {
return ImmutableSet.copyOf(uiLinkIds);
}
/**
* Returns the links in this region.
*
* @return the links in this region
*/
public Set<UiLink> links() {
return topology.linkSet(uiLinkIds);
}
/**
* Returns the order in which layers should be rendered. Lower layers
* come earlier in the list. For example, to indicate that nodes in the
* optical layer should be rendered "below" nodes in the packet layer,
* this method should return:
* <pre>
* [UiNode.LAYER_OPTICAL, UiNode.LAYER_PACKET]
* </pre>
*
* @return layer ordering
*/
public List<String> layerOrder() {
return Collections.unmodifiableList(layerOrder);
}
}