// Home | Go Back //

/*
 * Copyright (c) 2002-2003 iReasoning Inc. All Rights Reserved.
 * 
 * This SOURCE CODE FILE, which has been provided by iReasoning Inc. as part
 * of an iReasoning Software product for use ONLY by licensed users of the product,
 * includes CONFIDENTIAL and PROPRIETARY information of iReasoning Inc.  
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
 * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
 * THE PRODUCT.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD IREASONING SOFTWARE, ITS
 * RELATED COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY
 * CLAIMS OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR
 * DISTRIBUTION OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES
 * ARISING OUT OF OR RESULTING FROM THE USE, MODIFICATION, OR
 * DISTRIBUTION OF PROGRAMS OR FILES CREATED FROM, BASED ON, AND/OR
 * DERIVED FROM THIS SOURCE CODE FILE.
 */

package agent.master;
import java.io.*;
import java.util.*;
import com.ireasoning.util.*;
import com.ireasoning.protocol.snmp.*;
import javax.management.*;

/**
 * Class represents ifRcvAddressTable MIB object in IF_MIB
 */
public class IfRcvAddressTable extends SnmpTable implements IfRcvAddressTableMBean
{
    //public IfRcvAddressEntry( SnmpTable table, String ifRcvAddressAddress, int ifRcvAddressStatus, int ifRcvAddressType)

    //Active(1), notInService(2), notReady(3), createAndGo(4), createAndWait(5), and destroy(6) 

    Hashtable _entryMap = new Hashtable();
    ArrayList _oids = new ArrayList();
    
    /**
     * Constructor
     * @param root SnmpOID tree root
     * @param oid the SnmpOID of this table. For example, ".1.3.6.1.2.1.2.2" for IfTable in RFC1213  
     * @param args the objects passed from caller for Initialization purpose
     */
    public IfRcvAddressTable (OIDTreeNode root, String oid, Object[] args)
    {
        super(root, oid);
        setProcessSnmpRequestDirectly(true);
    }
 
    public SnmpTableEntry getOID(SnmpOID oid)
    {
        Logger.debug( "IfRcvAddressTable.getOID. oid=" + oid + ", _entryMap.size=" + _entryMap.size());
        SnmpTableEntry entry = (SnmpTableEntry) _entryMap.get(oid);
        return entry;       
    }

    public SnmpTableEntry getNextOID(SnmpOID oid)
    {
        Logger.debug( "IfRcvAddressTable.getNextOID. oid=" + oid + ", _entryMap.size=" + _entryMap.size());
        int size = _oids.size();
        for (int i = 0; i < size ; i++) 
        {
            SnmpOID o = (SnmpOID) _oids.get(i);
            if(o.compareTo(oid) > 0)
            {
                IfRcvAddressEntry entry = (IfRcvAddressEntry)  _entryMap.get(o);
                entry.setOID(o);
                return entry;
            }
        }
        return null;
    }

    private boolean checkDestroy(Collection varbinds, SnmpOID rowStatusColumnOID)
    {
        Iterator it = varbinds.iterator();
        while(it.hasNext())
        {
            SnmpVarBind vb = (SnmpVarBind)it.next();
            SnmpOID oid = (SnmpOID) vb.getName();
            if(oid.startsWith(rowStatusColumnOID))
            {
                int val = ((SnmpInt)vb.getValue()).getValue();
                if(val == 6)//destroy row
                {
                    SnmpTableEntry entry = (SnmpTableEntry) _entryMap.get(oid);
                    String suffix = entry.getIndexSuffix();
                    int size = _oids.size();
                    ArrayList removes = new ArrayList();
                    for (int i = 0; i < size ; i++) 
                    {
                        SnmpOID o = (SnmpOID) _oids.get(i);
                        if(o.endsWith(suffix)) removes.add(o);
                    }
                    size = removes.size();
                    for (int i = 0; i < size ; i++) 
                    {
                        SnmpOID o = (SnmpOID) removes.get(i);
                        Logger.debug( "delete:" + o);
                        _oids.remove(o);
                        _entryMap.remove(o);
                    }
                    return true;
                }
            }
        }            
        return false;
    }
    
