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 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);
}
}
......@@ -22,26 +22,48 @@ import org.onosproject.cluster.RoleInfo;
import org.onosproject.event.EventDispatcher;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.EdgeLink;
import org.onosproject.net.Host;
import org.onosproject.net.HostId;
import org.onosproject.net.HostLocation;
import org.onosproject.net.Link;
import org.onosproject.net.region.Region;
import org.onosproject.net.region.RegionId;
import org.onosproject.ui.model.ServiceBundle;
import org.onosproject.ui.model.topo.UiClusterMember;
import org.onosproject.ui.model.topo.UiDevice;
import org.onosproject.ui.model.topo.UiElement;
import org.onosproject.ui.model.topo.UiHost;
import org.onosproject.ui.model.topo.UiLink;
import org.onosproject.ui.model.topo.UiLinkId;
import org.onosproject.ui.model.topo.UiRegion;
import org.onosproject.ui.model.topo.UiTopology;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Set;
import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_ADDED_OR_UPDATED;
import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_REMOVED;
import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_ADDED_OR_UPDATED;
import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_REMOVED;
import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_ADDED_OR_UPDATED;
import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_MOVED;
import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_REMOVED;
import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.LINK_ADDED_OR_UPDATED;
import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.LINK_REMOVED;
import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.REGION_ADDED_OR_UPDATED;
import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.REGION_REMOVED;
import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId;
/**
* UI Topology Model cache.
*/
class ModelCache {
private static final String E_NO_ELEMENT = "Tried to remove non-member {}: {}";
private static final Logger log = LoggerFactory.getLogger(ModelCache.class);
private final ServiceBundle services;
......@@ -58,122 +80,310 @@ class ModelCache {
return "ModelCache{" + uiTopology + "}";
}
/**
* Clear our model.
*/
private void postEvent(UiModelEvent.Type type, UiElement subject) {
dispatcher.post(new UiModelEvent(type, subject));
}
void clear() {
uiTopology.clear();
}
/**
* Create our internal model of the global topology.
* Create our internal model of the global topology. An assumption we are
* making is that the topology is empty to start.
*/
void load() {
// TODO - implement loading of initial state
// loadClusterMembers();
// loadRegions();
// loadDevices();
// loadHosts();
// loadLinks();
loadClusterMembers();
loadRegions();
loadDevices();
loadLinks();
loadHosts();
}
/**
* Updates the model (adds a new instance if necessary) with the given
* controller node information.
*
* @param cnode controller node to be added/updated
*/
// === CLUSTER MEMBERS
private UiClusterMember addNewClusterMember(ControllerNode n) {
UiClusterMember member = new UiClusterMember(uiTopology, n);
uiTopology.add(member);
return member;
}
private void updateClusterMember(UiClusterMember member) {
ControllerNode.State state = services.cluster().getState(member.id());
member.setState(state);
member.setMastership(services.mastership().getDevicesOf(member.id()));
// NOTE: 'UI-attached' is session-based data, not global, so will
// be set elsewhere
}
private void loadClusterMembers() {
for (ControllerNode n : services.cluster().getNodes()) {
UiClusterMember member = addNewClusterMember(n);
updateClusterMember(member);
}
}
// invoked from UiSharedTopologyModel cluster event listener
void addOrUpdateClusterMember(ControllerNode cnode) {
NodeId id = cnode.id();
UiClusterMember member = uiTopology.findClusterMember(id);
if (member == null) {
member = new UiClusterMember(cnode);
uiTopology.add(member);
member = addNewClusterMember(cnode);
}
updateClusterMember(member);
// inject computed data about the cluster node, into the model object
ControllerNode.State state = services.cluster().getState(id);
member.setState(state);
member.setDeviceCount(services.mastership().getDevicesOf(id).size());
// NOTE: UI-attached is session-based data, not global
postEvent(CLUSTER_MEMBER_ADDED_OR_UPDATED, member);
}
dispatcher.post(new UiModelEvent(CLUSTER_MEMBER_ADDED_OR_UPDATED, member));
// package private for unit test access
UiClusterMember accessClusterMember(NodeId id) {
return uiTopology.findClusterMember(id);
}
/**
* Removes from the model the specified controller node.
*
* @param cnode controller node to be removed
*/
// invoked from UiSharedTopologyModel cluster event listener
void removeClusterMember(ControllerNode cnode) {
NodeId id = cnode.id();
UiClusterMember member = uiTopology.findClusterMember(id);
if (member != null) {
uiTopology.remove(member);
dispatcher.post(new UiModelEvent(CLUSTER_MEMBER_REMOVED, member));
postEvent(CLUSTER_MEMBER_REMOVED, member);
} else {
log.warn("Tried to remove non-member cluster node {}", id);
log.warn(E_NO_ELEMENT, "cluster node", id);
}
}
// === MASTERSHIP CHANGES
// invoked from UiSharedTopologyModel mastership listener
void updateMasterships(DeviceId deviceId, RoleInfo roleInfo) {
// To think about:: do we need to store mastership info?
// or can we rely on looking it up live?
// TODO: store the updated mastership information
// TODO: post event
}
// === REGIONS
private UiRegion addNewRegion(Region r) {
UiRegion region = new UiRegion(uiTopology, r);
uiTopology.add(region);
return region;
}
private void updateRegion(UiRegion region) {
Set<DeviceId> devs = services.region().getRegionDevices(region.id());
region.reconcileDevices(devs);
}
private void loadRegions() {
for (Region r : services.region().getRegions()) {
UiRegion region = addNewRegion(r);
updateRegion(region);
}
}
// invoked from UiSharedTopologyModel region listener
void addOrUpdateRegion(Region region) {
// TODO: find or create region assoc. with parameter
// TODO: post event
RegionId id = region.id();
UiRegion uiRegion = uiTopology.findRegion(id);
if (uiRegion == null) {
uiRegion = addNewRegion(region);
}
updateRegion(uiRegion);
postEvent(REGION_ADDED_OR_UPDATED, uiRegion);
}
// invoked from UiSharedTopologyModel region listener
void removeRegion(Region region) {
// TODO: find region assoc. with parameter; remove from model
// TODO: post event
RegionId id = region.id();
UiRegion uiRegion = uiTopology.findRegion(id);
if (uiRegion != null) {
uiTopology.remove(uiRegion);
postEvent(REGION_REMOVED, uiRegion);
} else {
log.warn(E_NO_ELEMENT, "region", id);
}
}
// === DEVICES
private UiDevice addNewDevice(Device d) {
UiDevice device = new UiDevice(uiTopology, d);
uiTopology.add(device);
return device;
}
private void updateDevice(UiDevice device) {
device.setRegionId(services.region().getRegionForDevice(device.id()).id());
}
private void loadDevices() {
for (Device d : services.device().getDevices()) {
UiDevice device = addNewDevice(d);
updateDevice(device);
}
}
// invoked from UiSharedTopologyModel device listener
void addOrUpdateDevice(Device device) {
// TODO: find or create device assoc. with parameter
// FIXME
UiDevice uiDevice = new UiDevice();
DeviceId id = device.id();
UiDevice uiDevice = uiTopology.findDevice(id);
if (uiDevice == null) {
uiDevice = addNewDevice(device);
}
updateDevice(uiDevice);
// TODO: post the (correct) event
dispatcher.post(new UiModelEvent(DEVICE_ADDED_OR_UPDATED, uiDevice));
postEvent(DEVICE_ADDED_OR_UPDATED, uiDevice);
}
// invoked from UiSharedTopologyModel device listener
void removeDevice(Device device) {
// TODO: get UiDevice associated with the given parameter; remove from model
// FIXME
UiDevice uiDevice = new UiDevice();
DeviceId id = device.id();
UiDevice uiDevice = uiTopology.findDevice(id);
if (uiDevice != null) {
uiTopology.remove(uiDevice);
postEvent(DEVICE_REMOVED, uiDevice);
} else {
log.warn(E_NO_ELEMENT, "device", id);
}
}
// === LINKS
private UiLink addNewLink(UiLinkId id) {
UiLink uiLink = new UiLink(uiTopology, id);
uiTopology.add(uiLink);
return uiLink;
}
private void updateLink(UiLink uiLink, Link link) {
uiLink.attachBackingLink(link);
}
// TODO: post the (correct) event
dispatcher.post(new UiModelEvent(DEVICE_REMOVED, uiDevice));
private void loadLinks() {
for (Link link : services.link().getLinks()) {
UiLinkId id = uiLinkId(link);
UiLink uiLink = uiTopology.findLink(id);
if (uiLink == null) {
uiLink = addNewLink(id);
}
updateLink(uiLink, link);
}
}
// invoked from UiSharedTopologyModel link listener
void addOrUpdateLink(Link link) {
// TODO: find ui-link assoc. with parameter; create or update.
// TODO: post event
UiLinkId id = uiLinkId(link);
UiLink uiLink = uiTopology.findLink(id);
if (uiLink == null) {
uiLink = addNewLink(id);
}
updateLink(uiLink, link);
postEvent(LINK_ADDED_OR_UPDATED, uiLink);
}
// invoked from UiSharedTopologyModel link listener
void removeLink(Link link) {
// TODO: find ui-link assoc. with parameter; update or remove.
// TODO: post event
UiLinkId id = uiLinkId(link);
UiLink uiLink = uiTopology.findLink(id);
if (uiLink != null) {
boolean remaining = uiLink.detachBackingLink(link);
if (remaining) {
postEvent(LINK_ADDED_OR_UPDATED, uiLink);
} else {
uiTopology.remove(uiLink);
postEvent(LINK_REMOVED, uiLink);
}
} else {
log.warn(E_NO_ELEMENT, "link", id);
}
}
// === HOSTS
private UiHost addNewHost(Host h) {
UiHost host = new UiHost(uiTopology, h);
uiTopology.add(host);
UiLink edgeLink = addNewEdgeLink(host);
host.setEdgeLinkId(edgeLink.id());
return host;
}
private void removeOldEdgeLink(UiHost uiHost) {
UiLink old = uiTopology.findLink(uiHost.edgeLinkId());
if (old != null) {
uiTopology.remove(old);
}
}
private UiLink addNewEdgeLink(UiHost uiHost) {
EdgeLink elink = createEdgeLink(uiHost.backingHost(), true);
UiLinkId elinkId = UiLinkId.uiLinkId(elink);
UiLink uiLink = addNewLink(elinkId);
uiLink.attachEdgeLink(elink);
return uiLink;
}
private void updateHost(UiHost uiHost, Host h) {
removeOldEdgeLink(uiHost);
HostLocation hloc = h.location();
uiHost.setLocation(hloc.deviceId(), hloc.port());
addNewEdgeLink(uiHost);
}
private void loadHosts() {
for (Host h : services.host().getHosts()) {
UiHost host = addNewHost(h);
updateHost(host, h);
}
}
// invoked from UiSharedTopologyModel host listener
void addOrUpdateHost(Host host) {
// TODO: find or create host assoc. with parameter
// TODO: post event
HostId id = host.id();
UiHost uiHost = uiTopology.findHost(id);
if (uiHost == null) {
uiHost = addNewHost(host);
}
updateHost(uiHost, host);
postEvent(HOST_ADDED_OR_UPDATED, uiHost);
}
// invoked from UiSharedTopologyModel host listener
void moveHost(Host host, Host prevHost) {
// TODO: process host-move
// TODO: post event
UiHost uiHost = uiTopology.findHost(prevHost.id());
updateHost(uiHost, host);
postEvent(HOST_MOVED, uiHost);
}
// invoked from UiSharedTopologyModel host listener
void removeHost(Host host) {
// TODO: find host assoc. with parameter; remove from model
HostId id = host.id();
UiHost uiHost = uiTopology.findHost(id);
if (uiHost != null) {
uiTopology.remove(uiHost);
removeOldEdgeLink(uiHost);
postEvent(HOST_REMOVED, uiHost);
} else {
log.warn(E_NO_ELEMENT, "host", id);
}
}
// === CACHE STATISTICS
/**
* Returns the number of members in the cluster.
......@@ -185,7 +395,7 @@ class ModelCache {
}
/**
* Returns the number of regions configured in the topology.
* Returns the number of regions in the topology.
*
* @return number of regions
*/
......
......@@ -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
}
}
......
......@@ -16,38 +16,241 @@
package org.onosproject.ui.impl.topo.model;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
import org.onlab.packet.VlanId;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.ClusterServiceAdapter;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.DefaultControllerNode;
import org.onosproject.cluster.NodeId;
import org.onosproject.cluster.RoleInfo;
import org.onosproject.mastership.MastershipService;
import org.onosproject.mastership.MastershipServiceAdapter;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DefaultDevice;
import org.onosproject.net.DefaultHost;
import org.onosproject.net.DefaultLink;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Host;
import org.onosproject.net.HostId;
import org.onosproject.net.HostLocation;
import org.onosproject.net.Link;
import org.onosproject.net.PortNumber;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.device.DeviceServiceAdapter;
import org.onosproject.net.flow.FlowRuleService;
import org.onosproject.net.host.HostService;
import org.onosproject.net.host.HostServiceAdapter;
import org.onosproject.net.intent.IntentService;
import org.onosproject.net.link.LinkService;
import org.onosproject.net.link.LinkServiceAdapter;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.net.region.DefaultRegion;
import org.onosproject.net.region.Region;
import org.onosproject.net.region.RegionId;
import org.onosproject.net.region.RegionListener;
import org.onosproject.net.region.RegionService;
import org.onosproject.ui.impl.AbstractUiImplTest;
import org.onosproject.ui.model.ServiceBundle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.onosproject.cluster.NodeId.nodeId;
import static org.onosproject.net.DeviceId.deviceId;
import static org.onosproject.net.HostId.hostId;
import static org.onosproject.net.PortNumber.portNumber;
/**
* Base class for model test classes.
*/
abstract class AbstractTopoModelTest extends AbstractUiImplTest {
/*
Our mock environment:
Three controllers: C1, C2, C3
Nine devices: D1 .. D9
D4 ---+ +--- D7
| |
D5 --- D1 --- D2 --- D3 --- D8
| |
D6 ---+ +--- D9
Twelve hosts (two per D4 ... D9) H4a, H4b, H5a, H5b, ...
Regions:
R1 : D1, D2, D3
R2 : D4, D5, D6
R3 : D7, D8, D9
Mastership:
C1 : D1, D2, D3
C2 : D4, D5, D6
C3 : D7, D8, D9
Roles: (backups)
C1 -> C2, C3
C2 -> C1, C3
C3 -> C1, C2
*/
protected static final String C1 = "C1";
protected static final String C2 = "C2";
protected static final String C3 = "C3";
protected static final NodeId CNID_1 = nodeId(C1);
protected static final NodeId CNID_2 = nodeId(C2);
protected static final NodeId CNID_3 = nodeId(C3);
protected static final ControllerNode CNODE_1 = cnode(CNID_1, "10.0.0.1");
protected static final ControllerNode CNODE_2 = cnode(CNID_2, "10.0.0.2");
protected static final ControllerNode CNODE_3 = cnode(CNID_3, "10.0.0.3");
protected static final String R1 = "R1";
protected static final String R2 = "R2";
protected static final String R3 = "R3";
protected static final Set<NodeId> SET_C1 = ImmutableSet.of(CNID_1);
protected static final Set<NodeId> SET_C2 = ImmutableSet.of(CNID_2);
protected static final Set<NodeId> SET_C3 = ImmutableSet.of(CNID_3);
protected static final Region REGION_1 =
region(R1, Region.Type.METRO, ImmutableList.of(SET_C1, SET_C2));
protected static final Region REGION_2 =
region(R2, Region.Type.CAMPUS, ImmutableList.of(SET_C2, SET_C1));
protected static final Region REGION_3 =
region(R3, Region.Type.CAMPUS, ImmutableList.of(SET_C3, SET_C1));
protected static final Set<Region> REGION_SET =
ImmutableSet.of(REGION_1, REGION_2, REGION_3);
protected static final String D1 = "D1";
protected static final String D2 = "D2";
protected static final String D3 = "D3";
protected static final String D4 = "D4";
protected static final String D5 = "D5";
protected static final String D6 = "D6";
protected static final String D7 = "D7";
protected static final String D8 = "D8";
protected static final String D9 = "D9";
protected static final String MFR = "Mfr";
protected static final String HW = "h/w";
protected static final String SW = "s/w";
protected static final String SERIAL = "ser123";
protected static final DeviceId DEVID_1 = deviceId(D1);
protected static final DeviceId DEVID_2 = deviceId(D2);
protected static final DeviceId DEVID_3 = deviceId(D3);
protected static final DeviceId DEVID_4 = deviceId(D4);
protected static final DeviceId DEVID_5 = deviceId(D5);
protected static final DeviceId DEVID_6 = deviceId(D6);
protected static final DeviceId DEVID_7 = deviceId(D7);
protected static final DeviceId DEVID_8 = deviceId(D8);
protected static final DeviceId DEVID_9 = deviceId(D9);
protected static final Device DEV_1 = device(D1);
protected static final Device DEV_2 = device(D2);
protected static final Device DEV_3 = device(D3);
protected static final Device DEV_4 = device(D4);
protected static final Device DEV_5 = device(D5);
protected static final Device DEV_6 = device(D6);
protected static final Device DEV_7 = device(D7);
protected static final Device DEV_8 = device(D8);
protected static final Device DEV_9 = device(D9);
protected static final List<Device> ALL_DEVS =
ImmutableList.of(
DEV_1, DEV_2, DEV_3,
DEV_4, DEV_5, DEV_6,
DEV_7, DEV_8, DEV_9
);
private static final Set<DeviceId> DEVS_TRUNK =
ImmutableSet.of(DEVID_1, DEVID_2, DEVID_3);
private static final Set<DeviceId> DEVS_LEFT =
ImmutableSet.of(DEVID_4, DEVID_5, DEVID_6);
private static final Set<DeviceId> DEVS_RIGHT =
ImmutableSet.of(DEVID_7, DEVID_8, DEVID_9);
private static final String[][] LINK_CONNECT_DATA = {
{D1, "12", D2, "21"},
{D2, "23", D3, "32"},
{D4, "41", D1, "14"},
{D5, "51", D1, "15"},
{D6, "61", D1, "16"},
{D7, "73", D3, "37"},
{D8, "83", D3, "38"},
{D9, "93", D3, "39"},
};
private static final String HOST_MAC_PREFIX = "aa:00:00:00:00:";
/**
* Returns IP address instance for given string.
*
* @param s string
* @return IP address
*/
protected static IpAddress ip(String s) {
return IpAddress.valueOf(s);
}
/**
* Returns controller node instance for given ID and IP.
*
* @param id identifier
* @param ip IP address
* @return controller node instance
*/
protected static ControllerNode cnode(NodeId id, String ip) {
return new DefaultControllerNode(id, ip(ip));
}
/**
* Returns a region instance with specified parameters.
*
* @param id region id
* @param type region type
* @param masters ordered list of master sets
* @return region instance
*/
protected static Region region(String id, Region.Type type,
List<Set<NodeId>> masters) {
return new DefaultRegion(RegionId.regionId(id), "Region-" + id,
type, masters);
}
/**
* Returns device with given ID.
*
* @param id device ID
* @return device instance
*/
protected static Device device(String id) {
return new DefaultDevice(ProviderId.NONE, deviceId(id),
Device.Type.SWITCH, MFR, HW, SW, SERIAL, null);
}
/**
* Returns canned results.
* <p>
* At some future point, we may make this "programmable", so that
* it returns certain values based on element IDs etc.
* its state can be changed over the course of a unit test.
*/
protected static final ServiceBundle MOCK_SERVICES =
new ServiceBundle() {
......@@ -63,22 +266,22 @@ abstract class AbstractTopoModelTest extends AbstractUiImplTest {
@Override
public RegionService region() {
return null;
return MOCK_REGION;
}
@Override
public DeviceService device() {
return null;
return MOCK_DEVICE;
}
@Override
public LinkService link() {
return null;
return MOCK_LINK;
}
@Override
public HostService host() {
return null;
return MOCK_HOST;
}
@Override
......@@ -94,64 +297,286 @@ abstract class AbstractTopoModelTest extends AbstractUiImplTest {
private static final ClusterService MOCK_CLUSTER = new MockClusterService();
private static final MastershipService MOCK_MASTER = new MockMasterService();
// TODO: fill out as necessary
private static final RegionService MOCK_REGION = new MockRegionService();
private static final DeviceService MOCK_DEVICE = new MockDeviceService();
private static final LinkService MOCK_LINK = new MockLinkService();
private static final HostService MOCK_HOST = new MockHostService();
/*
Our mock environment:
Three controllers: C1, C2, C3
Nine devices: D1 .. D9
D4 ---+ +--- D7
| |
D5 --- D1 --- D2 --- D3 --- D8
| |
D6 ---+ +--- D9
private static class MockClusterService extends ClusterServiceAdapter {
private final Map<NodeId, ControllerNode> nodes = new HashMap<>();
private final Map<NodeId, ControllerNode.State> states = new HashMap<>();
Twelve hosts (two per D4 ... D9) H41, H42, H51, H52, ...
Regions:
R1 : D1, D2, D3
R2 : D4, D5, D6
R3 : D7, D8, D9
Mastership:
C1 : D1, D2, D3
C2 : D4, D5, D6
C3 : D7, D8, D9
*/
MockClusterService() {
nodes.put(CNODE_1.id(), CNODE_1);
nodes.put(CNODE_2.id(), CNODE_2);
nodes.put(CNODE_3.id(), CNODE_3);
states.put(CNODE_1.id(), ControllerNode.State.READY);
states.put(CNODE_2.id(), ControllerNode.State.ACTIVE);
states.put(CNODE_3.id(), ControllerNode.State.ACTIVE);
}
private static class MockClusterService extends ClusterServiceAdapter {
private final Map<NodeId, ControllerNode.State> states = new HashMap<>();
@Override
public Set<ControllerNode> getNodes() {
return ImmutableSet.copyOf(nodes.values());
}
@Override
public ControllerNode getNode(NodeId nodeId) {
return nodes.get(nodeId);
}
@Override
public ControllerNode.State getState(NodeId nodeId) {
// For now, a hardcoded state of ACTIVE (but not READY)
// irrespective of the node ID.
return ControllerNode.State.ACTIVE;
return states.get(nodeId);
}
}
protected static final DeviceId D1_ID = deviceId("D1");
protected static final DeviceId D2_ID = deviceId("D2");
protected static final DeviceId D3_ID = deviceId("D3");
protected static final DeviceId D4_ID = deviceId("D4");
protected static final DeviceId D5_ID = deviceId("D5");
protected static final DeviceId D6_ID = deviceId("D6");
protected static final DeviceId D7_ID = deviceId("D7");
protected static final DeviceId D8_ID = deviceId("D8");
protected static final DeviceId D9_ID = deviceId("D9");
private static class MockMasterService extends MastershipServiceAdapter {
private final Map<NodeId, Set<DeviceId>> masterOf = new HashMap<>();
MockMasterService() {
masterOf.put(CNODE_1.id(), DEVS_TRUNK);
masterOf.put(CNODE_2.id(), DEVS_LEFT);
masterOf.put(CNODE_3.id(), DEVS_RIGHT);
}
@Override
public NodeId getMasterFor(DeviceId deviceId) {
if (DEVS_TRUNK.contains(deviceId)) {
return CNID_1;
}
if (DEVS_LEFT.contains(deviceId)) {
return CNID_2;
}
if (DEVS_RIGHT.contains(deviceId)) {
return CNID_3;
}
return null;
}
@Override
public Set<DeviceId> getDevicesOf(NodeId nodeId) {
// For now, a hard coded set of two device IDs
// irrespective of the node ID.
return ImmutableSet.of(D1_ID, D2_ID);
return masterOf.get(nodeId);
}
@Override
public RoleInfo getNodesFor(DeviceId deviceId) {
NodeId master = null;
List<NodeId> backups = new ArrayList<>();
if (DEVS_TRUNK.contains(deviceId)) {
master = CNID_1;
backups.add(CNID_2);
backups.add(CNID_3);
} else if (DEVS_LEFT.contains(deviceId)) {
master = CNID_2;
backups.add(CNID_1);
backups.add(CNID_3);
} else if (DEVS_RIGHT.contains(deviceId)) {
master = CNID_3;
backups.add(CNID_1);
backups.add(CNID_2);
}
return new RoleInfo(master, backups);
}
}
// TODO: consider implementing RegionServiceAdapter and extending that here
private static class MockRegionService implements RegionService {
private final Map<RegionId, Region> lookup = new HashMap<>();
MockRegionService() {
lookup.put(REGION_1.id(), REGION_1);
lookup.put(REGION_2.id(), REGION_2);
lookup.put(REGION_3.id(), REGION_3);
}
@Override
public Set<Region> getRegions() {
return REGION_SET;
}
@Override
public Region getRegion(RegionId regionId) {
return lookup.get(regionId);
}
@Override
public Region getRegionForDevice(DeviceId deviceId) {
if (DEVS_TRUNK.contains(deviceId)) {
return REGION_1;
}
if (DEVS_LEFT.contains(deviceId)) {
return REGION_2;
}
if (DEVS_RIGHT.contains(deviceId)) {
return REGION_3;
}
return null;
}
@Override
public Set<DeviceId> getRegionDevices(RegionId regionId) {
if (REGION_1.id().equals(regionId)) {
return DEVS_TRUNK;
}
if (REGION_2.id().equals(regionId)) {
return DEVS_LEFT;
}
if (REGION_3.id().equals(regionId)) {
return DEVS_RIGHT;
}
return Collections.emptySet();
}
@Override
public void addListener(RegionListener listener) {
}
@Override
public void removeListener(RegionListener listener) {
}
}
private static class MockDeviceService extends DeviceServiceAdapter {
private final Map<DeviceId, Device> devices = new HashMap<>();
MockDeviceService() {
for (Device dev : ALL_DEVS) {
devices.put(dev.id(), dev);
}
}
@Override
public int getDeviceCount() {
return devices.size();
}
@Override
public Iterable<Device> getDevices() {
return ImmutableList.copyOf(devices.values());
}
@Override
public Device getDevice(DeviceId deviceId) {
return devices.get(deviceId);
}
}
private static class MockLinkService extends LinkServiceAdapter {
private final Set<Link> links = new HashSet<>();
MockLinkService() {
for (String[] linkPair : LINK_CONNECT_DATA) {
links.addAll(makeLinks(linkPair));
}
}
private Set<Link> makeLinks(String[] linkPair) {
DeviceId devA = deviceId(linkPair[0]);
PortNumber portA = portNumber(Long.valueOf(linkPair[1]));
DeviceId devB = deviceId(linkPair[2]);
PortNumber portB = portNumber(Long.valueOf(linkPair[3]));
Link linkA = DefaultLink.builder()
.providerId(ProviderId.NONE)
.type(Link.Type.DIRECT)
.src(new ConnectPoint(devA, portA))
.dst(new ConnectPoint(devB, portB))
.build();
Link linkB = DefaultLink.builder()
.providerId(ProviderId.NONE)
.type(Link.Type.DIRECT)
.src(new ConnectPoint(devB, portB))
.dst(new ConnectPoint(devA, portA))
.build();
return ImmutableSet.of(linkA, linkB);
}
@Override
public int getLinkCount() {
return links.size();
}
@Override
public Iterable<Link> getLinks() {
return ImmutableSet.copyOf(links);
}
// TODO: possibly fill out other methods if we find the model uses them
}
private static class MockHostService extends HostServiceAdapter {
private final Map<HostId, Host> hosts = new HashMap<>();
MockHostService() {
for (Device d : ALL_DEVS) {
// two hosts per device
createHosts(hosts, d);
}
}
private void createHosts(Map<HostId, Host> hosts, Device d) {
DeviceId deviceId = d.id();
String devNum = deviceId.toString().substring(1);
String ha = devNum + "a";
String hb = devNum + "b";
MacAddress macA = MacAddress.valueOf(HOST_MAC_PREFIX + ha);
MacAddress macB = MacAddress.valueOf(HOST_MAC_PREFIX + hb);
HostId hostA = hostId(String.format("%s/-1", macA));
HostId hostB = hostId(String.format("%s/-1", macB));
PortNumber portA = portNumber(101);
PortNumber portB = portNumber(102);
HostLocation locA = new HostLocation(deviceId, portA, 0);
HostLocation locB = new HostLocation(deviceId, portB, 0);
IpAddress ipA = ip("10." + devNum + ".0.1");
IpAddress ipB = ip("10." + devNum + ".0.2");
Host host = new DefaultHost(ProviderId.NONE,
hostA, macA, VlanId.NONE, locA,
ImmutableSet.of(ipA));
hosts.put(hostA, host);
host = new DefaultHost(ProviderId.NONE,
hostB, macB, VlanId.NONE, locB,
ImmutableSet.of(ipB));
hosts.put(hostB, host);
}
@Override
public int getHostCount() {
return hosts.size();
}
@Override
public Iterable<Host> getHosts() {
return ImmutableSet.copyOf(hosts.values());
}
@Override
public Host getHost(HostId hostId) {
return hosts.get(hostId);
}
// TODO: possibly fill out other methods, should the model require them
}
}
......
......@@ -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);
}
}
......