Simon Hunt

Implemented initial loading of ModelCache.

Created UiLinkId to canonicalize identifiers for UI links, based on src and dst elements.
Added idAsString() and name() methods to UiElement.
Added toString() to UiDevice, UiLink, UiHost.
Created Mock services for testing.

Change-Id: I4d27110e5aca08f29bb719f17e9ec65d6786e2c8
/*
* 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 org.onosproject.cluster.NodeId;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Encapsulates the notion of the ONOS cluster.
*/
class UiCluster extends UiElement {
private static final String DEFAULT_CLUSTER_ID = "CLUSTER-0";
private final List<UiClusterMember> members = new ArrayList<>();
private final Map<NodeId, UiClusterMember> lookup = new HashMap<>();
@Override
public String toString() {
return String.valueOf(size()) + "-member cluster";
}
/**
* Removes all cluster members.
*/
void clear() {
members.clear();
}
/**
* Returns the cluster member with the given identifier, or null if no
* such member exists.
*
* @param id identifier of member to find
* @return corresponding member
*/
public UiClusterMember find(NodeId id) {
return lookup.get(id);
}
/**
* Adds the given member to the cluster.
*
* @param member member to add
*/
public void add(UiClusterMember member) {
members.add(member);
lookup.put(member.id(), member);
}
/**
* Removes the given member from the cluster.
*
* @param member member to remove
*/
public void remove(UiClusterMember member) {
members.remove(member);
lookup.remove(member.id());
}
/**
* Returns the number of members in the cluster.
*
* @return number of members
*/
public int size() {
return members.size();
}
@Override
public String idAsString() {
return DEFAULT_CLUSTER_ID;
}
}
......@@ -19,6 +19,11 @@ package org.onosproject.ui.model.topo;
import org.onlab.packet.IpAddress;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.NodeId;
import org.onosproject.net.DeviceId;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import static org.onosproject.cluster.ControllerNode.State.INACTIVE;
......@@ -27,18 +32,21 @@ import static org.onosproject.cluster.ControllerNode.State.INACTIVE;
*/
public class UiClusterMember extends UiElement {
private final UiTopology topology;
private final ControllerNode cnode;
private int deviceCount = 0;
private ControllerNode.State state = INACTIVE;
private final Set<DeviceId> mastership = new HashSet<>();
/**
* Constructs a cluster member, with a reference to the specified
* controller node instance.
* Constructs a UI cluster member, with a reference to the parent
* topology instance and the specified controller node instance.
*
* @param cnode underlying controller node.
* @param topology parent topology containing this cluster member
* @param cnode underlying controller node.
*/
public UiClusterMember(ControllerNode cnode) {
public UiClusterMember(UiTopology topology, ControllerNode cnode) {
this.topology = topology;
this.cnode = cnode;
}
......@@ -47,10 +55,23 @@ public class UiClusterMember extends UiElement {
return "UiClusterMember{" + cnode +
", online=" + isOnline() +
", ready=" + isReady() +
", #devices=" + deviceCount +
", #devices=" + deviceCount() +
"}";
}
@Override
public String idAsString() {
return id().toString();
}
/**
* Returns the controller node instance backing this UI cluster member.
*
* @return the backing controller node instance
*/
public ControllerNode backingNode() {
return cnode;
}
/**
* Sets the state of this cluster member.
......@@ -61,14 +82,15 @@ public class UiClusterMember extends UiElement {
this.state = state;
}
/**
* Sets the number of devices for which this cluster member is master.
* Sets the collection of identities of devices for which this
* controller node is master.
*
* @param deviceCount number of devices
* @param mastership device IDs
*/
public void setDeviceCount(int deviceCount) {
this.deviceCount = deviceCount;
public void setMastership(Set<DeviceId> mastership) {
this.mastership.clear();
this.mastership.addAll(mastership);
}
/**
......@@ -113,11 +135,26 @@ public class UiClusterMember extends UiElement {
* @return number of devices for which this member is master
*/
public int deviceCount() {
return deviceCount;
return mastership.size();
}
@Override
public String idAsString() {
return id().toString();
/**
* Returns the list of devices for which this cluster member is master.
*
* @return list of devices for which this member is master
*/
public Set<DeviceId> masterOf() {
return Collections.unmodifiableSet(mastership);
}
/**
* Returns true if the specified device is one for which this cluster
* member is master.
*
* @param deviceId device ID
* @return true if this cluster member is master for the given device
*/
public boolean masterOf(DeviceId deviceId) {
return mastership.contains(deviceId);
}
}
......
......@@ -16,21 +16,53 @@
package org.onosproject.ui.model.topo;
import com.google.common.base.MoreObjects;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.region.RegionId;
/**
* Represents a device.
*/
public class UiDevice extends UiNode {
private Device device;
private final UiTopology topology;
private final Device device;
private RegionId regionId;
/**
* Creates a new UI device.
*
* @param topology parent topology
* @param device backing device
*/
public UiDevice(UiTopology topology, Device device) {
this.topology = topology;
this.device = device;
}
/**
* Sets the ID of the region to which this device belongs.
*
* @param regionId region identifier
*/
public void setRegionId(RegionId regionId) {
this.regionId = regionId;
}
@Override
protected void destroy() {
device = null;
public String toString() {
return MoreObjects.toStringHelper(this)
.add("id", id())
.add("region", regionId)
.toString();
}
// @Override
// protected void destroy() {
// }
/**
* Returns the identity of the device.
*
......@@ -44,4 +76,22 @@ public class UiDevice extends UiNode {
public String idAsString() {
return id().toString();
}
/**
* Returns the device instance backing this UI device.
*
* @return the backing device instance
*/
public Device backingDevice() {
return device;
}
/**
* Returns the UI region to which this device belongs.
*
* @return the UI region
*/
public UiRegion uiRegion() {
return topology.findRegion(regionId);
}
}
......
......@@ -35,4 +35,15 @@ public abstract class UiElement {
* @return the element unique identifier
*/
public abstract String idAsString();
/**
* Returns a friendly name to be used for display purposes.
* This default implementation returns the result of calling
* {@link #idAsString()}.
*
* @return the friendly name
*/
public String name() {
return idAsString();
}
}
......
......@@ -16,19 +16,49 @@
package org.onosproject.ui.model.topo;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Host;
import org.onosproject.net.HostId;
import org.onosproject.net.PortNumber;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* Represents an end-station host.
*/
public class UiHost extends UiNode {
private Host host;
private final UiTopology topology;
private final Host host;
// Host location
private DeviceId locDevice;
private PortNumber locPort;
private UiLinkId edgeLinkId;
/**
* Creates a new UI host.
*
* @param topology parent topology
* @param host backing host
*/
public UiHost(UiTopology topology, Host host) {
this.topology = topology;
this.host = host;
}
// @Override
// protected void destroy() {
// }
@Override
protected void destroy() {
host = null;
public String toString() {
return toStringHelper(this)
.add("id", id())
.add("dev", locDevice)
.add("port", locPort)
.toString();
}
/**
......@@ -44,4 +74,44 @@ public class UiHost extends UiNode {
public String idAsString() {
return id().toString();
}
/**
* Sets the host's current location.
*
* @param deviceId ID of device
* @param port port number
*/
public void setLocation(DeviceId deviceId, PortNumber port) {
locDevice = deviceId;
locPort = port;
}
/**
* Sets the ID of the edge link between this host and the device to which
* it connects.
*
* @param id edge link identifier to set
*/
public void setEdgeLinkId(UiLinkId id) {
this.edgeLinkId = id;
}
/**
* Returns the host instance backing this UI host.
*
* @return the backing host instance
*/
public Host backingHost() {
return host;
}
/**
* Identifier for the edge link between this host and the device to which
* it is connected.
*
* @return edge link identifier
*/
public UiLinkId edgeLinkId() {
return null;
}
}
......
/*
* 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;
/**
* Designates the logical layer of the network that an element belongs to.
*/
public enum UiLayer {
PACKET, OPTICAL;
/**
* Returns the default layer (for those elements that do not explicitly
* define which layer they belong to).
*
* @return default layer
*/
public static UiLayer defaultLayer() {
return PACKET;
}
}
......@@ -16,26 +16,61 @@
package org.onosproject.ui.model.topo;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.EdgeLink;
import org.onosproject.net.Link;
import java.util.Set;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* Represents a bi-directional link backed by two uni-directional links.
* Represents a link (line between two elements). This may have one of
* several forms:
* <ul>
* <li>
* An infrastructure link:
* two backing unidirectional links between two devices.
* </li>
* <li>
* An edge link:
* representing the connection between a host and a device.
* </li>
* <li>
* An aggregation link:
* representing multiple underlying UI link instances.
* </li>
* </ul>
*/
public class UiLink extends UiElement {
private static final String E_UNASSOC =
"backing link not associated with this UI link: ";
private final UiTopology topology;
private final UiLinkId id;
/**
* Creates a UI link.
*
* @param topology parent topology
* @param id canonicalized link identifier
*/
public UiLink(UiTopology topology, UiLinkId id) {
this.topology = topology;
this.id = id;
}
// devices at either end of this link
private Device deviceA;
private Device deviceB;
private DeviceId deviceA;
private DeviceId deviceB;
// two unidirectional links underlying this link...
private Link linkAtoB;
private Link linkBtoA;
// ==OR== : private (synthetic) host link
private DeviceId edgeDevice;
private EdgeLink edgeLink;
// ==OR== : set of underlying UI links that this link aggregates
......@@ -43,6 +78,13 @@ public class UiLink extends UiElement {
@Override
public String toString() {
return toStringHelper(this)
.add("id", id)
.toString();
}
@Override
protected void destroy() {
deviceA = null;
deviceB = null;
......@@ -55,9 +97,84 @@ public class UiLink extends UiElement {
}
}
/**
* Returns the canonicalized link identifier for this link.
*
* @return the link identifier
*/
public UiLinkId id() {
return id;
}
@Override
public String idAsString() {
// TODO
return null;
return id.toString();
}
/**
* Attaches the given backing link to this UI link. This method will
* throw an exception if this UI link is not representative of the
* supplied link.
*
* @param link backing link to attach
* @throws IllegalArgumentException if the link is not appropriate
*/
public void attachBackingLink(Link link) {
UiLinkId.Direction d = id.directionOf(link);
if (d == UiLinkId.Direction.A_TO_B) {
linkAtoB = link;
deviceA = link.src().deviceId();
deviceB = link.dst().deviceId();
} else if (d == UiLinkId.Direction.B_TO_A) {
linkBtoA = link;
deviceB = link.src().deviceId();
deviceA = link.dst().deviceId();
} else {
throw new IllegalArgumentException(E_UNASSOC + link);
}
}
/**
* Detaches the given backing link from this UI link, returning true if the
* reverse link is still attached, or false otherwise.
*
* @param link the backing link to detach
* @return true if other link still attached, false otherwise
* @throws IllegalArgumentException if the link is not appropriate
*/
public boolean detachBackingLink(Link link) {
UiLinkId.Direction d = id.directionOf(link);
if (d == UiLinkId.Direction.A_TO_B) {
linkAtoB = null;
return linkBtoA != null;
}
if (d == UiLinkId.Direction.B_TO_A) {
linkBtoA = null;
return linkAtoB != null;
}
throw new IllegalArgumentException(E_UNASSOC + link);
}
/**
* Attaches the given edge link to this UI link. This method will
* throw an exception if this UI link is not representative of the
* supplied link.
*
* @param elink edge link to attach
* @throws IllegalArgumentException if the link is not appropriate
*/
public void attachEdgeLink(EdgeLink elink) {
UiLinkId.Direction d = id.directionOf(elink);
// Expected direction of edge links is A-to-B (Host to device)
// but checking not null is sufficient
if (d == null) {
throw new IllegalArgumentException(E_UNASSOC + elink);
}
edgeLink = elink;
edgeDevice = elink.hostLocation().deviceId();
}
}
......
/*
* 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 org.onosproject.net.ConnectPoint;
import org.onosproject.net.ElementId;
import org.onosproject.net.Link;
/**
* A canonical representation of an identifier for {@link UiLink}s.
*/
public final class UiLinkId {
/**
* Designates the directionality of an underlying (uni-directional) link.
*/
public enum Direction {
A_TO_B,
B_TO_A
}
private static final String ID_DELIMITER = "~";
private final ElementId idA;
private final ElementId idB;
private final String idStr;
/**
* Creates a UI link identifier. It is expected that A comes before B when
* the two identifiers are naturally sorted, thus providing a representation
* which is invariant to whether A or B is source or destination of the
* underlying link.
*
* @param a first element ID
* @param b second element ID
*/
private UiLinkId(ElementId a, ElementId b) {
idA = a;
idB = b;
idStr = a.toString() + ID_DELIMITER + b.toString();
}
@Override
public String toString() {
return idStr;
}
/**
* Returns the identifier of the first element.
*
* @return first element identity
*/
public ElementId elementA() {
return idA;
}
/**
* Returns the identifier of the second element.
*
* @return second element identity
*/
public ElementId elementB() {
return idB;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
UiLinkId uiLinkId = (UiLinkId) o;
return idStr.equals(uiLinkId.idStr);
}
@Override
public int hashCode() {
return idStr.hashCode();
}
/**
* Returns the direction of the given link, or null if this link ID does
* not correspond to the given link.
*
* @param link the link to examine
* @return corresponding direction
*/
Direction directionOf(Link link) {
ConnectPoint src = link.src();
ElementId srcId = src.elementId();
return idA.equals(srcId) ? Direction.A_TO_B
: idB.equals(srcId) ? Direction.B_TO_A
: null;
}
/**
* Generates the canonical link identifier for the given link.
*
* @param link link for which the identifier is required
* @return link identifier
* @throws NullPointerException if any of the required fields are null
*/
public static UiLinkId uiLinkId(Link link) {
ConnectPoint src = link.src();
ConnectPoint dst = link.dst();
if (src == null || dst == null) {
throw new NullPointerException("null src or dst connect point: " + link);
}
ElementId srcId = src.elementId();
ElementId dstId = dst.elementId();
if (srcId == null || dstId == null) {
throw new NullPointerException("null element ID in connect point: " + link);
}
// canonicalize
int comp = srcId.toString().compareTo(dstId.toString());
return comp <= 0 ? new UiLinkId(srcId, dstId)
: new UiLinkId(dstId, srcId);
}
}
......@@ -16,35 +16,46 @@
package org.onosproject.ui.model.topo;
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.HashSet;
import java.util.Set;
import java.util.TreeSet;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* Represents a region.
*/
public class UiRegion extends UiNode {
private final Set<UiDevice> uiDevices = new TreeSet<>();
private final Set<UiHost> uiHosts = new TreeSet<>();
private final Set<UiLink> uiLinks = new TreeSet<>();
// 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 UiTopology topology;
private Region region;
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;
}
@Override
protected void destroy() {
uiDevices.forEach(UiDevice::destroy);
uiHosts.forEach(UiHost::destroy);
uiLinks.forEach(UiLink::destroy);
uiDevices.clear();
uiHosts.clear();
uiLinks.clear();
region = null;
deviceIds.clear();
hostIds.clear();
uiLinkIds.clear();
}
/**
......@@ -60,4 +71,75 @@ public class UiRegion extends UiNode {
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 devices in this region.
*
* @return the devices in this region
*/
public Set<UiDevice> devices() {
return topology.deviceSet(deviceIds);
}
/**
* Returns the hosts in this region.
*
* @return the hosts in this region
*/
public Set<UiHost> hosts() {
return topology.hostSet(hostIds);
}
/**
* Returns the links in this region.
*
* @return the links in this region
*/
public Set<UiLink> links() {
return topology.linkSet(uiLinkIds);
}
}
......
......@@ -17,27 +17,54 @@
package org.onosproject.ui.model.topo;
import org.onosproject.cluster.NodeId;
import org.onosproject.net.DeviceId;
import org.onosproject.net.HostId;
import org.onosproject.net.region.RegionId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* Represents the overall network topology.
*/
public class UiTopology extends UiElement {
private static final String E_UNMAPPED =
"Attempting to retrieve unmapped {}: {}";
private static final String DEFAULT_TOPOLOGY_ID = "TOPOLOGY-0";
private static final Logger log = LoggerFactory.getLogger(UiTopology.class);
private final UiCluster uiCluster = new UiCluster();
private final Set<UiRegion> uiRegions = new TreeSet<>();
// top level mappings of topology elements by ID
private final Map<NodeId, UiClusterMember> cnodeLookup = new HashMap<>();
private final Map<RegionId, UiRegion> regionLookup = new HashMap<>();
private final Map<DeviceId, UiDevice> deviceLookup = new HashMap<>();
private final Map<HostId, UiHost> hostLookup = new HashMap<>();
private final Map<UiLinkId, UiLink> linkLookup = new HashMap<>();
@Override
public String toString() {
return "Topology: " + uiCluster + ", " + uiRegions.size() + " regions";
return toStringHelper(this)
.add("#cnodes", clusterMemberCount())
.add("#regions", regionCount())
.add("#devices", deviceLookup.size())
.add("#hosts", hostLookup.size())
.add("#links", linkLookup.size())
.toString();
}
@Override
public String idAsString() {
return DEFAULT_TOPOLOGY_ID;
}
/**
......@@ -46,19 +73,22 @@ public class UiTopology extends UiElement {
*/
public void clear() {
log.debug("clearing topology model");
uiRegions.clear();
uiCluster.clear();
cnodeLookup.clear();
regionLookup.clear();
deviceLookup.clear();
hostLookup.clear();
linkLookup.clear();
}
/**
* Returns the cluster member with the given identifier, or null if no
* such member.
* such member exists.
*
* @param id cluster node identifier
* @return the cluster member with that identifier
* @return corresponding UI cluster member
*/
public UiClusterMember findClusterMember(NodeId id) {
return uiCluster.find(id);
return cnodeLookup.get(id);
}
/**
......@@ -67,7 +97,7 @@ public class UiTopology extends UiElement {
* @param member cluster member to add
*/
public void add(UiClusterMember member) {
uiCluster.add(member);
cnodeLookup.put(member.id(), member);
}
/**
......@@ -76,7 +106,10 @@ public class UiTopology extends UiElement {
* @param member cluster member to remove
*/
public void remove(UiClusterMember member) {
uiCluster.remove(member);
UiClusterMember m = cnodeLookup.remove(member.id());
if (m != null) {
m.destroy();
}
}
/**
......@@ -85,7 +118,18 @@ public class UiTopology extends UiElement {
* @return number of cluster members
*/
public int clusterMemberCount() {
return uiCluster.size();
return cnodeLookup.size();
}
/**
* Returns the region with the specified identifier, or null if
* no such region exists.
*
* @param id region identifier
* @return corresponding UI region
*/
public UiRegion findRegion(RegionId id) {
return regionLookup.get(id);
}
/**
......@@ -94,11 +138,182 @@ public class UiTopology extends UiElement {
* @return number of regions
*/
public int regionCount() {
return uiRegions.size();
return regionLookup.size();
}
@Override
public String idAsString() {
return DEFAULT_TOPOLOGY_ID;
/**
* Adds the given region to the topology model.
*
* @param uiRegion region to add
*/
public void add(UiRegion uiRegion) {
regionLookup.put(uiRegion.id(), uiRegion);
}
/**
* Removes the given region from the topology model.
*
* @param uiRegion region to remove
*/
public void remove(UiRegion uiRegion) {
regionLookup.remove(uiRegion.id());
}
/**
* Returns the device with the specified identifier, or null if
* no such device exists.
*
* @param id device identifier
* @return corresponding UI device
*/
public UiDevice findDevice(DeviceId id) {
return deviceLookup.get(id);
}
/**
* Adds the given device to the topology model.
*
* @param uiDevice device to add
*/
public void add(UiDevice uiDevice) {
deviceLookup.put(uiDevice.id(), uiDevice);
}
/**
* Removes the given device from the topology model.
*
* @param uiDevice device to remove
*/
public void remove(UiDevice uiDevice) {
UiDevice d = deviceLookup.remove(uiDevice.id());
if (d != null) {
d.destroy();
}
}
/**
* Returns the link with the specified identifier, or null if no such
* link exists.
*
* @param id the canonicalized link identifier
* @return corresponding UI link
*/
public UiLink findLink(UiLinkId id) {
return linkLookup.get(id);
}
/**
* Adds the given UI link to the topology model.
*
* @param uiLink link to add
*/
public void add(UiLink uiLink) {
linkLookup.put(uiLink.id(), uiLink);
}
/**
* Removes the given UI link from the model.
*
* @param uiLink link to remove
*/
public void remove(UiLink uiLink) {
UiLink link = linkLookup.get(uiLink.id());
if (link != null) {
link.destroy();
}
}
/**
* Returns the host with the specified identifier, or null if no such
* host exists.
*
* @param id host identifier
* @return corresponding UI host
*/
public UiHost findHost(HostId id) {
return hostLookup.get(id);
}
/**
* Adds the given host to the topology model.
*
* @param uiHost host to add
*/
public void add(UiHost uiHost) {
hostLookup.put(uiHost.id(), uiHost);
}
/**
* Removes the given host from the topology model.
*
* @param uiHost host to remove
*/
public void remove(UiHost uiHost) {
UiHost h = hostLookup.remove(uiHost.id());
if (h != null) {
h.destroy();
}
}
// ==
// package private methods for supporting linkage amongst topology entities
// ==
/**
* Returns the set of UI devices with the given identifiers.
*
* @param deviceIds device identifiers
* @return set of matching UI device instances
*/
Set<UiDevice> deviceSet(Set<DeviceId> deviceIds) {
Set<UiDevice> uiDevices = new HashSet<>();
for (DeviceId id : deviceIds) {
UiDevice d = deviceLookup.get(id);
if (d != null) {
uiDevices.add(d);
} else {
log.warn(E_UNMAPPED, "device", id);
}
}
return uiDevices;
}
/**
* Returns the set of UI hosts with the given identifiers.
*
* @param hostIds host identifiers
* @return set of matching UI host instances
*/
Set<UiHost> hostSet(Set<HostId> hostIds) {
Set<UiHost> uiHosts = new HashSet<>();
for (HostId id : hostIds) {
UiHost h = hostLookup.get(id);
if (h != null) {
uiHosts.add(h);
} else {
log.warn(E_UNMAPPED, "host", id);
}
}
return uiHosts;
}
/**
* Returns the set of UI links with the given identifiers.
*
* @param uiLinkIds link identifiers
* @return set of matching UI link instances
*/
Set<UiLink> linkSet(Set<UiLinkId> uiLinkIds) {
Set<UiLink> uiLinks = new HashSet<>();
for (UiLinkId id : uiLinkIds) {
UiLink link = linkLookup.get(id);
if (link != null) {
uiLinks.add(link);
} else {
log.warn(E_UNMAPPED, "link", id);
}
}
return uiLinks;
}
}
......
......@@ -16,6 +16,7 @@
package org.onosproject.ui.model.topo;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import org.onosproject.cluster.ControllerNode;
......@@ -36,12 +37,18 @@ public class UiClusterMemberTest extends AbstractUiModelTest {
private static final ControllerNode CNODE_1 =
new DefaultControllerNode(NODE_ID, NODE_IP);
private UiTopology topo;
private UiClusterMember member;
@Before
public void setUp() {
topo = new UiTopology();
}
@Test
public void basic() {
title("basic");
member = new UiClusterMember(CNODE_1);
member = new UiClusterMember(topo, CNODE_1);
print(member);
assertEquals("wrong id", NODE_ID, member.id());
......
/*
* 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 org.junit.Test;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DefaultLink;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
import org.onosproject.net.PortNumber;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.ui.model.AbstractUiModelTest;
import static org.junit.Assert.assertEquals;
import static org.onosproject.net.DeviceId.deviceId;
import static org.onosproject.net.PortNumber.portNumber;
/**
* Unit tests for {@link UiLinkId}.
*/
public class UiLinkIdTest extends AbstractUiModelTest {
private static final ProviderId PROVIDER_ID = ProviderId.NONE;
private static final DeviceId DEV_X = deviceId("device-X");
private static final DeviceId DEV_Y = deviceId("device-Y");
private static final PortNumber P1 = portNumber(1);
private static final PortNumber P2 = portNumber(2);
private static final ConnectPoint CP_X = new ConnectPoint(DEV_X, P1);
private static final ConnectPoint CP_Y = new ConnectPoint(DEV_Y, P2);
private static final Link LINK_X_TO_Y = DefaultLink.builder()
.providerId(ProviderId.NONE)
.src(CP_X)
.dst(CP_Y)
.type(Link.Type.DIRECT)
.build();
private static final Link LINK_Y_TO_X = DefaultLink.builder()
.providerId(ProviderId.NONE)
.src(CP_Y)
.dst(CP_X)
.type(Link.Type.DIRECT)
.build();
@Test
public void canonical() {
title("canonical");
UiLinkId one = UiLinkId.uiLinkId(LINK_X_TO_Y);
UiLinkId two = UiLinkId.uiLinkId(LINK_Y_TO_X);
print("link one: %s", one);
print("link two: %s", two);
assertEquals("not equiv", one, two);
}
}
/*
* 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 org.junit.Test;
import org.onosproject.ui.AbstractUiTest;
/**
* Unit tests for {@link UiTopology}.
*/
public class UiTopologyTest extends AbstractUiTest {
private UiTopology topo;
@Test
public void basic() {
title("basic");
topo = new UiTopology();
print(topo);
}
}
......@@ -32,9 +32,17 @@ public class UiModelEvent extends AbstractEvent<UiModelEvent.Type, UiElement> {
CLUSTER_MEMBER_ADDED_OR_UPDATED,
CLUSTER_MEMBER_REMOVED,
REGION_ADDED_OR_UPDATED,
REGION_REMOVED,
DEVICE_ADDED_OR_UPDATED,
DEVICE_REMOVED,
// TODO...
LINK_ADDED_OR_UPDATED,
LINK_REMOVED,
HOST_ADDED_OR_UPDATED,
HOST_MOVED,
HOST_REMOVED
}
}
......
......@@ -18,16 +18,17 @@ package org.onosproject.ui.impl.topo.model;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.DefaultControllerNode;
import org.onosproject.event.Event;
import org.onosproject.event.EventDispatcher;
import org.onosproject.net.DeviceId;
import org.onosproject.ui.impl.topo.model.UiModelEvent.Type;
import org.onosproject.ui.model.topo.UiClusterMember;
import org.onosproject.ui.model.topo.UiElement;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.onosproject.cluster.NodeId.nodeId;
/**
......@@ -58,22 +59,6 @@ public class ModelCacheTest extends AbstractTopoModelTest {
}
}
private static IpAddress ip(String s) {
return IpAddress.valueOf(s);
}
private static ControllerNode cnode(String id, String ip) {
return new DefaultControllerNode(nodeId(id), ip(ip));
}
private static final String C1 = "C1";
private static final String C2 = "C2";
private static final String C3 = "C3";
private static final ControllerNode NODE_1 = cnode(C1, "10.0.0.1");
private static final ControllerNode NODE_2 = cnode(C2, "10.0.0.2");
private static final ControllerNode NODE_3 = cnode(C3, "10.0.0.3");
private final TestEvDisp dispatcher = new TestEvDisp();
......@@ -99,13 +84,13 @@ public class ModelCacheTest extends AbstractTopoModelTest {
assertEquals("unex # members", 0, cache.clusterMemberCount());
dispatcher.assertEventCount(0);
cache.addOrUpdateClusterMember(NODE_1);
cache.addOrUpdateClusterMember(CNODE_1);
print(cache);
assertEquals("unex # members", 1, cache.clusterMemberCount());
dispatcher.assertEventCount(1);
dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1);
cache.removeClusterMember(NODE_1);
cache.removeClusterMember(CNODE_1);
print(cache);
assertEquals("unex # members", 0, cache.clusterMemberCount());
dispatcher.assertEventCount(2);
......@@ -115,14 +100,69 @@ public class ModelCacheTest extends AbstractTopoModelTest {
@Test
public void createThreeNodeCluster() {
title("createThreeNodeCluster");
cache.addOrUpdateClusterMember(NODE_1);
cache.addOrUpdateClusterMember(CNODE_1);
dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1);
cache.addOrUpdateClusterMember(NODE_2);
cache.addOrUpdateClusterMember(CNODE_2);
dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C2);
cache.addOrUpdateClusterMember(NODE_3);
cache.addOrUpdateClusterMember(CNODE_3);
dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C3);
dispatcher.assertEventCount(3);
print(cache);
}
@Test
public void addNodeThenExamineIt() {
title("addNodeThenExamineIt");
cache.addOrUpdateClusterMember(CNODE_1);
dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1);
UiClusterMember member = cache.accessClusterMember(nodeId(C1));
print(member);
// see AbstractUiImplTest Mock Environment for expected values...
assertEquals("wrong id str", C1, member.idAsString());
assertEquals("wrong id", nodeId(C1), member.id());
assertEquals("wrong dev count", 3, member.deviceCount());
assertEquals("not online", true, member.isOnline());
assertEquals("not ready", true, member.isReady());
assertMasterOf(member, DEVID_1, DEVID_2, DEVID_3);
assertNotMasterOf(member, DEVID_4, DEVID_6, DEVID_9);
}
private void assertMasterOf(UiClusterMember member, DeviceId... ids) {
for (DeviceId id : ids) {
assertTrue("not master of " + id, member.masterOf(id));
}
}
private void assertNotMasterOf(UiClusterMember member, DeviceId... ids) {
for (DeviceId id : ids) {
assertFalse("? master of " + id, member.masterOf(id));
}
}
@Test
public void addNodeAndDevices() {
title("addNodeAndDevices");
cache.addOrUpdateClusterMember(CNODE_1);
cache.addOrUpdateDevice(DEV_1);
cache.addOrUpdateDevice(DEV_2);
cache.addOrUpdateDevice(DEV_3);
print(cache);
}
@Test
public void addRegions() {
title("addRegions");
cache.addOrUpdateRegion(REGION_1);
print(cache);
}
@Test
public void load() {
title("load");
cache.load();
print(cache);
}
}
......