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

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

/*_############################################################################
  _##
  _##  SNMP4J-Agent - RowStatus.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;

import java.util.*;

import org.snmp4j.*;
import org.snmp4j.agent.*;
import org.snmp4j.agent.mo.*;
import org.snmp4j.agent.request.*;
import org.snmp4j.smi.*;
import org.snmp4j.log.LogAdapter;
import org.snmp4j.log.LogFactory;

/**
 * The RowStatus class implements the columnar object TC RowStatus.
 * The RowStatus textual convention is used to manage the creation and deletion
 * of conceptual rows, and is used as the value of the SYNTAX clause for the
 * status column of a conceptual row. See RFC 2579.
 *
 * The RowStatus column controls row creation and deletion in SNMP tables with
 * READ-CREATE maximum access. Since the state of a dynamic row is/may be
 * important to dependent rows / other objects of an agent, row status change
 * events can be propagated to other objects through registering
 * {@link RowStatusListener}s.
 *
 * @author Frank Fock
 * @version 1.0
 */
public class RowStatus extends MOMutableColumn implements MOChangeListener,
    MOTableRowListener
{

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

  public static final int notExistant = 0;
  public static final int active = 1;
  public static final int notInService = 2;
  public static final int notReady = 3;
  public static final int createAndGo = 4;
  public static final int createAndWait = 5;
  public static final int destroy = 6;

  private OID oid;
  private int columnIndex;

  private transient Vector rowStatusListeners;

  /**
   * Creates a RowStatus column with the specified column sub-identifier and
   * maximum access of 'read-create'.
   * @param columnID
   *    a column sub-identifier.
   */
  public RowStatus(int columnID) {
    super(columnID, SMIConstants.SYNTAX_INTEGER,
          MOAccessImpl.ACCESS_READ_CREATE);
    this.addMOValueValidationListener(new RowStatusValidator());
  }

  /**
   * Creates a RowStatus column with the specified column sub-identifier.
   * @param columnID
   *    a column sub-identifier.
   * @param access
   *    the maximum access for the RowStatus column (should be READ-CREATE).
   */
  public RowStatus(int columnID, MOAccess access) {
    super(columnID, SMIConstants.SYNTAX_INTEGER, access);
    this.addMOValueValidationListener(new RowStatusValidator());
  }

  /**
   * Sets the table instance this columnar object is contained in. This method
   * should be called by {@link MOTable} instance to register the table with
   * the RowStatus column. When called, this RowStatus registers itself as
   * {@link MOChangeListener} and {@link MOTableRowListener}.
   * @param table
   *    the MOTable instance where this column is contained in.
   */
  public void setTable(MOTable table) {
    super.setTable(table);
    oid = new OID(table.getOID());
    oid.append(getColumnID());
    columnIndex = table.getColumnIndex(getColumnID());

    table.addMOChangeListener(this);
    table.addMOTableRowListener(this);
  }

  /**
   * Unsets the table instance and thus unregisters itself as
   * {@link MOChangeListener} and {@link MOTableRowListener}.
   * @param table
   *    the MOTable instance where this column was part of.
   */
  public void unsetTable(MOTable table) {
    columnIndex = 0;
    table.removeMOChangeListener(this);
    table.removeMOTableRowListener(this);
  }

  protected boolean isReady(MOTableRow row, int rowStatusColumn) {
    return isReady(row, rowStatusColumn, null);
  }

  protected boolean isReady(MOTableRow row, int rowStatusColumn,
                            MOTableRow changeSet) {
    MOColumn[] columns = getTable().getColumns();
    for (int i=0; irow.
   * @return
   *    true if row is active.
   */
  public static boolean isRowActive(MOTableRow row, int rowStatusColumnIndex) {
    Integer32 rowStatus = (Integer32) row.getValue(rowStatusColumnIndex);
    if (rowStatus != null) {
      return rowStatus.getValue() == RowStatus.active;
    }
    return false;
  }

  /**
   * The ActiveRowsFilter is a {@link MOTableRowFilter} that
   * returns only the active rows of a table with a RowStatus column.
   *
   * @author Frank Fock
   * @version 1.0
   */
  public static class ActiveRowsFilter implements MOTableRowFilter {

    private int rowStatusColumnIndex;

    /**
     * Creates an active row filter by specifying the RowStatus column's index
     * in the target table.
     * @param rowStatusColumnIndex
     *    the column index (zero-based) of the RowStatus column on behalf the
     *    filtering is done.
     */
    public ActiveRowsFilter(int rowStatusColumnIndex) {
      this.rowStatusColumnIndex = rowStatusColumnIndex;
    }

    public boolean passesFilter(MOTableRow row) {
      Integer32 rs = (Integer32) row.getValue(rowStatusColumnIndex);
      if (rs == null) {
        logger.warn("RowStatus column "+rowStatusColumnIndex+
                    " does not exists although it is filter criteria");
        return false;
      }
      return (rs.getValue() == active);
    }
  }

  static class RowStatusValidator implements MOValueValidationListener {

    public void validate(MOValueValidationEvent event) {
      if (!(event.getNewValue()  instanceof Integer32)) {
        event.setValidationStatus(PDU.wrongType);
      }
      int v = ((Integer32)event.getNewValue()).getValue();
      if ((v < 1) || (v > 6) || (v == 3)) {
        event.setValidationStatus(PDU.wrongValue);
      }
    }
  }

  public void rowChanged(MOTableRowEvent event) {
    switch (event.getType()) {
      case MOTableRowEvent.CREATE: {
        // by default do not allow row creation if RowStatus is not set!
        MOTableRow row = event.getRow();
        int myIndex = getTable().getColumnIndex(getColumnID());
        if (row.getValue(myIndex) == null) {
          event.setVetoStatus(PDU.inconsistentName);
        }
        break;
      }
      case MOTableRowEvent.CHANGE: {
        // check whether the changed column can be changed if row is active
        int rowStatus = getCurrentRowStatus(event);
        switch (rowStatus) {
          case active: {
            for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy