Search in sources :

Example 1 with OnmsNodeList

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());
}
Also used : OnmsNodeList(org.opennms.netmgt.model.OnmsNodeList) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 2 with OnmsNodeList

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;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) Order(org.opennms.core.criteria.Order) Set(java.util.Set) OnmsNodeList(org.opennms.netmgt.model.OnmsNodeList) ArrayList(java.util.ArrayList) Criteria(org.opennms.core.criteria.Criteria) Restriction(org.opennms.core.criteria.restrictions.Restriction) List(java.util.List) OnmsNodeList(org.opennms.netmgt.model.OnmsNodeList) ArrayList(java.util.ArrayList) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with OnmsNodeList

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());
    }
}
Also used : Pattern(java.util.regex.Pattern) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsNodeList(org.opennms.netmgt.model.OnmsNodeList) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) JAXBContext(javax.xml.bind.JAXBContext) TreeSet(java.util.TreeSet) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 4 with OnmsNodeList

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();
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) OnmsNodeList(org.opennms.netmgt.model.OnmsNodeList) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 5 with OnmsNodeList

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();
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) OnmsNodeList(org.opennms.netmgt.model.OnmsNodeList) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Aggregations

OnmsNodeList (org.opennms.netmgt.model.OnmsNodeList)5 Test (org.junit.Test)4 JUnitTemporaryDatabase (org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)4 StringReader (java.io.StringReader)2 JAXBContext (javax.xml.bind.JAXBContext)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 Criteria (org.opennms.core.criteria.Criteria)1 CriteriaBuilder (org.opennms.core.criteria.CriteriaBuilder)1 Order (org.opennms.core.criteria.Order)1 Restriction (org.opennms.core.criteria.restrictions.Restriction)1