Node MAC address to be derived inside the protocol plugin 22/422/4
authorAlessandro Boch <aboch@cisco.com>
Mon, 3 Jun 2013 16:46:09 +0000 (09:46 -0700)
committerAlessandro Boch <aboch@cisco.com>
Mon, 3 Jun 2013 18:54:59 +0000 (11:54 -0700)
- Moved network node's MAC computation from switch mgr to InventoryShimService in protocol plugin
- Modified MAC property to only carry network node MAC
- Some automatic coding style changes
- Some static analysis fix

Change-Id:I8481176d3fa3fcc004f642ea865b7be3593bdfc4
Signed-off-by: Alessandro Boch <aboch@cisco.com>
opendaylight/northbound/switchmanager/src/main/java/org/opendaylight/controller/switchmanager/northbound/SwitchNorthbound.java
opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/internal/InventoryServiceShim.java
opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/MacAddress.java
opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Property.java
opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/Switch.java
opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerImpl.java
opendaylight/switchmanager/implementation/src/test/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerImplTest.java

index 7bdd3f322b4ab5d85dceb896f0d19dbca772f20f..e92583a33a9543c30c60b269ddc67a0c075c8561 100644 (file)
@@ -39,7 +39,6 @@ import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailab
 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
 import org.opendaylight.controller.sal.authorization.Privilege;
-import org.opendaylight.controller.sal.core.MacAddress;
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.core.NodeConnector;
 import org.opendaylight.controller.sal.core.Property;
@@ -51,7 +50,7 @@ import org.opendaylight.controller.switchmanager.ISwitchManager;
 /**
  * The class provides Northbound REST APIs to access the nodes, node connectors
  * and their properties.
- * 
+ *
  */
 
 @Path("/")
@@ -102,9 +101,9 @@ public class SwitchNorthbound {
     }
 
     /**
-     * 
+     *
      * Retrieve a list of all the nodes and their properties in the network
-     * 
+     *
      * @param containerName
      *            The container for which we want to retrieve the list
      * @return A list of Pair each pair represents a
@@ -129,7 +128,7 @@ public class SwitchNorthbound {
                             + containerName);
         }
 
-        ISwitchManager switchManager = (ISwitchManager) getIfSwitchManagerService(containerName);
+        ISwitchManager switchManager = getIfSwitchManagerService(containerName);
         if (switchManager == null) {
             throw new ServiceUnavailableException("Switch Manager "
                     + RestMessages.SERVICEUNAVAILABLE.toString());
@@ -141,7 +140,6 @@ public class SwitchNorthbound {
             return null;
         }
 
-        byte[] controllerMac = switchManager.getControllerMAC();
         for (Node node : nodes) {
             Map<String, Property> propMap = switchManager.getNodeProps(node);
             if (propMap == null) {
@@ -149,10 +147,6 @@ public class SwitchNorthbound {
             }
             Set<Property> props = new HashSet<Property>(propMap.values());
 
-            byte[] nodeMac = switchManager.getNodeMAC(node);
-            Property macAddr = new MacAddress(controllerMac, nodeMac);
-            props.add(macAddr);
-
             NodeProperties nodeProps = new NodeProperties(node, props);
             res.add(nodeProps);
         }
@@ -162,7 +156,7 @@ public class SwitchNorthbound {
 
     /**
      * Add a Name/Tier property to a node
-     * 
+     *
      * @param containerName
      *            Name of the Container
      * @param nodeType
@@ -204,7 +198,7 @@ public class SwitchNorthbound {
         }
         handleDefaultDisabled(containerName);
 
-        ISwitchManager switchManager = (ISwitchManager) getIfSwitchManagerService(containerName);
+        ISwitchManager switchManager = getIfSwitchManagerService(containerName);
         if (switchManager == null) {
             throw new ServiceUnavailableException("Switch Manager "
                     + RestMessages.SERVICEUNAVAILABLE.toString());
@@ -225,7 +219,7 @@ public class SwitchNorthbound {
 
     /**
      * Delete a property of a node
-     * 
+     *
      * @param containerName
      *            Name of the Container
      * @param nodeType
@@ -261,7 +255,7 @@ public class SwitchNorthbound {
         }
         handleDefaultDisabled(containerName);
 
-        ISwitchManager switchManager = (ISwitchManager) getIfSwitchManagerService(containerName);
+        ISwitchManager switchManager = getIfSwitchManagerService(containerName);
         if (switchManager == null) {
             throw new ServiceUnavailableException("Switch Manager "
                     + RestMessages.SERVICEUNAVAILABLE.toString());
@@ -278,10 +272,10 @@ public class SwitchNorthbound {
     }
 
     /**
-     * 
+     *
      * Retrieve a list of all the node connectors and their properties in a
      * given node
-     * 
+     *
      * @param containerName
      *            The container for which we want to retrieve the list
      * @param nodeType
@@ -315,7 +309,7 @@ public class SwitchNorthbound {
                             + containerName);
         }
 
-        ISwitchManager switchManager = (ISwitchManager) getIfSwitchManagerService(containerName);
+        ISwitchManager switchManager = getIfSwitchManagerService(containerName);
         if (switchManager == null) {
             throw new ServiceUnavailableException("Switch Manager "
                     + RestMessages.SERVICEUNAVAILABLE.toString());
@@ -347,7 +341,7 @@ public class SwitchNorthbound {
 
     /**
      * Add a Name/Bandwidth property to a node connector
-     * 
+     *
      * @param containerName
      *            Name of the Container
      * @param nodeType
@@ -396,7 +390,7 @@ public class SwitchNorthbound {
 
         handleDefaultDisabled(containerName);
 
-        ISwitchManager switchManager = (ISwitchManager) getIfSwitchManagerService(containerName);
+        ISwitchManager switchManager = getIfSwitchManagerService(containerName);
         if (switchManager == null) {
             throw new ServiceUnavailableException("Switch Manager "
                     + RestMessages.SERVICEUNAVAILABLE.toString());
@@ -425,7 +419,7 @@ public class SwitchNorthbound {
 
     /**
      * Delete a property of a node connector
-     * 
+     *
      * @param containerName
      *            Name of the Container
      * @param nodeType
@@ -469,7 +463,7 @@ public class SwitchNorthbound {
 
         handleDefaultDisabled(containerName);
 
-        ISwitchManager switchManager = (ISwitchManager) getIfSwitchManagerService(containerName);
+        ISwitchManager switchManager = getIfSwitchManagerService(containerName);
         if (switchManager == null) {
             throw new ServiceUnavailableException("Switch Manager "
                     + RestMessages.SERVICEUNAVAILABLE.toString());
@@ -492,7 +486,7 @@ public class SwitchNorthbound {
 
     /*    *//**
      * Retrieve a list of Span ports that were configured previously.
-     * 
+     *
      * @param containerName
      *            Name of the Container
      * @return list of
@@ -501,17 +495,17 @@ public class SwitchNorthbound {
      */
     /*
      * @Path("/span-config/{containerName}")
-     * 
+     *
      * @GET
-     * 
+     *
      * @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
-     * 
+     *
      * @StatusCodes( {
-     * 
+     *
      * @ResponseCode(code = 200, condition = "Operation successful"),
-     * 
+     *
      * @ResponseCode(code = 404, condition = "The containerName is not found"),
-     * 
+     *
      * @ResponseCode(code = 503, condition =
      * "One or more of Controller Services are unavailable") }) public
      * List<SpanConfig> getSpanConfigList(@PathParam("containerName") String
@@ -519,11 +513,11 @@ public class SwitchNorthbound {
      * getIfSwitchManagerService(containerName); if (switchManager == null) {
      * throw new ServiceUnavailableException("Switch Manager " +
      * RestMessages.SERVICEUNAVAILABLE.toString()); }
-     * 
+     *
      * return switchManager.getSpanConfigList(); }
      *//**
      * Add a span configuration
-     * 
+     *
      * @param containerName
      *            Name of the Container
      * @param config
@@ -533,34 +527,34 @@ public class SwitchNorthbound {
      */
     /*
      * @Path("/span-config/{containerName}")
-     * 
+     *
      * @PUT
-     * 
+     *
      * @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
-     * 
+     *
      * @StatusCodes( {
-     * 
+     *
      * @ResponseCode(code = 200, condition = "Operation successful"),
-     * 
+     *
      * @ResponseCode(code = 404, condition = "The containerName is not found"),
-     * 
+     *
      * @ResponseCode(code = 503, condition =
      * "One or more of Controller Services are unavailable") }) public Response
      * addSpanConfig(@PathParam("containerName") String containerName,
-     * 
+     *
      * @TypeHint(SubnetConfig.class) JAXBElement<SpanConfig> config) {
      * ISwitchManager switchManager = (ISwitchManager)
      * getIfSwitchManagerService(containerName); if (switchManager == null) {
      * throw new ServiceUnavailableException("Switch Manager " +
      * RestMessages.SERVICEUNAVAILABLE.toString()); }
-     * 
+     *
      * String ret = switchManager.addSpanConfig(config.getValue()); if
      * (ret.equals(ReturnString.SUCCESS.toString())) { return
      * Response.status(Response.Status.CREATED).build(); } throw new
      * InternalServerErrorException(ret); }
      *//**
      * Delete a span configuration
-     * 
+     *
      * @param containerName
      *            Name of the Container
      * @param config
@@ -570,27 +564,27 @@ public class SwitchNorthbound {
      */
     /*
      * @Path("/span-config/{containerName}")
-     * 
+     *
      * @DELETE
-     * 
+     *
      * @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
-     * 
+     *
      * @StatusCodes( {
-     * 
+     *
      * @ResponseCode(code = 200, condition = "Operation successful"),
-     * 
+     *
      * @ResponseCode(code = 404, condition = "The containerName is not found"),
-     * 
+     *
      * @ResponseCode(code = 503, condition =
      * "One or more of Controller Services are unavailable") }) public Response
      * deleteSpanConfig(@PathParam("containerName") String containerName,
-     * 
+     *
      * @TypeHint(SubnetConfig.class) JAXBElement<SpanConfig> config) {
      * ISwitchManager switchManager = (ISwitchManager)
      * getIfSwitchManagerService(containerName); if (switchManager == null) {
      * throw new ServiceUnavailableException("Switch Manager " +
      * RestMessages.SERVICEUNAVAILABLE.toString()); }
-     * 
+     *
      * String ret = switchManager.removeSpanConfig(config.getValue()); if
      * (ret.equals(ReturnString.SUCCESS.toString())) { return
      * Response.ok().build(); } throw new ResourceNotFoundException(ret); }
@@ -598,7 +592,7 @@ public class SwitchNorthbound {
 
     /**
      * Save the current switch configurations
-     * 
+     *
      * @param containerName
      *            Name of the Container
      * @return Response as dictated by the HTTP Response Status code
@@ -619,7 +613,7 @@ public class SwitchNorthbound {
                     "User is not authorized to perform this operation on container "
                             + containerName);
         }
-        ISwitchManager switchManager = (ISwitchManager) getIfSwitchManagerService(containerName);
+        ISwitchManager switchManager = getIfSwitchManagerService(containerName);
         if (switchManager == null) {
             throw new ServiceUnavailableException("Switch Manager "
                     + RestMessages.SERVICEUNAVAILABLE.toString());
index c472747a8e16eac2b421e17931f51a9d09f1b84f..f7210a333bd870d87216128b73cffb8a8402842f 100644 (file)
@@ -31,6 +31,7 @@ import org.opendaylight.controller.sal.core.ConstructionException;
 import org.opendaylight.controller.sal.core.ContainerFlow;
 import org.opendaylight.controller.sal.core.Description;
 import org.opendaylight.controller.sal.core.IContainerListener;
+import org.opendaylight.controller.sal.core.MacAddress;
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.core.Node.NodeIDType;
 import org.opendaylight.controller.sal.core.NodeConnector;
@@ -51,17 +52,17 @@ import org.slf4j.LoggerFactory;
  * The class describes a shim layer that bridges inventory events from Openflow
  * core to various listeners. The notifications are filtered based on container
  * configurations.
- * 
- * 
+ *
+ *
  */
 public class InventoryServiceShim implements IContainerListener,
         IMessageListener, ISwitchStateListener, IStatisticsListener {
     protected static final Logger logger = LoggerFactory
             .getLogger(InventoryServiceShim.class);
     private IController controller = null;
-    private ConcurrentMap<String, IInventoryShimInternalListener> inventoryShimInternalListeners = new ConcurrentHashMap<String, IInventoryShimInternalListener>();
-    private List<IInventoryShimExternalListener> inventoryShimExternalListeners = new CopyOnWriteArrayList<IInventoryShimExternalListener>();
-    private ConcurrentMap<NodeConnector, List<String>> containerMap = new ConcurrentHashMap<NodeConnector, List<String>>();
+    private final ConcurrentMap<String, IInventoryShimInternalListener> inventoryShimInternalListeners = new ConcurrentHashMap<String, IInventoryShimInternalListener>();
+    private final List<IInventoryShimExternalListener> inventoryShimExternalListeners = new CopyOnWriteArrayList<IInventoryShimExternalListener>();
+    private final ConcurrentMap<NodeConnector, List<String>> containerMap = new ConcurrentHashMap<NodeConnector, List<String>>();
 
     void setController(IController s) {
         this.controller = s;
@@ -133,7 +134,7 @@ public class InventoryServiceShim implements IContainerListener,
     /**
      * Function called by the dependency manager when all the required
      * dependencies are satisfied
-     * 
+     *
      */
     void init() {
         this.controller.addMessageListener(OFType.PORT_STATUS, this);
@@ -152,7 +153,7 @@ public class InventoryServiceShim implements IContainerListener,
      * Function called by the dependency manager when at least one dependency
      * become unsatisfied or when the component is shutting down because for
      * example bundle is being stopped.
-     * 
+     *
      */
     void destroy() {
         this.controller.removeMessageListener(OFType.PACKET_IN, this);
@@ -202,8 +203,9 @@ public class InventoryServiceShim implements IContainerListener,
 
     @Override
     public void switchAdded(ISwitch sw) {
-        if (sw == null)
+        if (sw == null) {
             return;
+        }
 
         // Add all the nodeConnectors of this switch
         Map<NodeConnector, Set<Property>> ncProps = InventoryServiceHelper
@@ -219,8 +221,9 @@ public class InventoryServiceShim implements IContainerListener,
 
     @Override
     public void switchDeleted(ISwitch sw) {
-        if (sw == null)
+        if (sw == null) {
             return;
+        }
 
         removeNode(sw);
     }
@@ -389,6 +392,7 @@ public class InventoryServiceShim implements IContainerListener,
         Long connectedSinceTime = (connectedSince == null) ? 0 : connectedSince
                 .getTime();
         props.add(new TimeStamp(connectedSinceTime, "connectedSince"));
+        props.add(new MacAddress(deriveMacAddress(sid)));
 
         byte tables = sw.getTables();
         Tables t = new Tables(tables);
@@ -410,6 +414,7 @@ public class InventoryServiceShim implements IContainerListener,
         if (b != null) {
             props.add(b);
         }
+
         // Notify all internal and external listeners
         notifyInventoryShimListener(node, type, props);
     }
@@ -447,15 +452,24 @@ public class InventoryServiceShim implements IContainerListener,
             logger.error("{}", e.getMessage());
             return;
         }
-        
+
         Set<Property> properties = new HashSet<Property>(1);
         Description desc = new Description(
                 descriptionStats.getDatapathDescription());
         properties.add(desc);
-        
+
         // Notify all internal and external listeners
         notifyInventoryShimListener(node, UpdateType.CHANGED, properties);
-    }  
+    }
+
+    private byte[] deriveMacAddress(long dpid) {
+        byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 
-   
+        for (short i = 0; i < 6; i++) {
+            mac[5 - i] = (byte) dpid;
+            dpid >>= 8;
+        }
+
+        return mac;
+    }
 }
