Package org.chiefly.sunlamp

Source Code of org.chiefly.sunlamp.StandardSNMPv2

/**
* 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 org.chiefly.sunlamp.util.VariableBindings;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
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.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;

import com.google.common.base.Preconditions;

/**
* {@link SNMPv2} implementation. Example usage:
*
* <pre>
* final OctetString community = new OctetString(&quot;c0mmun1ty&quot;);
* final TransportMapping&lt;UdpAddress&gt; transport = new DefaultUdpTransportMapping();
* final SnmpClientV2&lt;UdpAddress&gt; v2Client = new StandardSnmpClientV2&lt;UdpAddress&gt;(community, transport);
* final UdpAddress address = new UdpAddress(&quot;192.168.10.10/161&quot;);
* final OID oid = new OID(&quot;1.2.3.4.5.6&quot;);
* final ResponseEvent response = v2Client.getSync(address, oid);
* </pre>
* */
public class StandardSNMPv2<A extends Address> extends AbstractSNMP<A> implements SNMPv2<A> {

  /** The SNMP community string. */
  private final OctetString community;

  /**
   * @param communityString The SNMPv2c community string.
   * @param transportMapping The {@link TransportMapping} to use.
   * @throws IOException if a {@link TransportMapping} throws an {@link IOException}.
   */
  public StandardSNMPv2(final OctetString communityString, final TransportMapping<A> transportMapping)
      throws IOException {

    this(communityString, transportMapping, DEFAULT_RETRIES, DEFAULT_TIMEOUT);
  }

  /**
   * @param communityString The SNMPv2 community string.
   * @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}.
   */
  public StandardSNMPv2(final OctetString communityString, final TransportMapping<A> transportMapping,
      final int retries, final long timeout) throws IOException {

    this(communityString, transportMapping, retries, timeout, null);
  }

  /**
   * @param communityString The SNMPv2 community string.
   * @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}.
   */
  public StandardSNMPv2(final OctetString communityString, final TransportMapping<A> transportMapping,
      final int retries, final long timeout, final ReportHandler reportHandler) throws IOException {

    super(transportMapping, retries, timeout, reportHandler);

    this.community = Preconditions.checkNotNull(communityString);
  }

  /**
   * @see org.chiefly.sunlamp.SNMPv2#getBulkAsync(org.snmp4j.smi.Address, int, int, org.snmp4j.event.ResponseListener,
   *      java.lang.Object, org.snmp4j.smi.OID[])
   */
  @Override
  public void getBulkAsync(final A address, final int maxRepetitions, final int nonRepeaters,
      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.GETBULK, target, VariableBindings.wrapOids(oids));
    requestPdu.setMaxRepetitions(maxRepetitions);
    requestPdu.setNonRepeaters(nonRepeaters);
    this.getSnmp().send(requestPdu, target, handle, listener);
  }

  /**
   * @see org.chiefly.sunlamp.SNMPv2#getBulkSync(org.snmp4j.smi.Address, int, int, org.snmp4j.smi.OID[])
   */
  @Override
  public ResponseEvent getBulkSync(final A address, final int maxRepetitions, final int nonRepeaters,
      final OID... oids) throws IOException {

    checkNoNulls(address, oids);

    final Target target = this.createTarget(address);
    final PDU requestPdu = this.createRequestPdu(PDU.GETBULK, target, VariableBindings.wrapOids(oids));
    requestPdu.setMaxRepetitions(maxRepetitions);
    requestPdu.setNonRepeaters(nonRepeaters);
    return this.getSnmp().send(requestPdu, target);
  }

  /**
   * @see org.chiefly.sunlamp.SNMPv2#informAsync(org.snmp4j.smi.Address, org.snmp4j.event.ResponseListener,
   *      java.lang.Object, org.snmp4j.smi.VariableBinding[])
   */
  @Override
  public void informAsync(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.INFORM, target, bindings);
    this.getSnmp().send(requestPdu, target, handle, listener);
  }

  /**
   * @see org.chiefly.sunlamp.SNMPv2#informSync(org.snmp4j.smi.Address, org.snmp4j.smi.VariableBinding[])
   */
  @Override
  public ResponseEvent informSync(final A address, final VariableBinding... bindings) throws IOException {

    checkNoNulls(address, bindings);

    final Target target = this.createTarget(address);
    final PDU requestPdu = this.createRequestPdu(PDU.INFORM, target, bindings);
    return this.getSnmp().send(requestPdu, target);
  }

  /**
   * @see org.chiefly.sunlamp.SNMPv2#notify(org.snmp4j.smi.Address, org.snmp4j.smi.VariableBinding[])
   */
  @Override
  public void notify(final A address, final VariableBinding... bindings) throws IOException {

    checkNoNulls(address, bindings);

    final Target target = this.createTarget(address);
    final PDU requestPdu = this.createRequestPdu(PDU.NOTIFICATION, target, bindings);
    this.getSnmp().send(requestPdu, target);
  }

  /** @see org.chiefly.sunlamp.AbstractSNMP#createVersionSpecificTarget() */
  @Override
  protected Target createVersionSpecificTarget() {
    final CommunityTarget target = new CommunityTarget();
    target.setVersion(SnmpConstants.version2c);
    target.setCommunity(this.community);
    return target;
  }
}
TOP

Related Classes of org.chiefly.sunlamp.StandardSNMPv2

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.