Fixed a bug in removeSpanPorts
[controller.git] / opendaylight / switchmanager / implementation / src / main / java / org / opendaylight / controller / switchmanager / internal / SwitchManagerImpl.java
index 8ed7e4627594195a783f9db236b713bb16c829d7..971213e483e567b4d4a6d0336ee613b85ceb25a8 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,10 +52,10 @@ 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;
-import org.opendaylight.controller.sal.utils.NodeCreator;
 import org.opendaylight.controller.sal.utils.ObjectReader;
 import org.opendaylight.controller.sal.utils.ObjectWriter;
 import org.opendaylight.controller.sal.utils.ServiceHelper;
@@ -82,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;
@@ -101,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;
@@ -117,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;
         }
@@ -185,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();
     }
@@ -315,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());
     }
@@ -345,6 +351,7 @@ public class SwitchManagerImpl implements ISwitchManager,
         return new ArrayList<SwitchConfig>(nodeConfigList.values());
     }
 
+    @Override
     public SwitchConfig getSwitchConfig(String switchId) {
         return nodeConfigList.get(switchId);
     }
@@ -352,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);
 
@@ -367,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>();
@@ -401,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());
         }
     }
@@ -455,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);
     }
@@ -715,7 +729,7 @@ public class SwitchManagerImpl implements ISwitchManager,
     }
 
     private void addNode(Node node, Set<Property> props) {
-        log.trace("{} added", node);
+        log.trace("{} added, props: {}", node, props);
         if (nodeProps == null) {
             return;
         }
@@ -766,8 +780,9 @@ public class SwitchManagerImpl implements ISwitchManager,
 
     private void removeNode(Node node) {
         log.trace("{} removed", node);
-        if (nodeProps == null)
+        if (nodeProps == null) {
             return;
+        }
         nodeProps.remove(node);
 
         // check if span ports need to be cleaned up
@@ -778,8 +793,9 @@ public class SwitchManagerImpl implements ISwitchManager,
     }
 
     private void updateNode(Node node, Set<Property> props) {
-        log.trace("{} updated", node);
-        if (nodeProps == null) {
+        log.trace("{} updated, props: {}", node, props);
+        if (nodeProps == null || !nodeProps.containsKey(node) ||
+                props == null || props.isEmpty()) {
             return;
         }
 
@@ -867,26 +883,11 @@ public class SwitchManagerImpl implements ISwitchManager,
                 : null;
     }
 
-    /*
-     * test utility function which assumes all nodes are OF nodes
-     */
-    private Node getNode(Long id) {
-        Set<Node> nodes = getNodes();
-        if (nodes != null) {
-            for (Node node : nodes) {
-                if (id.equals((Long) node.getID())) {
-                    return node;
-                }
-            }
-        }
-        return null;
-    }
-
     /*
      * Returns a copy of a list of properties for a given node
-     * 
+     *
      * (non-Javadoc)
-     * 
+     *
      * @see
      * org.opendaylight.controller.switchmanager.ISwitchManager#getNodeProps
      * (org.opendaylight.controller.sal.core.Node)
@@ -922,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);
@@ -947,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 (((Long) nodeConnector.getNode().getID()).longValue() != (Long) node
-                    .getID())
+            if (!nodeConnector.getNode().equals(node)) {
                 continue;
-            if (isNodeConnectorEnabled(nodeConnector))
+            }
+            if (isNodeConnectorEnabled(nodeConnector)) {
                 nodeConnectorSet.add(nodeConnector);
+            }
         }
 
         return nodeConnectorSet;
@@ -964,14 +968,15 @@ 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()) {
-            if (((Long) nodeConnector.getNode().getID()).longValue() != (Long) node
-                    .getID())
+            if (!nodeConnector.getNode().equals(node)) {
                 continue;
+            }
             nodeConnectorSet.add(nodeConnector);
         }
 
@@ -980,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()) {
@@ -995,24 +1001,6 @@ public class SwitchManagerImpl implements ISwitchManager,
         return nodeConnectorSet;
     }
 
-    /*
-     * testing utility function which assumes we are dealing with OF Node
-     * nodeconnectors only
-     */
-    @SuppressWarnings("unused")
-    private Set<Long> getEnabledNodeConnectorIds(Node node) {
-        Set<Long> ids = new HashSet<Long>();
-        Set<NodeConnector> nodeConnectors = getUpNodeConnectors(node);
-
-        if (nodeConnectors != null) {
-            for (NodeConnector nodeConnector : nodeConnectors) {
-                ids.add((Long) nodeConnector.getID());
-            }
-        }
-
-        return ids;
-    }
-
     @Override
     public Map<String, Property> getNodeConnectorProps(
             NodeConnector nodeConnector) {
@@ -1081,19 +1069,21 @@ 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);
     }
 
     /**
      * Adds a node connector and its property if any
-     * 
+     *
      * @param nodeConnector
      *            {@link org.opendaylight.controller.sal.core.NodeConnector}
      * @param propName
@@ -1137,7 +1127,7 @@ public class SwitchManagerImpl implements ISwitchManager,
 
     /**
      * Removes one property of a node connector
-     * 
+     *
      * @param nodeConnector
      *            {@link org.opendaylight.controller.sal.core.NodeConnector}
      * @param propName
@@ -1175,7 +1165,7 @@ public class SwitchManagerImpl implements ISwitchManager,
 
     /**
      * Removes all the properties of a node connector
-     * 
+     *
      * @param nodeConnector
      *            {@link org.opendaylight.controller.sal.core.NodeConnector}
      * @return success or failed reason
@@ -1202,7 +1192,7 @@ public class SwitchManagerImpl implements ISwitchManager,
     /**
      * Function called by the dependency manager when all the required
      * dependencies are satisfied
-     * 
+     *
      */
     void init(Component c) {
         Dictionary<?, ?> props = c.getServiceProperties();
@@ -1223,7 +1213,7 @@ public class SwitchManagerImpl implements ISwitchManager,
      * 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() {
         shutDown();
@@ -1232,7 +1222,7 @@ public class SwitchManagerImpl implements ISwitchManager,
     /**
      * Function called by dependency manager after "init ()" is called and after
      * the services provided by the class are registered in the service registry
-     * 
+     *
      */
     void start() {
         // OSGI console
@@ -1251,7 +1241,7 @@ public class SwitchManagerImpl implements ISwitchManager,
      * Function called by the dependency manager before the services exported by
      * the component are unregistered, this will be followed by a "destroy ()"
      * calls
-     * 
+     *
      */
     void stop() {
     }
@@ -1425,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);
@@ -1451,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;
         }
@@ -1464,9 +1455,13 @@ 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);
+            String macAddr = (mac == null) ? "" : HexEncode
+                    .bytesToHexStringFormat(mac.getMacAddress());
             int tierNum = (tier == null) ? 0 : tier.getValue();
-            ci.println(node + "            " + node.getType() + "            "
-                    + nodeName + "            " + tierNum);
+            ci.println(node + "     " + node.getType() + "     " + macAddr
+                    + "     " + nodeName + "     " + tierNum);
         }
         ci.println("Total number of Nodes: " + nodeSet.size());
     }
