use of org.opennms.netmgt.model.OnmsNodeList in project opennms by OpenNMS.
the class NodeRestServiceIT method canGetNodesWithFilterRule.
@Test
@JUnitTemporaryDatabase
public void canGetNodesWithFilterRule() throws Exception {
// Create a node which doesn't have any categories assigned
createNode();
// The FilterDao requires requires the node to have at least one IP interface
createIpInterface();
String url = "/nodes";
// Retrieve all nodes with the 'Routers' category, there should be none
String xml = sendRequest(GET, url, parseParamData("filterRule=catincRouters"), 200);
OnmsNodeList list = JaxbUtils.unmarshal(OnmsNodeList.class, xml);
assertEquals(0, list.size());
assertEquals(Integer.valueOf(0), list.getTotalCount());
// Assign the category to the node
sendRequest(POST, "/nodes/1/categories/Routers", 201);
xml = sendRequest(GET, "/nodes/1/categories", 200);
assertTrue(xml.contains("name=\"Routers\""));
// Now apply try searching with the filter again, we should get a match
xml = sendRequest(GET, url, parseParamData("filterRule=catincRouters"), 200);
list = JaxbUtils.unmarshal(OnmsNodeList.class, xml);
assertEquals(xml, 1, list.size());
assertEquals(xml, Integer.valueOf(1), list.getTotalCount());
assertEquals(xml, "TestMachine0", list.get(0).getLabel());
}
use of org.opennms.netmgt.model.OnmsNodeList in project opennms by OpenNMS.
the class NodeRestService method getNodes.
/**
* <p>getNodes</p>
*
* @return a {@link org.opennms.netmgt.model.OnmsNodeList} object.
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
public OnmsNodeList getNodes(@Context final UriInfo uriInfo) {
final MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
final String type = params.getFirst("type");
final CriteriaBuilder builder = getCriteriaBuilder(params);
Criteria crit = null;
if (params.size() == 1 && params.getFirst("nodeId") != null && params.getFirst("nodeId").contains(",")) {
// we've been specifically asked for a list of nodes by ID
final List<Integer> nodeIds = new ArrayList<Integer>();
for (final String id : params.getFirst("nodeId").split(",")) {
nodeIds.add(Integer.valueOf(id));
}
crit = filterForNodeIds(builder, nodeIds).toCriteria();
} else if (params.getFirst("filterRule") != null) {
final Set<Integer> filteredNodeIds = m_filterDao.getNodeMap(params.getFirst("filterRule")).keySet();
if (filteredNodeIds.size() < 1) {
// The "in" criteria fails if the list of node ids is empty
final OnmsNodeList coll = new OnmsNodeList(Collections.emptyList());
coll.setTotalCount(0);
return coll;
}
// Apply the criteria without the filter rule
params.remove("filterRule");
final CriteriaBuilder filterRuleCriteriaBuilder = getCriteriaBuilder(params);
crit = filterForNodeIds(filterRuleCriteriaBuilder, filteredNodeIds).toCriteria();
} else {
applyQueryFilters(params, builder);
builder.orderBy("label").asc();
crit = builder.toCriteria();
if (type == null) {
final List<Restriction> restrictions = new ArrayList<Restriction>(crit.getRestrictions());
restrictions.add(Restrictions.ne("type", "D"));
crit.setRestrictions(restrictions);
}
}
final OnmsNodeList coll = new OnmsNodeList(m_nodeDao.findMatching(crit));
crit.setLimit(null);
crit.setOffset(null);
crit.setOrders(new ArrayList<Order>());
coll.setTotalCount(m_nodeDao.countMatching(crit));
return coll;
}
use of org.opennms.netmgt.model.OnmsNodeList in project opennms by OpenNMS.
the class NodeRestServiceIT method testLimits.
@Test
@JUnitTemporaryDatabase
public void testLimits() throws Exception {
JAXBContext context = JAXBContext.newInstance(OnmsNodeList.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
// Testing POST
for (m_nodeCounter = 0; m_nodeCounter < 20; m_nodeCounter++) {
createNode();
}
String url = "/nodes";
// Testing GET Collection
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("limit", "10");
parameters.put("orderBy", "id");
String xml = sendRequest(GET, url, parameters, 200);
assertTrue(xml, xml.contains("Darwin TestMachine 9.4.0 Darwin Kernel Version 9.4.0"));
Pattern p = Pattern.compile("<node [^>]*\\s*id=", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);
Matcher m = p.matcher(xml);
int count = 0;
while (m.find()) {
count++;
}
assertEquals("should get 10 nodes back", 10, count);
// Validate object by unmarshalling
OnmsNodeList list = (OnmsNodeList) unmarshaller.unmarshal(new StringReader(xml));
assertEquals(Integer.valueOf(10), list.getCount());
assertEquals(10, list.size());
assertEquals(Integer.valueOf(20), list.getTotalCount());
int i = 0;
Set<OnmsNode> sortedNodes = new TreeSet<OnmsNode>(new Comparator<OnmsNode>() {
@Override
public int compare(OnmsNode o1, OnmsNode o2) {
if (o1 == null && o2 == null) {
return 0;
} else if (o1 == null) {
return 1;
} else if (o2 == null) {
return -1;
} else {
if (o1.getId() == null) {
throw new IllegalStateException("Null ID on node: " + o1.toString());
}
return o1.getId().compareTo(o2.getId());
}
}
});
// Sort the nodes by ID
sortedNodes.addAll(list.getObjects());
for (OnmsNode node : sortedNodes) {
assertEquals(node.toString(), "TestMachine" + i++, node.getLabel());
}
}
use of org.opennms.netmgt.model.OnmsNodeList in project opennms by OpenNMS.
the class NodeRestServiceIT method testPutNode.
@Test
@JUnitTemporaryDatabase
public void testPutNode() throws Exception {
JAXBContext context = JAXBContext.newInstance(OnmsNodeList.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
// Testing POST
createNode();
String url = "/nodes";
// Testing GET Collection
String xml = sendRequest(GET, url, 200);
assertTrue(xml.contains("Darwin TestMachine 9.4.0 Darwin Kernel Version 9.4.0"));
OnmsNodeList list = (OnmsNodeList) unmarshaller.unmarshal(new StringReader(xml));
assertEquals(1, list.size());
assertEquals("TestMachine0", list.get(0).getLabel());
// Testing PUT
url += "/1";
sendPut(url, "sysContact=OpenNMS&assetRecord.manufacturer=Apple&assetRecord.operatingSystem=MacOSX Leopard", 204);
// Testing GET Single Object to make sure that the parameters changed
xml = sendRequest(GET, url, 200);
assertTrue(xml.contains("<sysContact>OpenNMS</sysContact>"));
assertTrue(xml.contains("<operatingSystem>MacOSX Leopard</operatingSystem>"));
// Testing DELETE
m_mockEventIpcManager.getEventAnticipator().reset();
m_mockEventIpcManager.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.DELETE_NODE_EVENT_UEI, "Test").setNodeid(1).getEvent());
sendRequest(DELETE, url, 204);
m_mockEventIpcManager.getEventAnticipator().waitForAnticipated(10000);
m_mockEventIpcManager.getEventAnticipator().verifyAnticipated();
}
use of org.opennms.netmgt.model.OnmsNodeList in project opennms by OpenNMS.
the class NodeRestServiceIT method testNode.
@Test
@JUnitTemporaryDatabase
public void testNode() throws Exception {
// Testing POST
createNode();
String url = "/nodes";
// Testing GET Collection
String xml = sendRequest(GET, url, 200);
assertTrue(xml.contains("Darwin TestMachine 9.4.0 Darwin Kernel Version 9.4.0"));
OnmsNodeList list = JaxbUtils.unmarshal(OnmsNodeList.class, xml);
assertEquals(1, list.size());
assertEquals(xml, "TestMachine0", list.get(0).getLabel());
// Testing orderBy
xml = sendRequest(GET, url, parseParamData("orderBy=sysObjectId"), 200);
list = JaxbUtils.unmarshal(OnmsNodeList.class, xml);
assertEquals(1, list.size());
assertEquals("TestMachine0", list.get(0).getLabel());
// Add 4 more nodes
for (m_nodeCounter = 1; m_nodeCounter < 5; m_nodeCounter++) {
createNode();
}
// Testing limit/offset
xml = sendRequest(GET, url, parseParamData("limit=3&offset=0&orderBy=label"), 200);
list = JaxbUtils.unmarshal(OnmsNodeList.class, xml);
assertEquals(3, list.size());
assertEquals(Integer.valueOf(3), list.getCount());
assertEquals(Integer.valueOf(5), list.getTotalCount());
assertEquals("TestMachine0", list.get(0).getLabel());
assertEquals("TestMachine1", list.get(1).getLabel());
assertEquals("TestMachine2", list.get(2).getLabel());
// This filter should match
xml = sendRequest(GET, url, parseParamData("comparator=like&label=%25Test%25"), 200);
LOG.info(xml);
list = JaxbUtils.unmarshal(OnmsNodeList.class, xml);
assertEquals(Integer.valueOf(5), list.getCount());
assertEquals(Integer.valueOf(5), list.getTotalCount());
// This filter should fail (return 0 results)
xml = sendRequest(GET, url, parseParamData("comparator=like&label=%25DOES_NOT_MATCH%25"), 200);
LOG.info(xml);
list = JaxbUtils.unmarshal(OnmsNodeList.class, xml);
assertEquals(null, list.getCount());
assertEquals(Integer.valueOf(0), list.getTotalCount());
// Testing PUT
url += "/1";
sendPut(url, "sysContact=OpenNMS&assetRecord.manufacturer=Apple&assetRecord.operatingSystem=MacOSX Leopard", 204);
// Testing GET Single Object
xml = sendRequest(GET, url, 200);
assertTrue(xml.contains("<sysContact>OpenNMS</sysContact>"));
assertTrue(xml.contains("<operatingSystem>MacOSX Leopard</operatingSystem>"));
// Testing DELETE
m_mockEventIpcManager.getEventAnticipator().reset();
m_mockEventIpcManager.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.DELETE_NODE_EVENT_UEI, "Test").setNodeid(1).getEvent());
sendRequest(DELETE, url, 204);
m_mockEventIpcManager.getEventAnticipator().waitForAnticipated(10000);
m_mockEventIpcManager.getEventAnticipator().verifyAnticipated();
}
Aggregations