X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fswitchmanager%2Fimplementation%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fswitchmanager%2Finternal%2FSwitchManagerImpl.java;h=d32f98650b8b549ce8235fd3c14d22c55205ac56;hb=7aac9c5fcb0f617c6b15e527f3c0f3d87032b83a;hp=4ef841643691bdb145c377c85199fa1635f49d29;hpb=a640c5c549376e5d72038e033d49ef6f0df96c92;p=controller.git diff --git a/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerImpl.java b/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerImpl.java index 4ef8416436..d32f98650b 100644 --- a/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerImpl.java +++ b/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerImpl.java @@ -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,14 +52,14 @@ 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.StatusCode; import org.opendaylight.controller.sal.utils.GlobalConstants; +import org.opendaylight.controller.sal.utils.HexEncode; 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; import org.opendaylight.controller.sal.utils.Status; +import org.opendaylight.controller.sal.utils.StatusCode; import org.opendaylight.controller.switchmanager.IInventoryListener; import org.opendaylight.controller.switchmanager.ISpanAware; import org.opendaylight.controller.switchmanager.ISwitchManager; @@ -82,16 +83,16 @@ import org.slf4j.LoggerFactory; * are maintained in the default container only. */ public class SwitchManagerImpl implements ISwitchManager, - IConfigurationContainerAware, IObjectReader, - ICacheUpdateAware, IListenInventoryUpdates, - CommandProvider { +IConfigurationContainerAware, IObjectReader, +ICacheUpdateAware, 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 spanNodeConnectors = new CopyOnWriteArrayList(); + private final List spanNodeConnectors = new CopyOnWriteArrayList(); private ConcurrentMap subnets; // set of Subnets keyed by the InetAddress private ConcurrentMap subnetsConfigList; private ConcurrentMap spanConfigList; @@ -101,11 +102,11 @@ public class SwitchManagerImpl implements ISwitchManager, private ConcurrentMap> nodeConnectorProps; // properties are maintained in global container only private ConcurrentMap> nodeConnectorNames; private IInventoryService inventoryService; - private Set switchManagerAware = Collections + private final Set switchManagerAware = Collections .synchronizedSet(new HashSet()); - private Set inventoryListeners = Collections + private final Set inventoryListeners = Collections .synchronizedSet(new HashSet()); - private Set spanAware = Collections + private final Set spanAware = Collections .synchronizedSet(new HashSet()); 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; } @@ -172,7 +174,7 @@ public class SwitchManagerImpl implements ISwitchManager, public void startUp() { // Initialize configuration file names - subnetFileName = ROOT + "subnets" + this.getContainerName() + ".conf"; + subnetFileName = ROOT + "subnets_" + this.getContainerName() + ".conf"; spanFileName = ROOT + "spanPorts_" + this.getContainerName() + ".conf"; switchConfigFileName = ROOT + "switchConfig_" + this.getContainerName() + ".conf"; @@ -185,18 +187,20 @@ 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(); } public void shutDown() { - destroyCaches(this.getContainerName()); } @SuppressWarnings("deprecation") @@ -315,12 +319,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 getSubnetsConfigList() { return new ArrayList(subnetsConfigList.values()); } @@ -345,6 +350,7 @@ public class SwitchManagerImpl implements ISwitchManager, return new ArrayList(nodeConfigList.values()); } + @Override public SwitchConfig getSwitchConfig(String switchId) { return nodeConfigList.get(switchId); } @@ -352,7 +358,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 ncSet = getPhysicalNodeConnectors(node); sw.setNodeConnectors(ncSet); @@ -367,6 +377,7 @@ public class SwitchManagerImpl implements ISwitchManager, return sw; } + @Override public List getNetworkDevices() { Set nodeSet = getNodes(); List swList = new ArrayList(); @@ -401,8 +412,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 +467,7 @@ public class SwitchManagerImpl implements ISwitchManager, /** * Adds Subnet configured in GUI or API3 */ + @Override public Status addSubnet(SubnetConfig conf) { return this.addRemoveSubnet(conf, true); } @@ -485,11 +498,13 @@ public class SwitchManagerImpl implements ISwitchManager, } conf.addNodeConnectors(switchPorts); + subnetsConfigList.put(name, conf); // Update Database Subnet sub = subnets.get(conf.getIPnum()); Set sp = conf.getNodeConnectors(switchPorts); sub.addNodeConnectors(sp); + subnets.put(conf.getIPnum(), sub); return new Status(StatusCode.SUCCESS, null); } @@ -501,11 +516,13 @@ public class SwitchManagerImpl implements ISwitchManager, return new Status(StatusCode.NOTFOUND, "Subnet does not exist"); } conf.removeNodeConnectors(switchPorts); + subnetsConfigList.put(name, conf); // Update Database Subnet sub = subnets.get(conf.getIPnum()); Set sp = conf.getNodeConnectors(switchPorts); sub.deleteNodeConnectors(sp); + subnets.put(conf.getIPnum(), sub); return new Status(StatusCode.SUCCESS, null); } @@ -540,7 +557,7 @@ public class SwitchManagerImpl implements ISwitchManager, @SuppressWarnings("unchecked") private void loadSubnetConfiguration() { ObjectReader objReader = new ObjectReader(); - ConcurrentMap confList = (ConcurrentMap) objReader + ConcurrentMap confList = (ConcurrentMap) objReader .read(this, subnetFileName); if (confList == null) { @@ -584,6 +601,11 @@ public class SwitchManagerImpl implements ISwitchManager, @Override public void updateSwitchConfig(SwitchConfig cfgObject) { + // update default container only + if (!isDefaultContainer) { + return; + } + boolean modeChange = false; SwitchConfig sc = nodeConfigList.get(cfgObject.getNodeId()); @@ -593,28 +615,25 @@ public class SwitchManagerImpl implements ISwitchManager, nodeConfigList.put(cfgObject.getNodeId(), cfgObject); try { - // update default container only - if (isDefaultContainer) { - String nodeId = cfgObject.getNodeId(); - Node node = Node.fromString(nodeId); - Map propMap; - if (nodeProps.get(node) != null) { - propMap = nodeProps.get(node); - } else { - propMap = new HashMap(); - } - Property desc = new Description(cfgObject.getNodeDescription()); - propMap.put(desc.getName(), desc); - Property tier = new Tier(Integer.parseInt(cfgObject.getTier())); - propMap.put(tier.getName(), tier); - addNodeProps(node, propMap); + String nodeId = cfgObject.getNodeId(); + Node node = Node.fromString(nodeId); + Map propMap; + if (nodeProps.get(node) != null) { + propMap = nodeProps.get(node); + } else { + propMap = new HashMap(); + } + Property desc = new Description(cfgObject.getNodeDescription()); + propMap.put(desc.getName(), desc); + Property tier = new Tier(Integer.parseInt(cfgObject.getTier())); + propMap.put(tier.getName(), tier); + addNodeProps(node, propMap); - log.info("Set Node {}'s Mode to {}", nodeId, - cfgObject.getMode()); + log.info("Set Node {}'s Mode to {}", nodeId, + cfgObject.getMode()); - if (modeChange) { - notifyModeChange(node, cfgObject.isProactive()); - } + if (modeChange) { + notifyModeChange(node, cfgObject.isProactive()); } } catch (Exception e) { log.debug("updateSwitchConfig: {}", e.getMessage()); @@ -806,6 +825,8 @@ public class SwitchManagerImpl implements ISwitchManager, @Override public void updateNode(Node node, UpdateType type, Set props) { + log.debug("updateNode: {} type {} props {} for container {}", + new Object[] { node, type, props, containerName }); switch (type) { case ADDED: addNode(node, props); @@ -827,7 +848,8 @@ public class SwitchManagerImpl implements ISwitchManager, Node node = nodeConnector.getNode(); Map propMap = new HashMap(); - log.trace("{} {}", nodeConnector, type); + log.debug("updateNodeConnector: {} type {} props {} for container {}", + new Object[] { nodeConnector, type, props, containerName }); if (nodeConnectorProps == null) { return; @@ -846,7 +868,6 @@ public class SwitchManagerImpl implements ISwitchManager, addNodeProps(node, null); } - // check if span is configed addSpanPort(nodeConnector); break; case REMOVED: @@ -869,26 +890,11 @@ public class SwitchManagerImpl implements ISwitchManager, : null; } - /* - * test utility function which assumes all nodes are OF nodes - */ - private Node getNode(Long id) { - Set 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) @@ -924,8 +930,9 @@ public class SwitchManagerImpl implements ISwitchManager, public void setNodeProp(Node node, Property prop) { /* Get a copy of the property map */ Map propMap = getNodeProps(node); - if (propMap == null) + if (propMap == null) { return; + } propMap.put(prop.getName(), prop); this.nodeProps.put(node, propMap); @@ -949,16 +956,18 @@ public class SwitchManagerImpl implements ISwitchManager, @Override public Set getUpNodeConnectors(Node node) { - if (nodeConnectorProps == null) + if (nodeConnectorProps == null) { return null; + } Set nodeConnectorSet = new HashSet(); 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; @@ -966,14 +975,15 @@ public class SwitchManagerImpl implements ISwitchManager, @Override public Set getNodeConnectors(Node node) { - if (nodeConnectorProps == null) + if (nodeConnectorProps == null) { return null; + } Set nodeConnectorSet = new HashSet(); for (NodeConnector nodeConnector : nodeConnectorProps.keySet()) { - if (((Long) nodeConnector.getNode().getID()).longValue() != (Long) node - .getID()) + if (!nodeConnector.getNode().equals(node)) { continue; + } nodeConnectorSet.add(nodeConnector); } @@ -982,8 +992,9 @@ public class SwitchManagerImpl implements ISwitchManager, @Override public Set getPhysicalNodeConnectors(Node node) { - if (nodeConnectorProps == null) + if (nodeConnectorProps == null) { return null; + } Set nodeConnectorSet = new HashSet(); for (NodeConnector nodeConnector : nodeConnectorProps.keySet()) { @@ -997,24 +1008,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 getEnabledNodeConnectorIds(Node node) { - Set ids = new HashSet(); - Set nodeConnectors = getUpNodeConnectors(node); - - if (nodeConnectors != null) { - for (NodeConnector nodeConnector : nodeConnectors) { - ids.add((Long) nodeConnector.getID()); - } - } - - return ids; - } - @Override public Map getNodeConnectorProps( NodeConnector nodeConnector) { @@ -1083,19 +1076,21 @@ public class SwitchManagerImpl implements ISwitchManager, @Override public NodeConnector getNodeConnector(Node node, String nodeConnectorName) { - if (nodeConnectorNames == null) + if (nodeConnectorNames == null) { return null; + } Map 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 @@ -1139,7 +1134,7 @@ public class SwitchManagerImpl implements ISwitchManager, /** * Removes one property of a node connector - * + * * @param nodeConnector * {@link org.opendaylight.controller.sal.core.NodeConnector} * @param propName @@ -1177,7 +1172,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 @@ -1204,7 +1199,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(); @@ -1225,7 +1220,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(); @@ -1234,7 +1229,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 @@ -1253,7 +1248,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() { } @@ -1343,15 +1338,26 @@ public class SwitchManagerImpl implements ISwitchManager, return; } - nodeProps = this.inventoryService.getNodeProps(); - Set nodeSet = nodeProps.keySet(); - if (nodeSet != null) { - for (Node node : nodeSet) { - addNode(node, null); + Map> nodeProp = this.inventoryService.getNodeProps(); + for(Map.Entry> entry : nodeProp.entrySet()) { + Node node = entry.getKey(); + log.debug("getInventories: {} added for container {}", + new Object[] { node, containerName }); + Map propMap = entry.getValue(); + Set props = new HashSet(); + for(Property property : propMap.values()) { + props.add(property); } + addNode(node, props); } - nodeConnectorProps = inventoryService.getNodeConnectorProps(); + Map> nodeConnectorProp = this.inventoryService.getNodeConnectorProps(); + for(Map.Entry> entry : nodeConnectorProp.entrySet()) { + Map propMap = entry.getValue(); + for(Property property : propMap.values()) { + addNodeConnectorProp(entry.getKey(), property); + } + } } private void clearInventories() { @@ -1396,17 +1402,17 @@ public class SwitchManagerImpl implements ISwitchManager, } private void bulkUpdateService(IInventoryListener service) { + Map propMap; + UpdateType type = UpdateType.ADDED; + for (Node node : getNodes()) { - service.notifyNode(node, UpdateType.ADDED, null); + propMap = nodeProps.get(node); + service.notifyNode(node, type, propMap); } - Map propMap = new HashMap(); - propMap.put(State.StatePropName, new State(State.EDGE_UP)); for (NodeConnector nodeConnector : nodeConnectorProps.keySet()) { - if (isNodeConnectorEnabled(nodeConnector)) { - service.notifyNodeConnector(nodeConnector, UpdateType.ADDED, - propMap); - } + propMap = nodeConnectorProps.get(nodeConnector); + service.notifyNodeConnector(nodeConnector, type, propMap); } } @@ -1427,8 +1433,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); @@ -1453,7 +1460,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; } @@ -1466,9 +1473,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()); } @@ -1620,17 +1631,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; } @@ -1651,17 +1664,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 @@ -1678,10 +1683,10 @@ public class SwitchManagerImpl implements ISwitchManager, /* * Add span configuration to local cache and notify clients */ - private void addSpanPorts(Node node, List nodeConncetors) { + private void addSpanPorts(Node node, List nodeConnectors) { List ncLists = new ArrayList(); - for (NodeConnector nodeConnector : nodeConncetors) { + for (NodeConnector nodeConnector : nodeConnectors) { if (!spanNodeConnectors.contains(nodeConnector)) { ncLists.add(nodeConnector); } @@ -1699,20 +1704,26 @@ public class SwitchManagerImpl implements ISwitchManager, } } - private void addSpanPort(NodeConnector nodeConncetor) { - List ncLists = new ArrayList(); - ncLists.add(nodeConncetor); - addSpanPorts(nodeConncetor.getNode(), ncLists); + private void addSpanPort(NodeConnector nodeConnector) { + // only add if span is configured on this nodeConnector + for (SpanConfig conf : getSpanConfigList(nodeConnector.getNode())) { + if (conf.getPortArrayList().contains(nodeConnector)) { + List ncLists = new ArrayList(); + ncLists.add(nodeConnector); + addSpanPorts(nodeConnector.getNode(), ncLists); + return; + } + } } /* * Remove span configuration to local cache and notify clients */ - private void removeSpanPorts(Node node, List nodeConncetors) { + private void removeSpanPorts(Node node, List nodeConnectors) { List ncLists = new ArrayList(); - for (NodeConnector nodeConnector : nodeConncetors) { - if (!spanNodeConnectors.contains(nodeConnector)) { + for (NodeConnector nodeConnector : nodeConnectors) { + if (spanNodeConnectors.contains(nodeConnector)) { ncLists.add(nodeConnector); } } @@ -1729,10 +1740,12 @@ public class SwitchManagerImpl implements ISwitchManager, } } - private void removeSpanPort(NodeConnector nodeConncetor) { - List ncLists = new ArrayList(); - ncLists.add(nodeConncetor); - removeSpanPorts(nodeConncetor.getNode(), ncLists); + private void removeSpanPort(NodeConnector nodeConnector) { + if (spanNodeConnectors.contains(nodeConnector)) { + List ncLists = new ArrayList(); + ncLists.add(nodeConnector); + removeSpanPorts(nodeConnector.getNode(), ncLists); + } } private void addNodeProps(Node node, Map propMap) { @@ -1756,7 +1769,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