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;
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;
* 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;
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;
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;
}
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";
* 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")
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());
}
return new ArrayList<SwitchConfig>(nodeConfigList.values());
}
+ @Override
public SwitchConfig getSwitchConfig(String switchId) {
return nodeConfigList.get(switchId);
}
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);
return sw;
}
+ @Override
public List<Switch> getNetworkDevices() {
Set<Node> nodeSet = getNodes();
List<Switch> swList = new ArrayList<Switch>();
}
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());
}
}
/**
* Adds Subnet configured in GUI or API3
*/
+ @Override
public Status addSubnet(SubnetConfig conf) {
return this.addRemoveSubnet(conf, true);
}
}
conf.addNodeConnectors(switchPorts);
+ subnetsConfigList.put(name, conf);
// Update Database
Subnet sub = subnets.get(conf.getIPnum());
Set<NodeConnector> sp = conf.getNodeConnectors(switchPorts);
sub.addNodeConnectors(sp);
+ subnets.put(conf.getIPnum(), sub);
return new Status(StatusCode.SUCCESS, null);
}
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<NodeConnector> sp = conf.getNodeConnectors(switchPorts);
sub.deleteNodeConnectors(sp);
+ subnets.put(conf.getIPnum(), sub);
return new Status(StatusCode.SUCCESS, null);
}
@SuppressWarnings("unchecked")
private void loadSubnetConfiguration() {
ObjectReader objReader = new ObjectReader();
- ConcurrentMap<Integer, SubnetConfig> confList = (ConcurrentMap<Integer, SubnetConfig>) objReader
+ ConcurrentMap<String, SubnetConfig> confList = (ConcurrentMap<String, SubnetConfig>) objReader
.read(this, subnetFileName);
if (confList == null) {
@Override
public void updateSwitchConfig(SwitchConfig cfgObject) {
+ // update default container only
+ if (!isDefaultContainer) {
+ return;
+ }
+
boolean modeChange = false;
SwitchConfig sc = nodeConfigList.get(cfgObject.getNodeId());
nodeConfigList.put(cfgObject.getNodeId(), cfgObject);
try {
- // update default container only
- if (isDefaultContainer) {
- String nodeId = cfgObject.getNodeId();
- Node node = Node.fromString(nodeId);
- Map<String, Property> propMap;
- if (nodeProps.get(node) != null) {
- propMap = nodeProps.get(node);
- } else {
- propMap = new HashMap<String, Property>();
- }
- 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<String, Property> propMap;
+ if (nodeProps.get(node) != null) {
+ propMap = nodeProps.get(node);
+ } else {
+ propMap = new HashMap<String, Property>();
+ }
+ 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());
@Override
public void updateNode(Node node, UpdateType type, Set<Property> props) {
+ log.debug("updateNode: {} type {} props {} for container {}",
+ new Object[] { node, type, props, containerName });
switch (type) {
case ADDED:
addNode(node, props);
Node node = nodeConnector.getNode();
Map<String, Property> propMap = new HashMap<String, Property>();
- log.trace("{} {}", nodeConnector, type);
+ log.debug("updateNodeConnector: {} type {} props {} for container {}",
+ new Object[] { nodeConnector, type, props, containerName });
if (nodeConnectorProps == null) {
return;
addNodeProps(node, null);
}
- // check if span is configed
addSpanPort(nodeConnector);
break;
case REMOVED:
: 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)
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);
@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;
@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);
}
@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()) {
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) {
@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
/**
* Removes one property of a node connector
- *
+ *
* @param nodeConnector
* {@link org.opendaylight.controller.sal.core.NodeConnector}
* @param propName
/**
* Removes all the properties of a node connector
- *
+ *
* @param nodeConnector
* {@link org.opendaylight.controller.sal.core.NodeConnector}
* @return success or failed reason
/**
* Function called by the dependency manager when all the required
* dependencies are satisfied
- *
+ *
*/
void init(Component c) {
Dictionary<?, ?> props = c.getServiceProperties();
* 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();
/**
* 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
* 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() {
}
return;
}
- nodeProps = this.inventoryService.getNodeProps();
- Set<Node> nodeSet = nodeProps.keySet();
- if (nodeSet != null) {
- for (Node node : nodeSet) {
- addNode(node, null);
+ Map<Node, Map<String, Property>> nodeProp = this.inventoryService.getNodeProps();
+ for(Map.Entry<Node, Map<String, Property>> entry : nodeProp.entrySet()) {
+ Node node = entry.getKey();
+ log.debug("getInventories: {} added for container {}",
+ new Object[] { node, containerName });
+ Map<String, Property> propMap = entry.getValue();
+ Set<Property> props = new HashSet<Property>();
+ for(Property property : propMap.values()) {
+ props.add(property);
}
+ addNode(node, props);
}
- nodeConnectorProps = inventoryService.getNodeConnectorProps();
+ Map<NodeConnector, Map<String, Property>> nodeConnectorProp = this.inventoryService.getNodeConnectorProps();
+ for(Map.Entry<NodeConnector, Map<String, Property>> entry : nodeConnectorProp.entrySet()) {
+ Map<String, Property> propMap = entry.getValue();
+ for(Property property : propMap.values()) {
+ addNodeConnectorProp(entry.getKey(), property);
+ }
+ }
}
private void clearInventories() {
}
private void bulkUpdateService(IInventoryListener service) {
+ Map<String, Property> 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<String, Property> propMap = new HashMap<String, Property>();
- 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);
}
}
@Override
public Boolean isNodeConnectorEnabled(NodeConnector nodeConnector) {
- if (nodeConnector == null)
+ if (nodeConnector == null) {
return false;
+ }
Config config = (Config) getNodeConnectorProp(nodeConnector,
Config.ConfigPropName);
}
public void _pns(CommandInterpreter ci) {
- ci.println(" Node Type Name Tier");
+ ci.println(" Node Type MAC Name Tier");
if (nodeProps == null) {
return;
}
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());
}
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;
}
@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
/*
* Add span configuration to local cache and notify clients
*/
- private void addSpanPorts(Node node, List<NodeConnector> nodeConncetors) {
+ private void addSpanPorts(Node node, List<NodeConnector> nodeConnectors) {
List<NodeConnector> ncLists = new ArrayList<NodeConnector>();
- for (NodeConnector nodeConnector : nodeConncetors) {
+ for (NodeConnector nodeConnector : nodeConnectors) {
if (!spanNodeConnectors.contains(nodeConnector)) {
ncLists.add(nodeConnector);
}
}
}
- private void addSpanPort(NodeConnector nodeConncetor) {
- List<NodeConnector> ncLists = new ArrayList<NodeConnector>();
- 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<NodeConnector> ncLists = new ArrayList<NodeConnector>();
+ ncLists.add(nodeConnector);
+ addSpanPorts(nodeConnector.getNode(), ncLists);
+ return;
+ }
+ }
}
/*
* Remove span configuration to local cache and notify clients
*/
- private void removeSpanPorts(Node node, List<NodeConnector> nodeConncetors) {
+ private void removeSpanPorts(Node node, List<NodeConnector> nodeConnectors) {
List<NodeConnector> ncLists = new ArrayList<NodeConnector>();
- for (NodeConnector nodeConnector : nodeConncetors) {
- if (!spanNodeConnectors.contains(nodeConnector)) {
+ for (NodeConnector nodeConnector : nodeConnectors) {
+ if (spanNodeConnectors.contains(nodeConnector)) {
ncLists.add(nodeConnector);
}
}
}
}
- private void removeSpanPort(NodeConnector nodeConncetor) {
- List<NodeConnector> ncLists = new ArrayList<NodeConnector>();
- ncLists.add(nodeConncetor);
- removeSpanPorts(nodeConncetor.getNode(), ncLists);
+ private void removeSpanPort(NodeConnector nodeConnector) {
+ if (spanNodeConnectors.contains(nodeConnector)) {
+ List<NodeConnector> ncLists = new ArrayList<NodeConnector>();
+ ncLists.add(nodeConnector);
+ removeSpanPorts(nodeConnector.getNode(), ncLists);
+ }
}
private void addNodeProps(Node node, Map<String, Property> propMap) {
/**
* 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