Skip to content

Commit

Permalink
ReST API Enhancements
Browse files Browse the repository at this point in the history
EventsRestService: provide an ability to filter by asset record or
service type, for example:

/opennms/rest/events?serviceType.name=ICMP
/opennms/rest/events?assetRecord.zip=27312

OnmsIpInterfaceResource: provide an ability to filter by service type,
for example:

/opennms/rest/nodes/10/ipinterfaces?serviceType.name=ICMP

NotificationRestService: provide an ability to filter by node, IP
interface, SNMP interface, events, or users notified, for example:

/opennms/rest/notifications?node.foreignSource=Routers
/opennms/rest/notifications?ipInterface.ipAddress=10.10.10.10
/opennms/rest/notifications?snmpInterface.ifDescr=eth0
/opennms/rest/notifications?event.severity=5
/opennms/rest/notifications?usersNotified.userId=agalue
  • Loading branch information
agalue committed Mar 25, 2014
1 parent 03c0519 commit 21a2e90
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 19 deletions.
Expand Up @@ -48,6 +48,7 @@

import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.opennms.core.criteria.Alias.JoinType;
import org.opennms.core.criteria.CriteriaBuilder;
import org.opennms.netmgt.dao.api.EventDao;
import org.opennms.netmgt.model.OnmsEvent;
Expand Down Expand Up @@ -134,8 +135,7 @@ public OnmsEventCollection getEvents() throws ParseException {
readLock();

try {
final CriteriaBuilder builder = new CriteriaBuilder(OnmsEvent.class);
applyQueryFilters(m_uriInfo.getQueryParameters(), builder);
final CriteriaBuilder builder = getCriteriaBuilder(m_uriInfo.getQueryParameters());
builder.orderBy("eventTime").asc();

final OnmsEventCollection coll = new OnmsEventCollection(m_eventDao.findMatching(builder.toCriteria()));
Expand Down Expand Up @@ -163,7 +163,6 @@ public OnmsEventCollection getEventsBetween() throws ParseException {
readLock();

try {
final CriteriaBuilder builder = new CriteriaBuilder(OnmsEvent.class);
final MultivaluedMap<String, String> params = m_uriInfo.getQueryParameters();

final String column;
Expand Down Expand Up @@ -196,7 +195,7 @@ public OnmsEventCollection getEventsBetween() throws ParseException {
end = new Date();
}

applyQueryFilters(params, builder);
final CriteriaBuilder builder = getCriteriaBuilder(params);
builder.match("all");
try {
builder.between(column, begin, end);
Expand Down Expand Up @@ -263,8 +262,7 @@ public Response updateEvents(final MultivaluedMapImpl formProperties) {
formProperties.remove("ack");
}

final CriteriaBuilder builder = new CriteriaBuilder(OnmsEvent.class);
applyQueryFilters(formProperties, builder);
final CriteriaBuilder builder = getCriteriaBuilder(formProperties);
builder.orderBy("eventTime").desc();

for (final OnmsEvent event : m_eventDao.findMatching(builder.toCriteria())) {
Expand All @@ -286,4 +284,16 @@ private void processEventAck(final OnmsEvent event, final Boolean ack) {
}
m_eventDao.save(event);
}

private CriteriaBuilder getCriteriaBuilder(final MultivaluedMap<String, String> params) {
final CriteriaBuilder builder = new CriteriaBuilder(OnmsEvent.class);
builder.alias("node", "node", JoinType.LEFT_JOIN);
builder.alias("node.snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN);
builder.alias("node.ipInterfaces", "ipInterface", JoinType.LEFT_JOIN);
builder.alias("serviceType", "serviceType", JoinType.LEFT_JOIN);

applyQueryFilters(params, builder);
return builder;
}

}
Expand Up @@ -117,15 +117,10 @@ public OnmsNodeList getNodes() {
readLock();

try {
final CriteriaBuilder builder = new CriteriaBuilder(OnmsNode.class);
builder.alias("snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN);
builder.alias("ipInterfaces", "ipInterface", JoinType.LEFT_JOIN);
builder.alias("categories", "category", JoinType.LEFT_JOIN);

final MultivaluedMap<String, String> params = m_uriInfo.getQueryParameters();
final String type = params.getFirst("type");
applyQueryFilters(params, builder);

final CriteriaBuilder builder = getCriteriaBuilder(params);
builder.orderBy("label").asc();

final Criteria crit = builder.toCriteria();
Expand Down Expand Up @@ -421,12 +416,23 @@ private OnmsCategory getCategory(OnmsNode node, String categoryName) {
return null;
}


private void sendEvent(final String uei, final int nodeId, String nodeLabel) throws EventProxyException {
final EventBuilder bldr = new EventBuilder(uei, getClass().getName());
bldr.setNodeid(nodeId);
bldr.addParam("nodelabel", nodeLabel);
m_eventProxy.send(bldr.getEvent());
}

private CriteriaBuilder getCriteriaBuilder(final MultivaluedMap<String, String> params) {
final CriteriaBuilder builder = new CriteriaBuilder(OnmsNode.class);
builder.alias("snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN);
builder.alias("ipInterfaces", "ipInterface", JoinType.LEFT_JOIN);
builder.alias("categories", "category", JoinType.LEFT_JOIN);
builder.alias("assetRecord", "assetRecord", JoinType.LEFT_JOIN);
builder.alias("ipInterfaces.monitoredServices.serviceType", "serviceType", JoinType.LEFT_JOIN);

applyQueryFilters(params, builder);
return builder;
}

}
Expand Up @@ -39,10 +39,12 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;

import org.opennms.core.criteria.Alias.JoinType;
import org.opennms.core.criteria.CriteriaBuilder;
import org.opennms.netmgt.dao.api.NotificationDao;
import org.opennms.netmgt.model.OnmsNotification;
Expand Down Expand Up @@ -125,8 +127,7 @@ public OnmsNotificationCollection getNotifications() {
readLock();

try {
final CriteriaBuilder builder = new CriteriaBuilder(OnmsNotification.class);
applyQueryFilters(m_uriInfo.getQueryParameters(), builder);
final CriteriaBuilder builder = getCriteriaBuilder(m_uriInfo.getQueryParameters());
builder.orderBy("notifyId").desc();

OnmsNotificationCollection coll = new OnmsNotificationCollection(m_notifDao.findMatching(builder.toCriteria()));
Expand Down Expand Up @@ -182,8 +183,7 @@ public Response updateNotifications(final MultivaluedMapImpl params) {
params.remove("ack");
}

final CriteriaBuilder builder = new CriteriaBuilder(OnmsNotification.class);
applyQueryFilters(params, builder);
final CriteriaBuilder builder = getCriteriaBuilder(params);

for (final OnmsNotification notif : m_notifDao.findMatching(builder.toCriteria())) {
processNotifAck(notif, ack);
Expand All @@ -194,7 +194,6 @@ public Response updateNotifications(final MultivaluedMapImpl params) {
}
}


private void processNotifAck(final OnmsNotification notif, final Boolean ack) {
if(ack) {
notif.setRespondTime(new Date());
Expand All @@ -206,4 +205,16 @@ private void processNotifAck(final OnmsNotification notif, final Boolean ack) {
m_notifDao.save(notif);
}

private CriteriaBuilder getCriteriaBuilder(final MultivaluedMap<String, String> params) {
final CriteriaBuilder builder = new CriteriaBuilder(OnmsNotification.class);
builder.alias("node", "node", JoinType.LEFT_JOIN);
builder.alias("node.snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN);
builder.alias("node.ipInterfaces", "ipInterface", JoinType.LEFT_JOIN);
builder.alias("event", "event", JoinType.LEFT_JOIN);
builder.alias("usersNotified", "usersNotified", JoinType.LEFT_JOIN);

applyQueryFilters(params, builder);
return builder;
}

}
Expand Up @@ -47,6 +47,7 @@
import javax.ws.rs.core.UriInfo;

import org.opennms.core.criteria.CriteriaBuilder;
import org.opennms.core.criteria.Alias.JoinType;
import org.opennms.core.utils.InetAddressUtils;
import org.opennms.netmgt.EventConstants;
import org.opennms.netmgt.dao.api.IpInterfaceDao;
Expand Down Expand Up @@ -112,6 +113,7 @@ public OnmsIpInterfaceList getIpInterfaces(@PathParam("nodeCriteria") final Stri
final MultivaluedMap<String,String> params = m_uriInfo.getQueryParameters();

final CriteriaBuilder builder = new CriteriaBuilder(OnmsIpInterface.class);
builder.alias("monitoredServices.serviceType", "serviceType", JoinType.LEFT_JOIN);
builder.ne("isManaged", "D");
builder.limit(20);
applyQueryFilters(params, builder);
Expand Down
@@ -0,0 +1,86 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2012 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/

package org.opennms.web.rest;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.opennms.core.test.MockLogAppender;
import org.opennms.core.test.OpenNMSJUnit4ClassRunner;
import org.opennms.core.test.db.annotations.JUnitTemporaryDatabase;
import org.opennms.core.test.rest.AbstractSpringJerseyRestTestCase;
import org.opennms.netmgt.dao.DatabasePopulator;
import org.opennms.test.JUnitConfigurationEnvironment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

@RunWith(OpenNMSJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={
"classpath:/org/opennms/web/rest/applicationContext-test.xml",
"classpath:/META-INF/opennms/applicationContext-commonConfigs.xml",
"classpath:/META-INF/opennms/applicationContext-soa.xml",
"classpath:/META-INF/opennms/applicationContext-dao.xml",
"classpath*:/META-INF/opennms/component-service.xml",
"classpath*:/META-INF/opennms/component-dao.xml",
"classpath:/META-INF/opennms/applicationContext-reportingCore.xml",
"classpath:/META-INF/opennms/applicationContext-databasePopulator.xml",
"classpath:/org/opennms/web/svclayer/applicationContext-svclayer.xml",
"classpath:/META-INF/opennms/applicationContext-mockEventProxy.xml",
"classpath:/applicationContext-jersey-test.xml",
"classpath:/META-INF/opennms/applicationContext-reporting.xml",
"classpath:/META-INF/opennms/applicationContext-mock-usergroup.xml",
"classpath:/META-INF/opennms/applicationContext-minimal-conf.xml",
"file:src/main/webapp/WEB-INF/applicationContext-spring-security.xml",
"file:src/main/webapp/WEB-INF/applicationContext-jersey.xml"
})
@JUnitConfigurationEnvironment
@JUnitTemporaryDatabase
@Transactional
public class NotificationRestServiceTest extends AbstractSpringJerseyRestTestCase {
private DatabasePopulator m_databasePopulator;

@Override
protected void afterServletStart() {
MockLogAppender.setupLogging(true, "DEBUG");
final WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
m_databasePopulator = context.getBean("databasePopulator", DatabasePopulator.class);
m_databasePopulator.populateDatabase();
}

@Test
public void testNotifications() throws Exception {
String xml = sendRequest(GET, "/notifications", 200);
assertTrue(xml.contains("This is a test notification"));
}
}

0 comments on commit 21a2e90

Please sign in to comment.