Port QoSExplorer from Perl to Java

I want to port my QoSExplorer from Perl to Java. And this blog entry is an effort to document the lessons learned during the process. (NOTE: This is not a complete and clear article so I do apologize if it’s confusing. It’s posted mainly for my team members but I am in the process of add a Perl-Java series in this blog, so please check back.)

While porting QoS perl script to Java, I tried these following Java tips:

Access to Class Members: (Sun Tutorial), Reflection Java Array (Java2s page), HashMap (Example), enum: (Sun tutorial), Naming convention, Split Text, SNMP4J open source library ( JavaDoc ), Type Conversion CharSequence Class, Regex remove duplicate white space, snmpwalk code using SNMP4j, also found Steven R Brandt’s Regex tutorial .

Steps to Port Perl-based QosExplorer to Java

1.Create a Enum type to represent Cisco mapping from an integer to a object type

my %hash_QosType = (
'1' => "policymap",
'2' => "classmap",
'3' => "matchStatement",
'4' => "queueing",
'5' => "randomDetect",
'6' => "trafficShaping",
'7' => "police",
'8' => "set"
);

Helpful links: Eclipse Enum Wizard, Sun’s Enums page, and final keyword,

2. Create an empty project using Eclipse.

3. Create a ssc.snmp.qos.Node class
4. Create a ssc.snmp.qos.Interface class

5. Create a ssc.snmp.qos.policyMap class

6. Create a ssc.snmp.qos.ClassMap class

7. Create a ssc.snmp.qos.MIBParser class (main class to parse CiscoClassBasedQoSMIB.

Need to create Java equivalent for the following previously global variables:

my %hash_ifDescr;
my %hash_obPolicyMap;
my %hash_obClassMap;
my %hash_obInterface;
my %hash_cbQosPolicyIndex;
my %hash_cbQosPolicyMapName;
my %hash_cbQosCMName;
my %hash_cbQosParentObjectsIndex;
my %hash_cbQosConfigIndex;
my $hash_RED_CM_mapping;
my %hash_CM_PM_mapping;
my %hash_QUE_CM_mapping;

java.util.HashMap(doc, sample) is the choice.

For functions

sub print_hash {}
sub print_hash_interface{}
sub print_ClassMap {}
sub print_PolicyMap {}
sub print_hash_PolicyMap{}

I should be able to just use HashMap::toString function.

I will refer to this Java Almanac page to implement the following functions to obtain a portion of an OID and the value:


sub get_index{}
sub get_val{}

For functions which require SNMP utilities, I have two options: calling an external SNMP agent(Net-SNMP to be exact) or using SNMP4j library.


sub getREDTransmitBytes {}
sub getCurrentQueueDepth {}
sub getREDRandomDropBytes {}
sub parseMIB {}
sub SNMP_get {}
sub SNMP_request {}
sub SNMP_request_arr {}

Since this is a proof-of-concept (and short) task, I would call Net-SNMP utilities as external programs first, then in the future replace with SNMP4J ( official site, official doc, sample1, sameple2, CableModemSNMPAgent.java, CableModemSNMPAgentImpl.java ).

I first tried calling snmpget.exe from Java, but the program hanged at the line calling snmpget.exe just would not return. I decided to give SNMP4J a quick try. I downloaded snmp4j-1.8.2 from snmp4j site and then include snmp4j-1.8.2snmp4jdistlibSNMP4j.jar to my QoSExplorer project’s library. Quite happily, I created a snippet of code to use SNMP4j and it worked!!


package ssc.snmp.qos;

import java.io.IOException;

import org.snmp4j.*;
import org.snmp4j.event.*;
import org.snmp4j.mp.*;
import org.snmp4j.smi.*;
import org.snmp4j.transport.*;

public class QoSExplorer {

public static void main(String[] args) throws IOException {

TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);

// setting up target
CommunityTarget target = new CommunityTarget();

target.setCommunity(new OctetString(“cndl”));
Address targetAddress = GenericAddress.parse(“udp:192.168.15.1/161″);
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version1);
transport.listen();

// creating PDU
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,1})));
pdu.setType(PDU.GETNEXT);
ResponseEvent event = snmp.send(pdu,target, transport); // use synchronous event

System.out.println(“Received response PDU is: “+event.getResponse());

}

}