From: Kalvin Hom Date: Wed, 19 Jun 2013 00:08:53 +0000 (-0700) Subject: HostTracker StaticHost changes X-Git-Tag: releasepom-0.1.0~340^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=68d5804a517d9b035861287dafa4d715b00719b2 HostTracker StaticHost changes Added "staticHost" attribute for Device so ported HostTracker can distinguish between dynamically discovered hosts and static hosts given through the API. Implemented the add/remove/get static host methods. Change-Id: I1374b6c40919c6f31b48c8191bf2021620a9509d Signed-off-by: Kalvin Hom --- diff --git a/opendaylight/hosttracker_new/api/src/main/java/org/opendaylight/controller/hosttracker/IDevice.java b/opendaylight/hosttracker_new/api/src/main/java/org/opendaylight/controller/hosttracker/IDevice.java index b2ba643a0c..8c9367e25a 100644 --- a/opendaylight/hosttracker_new/api/src/main/java/org/opendaylight/controller/hosttracker/IDevice.java +++ b/opendaylight/hosttracker_new/api/src/main/java/org/opendaylight/controller/hosttracker/IDevice.java @@ -134,6 +134,10 @@ public interface IDevice { */ public IEntityClass getEntityClass(); + public boolean isStaticHost(); + + public void setStaticHost(boolean isStatic); + public HostNodeConnector toHostNodeConnector(); } diff --git a/opendaylight/hosttracker_new/implementation/src/main/java/org/opendaylight/controller/hosttracker/internal/Device.java b/opendaylight/hosttracker_new/implementation/src/main/java/org/opendaylight/controller/hosttracker/internal/Device.java index a97cb27383..fb81cddc96 100755 --- a/opendaylight/hosttracker_new/implementation/src/main/java/org/opendaylight/controller/hosttracker/internal/Device.java +++ b/opendaylight/hosttracker_new/implementation/src/main/java/org/opendaylight/controller/hosttracker/internal/Device.java @@ -78,6 +78,8 @@ public class Device implements IDevice { protected final Short[] vlanIds; protected volatile String dhcpClientName; + private boolean staticHost; + /** * These are the old attachment points for the device that were valid no * more than INACTIVITY_TIME ago. @@ -793,6 +795,7 @@ public class Device implements IDevice { byte[] macAddr = macLongToByte(this.getMACAddress()); HostNodeConnector nc = new HostNodeConnector(macAddr, ip, n, (short) 0); + nc.setStaticHost(this.isStaticHost()); return nc; } catch (Exception e) { return null; @@ -807,6 +810,14 @@ public class Device implements IDevice { return macAddr; } + public boolean isStaticHost(){ + return this.staticHost; + } + + public void setStaticHost(boolean isStatic){ + this.staticHost = isStatic; + } + @Override public String toString() { StringBuilder builder = new StringBuilder(); diff --git a/opendaylight/hosttracker_new/implementation/src/main/java/org/opendaylight/controller/hosttracker/internal/DeviceManagerImpl.java b/opendaylight/hosttracker_new/implementation/src/main/java/org/opendaylight/controller/hosttracker/internal/DeviceManagerImpl.java index 0a70b72575..faf9416934 100755 --- a/opendaylight/hosttracker_new/implementation/src/main/java/org/opendaylight/controller/hosttracker/internal/DeviceManagerImpl.java +++ b/opendaylight/hosttracker_new/implementation/src/main/java/org/opendaylight/controller/hosttracker/internal/DeviceManagerImpl.java @@ -38,6 +38,7 @@ import static org.opendaylight.controller.hosttracker.internal.DeviceManagerImpl import static org.opendaylight.controller.hosttracker.internal.DeviceManagerImpl.DeviceUpdate.Change.DELETE; import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; @@ -81,10 +82,12 @@ import org.opendaylight.controller.sal.packet.Packet; import org.opendaylight.controller.sal.packet.PacketResult; import org.opendaylight.controller.sal.packet.RawPacket; import org.opendaylight.controller.sal.topology.TopoEdgeUpdate; +import org.opendaylight.controller.sal.utils.HexEncode; import org.opendaylight.controller.sal.utils.ListenerDispatcher; import org.opendaylight.controller.sal.utils.MultiIterator; import org.opendaylight.controller.sal.utils.SingletonTask; import org.opendaylight.controller.sal.utils.Status; +import org.opendaylight.controller.sal.utils.StatusCode; import org.opendaylight.controller.switchmanager.ISwitchManager; import org.opendaylight.controller.topologymanager.ITopologyManager; import org.opendaylight.controller.topologymanager.ITopologyManagerAware; @@ -1179,6 +1182,7 @@ public class DeviceManagerImpl implements IDeviceService, IEntityClassListener, logger.info("Primary index {}", primaryIndex); ArrayList deleteQueue = null; LinkedList deviceUpdates = null; + Device oldDevice = null; Device device = null; // we may need to restart the learning process if we detect @@ -1334,7 +1338,7 @@ public class DeviceManagerImpl implements IDeviceService, IEntityClassListener, // modified this Device). if (!res) continue; - + oldDevice = device; device = newDevice; // update indices if (!updateIndices(device, deviceKey)) { @@ -1363,7 +1367,7 @@ public class DeviceManagerImpl implements IDeviceService, IEntityClassListener, if (moved) { // we count device moved events in // sendDeviceMovedNotification() - sendDeviceMovedNotification(device); + sendDeviceMovedNotification(device, oldDevice); if (logger.isTraceEnabled()) { logger.trace("Device moved: attachment points {}," + "entities {}", device.attachmentPoints, @@ -1472,6 +1476,10 @@ public class DeviceManagerImpl implements IDeviceService, IEntityClassListener, case ADD: notify.notifyHTClient(update.device.toHostNodeConnector()); break; + case DELETE: + notify.notifyHTClientHostRemoved(update.device.toHostNodeConnector()); + break; + case CHANGE: } } @@ -1898,6 +1906,19 @@ public class DeviceManagerImpl implements IDeviceService, IEntityClassListener, } } } + /** + * Send update notifications to listeners. + * IfNewHostNotify listeners need to remove old device and add new device. + * @param device + * @param oldDevice + */ + protected void sendDeviceMovedNotification(Device device, Device oldDevice){ + for (IfNewHostNotify notify : newHostNotify){ + notify.notifyHTClientHostRemoved(oldDevice.toHostNodeConnector()); + notify.notifyHTClient(device.toHostNodeConnector()); + } + sendDeviceMovedNotification(device); + } /** * this method will reclassify and reconcile a device - possibilities are - @@ -2131,8 +2152,16 @@ public class DeviceManagerImpl implements IDeviceService, IEntityClassListener, @Override public Set getActiveStaticHosts() { - // TODO Auto-generated method stub - return null; + Collection devices = Collections + .unmodifiableCollection(deviceMap.values()); + Iterator i = devices.iterator(); + Set nc = new HashSet(); + while (i.hasNext()) { + Device device = i.next(); + if(device.isStaticHost()) + nc.add(device.toHostNodeConnector()); + } + return nc; } @Override @@ -2144,14 +2173,41 @@ public class DeviceManagerImpl implements IDeviceService, IEntityClassListener, @Override public Status addStaticHost(String networkAddress, String dataLayerAddress, NodeConnector nc, String vlan) { - // TODO Auto-generated method stub - return null; + Long mac = HexEncode.stringToLong(dataLayerAddress); + try{ + InetAddress addr = InetAddress.getByName(networkAddress); + int ip = toIPv4Address(addr.getAddress()); + Entity e = new Entity(mac, Short.valueOf(vlan), ip, nc, new Date()); + Device d = this.learnDeviceByEntity(e); + d.setStaticHost(true); + return new Status(StatusCode.SUCCESS); + }catch(UnknownHostException e){ + return new Status(StatusCode.INTERNALERROR); + } } @Override public Status removeStaticHost(String networkAddress) { - // TODO Auto-generated method stub - return null; + Integer addr; + try { + addr = toIPv4Address(InetAddress.getByName(networkAddress).getAddress()); + } catch (UnknownHostException e) { + return new Status(StatusCode.NOTFOUND, "Host does not exist"); + } + Iterator di = this.getDeviceIteratorForQuery(null, null, addr, null); + List listeners = deviceListeners + .getOrderedListeners(); + while(di.hasNext()){ + Device d = di.next(); + if(d.isStaticHost()){ + deleteDevice(d); + for (IfNewHostNotify notify : newHostNotify) + notify.notifyHTClientHostRemoved(d.toHostNodeConnector()); + for (IDeviceListener listener : listeners) + listener.deviceRemoved(d); + } + } + return new Status(StatusCode.SUCCESS); } /**