Package org.chiefly.sunlamp

Source Code of org.chiefly.sunlamp.StandardSNMPv1

/**
* 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.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.PDUv1;
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.Counter64;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;

import com.google.common.base.Preconditions;

/**
* {@link SNMPv1} implementation. Example usage:
*
* <pre>
* final OctetString community = new OctetString(&quot;c0mmun1ty&quot;);
* final TransportMapping&lt;UdpAddress&gt; transport = new DefaultUdpTransportMapping();
* final SnmpClientV1&lt;UdpAddress&gt; v1Client = new StandardSnmpClientV1&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 = v1Client.getSync(address, oid);
* </pre>
* */
public class StandardSNMPv1<A extends Address> extends AbstractSNMP<A> implements SNMPv1<A> {

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

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

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

  /**
   * @param communityString The SNMPv1 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 StandardSNMPv1(final OctetString communityString, final TransportMapping<A> transportMapping,
      final int retries, final long timeout) throws IOException {

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

  /**
   * @param communityString The SNMPv1 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 StandardSNMPv1(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);
  }

  /**
   * Makes sure that the {@link VariableBinding}(s) are SNMPv1-compatible.
   *
   * @param bindings The {@link VariableBinding}(s) to test.
   * @throws IllegalArgumentException if any binding contains a {@link Counter64}.
   */
  private static void checkV1Bindings(final VariableBinding... bindings) {
    if (bindings != null) {
      for (final VariableBinding binding : bindings) {
        if (binding.getVariable() instanceof Counter64) {
          throw new IllegalArgumentException("Counter64 encountered in SNMPv1 PDU (See RFC 2576 4.1.2.1)");
        }
      }
    }
  }

  /**
   * @see org.chiefly.sunlamp.AbstractSNMP#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) {

    checkV1Bindings(bindings);

    return super.createRowSync(address, rowStatusColumnOid, rowIndex, bindings);
  }

  /**
   * @see org.chiefly.sunlamp.AbstractSNMP#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 {

    checkV1Bindings(bindings);

    super.setAsync(address, listener, handle, bindings);
  }

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

    checkV1Bindings(bindings);

    return super.setSync(address, bindings);
  }

  /**
   * @see org.chiefly.sunlamp.SNMPv1#trap(org.snmp4j.smi.Address, org.snmp4j.smi.OID, int, int,
   *      org.snmp4j.smi.VariableBinding[])
   */
  @Override
  public void trap(final A address, final OID enterprise, final int genericTrap, final int specificTrap,
      final VariableBinding... bindings) throws IOException {

    checkNoNulls(address, enterprise, bindings);
    checkV1Bindings(bindings);

    final Target target = this.createTarget(address);
    final PDUv1 requestPdu = (PDUv1) this.createRequestPdu(PDU.V1TRAP, target, bindings);
    requestPdu.setEnterprise(enterprise);
    requestPdu.setGenericTrap(genericTrap);
    requestPdu.setSpecificTrap(specificTrap);
    this.getSnmp().send(requestPdu, target);
  }

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

Related Classes of org.chiefly.sunlamp.StandardSNMPv1

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.