X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fswitchmanager%2Fimplementation%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fswitchmanager%2Finternal%2FSwitchManager.java;h=8372f88e7dce895e135debf1daef17e18d02b0f1;hp=f3abd26c6825a8db1e0e832ddd64a531dd86b711;hb=e18b0fd6a43fb909f5548e6a9130ddf7654beef0;hpb=1ff92eee342d5c5e12db620a4bb6a5a8701aa924 diff --git a/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManager.java b/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManager.java index f3abd26c68..8372f88e7d 100644 --- a/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManager.java +++ b/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManager.java @@ -12,13 +12,10 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; import java.net.InetAddress; -import java.net.NetworkInterface; -import java.net.SocketException; import java.util.ArrayList; import java.util.Collections; import java.util.Dictionary; import java.util.EnumSet; -import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -35,7 +32,9 @@ import org.opendaylight.controller.clustering.services.CacheConfigException; import org.opendaylight.controller.clustering.services.CacheExistException; import org.opendaylight.controller.clustering.services.IClusterContainerServices; import org.opendaylight.controller.clustering.services.IClusterServices; +import org.opendaylight.controller.configuration.ConfigurationObject; import org.opendaylight.controller.configuration.IConfigurationContainerAware; +import org.opendaylight.controller.configuration.IConfigurationContainerService; import org.opendaylight.controller.sal.core.Bandwidth; import org.opendaylight.controller.sal.core.Config; import org.opendaylight.controller.sal.core.ConstructionException; @@ -54,10 +53,8 @@ import org.opendaylight.controller.sal.inventory.IInventoryService; import org.opendaylight.controller.sal.inventory.IListenInventoryUpdates; import org.opendaylight.controller.sal.reader.NodeDescription; 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.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.statisticsmanager.IStatisticsManager; @@ -86,8 +83,9 @@ import org.slf4j.LoggerFactory; public class SwitchManager implements ISwitchManager, IConfigurationContainerAware, IObjectReader, IListenInventoryUpdates, CommandProvider { private static Logger log = LoggerFactory.getLogger(SwitchManager.class); - private static String ROOT = GlobalConstants.STARTUPHOME.toString(); - private String subnetFileName, spanFileName, switchConfigFileName; + private static final String SUBNETS_FILE_NAME = "subnets.conf"; + private static final String SPAN_FILE_NAME = "spanPorts.conf"; + private static final String SWITCH_CONFIG_FILE_NAME = "switchConfig.conf"; private final List spanNodeConnectors = new CopyOnWriteArrayList(); // Collection of Subnets keyed by the InetAddress private ConcurrentMap subnets; @@ -101,6 +99,8 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa private ConcurrentMap controllerProps; private IInventoryService inventoryService; private IStatisticsManager statisticsManager; + private IControllerProperties controllerProperties; + private IConfigurationContainerService configurationService; private final Set switchManagerAware = Collections .synchronizedSet(new HashSet()); private final Set inventoryListeners = Collections @@ -116,9 +116,12 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa * only subnet returned. As soon as a user-configured subnet is created this one will * vanish. */ - protected static SubnetConfig DEFAULT_SUBNETCONFIG; - protected static Subnet DEFAULT_SUBNET; - protected static String DEFAULT_SUBNET_NAME = "default (cannot be modifed)"; + private static final String DISABLE_DEFAULT_SUBNET_PROP = "switchmanager.disableDefaultSubnetGateway"; + private static final String DISABLE_DEFAULT_SUBNET_PROP_VAL = System.getProperty(DISABLE_DEFAULT_SUBNET_PROP); + private static final boolean USE_DEFAULT_SUBNET_GW = !Boolean.valueOf(DISABLE_DEFAULT_SUBNET_PROP_VAL); + protected static final SubnetConfig DEFAULT_SUBNETCONFIG; + protected static final Subnet DEFAULT_SUBNET; + protected static final String DEFAULT_SUBNET_NAME = "default (cannot be modifed)"; static{ DEFAULT_SUBNETCONFIG = new SubnetConfig(DEFAULT_SUBNET_NAME, "0.0.0.0/0", new ArrayList()); DEFAULT_SUBNET = new Subnet(DEFAULT_SUBNETCONFIG); @@ -164,38 +167,18 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa } public void startUp() { - String container = this.getContainerName(); - // Initialize configuration file names - subnetFileName = ROOT + "subnets_" + container + ".conf"; - spanFileName = ROOT + "spanPorts_" + container + ".conf"; - switchConfigFileName = ROOT + "switchConfig_" + container + ".conf"; - // Instantiate cluster synced variables allocateCaches(); retrieveCaches(); - /* - * Read startup and build database if we have not already gotten the - * configurations synced from another node - */ - if (subnetsConfigList.isEmpty()) { - loadSubnetConfiguration(); - } - if (spanConfigList.isEmpty()) { - loadSpanConfiguration(); - } - if (nodeConfigList.isEmpty()) { - loadSwitchConfiguration(); - } - // Add controller MAC, if first node in the cluster - if (!controllerProps.containsKey(MacAddress.name)) { - byte controllerMac[] = getHardwareMAC(); + if ((!controllerProps.containsKey(MacAddress.name)) && (controllerProperties != null)) { + Property controllerMac = controllerProperties.getControllerProperty(MacAddress.name); if (controllerMac != null) { - Property existing = controllerProps.putIfAbsent(MacAddress.name, new MacAddress(controllerMac)); + Property existing = controllerProps.putIfAbsent(MacAddress.name, controllerMac); if (existing == null && log.isTraceEnabled()) { - log.trace("Container {}: Setting controller MAC address in the cluster: {}", container, - HexEncode.bytesToHexStringFormat(controllerMac)); + log.trace("Container {}: Setting controller MAC address in the cluster: {}", getContainerName(), + controllerMac); } } } @@ -242,7 +225,7 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa @SuppressWarnings({ "unchecked" }) private void retrieveCaches() { if (this.clusterContainerService == null) { - log.info("un-initialized clusterContainerService, can't create cache"); + log.warn("un-initialized clusterContainerService, can't create cache"); return; } @@ -309,9 +292,9 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa @Override public List getSubnetsConfigList() { // if there are no subnets, return the default subnet - if(subnetsConfigList.size() == 0){ + if (USE_DEFAULT_SUBNET_GW && subnetsConfigList.isEmpty()) { return Collections.singletonList(DEFAULT_SUBNETCONFIG); - }else{ + } else { return new ArrayList(subnetsConfigList.values()); } } @@ -319,9 +302,9 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa @Override public SubnetConfig getSubnetConfig(String subnet) { // if there are no subnets, return the default subnet - if(subnetsConfigList.isEmpty() && subnet.equalsIgnoreCase(DEFAULT_SUBNET_NAME)){ + if (USE_DEFAULT_SUBNET_GW && subnetsConfigList.isEmpty() && subnet.equalsIgnoreCase(DEFAULT_SUBNET_NAME)) { return DEFAULT_SUBNETCONFIG; - }else{ + } else { return subnetsConfigList.get(subnet); } } @@ -370,14 +353,10 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa @Override public List getNetworkDevices() { - Set nodeSet = getNodes(); List swList = new ArrayList(); - if (nodeSet != null) { - for (Node node : nodeSet) { - swList.add(getSwitchByNode(node)); - } + for (Node node : getNodes()) { + swList.add(getSwitchByNode(node)); } - return swList; } @@ -707,48 +686,21 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa return ois.readObject(); } - @SuppressWarnings("unchecked") private void loadSubnetConfiguration() { - ObjectReader objReader = new ObjectReader(); - ConcurrentMap confList = (ConcurrentMap) objReader - .read(this, subnetFileName); - - if (confList == null) { - return; - } - - for (SubnetConfig conf : confList.values()) { - addSubnet(conf); + for (ConfigurationObject conf : configurationService.retrieveConfiguration(this, SUBNETS_FILE_NAME)) { + addSubnet((SubnetConfig) conf); } } - @SuppressWarnings("unchecked") private void loadSpanConfiguration() { - ObjectReader objReader = new ObjectReader(); - ConcurrentMap confList = (ConcurrentMap) objReader - .read(this, spanFileName); - - if (confList == null) { - return; - } - - for (SpanConfig conf : confList.values()) { - addSpanConfig(conf); + for (ConfigurationObject conf : configurationService.retrieveConfiguration(this, SPAN_FILE_NAME)) { + addSpanConfig((SpanConfig) conf); } } - @SuppressWarnings("unchecked") private void loadSwitchConfiguration() { - ObjectReader objReader = new ObjectReader(); - ConcurrentMap confList = (ConcurrentMap) objReader - .read(this, switchConfigFileName); - - if (confList == null) { - return; - } - - for (SwitchConfig conf : confList.values()) { - updateNodeConfig(conf); + for (ConfigurationObject conf : configurationService.retrieveConfiguration(this, SWITCH_CONFIG_FILE_NAME)) { + updateNodeConfig((SwitchConfig) conf); } } @@ -794,7 +746,7 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa return; } - log.info("Set Node {}'s Mode to {}", nodeId, cfgObject.getMode()); + log.trace("Set Node {}'s Mode to {}", nodeId, cfgObject.getMode()); if (modeChange) { notifyModeChange(node, cfgObject.isProactive()); @@ -888,9 +840,14 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa String prop = entry.getKey(); if (!updateProperties.containsKey(prop)) { if (prop.equals(Description.propertyName)) { - if (!advertisedDesc.isEmpty()) { - Property desc = new Description(advertisedDesc); - propMap.put(Description.propertyName, desc); + if (advertisedDesc != null) { + if (!advertisedDesc.isEmpty()) { + Property desc = new Description(advertisedDesc); + propMap.put(Description.propertyName, desc); + } + } + else { + propMap.remove(prop); } continue; } else if (prop.equals(ForwardingMode.name)) { @@ -949,24 +906,36 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa } public Status saveSwitchConfigInternal() { - Status retS = null, retP = null; - ObjectWriter objWriter = new ObjectWriter(); - - retS = objWriter.write(new ConcurrentHashMap( - subnetsConfigList), subnetFileName); - retP = objWriter.write(new ConcurrentHashMap( - spanConfigList), spanFileName); - retS = objWriter.write(new ConcurrentHashMap( - nodeConfigList), switchConfigFileName); - if (retS.equals(retP)) { - if (retS.isSuccess()) { - return retS; - } else { - return new Status(StatusCode.INTERNALERROR, "Save failed"); - } + Status status; + short number = 0; + status = configurationService.persistConfiguration( + new ArrayList(subnetsConfigList.values()), SUBNETS_FILE_NAME); + if (status.isSuccess()) { + number++; + } else { + log.warn("Failed to save subnet gateway configurations: " + status.getDescription()); + } + status = configurationService.persistConfiguration(new ArrayList(spanConfigList.values()), + SPAN_FILE_NAME); + if (status.isSuccess()) { + number++; } else { + log.warn("Failed to save span port configurations: " + status.getDescription()); + } + status = configurationService.persistConfiguration(new ArrayList(nodeConfigList.values()), + SWITCH_CONFIG_FILE_NAME); + if (status.isSuccess()) { + number++; + } else { + log.warn("Failed to save node configurations: " + status.getDescription()); + } + if (number == 0) { + return new Status(StatusCode.INTERNALERROR, "Save failed"); + } + if (number < 3) { return new Status(StatusCode.INTERNALERROR, "Partial save failure"); } + return status; } @Override @@ -1036,7 +1005,8 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa } } - boolean proactiveForwarding = false; + boolean forwardingModeChanged = false; + // copy node properties from config if (nodeConfigList != null) { String nodeId = node.toString(); @@ -1046,7 +1016,19 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa propMap.putAll(nodeProperties); if (nodeProperties.get(ForwardingMode.name) != null) { ForwardingMode mode = (ForwardingMode) nodeProperties.get(ForwardingMode.name); - proactiveForwarding = mode.isProactive(); + forwardingModeChanged = mode.isProactive(); + } + } else if ((conf == null) && !(GlobalConstants.DEFAULT.toString().equals(containerName))) { + ISwitchManager defaultSwitchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, GlobalConstants.DEFAULT.toString(), this); + if (defaultSwitchManager != null) { + Property defaultContainerSwitchDesc = (Description) defaultSwitchManager.getNodeProp(node, Description.propertyName); + if (defaultContainerSwitchDesc != null) { + Map descPropMap = new HashMap(); + descPropMap.put(Description.propertyName, defaultContainerSwitchDesc); + conf = new SwitchConfig(nodeId, descPropMap); + updateNodeConfig(conf); + propMap.put(Description.propertyName, defaultContainerSwitchDesc); + } } } } @@ -1055,28 +1037,35 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa Property defaultMode = new ForwardingMode(ForwardingMode.REACTIVE_FORWARDING); propMap.put(ForwardingMode.name, defaultMode); } - boolean result = false; - if (propMapCurr == null) { - if (nodeProps.putIfAbsent(node, propMap) == null) { - result = true; - } + + boolean propsAdded = false; + // Attempt initial add + if (nodeProps.putIfAbsent(node, propMap) == null) { + propsAdded = true; + + /* Notify listeners only for initial node addition + * to avoid expensive tasks triggered by redundant notifications + */ + notifyNode(node, UpdateType.ADDED, propMap); } else { - result = nodeProps.replace(node, propMapCurr, propMap); + + propsAdded = nodeProps.replace(node, propMapCurr, propMap); + + // check whether forwarding mode changed + if (propMapCurr.get(ForwardingMode.name) != null) { + ForwardingMode mode = (ForwardingMode) propMapCurr.get(ForwardingMode.name); + forwardingModeChanged ^= mode.isProactive(); + } } - if (!result) { - log.debug("Cluster conflict: Conflict while adding the node properties. Node: {} Properties: {}", - node.getID(), props); + if (!propsAdded) { + log.debug("Cluster conflict while adding node {}. Overwriting with latest props: {}", node.getID(), props); addNodeProps(node, propMap); } - // check if span ports are configed + // check if span ports are configured addSpanPorts(node); - - // notify node listeners - notifyNode(node, UpdateType.ADDED, propMap); - // notify proactive mode forwarding - if (proactiveForwarding) { + if (forwardingModeChanged) { notifyModeChange(node, true); } } @@ -1086,7 +1075,12 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa if (nodeProps == null) { return; } - nodeProps.remove(node); + + if (nodeProps.remove(node) == null) { + log.debug("Received redundant node REMOVED udate for {}. Skipping..", node); + return; + } + nodeConnectorNames.remove(node); Set removeNodeConnectorSet = new HashSet(); for (Map.Entry> entry : nodeConnectorProps.entrySet()) { @@ -1108,8 +1102,7 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa private void updateNode(Node node, Set props) { log.trace("{} updated, props: {}", node, props); - if (nodeProps == null || !nodeProps.containsKey(node) || - props == null || props.isEmpty()) { + if (nodeProps == null || props == null) { return; } @@ -1182,6 +1175,13 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa switch (type) { case ADDED: + // Skip redundant ADDED update (e.g. cluster switch-over) + if (nodeConnectorProps.containsKey(nodeConnector)) { + log.debug("Redundant nodeconnector ADDED for {}, props {} for container {}", + nodeConnector, props, containerName); + update = false; + } + if (props != null) { for (Property prop : props) { addNodeConnectorProp(nodeConnector, prop); @@ -1191,6 +1191,7 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa addNodeConnectorProp(nodeConnector, null); } + addSpanPort(nodeConnector); break; case CHANGED: @@ -1417,36 +1418,6 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa return (propMap != null) ? propMap.get(propName) : null; } - private byte[] getHardwareMAC() { - Enumeration nis; - byte[] macAddress = null; - - try { - nis = NetworkInterface.getNetworkInterfaces(); - } catch (SocketException e) { - log.error("Failed to acquire controller MAC: ", e); - return macAddress; - } - - while (nis.hasMoreElements()) { - NetworkInterface ni = nis.nextElement(); - try { - macAddress = ni.getHardwareAddress(); - } catch (SocketException e) { - log.error("Failed to acquire controller MAC: ", e); - } - if (macAddress != null) { - break; - } - } - if (macAddress == null) { - log.warn("Failed to acquire controller MAC: No physical interface found"); - // This happens when running controller on windows VM, for example - // Try parsing the OS command output - } - return macAddress; - } - @Override public byte[] getControllerMAC() { MacAddress macProperty = (MacAddress)controllerProps.get(MacAddress.name); @@ -1658,7 +1629,6 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa isDefaultContainer = containerName.equals(GlobalConstants.DEFAULT .toString()); - startUp(); } /** @@ -1677,6 +1647,15 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa * */ void start() { + startUp(); + + /* + * Read startup and build database if we are the coordinator + */ + loadSubnetConfiguration(); + loadSpanConfiguration(); + loadSwitchConfiguration(); + // OSGI console registerWithOSGIConsole(); } @@ -1698,6 +1677,16 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa void stop() { } + public void setConfigurationContainerService(IConfigurationContainerService service) { + log.trace("Got configuration service set request {}", service); + this.configurationService = service; + } + + public void unsetConfigurationContainerService(IConfigurationContainerService service) { + log.trace("Got configuration service UNset request"); + this.configurationService = null; + } + public void setInventoryService(IInventoryService service) { log.trace("Got inventory service set request {}", service); this.inventoryService = service; @@ -1787,6 +1776,16 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa } } + public void setControllerProperties(IControllerProperties controllerProperties) { + log.trace("Got controller properties set request {}", controllerProperties); + this.controllerProperties = controllerProperties; + } + + public void unsetControllerProperties(IControllerProperties controllerProperties) { + log.trace("Got controller properties UNset request"); + this.controllerProperties = null; + } + private void getInventories() { if (inventoryService == null) { log.trace("inventory service not avaiable"); @@ -2041,9 +2040,9 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa // 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); + List ncList = new ArrayList(); + ncList.add(nodeConnector); + addSpanPorts(nodeConnector.getNode(), ncList); return; } } @@ -2164,7 +2163,7 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa public Set getConfiguredNotConnectedSwitches() { Set configuredNotConnectedSwitches = new HashSet(); if (this.inventoryService == null) { - log.trace("inventory service not avaiable"); + log.trace("inventory service not available"); return configuredNotConnectedSwitches; } @@ -2172,9 +2171,7 @@ public class SwitchManager implements ISwitchManager, IConfigurationContainerAwa if (configuredNotConnectedNodes != null) { for (Node node : configuredNotConnectedNodes) { Switch sw = getSwitchByNode(node); - if (sw != null) { - configuredNotConnectedSwitches.add(sw); - } + configuredNotConnectedSwitches.add(sw); } } return configuredNotConnectedSwitches;