@@ -1477,9 +1472,13 @@ public class SwitchManagerImpl implements ISwitchManager,
             ci.println("Please enter node id");
             return;
         }
-        Long id = Long.decode(st);
 
-        Node node = NodeCreator.createOFNode(id);
+        Node node = Node.fromString(st);
+        if (node == null) {
+            ci.println("Please enter node id");
+            return;
+        }
+
         Set<NodeConnector> nodeConnectorSet = getUpNodeConnectors(node);
         if (nodeConnectorSet == null) {
             return;
@@ -1499,10 +1498,14 @@ public class SwitchManagerImpl implements ISwitchManager,
             ci.println("Please enter node id");
             return;
         }
-        Long id = Long.decode(st);
+
+        Node node = Node.fromString(st);
+        if (node == null) {
+            ci.println("Please enter node id");
+            return;
+        }
 
         ci.println("          NodeConnector               BandWidth(Gbps)     Admin     State");
-        Node node = NodeCreator.createOFNode(id);
         Set<NodeConnector> nodeConnectorSet = getNodeConnectors(node);
         if (nodeConnectorSet == null) {
             return;
@@ -1532,8 +1535,14 @@ public class SwitchManagerImpl implements ISwitchManager,
             ci.println("Please enter node id");
             return;
         }
-        Object id = Long.decode(st);
-        Switch sw = getSwitchByNode(NodeCreator.createOFNode((Long) id));
+
+        Node node = Node.fromString(st);
+        if (node == null) {
+            ci.println("Please enter node id");
+            return;
+        }
+
+        Switch sw = getSwitchByNode(node);
 
         ci.println("          NodeConnector                        Name");
         if (sw == null) {
@@ -1547,10 +1556,9 @@ public class SwitchManagerImpl implements ISwitchManager,
                 nodeConnectorName = (propMap == null) ? null : ((Name) propMap
                         .get(Name.NamePropName)).getValue();
                 if (nodeConnectorName != null) {
-                    Node node = nodeConnector.getNode();
-                    if (!node.equals(getNode((Long) id))) {
-                        log.debug("node not match {} {}", node,
-                                getNode((Long) id));
+                    Node nd = nodeConnector.getNode();
+                    if (!nd.equals(node)) {
+                        log.debug("node not match {} {}", nd, node);
                     }
                     Map<String, NodeConnector> map = nodeConnectorNames
                             .get(node);
@@ -1582,9 +1590,12 @@ public class SwitchManagerImpl implements ISwitchManager,
             ci.println("Please enter node id");
             return;
         }
-        Long id = Long.decode(st);
 
-        Node node = NodeCreator.createOFNode(id);
+        Node node = Node.fromString(st);
+        if (node == null) {
+            ci.println("Please enter node id");
+            return;
+        }
 
         st = ci.nextArgument();
         if (st == null) {
@@ -1602,17 +1613,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;
     }
 
@@ -1633,17 +1646,9 @@ 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) this.getNodeProp(node,
+                MacAddress.name);
+        return (mac != null) ? mac.getMacAddress() : null;
     }
 
     @Override
@@ -1694,7 +1699,7 @@ public class SwitchManagerImpl implements ISwitchManager,
         List<NodeConnector> ncLists = new ArrayList<NodeConnector>();
 
         for (NodeConnector nodeConnector : nodeConncetors) {
-            if (!spanNodeConnectors.contains(nodeConnector)) {
+            if (spanNodeConnectors.contains(nodeConnector)) {
                 ncLists.add(nodeConnector);
             }
         }
@@ -1738,7 +1743,7 @@ public class SwitchManagerImpl implements ISwitchManager,
     /**
      * Creates a Name/Tier/Bandwidth Property object based on given property
      * name and value. Other property types are not supported yet.
-     * 
+     *
      * @param propName
      *            Name of the Property
      * @param propValue