/**
*
*/
package com.onaro.sanscreen.client.view.selection;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.onaro.client.server.ObjectTypeResourcesManager;
import com.onaro.commons.lang.CommonStringUtils;
import com.onaro.sanscreen.client.Messages;
import com.onaro.sanscreen.client.SANscreenClientPlugin;
import com.onaro.sanscreen.server.dataobject.historyobject.path.Approve;
import com.onaro.sanscreen.server.interfaces.data.AbstractDataObjectDescription;
import com.onaro.sanscreen.server.interfaces.data.reservation.TaskType;
import com.onaro.sanscreen.types.ObjectType;
import com.onaro.util.enumeration.EnumPresentation;
/**
*
*/
/*package*/ final class SelectedObjectDescription extends AbstractDataObjectDescription {
public static final String HOST_TYPE_NAME = ObjectTypeResourcesManager.INSTANCE.getTitle(ObjectType.HOST);
public static final String STORAGE_TYPE_NAME = ObjectTypeResourcesManager.INSTANCE.getTitle(ObjectType.STORAGE);
public static final String DISK_TYPE_NAME = ObjectTypeResourcesManager.INSTANCE.getTitle(ObjectType.DISK);
public static final String SWITCH_TYPE_NAME = ObjectTypeResourcesManager.INSTANCE.getTitle(ObjectType.SWITCH);
public static final String PORT_TYPE_NAME = ObjectTypeResourcesManager.INSTANCE.getTitle(ObjectType.PORT);
public static final String ZONE_TYPE_NAME = ObjectTypeResourcesManager.INSTANCE.getTitle(ObjectType.ZONE);
public static final String REQUEST_TYPE_NAME = Messages.INSTANCE.getRequestTypeName();
public static final String TICKET_TYPE_NAME = Messages.INSTANCE.getTicketTypeName();
private static final Logger logger = LogManager.getLogger (SelectedObjectDescription.class);
private static final String UNKNOWN_VALUE = "?"; //$NON-NLS-1$
private final SelectedObject selection;
// TODO: Because all the properties used are column ID's from the source view tables, we should place these
// column ID's in a common place used by both the tables and by this class, so we can always be sure that this
// class is using the correct ID. Otherwise, even minor differences like capitalization can prevent this class
// from working properly.
// In the meantime, note that these should correspond with the COL constants for the respective tables.
/** Correspond to com.onaro.sanscreen.client.si.views.inventory.ports.PortsTableModel.COL_DEVICE_NAME **/
private static final String FC_PORTS_COL_DEVICE_NAME = "DeviceName"; //$NON-NLS-1$
/** Correspond to com.onaro.sanscreen.client.sw.perf.views.switchPort.SwitchPortPerformanceTableModel.COL_SWITCH **/
private static final String SWITCH_PORTS_COL_SWITCH = "switch"; //$NON-NLS-1$
/** Corresponds to the WWN property name available to port objects in the topology model. **/
private static final String TOPOLOGY_PORT_PROPERTY_WWN = "WWN"; //$NON-NLS-1$
/**
* Constructor
*
* @param selection selected object for which the descriptions are generated
*/
public SelectedObjectDescription (SelectedObject selection) {
this.selection = selection;
}
@Override
public String getName() {
// Try using Alias property for name, then Name property.
String name = selection.getAlias();
if (StringUtils.isBlank (name)) {
name = selection.getName();
}
// Special case for applications.
if (StringUtils.isBlank (name)) {
name = CommonStringUtils.getTextFromHtml (
selection.getStringProperty ("application.details.name")); //$NON-NLS-1$
}
return name;
}
@Override
public String getShortDescription() {
ObjectType objectType = selection.getObjectType();
// Generic devices have no known name or type.
if (ObjectType.UNKNOWN.equals (objectType)) {
return getGenericDeviceShortDescription();
}
// Get the name or a short description of the object.
String description;
if (ObjectType.DISK.equals (objectType)) {
description = getDiskShortDescription();
} else if (ObjectType.VOLUME.equals (objectType)) {
description = getVolumeShortDescription();
} else if (ObjectType.PORT.equals (objectType)) {
description = getPortShortDescription();
} else {
description = getName();
}
// Prepend the object type name, if known.
String typeName = ObjectTypeResourcesManager.INSTANCE.getTitle (objectType);
if (typeName != null) {
description = Messages.INSTANCE.getTypedName (typeName, description);
}
return description;
}
@Override
public String getLongDescription() {
/*
* Create description according to type of selected object. Unfortunately,
* the type is not determined by a simple enum. Sometimes an ObjectType is
* defined. Sometimes a type name is defined even if ObjectType is not
* defined. And sometimes the selection identifyingKey or other properties
* have to be examined to determine the type.
*
* Note ObjectType is deprecated. Should be replaced with DataObjectType when
* legacy views are no longer supported.
*/
ObjectType objectType = selection.getObjectType();
if (objectType == null) {
String typeName = selection.getType();
if ("Task".equals (typeName)) { //$NON-NLS-1$
return getTaskDescription();
} else if ("SwitchMigrationTask".equals (typeName)) { //$NON-NLS-1$
return getMigrationTaskDescription();
}
String identifyingKey = selection.getIdentifyingKey();
if ("VulnerabilityType".equals (identifyingKey)) { //$NON-NLS-1$
return getVulnerabilityDescription();
} else if ("objId".equals (identifyingKey)) { //$NON-NLS-1$
// Note it's tough to figure out if a switch performance alert is selected.
if ("SWITCH".equals (selection.getStringProperty ("Scope")) //$NON-NLS-1$ //$NON-NLS-2$
&& selection.getProperty ("ConnectedDeviceType") != null //$NON-NLS-1$
&& selection.getProperty ("Threshold") != null) { //$NON-NLS-1$
return getSwitchPerformanceAlertDescription();
}
}
} else if (ObjectType.DISK.equals (objectType)) {
return getDiskDescription();
} else if (ObjectType.PORT.equals (objectType)) {
return getPortDescription();
} else if (ObjectType.PATH.equals (objectType)) {
return getPathDescription();
} else if (ObjectType.ZONE.equals (objectType)) {
return getZoneDescription();
} else if (ObjectType.APPLICATION.equals (objectType)) {
return getApplicationDescription();
} else if (ObjectType.VIOLATION.equals (objectType)) {
return getViolationDescription();
} else if (ObjectType.RESERVATION_VIOLATION.equals (objectType)) {
return getReservationViolationDescription();
} else if (ObjectType.POLICY.equals (objectType)) {
return getPolicyDescription();
} else if (ObjectType.APL.equals (objectType)) {
String policyType = selection.getStringProperty ("ApproveType"); //$NON-NLS-1$
if (policyType.equals (Approve.TYPE_HOST)) {
return getHostPolicyDescription();
} else if (policyType.equals (Approve.TYPE_PATH)) {
return getPathPolicyDescription();
}
} else if (ObjectType.REQUEST.equals (objectType)) {
return getRequestDescription();
} else if (ObjectType.PENDING_TASK.equals (objectType)) {
return getPendingTaskDescription();
} else if (ObjectType.DATA_SOURCE.equals (objectType)) {
return getNameDescription();
} else if (ObjectType.ADAPTER.equals (objectType)) {
return getTypeAndWwnDescription();
} else if (ObjectType.CONTROLLER.equals (objectType)) {
return getTypeAndWwnDescription();
} else if (ObjectType.UNKNOWN.equals (objectType)) {
return getGenericDeviceLongDescription();
} else {
// For most devices, type and name is a good description.
if (StringUtils.isNotBlank (getName())) {
return getTypeAndNameDescription();
}
}
return super.getLongDescription();
}
private String getTypeName() {
return ObjectTypeResourcesManager.INSTANCE.getName (selection.getObjectType());
}
private String getNameDescription() {
return Messages.INSTANCE.getSelectionHtml (getName());
}
private String getTypeAndNameDescription() {
return getTypeAndNameDescription (getTypeName(), getName());
}
private String getTypeAndNameDescription (String typeName, String objectName) {
// Don't display type if name already begins with type.
if (CommonStringUtils.beginsWithIgnoreCase (objectName, typeName)) {
return Messages.INSTANCE.getSelectionHtml (objectName);
} else {
return Messages.INSTANCE.getTypedSelectionHtml (typeName, objectName);
}
}
private String getTypeAndWwnDescription() {
String typeName = getTypeName();
String wwn = selection.getStringProperty (TOPOLOGY_PORT_PROPERTY_WWN);
if (StringUtils.isBlank (wwn)) {
wwn = UNKNOWN_VALUE;
logger.warn ("No WWN property for " + selection.getType() //$NON-NLS-1$
+ " " + selection.getId()); //$NON-NLS-1$
}
return Messages.INSTANCE.getTypedSelectionHtml (typeName, wwn);
}
private String getDiskDescription() {
String diskName = getName();
String storageName = selection.getStringProperty ("StorageName"); //$NON-NLS-1$
/*
* Description is of form "Storage Disk "
* except that "Storage" is dropped if it's already in ,
* "Disk" is dropped if it's already in , and
* "Storage " is dropped if is not defined.
*/
if (StringUtils.isBlank (storageName)) {
if (CommonStringUtils.beginsWithIgnoreCase (diskName, DISK_TYPE_NAME)) {
return Messages.INSTANCE.getSelectionHtml (diskName);
} else {
return Messages.INSTANCE.getDiskTypeSelectionHtml (diskName);
}
} else if (CommonStringUtils.beginsWithIgnoreCase (storageName, STORAGE_TYPE_NAME)) {
if (CommonStringUtils.beginsWithIgnoreCase (diskName, DISK_TYPE_NAME)) {
return Messages.INSTANCE.getStorageDiskSelectionHtml (storageName, diskName);
} else {
return Messages.INSTANCE.getStorageDiskTypeSelectionHtml (storageName, diskName);
}
} else if (CommonStringUtils.beginsWithIgnoreCase (diskName, DISK_TYPE_NAME)) {
return Messages.INSTANCE.getStorageTypeDiskSelectionHtml (storageName, diskName);
} else {
return Messages.INSTANCE.getStorageTypeDiskTypeSelectionHtml (storageName, diskName);
}
}
private String getDiskShortDescription() {
/*
* Description is of form " on "
* except that " on " is dropped if is not defined.
*/
String diskName = getName();
String storageName = selection.getStringProperty ("StorageName"); //$NON-NLS-1$
if (StringUtils.isBlank (storageName)) {
return diskName;
} else {
return Messages.INSTANCE.getSubElementSelection (storageName, diskName);
}
}
private String getVolumeShortDescription() {
/*
* Description is of form " on "
* except that " on " is dropped if is not defined.
*/
String volumeName = getName();
String storageName = selection.getStringProperty ("StorageName"); //$NON-NLS-1$
if (StringUtils.isBlank (storageName)) {
return volumeName;
} else {
return Messages.INSTANCE.getSubElementSelection (storageName, volumeName);
}
}
private String getPortDescription() {
// NOTE: This produces a string with the word "Switch" in it, assuming that the given port is a switch port.
// This happens to be true for now, as this is used only to produce the selection description for main views
// and would therefore be relevant for ports selected via the Switch Port Performance main view.
String portName = getPortName();
// Device/switch name property name depends on whether selected object from FC Ports view or
// Switch Ports Performance view:
String deviceName = selection.getStringProperty (FC_PORTS_COL_DEVICE_NAME);
if (StringUtils.isBlank (deviceName)) {
deviceName = selection.getStringProperty(SWITCH_PORTS_COL_SWITCH);
}
/*
* Description is of form "Switch Port "
* except that "Switch" is dropped if it's already in ,
* "Port" is dropped if it's already in , and
* "Switch " is dropped if is not defined.
*/
if (StringUtils.isBlank (deviceName)) {
if (CommonStringUtils.beginsWithIgnoreCase (portName, PORT_TYPE_NAME)) {
return Messages.INSTANCE.getSelectionHtml (portName);
} else {
return Messages.INSTANCE.getPortTypeSelectionHtml (portName);
}
} else if (CommonStringUtils.beginsWithIgnoreCase (deviceName, SWITCH_TYPE_NAME)) {
if (CommonStringUtils.beginsWithIgnoreCase (portName, PORT_TYPE_NAME)) {
return Messages.INSTANCE.getSwitchPortSelectionHtml (deviceName, portName);
} else {
return Messages.INSTANCE.getSwitchPortTypeSelectionHtml (deviceName, portName);
}
} else if (CommonStringUtils.beginsWithIgnoreCase (portName, PORT_TYPE_NAME)) {
return Messages.INSTANCE.getSwitchTypePortSelectionHtml (deviceName, portName);
} else {
return Messages.INSTANCE.getSwitchTypePortTypeSelectionHtml (deviceName, portName);
}
}
private String getPortShortDescription() {
String portName = getPortName();
/*
* Description is of form " on "
* except that "on " is dropped if is not defined.
*/
String deviceName = selection.getStringProperty (FC_PORTS_COL_DEVICE_NAME);
if (StringUtils.isBlank (deviceName)) {
deviceName = selection.getStringProperty(SWITCH_PORTS_COL_SWITCH);
}
if (StringUtils.isBlank (deviceName)) {
return portName;
} else {
return Messages.INSTANCE.getSubElementSelection (deviceName, portName);
}
}
private String getPortName() {
String portName = selection.getName();
// If updating the selection status via a selected port in the topology, just have the "WWN" property:
if (portName == null) {
portName = selection.getStringProperty(TOPOLOGY_PORT_PROPERTY_WWN);
}
// Shouldn't happen:
if (portName == null) {
portName = UNKNOWN_VALUE;
}
return portName;
}
private String getPathDescription() {
// Source.
String source = selection.getStringProperty ("SourceName"); //$NON-NLS-1$
if (StringUtils.isBlank (source)) {
source = UNKNOWN_VALUE;
logger.warn ("No SourceName for path " + selection.getId()); //$NON-NLS-1$
}
// Target.
String target = selection.getStringProperty ("TargetName"); //$NON-NLS-1$
String targetVolume = null;
String targetReplica = null;
if (StringUtils.isBlank (target)) {
target = UNKNOWN_VALUE;
logger.warn ("No TargetName for path " + selection.getId()); //$NON-NLS-1$
} else {
targetVolume = selection.getStringProperty ("VolumeName"); //$NON-NLS-1$
targetReplica = selection.getStringProperty ("Replica"); //$NON-NLS-1$
}
/*
* Description is of form
* "