package com.onaro.sanscreen.client.view.task.actions.inventory; import com.onaro.sanscreen.task.interfaces.data.inventory.Device; import com.onaro.sanscreen.task.interfaces.data.inventory.Port; import com.onaro.sanscreen.task.interfaces.data.inventory.InventoryException; import com.onaro.sanscreen.types.ObjectType; import com.onaro.sanscreen.client.error.CentralErrorHandler; import java.util.*; /** * Allow for viewing of ports of devices. It can be configured to show ports of a particular type * of device. Also it can show the ports of a paricular device instance. */ public class PortTreeModelFilter extends InventoryTreeModelFilter { protected InventoryTreeModel.ListNode[] root; private final Set objectTypeFilters; private final String storageIp; private final boolean showDevicesWhenNullParams; private boolean showStorageControllers; /** * Keeps the ports of a storage device as {@link InventoryTreeModel.ListNode} indexed by its ID (Long). */ private Map> storagePorts = new HashMap>(); /** * Allow for viewing of ports of devices. It'll show ports of a particular type * of device. * * @param objectTypeFilter the types of devices for which ports will be visible */ public PortTreeModelFilter(ObjectType[] objectTypeFilter) { this.showDevicesWhenNullParams = false; if (objectTypeFilter == null) { this.objectTypeFilters = null; } else { this.objectTypeFilters = new HashSet(Arrays.asList(objectTypeFilter)); } this.storageIp = null; } /** * Allow for viewing of ports of devices. It'll show the ports of a particular device instance. * * @param storageIp the IP of the device (null if WWN is specified) */ public PortTreeModelFilter(String storageIp) { this.showDevicesWhenNullParams = false; this.objectTypeFilters = null; this.storageIp = storageIp; } public PortTreeModelFilter() { this.showDevicesWhenNullParams = true; this.objectTypeFilters = null; this.storageIp = null; } public PortTreeModelFilter(boolean showDevicesWhenNullParams) { this.showDevicesWhenNullParams = showDevicesWhenNullParams; this.objectTypeFilters = null; this.storageIp = null; } public void setInventoryTreeModel(InventoryTreeModel inventoryTreeModel) throws InventoryException { super.setInventoryTreeModel(inventoryTreeModel); if (storageIp != null) { final List device = new LinkedList(); device.add(inventoryTreeModel.getInventory().findDeviceByIp(storageIp)); root = new InventoryTreeModel.ListNode[]{ new InventoryTreeModel.ListNode("Storage Devices") { //$NON-NLS-1$ List loadList() { return device; } }, }; } else if (objectTypeFilters == null) { if (showDevicesWhenNullParams) { root = new InventoryTreeModel.ListNode[]{ getHostsListNode(), getSwitchesListNode(), getGenericsListNode(), getStorageListNode(), getTapesListNode(), }; } else { root = new InventoryTreeModel.ListNode[]{ new InventoryTreeModel.ListNode("Storage Devices") { //$NON-NLS-1$ List loadList() { return new LinkedList(); } }, }; } } else { if (objectTypeFilters.contains(ObjectType.HOST)) { root = new InventoryTreeModel.ListNode[]{ getHostsListNode(), }; } else if (objectTypeFilters.contains(ObjectType.STORAGE)) { root = new InventoryTreeModel.ListNode[]{ getStorageListNode(), }; } else if (objectTypeFilters.contains(ObjectType.TAPE)) { root = new InventoryTreeModel.ListNode[]{ getTapesListNode(), }; } else if (objectTypeFilters.contains(ObjectType.SWITCH)) { root = new InventoryTreeModel.ListNode[]{ getSwitchesListNode(), }; } else if (objectTypeFilters.contains(ObjectType.UNKNOWN)) { root = new InventoryTreeModel.ListNode[]{ getGenericsListNode(), }; } } if (root == null) { root = new InventoryTreeModel.ListNode[]{}; } } public int getChildCount(Object parent) { try { if (parent == root) return root.length; if (parent instanceof Device) { Device device = (Device) parent; if (ObjectType.STORAGE.equals(device.getType())) { if (showStorageControllers) return inventoryTreeModel.getInventory().getChilds(device.getId()).size(); else return getStoragePorts(device.getId()).getChildCount(); } } return inventoryTreeModel.getChildCount(parent); } catch (InventoryException e) { CentralErrorHandler.handle("Failed getting inventory data from the server", this, e); //$NON-NLS-1$ return 0; } } public Object getRoot() { return root; } public Object getChild(Object parent, int index) { try { if (parent == root) return root[index]; if (parent instanceof Device) { Device device = (Device) parent; if (ObjectType.STORAGE.equals(device.getType())) { if (showStorageControllers) return inventoryTreeModel.getChild(parent, index + 1); else return getStoragePorts(device.getId()).getChild(index); } } Object obj = inventoryTreeModel.getChild(parent, index); if (obj instanceof Device || obj instanceof Port) return obj; return null; } catch (InventoryException e) { CentralErrorHandler.handle("Failed getting the ports of device: " + parent, e); //$NON-NLS-1$ return null; } } public int getIndexOfChild(Object parent, Object child) { try { if (parent == null || child == null) { return -1; } if (parent == root) { for (int i = 0; i < root.length; ++i) { if (root[i] == parent) return i; } return root.length; } if (parent instanceof Device) { Device device = (Device) parent; if (ObjectType.STORAGE.equals(device.getType())) { if (showStorageControllers) return inventoryTreeModel.getInventory().getChilds(device.getId()).size() + 1; else return getStoragePorts(device.getId()).indexOf(child); } } return inventoryTreeModel.getIndexOfChild(parent, child); } catch (InventoryException e) { CentralErrorHandler.handle("Failed getting inventory data from the server", this, e); //$NON-NLS-1$ return -1; } } public boolean isLeaf(Object node) { return inventoryTreeModel.isLeaf(node); } public boolean isShowStorageControllers() { return showStorageControllers; } public void setShowStorageControllers(boolean showStorageControllers) { this.showStorageControllers = showStorageControllers; } private InventoryTreeModel.ListNode getStoragePorts(Long storageId) { InventoryTreeModel.ListNode ports = storagePorts.get(storageId); if (ports == null) { List allPorts = new ArrayList(); try { List controllers = inventoryTreeModel.getInventory().getChilds(storageId); for (int i = 0; i < controllers.size(); i++) { Device controller = controllers.get(i); allPorts.addAll(inventoryTreeModel.getInventory().getPorts(controller.getId())); } } catch (InventoryException e) { CentralErrorHandler.handle("Failed getting the list of ports of storage ID: " + storageId, e); //$NON-NLS-1$ } ports = new PortsListNode(allPorts); } return ports; } class PortsListNode extends InventoryTreeModel.ListNode { List ports; public PortsListNode(List ports) { super(null); this.ports = ports; } List loadList() throws InventoryException { Collections.sort(ports, InventoryTreeModel.portComparator); return ports; } } }