* @param port
* @param lastSeen
* @return
*/
protected boolean updateAttachmentPoint(long sw, short port, long lastSeen){
ITopologyService topology = deviceManager.topology;
List<AttachmentPoint> oldAPList;
List<AttachmentPoint> apList;
boolean oldAPFlag = false;
if (!deviceManager.isValidAttachmentPoint(sw, port)) return false;
AttachmentPoint newAP = new AttachmentPoint(sw, port, lastSeen);
//Copy the oldAP and ap list.
apList = new ArrayList<AttachmentPoint>();
if (attachmentPoints != null) apList.addAll(attachmentPoints);
oldAPList = new ArrayList<AttachmentPoint>();
if (oldAPs != null) oldAPList.addAll(oldAPs);
// if the sw, port is in old AP, remove it from there
// and update the lastSeen in that object.
if (oldAPList.contains(newAP)) {
int index = oldAPList.indexOf(newAP);
newAP = oldAPList.remove(index);
newAP.setLastSeen(lastSeen);
this.oldAPs = oldAPList;
oldAPFlag = true;
}
// newAP now contains the new attachment point.
// Get the APMap is null or empty.
Map<Long, AttachmentPoint> apMap = getAPMap(apList);
if (apMap == null || apMap.isEmpty()) {
apList.add(newAP);
attachmentPoints = apList;
// there are no old attachment points - since the device exists, this
// may be because the host really moved (so the old AP port went down);
// or it may be because the switch restarted (so old APs were nullified).
// For now we will treat both cases as host moved.
return true;
}
long id = topology.getL2DomainId(sw);
AttachmentPoint oldAP = apMap.get(id);
if (oldAP == null) // No attachment on this L2 domain.
{
apList = new ArrayList<AttachmentPoint>();
apList.addAll(apMap.values());
apList.add(newAP);
this.attachmentPoints = apList;
return true; // new AP found on an L2 island.
}
// There is already a known attachment point on the same L2 island.
// we need to compare oldAP and newAP.
if (oldAP.equals(newAP)) {
// nothing to do here. just the last seen has to be changed.
if (newAP.lastSeen > oldAP.lastSeen) {
oldAP.setLastSeen(newAP.lastSeen);
}
this.attachmentPoints =
new ArrayList<AttachmentPoint>(apMap.values());
return false; // nothing to do here.
}
int x = deviceManager.apComparator.compare(oldAP, newAP);
if (x < 0) {
// newAP replaces oldAP.
apMap.put(id, newAP);
this.attachmentPoints =
new ArrayList<AttachmentPoint>(apMap.values());
oldAPList = new ArrayList<AttachmentPoint>();
if (oldAPs != null) oldAPList.addAll(oldAPs);
oldAPList.add(oldAP);
this.oldAPs = oldAPList;
if (!topology.isInSameBroadcastDomain(oldAP.getSw(), oldAP.getPort(),
newAP.getSw(), newAP.getPort()))
return true; // attachment point changed.
} else if (oldAPFlag) {
// retain oldAP as is. Put the newAP in oldAPs for flagging
// possible duplicates.