Search in sources :

Example 16 with ObjectRetrievalFailureException

use of org.springframework.orm.ObjectRetrievalFailureException in project opennms by OpenNMS.

the class CustomViewController method handleRequestInternal.

/** {@inheritDoc} */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String[] requiredParameters = new String[] { "report", "type" };
    // Get Form Variable
    int reportId = -1;
    String reportType = WebSecurityUtils.sanitizeString(request.getParameter(Parameters.type.toString()));
    String reportIdString = WebSecurityUtils.sanitizeString(request.getParameter(Parameters.report.toString()));
    if (reportType == null) {
        throw new MissingParameterException(Parameters.type.toString(), requiredParameters);
    }
    if (reportIdString == null) {
        throw new MissingParameterException(Parameters.report.toString(), requiredParameters);
    }
    if (reportType.equals("node") || reportType.equals("custom")) {
        reportId = WebSecurityUtils.safeParseInt(reportIdString);
    }
    String overrideTimespan = WebSecurityUtils.sanitizeString(request.getParameter(Parameters.timespan.toString()));
    if ("null".equals(overrideTimespan) || "none".equals(overrideTimespan)) {
        overrideTimespan = null;
    }
    String overrideGraphType = WebSecurityUtils.sanitizeString(request.getParameter(Parameters.graphtype.toString()));
    if ("null".equals(overrideGraphType) || "none".equals(overrideGraphType)) {
        overrideGraphType = null;
    }
    // Load report to view 
    Report report = null;
    if ("node".equals(reportType)) {
        LOG.debug("handleRequestInternal: buildNodeReport(reportId) {}", reportId);
        report = getKscReportService().buildNodeReport(reportId);
    } else if ("nodeSource".equals(reportType)) {
        LOG.debug("handleRequestInternal: buildNodeSourceReport(nodeSource) {}", reportIdString);
        report = getKscReportService().buildNodeSourceReport(reportIdString);
    } else if ("domain".equals(reportType)) {
        LOG.debug("handleRequestInternal: buildDomainReport(reportIdString) {}", reportIdString);
        report = getKscReportService().buildDomainReport(reportIdString);
    } else if ("custom".equals(reportType)) {
        LOG.debug("handleRequestInternal: getReportByIndex(reportId) {}", reportId);
        report = m_kscReportFactory.getReportByIndex(reportId);
        if (report == null) {
            throw new ServletException("Report could not be found in config file for index '" + reportId + "'");
        }
    } else {
        throw new IllegalArgumentException("value to 'type' parameter of '" + reportType + "' is not supported.  Must be one of: node, nodeSource, domain, or custom");
    }
    // Get the list of available prefabricated graph options 
    Map<String, OnmsResource> resourceMap = new HashMap<String, OnmsResource>();
    Set<PrefabGraph> prefabGraphs = new TreeSet<PrefabGraph>();
    if (removeBrokenGraphsFromReport(report) && reportId > -1) {
        m_kscReportFactory.setReport(reportId, report);
        m_kscReportFactory.saveCurrent();
        EventBuilder eb = new EventBuilder(EventConstants.KSC_REPORT_UPDATED_UEI, "Web UI");
        eb.addParam(EventConstants.PARAM_REPORT_TITLE, report.getTitle() == null ? "Report #" + report.getId() : report.getTitle());
        eb.addParam(EventConstants.PARAM_REPORT_GRAPH_COUNT, report.getGraphs().size());
        try {
            Util.createEventProxy().send(eb.getEvent());
        } catch (Throwable e) {
            LOG.error("Can't send event " + eb.getEvent(), e);
        }
    }
    List<Graph> graphCollection = report.getGraphs();
    if (!graphCollection.isEmpty()) {
        for (Graph graph : graphCollection) {
            final OnmsResource resource = getKscReportService().getResourceFromGraph(graph);
            resourceMap.put(graph.toString(), resource);
            if (resource == null) {
                LOG.debug("Could not get resource for graph {} in report {}", graph, report.getTitle());
            } else {
                prefabGraphs.addAll(Arrays.asList(getResourceService().findPrefabGraphsForResource(resource)));
            }
        }
    }
    List<KscResultSet> resultSets = new ArrayList<KscResultSet>(report.getGraphs().size());
    for (Graph graph : graphCollection) {
        OnmsResource resource = resourceMap.get(graph.toString());
        if (resource != null) {
            promoteResourceAttributesIfNecessary(resource);
        }
        String displayGraphType;
        if (overrideGraphType == null) {
            displayGraphType = graph.getGraphtype();
        } else {
            displayGraphType = overrideGraphType;
        }
        PrefabGraph displayGraph;
        try {
            displayGraph = getResourceService().getPrefabGraph(displayGraphType);
        } catch (ObjectRetrievalFailureException e) {
            LOG.debug("The prefabricated graph '{}' does not exist: {}", displayGraphType, e, e);
            displayGraph = null;
        }
        boolean foundGraph = false;
        if (resource != null) {
            for (PrefabGraph availableGraph : getResourceService().findPrefabGraphsForResource(resource)) {
                if (availableGraph.equals(displayGraph)) {
                    foundGraph = true;
                    break;
                }
            }
        }
        if (!foundGraph) {
            displayGraph = null;
        }
        // gather start/stop time information
        String displayTimespan = null;
        if (overrideTimespan == null) {
            displayTimespan = graph.getTimespan();
        } else {
            displayTimespan = overrideTimespan;
        }
        Calendar beginTime = Calendar.getInstance();
        Calendar endTime = Calendar.getInstance();
        KSC_PerformanceReportFactory.getBeginEndTime(displayTimespan, beginTime, endTime);
        KscResultSet resultSet = new KscResultSet(graph.getTitle(), beginTime.getTime(), endTime.getTime(), resource, displayGraph);
        resultSets.add(resultSet);
    }
    ModelAndView modelAndView = new ModelAndView("KSC/customView");
    modelAndView.addObject("loggedIn", request.getRemoteUser() != null);
    modelAndView.addObject("reportType", reportType);
    if (report != null) {
        modelAndView.addObject("report", reportIdString);
    }
    modelAndView.addObject("title", report.getTitle());
    modelAndView.addObject("resultSets", resultSets);
    if (report.getShowTimespanButton().orElse(false)) {
        if (overrideTimespan == null || !getKscReportService().getTimeSpans(true).containsKey(overrideTimespan)) {
            modelAndView.addObject("timeSpan", "none");
        } else {
            modelAndView.addObject("timeSpan", overrideTimespan);
        }
        modelAndView.addObject("timeSpans", getKscReportService().getTimeSpans(true));
    } else {
        // Make sure it's null so the pulldown list isn't shown
        modelAndView.addObject("timeSpan", null);
    }
    if (report.getShowGraphtypeButton().orElse(false)) {
        LinkedHashMap<String, String> graphTypes = new LinkedHashMap<String, String>();
        graphTypes.put("none", "none");
        for (PrefabGraph graphOption : prefabGraphs) {
            graphTypes.put(graphOption.getName(), graphOption.getName());
        }
        if (overrideGraphType == null || !graphTypes.containsKey(overrideGraphType)) {
            modelAndView.addObject("graphType", "none");
        } else {
            modelAndView.addObject("graphType", overrideGraphType);
        }
        modelAndView.addObject("graphTypes", graphTypes);
    } else {
        // Make sure it's null so the pulldown list isn't shown
        modelAndView.addObject("graphType", null);
    }
    modelAndView.addObject("showCustomizeButton", (request.isUserInRole(Authentication.ROLE_ADMIN) || !request.isUserInRole(Authentication.ROLE_READONLY)) && (request.getRemoteUser() != null));
    if (report.getGraphsPerLine() != null && report.getGraphsPerLine().get() > 0) {
        modelAndView.addObject("graphsPerLine", report.getGraphsPerLine());
    } else {
        modelAndView.addObject("graphsPerLine", getDefaultGraphsPerLine());
    }
    return modelAndView;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) KscResultSet(org.opennms.web.graph.KscResultSet) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) LinkedHashMap(java.util.LinkedHashMap) ServletException(javax.servlet.ServletException) TreeSet(java.util.TreeSet) ObjectRetrievalFailureException(org.springframework.orm.ObjectRetrievalFailureException) Report(org.opennms.netmgt.config.kscReports.Report) Calendar(java.util.Calendar) OnmsResource(org.opennms.netmgt.model.OnmsResource) PrefabGraph(org.opennms.netmgt.model.PrefabGraph) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) Graph(org.opennms.netmgt.config.kscReports.Graph) PrefabGraph(org.opennms.netmgt.model.PrefabGraph) MissingParameterException(org.opennms.web.servlet.MissingParameterException)

