All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snmp4j.agent.mo.snmp.SnmpTargetMIB Maven / Gradle / Ivy

/*_############################################################################
  _## 
  _##  SNMP4J-Agent - SnmpTargetMIB.java  
  _## 
  _##  Copyright (C) 2005-2009  Frank Fock (SNMP4J.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.snmp4j.agent.mo.snmp;

//--AgentGen BEGIN=_BEGIN
//--AgentGen END

import java.util.*;

import org.snmp4j.*;
import org.snmp4j.agent.*;
import org.snmp4j.agent.mo.*;
import org.snmp4j.agent.request.*;
import org.snmp4j.event.*;
import org.snmp4j.log.*;
import org.snmp4j.mp.*;
import org.snmp4j.security.*;
import org.snmp4j.smi.*;

//--AgentGen BEGIN=_IMPORT
//--AgentGen END

public class SnmpTargetMIB implements MOGroup, CounterListener,
    MOTableRowListener {

  private static final LogAdapter logger =
      LogFactory.getLogger(SnmpTargetMIB.class);

  // Constants
  private static final OID oidSnmpTargetSpinLock =
      new OID(new int[] {1, 3, 6, 1, 6, 3, 12, 1, 1, 0});

  private MOScalar snmpTargetSpinLock;

  private static final OID oidSnmpUnknownContexts =
      new OID(new int[] {1, 3, 6, 1, 6, 3, 12, 1, 5, 0});

  private MOScalar snmpUnknownContexts;

  private static final OID oidSnmpUnavailableContexts =
      new OID(new int[] {1, 3, 6, 1, 6, 3, 12, 1, 4, 0});

  private MOScalar snmpUnavailableContexts;

  private static final OID oidSnmpTargetParamsEntry =
      new OID(new int[] {1, 3, 6, 1, 6, 3, 12, 1, 3, 1});

  // Column sub-identifer defintions for snmpTargetParamsEntry:
  private static final int colSnmpTargetParamsMPModel = 2;
  private static final int colSnmpTargetParamsSecurityModel = 3;
  private static final int colSnmpTargetParamsSecurityName = 4;
  private static final int colSnmpTargetParamsSecurityLevel = 5;
  private static final int colSnmpTargetParamsStorageType = 6;
  private static final int colSnmpTargetParamsRowStatus = 7;

  // Column index defintions for snmpTargetParamsEntry:
  static final int idxSnmpTargetParamsMPModel = 0;
  static final int idxSnmpTargetParamsSecurityModel = 1;
  static final int idxSnmpTargetParamsSecurityName = 2;
  static final int idxSnmpTargetParamsSecurityLevel = 3;
  static final int idxSnmpTargetParamsStorageType = 4;
  static final int idxSnmpTargetParamsRowStatus = 5;
  private static MOTableSubIndex[] snmpTargetParamsEntryIndexes =
      new MOTableSubIndex[] {
      new MOTableSubIndex(SMIConstants.SYNTAX_OCTET_STRING, 1, 32)
  };

  private static MOTableIndex snmpTargetParamsEntryIndex =
      new MOTableIndex(snmpTargetParamsEntryIndexes, true);

  private DefaultMOTable snmpTargetParamsEntry;
  private DefaultMOMutableTableModel snmpTargetParamsEntryModel;
  private static final OID oidSnmpTargetAddrEntry =
      new OID(new int[] {1, 3, 6, 1, 6, 3, 12, 1, 2, 1});

  // Column sub-identifer defintions for snmpTargetAddrEntry:
  private static final int colSnmpTargetAddrTDomain = 2;
  private static final int colSnmpTargetAddrTAddress = 3;
  private static final int colSnmpTargetAddrTimeout = 4;
  private static final int colSnmpTargetAddrRetryCount = 5;
  private static final int colSnmpTargetAddrTagList = 6;
  private static final int colSnmpTargetAddrParams = 7;
  private static final int colSnmpTargetAddrStorageType = 8;
  private static final int colSnmpTargetAddrRowStatus = 9;

  // Column index defintions for snmpTargetAddrEntry:
  static final int idxSnmpTargetAddrTDomain = 0;
  static final int idxSnmpTargetAddrTAddress = 1;
  static final int idxSnmpTargetAddrTimeout = 2;
  static final int idxSnmpTargetAddrRetryCount = 3;
  static final int idxSnmpTargetAddrTagList = 4;
  static final int idxSnmpTargetAddrParams = 5;
  static final int idxSnmpTargetAddrStorageType = 6;
  static final int idxSnmpTargetAddrRowStatus = 7;
  private static MOTableSubIndex[] snmpTargetAddrEntryIndexes =
      new MOTableSubIndex[] {
      new MOTableSubIndex(SMIConstants.SYNTAX_OCTET_STRING, 1, 32)
  };

  private static MOTableIndex snmpTargetAddrEntryIndex =
      new MOTableIndex(snmpTargetAddrEntryIndexes, true);

  private static final OID[] DEFAULT_TDOMAINS = {
     TransportDomains.snmpUDPDomain,
     TransportDomains.transportDomainTcpIpv4,
     TransportDomains.transportDomainTcpIpv6,
     TransportDomains.transportDomainUdpIpv4,
     TransportDomains.transportDomainUdpIpv6
  };

  private DefaultMOTable snmpTargetAddrEntry;
  private DefaultMOMutableTableModel snmpTargetAddrEntryModel;

  private Hashtable snmpTargetAddrTagIndex = new Hashtable();

  // maps TDomain OIDs to TDomainAddressFactory instances
  private Hashtable supportedAddressClasses = new Hashtable();
  private MessageDispatcher messageDispatcher;
  private CoexistenceInfoProvider coexistenceProvider;

  public SnmpTargetMIB(MessageDispatcher messageDispatcher) {
    this.messageDispatcher = messageDispatcher;
    snmpTargetSpinLock = new TestAndIncr(oidSnmpTargetSpinLock);
    snmpUnknownContexts =
        new MOScalar(oidSnmpUnknownContexts,
                     MOAccessImpl.ACCESS_READ_ONLY, new Counter32());
    snmpUnavailableContexts =
        new MOScalar(oidSnmpUnavailableContexts,
                     MOAccessImpl.ACCESS_READ_ONLY, new Counter32());
    createSnmpTargetParamsEntry();
    createSnmpTargetAddrEntry();
  }

  public Collection getTargetAddrRowsForTag(OctetString tag) {
    Collection l = (Collection)snmpTargetAddrTagIndex.get(tag);
    if (l == null) {
      return Collections.EMPTY_SET;
    }
    else {
      synchronized (l) {
        l = new ArrayList(l);
      }
    }
    return l;
  }

  /**
   * Returns the local SNMPv3 engine ID.
   * @return
   *    the SNMP3v local engine ID, if the {@link MPv3} is available or
   *    null otherwise.
   * @since 1.2
   */
  public byte[] getLocalEngineID() {
    MPv3 mpv3 = (MPv3)
        messageDispatcher.getMessageProcessingModel(MessageProcessingModel.MPv3);
    if (mpv3 == null) {
      return null;
    }
    return mpv3.getLocalEngineID();
  }

  public Address getTargetAddress(OctetString name) {
    OID index = name.toSubIndex(true);
    SnmpTargetAddrEntryRow trow =
        (SnmpTargetAddrEntryRow) this.snmpTargetAddrEntryModel.getRow(index);
    if (trow != null) {
      return trow.getAddress();
    }
    return null;
  }

  public Target getTarget(OctetString name,
                          OctetString contextEngineID,
                          OctetString contextName) {
    OID index = name.toSubIndex(true);
    SnmpTargetAddrEntryRow trow =
        (SnmpTargetAddrEntryRow) this.snmpTargetAddrEntryModel.getRow(index);
    if (trow != null) {
      return trow.getTarget(contextEngineID, contextName);
    }
    return null;
  }

  public void addDefaultTDomains() {
    TDomainAddressFactoryImpl factory = new TDomainAddressFactoryImpl();
    for (int i=0; iSnmpTargetParamsMPModelValidator implements the value
   * validation for SnmpTargetParamsMPModel.
   */
  class SnmpTargetParamsMPModelValidator implements
      MOValueValidationListener {

    public void validate(MOValueValidationEvent validationEvent) {
      Variable newValue = validationEvent.getNewValue();
      int v = ((Integer32) newValue).getValue();
      if (messageDispatcher.getMessageProcessingModel(v) == null) {
        validationEvent.setValidationStatus(SnmpConstants.
                                            SNMP_ERROR_WRONG_VALUE);
        return;
      }
      //--AgentGen BEGIN=snmpTargetParamsMPModel::validate
      //--AgentGen END
    }
  }

  /**
   * The SnmpTargetParamsSecurityModelValidator implements the value validation
   * for SnmpTargetParamsSecurityModel.
   */
  static class SnmpTargetParamsSecurityModelValidator implements
      MOValueValidationListener {

    public void validate(MOValueValidationEvent validationEvent) {
      //--AgentGen BEGIN=snmpTargetParamsSecurityModel::validate
      Variable newValue = validationEvent.getNewValue();
      switch (((Integer32)newValue).getValue()) {
        case SecurityModel.SECURITY_MODEL_USM: {
          if (SecurityModels.getInstance().
              getSecurityModel((Integer32) newValue) == null) {
            validationEvent.setValidationStatus(SnmpConstants.
                                                SNMP_ERROR_WRONG_VALUE);
            return;
          }
          break;
        }
        default:
          break;
      }
      //--AgentGen END
    }
  }

  /**
   * The SnmpTargetAddrTDomainValidator implements the value validation
   * for SnmpTargetAddrTDomain.
   */
  class SnmpTargetAddrTDomainValidator implements
      MOValueValidationListener {

    public void validate(MOValueValidationEvent validationEvent) {
      Variable newValue = validationEvent.getNewValue();
      //--AgentGen BEGIN=snmpTargetAddrTDomain::validate
      if (newValue instanceof OID) {
        OID tdomain = (OID)newValue;
        if (!supportedAddressClasses.containsKey(tdomain)) {
          validationEvent.setValidationStatus(SnmpConstants.SNMP_ERROR_BAD_VALUE);
        }
      }
      else {
        validationEvent.setValidationStatus(SnmpConstants.SNMP_ERROR_WRONG_TYPE);
      }
      //--AgentGen END
    }
  }

  /**
   * The SnmpTargetAddrTAddressValidator implements the value
   * validation for SnmpTargetAddrTAddress.
   */
  static class SnmpTargetAddrTAddressValidator implements
      MOValueValidationListener {

    public void validate(MOValueValidationEvent validationEvent) {
      Variable newValue = validationEvent.getNewValue();
      OctetString os = (OctetString) newValue;
      if (!(((os.length() >= 1) && (os.length() <= 255)))) {
        validationEvent.setValidationStatus(SnmpConstants.
                                            SNMP_ERROR_WRONG_LENGTH);
        return;
      }
      //--AgentGen BEGIN=snmpTargetAddrTAddress::validate
      //--AgentGen END
    }
  }

  /**
   * The SnmpTargetAddrTimeoutValidator implements the value
   * validation for SnmpTargetAddrTimeout.
   */
  static class SnmpTargetAddrTimeoutValidator implements
      MOValueValidationListener {

    public void validate(MOValueValidationEvent validationEvent) {
      Variable newValue = validationEvent.getNewValue();
      long v = ((Integer32) newValue).getValue();
      if (!(((v >= 0L) /*&& (v <= 2147483647L)*/))) {
        validationEvent.setValidationStatus(SnmpConstants.
                                            SNMP_ERROR_WRONG_VALUE);
        return;
      }
      //--AgentGen BEGIN=snmpTargetAddrTimeout::validate
      //--AgentGen END
    }
  }

  /**
   * The SnmpTargetAddrRetryCountValidator implements the value validation
   * for SnmpTargetAddrRetryCount.
   */
  static class SnmpTargetAddrRetryCountValidator implements
      MOValueValidationListener {

    public void validate(MOValueValidationEvent validationEvent) {
      Variable newValue = validationEvent.getNewValue();
      long v = ((Integer32) newValue).getValue();
      if (!(((v >= 0L) && (v <= 255L)))) {
        validationEvent.setValidationStatus(SnmpConstants.
                                            SNMP_ERROR_WRONG_VALUE);
        return;
      }
      //--AgentGen BEGIN=snmpTargetAddrRetryCount::validate
      //--AgentGen END
    }
  }

  /**
   * The SnmpTargetAddrParamsValidator implements the value validation
   * for SnmpTargetAddrParams.
   */
  class SnmpTargetAddrParamsValidator implements
      MOValueValidationListener {

    public void validate(MOValueValidationEvent validationEvent) {
      Variable newValue = validationEvent.getNewValue();
      OctetString os = (OctetString) newValue;
      if (!(((os.length() >= 1) && (os.length() <= 32)))) {
        validationEvent.setValidationStatus(SnmpConstants.
                                            SNMP_ERROR_WRONG_LENGTH);
        return;
      }
      //--AgentGen BEGIN=snmpTargetAddrParams::validate
      OID paramsIndexOID = os.toSubIndex(true);
      MOTableRow paramsRow =
          snmpTargetParamsEntryModel.getRow(paramsIndexOID);
      if (paramsRow == null) {
        validationEvent.
            setValidationStatus(SnmpConstants.SNMP_ERROR_INCONSISTENT_VALUE);
      }
      //--AgentGen END
    }
  }

  public void incrementCounter(CounterEvent event) {
    if (event.getOid().equals(snmpUnavailableContexts.getOid())) {
      ((Counter32) snmpUnavailableContexts.getValue()).increment();
      event.setCurrentValue(snmpUnavailableContexts.getValue());
    }
    else if (event.getOid().equals(snmpUnknownContexts.getOid())) {
      ((Counter32) snmpUnknownContexts.getValue()).increment();
      event.setCurrentValue(snmpUnknownContexts.getValue());
    }
  }

 // Enumerations

  public static final class SnmpTargetParamsSecurityLevelEnum {
    public static final int noAuthNoPriv = 1;
    public static final int authNoPriv = 2;
    public static final int authPriv = 3;
  }

//--AgentGen BEGIN=_CLASSES

  public void rowChanged(MOTableRowEvent event) {
    if ((event.getType() == MOTableRowEvent.DELETE) &&
        (event.getRow() instanceof SnmpTargetAddrEntryRow)) {
      Variable[] vbs = new Variable[event.getRow().size()];
      vbs[idxSnmpTargetAddrTagList] = new OctetString();
      MOTableRow dummyRow =
          new DefaultMOTableRow(event.getRow().getIndex(), vbs);
      ((SnmpTargetAddrEntryRow)event.getRow()).updateUserObject(dummyRow);
    }
  }

  /**
   * Returns the SNMP Target Parameters row for the specified name.
   * @param paramsName
   *    the name of the parameters set to return.
   * @return
   *    if the row containing the target parameters if such an entry
   *    exists or null if no such entry exists
   */
  public MOTableRow getTargetParamsRow(OctetString paramsName) {
    if (paramsName == null) {
      return null;
    }
    OID paramsIndex = paramsName.toSubIndex(true);
    return snmpTargetParamsEntryModel.getRow(paramsIndex);
  }

  /**
   * Returns the SNMP Target Parameters row for the specified name.
   * @param paramsName
   *    the name of the parameters set to return.
   * @param activeOnly
   *    if true only an active row will be returned.
   * @return
   *    if the row containing the target parameters if such an entry
   *    exists or null if no such entry exists
   */
  public MOTableRow getTargetParamsRow(OctetString paramsName,
                                       boolean activeOnly) {
    MOTableRow row = getTargetParamsRow(paramsName);
    if (activeOnly && (row != null)) {
      if (((Integer32)row.getValue(idxSnmpTargetParamsRowStatus)).getValue()
          != RowStatus.active) {
        return null;
      }
    }
    return row;
  }

  public DefaultMOTable getSnmpTargetAddrEntry() {
    return snmpTargetAddrEntry;
  }

  public CoexistenceInfoProvider getCoexistenceProvider() {
    return coexistenceProvider;
  }

  public void setCoexistenceProvider(CoexistenceInfoProvider
                                     coexistenceProvider) {
    this.coexistenceProvider = coexistenceProvider;
  }
  //--AgentGen END

//--AgentGen BEGIN=_END
//--AgentGen END
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy