org.opendaylight.cardinal.impl.SimpleSnmpClient Maven / Gradle / Ivy
The newest version!
/*
* Copyright © 2016 TCS and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.cardinal.impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.agent.mo.MOAccessImpl;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.SMIConstants;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableUtils;
/**
*
* @author Subodh Roy
*
*/
public class SimpleSnmpClient {
private String address;
private Snmp snmp;
public SimpleSnmpClient(String address) {
super();
this.address = address;
try {
start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// Since snmp4j relies on asynch req/resp we need a listener
// for responses which should be closed
public void stop() throws IOException {
snmp.close();
}
private void start() throws IOException {
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
// Do not forget this line!
transport.listen();
}
public String getAsString(OID oid) throws IOException {
ResponseEvent event = get(new OID[] { oid });
return event.getResponse().get(0).getVariable().toString();
}
public void getAsString(OID oids, ResponseListener listener) {
try {
snmp.send(getPDU(new OID[] { oids }), getTarget(), null, listener);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private PDU getPDU(OID oids[]) {
PDU pdu = new PDU();
for (OID oid : oids) {
pdu.add(new VariableBinding(oid));
}
pdu.setType(PDU.GET);
return pdu;
}
public ResponseEvent get(OID oids[]) throws IOException {
ResponseEvent event = snmp.send(getPDU(oids), getTarget(), null);
if (event != null) {
return event;
}
throw new RuntimeException("GET timed out");
}
private Target getTarget() {
Address targetAddress = GenericAddress.parse(address);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version2c);
return target;
}
/**
* Normally this would return domain objects or something else than this...
*/
public List> getTableAsStrings(OID[] oids) {
TableUtils tUtils = new TableUtils(snmp, new DefaultPDUFactory());
@SuppressWarnings("unchecked")
List events = tUtils.getTable(getTarget(), oids, null, null);
List> list = new ArrayList>();
for (TableEvent event : events) {
if (event.isError()) {
throw new RuntimeException(event.getErrorMessage());
}
List strList = new ArrayList();
list.add(strList);
for (VariableBinding vb : event.getColumns()) {
strList.add(vb.getVariable().toString());
}
}
return list;
}
public static String extractSingleString(ResponseEvent event) {
return event.getResponse().get(0).getVariable().toString();
}
}