package com.onaro.sanscreen.client.view.actions; import com.onaro.client.leekui.runtime.OnaroAdapterUtils; import com.onaro.sanscreen.client.ejb.logging.UsageLog; import com.onaro.sanscreen.client.ejb.logging.UsageLogType; import com.onaro.sanscreen.client.view.ActionFactory.ActionInitInfo; import com.onaro.sanscreen.client.view.ViewDirector; import com.onaro.sanscreen.client.view.selection.ObjectSelectionModel; import com.onaro.sanscreen.server.interfaces.DataObjectTypeEnum; import com.onaro.sanscreen.server.interfaces.data.IDataObjectType; import com.onaro.util.IllegalInitException; import com.onaro.util.jfc.date.TimeSpan; import com.onaro.util.launch.LaunchUrl; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.eclipse.core.runtime.IAdaptable; import java.awt.event.ActionEvent; import java.util.HashMap; import java.util.Map; import java.util.ResourceBundle; /** * Action to display the Insight web page corresponding to the selected object. */ public class VisitWebPageAction extends SingleSelectionAction { public static final String ID = "VisitWebPage"; //$NON-NLS-1$ public static final String RESOURCE = "menu.performance.inventory.visit.web.page"; //$NON-NLS-1$ private static final Logger logger = LogManager.getLogger (VisitWebPageAction.class); private static final long serialVersionUID = 1L; private static final String URN_FORMAT = "#assets/%s/%d"; //$NON-NLS-1$ private static final String URN_TIMESPAN_FORMAT = "#assets/%s/%d?fromTime=%d&toTime=%d"; //$NON-NLS-1$ // TODO: Query web UI to determine which object types have a web page and the URN for each page. private static final Map PAGE_MAP = new HashMap(); static { // TEMP: For now, the supported web pages are hard-coded. PAGE_MAP.put (DataObjectTypeEnum.APPLICATION, "applications"); //$NON-NLS-1$ PAGE_MAP.put (DataObjectTypeEnum.DATA_STORE, "dataStores"); //$NON-NLS-1$ PAGE_MAP.put (DataObjectTypeEnum.DISK, "disks"); //$NON-NLS-1$ PAGE_MAP.put (DataObjectTypeEnum.HOST, "hosts"); //$NON-NLS-1$ PAGE_MAP.put (DataObjectTypeEnum.INTERNAL_VOLUME, "internalVolumes"); //$NON-NLS-1$ PAGE_MAP.put (DataObjectTypeEnum.QTREE, "qtrees"); //$NON-NLS-1$ PAGE_MAP.put (DataObjectTypeEnum.STORAGE, "storages"); //$NON-NLS-1$ PAGE_MAP.put (DataObjectTypeEnum.STORAGE_NODE, "storageNodes"); //$NON-NLS-1$ PAGE_MAP.put (DataObjectTypeEnum.STORAGE_POOL, "storagePools"); //$NON-NLS-1$ PAGE_MAP.put (DataObjectTypeEnum.VIRTUAL_MACHINE, "virtualMachines"); //$NON-NLS-1$ PAGE_MAP.put (DataObjectTypeEnum.VIRTUAL_MACHINE_DISK, "vmdks"); //$NON-NLS-1$ PAGE_MAP.put (DataObjectTypeEnum.VOLUME, "volumes"); //$NON-NLS-1$ PAGE_MAP.put (DataObjectTypeEnum.SWITCH, "switches"); //$NON-NLS-1$ PAGE_MAP.put (DataObjectTypeEnum.PORT, "ports"); //$NON-NLS-1$ } /** * Create action to show web page for selection * @param actionInitInfo Provides action command label * @param resources {@link ResourceBundle} * @param targetView View on which the action is invoked * @throws IllegalInitException */ public VisitWebPageAction (ActionInitInfo actionInitInfo, ResourceBundle resources, ViewDirector targetView) throws IllegalInitException { super (actionInitInfo, resources, targetView); } /** * Indicates if action can be performed on an adaptable, i.e., whether the menu command * should be enabled. * @param adaptable data object whose web page is to be visited * @return {@code true} if action can be performed */ public static boolean hasPage (IAdaptable adaptable) { if (adaptable == null) { return false; } // Ensure a DataObject is selected. IDataObjectType dataObjectType = OnaroAdapterUtils.getAdapter (adaptable, IDataObjectType.class); if (dataObjectType == null) { return false; } // Ensure selected data object has a web page. DataObjectTypeEnum typeEnum = dataObjectType.getDataObjectTypeEnum(); return PAGE_MAP.containsKey (typeEnum); } /** * Visits the web page corresponding to an adaptable (data object). * @param adaptable data object whose web page is to be visited * @param view view in which action is performed (used for logging action) * @param timespan {@link TimeSpan} for which performance data should be displayed; * may be {@code null} to indicate default time span */ public static void visitPage (IAdaptable adaptable, ViewDirector view, TimeSpan timespan) { if (adaptable == null) { logger.error ("Can't visit web page. No object is selected."); //$NON-NLS-1$ return; } // Get object type. IDataObjectType dataObjectType = OnaroAdapterUtils.getAdapter (adaptable, IDataObjectType.class); DataObjectTypeEnum typeEnum = dataObjectType.getDataObjectTypeEnum(); // Get object ID. final Long objectId = OnaroAdapterUtils.getAdapter (adaptable, Long.class); if (objectId == null) { logger.error ("Can't visit web page. Object ID is null"); //$NON-NLS-1$ return; } // Launch web site. String urlStr = getUrl (typeEnum, objectId, timespan); if (urlStr != null) { LaunchUrl.launch (urlStr); } // Log usage. String objectTypeDisplay = dataObjectType.getDataObjectClass().getSimpleName(); UsageLog.log (UsageLogType.VISIT_WEB_PAGE, "targetView", view.getName(), //$NON-NLS-1$ "targetObjectType", objectTypeDisplay); //$NON-NLS-1$ } /** * Gets the OCI URL for a data object. * @param typeEnum {@link DataObjectTypeEnum} * @param objectId * @param timespan {@link TimeSpan} for which performance data should be displayed; * may be {@code null} to indicate default time span * @return URL string */ public static String getUrl (DataObjectTypeEnum typeEnum, Long objectId, TimeSpan timespan) { // Get web page resource for object type. String resource = PAGE_MAP.get (typeEnum); if (resource == null) { // isSelected method should have prevented getting here. logger.error ("Can't visit web page for selection type " + typeEnum.name()); //$NON-NLS-1$ return null; } // Compose URL. final String urnStr = timespan == null? String.format (URN_FORMAT, resource, objectId): String.format (URN_TIMESPAN_FORMAT, resource, objectId, Long.valueOf(timespan.getFromTime().getTime()), Long.valueOf(timespan.getToTime().getTime())); return LaunchUrl.makeUrlRelativeToWebUi (urnStr); } @Override protected boolean isSelected (ObjectSelectionModel selectionModel) { return hasPage (selectionModel.getFirstSelectedAdaptable()); } @Override public void actionPerformed (ActionEvent e) { visitPage (getSelectedAdaptable(), targetView, targetView.getNavigationInfo().getTimeSpan()); } }