简体   繁体   中英

SNMP4J How Mock a GETBULK Request?

I need to mock a GETBULK request with snmp4j in java. But I don't understand how to do it. More specific i don't understand how create TreeEvent for create response List and how answer to client.

I've a snmpwalk like this

public void sendWalk(String community, String ipAddress, int port, String oidValue, int retries, long timeoutMillis) throws IOException {

    CommunityTarget comtarget = new CommunityTarget();
    comtarget.setCommunity(new OctetString(community));
    comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
    comtarget.setRetries(retries);
    comtarget.setTimeout(timeoutMillis);
    comtarget.setVersion(snmpVersion);


    OID oid = new OID(oidValue);

    TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory());
    List<TreeEvent> events = treeUtils.walk(comtarget, new OID[]{oid});
    if(events == null || events.size() == 0) {
        // TODO inserire in allarm manager ???
        log.warn(" No events . Request[Oid:"+oidValue+"]");
    }else{
        parseWalkResult(events);
    }

}

A snmpwalk on SNMP4J is a GETBULK request, then i want to implement a mock to response at treeUtils.walk method and have List of TreeEvents as answer from mockup.

Thank you in advance.

You can use SNMP4j to do SNMPBULKWALK on the router. SNMP4j provides below API to retrieve entire subtree of a given rootnode synchronously.

Class: TreeUtils.java Method: getSubtree(targetV2, rootOID);

It also provides another method which is asynchronous.

Class: TreeUtils.java Method: walk(targetV2, rootOIDs);

I have a github project which has the complete working code. You can clone the github repo and try it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM