X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fprotocol_plugins%2Fopenflow%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fprotocol_plugin%2Fopenflow%2Finternal%2FDiscoveryService.java;h=3f3c8bde2e2aef4751c735f84e6259da1b10c730;hb=refs%2Fchanges%2F90%2F590%2F2;hp=8c41624570b5239dfb0e4a84e72d95a250780522;hpb=fc604dcce8abb16b675a03df821fb6e5b1760dc0;p=controller.git diff --git a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/DiscoveryService.java b/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/DiscoveryService.java index 8c41624570..3f3c8bde2e 100644 --- a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/DiscoveryService.java +++ b/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/DiscoveryService.java @@ -1,4 +1,3 @@ - /* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * @@ -9,6 +8,7 @@ package org.opendaylight.controller.protocol_plugin.openflow.internal; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -27,6 +27,8 @@ import org.eclipse.osgi.framework.console.CommandInterpreter; import org.eclipse.osgi.framework.console.CommandProvider; import org.opendaylight.controller.protocol_plugin.openflow.IDataPacketListen; import org.opendaylight.controller.protocol_plugin.openflow.IDataPacketMux; +import org.opendaylight.controller.protocol_plugin.openflow.IDiscoveryListener; +import org.opendaylight.controller.protocol_plugin.openflow.IInventoryProvider; import org.opendaylight.controller.protocol_plugin.openflow.IInventoryShimExternalListener; import org.opendaylight.controller.protocol_plugin.openflow.core.IController; import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch; @@ -46,8 +48,6 @@ import org.opendaylight.controller.sal.core.NodeConnector; import org.opendaylight.controller.sal.core.Property; import org.opendaylight.controller.sal.core.State; import org.opendaylight.controller.sal.core.UpdateType; -import org.opendaylight.controller.sal.discovery.IDiscoveryService; -import org.opendaylight.controller.sal.inventory.IPluginInInventoryService; import org.opendaylight.controller.sal.packet.Ethernet; import org.opendaylight.controller.sal.packet.LLDP; import org.opendaylight.controller.sal.packet.LLDPTLV; @@ -63,42 +63,60 @@ import org.opendaylight.controller.sal.utils.NodeCreator; /** * The class describes neighbor discovery service for an OpenFlow network. */ -public class DiscoveryService implements IInventoryShimExternalListener, - IDataPacketListen, IContainerListener, CommandProvider { - private static Logger logger = LoggerFactory - .getLogger(DiscoveryService.class); +public class DiscoveryService implements IInventoryShimExternalListener, IDataPacketListen, IContainerListener, + CommandProvider { + private static Logger logger = LoggerFactory.getLogger(DiscoveryService.class); private IController controller = null; - private IDiscoveryService discoveryService = null; - private IPluginInInventoryService pluginInInventoryService = null; + private IDiscoveryListener discoveryListener = null; + private IInventoryProvider inventoryProvider = null; private IDataPacketMux iDataPacketMux = null; - - private List readyListHi = null; // newly added ports go into this list and will be served first - private List readyListLo = null; // come here after served at least once - private List waitingList = null; // staging area during quiet period - private ConcurrentMap pendingMap = null;// wait for response back - private ConcurrentMap edgeMap = null; // openflow edges keyed by head connector - private ConcurrentMap agingMap = null; // aging entries keyed by edge port - private ConcurrentMap prodMap = null; // production edges keyed by edge port - - private Timer discoveryTimer; // discovery timer - private DiscoveryTimerTask discoveryTimerTask; // timer task - private long discoveryTimerTick = 1L * 1000; // per tick in msec + // Newly added ports go into this list and will be served first + private List readyListHi = null; + // Come here after served at least once + private List readyListLo = null; + // Staging area during quiet period + private List waitingList = null; + // Wait for next discovery packet. The map contains the time elapsed since + // the last received LLDP frame on each node connector + private ConcurrentMap pendingMap = null; + // openflow edges keyed by head connector + private ConcurrentMap edgeMap = null; + // Aging entries keyed by head edge connector + private ConcurrentMap agingMap = null; + // Production edges keyed by head edge connector + private ConcurrentMap prodMap = null; + + private Timer discoveryTimer; + private DiscoveryTimerTask discoveryTimerTask; + private final static long discoveryTimerTick = 2L * 1000; // per tick in msec private int discoveryTimerTickCount = 0; // main tick counter - private int discoveryBatchMaxPorts = 500; // max # of ports handled in one batch - private int discoveryBatchRestartTicks = getDiscoveryInterval(); // periodically restart batching process - private int discoveryBatchPausePeriod = 5; // pause for few secs - private int discoveryBatchPauseTicks = discoveryBatchRestartTicks - discoveryBatchPausePeriod; // pause after this point - private int discoveryRetry = getDiscoveryRetry(); // number of retries after initial timeout - private int discoveryTimeoutTicks = getDiscoveryTimeout(); // timeout in sec - private int discoveryAgeoutTicks = 120; // age out 2 min - private int discoveryConsistencyCheckMultiple = 2; // multiple of discoveryBatchRestartTicks - private int discoveryConsistencyCheckTickCount = discoveryBatchPauseTicks; // CC tick counter - private int discoveryConsistencyCheckCallingTimes = 0; // # of times CC gets called - private int discoveryConsistencyCheckCorrected = 0; // # of cases CC corrected - private boolean discoveryConsistencyCheckEnabled = true;// enable or disable CC - private boolean discoveryAgingEnabled = true; // enable or disable aging - private boolean discoverySnoopingEnabled = true; // global flag to enable or disable LLDP snooping - private List discoverySnoopingDisableList; // the list of ports that will not do LLDP snooping + // Max # of ports handled in one batch + private int discoveryBatchMaxPorts = 500; + // Periodically restart batching process + private int discoveryBatchRestartTicks = getDiscoveryInterval(); + private int discoveryBatchPausePeriod = 5; + // Pause after this point + private int discoveryBatchPauseTicks = discoveryBatchRestartTicks - discoveryBatchPausePeriod; + // Number of retries after initial timeout + private int discoveryRetry = getDiscoveryRetry(); + private int discoveryTimeoutTicks = getDiscoveryTimeout(); + private int discoveryAgeoutTicks = getDiscoveryAgeout(); + // multiple of discoveryBatchRestartTicks + private int discoveryConsistencyCheckMultiple = 2; + // CC tick counter + private int discoveryConsistencyCheckTickCount = discoveryBatchPauseTicks; + // # of times CC getscalled + private int discoveryConsistencyCheckCallingTimes = 0; + // # of cases CC corrected + private int discoveryConsistencyCheckCorrected = 0; + // Enable or disable CC + private boolean discoveryConsistencyCheckEnabled = true; + // Enable or disable aging + private boolean discoveryAgingEnabled = true; + // Global flag to enable or disable LLDP snooping + private boolean discoverySnoopingEnabled = true; + // The list of ports that will not do LLDP snooping + private List discoverySnoopingDisableList; private BlockingQueue transmitQ; private Thread transmitThread; private Boolean throttling = false; // if true, no more batching. @@ -113,6 +131,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, this.transmitQ = transmitQ; } + @Override public void run() { while (true) { try { @@ -121,19 +140,19 @@ public class DiscoveryService implements IInventoryShimExternalListener, sendDiscoveryPacket(nodeConnector, outPkt); nodeConnector = null; } catch (InterruptedException e1) { - logger - .warn("DiscoveryTransmit interupted", e1 - .getMessage()); - if (shuttingDown) + logger.warn("DiscoveryTransmit interupted", e1.getMessage()); + if (shuttingDown) { return; + } } catch (Exception e2) { - logger.error("",e2); + logger.error("", e2); } } } } class DiscoveryTimerTask extends TimerTask { + @Override public void run() { checkTimeout(); checkAging(); @@ -142,25 +161,54 @@ public class DiscoveryService implements IInventoryShimExternalListener, } } + public enum DiscoveryPeriod { + INTERVAL(300), + TIMEOUT (60), + AGEOUT (120); + + private int time; // sec + private int tick; // tick + + DiscoveryPeriod(int time) { + this.time = time; + this.tick = time2Tick(time); + } + + public int getTime() { + return time; + } + + public void setTime(int time) { + this.time = time; + this.tick = time2Tick(time); + } + + public int getTick() { + return tick; + } + + private int time2Tick(int time) { + return (int) (time / (discoveryTimerTick / 1000)); + } + } + private RawPacket createDiscoveryPacket(NodeConnector nodeConnector) { - String nodeId = HexEncode.longToHexString((Long) nodeConnector - .getNode().getID()); + String nodeId = HexEncode.longToHexString((Long) nodeConnector.getNode().getID()); // Create LLDP ChassisID TLV byte[] cidValue = LLDPTLV.createChassisIDTLVValue(nodeId); - chassisIdTlv.setType((byte) LLDPTLV.TLVType.ChassisID.getValue()) - .setLength((short) cidValue.length).setValue(cidValue); + chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue()).setLength((short) cidValue.length) + .setValue(cidValue); // Create LLDP PortID TLV String portId = nodeConnector.getNodeConnectorIDString(); byte[] pidValue = LLDPTLV.createPortIDTLVValue(portId); - portIdTlv.setType((byte) LLDPTLV.TLVType.PortID.getValue()) - .setLength((short) pidValue.length).setValue(pidValue); + portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue()).setLength((short) pidValue.length).setValue(pidValue); // Create LLDP Custom TLV byte[] customValue = LLDPTLV.createCustomTLVValue(nodeConnector.toString()); - customTlv.setType((byte) LLDPTLV.TLVType.Custom.getValue()) - .setLength((short) customValue.length).setValue(customValue); + customTlv.setType(LLDPTLV.TLVType.Custom.getValue()).setLength((short) customValue.length) + .setValue(customValue); // Create LLDP Custom Option list List customList = new ArrayList(); @@ -168,24 +216,21 @@ public class DiscoveryService implements IInventoryShimExternalListener, // Create discovery pkt LLDP discoveryPkt = new LLDP(); - discoveryPkt.setChassisId(chassisIdTlv).setPortId(portIdTlv).setTtl( - ttlTlv).setOptionalTLVList(customList); + discoveryPkt.setChassisId(chassisIdTlv).setPortId(portIdTlv).setTtl(ttlTlv).setOptionalTLVList(customList); RawPacket rawPkt = null; try { // Create ethernet pkt - byte[] sourceMac = getSouceMACFromNodeID(nodeId); + byte[] sourceMac = getSourceMACFromNodeID(nodeId); Ethernet ethPkt = new Ethernet(); - ethPkt.setSourceMACAddress(sourceMac).setDestinationMACAddress( - LLDP.LLDPMulticastMac).setEtherType( - EtherTypes.LLDP.shortValue()).setPayload(discoveryPkt); + ethPkt.setSourceMACAddress(sourceMac).setDestinationMACAddress(LLDP.LLDPMulticastMac) + .setEtherType(EtherTypes.LLDP.shortValue()).setPayload(discoveryPkt); byte[] data = ethPkt.serialize(); rawPkt = new RawPacket(data); rawPkt.setOutgoingNodeConnector(nodeConnector); } catch (ConstructionException cex) { - logger.warn("RawPacket creation caught exception {}", cex - .getMessage()); + logger.warn("RawPacket creation caught exception {}", cex.getMessage()); } catch (Exception e) { logger.error("Failed to serialize the LLDP packet: " + e); } @@ -193,8 +238,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, return rawPkt; } - private void sendDiscoveryPacket(NodeConnector nodeConnector, - RawPacket outPkt) { + private void sendDiscoveryPacket(NodeConnector nodeConnector, RawPacket outPkt) { if (nodeConnector == null) { logger.debug("Can not send discovery packet out since nodeConnector is null"); return; @@ -245,8 +289,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, return PacketResult.IGNORED; } - if (((Short) inPkt.getIncomingNodeConnector().getID()) - .equals(NodeConnector.SPECIALNODECONNECTORID)) { + if (((Short) inPkt.getIncomingNodeConnector().getID()).equals(NodeConnector.SPECIALNODECONNECTORID)) { logger.trace("Ignoring ethernet packet received on special port: " + inPkt.getIncomingNodeConnector().toString()); return PacketResult.IGNORED; @@ -256,31 +299,30 @@ public class DiscoveryService implements IInventoryShimExternalListener, try { ethPkt.deserialize(data, 0, data.length * NetUtils.NumBitsInAByte); } catch (Exception e) { - logger.warn("Failed to decode LLDP packet from {}: {}", - inPkt.getIncomingNodeConnector(), e); + logger.warn("Failed to decode LLDP packet from {}: {}", inPkt.getIncomingNodeConnector(), e); return PacketResult.IGNORED; } + if (ethPkt.getPayload() instanceof LLDP) { NodeConnector dst = inPkt.getIncomingNodeConnector(); - if (!processDiscoveryPacket(dst, ethPkt)) { - /* Snoop the discovery pkt if not generated from us */ - snoopDiscoveryPacket(dst, ethPkt); + if (isEnabled(dst)) { + if (!processDiscoveryPacket(dst, ethPkt)) { + // Snoop the discovery pkt if not generated from us + snoopDiscoveryPacket(dst, ethPkt); + } + return PacketResult.CONSUME; } - return PacketResult.CONSUME; } return PacketResult.IGNORED; } /* - * Snoop incoming discovery frames generated by the production network neighbor switch + * Snoop incoming discovery frames generated by the production network + * neighbor switch */ - private void snoopDiscoveryPacket(NodeConnector dstNodeConnector, - Ethernet ethPkt) { - if (!this.discoverySnoopingEnabled - || discoverySnoopingDisableList.contains(dstNodeConnector)) { - logger.trace( - "Discarded received discovery packet on {} since snooping is turned off", - dstNodeConnector); + private void snoopDiscoveryPacket(NodeConnector dstNodeConnector, Ethernet ethPkt) { + if (!this.discoverySnoopingEnabled || discoverySnoopingDisableList.contains(dstNodeConnector)) { + logger.trace("Discarded received discovery packet on {} since snooping is turned off", dstNodeConnector); return; } @@ -294,19 +336,19 @@ public class DiscoveryService implements IInventoryShimExternalListener, try { String nodeId = LLDPTLV.getHexStringValue(lldp.getChassisId().getValue(), lldp.getChassisId().getLength()); String portId = LLDPTLV.getStringValue(lldp.getPortId().getValue(), lldp.getPortId().getLength()); - byte[] systemNameBytes = null; - // get system name if present in the LLDP pkt - for (LLDPTLV lldptlv : lldp.getOptionalTLVList()) { - if (lldptlv.getType() == LLDPTLV.TLVType.SystemName.getValue()) { - systemNameBytes = lldptlv.getValue(); - break; - } - } - String nodeName = (systemNameBytes == null) ? nodeId : new String(systemNameBytes); - Node srcNode = new Node(Node.NodeIDType.PRODUCTION, nodeName); - NodeConnector srcNodeConnector = NodeConnectorCreator - .createNodeConnector(NodeConnector.NodeConnectorIDType.PRODUCTION, - portId, srcNode); + byte[] systemNameBytes = null; + // get system name if present in the LLDP pkt + for (LLDPTLV lldptlv : lldp.getOptionalTLVList()) { + if (lldptlv.getType() == LLDPTLV.TLVType.SystemName.getValue()) { + systemNameBytes = lldptlv.getValue(); + break; + } + } + String nodeName = (systemNameBytes == null) ? nodeId + : new String(systemNameBytes, Charset.defaultCharset()); + Node srcNode = new Node(Node.NodeIDType.PRODUCTION, nodeName); + NodeConnector srcNodeConnector = NodeConnectorCreator.createNodeConnector( + NodeConnector.NodeConnectorIDType.PRODUCTION, portId, srcNode); Edge edge = null; Set props = null; @@ -321,25 +363,22 @@ public class DiscoveryService implements IInventoryShimExternalListener, /* * Handle discovery frames generated by our controller + * * @return true if it's a success */ - private boolean processDiscoveryPacket(NodeConnector dstNodeConnector, - Ethernet ethPkt) { + private boolean processDiscoveryPacket(NodeConnector dstNodeConnector, Ethernet ethPkt) { if ((dstNodeConnector == null) || (ethPkt == null)) { - logger - .trace("Ignoring processing of discovery packet: Null node connector or packet"); + logger.trace("Ignoring processing of discovery packet: Null node connector or packet"); return false; } - logger.trace("Handle discovery packet {} from {}", ethPkt, - dstNodeConnector); + logger.trace("Handle discovery packet {} from {}", ethPkt, dstNodeConnector); LLDP lldp = (LLDP) ethPkt.getPayload(); List optionalTLVList = lldp.getOptionalTLVList(); if (optionalTLVList == null) { - logger.info("The discovery packet with null custom option from {}", - dstNodeConnector); + logger.info("The discovery packet with null custom option from {}", dstNodeConnector); return false; } @@ -347,26 +386,16 @@ public class DiscoveryService implements IInventoryShimExternalListener, NodeConnector srcNodeConnector = null; for (LLDPTLV lldptlv : lldp.getOptionalTLVList()) { if (lldptlv.getType() == LLDPTLV.TLVType.Custom.getValue()) { - String ncString = LLDPTLV.getCustomString(lldptlv.getValue(), lldptlv.getLength()); - srcNodeConnector = NodeConnector.fromString(ncString); - if (srcNodeConnector != null) { - srcNode = srcNodeConnector.getNode(); - /* Check if it's expected */ - if (isTracked(srcNodeConnector)) { - break; - } else { - srcNode = null; - srcNodeConnector = null; - } + String ncString = LLDPTLV.getCustomString(lldptlv.getValue(), lldptlv.getLength()); + srcNodeConnector = NodeConnector.fromString(ncString); + if (srcNodeConnector != null) { + srcNode = srcNodeConnector.getNode(); } } } if ((srcNode == null) || (srcNodeConnector == null)) { - logger - .trace( - "Received non-controller generated discovery packet from {}", - dstNodeConnector); + logger.trace("Received non-controller generated discovery packet from {}", dstNodeConnector); return false; } @@ -380,6 +409,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, logger.error("Caught exception ", e); } addEdge(edge, props); + pendingMap.put(dstNodeConnector, 0); logger.trace("Received discovery packet for Edge {}", edge); @@ -391,12 +421,11 @@ public class DiscoveryService implements IInventoryShimExternalListener, return null; } - if (pluginInInventoryService == null) { + if (inventoryProvider == null) { return null; } - Map> props = pluginInInventoryService - .getNodeConnectorProps(false); + Map> props = inventoryProvider.getNodeConnectorProps(false); if (props == null) { return null; } @@ -410,7 +439,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, return null; } - Property prop = (Property) propMap.get(propName); + Property prop = propMap.get(propName); return prop; } @@ -431,8 +460,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, Config config = (Config) getProp(nodeConnector, Config.ConfigPropName); State state = (State) getProp(nodeConnector, State.StatePropName); - return ((config != null) && (config.getValue() == Config.ADMIN_UP) - && (state != null) && (state.getValue() == State.EDGE_UP)); + return ((config != null) && (config.getValue() == Config.ADMIN_UP) && (state != null) && (state.getValue() == State.EDGE_UP)); } private boolean isTracked(NodeConnector nodeConnector) { @@ -501,14 +529,13 @@ public class DiscoveryService implements IInventoryShimExternalListener, private void addDiscovery(Node node) { Map switches = controller.getSwitches(); - ISwitch sw = switches.get((Long) node.getID()); + ISwitch sw = switches.get(node.getID()); List ports = sw.getEnabledPorts(); if (ports == null) { return; } for (OFPhysicalPort port : ports) { - NodeConnector nodeConnector = NodeConnectorCreator - .createOFNodeConnector(port.getPortNumber(), node); + NodeConnector nodeConnector = NodeConnectorCreator.createOFNodeConnector(port.getPortNumber(), node); if (!readyListHi.contains(nodeConnector)) { readyListHi.add(nodeConnector); } @@ -523,8 +550,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, readyListHi.add(nodeConnector); } - private Set getRemoveSet(Collection c, - Node node) { + private Set getRemoveSet(Collection c, Node node) { Set removeSet = new HashSet(); if (c == null) { return removeSet; @@ -577,18 +603,18 @@ public class DiscoveryService implements IInventoryShimExternalListener, private void checkTimeout() { Set removeSet = new HashSet(); Set retrySet = new HashSet(); - int sentCount; + int ticks; Set pendingSet = pendingMap.keySet(); if (pendingSet != null) { for (NodeConnector nodeConnector : pendingSet) { - sentCount = pendingMap.get(nodeConnector); - pendingMap.put(nodeConnector, ++sentCount); - if (sentCount > getDiscoveryFinalTimeoutInterval()) { + ticks = pendingMap.get(nodeConnector); + pendingMap.put(nodeConnector, ++ticks); + if (ticks > getDiscoveryFinalTimeoutInterval()) { // timeout the edge removeSet.add(nodeConnector); logger.trace("Discovery timeout {}", nodeConnector); - } else if (sentCount % discoveryTimeoutTicks == 0) { + } else if (ticks % discoveryTimeoutTicks == 0) { retrySet.add(nodeConnector); } } @@ -609,14 +635,14 @@ public class DiscoveryService implements IInventoryShimExternalListener, } Set removeSet = new HashSet(); - int sentCount; + int ticks; Set agingSet = agingMap.keySet(); if (agingSet != null) { for (NodeConnector nodeConnector : agingSet) { - sentCount = agingMap.get(nodeConnector); - agingMap.put(nodeConnector, ++sentCount); - if (sentCount > discoveryAgeoutTicks) { + ticks = agingMap.get(nodeConnector); + agingMap.put(nodeConnector, ++ticks); + if (ticks > discoveryAgeoutTicks) { // age out the edge removeSet.add(nodeConnector); logger.trace("Discovery age out {}", nodeConnector); @@ -632,14 +658,14 @@ public class DiscoveryService implements IInventoryShimExternalListener, private void doDiscovery() { if (++discoveryTimerTickCount <= discoveryBatchPauseTicks) { for (NodeConnector nodeConnector : getWorkingSet()) { - pendingMap.put(nodeConnector, 0); transmitQ.add(nodeConnector); } } else if (discoveryTimerTickCount >= discoveryBatchRestartTicks) { discoveryTimerTickCount = 0; for (NodeConnector nodeConnector : waitingList) { - if (!readyListLo.contains(nodeConnector)) + if (!readyListLo.contains(nodeConnector)) { readyListLo.add(nodeConnector); + } } waitingList.removeAll(readyListLo); } @@ -650,8 +676,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, return; } - if (++discoveryConsistencyCheckTickCount - % getDiscoveryConsistencyCheckInterval() != 0) { + if (++discoveryConsistencyCheckTickCount % getDiscoveryConsistencyCheckInterval() != 0) { return; } @@ -666,16 +691,14 @@ public class DiscoveryService implements IInventoryShimExternalListener, if (!isEnabled(nodeConnector)) { removeSet.add(nodeConnector); discoveryConsistencyCheckCorrected++; - logger.debug("ConsistencyChecker: remove disabled {}", - nodeConnector); + logger.debug("ConsistencyChecker: remove disabled {}", nodeConnector); continue; } if (!isTracked(nodeConnector)) { waitingList.add(nodeConnector); discoveryConsistencyCheckCorrected++; - logger.debug("ConsistencyChecker: add back untracked {}", - nodeConnector); + logger.debug("ConsistencyChecker: add back untracked {}", nodeConnector); continue; } } @@ -690,8 +713,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, if (!isEnabled(nodeConnector)) { removeSet.add(nodeConnector); discoveryConsistencyCheckCorrected++; - logger.debug("ConsistencyChecker: remove disabled {}", - nodeConnector); + logger.debug("ConsistencyChecker: remove disabled {}", nodeConnector); } } waitingList.removeAll(removeSet); @@ -701,13 +723,11 @@ public class DiscoveryService implements IInventoryShimExternalListener, for (ISwitch sw : switches.values()) { for (OFPhysicalPort port : sw.getEnabledPorts()) { Node node = NodeCreator.createOFNode(sw.getId()); - NodeConnector nodeConnector = NodeConnectorCreator - .createOFNodeConnector(port.getPortNumber(), node); + NodeConnector nodeConnector = NodeConnectorCreator.createOFNodeConnector(port.getPortNumber(), node); if (!isTracked(nodeConnector)) { waitingList.add(nodeConnector); discoveryConsistencyCheckCorrected++; - logger.debug("ConsistencyChecker: add back untracked {}", - nodeConnector); + logger.debug("ConsistencyChecker: add back untracked {}", nodeConnector); } } } @@ -719,60 +739,57 @@ public class DiscoveryService implements IInventoryShimExternalListener, } NodeConnector src = edge.getTailNodeConnector(); - if (!src.getType().equals( - NodeConnector.NodeConnectorIDType.PRODUCTION)) { + if (!src.getType().equals(NodeConnector.NodeConnectorIDType.PRODUCTION)) { pendingMap.remove(src); - if (!waitingList.contains(src)) { - waitingList.add(src); - } } else { NodeConnector dst = edge.getHeadNodeConnector(); agingMap.put(dst, 0); } - // notify routeEngine + // notify updateEdge(edge, UpdateType.ADDED, props); logger.trace("Add edge {}", edge); } - /** * Update Production Edge - * - * @param edge The Production Edge - * @param props Properties associated with the edge + * + * @param edge + * The Production Edge + * @param props + * Properties associated with the edge */ private void updateProdEdge(Edge edge, Set props) { - NodeConnector edgePort = edge.getHeadNodeConnector(); - - /* Do not update in case there is an existing OpenFlow link */ - if (edgeMap.get(edgePort) != null) { - logger.trace("Discarded edge {} since there is an existing OF link {}", - edge, edgeMap.get(edgePort)); - return; - } - - /* Look for any existing Production Edge */ - Edge oldEdge = prodMap.get(edgePort); - if (oldEdge == null) { - /* Let's add a new one */ - addEdge(edge, props); - } else if (!edge.equals(oldEdge)) { - /* Remove the old one first */ - removeProdEdge(oldEdge.getHeadNodeConnector()); - /* Then add the new one */ - addEdge(edge, props); - } else { - /* o/w, just reset the aging timer */ + NodeConnector edgePort = edge.getHeadNodeConnector(); + + /* Do not update in case there is an existing OpenFlow link */ + if (edgeMap.get(edgePort) != null) { + logger.trace("Discarded edge {} since there is an existing OF link {}", edge, edgeMap.get(edgePort)); + return; + } + + /* Look for any existing Production Edge */ + Edge oldEdge = prodMap.get(edgePort); + if (oldEdge == null) { + /* Let's add a new one */ + addEdge(edge, props); + } else if (!edge.equals(oldEdge)) { + /* Remove the old one first */ + removeProdEdge(oldEdge.getHeadNodeConnector()); + /* Then add the new one */ + addEdge(edge, props); + } else { + /* o/w, just reset the aging timer */ NodeConnector dst = edge.getHeadNodeConnector(); - agingMap.put(dst, 0); - } + agingMap.put(dst, 0); + } } /** * Remove Production Edge for a given edge port - * - * @param edgePort The OF edge port + * + * @param edgePort + * The OF edge port */ private void removeProdEdge(NodeConnector edgePort) { agingMap.remove(edgePort); @@ -785,8 +802,8 @@ public class DiscoveryService implements IInventoryShimExternalListener, } // notify Topology - if (this.discoveryService != null) { - this.discoveryService.notifyEdge(edge, UpdateType.REMOVED, null); + if (this.discoveryListener != null) { + this.discoveryListener.notifyEdge(edge, UpdateType.REMOVED, null); } logger.trace("Remove edge {}", edge); } @@ -817,8 +834,8 @@ public class DiscoveryService implements IInventoryShimExternalListener, } // notify Topology - if (this.discoveryService != null) { - this.discoveryService.notifyEdge(edge, UpdateType.REMOVED, null); + if (this.discoveryListener != null) { + this.discoveryListener.notifyEdge(edge, UpdateType.REMOVED, null); } logger.trace("Remove {}", nodeConnector); } @@ -828,20 +845,18 @@ public class DiscoveryService implements IInventoryShimExternalListener, } private void updateEdge(Edge edge, UpdateType type, Set props) { - if (discoveryService == null) { + if (discoveryListener == null) { return; } - this.discoveryService.notifyEdge(edge, type, props); + this.discoveryListener.notifyEdge(edge, type, props); - NodeConnector src = edge.getTailNodeConnector(), dst = edge - .getHeadNodeConnector(); - if (!src.getType().equals( - NodeConnector.NodeConnectorIDType.PRODUCTION)) { + NodeConnector src = edge.getTailNodeConnector(), dst = edge.getHeadNodeConnector(); + if (!src.getType().equals(NodeConnector.NodeConnectorIDType.PRODUCTION)) { if (type == UpdateType.ADDED) { - edgeMap.put(src, edge); + edgeMap.put(dst, edge); } else { - edgeMap.remove(src); + edgeMap.remove(dst); } } else { /* @@ -855,21 +870,18 @@ public class DiscoveryService implements IInventoryShimExternalListener, } } - private void moreToReadyListHi(NodeConnector nodeConnector) { + private void moveToReadyListHi(NodeConnector nodeConnector) { if (readyListLo.contains(nodeConnector)) { readyListLo.remove(nodeConnector); - readyListHi.add(nodeConnector); } else if (waitingList.contains(nodeConnector)) { waitingList.remove(nodeConnector); - readyListHi.add(nodeConnector); } + readyListHi.add(nodeConnector); } private void registerWithOSGIConsole() { - BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()) - .getBundleContext(); - bundleContext.registerService(CommandProvider.class.getName(), this, - null); + BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); + bundleContext.registerService(CommandProvider.class.getName(), this, null); } private int getDiscoveryConsistencyCheckInterval() { @@ -892,20 +904,20 @@ public class DiscoveryService implements IInventoryShimExternalListener, help.append("\t pcc - Print CC info\n"); help.append("\t psize - Print sizes of all the lists\n"); help.append("\t ptm - Print timeout info\n"); - help.append("\t ecc - Enable CC\n"); - help.append("\t dcc - Disable CC\n"); + help.append("\t ecc - Enable CC\n"); + help.append("\t dcc - Disable CC\n"); help.append("\t scc [multiple] - Set/show CC multiple and interval\n"); - help.append("\t sports [ports] - Set/show max ports per batch\n"); + help.append("\t sports [ports] - Set/show max ports per batch\n"); help.append("\t spause [ticks] - Set/show pause period\n"); - help.append("\t sdi [ticks] - Set/show discovery interval in ticks\n"); + help.append("\t sdi [ticks] - Set/show discovery interval in ticks\n"); help.append("\t stm [ticks] - Set/show per timeout ticks\n"); - help.append("\t sretry [count] - Set/show num of retries\n"); - help.append("\t addsw - Add a switch\n"); - help.append("\t remsw - Remove a switch\n"); + help.append("\t sretry [count] - Set/show num of retries\n"); + help.append("\t addsw - Add a switch\n"); + help.append("\t remsw - Remove a switch\n"); help.append("\t page - Print aging info\n"); help.append("\t sage - Set/Show aging time limit\n"); - help.append("\t eage - Enable aging\n"); - help.append("\t dage - Disable aging\n"); + help.append("\t eage - Enable aging\n"); + help.append("\t dage - Disable aging\n"); help.append("\t pthrot - Print throttling\n"); help.append("\t ethrot - Enable throttling\n"); help.append("\t dthrot - Disable throttling\n"); @@ -946,12 +958,10 @@ public class DiscoveryService implements IInventoryShimExternalListener, } public void _ppl(CommandInterpreter ci) { - ci.println("PendingList\n"); - for (NodeConnector nodeConnector : pendingMap.keySet()) { - if (nodeConnector == null) { - continue; - } - ci.println(nodeConnector); + ci.println("pendingMap\n"); + ci.println(" NodeConnector Last rx LLDP (s)"); + for (ConcurrentMap.Entry entry: pendingMap.entrySet()) { + ci.println(entry.getKey() + "\t\t" + entry.getValue()); } } @@ -967,8 +977,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, } ci.println("Interval " + getDiscoveryConsistencyCheckInterval()); ci.println("Multiple " + discoveryConsistencyCheckMultiple); - ci.println("Number of times called " - + discoveryConsistencyCheckCallingTimes); + ci.println("Number of times called " + discoveryConsistencyCheckCallingTimes); ci.println("Corrected count " + discoveryConsistencyCheckCorrected); } @@ -979,12 +988,10 @@ public class DiscoveryService implements IInventoryShimExternalListener, } public void _psize(CommandInterpreter ci) { - ci.println("readyListLo size " + readyListLo.size() + "\n" - + "readyListHi size " + readyListHi.size() + "\n" - + "waitingList size " + waitingList.size() + "\n" - + "pendingMap size " + pendingMap.size() + "\n" - + "edgeMap size " + edgeMap.size() + "\n" + "prodMap size " - + prodMap.size() + "\n" + "agingMap size " + agingMap.size()); + ci.println("readyListLo size " + readyListLo.size() + "\n" + "readyListHi size " + readyListHi.size() + "\n" + + "waitingList size " + waitingList.size() + "\n" + "pendingMap size " + pendingMap.size() + "\n" + + "edgeMap size " + edgeMap.size() + "\n" + "prodMap size " + prodMap.size() + "\n" + "agingMap size " + + agingMap.size()); } public void _page(CommandInterpreter ci) { @@ -1007,7 +1014,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, } } ci.println("\n"); - ci.println(" NodeConnector Edge "); + ci.println(" NodeConnector Edge "); Set keySet = prodMap.keySet(); if (keySet == null) { return; @@ -1021,8 +1028,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, public void _sage(CommandInterpreter ci) { String val = ci.nextArgument(); if (val == null) { - ci.println("Please enter aging time limit. Current value " - + this.discoveryAgeoutTicks); + ci.println("Please enter aging time limit. Current value " + this.discoveryAgeoutTicks); return; } try { @@ -1048,10 +1054,8 @@ public class DiscoveryService implements IInventoryShimExternalListener, public void _scc(CommandInterpreter ci) { String val = ci.nextArgument(); if (val == null) { - ci.println("Please enter CC multiple. Current multiple " - + discoveryConsistencyCheckMultiple + " (interval " - + getDiscoveryConsistencyCheckInterval() - + ") calling times " + ci.println("Please enter CC multiple. Current multiple " + discoveryConsistencyCheckMultiple + + " (interval " + getDiscoveryConsistencyCheckInterval() + ") calling times " + discoveryConsistencyCheckCallingTimes); return; } @@ -1081,7 +1085,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, } else { ci.println("Discovery snooping is globally disabled"); } - + ci.println("\nDiscovery snooping is locally disabled on these ports"); for (NodeConnector nodeConnector : discoverySnoopingDisableList) { ci.println(nodeConnector); @@ -1091,20 +1095,19 @@ public class DiscoveryService implements IInventoryShimExternalListener, public void _esnp(CommandInterpreter ci) { String val = ci.nextArgument(); - + if (val == null) { - ci.println("Usage: esnp "); + ci.println("Usage: esnp "); } else if (val.equalsIgnoreCase("all")) { this.discoverySnoopingEnabled = true; ci.println("Discovery snooping is globally enabled"); } else { NodeConnector nodeConnector = NodeConnector.fromString(val); if (nodeConnector != null) { - discoverySnoopingDisableList.remove(nodeConnector); - ci.println("Discovery snooping is locally enabled on port " - + nodeConnector); + discoverySnoopingDisableList.remove(nodeConnector); + ci.println("Discovery snooping is locally enabled on port " + nodeConnector); } else { - ci.println("Entered invalid NodeConnector " + val); + ci.println("Entered invalid NodeConnector " + val); } } return; @@ -1112,9 +1115,9 @@ public class DiscoveryService implements IInventoryShimExternalListener, public void _dsnp(CommandInterpreter ci) { String val = ci.nextArgument(); - + if (val == null) { - ci.println("Usage: dsnp "); + ci.println("Usage: dsnp "); } else if (val.equalsIgnoreCase("all")) { this.discoverySnoopingEnabled = false; ci.println("Discovery snooping is globally disabled"); @@ -1122,10 +1125,9 @@ public class DiscoveryService implements IInventoryShimExternalListener, NodeConnector nodeConnector = NodeConnector.fromString(val); if (nodeConnector != null) { discoverySnoopingDisableList.add(nodeConnector); - ci.println("Discovery snooping is locally disabled on port " - + nodeConnector); + ci.println("Discovery snooping is locally disabled on port " + nodeConnector); } else { - ci.println("Entered invalid NodeConnector " + val); + ci.println("Entered invalid NodeConnector " + val); } } return; @@ -1133,16 +1135,14 @@ public class DiscoveryService implements IInventoryShimExternalListener, public void _spause(CommandInterpreter ci) { String val = ci.nextArgument(); - String out = "Please enter pause period less than " - + discoveryBatchRestartTicks + ". Current pause period is " - + discoveryBatchPausePeriod + " pause tick is " - + discoveryBatchPauseTicks + "."; + String out = "Please enter pause period less than " + discoveryBatchRestartTicks + ". Current pause period is " + + discoveryBatchPausePeriod + " pause tick is " + discoveryBatchPauseTicks + "."; if (val != null) { try { int pause = Integer.parseInt(val); if (pause < discoveryBatchRestartTicks) { - discoveryBatchPausePeriod = pause; + discoveryBatchPausePeriod = pause; discoveryBatchPauseTicks = discoveryBatchRestartTicks - discoveryBatchPausePeriod; return; } @@ -1155,20 +1155,19 @@ public class DiscoveryService implements IInventoryShimExternalListener, public void _sdi(CommandInterpreter ci) { String val = ci.nextArgument(); - String out = "Please enter discovery interval greater than " - + discoveryBatchPausePeriod + ". Current value is " - + discoveryBatchRestartTicks + "."; + String out = "Please enter discovery interval greater than " + discoveryBatchPausePeriod + + ". Current value is " + discoveryBatchRestartTicks + "."; if (val != null) { - try { - int restart = Integer.parseInt(val); - if (restart > discoveryBatchPausePeriod) { - discoveryBatchRestartTicks = restart; - discoveryBatchPauseTicks = discoveryBatchRestartTicks - discoveryBatchPausePeriod; - return; - } - } catch (Exception e) { - } + try { + int restart = Integer.parseInt(val); + if (restart > discoveryBatchPausePeriod) { + discoveryBatchRestartTicks = restart; + discoveryBatchPauseTicks = discoveryBatchRestartTicks - discoveryBatchPausePeriod; + return; + } + } catch (Exception e) { + } } ci.println(out); } @@ -1176,8 +1175,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, public void _sports(CommandInterpreter ci) { String val = ci.nextArgument(); if (val == null) { - ci.println("Please enter max ports per batch. Current value is " - + discoveryBatchMaxPorts); + ci.println("Please enter max ports per batch. Current value is " + discoveryBatchMaxPorts); return; } try { @@ -1191,8 +1189,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, public void _sretry(CommandInterpreter ci) { String val = ci.nextArgument(); if (val == null) { - ci.println("Please enter number of retries. Current value is " - + discoveryRetry); + ci.println("Please enter number of retries. Current value is " + discoveryRetry); return; } try { @@ -1205,8 +1202,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, public void _stm(CommandInterpreter ci) { String val = ci.nextArgument(); - String out = "Please enter timeout tick value less than " - + discoveryBatchRestartTicks + ". Current value is " + String out = "Please enter timeout tick value less than " + discoveryBatchRestartTicks + ". Current value is " + discoveryTimeoutTicks; if (val != null) { try { @@ -1283,8 +1279,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, } @Override - public void updateNodeConnector(NodeConnector nodeConnector, - UpdateType type, Set props) { + public void updateNodeConnector(NodeConnector nodeConnector, UpdateType type, Set props) { Config config = null; State state = null; boolean enabled = false; @@ -1296,8 +1291,7 @@ public class DiscoveryService implements IInventoryShimExternalListener, state = (State) prop; } } - enabled = ((config != null) && (config.getValue() == Config.ADMIN_UP) - && (state != null) && (state.getValue() == State.EDGE_UP)); + enabled = ((config != null) && (config.getValue() == Config.ADMIN_UP) && (state != null) && (state.getValue() == State.EDGE_UP)); switch (type) { case ADDED: @@ -1327,22 +1321,21 @@ public class DiscoveryService implements IInventoryShimExternalListener, } public void addNode(Node node, Set props) { - if (node == null) + if (node == null) { return; + } addDiscovery(node); } public void removeNode(Node node) { - if (node == null) + if (node == null) { return; + } removeDiscovery(node); } - public void updateNode(Node node, Set props) { - } - void setController(IController s) { this.controller = s; } @@ -1353,12 +1346,12 @@ public class DiscoveryService implements IInventoryShimExternalListener, } } - public void setPluginInInventoryService(IPluginInInventoryService service) { - this.pluginInInventoryService = service; + public void setInventoryProvider(IInventoryProvider service) { + this.inventoryProvider = service; } - public void unsetPluginInInventoryService(IPluginInInventoryService service) { - this.pluginInInventoryService = null; + public void unsetInventoryProvider(IInventoryProvider service) { + this.inventoryProvider = null; } public void setIDataPacketMux(IDataPacketMux service) { @@ -1371,30 +1364,29 @@ public class DiscoveryService implements IInventoryShimExternalListener, } } - void setDiscoveryService(IDiscoveryService s) { - this.discoveryService = s; + void setDiscoveryListener(IDiscoveryListener s) { + this.discoveryListener = s; } - void unsetDiscoveryService(IDiscoveryService s) { - if (this.discoveryService == s) { - this.discoveryService = null; + void unsetDiscoveryListener(IDiscoveryListener s) { + if (this.discoveryListener == s) { + this.discoveryListener = null; } } private void initDiscoveryPacket() { // Create LLDP ChassisID TLV chassisIdTlv = new LLDPTLV(); - chassisIdTlv.setType((byte) LLDPTLV.TLVType.ChassisID.getValue()); + chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue()); // Create LLDP PortID TLV portIdTlv = new LLDPTLV(); - portIdTlv.setType((byte) LLDPTLV.TLVType.PortID.getValue()); + portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue()); // Create LLDP TTL TLV - byte[] ttl = new byte[] {(byte) 0, (byte) 120 }; + byte[] ttl = new byte[] { (byte) 0, (byte) 120 }; ttlTlv = new LLDPTLV(); - ttlTlv.setType((byte) LLDPTLV.TLVType.TTL.getValue()).setLength( - (short) ttl.length).setValue(ttl); + ttlTlv.setType(LLDPTLV.TLVType.TTL.getValue()).setLength((short) ttl.length).setValue(ttl); customTlv = new LLDPTLV(); } @@ -1454,14 +1446,12 @@ public class DiscoveryService implements IInventoryShimExternalListener, * */ void start() { - discoveryTimer.schedule(discoveryTimerTask, discoveryTimerTick, - discoveryTimerTick); + discoveryTimer.schedule(discoveryTimerTask, discoveryTimerTick, discoveryTimerTick); transmitThread.start(); } /** - * Function called after registering the - * service in OSGi service registry. + * Function called after registering the service in OSGi service registry. */ void started() { /* get a snapshot of all the existing switches */ @@ -1481,21 +1471,19 @@ public class DiscoveryService implements IInventoryShimExternalListener, } @Override - public void tagUpdated(String containerName, Node n, short oldTag, - short newTag, UpdateType t) { + public void tagUpdated(String containerName, Node n, short oldTag, short newTag, UpdateType t) { } @Override - public void containerFlowUpdated(String containerName, - ContainerFlow previousFlow, ContainerFlow currentFlow, UpdateType t) { + public void containerFlowUpdated(String containerName, ContainerFlow previousFlow, ContainerFlow currentFlow, + UpdateType t) { } @Override - public void nodeConnectorUpdated(String containerName, NodeConnector p, - UpdateType t) { + public void nodeConnectorUpdated(String containerName, NodeConnector p, UpdateType t) { switch (t) { case ADDED: - moreToReadyListHi(p); + moveToReadyListHi(p); break; default: break; @@ -1506,63 +1494,69 @@ public class DiscoveryService implements IInventoryShimExternalListener, public void containerModeUpdated(UpdateType t) { // do nothing } - - private byte[] getSouceMACFromNodeID(String nodeId) { + + private byte[] getSourceMACFromNodeID(String nodeId) { byte[] cid = HexEncode.bytesFromHexString(nodeId); byte[] sourceMac = new byte[6]; int pos = cid.length - sourceMac.length; if (pos >= 0) { - System.arraycopy(cid, pos, sourceMac, 0, sourceMac.length); + System.arraycopy(cid, pos, sourceMac, 0, sourceMac.length); } - + return sourceMac; } - - /** - * This method returns the interval which determines how often the discovery - * packets will be sent. Default is 300 seconds. - * - * @return The discovery interval in second - */ - private int getDiscoveryInterval() { - String elapsedTime = System.getProperty("of.discoveryInterval"); - int rv = 300; - try { - if (elapsedTime != null) { - rv = Integer.parseInt(elapsedTime); + private int getDiscoveryTicks(DiscoveryPeriod dp, String val) { + if (dp == null) { + return 0; + } + + if (val != null) { + try { + dp.setTime(Integer.parseInt(val)); + } catch (Exception e) { } - } catch (Exception e) { } - return rv; + return dp.getTick(); + } + + /** + * This method returns the interval which determines how often the discovery + * packets will be sent. + * + * @return The discovery interval in ticks + */ + private int getDiscoveryInterval() { + String intvl = System.getProperty("of.discoveryInterval"); + return getDiscoveryTicks(DiscoveryPeriod.INTERVAL, intvl); } /** * This method returns the timeout value in waiting for response of a - * discovery query. Default is 60 seconds. - * - * @return The discovery timeout in second + * discovery query. + * + * @return The discovery timeout in ticks */ private int getDiscoveryTimeout() { - String elapsedTime = System.getProperty("of.discoveryTimeout"); - int rv = 60; - - try { - if (elapsedTime != null) { - rv = Integer.parseInt(elapsedTime); - } - } catch (Exception e) { - } + String timeout = System.getProperty("of.discoveryTimeout"); + return getDiscoveryTicks(DiscoveryPeriod.TIMEOUT, timeout); + } - return rv; + /** + * This method returns the discovery entry aging time in ticks. + * + * @return The aging time in ticks + */ + private int getDiscoveryAgeout() { + return getDiscoveryTicks(DiscoveryPeriod.AGEOUT, null); } /** * This method returns the number of retries after the initial discovery * packet is not received within the timeout period. Default is 2 times. - * + * * @return The number of discovery retries */ private int getDiscoveryRetry() {