/**
* Copyright (C) 2011 Matt Doyle (chief@chiefly.org)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.chiefly.sunlamp;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import org.chiefly.sunlamp.util.VariableBindings;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Snmp.ReportHandler;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.PDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableListener;
import org.snmp4j.util.TableUtils;
import org.snmp4j.util.TreeEvent;
import org.snmp4j.util.TreeListener;
import org.snmp4j.util.TreeUtils;
import com.google.common.base.Preconditions;
/** Base class which all SNMP clients inherit from. */
public abstract class AbstractSNMP<A extends Address> implements SNMP<A> {
/** Default number of times to retry an unsuccessful query. */
protected static final int DEFAULT_RETRIES = 5;
/** Default timeout for an unanswered query (in milliseconds). */
protected static final long DEFAULT_TIMEOUT = 1000;
/** {@link Random} instance to use. */
private final Random random = new Random();
/** The number of times to retry a request. */
private final int retries;
/** Internal {@link Snmp} instance to use. */
private final Snmp snmp;
/** The timeout for a given request (in milliseconds). */
private final long timeout;
/** {@link TransportMapping} specifying the transport protocol to use. */
private final TransportMapping<A> transportMapping;
/**
* @param transportMapping The {@link TransportMapping} to use.
* @throws IOException if a {@link TransportMapping} throws an {@link IOException}.
*/
protected AbstractSNMP(final TransportMapping<A> transportMapping) throws IOException {
this(transportMapping, AbstractSNMP.DEFAULT_RETRIES, AbstractSNMP.DEFAULT_TIMEOUT);
}
/**
* @param transportMapping The {@link TransportMapping} to use.
* @param retries The number of times to retry a request.
* @param timeout The timeout for a given request (in milliseconds).
* @throws IOException if a {@link TransportMapping} throws an {@link IOException}.
*/
protected AbstractSNMP(final TransportMapping<A> transportMapping, final int retries, final long timeout)
throws IOException {
this(transportMapping, retries, timeout, null);
}
/**
* @param transportMapping The {@link TransportMapping} to use.
* @param retries The number of times to retry a request.
* @param timeout The timeout for a given request (in milliseconds).
* @param reportHandler The (optional) {@link ReportHandler} to use.
* @throws IOException if a {@link TransportMapping} throws an {@link IOException}.
*/
protected AbstractSNMP(final TransportMapping<A> transportMapping, final int retries, final long timeout,
final ReportHandler reportHandler) throws IOException {
Preconditions.checkState(retries >= 0, "Retries cannot be less than zero");
Preconditions.checkState(timeout >= 0, "Timeout cannot be less than zero");
this.transportMapping = Preconditions.checkNotNull(transportMapping);
this.snmp = new Snmp(this.transportMapping);
this.retries = retries;
this.timeout = timeout;
if (reportHandler != null) {
this.snmp.setReportHandler(reportHandler);
}
this.snmp.listen();
}
/**
* @param objects The {@link Object}s to test for {@code null}.
*/
protected static void checkNoNulls(final Object... objects) {
for (final Object object : objects) {
Preconditions.checkNotNull(object);
}
}
/**
* Closes down the client and frees any held resources.
*
* @throws IOException if the client cannot be closed.
*/
@Override
public void close() throws IOException {
if (this.snmp != null) {
this.snmp.close();
}
}
/**
* @see org.chiefly.sunlamp.SNMP#createRowSync(org.snmp4j.smi.Address, org.snmp4j.smi.OID, org.snmp4j.smi.OID,
* org.snmp4j.smi.VariableBinding[])
*/
@Override
public ResponseEvent createRowSync(final A address, final OID rowStatusColumnOid, final OID rowIndex,
final VariableBinding... bindings) {
checkNoNulls(address, rowStatusColumnOid, rowIndex, bindings);
final Target target = this.createTarget(address);
final TableUtils tableUtils = new TableUtils(this.snmp, new DefaultPDUFactory());
return tableUtils.createRow(target, rowStatusColumnOid, rowIndex, bindings);
}
/**
* @see org.chiefly.sunlamp.SNMP#destroyRowSync(org.snmp4j.smi.Address, org.snmp4j.smi.OID, org.snmp4j.smi.OID)
*/
@Override
public ResponseEvent destroyRowSync(final A address, final OID rowStatusColumnOid, final OID rowIndex) {
checkNoNulls(address, rowStatusColumnOid, rowIndex);
final Target target = this.createTarget(address);
final TableUtils tableUtils = new TableUtils(this.snmp, new DefaultPDUFactory());
return tableUtils.destroyRow(target, rowStatusColumnOid, rowIndex);
}
/**
* @see org.chiefly.sunlamp.SNMP#getAsync(org.snmp4j.smi.Address, org.snmp4j.event.ResponseListener,
* java.lang.Object, org.snmp4j.smi.OID[])
*/
@Override
public void getAsync(final A address, final ResponseListener listener, final Object handle, final OID... oids)
throws IOException {
checkNoNulls(address, listener, oids);
final Target target = this.createTarget(address);
final PDU requestPdu = this.createRequestPdu(PDU.GET, target, VariableBindings.wrapOids(oids));
this.snmp.send(requestPdu, target, handle, listener);
}
/**
* @see org.chiefly.sunlamp.SNMP#getDenseTableAsync(org.snmp4j.smi.Address, org.snmp4j.util.TableListener,
* java.lang.Object, org.snmp4j.smi.OID, org.snmp4j.smi.OID, org.snmp4j.smi.OID[])
*/
@Override
public void getDenseTableAsync(final A address, final TableListener listener, final Object handle,
final OID lowerBoundIndex, final OID upperBoundIndex, final OID... columnOids) {
checkNoNulls(address, listener, handle, lowerBoundIndex, upperBoundIndex, columnOids);
final Target target = this.createTarget(address);
final TableUtils tableUtils = new TableUtils(this.snmp, new DefaultPDUFactory());
tableUtils.getDenseTable(target, columnOids, listener, handle, lowerBoundIndex, upperBoundIndex);
}
/**
* @see org.chiefly.sunlamp.SNMP#getNextAsync(org.snmp4j.smi.Address, org.snmp4j.event.ResponseListener,
* java.lang.Object, org.snmp4j.smi.OID[])
*/
@Override
public void getNextAsync(final A address, final ResponseListener listener, final Object handle, final OID... oids)
throws IOException {
checkNoNulls(address, listener, oids);
final Target target = this.createTarget(address);
final PDU requestPdu = this.createRequestPdu(PDU.GETNEXT, target, VariableBindings.wrapOids(oids));
this.snmp.send(requestPdu, target, handle, listener);
}
/**
* @see org.chiefly.sunlamp.SNMP#getNextSync(org.snmp4j.smi.Address, org.snmp4j.smi.OID[])
*/
@Override
public ResponseEvent getNextSync(final A address, final OID... oids) throws IOException {
checkNoNulls(address, oids);
final Target target = this.createTarget(address);
final PDU requestPdu = this.createRequestPdu(PDU.GETNEXT, target, VariableBindings.wrapOids(oids));
return this.snmp.send(requestPdu, target);
}
/**
* @see org.chiefly.sunlamp.SNMP#getSync(org.snmp4j.smi.Address, org.snmp4j.smi.OID[])
*/
@Override
public ResponseEvent getSync(final A address, final OID... oids) throws IOException {
checkNoNulls(address, oids);
final Target target = this.createTarget(address);
final PDU requestPdu = this.createRequestPdu(PDU.GET, target, VariableBindings.wrapOids(oids));
return this.snmp.send(requestPdu, target);
}
/**
* @see org.chiefly.sunlamp.SNMP#getTableAsync(org.snmp4j.smi.Address, org.snmp4j.util.TableListener,
* java.lang.Object, org.snmp4j.smi.OID, org.snmp4j.smi.OID, org.snmp4j.smi.OID[])
*/
@Override
public void getTableAsync(final A address, final TableListener listener, final Object handle,
final OID lowerBoundIndex, final OID upperBoundIndex, final OID... columnOids) {
checkNoNulls(address, listener, handle, lowerBoundIndex, upperBoundIndex, columnOids);
final Target target = this.createTarget(address);
final TableUtils tableUtils = new TableUtils(this.snmp, new DefaultPDUFactory());
tableUtils.getTable(target, columnOids, listener, handle, lowerBoundIndex, upperBoundIndex);
}
/**
* @see org.chiefly.sunlamp.SNMP#getTableSync(org.snmp4j.smi.Address, org.snmp4j.smi.OID, org.snmp4j.smi.OID,
* org.snmp4j.smi.OID[])
*/
@Override
public List<TableEvent> getTableSync(final A address, final OID lowerBoundIndex, final OID upperBoundIndex,
final OID... columnOids) {
checkNoNulls(address, lowerBoundIndex, upperBoundIndex, columnOids);
final Target target = this.createTarget(address);
final TableUtils tableUtils = new TableUtils(this.snmp, new DefaultPDUFactory());
return tableUtils.getTable(target, columnOids, lowerBoundIndex, upperBoundIndex);
}
/**
* @see org.chiefly.sunlamp.SNMP#setAsync(org.snmp4j.smi.Address, org.snmp4j.event.ResponseListener,
* java.lang.Object, org.snmp4j.smi.VariableBinding[])
*/
@Override
public void setAsync(final A address, final ResponseListener listener, final Object handle,
final VariableBinding... bindings) throws IOException {
checkNoNulls(address, listener, bindings);
final Target target = this.createTarget(address);
final PDU requestPdu = this.createRequestPdu(PDU.SET, target, bindings);
this.snmp.send(requestPdu, target, handle, listener);
}
/**
* @see org.chiefly.sunlamp.SNMP#setSync(org.snmp4j.smi.Address, org.snmp4j.smi.VariableBinding[])
*/
@Override
public ResponseEvent setSync(final A address, final VariableBinding... bindings) throws IOException {
checkNoNulls(address, bindings);
final Target target = this.createTarget(address);
final PDU requestPdu = this.createRequestPdu(PDU.SET, target, bindings);
return this.snmp.send(requestPdu, target);
}
/**
* @see org.chiefly.sunlamp.SNMP#walkAsync(org.snmp4j.smi.Address, org.snmp4j.util.TreeListener, java.lang.Object,
* org.snmp4j.smi.OID)
*/
@Override
public void walkAsync(final A address, final TreeListener listener, final Object handle, final OID oid) {
final Target target = this.createTarget(address);
final TreeUtils treeUtils = new TreeUtils(this.snmp, new DefaultPDUFactory());
treeUtils.setIgnoreLexicographicOrder(true);
treeUtils.getSubtree(target, oid, handle, listener);
}
/**
* @see org.chiefly.sunlamp.SNMP#walkSync(org.snmp4j.smi.Address, org.snmp4j.smi.OID)
*/
@Override
public List<TreeEvent> walkSync(final A address, final OID oid) {
final Target target = this.createTarget(address);
final TreeUtils treeUtils = new TreeUtils(this.snmp, new DefaultPDUFactory());
treeUtils.setIgnoreLexicographicOrder(true);
return treeUtils.getSubtree(target, oid);
}
/**
* @param type The type of request {@link PDU} to construct.
* @param target The {@link Target} of the request.
* @param bindings The {@link VariableBinding}s to send in the request.
* @return A fully constructed request {@link PDU}.
*/
protected PDU createRequestPdu(final int type, final Target target, final VariableBinding... bindings) {
/* Build the request PDU */
final PDUFactory pduFactory = new DefaultPDUFactory(type);
final PDU requestPdu = pduFactory.createPDU(target);
/* Load up the OIDs or VariableBindings */
if ((type == PDU.GET) || (type == PDU.GETBULK) || (type == PDU.GETNEXT)) {
requestPdu.addAllOIDs(bindings);
} else {
requestPdu.addAll(bindings);
}
requestPdu.setRequestID(new Integer32(Math.abs(this.random.nextInt())));
return requestPdu;
}
/**
* @param address The {@link Address} to query.
* @return Returns a fully-initialized {@link Target} instance.
*/
protected Target createTarget(final A address) {
final Target target = this.createVersionSpecificTarget();
target.setAddress(address);
target.setRetries(this.retries);
target.setTimeout(this.timeout);
return target;
}
/**
* Creates a version-specific SNMP {@link Target}.
*
* @return Returns an initialized {@link Target}.
*/
protected abstract Target createVersionSpecificTarget();
/** @return Returns the {@link Snmp} instance in use. */
protected Snmp getSnmp() {
return this.snmp;
}
}