Example 17 with ObjectRetrievalFailureException

use of org.springframework.orm.ObjectRetrievalFailureException in project opennms by OpenNMS.

the class CustomViewController method removeBrokenGraphsFromReport.

// Returns true if the report was modified due to invalid resource IDs. 
private boolean removeBrokenGraphsFromReport(Report report) {
    for (Iterator<Graph> itr = report.getGraphs().iterator(); itr.hasNext(); ) {
        Graph graph = itr.next();
        try {
            OnmsResource r = getKscReportService().getResourceFromGraph(graph);
            if (r == null) {
                LOG.error("Removing graph '{}' in KSC report '{}' because the resource it refers to could not be found. Perhaps resource '{}' (or its ancestor) referenced by this graph no longer exists?", graph.getTitle(), report.getTitle(), graph.getResourceId());
                itr.remove();
                return true;
            }
        } catch (ObjectRetrievalFailureException orfe) {
            LOG.error("Removing graph '{}' in KSC report '{}' because the resource it refers to could not be found. Perhaps resource '{}' (or its ancestor) referenced by this graph no longer exists?", graph.getTitle(), report.getTitle(), graph.getResourceId());
            itr.remove();
            return true;
        } catch (Throwable e) {
            LOG.error("Unexpected error while scanning through graphs in report: {}", e.getMessage(), e);
            itr.remove();
            return true;
        }
    }
    return false;
}
Also used : Graph(org.opennms.netmgt.config.kscReports.Graph) PrefabGraph(org.opennms.netmgt.model.PrefabGraph) OnmsResource(org.opennms.netmgt.model.OnmsResource) ObjectRetrievalFailureException(org.springframework.orm.ObjectRetrievalFailureException)

