Skip to content

Commit

Permalink
Fix for Bug NMS-8823
Browse files Browse the repository at this point in the history
Outage ReST service forNode use case calculates dates incorrectly
  • Loading branch information
agalue committed Nov 3, 2016
1 parent 0158b40 commit 34c08bf
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
Expand Up @@ -31,10 +31,12 @@
import java.util.Date;
import java.util.List;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
Expand All @@ -44,12 +46,14 @@

import org.opennms.core.criteria.Alias.JoinType;
import org.opennms.core.criteria.CriteriaBuilder;
import org.opennms.core.criteria.restrictions.Restrictions;
import org.opennms.netmgt.dao.api.OutageDao;
import org.opennms.netmgt.model.OnmsOutage;
import org.opennms.netmgt.model.OnmsOutageCollection;
import org.opennms.netmgt.model.outage.OutageSummary;
import org.opennms.netmgt.model.outage.OutageSummaryCollection;
import org.opennms.web.rest.support.MultivaluedMapImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -73,6 +77,7 @@
@Component("outageRestService")
@Path("outages")
public class OutageRestService extends OnmsRestService {
private static final Logger LOG = LoggerFactory.getLogger(OutageRestService.class);

@Autowired
private OutageDao m_outageDao;
Expand Down Expand Up @@ -147,25 +152,48 @@ public OnmsOutageCollection getOutages(@Context final UriInfo uriInfo) {
* <p>forNodeId</p>
*
* @param nodeId a int.
* @param dateRange a long.
* @param startTs a java.lang.Long.
* @param endTs a java.lang.Long.
* @return a {@link org.opennms.netmgt.model.OnmsOutageCollection} object.
*/
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML})
@Transactional
@Path("forNode/{nodeId}")
public OnmsOutageCollection forNodeId(@Context final UriInfo uriInfo, @PathParam("nodeId") final int nodeId) {
public OnmsOutageCollection forNodeId(@Context final UriInfo uriInfo,
@PathParam("nodeId") final int nodeId,
@DefaultValue("604800000") @QueryParam("dateRange") final long dateRange,
@QueryParam("start") final Long startTs,
@QueryParam("end") final Long endTs) {

final CriteriaBuilder builder = new CriteriaBuilder(OnmsOutage.class);
builder.eq("node.id", nodeId);
final Date d = new Date(System.currentTimeMillis() - (60 * 60 * 24 * 7));
builder.or(Restrictions.isNull("ifRegainedService"), Restrictions.gt("ifRegainedService", d));

builder.alias("monitoredService", "monitoredService");
builder.alias("monitoredService.ipInterface", "ipInterface");
builder.alias("monitoredService.ipInterface.node", "node");
builder.alias("monitoredService.serviceType", "serviceType");

applyQueryFilters(uriInfo.getQueryParameters(), builder);
final MultivaluedMap<String, String> params = new MultivaluedMapImpl();
params.putAll(uriInfo.getQueryParameters());

if (startTs != null && endTs != null) {
params.remove("start");
params.remove("end");
final Date start = new Date(startTs);
final Date end = new Date(endTs);
LOG.debug("Getting all outages from {} to {} for node {}", start, end, nodeId);
builder.gt("ifLostService", start);
builder.lt("ifLostService", end);
} else {
params.remove("dateRange");
final Date start = new Date(System.currentTimeMillis() - dateRange);
LOG.debug("Getting all outgae from {} to current date for node {}", start, nodeId);
builder.gt("ifLostService", start);
}

applyQueryFilters(params, builder);

builder.orderBy("id").desc();

Expand Down
Expand Up @@ -229,6 +229,39 @@ public void testOutageSearches() throws Exception {
assertEquals(1, restObject.getJSONArray("outage").length());
}

@Test
@JUnitTemporaryDatabase
public void testGetOutagesForNode() throws Exception {
// Test last week (no outages)
MockHttpServletRequest jsonRequest = createRequest(m_context, GET, "/outages/forNode/1");
jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
String json = sendRequest(jsonRequest, 200);
JSONObject restObject = new JSONObject(json);
Assert.assertEquals(0, restObject.getJSONArray("outage").length());

// Test a range with outages
long start = 1436846400000l;
long end = 1436932800000l;
jsonRequest = createRequest(m_context, GET, "/outages/forNode/1");
jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
jsonRequest.setQueryString("start=" + start + "&end=" + end);
json = sendRequest(jsonRequest, 200);
restObject = new JSONObject(json);
for (int i=0; i < restObject.getJSONArray("outage").length(); i++) {
JSONObject obj = restObject.getJSONArray("outage").getJSONObject(i);
Assert.assertTrue(obj.getLong("ifLostService") > start);
Assert.assertTrue(obj.getLong("ifLostService") < end);
}

// Test a range without outages
jsonRequest = createRequest(m_context, GET, "/outages/forNode/1");
jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
jsonRequest.setQueryString("start=1436932800000&end=1437019200000");
json = sendRequest(jsonRequest, 200);
restObject = new JSONObject(json);
Assert.assertEquals(0, restObject.getJSONArray("outage").length());
}

@Test
@JUnitTemporaryDatabase
public void testOutagesJson() throws Exception {
Expand Down

0 comments on commit 34c08bf

Please sign in to comment.