From 93e4ab834487a4d86a5b6ab27adb542cc9f86233 Mon Sep 17 00:00:00 2001 From: Alessandro Boch Date: Sat, 3 Aug 2013 09:34:23 -0700 Subject: [PATCH] Controller MAC to be synced in cluster CHANGE: - Add a new property database for controller properties in switch manager. Switch manager maintains the container inventory and we could have different controller properties in different container instances. - Have switch mgr acquire the hardware mac and set the MacAddress controller property only if no other controller has already done so - Removed ReasonCode enum that was old and unused - Rename SwitchManagerImpl class to SwitchManager as it is already part of the implementaiton package Change-Id: Id3859d0ca7873ea2e3c2f597e0003bd220e90d9a Signed-off-by: Alessandro Boch --- .../switchmanager/internal/Activator.java | 4 +- ...tchManagerImpl.java => SwitchManager.java} | 93 ++++++++++--------- ...erImplTest.java => SwitchManagerTest.java} | 6 +- 3 files changed, 55 insertions(+), 48 deletions(-) rename opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/{SwitchManagerImpl.java => SwitchManager.java} (96%) rename opendaylight/switchmanager/implementation/src/test/java/org/opendaylight/controller/switchmanager/internal/{SwitchManagerImplTest.java => SwitchManagerTest.java} (96%) diff --git a/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/Activator.java b/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/Activator.java index d4511ffde5..2420d609b7 100644 --- a/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/Activator.java +++ b/opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/Activator.java @@ -66,7 +66,7 @@ public class Activator extends ComponentActivatorAbstractBase { * Object */ public Object[] getImplementations() { - Object[] res = { SwitchManagerImpl.class }; + Object[] res = { SwitchManager.class }; return res; } @@ -84,7 +84,7 @@ public class Activator extends ComponentActivatorAbstractBase { * should not be the case though. */ public void configureInstance(Component c, Object imp, String containerName) { - if (imp.equals(SwitchManagerImpl.class)) { + if (imp.equals(SwitchManager.class)) { Dictionary> props = new Hashtable>(); Set propSet = new HashSet(); propSet.add("switchmanager.configSaveEvent"); 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/SwitchManager.java similarity index 96% rename from opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerImpl.java rename to opendaylight/switchmanager/implementation/src/main/java/org/opendaylight/controller/switchmanager/internal/SwitchManager.java index a91edb0c25..0705984bfe 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/SwitchManager.java @@ -83,16 +83,14 @@ import org.slf4j.LoggerFactory; * instance per container of the network. All the node/nodeConnector properties * are maintained in the default container only. */ -public class SwitchManagerImpl implements ISwitchManager, +public class SwitchManager implements ISwitchManager, IConfigurationContainerAware, IObjectReader, ICacheUpdateAware, IListenInventoryUpdates, CommandProvider { - private static Logger log = LoggerFactory - .getLogger(SwitchManagerImpl.class); + private static Logger log = LoggerFactory.getLogger(SwitchManager.class); private static String ROOT = GlobalConstants.STARTUPHOME.toString(); private static final String SAVE = "Save"; - private String subnetFileName = null, spanFileName = null, - switchConfigFileName = null; + private String subnetFileName, spanFileName, switchConfigFileName; private final List spanNodeConnectors = new CopyOnWriteArrayList(); // set of Subnets keyed by the InetAddress private ConcurrentMap subnets; @@ -104,14 +102,13 @@ CommandProvider { private ConcurrentMap> nodeProps; private ConcurrentMap> nodeConnectorProps; private ConcurrentMap> nodeConnectorNames; + private ConcurrentMap controllerProps; private IInventoryService inventoryService; private final Set switchManagerAware = Collections .synchronizedSet(new HashSet()); private final Set inventoryListeners = Collections .synchronizedSet(new HashSet()); - private final Set spanAware = Collections - .synchronizedSet(new HashSet()); - private byte[] MAC; + private final Set spanAware = Collections.synchronizedSet(new HashSet()); private static boolean hostRefresh = true; private int hostRetryCount = 5; private IClusterContainerServices clusterContainerService = null; @@ -119,23 +116,6 @@ CommandProvider { private boolean isDefaultContainer = true; private static final int REPLACE_RETRY = 1; - public enum ReasonCode { - SUCCESS("Success"), FAILURE("Failure"), INVALID_CONF( - "Invalid Configuration"), EXIST("Entry Already Exist"), CONFLICT( - "Configuration Conflict with Existing Entry"); - - private final String name; - - private ReasonCode(String name) { - this.name = name; - } - - @Override - public String toString() { - return name; - } - } - public void notifySubnetChange(Subnet sub, boolean add) { synchronized (switchManagerAware) { for (Object subAware : switchManagerAware) { @@ -149,8 +129,7 @@ CommandProvider { } } - public void notifySpanPortChange(Node node, List ports, - boolean add) { + public void notifySpanPortChange(Node node, List ports, boolean add) { synchronized (spanAware) { for (Object sa : spanAware) { try { @@ -177,11 +156,11 @@ CommandProvider { } public void startUp() { + String container = this.getContainerName(); // Initialize configuration file names - subnetFileName = ROOT + "subnets_" + this.getContainerName() + ".conf"; - spanFileName = ROOT + "spanPorts_" + this.getContainerName() + ".conf"; - switchConfigFileName = ROOT + "switchConfig_" + this.getContainerName() - + ".conf"; + subnetFileName = ROOT + "subnets_" + container + ".conf"; + spanFileName = ROOT + "spanPorts_" + container + ".conf"; + switchConfigFileName = ROOT + "switchConfig_" + container + ".conf"; // Instantiate cluster synced variables allocateCaches(); @@ -201,7 +180,17 @@ CommandProvider { loadSwitchConfiguration(); } - MAC = getHardwareMAC(); + // Add controller MAC, if first node in the cluster + if (!controllerProps.containsKey(MacAddress.name)) { + byte controllerMac[] = getHardwareMAC(); + if (controllerMac != null) { + Property existing = controllerProps.putIfAbsent(MacAddress.name, new MacAddress(controllerMac)); + if (existing == null && log.isTraceEnabled()) { + log.trace("Container {}: Setting controller MAC address in the cluster: {}", container, + HexEncode.bytesToHexStringFormat(controllerMac)); + } + } + } } public void shutDown() { @@ -236,6 +225,9 @@ CommandProvider { clusterContainerService.createCache( "switchmanager.nodeConnectorNames", EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL)); + clusterContainerService.createCache( + "switchmanager.controllerProps", + EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL)); } catch (CacheConfigException cce) { log.error("\nCache configuration invalid - check cache mode"); } catch (CacheExistException ce) { @@ -297,6 +289,12 @@ CommandProvider { if (nodeConnectorNames == null) { log.error("\nFailed to get cache for nodeConnectorNames"); } + + controllerProps = (ConcurrentMap) clusterContainerService + .getCache("switchmanager.controllerProps"); + if (controllerProps == null) { + log.error("\nFailed to get cache for controllerProps"); + } } private void nonClusterObjectCreate() { @@ -308,6 +306,7 @@ CommandProvider { nodeProps = new ConcurrentHashMap>(); nodeConnectorProps = new ConcurrentHashMap>(); nodeConnectorNames = new ConcurrentHashMap>(); + controllerProps = new ConcurrentHashMap(); } @Override @@ -1233,30 +1232,38 @@ CommandProvider { private byte[] getHardwareMAC() { Enumeration nis; + byte[] macAddress = null; + try { nis = NetworkInterface.getNetworkInterfaces(); - } catch (SocketException e1) { - log.error("",e1); - return null; + } catch (SocketException e) { + log.error("Failed to acquire controller MAC: ", e); + return macAddress; } - byte[] MAC = null; - for (; nis.hasMoreElements();) { + + while (nis.hasMoreElements()) { NetworkInterface ni = nis.nextElement(); try { - MAC = ni.getHardwareAddress(); + macAddress = ni.getHardwareAddress(); } catch (SocketException e) { - log.error("",e); + log.error("Failed to acquire controller MAC: ", e); } - if (MAC != null) { - return MAC; + if (macAddress != null) { + break; } } - return null; + 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() { - return MAC; + MacAddress macProperty = (MacAddress)controllerProps.get(MacAddress.name); + return (macProperty == null) ? null : macProperty.getMacAddress(); } @Override diff --git a/opendaylight/switchmanager/implementation/src/test/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerImplTest.java b/opendaylight/switchmanager/implementation/src/test/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerTest.java similarity index 96% rename from opendaylight/switchmanager/implementation/src/test/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerImplTest.java rename to opendaylight/switchmanager/implementation/src/test/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerTest.java index 25d54906ed..a233ad4b3e 100644 --- a/opendaylight/switchmanager/implementation/src/test/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerImplTest.java +++ b/opendaylight/switchmanager/implementation/src/test/java/org/opendaylight/controller/switchmanager/internal/SwitchManagerTest.java @@ -26,11 +26,11 @@ import org.opendaylight.controller.sal.utils.NodeCreator; import org.opendaylight.controller.sal.utils.Status; import org.opendaylight.controller.switchmanager.SubnetConfig; -public class SwitchManagerImplTest { +public class SwitchManagerTest { @Test public void testSwitchManagerAddRemoveSubnet() { - SwitchManagerImpl switchmgr = new SwitchManagerImpl(); + SwitchManager switchmgr = new SwitchManager(); switchmgr.startUp(); ArrayList portList = new ArrayList(); @@ -55,7 +55,7 @@ public class SwitchManagerImplTest { @Test public void testSwitchManagerNodeConnectors() { - SwitchManagerImpl switchmgr = new SwitchManagerImpl(); + SwitchManager switchmgr = new SwitchManager(); switchmgr.startUp(); State state; -- 2.36.6