Example 18 with ObjectRetrievalFailureException

use of org.springframework.orm.ObjectRetrievalFailureException in project opennms by OpenNMS.

the class GenericIndexResourceType method createTypes.

protected static Map<String, GenericIndexResourceType> createTypes(Map<String, ResourceType> configuredResourceTypes, ResourceStorageDao resourceStorageDao) {
    Map<String, GenericIndexResourceType> resourceTypes = Maps.newLinkedHashMap();
    List<ResourceType> resourceTypeList = new LinkedList<ResourceType>(configuredResourceTypes.values());
    Collections.sort(resourceTypeList, new Comparator<ResourceType>() {

        @Override
        public int compare(ResourceType r0, ResourceType r1) {
            // Sort by resource label, allowing the resource label to be null
            final Comparator<? super String> comparator = (a, b) -> a.compareTo(b);
            return Objects.compare(r0.getLabel(), r1.getLabel(), Comparator.nullsLast(comparator));
        }
    });
    for (ResourceType resourceType : resourceTypeList) {
        String className = resourceType.getStorageStrategy().getClazz();
        Class<?> cinst;
        try {
            cinst = Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw new ObjectRetrievalFailureException(StorageStrategy.class, className, "Could not load class '" + className + "' for resource type '" + resourceType.getName() + "'", e);
        }
        StorageStrategy storageStrategy;
        try {
            storageStrategy = (StorageStrategy) cinst.newInstance();
        } catch (InstantiationException e) {
            throw new ObjectRetrievalFailureException(StorageStrategy.class, className, "Could not instantiate class '" + className + "' for resource type '" + resourceType.getName() + "'", e);
        } catch (IllegalAccessException e) {
            throw new ObjectRetrievalFailureException(StorageStrategy.class, className, "Could not instantiate class '" + className + "' for resource type '" + resourceType.getName() + "'", e);
        }
        storageStrategy.setResourceTypeName(resourceType.getName());
        GenericIndexResourceType genericIndexResourceType = new GenericIndexResourceType(resourceStorageDao, resourceType.getName(), resourceType.getLabel(), resourceType.getResourceLabel(), storageStrategy);
        resourceTypes.put(genericIndexResourceType.getName(), genericIndexResourceType);
    }
    return resourceTypes;
}
Also used : StorageStrategy(org.opennms.netmgt.collection.api.StorageStrategy) ResourceType(org.opennms.netmgt.config.datacollection.ResourceType) OnmsResourceType(org.opennms.netmgt.model.OnmsResourceType) LinkedList(java.util.LinkedList) Comparator(java.util.Comparator) ObjectRetrievalFailureException(org.springframework.orm.ObjectRetrievalFailureException)