    public boolean preSetValue(SnmpTableEntry entry, SnmpVarBind newValue, Collection varbinds)
    {
        try
        {
            Logger.debug( "Enter preSetValue.........");
            SnmpOID oid = (SnmpOID) newValue.getName();

            SnmpOID entryNodeOID = (SnmpOID) _tableNode.getFirstChild().getName();
            SnmpOID rowStatusColumnOID = new SnmpOID(entryNodeOID).append(".2");
            SnmpOID addressTypeOID = new SnmpOID(entryNodeOID).append(".3");
            
            if(_entryMap.get(oid) != null && checkDestroy(varbinds, rowStatusColumnOID)) return true;
            
            boolean found = false;
            SnmpVarBind vb = null;
            SnmpVarBind addressTypeVB = null;
            SnmpVarBind rowStatusVB = null;
            Logger.debug( "varbinds.size=" + varbinds.size());
            Iterator it = varbinds.iterator();
            while(it.hasNext())
            {
                vb = (SnmpVarBind)it.next();
                SnmpOID o = (SnmpOID) vb.getName();
                Logger.debug( "o=" + o + ", rowStatusColumnOID=" + rowStatusColumnOID);
                if(o.startsWith(rowStatusColumnOID))
                {
                    rowStatusVB = vb;
                    found = true;
                }
                else if(o.startsWith(addressTypeOID))
                {
                    addressTypeVB = vb;
                }
            }
            if(!found) return false;

            oid = (SnmpOID) rowStatusVB.getName();
            SnmpOID suffix = oid.suboid(rowStatusColumnOID.getLength(), oid.getLength());

            int ifIndex = suffix.get(0);
            String address = suffix.suboid(1, suffix.getLength()).toString();
            address = address.substring(1, address.length());
            int ifRcvAddressStatus = ((SnmpInt) rowStatusVB.getValue()).getValue();
            if(ifRcvAddressStatus == 4)
            {
                ifRcvAddressStatus = 1; // become ready state 
            }
            else if(ifRcvAddressStatus == 5)
            {
                ifRcvAddressStatus = 3;//not ready yet
            }
            else return false;
            
            int ifRcvAddressType = 1;//just assign it an arbitrary value
            if(addressTypeVB != null)
            {
                ifRcvAddressType = ((SnmpInt)addressTypeVB.getValue()).getValue();
            }
            //public IfRcvAddressEntry( SnmpTable table, String ifRcvAddressAddress, int ifRcvAddressStatus, int ifRcvAddressType)
            IfRcvAddressEntry e = new IfRcvAddressEntry(this, address, ifRcvAddressStatus, ifRcvAddressType);
            e.setIndexSuffix(suffix.toString());

            for (int i = 2; i <= 3  ; i++) //starting from 2, because ifRcvAddressAddress is not accesible
            {
                SnmpOID o1 = new SnmpOID(entryNodeOID).append("." + i).append(suffix);
                Logger.debug( "add:" + o1 + ", suffix =" + suffix);
                _oids.add(o1);
                _entryMap.put(o1, e);
            }
            Collections.sort(_oids);
        }
        catch(Exception e)
        {
            Logger.error(e);
        }
        return true;
    }
   
    /**
     * Creates a new instance of table entry object
     */
    public SnmpTableEntry newEntryInstance()
    {
        return new IfRcvAddressEntry (this);
    }
    
    /**
     * Gets ifRcvAddressAddress value
     * @param ifRcvAddressEntry table entry object
     * @param pdu the received SnmpPdu object
     */
    public String getIfRcvAddressAddress(SnmpTableEntry ifRcvAddressEntry, SnmpPdu pdu)
    {
        // This MIB node is not readable, so IfRcvAddressTableMBean does not have this method
        synchronized (ifRcvAddressEntry)
        {
            IfRcvAddressEntry entry = (IfRcvAddressEntry) ifRcvAddressEntry;
            // TODO: Add your implementation
            return entry._ifRcvAddressAddress;
        }
    }
    
    /**
     * Sets new ifRcvAddressAddress value
     * @param ifRcvAddressEntry table entry object
     * @param newValue new value to be set
     * @param pdu the received SnmpPdu object
     */
    public void setIfRcvAddressAddressValue(SnmpTableEntry ifRcvAddressEntry, Object newValue, SnmpPdu pdu)
    {
        // This MIB node is not writeable, so IfRcvAddressTableMBean does not have this method
        synchronized (ifRcvAddressEntry)
        {
            IfRcvAddressEntry entry = (IfRcvAddressEntry) ifRcvAddressEntry;
            entry._ifRcvAddressAddress = (String) newValue;
            // TODO: Add your implementation
        }
    }
    
    /**
     * Gets ifRcvAddressStatus value
     * @param ifRcvAddressEntry table entry object
     * @param pdu the received SnmpPdu object
     */
    public int getIfRcvAddressStatus(SnmpTableEntry ifRcvAddressEntry, SnmpPdu pdu)
    {
        synchronized (ifRcvAddressEntry)
        {
            IfRcvAddressEntry entry = (IfRcvAddressEntry) ifRcvAddressEntry;
            // TODO: Add your implementation
            return entry._ifRcvAddressStatus;
        }
    }
    
    /**
     * Sets new ifRcvAddressStatus value
     * @param ifRcvAddressEntry table entry object
     * @param newValue new value to be set
     * @param pdu the received SnmpPdu object
     */
    public void setIfRcvAddressStatusValue(SnmpTableEntry ifRcvAddressEntry, Object newValue, SnmpPdu pdu)
    {
        synchronized (ifRcvAddressEntry)
        {
            IfRcvAddressEntry entry = (IfRcvAddressEntry) ifRcvAddressEntry;
            entry._ifRcvAddressStatus = ( (Integer) newValue).intValue() ;
            // TODO: Add your implementation
        }
    }
    
    /**
     * Gets ifRcvAddressType value
     * @param ifRcvAddressEntry table entry object
     * @param pdu the received SnmpPdu object
     */
    public int getIfRcvAddressType(SnmpTableEntry ifRcvAddressEntry, SnmpPdu pdu)
    {
        synchronized (ifRcvAddressEntry)
        {
            IfRcvAddressEntry entry = (IfRcvAddressEntry) ifRcvAddressEntry;
            // TODO: Add your implementation
            return entry._ifRcvAddressType;
        }
    }
    
    /**
     * Sets new ifRcvAddressType value
     * @param ifRcvAddressEntry table entry object
     * @param newValue new value to be set
     * @param pdu the received SnmpPdu object
     */
    public void setIfRcvAddressTypeValue(SnmpTableEntry ifRcvAddressEntry, Object newValue, SnmpPdu pdu)
    {
        synchronized (ifRcvAddressEntry)
        {
            IfRcvAddressEntry entry = (IfRcvAddressEntry) ifRcvAddressEntry;
            entry._ifRcvAddressType = ( (Integer) newValue).intValue() ;
            // TODO: Add your implementation
        }
    }
    
}