use of org.opennms.netmgt.model.ResourceId in project opennms by OpenNMS.
the class ResourceDaoIntegrityIT method walkResourceTree.
@Test
@Transactional
public void walkResourceTree() throws IOException {
// Setup the file tree and the necessary objects in the DAOs
createResourceTree();
createNodes();
Map<String, ResourceType> types = createResourceTypes();
expect(m_resourceTypesDao.getLastUpdate()).andReturn(new Date(System.currentTimeMillis())).anyTimes();
expect(m_resourceTypesDao.getResourceTypes()).andReturn(types).anyTimes();
m_easyMockUtils.replayAll();
m_resourceDao.afterPropertiesSet();
// Walk the tree and collect the results
ResourceCollector visitor = new ResourceCollector();
ResourceTreeWalker walker = new ResourceTreeWalker();
walker.setResourceDao(m_resourceDao);
walker.setVisitor(visitor);
walker.walk();
// We must have at least one resource for every known type
for (OnmsResourceType type : m_resourceDao.getResourceTypes()) {
// Ignore this type for now #needstoomanydbojects
if (type.getName() == DistributedStatusResourceType.TYPE_NAME) {
continue;
}
assertTrue("No resources of type: " + type.getLabel(), visitor.resourceTypes.contains(type));
}
// We must be able to retrieve the same resource by id
for (Entry<ResourceId, OnmsResource> entry : visitor.resourcesById.entrySet()) {
OnmsResource resourceRetrievedById = m_resourceDao.getResourceById(entry.getKey());
assertNotNull(String.format("Failed to retrieve resource with id '%s'.", entry.getKey()), resourceRetrievedById);
assertEquals(String.format("Result mismatch for resource with id '%s'. Retrieved id is '%s'.", entry.getKey(), resourceRetrievedById.getId()), entry.getValue().getName(), resourceRetrievedById.getName());
}
// Build a line that represent the resource for every unique id
// and compare it to the known results
int k = 0;
String[] expectedResults = loadExpectedResults();
for (Entry<ResourceId, OnmsResource> entry : visitor.resourcesById.entrySet()) {
// Convert the attributes to strings and order them lexicographically
Set<String> attributeNames = new TreeSet<String>();
for (OnmsAttribute attribute : entry.getValue().getAttributes()) {
attributeNames.add(attribute.toString());
}
// Compare
String actualResult = entry.getKey() + ": " + attributeNames;
assertEquals(String.format("Result mismatch at line %d.", k + 1), expectedResults[k], actualResult);
k++;
}
// We should have as many unique resource ids as we have results
assertEquals(expectedResults.length, visitor.resourcesById.size());
m_easyMockUtils.verifyAll();
}
use of org.opennms.netmgt.model.ResourceId in project opennms by OpenNMS.
the class GraphResultsController method handleRequestInternal.
/** {@inheritDoc} */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
String[] requiredParameters = new String[] { "resourceId", "reports" };
for (String requiredParameter : requiredParameters) {
if (request.getParameter(requiredParameter) == null) {
throw new MissingParameterException(requiredParameter, requiredParameters);
}
}
ResourceId[] resourceIds = Arrays.stream(request.getParameterValues("resourceId")).map(ResourceId::fromString).toArray(ResourceId[]::new);
String[] reports = request.getParameterValues("reports");
// see if the start and end time were explicitly set as params
String start = request.getParameter("start");
String end = request.getParameter("end");
String relativeTime = request.getParameter("relativetime");
final String startMonth = request.getParameter("startMonth");
final String startDate = request.getParameter("startDate");
final String startYear = request.getParameter("startYear");
final String startHour = request.getParameter("startHour");
final String endMonth = request.getParameter("endMonth");
final String endDate = request.getParameter("endDate");
final String endYear = request.getParameter("endYear");
final String endHour = request.getParameter("endHour");
long startLong = 0;
long endLong = 0;
if (start != null || end != null) {
String[] ourRequiredParameters = new String[] { "start", "end" };
if (start == null) {
throw new MissingParameterException("start", ourRequiredParameters);
}
if (end == null) {
throw new MissingParameterException("end", ourRequiredParameters);
}
//The following is very similar to RrdGraphController.parseTimes, but modified for the local context a bit
// There's merging possibilities, but I don't know how (common parent class seems wrong; service bean for a single
// method isn't much better. Ideas?
//Try a simple 'long' parsing. If either fails, do a full parse. If one is a straight 'long' but the other isn't
// that's fine, the TimeParser code will handle it fine (as long as we convert milliseconds to seconds)
// Indeed, we *have* to use TimeParse for both to ensure any relative references (using "start" or "end") work correctly.
// NB: can't do a "safe" parsing using the WebSecurityUtils; if we did, it would filter out all the possible rrdfetch
// format text and always work :)
boolean startIsInteger = false;
boolean endIsInteger = false;
// is expected for epoch times by TimeParser
try {
startLong = Long.valueOf(start);
startIsInteger = true;
start = "" + (startLong / 1000);
} catch (NumberFormatException e) {
}
try {
endLong = Long.valueOf(end);
endIsInteger = true;
end = "" + (endLong / 1000);
} catch (NumberFormatException e) {
}
if (!endIsInteger || !startIsInteger) {
//One or both of start/end aren't integers, so we need to do full parsing using TimeParser
TimeParser startParser = new TimeParser(start);
TimeParser endParser = new TimeParser(end);
try {
TimeSpec specStart = startParser.parse();
TimeSpec specEnd = endParser.parse();
long[] results = TimeSpec.getTimestamps(specStart, specEnd);
//Multiply by 1000. TimeSpec returns timestamps in Seconds, not Milliseconds.
startLong = results[0] * 1000;
endLong = results[1] * 1000;
} catch (RrdException e1) {
throw new IllegalArgumentException("Could not parse start '" + start + "' and end '" + end + "' as valid time specifications", e1);
}
}
} else if (startMonth != null || startDate != null || startYear != null || startHour != null || endMonth != null || endDate != null || endYear != null || endHour != null) {
String[] ourRequiredParameters = new String[] { "startMonth", "startDate", "startYear", "startHour", "endMonth", "endDate", "endYear", "endHour" };
for (String requiredParameter : ourRequiredParameters) {
if (request.getParameter(requiredParameter) == null) {
throw new MissingParameterException(requiredParameter, ourRequiredParameters);
}
}
Calendar startCal = Calendar.getInstance();
startCal.set(Calendar.MONTH, WebSecurityUtils.safeParseInt(startMonth));
startCal.set(Calendar.DATE, WebSecurityUtils.safeParseInt(startDate));
startCal.set(Calendar.YEAR, WebSecurityUtils.safeParseInt(startYear));
startCal.set(Calendar.HOUR_OF_DAY, WebSecurityUtils.safeParseInt(startHour));
startCal.set(Calendar.MINUTE, 0);
startCal.set(Calendar.SECOND, 0);
startCal.set(Calendar.MILLISECOND, 0);
Calendar endCal = Calendar.getInstance();
endCal.set(Calendar.MONTH, WebSecurityUtils.safeParseInt(endMonth));
endCal.set(Calendar.DATE, WebSecurityUtils.safeParseInt(endDate));
endCal.set(Calendar.YEAR, WebSecurityUtils.safeParseInt(endYear));
endCal.set(Calendar.HOUR_OF_DAY, WebSecurityUtils.safeParseInt(endHour));
endCal.set(Calendar.MINUTE, 0);
endCal.set(Calendar.SECOND, 0);
endCal.set(Calendar.MILLISECOND, 0);
startLong = startCal.getTime().getTime();
endLong = endCal.getTime().getTime();
} else {
if (relativeTime == null) {
relativeTime = s_periods[0].getId();
}
RelativeTimePeriod period = RelativeTimePeriod.getPeriodByIdOrDefault(s_periods, relativeTime, s_periods[0]);
long[] times = period.getStartAndEndTimes();
startLong = times[0];
endLong = times[1];
}
// The 'matching' parameter is going to work only for one resource.
String matching = request.getParameter("matching");
if (matching != null) {
reports = getSuggestedReports(resourceIds[0], matching);
}
ModelAndView modelAndView = null;
try {
GraphResults model = m_graphResultsService.findResults(resourceIds, reports, startLong, endLong, relativeTime);
modelAndView = new ModelAndView("/graph/results", "results", model);
} catch (Exception e) {
LOG.warn("Can't get graph results", e);
modelAndView = new ModelAndView("/graph/results-error");
}
modelAndView.addObject("loggedIn", request.getRemoteUser() != null);
return modelAndView;
}
use of org.opennms.netmgt.model.ResourceId in project opennms by OpenNMS.
the class RrdGraphController method handleRequestInternal.
/** {@inheritDoc} */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
String[] requiredParameters = new String[] { "resourceId", "start", "end" };
for (String requiredParameter : requiredParameters) {
if (request.getParameter(requiredParameter) == null) {
throw new MissingParameterException(requiredParameter, requiredParameters);
}
}
ResourceId resourceId = ResourceId.fromString(request.getParameter("resourceId"));
long[] times = this.parseTimes(request);
long startTime = times[0];
long endTime = times[1];
InputStream tempIn;
if ("true".equals(request.getParameter("adhoc"))) {
String[] adhocRequiredParameters = new String[] { "title", "ds", "agfunction", "color", "dstitle", "style" };
for (String requiredParameter : adhocRequiredParameters) {
if (request.getParameter(requiredParameter) == null) {
throw new MissingParameterException(requiredParameter, adhocRequiredParameters);
}
}
String title = request.getParameter("title");
String[] dataSources = request.getParameterValues("ds");
String[] aggregateFunctions = request.getParameterValues("agfunction");
String[] colors = request.getParameterValues("color");
String[] dataSourceTitles = request.getParameterValues("dstitle");
String[] styles = request.getParameterValues("style");
tempIn = m_rrdGraphService.getAdhocGraph(resourceId, title, dataSources, aggregateFunctions, colors, dataSourceTitles, styles, startTime, endTime);
} else {
String report = request.getParameter("report");
if (report == null) {
throw new MissingParameterException("report");
}
String width = request.getParameter("width");
String height = request.getParameter("height");
tempIn = m_rrdGraphService.getPrefabGraph(resourceId, report, startTime, endTime, width != null && !width.isEmpty() ? Integer.valueOf(width) : null, height != null && !height.isEmpty() ? Integer.valueOf(height) : null);
}
response.setContentType("image/png");
StreamUtils.streamToStream(tempIn, response.getOutputStream());
tempIn.close();
return null;
}
use of org.opennms.netmgt.model.ResourceId in project opennms by OpenNMS.
the class NewtsFetchStrategy method fetch.
@Override
public FetchResults fetch(long start, long end, long step, int maxrows, Long interval, Long heartbeat, List<Source> sources, boolean relaxed) {
final LateAggregationParams lag = getLagParams(step, interval, heartbeat);
final Optional<Timestamp> startTs = Optional.of(Timestamp.fromEpochMillis(start));
final Optional<Timestamp> endTs = Optional.of(Timestamp.fromEpochMillis(end));
final Map<String, Object> constants = Maps.newHashMap();
// Group the sources by resource id to avoid calling the ResourceDao
// multiple times for the same resource
Map<ResourceId, List<Source>> sourcesByResourceId = sources.stream().collect(Collectors.groupingBy((source) -> ResourceId.fromString(source.getResourceId())));
// Lookup the OnmsResources in parallel
Map<ResourceId, Future<OnmsResource>> resourceFuturesById = Maps.newHashMapWithExpectedSize(sourcesByResourceId.size());
for (ResourceId resourceId : sourcesByResourceId.keySet()) {
resourceFuturesById.put(resourceId, threadPool.submit(getResourceByIdCallable(resourceId)));
}
// Gather the results, fail if any of the resources were not found
Map<OnmsResource, List<Source>> sourcesByResource = Maps.newHashMapWithExpectedSize(sourcesByResourceId.size());
for (Entry<ResourceId, Future<OnmsResource>> entry : resourceFuturesById.entrySet()) {
try {
OnmsResource resource = entry.getValue().get();
if (resource == null) {
if (relaxed)
continue;
LOG.error("No resource with id: {}", entry.getKey());
return null;
}
sourcesByResource.put(resource, sourcesByResourceId.get(entry.getKey()));
} catch (ExecutionException | InterruptedException e) {
throw Throwables.propagate(e);
}
}
// Now group the sources by Newts Resource ID, which differs from the OpenNMS Resource ID.
Map<String, List<Source>> sourcesByNewtsResourceId = Maps.newHashMap();
for (Entry<OnmsResource, List<Source>> entry : sourcesByResource.entrySet()) {
final OnmsResource resource = entry.getKey();
for (Source source : entry.getValue()) {
// Gather the values from strings.properties
Utils.convertStringAttributesToConstants(source.getLabel(), resource.getStringPropertyAttributes(), constants);
// Grab the attribute that matches the source
RrdGraphAttribute rrdGraphAttribute = resource.getRrdGraphAttributes().get(source.getAttribute());
if (rrdGraphAttribute == null && !Strings.isNullOrEmpty(source.getFallbackAttribute())) {
LOG.error("No attribute with name '{}', using fallback-attribute with name '{}'", source.getAttribute(), source.getFallbackAttribute());
source.setAttribute(source.getFallbackAttribute());
source.setFallbackAttribute(null);
rrdGraphAttribute = resource.getRrdGraphAttributes().get(source.getAttribute());
}
if (rrdGraphAttribute == null) {
if (relaxed)
continue;
LOG.error("No attribute with name: {}", source.getAttribute());
return null;
}
// The Newts Resource ID is stored in the rrdFile attribute
String newtsResourceId = rrdGraphAttribute.getRrdRelativePath();
// Remove the file separator prefix, added by the RrdGraphAttribute class
if (newtsResourceId.startsWith(File.separator)) {
newtsResourceId = newtsResourceId.substring(File.separator.length(), newtsResourceId.length());
}
List<Source> listOfSources = sourcesByNewtsResourceId.get(newtsResourceId);
// Create the list if it doesn't exist
if (listOfSources == null) {
listOfSources = Lists.newLinkedList();
sourcesByNewtsResourceId.put(newtsResourceId, listOfSources);
}
listOfSources.add(source);
}
}
// The Newts API only allows us to perform a query using a single (Newts) Resource ID,
// so we perform multiple queries in parallel, and aggregate the results.
Map<String, Future<Collection<Row<Measurement>>>> measurementsByNewtsResourceId = Maps.newHashMapWithExpectedSize(sourcesByNewtsResourceId.size());
for (Entry<String, List<Source>> entry : sourcesByNewtsResourceId.entrySet()) {
measurementsByNewtsResourceId.put(entry.getKey(), threadPool.submit(getMeasurementsForResourceCallable(entry.getKey(), entry.getValue(), startTs, endTs, lag)));
}
long[] timestamps = null;
Map<String, double[]> columns = Maps.newHashMap();
for (Entry<String, Future<Collection<Row<Measurement>>>> entry : measurementsByNewtsResourceId.entrySet()) {
Collection<Row<Measurement>> rows;
try {
rows = entry.getValue().get();
} catch (InterruptedException | ExecutionException e) {
throw Throwables.propagate(e);
}
final int N = rows.size();
if (timestamps == null) {
timestamps = new long[N];
int k = 0;
for (final Row<Measurement> row : rows) {
timestamps[k] = row.getTimestamp().asMillis();
k++;
}
}
int k = 0;
for (Row<Measurement> row : rows) {
for (Measurement measurement : row.getElements()) {
double[] column = columns.get(measurement.getName());
if (column == null) {
column = new double[N];
columns.put(measurement.getName(), column);
}
column[k] = measurement.getValue();
}
k += 1;
}
}
FetchResults fetchResults = new FetchResults(timestamps, columns, lag.getStep(), constants);
if (relaxed) {
Utils.fillMissingValues(fetchResults, sources);
}
LOG.trace("Fetch results: {}", fetchResults);
return fetchResults;
}
use of org.opennms.netmgt.model.ResourceId in project opennms by OpenNMS.
the class DefaultKscReportService method buildNodeReport.
/** {@inheritDoc} */
@Override
public Report buildNodeReport(int node_id) {
ResourceId resourceId = ResourceId.get("node", Integer.toString(node_id));
OnmsResource node = getResourceService().getResourceById(resourceId);
return buildResourceReport(getResourceService(), node, "Node Report for Node Number " + node_id);
}
Aggregations