Example 19 with ObjectRetrievalFailureException

use of org.springframework.orm.ObjectRetrievalFailureException in project opennms by OpenNMS.

the class DefaultSurveillanceService method createNodePageUrl.

/*
     * This creates a relative url to the node list page and sets the category
     * parameters to show the categories for this cell.
     * FIXME: this code should move to the jsp after the status table is
     * enhanced to support this requirement.
     */
private String createNodePageUrl(final SurveillanceView view, final int colIndex, final int rowIndex) {
    Set<OnmsCategory> columns = Collections.emptySet();
    Set<OnmsCategory> rows = Collections.emptySet();
    try {
        columns = view.getCategoriesForColumn(colIndex);
    } catch (final ObjectRetrievalFailureException e) {
        LOG.warn("An error occurred while getting categories for view {}, column {}", view, colIndex);
    }
    try {
        rows = view.getCategoriesForRow(rowIndex);
    } catch (final ObjectRetrievalFailureException e) {
        LOG.warn("An error occurred while getting categories for view {}, row {}", view, rowIndex);
    }
    final List<String> params = new ArrayList<String>(columns.size() + rows.size());
    for (final OnmsCategory category : columns) {
        params.add("category1=" + Util.encode(category.getName()));
    }
    for (final OnmsCategory category : rows) {
        params.add("category2=" + Util.encode(category.getName()));
    }
    params.add("nodesWithDownAggregateStatus=true");
    return "element/nodeList.htm?" + StringUtils.collectionToDelimitedString(params, "&");
}
Also used : OnmsCategory(org.opennms.netmgt.model.OnmsCategory) ArrayList(java.util.ArrayList) ObjectRetrievalFailureException(org.springframework.orm.ObjectRetrievalFailureException)

Example 20 with ObjectRetrievalFailureException

use of org.springframework.orm.ObjectRetrievalFailureException in project opennms by OpenNMS.

the class DefaultSiteStatusViewService method getCategoriesForRowDef.

private Set<OnmsCategory> getCategoriesForRowDef(RowDef rowDef) {
    Set<OnmsCategory> categories = new LinkedHashSet<OnmsCategory>();
    //Loop over the defined categories and create model categories (OnmsCategory)
    List<Category> cats = rowDef.getCategories();
    for (Category cat : cats) {
        OnmsCategory category = m_categoryDao.findByName(cat.getName());
        if (category == null) {
            throw new ObjectRetrievalFailureException(OnmsCategory.class, cat.getName(), "Unable to locate OnmsCategory named: " + cat.getName() + " as specified in the site status view configuration file", null);
        }
        categories.add(category);
    }
    return categories;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) OnmsCategory(org.opennms.netmgt.model.OnmsCategory) Category(org.opennms.netmgt.config.siteStatusViews.Category) OnmsCategory(org.opennms.netmgt.model.OnmsCategory) ObjectRetrievalFailureException(org.springframework.orm.ObjectRetrievalFailureException)

Aggregations

ObjectRetrievalFailureException (org.springframework.orm.ObjectRetrievalFailureException)22 OnmsResource (org.opennms.netmgt.model.OnmsResource)7 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)7 StorageStrategy (org.opennms.netmgt.collection.api.StorageStrategy)4 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Graph (org.opennms.netmgt.config.kscReports.Graph)2 OnmsCategory (org.opennms.netmgt.model.OnmsCategory)2 OnmsLocationMonitor (org.opennms.netmgt.model.OnmsLocationMonitor)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 OnmsResourceType (org.opennms.netmgt.model.OnmsResourceType)2 ResourcePath (org.opennms.netmgt.model.ResourcePath)2 Resource (org.springframework.core.io.Resource)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1