index b4d1045bac2e461c4c408b6ae04e5531f07e4b53..d5fbdbc74371df64bbd204ed7965e156c21512da 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
@@ -19,86 +18,75 @@ import javax.xml.bind.annotation.XmlRootElement;
 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
 
 /**
- * The class contains the controller MAC address and node MAC address.
- *
- *
+ * The class contains MAC address property.
  */
 @XmlRootElement
 @XmlAccessorType(XmlAccessType.NONE)
-public class MacAddress extends Property {
+public class MacAddress extends Property implements Cloneable {
     private static final long serialVersionUID = 1L;
-    @XmlElement
-    private byte[] controllerMacAddress;
-    @XmlElement
-    private byte[] nodeMacAddress;
-    public static final String MacPropName = "macAddress";
+    @XmlElement(name="macAddress")
+    private final byte[] address;
+    public static final String name = "macAddress";
 
     /*
      * Private constructor used for JAXB mapping
      */
     private MacAddress() {
-        super(MacPropName);
-        this.controllerMacAddress = null;
-        this.nodeMacAddress = null;
-    }
-
-       /**
-        * Constructor to create DatalinkAddress property which contains the
-        * controller MAC address and node MAC address. The property will be
-        * attached to a {@link org.opendaylight.controller.sal.core.Node}.
-        * 
-        * @param controllerMacAddress Data Link Address for the controller
-        * @param nodeMacAddress Data Link Address for the node
-        * 
-        * @return the constructed object
-        */
-    public MacAddress(byte[] controllerMacAddress, byte[] nodeMacAddress) {
-        super(MacPropName);
-       
-        this.controllerMacAddress = controllerMacAddress;
-        this.nodeMacAddress = nodeMacAddress;
+        super(name);
+        this.address = null;
     }
 
     /**
-     * @return the controller MAC address
+     * Constructor to create DatalinkAddress property which contains the MAC
+     * address. The property will be attached to a
+     * {@link org.opendaylight.controller.sal.core.Node}.
+     *
+     *
+     * @param nodeMacAddress
+     *            Data Link Address for the node
+     *
+     * @return the constructed object
      */
-    public byte[] getControllerMacAddress() {
-        return this.controllerMacAddress;
+    public MacAddress(byte[] nodeMacAddress) {
+        super(name);
+        this.address = nodeMacAddress.clone();
     }
 
     /**
      * @return the node MAC address
      */
-    public byte[] getNodeMacAddress() {
-        return this.nodeMacAddress;
+    public byte[] getMacAddress() {
+        return this.address.clone();
     }
 
+    @Override
     public MacAddress clone() {
-       return new MacAddress(this.controllerMacAddress, this.nodeMacAddress);
+        return new MacAddress(this.address);
     }
 
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = super.hashCode();
-        result = prime * result + Arrays.hashCode(controllerMacAddress);
-        result = prime * result + Arrays.hashCode(nodeMacAddress);
+        result = prime * result + Arrays.hashCode(address);
         return result;
     }
 
     @Override
     public boolean equals(Object obj) {
-        if (this == obj)
+        if (this == obj) {
             return true;
-        if (!super.equals(obj))
+        }
+        if (!super.equals(obj)) {
             return false;
-        if (getClass() != obj.getClass())
+        }
+        if (getClass() != obj.getClass()) {
             return false;
+        }
         MacAddress other = (MacAddress) obj;
-        if (!Arrays.equals(controllerMacAddress, other.controllerMacAddress))
-            return false;
-        if (!Arrays.equals(nodeMacAddress, other.nodeMacAddress))
+        if (!Arrays.equals(address, other.address)) {
             return false;
+        }
         return true;
     }
 
index d50c81e8d79cd519968a441ac664766a8dc33fa1..97d3eac808845ef7e4c8627414c8ec9e2161898a 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
@@ -25,19 +24,18 @@ import javax.xml.bind.annotation.XmlSeeAlso;
  */
 
 /**
- * Abstract base class for a Property that can be attached to any sal
- * core element
- *
+ * Abstract base class for a Property that can be attached to any sal core
+ * element
  */
 @XmlRootElement
 @XmlSeeAlso({ Config.class, Name.class, State.class, TimeStamp.class,
-               Latency.class, Bandwidth.class, Tier.class, Actions.class,
-               AdvertisedBandwidth.class, Buffers.class, Capabilities.class,
-               MacAddress.class, PeerBandwidth.class, SupportedBandwidth.class,
-               Tables.class })
-abstract public class Property implements Serializable {
+    Latency.class, Bandwidth.class, Tier.class, Actions.class,
+    AdvertisedBandwidth.class, Buffers.class, Capabilities.class,
+    MacAddress.class, PeerBandwidth.class, SupportedBandwidth.class,
+    Tables.class })
+abstract public class Property implements Serializable, Cloneable {
     private static final long serialVersionUID = 1L;
-    private String name;
+    private final String name;
 
     /**
      * Private constructor used for JAXB mapping
@@ -58,10 +56,10 @@ abstract public class Property implements Serializable {
     /**
      * Used to copy the Property in a polymorphic way
      *
-     *
      * @return A clone of this Property
      */
-    abstract public Property clone();
+    @Override
+    public abstract Property clone();
 
     @Override
     public int hashCode() {
@@ -73,18 +71,23 @@ abstract public class Property implements Serializable {
 
     @Override
     public boolean equals(Object obj) {
-        if (this == obj)
+        if (this == obj) {
             return true;
-        if (obj == null)
+        }
+        if (obj == null) {
             return false;
-        if (getClass() != obj.getClass())
+        }
+        if (getClass() != obj.getClass()) {
             return false;
+        }
         Property other = (Property) obj;
         if (name == null) {
-            if (other.name != null)
+            if (other.name != null) {
                 return false;
-        } else if (!name.equals(other.name))
+            }
+        } else if (!name.equals(other.name)) {
             return false;
+        }
         return true;
     }
 
index 6ba78deaded8b817ee4fc382dc8560f1d3a74d10..eae01a6487cd5351783eb0e24292a3e947a9da7d 100644 (file)
@@ -28,7 +28,7 @@ public class Switch implements Serializable {
     private static final long serialVersionUID = 1L;
     private byte[] dataLayerAddress;
     private Set<NodeConnector> nodeConnectors;
-    private List<NodeConnector> spanPorts;
+    private final List<NodeConnector> spanPorts;
     private Node node;
 
     /*
@@ -44,7 +44,7 @@ public class Switch implements Serializable {
         this.node = node;
         this.nodeConnectors = new HashSet<NodeConnector>();
         this.spanPorts = new ArrayList<NodeConnector>(2);
-        this.dataLayerAddress = deriveMacAddress();
+        this.dataLayerAddress = null;
     }
 
     /**
@@ -98,18 +98,6 @@ public class Switch implements Serializable {
         this.node = node;
     }
 
-    private byte[] deriveMacAddress() {
-        long dpid = (Long) this.node.getID();
-        byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
-
-        for (short i = 0; i < 6; i++) {
-            mac[5 - i] = (byte) dpid;
-            dpid >>= 8;
-        }
-
-        return mac;
-    }
-
     public void addSpanPorts(List<NodeConnector> portList) {
         for (NodeConnector port : portList) {
             spanPorts.add(port);
@@ -137,30 +125,40 @@ public class Switch implements Serializable {
 
     @Override
     public boolean equals(Object obj) {
-        if (this == obj)
+        if (this == obj) {
             return true;
-        if (obj == null)
+        }
+        if (obj == null) {
             return false;
-        if (getClass() != obj.getClass())
+        }
+        if (getClass() != obj.getClass()) {
             return false;
+        }
         Switch other = (Switch) obj;
-        if (!Arrays.equals(dataLayerAddress, other.dataLayerAddress))
+        if (!Arrays.equals(dataLayerAddress, other.dataLayerAddress)) {
             return false;
+        }
         if (node == null) {
-            if (other.node != null)
+            if (other.node != null) {
                 return false;
-        } else if (!node.equals(other.node))
+            }
+        } else if (!node.equals(other.node)) {
             return false;
+        }
         if (nodeConnectors == null) {
-            if (other.nodeConnectors != null)
+            if (other.nodeConnectors != null) {
                 return false;
-        } else if (!nodeConnectors.equals(other.nodeConnectors))
+            }
+        } else if (!nodeConnectors.equals(other.nodeConnectors)) {
             return false;
+        }
         if (spanPorts == null) {
-            if (other.spanPorts != null)
+            if (other.spanPorts != null) {
                 return false;
-        } else if (!spanPorts.equals(other.spanPorts))
+            }
+        } else if (!spanPorts.equals(other.spanPorts)) {
             return false;
+        }
         return true;
     }
 
index d9333e9f824d1cdd7c6bd2dc579e9efb0cd009fb..2937fe7e14710c3655cf99e954bcc6cd4aefa05c 100644 (file)
@@ -41,6 +41,7 @@ import org.opendaylight.controller.configuration.IConfigurationContainerAware;
 import org.opendaylight.controller.sal.core.Bandwidth;
 import org.opendaylight.controller.sal.core.Config;
 import org.opendaylight.controller.sal.core.Description;
+import org.opendaylight.controller.sal.core.MacAddress;
 import org.opendaylight.controller.sal.core.Name;
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.core.NodeConnector;
@@ -51,6 +52,7 @@ import org.opendaylight.controller.sal.core.Tier;
 import org.opendaylight.controller.sal.core.UpdateType;
 import org.opendaylight.controller.sal.inventory.IInventoryService;
 import org.opendaylight.controller.sal.inventory.IListenInventoryUpdates;
+import org.opendaylight.controller.sal.utils.HexEncode;
 import org.opendaylight.controller.sal.utils.StatusCode;
 import org.opendaylight.controller.sal.utils.GlobalConstants;
 import org.opendaylight.controller.sal.utils.IObjectReader;
@@ -81,16 +83,16 @@ import org.slf4j.LoggerFactory;
  * are maintained in the default container only.
  */
 public class SwitchManagerImpl implements ISwitchManager,
-        IConfigurationContainerAware, IObjectReader,
-        ICacheUpdateAware<Long, String>, IListenInventoryUpdates,
-        CommandProvider {
+IConfigurationContainerAware, IObjectReader,
+ICacheUpdateAware<Long, String>, IListenInventoryUpdates,
+CommandProvider {
     private static Logger log = LoggerFactory
             .getLogger(SwitchManagerImpl.class);
     private static String ROOT = GlobalConstants.STARTUPHOME.toString();
     private static final String SAVE = "Save";
     private String subnetFileName = null, spanFileName = null,
             switchConfigFileName = null;
-    private List<NodeConnector> spanNodeConnectors = new CopyOnWriteArrayList<NodeConnector>();
+    private final List<NodeConnector> spanNodeConnectors = new CopyOnWriteArrayList<NodeConnector>();
     private ConcurrentMap<InetAddress, Subnet> subnets; // set of Subnets keyed by the InetAddress
     private ConcurrentMap<String, SubnetConfig> subnetsConfigList;
     private ConcurrentMap<Integer, SpanConfig> spanConfigList;
@@ -100,11 +102,11 @@ public class SwitchManagerImpl implements ISwitchManager,
     private ConcurrentMap<NodeConnector, Map<String, Property>> nodeConnectorProps; // properties are maintained in global container only
     private ConcurrentMap<Node, Map<String, NodeConnector>> nodeConnectorNames;
     private IInventoryService inventoryService;
-    private Set<ISwitchManagerAware> switchManagerAware = Collections
+    private final Set<ISwitchManagerAware> switchManagerAware = Collections
             .synchronizedSet(new HashSet<ISwitchManagerAware>());
-    private Set<IInventoryListener> inventoryListeners = Collections
+    private final Set<IInventoryListener> inventoryListeners = Collections
             .synchronizedSet(new HashSet<IInventoryListener>());
-    private Set<ISpanAware> spanAware = Collections
+    private final Set<ISpanAware> spanAware = Collections
             .synchronizedSet(new HashSet<ISpanAware>());
     private byte[] MAC;
     private static boolean hostRefresh = true;
@@ -116,14 +118,15 @@ public class SwitchManagerImpl implements ISwitchManager,
     public enum ReasonCode {
         SUCCESS("Success"), FAILURE("Failure"), INVALID_CONF(
                 "Invalid Configuration"), EXIST("Entry Already Exist"), CONFLICT(
-                "Configuration Conflict with Existing Entry");
+                        "Configuration Conflict with Existing Entry");
 
-        private String name;
+        private final String name;
 
         private ReasonCode(String name) {
             this.name = name;
         }
 
+        @Override
         public String toString() {
             return name;
         }
@@ -184,12 +187,15 @@ public class SwitchManagerImpl implements ISwitchManager,
          * Read startup and build database if we have not already gotten the
          * configurations synced from another node
          */
-        if (subnetsConfigList.isEmpty())
+        if (subnetsConfigList.isEmpty()) {
             loadSubnetConfiguration();
-        if (spanConfigList.isEmpty())
+        }
+        if (spanConfigList.isEmpty()) {
             loadSpanConfiguration();
-        if (nodeConfigList.isEmpty())
+        }
+        if (nodeConfigList.isEmpty()) {
             loadSwitchConfiguration();
+        }
 
         MAC = getHardwareMAC();
     }
@@ -314,12 +320,13 @@ public class SwitchManagerImpl implements ISwitchManager,
         clusterContainerService.destroyCache("switchmanager.configSaveEvent");
         clusterContainerService.destroyCache("switchmanager.nodeProps");
         clusterContainerService
-                .destroyCache("switchmanager.nodeConnectorProps");
+        .destroyCache("switchmanager.nodeConnectorProps");
         clusterContainerService
-                .destroyCache("switchmanager.nodeConnectorNames");
+        .destroyCache("switchmanager.nodeConnectorNames");
         nonClusterObjectCreate();
     }
 
+    @Override
     public List<SubnetConfig> getSubnetsConfigList() {
         return new ArrayList<SubnetConfig>(subnetsConfigList.values());
     }
@@ -344,6 +351,7 @@ public class SwitchManagerImpl implements ISwitchManager,
         return new ArrayList<SwitchConfig>(nodeConfigList.values());
     }
 
+    @Override
     public SwitchConfig getSwitchConfig(String switchId) {
         return nodeConfigList.get(switchId);
     }
@@ -351,7 +359,11 @@ public class SwitchManagerImpl implements ISwitchManager,
     public Switch getSwitchByNode(Node node) {
         Switch sw = new Switch(node);
         sw.setNode(node);
-
+        MacAddress mac = (MacAddress) this.getNodeProp(node,
+                MacAddress.name);
+        if (mac != null) {
+            sw.setDataLayerAddress(mac.getMacAddress());
+        }
         Set<NodeConnector> ncSet = getPhysicalNodeConnectors(node);
         sw.setNodeConnectors(ncSet);
 
@@ -366,6 +378,7 @@ public class SwitchManagerImpl implements ISwitchManager,
         return sw;
     }
 
+    @Override
     public List<Switch> getNetworkDevices() {
         Set<Node> nodeSet = getNodes();
         List<Switch> swList = new ArrayList<Switch>();
@@ -400,8 +413,9 @@ public class SwitchManagerImpl implements ISwitchManager,
             }
             subnets.put(conf.getIPnum(), subnet);
         } else { // This is the deletion of the whole subnet
-            if (subnet == null)
+            if (subnet == null) {
                 return;
+            }
             subnets.remove(conf.getIPnum());
         }
     }
@@ -454,6 +468,7 @@ public class SwitchManagerImpl implements ISwitchManager,
     /**
      * Adds Subnet configured in GUI or API3
      */
+    @Override
     public Status addSubnet(SubnetConfig conf) {
         return this.addRemoveSubnet(conf, true);
     }
@@ -908,8 +923,9 @@ public class SwitchManagerImpl implements ISwitchManager,
     public void setNodeProp(Node node, Property prop) {
         /* Get a copy of the property map */
         Map<String, Property> propMap = getNodeProps(node);
-        if (propMap == null)
+        if (propMap == null) {
             return;
+        }
 
         propMap.put(prop.getName(), prop);
         this.nodeProps.put(node, propMap);
@@ -933,16 +949,18 @@ public class SwitchManagerImpl implements ISwitchManager,
 
     @Override
     public Set<NodeConnector> getUpNodeConnectors(Node node) {
-        if (nodeConnectorProps == null)
+        if (nodeConnectorProps == null) {
             return null;
+        }
 
         Set<NodeConnector> nodeConnectorSet = new HashSet<NodeConnector>();
         for (NodeConnector nodeConnector : nodeConnectorProps.keySet()) {
             if (!nodeConnector.getNode().equals(node)) {
                 continue;
             }
-            if (isNodeConnectorEnabled(nodeConnector))
+            if (isNodeConnectorEnabled(nodeConnector)) {
                 nodeConnectorSet.add(nodeConnector);
+            }
         }
 
         return nodeConnectorSet;
@@ -950,8 +968,9 @@ public class SwitchManagerImpl implements ISwitchManager,
 
     @Override
     public Set<NodeConnector> getNodeConnectors(Node node) {
-        if (nodeConnectorProps == null)
+        if (nodeConnectorProps == null) {
             return null;
+        }
 
         Set<NodeConnector> nodeConnectorSet = new HashSet<NodeConnector>();
         for (NodeConnector nodeConnector : nodeConnectorProps.keySet()) {
@@ -966,8 +985,9 @@ public class SwitchManagerImpl implements ISwitchManager,
 
     @Override
     public Set<NodeConnector> getPhysicalNodeConnectors(Node node) {
-        if (nodeConnectorProps == null)
+        if (nodeConnectorProps == null) {
             return null;
+        }
 
         Set<NodeConnector> nodeConnectorSet = new HashSet<NodeConnector>();
         for (NodeConnector nodeConnector : nodeConnectorProps.keySet()) {
@@ -1049,12 +1069,14 @@ public class SwitchManagerImpl implements ISwitchManager,
 
     @Override
     public NodeConnector getNodeConnector(Node node, String nodeConnectorName) {
-        if (nodeConnectorNames == null)
+        if (nodeConnectorNames == null) {
             return null;
+        }
 
         Map<String, NodeConnector> map = nodeConnectorNames.get(node);
-        if (map == null)
+        if (map == null) {
             return null;
+        }
 
         return map.get(nodeConnectorName);
     }
@@ -1393,8 +1415,9 @@ public class SwitchManagerImpl implements ISwitchManager,
 
     @Override
     public Boolean isNodeConnectorEnabled(NodeConnector nodeConnector) {
-        if (nodeConnector == null)
+        if (nodeConnector == null) {
             return false;
+        }
 
         Config config = (Config) getNodeConnectorProp(nodeConnector,
                 Config.ConfigPropName);
@@ -1419,7 +1442,7 @@ public class SwitchManagerImpl implements ISwitchManager,
     }
 
     public void _pns(CommandInterpreter ci) {
-        ci.println("           Node                       Type             Name             Tier");
+        ci.println("           Node               Type           MAC            Name      Tier");
         if (nodeProps == null) {
             return;
         }
@@ -1432,9 +1455,12 @@ public class SwitchManagerImpl implements ISwitchManager,
                     Description.propertyName));
             Tier tier = ((Tier) getNodeProp(node, Tier.TierPropName));
             String nodeName = (desc == null) ? "" : desc.getValue();
+            MacAddress mac = (MacAddress) getNodeProp(node,
+                    MacAddress.name);
             int tierNum = (tier == null) ? 0 : tier.getValue();
-            ci.println(node + "            " + node.getType() + "            "
-                    + nodeName + "            " + tierNum);
+            ci.println(node + "     " + node.getType() + "     "
+                    + HexEncode.bytesToHexStringFormat(mac.getMacAddress())
+                    + "     " + nodeName + "     " + tierNum  );
         }
         ci.println("Total number of Nodes: " + nodeSet.size());
     }
@@ -1586,17 +1612,19 @@ public class SwitchManagerImpl implements ISwitchManager,
             ci.println("expecting on/off/?");
             return;
         }
-        if (mode.toLowerCase().equals("on"))
+        if (mode.toLowerCase().equals("on")) {
             hostRefresh = true;
-        else if (mode.toLowerCase().equals("off"))
+        } else if (mode.toLowerCase().equals("off")) {
             hostRefresh = false;
-        else if (mode.equals("?")) {
-            if (hostRefresh)
+        else if (mode.equals("?")) {
+            if (hostRefresh) {
                 ci.println("host refresh is ON");
-            else
+            } else {
                 ci.println("host refresh is OFF");
-        } else
+            }
+        } else {
             ci.println("expecting on/off/?");
+        }
         return;
     }
 
@@ -1617,17 +1645,8 @@ public class SwitchManagerImpl implements ISwitchManager,
 
     @Override
     public byte[] getNodeMAC(Node node) {
-        if (node.getType().equals(Node.NodeIDType.OPENFLOW)) {
-            byte[] gmac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
-            long dpid = (Long) node.getID();
-
-            for (short i = 0; i < 6; i++) {
-                gmac[5 - i] = (byte) dpid;
-                dpid >>= 8;
-            }
-            return gmac;
-        }
-        return null;
+        MacAddress mac = (MacAddress) nodeProps.get(MacAddress.name);
+        return (mac != null) ? mac.getMacAddress() : null;
     }
 
     @Override
index 8d869fc7f5dbadfb4b14ed9569e3386d34c21117..9bb231fb0398b1bcbf1fcaae96f606d596374676 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
@@ -29,81 +28,90 @@ import org.opendaylight.controller.switchmanager.SubnetConfig;
 
 public class SwitchManagerImplTest {
 
-       @Test
-       public void testSwitchManagerAddRemoveSubnet() {
-               SwitchManagerImpl switchmgr = new SwitchManagerImpl();
-               switchmgr.nonClusterObjectCreate();
-               
-               ArrayList<String>portList = new ArrayList<String>();
-               portList.add("1/1");
-               portList.add("1/2");
-               portList.add("1/3");
-
-               
-               SubnetConfig subnet = new SubnetConfig("subnet", "10.0.0.254/16", portList);
-               //System.out.println("*" + switchmgr.addSubnet(subnet) + "*");
-               Status addResult = (switchmgr.addSubnet(subnet));
-               Assert.assertTrue(addResult.isSuccess());
-               
-               Status removeResult = (switchmgr.removeSubnet(subnet.getName()));
-               Assert.assertTrue(removeResult.isSuccess());
-
-               SubnetConfig subnetConfigResult = switchmgr.getSubnetConfig(subnet.getName());
-               Assert.assertTrue(subnetConfigResult == null);
-               
-       }
-       
-       @Test
-       public void testSwitchManagerNodeConnectors() {
-               SwitchManagerImpl switchmgr = new SwitchManagerImpl();
-               switchmgr.nonClusterObjectCreate();
-               
-               State state;
-               Bandwidth bw;
-               Latency l;
-               
-               NodeConnector[] headnc = new NodeConnector[5];
-               NodeConnector[] tailnc = new NodeConnector[5];
-
-               Set<Property> props = new HashSet<Property>();
-               state = new State(State.EDGE_UP);
-               bw = new Bandwidth(Bandwidth.BW100Gbps);
-               l = new Latency(Latency.LATENCY100ns);
-               props.add(state);
-               props.add(bw);
-               props.add(l);
-               
-               for (short i = 1; i < 6; i = (short)(i + 1)) {
-
-                               headnc[i - 1] = NodeConnectorCreator.createOFNodeConnector(i, NodeCreator.createOFNode((long)i));
-                               tailnc[i - 1] = NodeConnectorCreator.createOFNodeConnector((short)(i+10), NodeCreator.createOFNode((long)(i+10)));
-                               switchmgr.updateNode(headnc[i - 1].getNode(), UpdateType.ADDED, props);
-                               switchmgr.updateNode(tailnc[i - 1].getNode(), UpdateType.ADDED, props);
-
-                               switchmgr.updateNodeConnector(headnc[i - 1], UpdateType.ADDED, props);
-                               switchmgr.updateNodeConnector(tailnc[i - 1], UpdateType.ADDED, props);
-               }
-               
-               for (int i = 0; i < 5; i++) {
-                       Property bwProp = switchmgr.getNodeConnectorProp(headnc[i], Bandwidth.BandwidthPropName);
-                       Assert.assertTrue(bwProp.equals(bw));
-                       Property latencyProp = switchmgr.getNodeConnectorProp(tailnc[i], Latency.LatencyPropName);
-                       Assert.assertEquals(latencyProp, l);
-                       
-                       byte[] headNodeMac = switchmgr.getNodeMAC(headnc[i].getNode());
-                       Assert.assertTrue(headNodeMac[headNodeMac.length - 1] == (byte)(i + 1));
-               }
-               
-               Set<Node> nodes = switchmgr.getNodes();
-               for (int i = 0; i < 5; i++) {
-                       if (nodes.contains(headnc[i].getNode()) == true) 
-                                       nodes.remove(headnc[i].getNode());
-                       
-                       if (nodes.contains(tailnc[i].getNode()) == true) 
-                                       nodes.remove(tailnc[i].getNode());
-                       
-               }
-               Assert.assertTrue(nodes.isEmpty());
-       }
-       
+    @Test
+    public void testSwitchManagerAddRemoveSubnet() {
+        SwitchManagerImpl switchmgr = new SwitchManagerImpl();
+        switchmgr.nonClusterObjectCreate();
+
+        ArrayList<String> portList = new ArrayList<String>();
+        portList.add("1/1");
+        portList.add("1/2");
+        portList.add("1/3");
+
+        SubnetConfig subnet = new SubnetConfig("subnet", "10.0.0.254/16",
+                portList);
+        // System.out.println("*" + switchmgr.addSubnet(subnet) + "*");
+        Status addResult = (switchmgr.addSubnet(subnet));
+        Assert.assertTrue(addResult.isSuccess());
+
+        Status removeResult = (switchmgr.removeSubnet(subnet.getName()));
+        Assert.assertTrue(removeResult.isSuccess());
+
+        SubnetConfig subnetConfigResult = switchmgr.getSubnetConfig(subnet
+                .getName());
+        Assert.assertTrue(subnetConfigResult == null);
+
+    }
+
+    @Test
+    public void testSwitchManagerNodeConnectors() {
+        SwitchManagerImpl switchmgr = new SwitchManagerImpl();
+        switchmgr.nonClusterObjectCreate();
+
+        State state;
+        Bandwidth bw;
+        Latency l;
+
+        NodeConnector[] headnc = new NodeConnector[5];
+        NodeConnector[] tailnc = new NodeConnector[5];
+
+        Set<Property> props = new HashSet<Property>();
+        state = new State(State.EDGE_UP);
+        bw = new Bandwidth(Bandwidth.BW100Gbps);
+        l = new Latency(Latency.LATENCY100ns);
+        props.add(state);
+        props.add(bw);
+        props.add(l);
+
+        for (short i = 1; i < 6; i = (short) (i + 1)) {
+
+            headnc[i - 1] = NodeConnectorCreator.createOFNodeConnector(i,
+                    NodeCreator.createOFNode((long) i));
+            tailnc[i - 1] = NodeConnectorCreator
+                    .createOFNodeConnector((short) (i + 10),
+                            NodeCreator.createOFNode((long) (i + 10)));
+            switchmgr.updateNode(headnc[i - 1].getNode(), UpdateType.ADDED,
+                    props);
+            switchmgr.updateNode(tailnc[i - 1].getNode(), UpdateType.ADDED,
+                    props);
+
+            switchmgr.updateNodeConnector(headnc[i - 1], UpdateType.ADDED,
+                    props);
+            switchmgr.updateNodeConnector(tailnc[i - 1], UpdateType.ADDED,
+                    props);
+        }
+
+        for (int i = 0; i < 5; i++) {
+            Property bwProp = switchmgr.getNodeConnectorProp(headnc[i],
+                    Bandwidth.BandwidthPropName);
+            Assert.assertTrue(bwProp.equals(bw));
+            Property latencyProp = switchmgr.getNodeConnectorProp(tailnc[i],
+                    Latency.LatencyPropName);
+            Assert.assertEquals(latencyProp, l);
+        }
+
+        Set<Node> nodes = switchmgr.getNodes();
+        for (int i = 0; i < 5; i++) {
+            if (nodes.contains(headnc[i].getNode()) == true) {
+                nodes.remove(headnc[i].getNode());
+            }
+
+            if (nodes.contains(tailnc[i].getNode()) == true) {
+                nodes.remove(tailnc[i].getNode());
+            }
+
+        }
+        Assert.assertTrue(nodes.isEmpty());
+    }
+
 }