// 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.mib2jni;
import java.io.*;
import java.util.*;
import com.ireasoning.util.*;
import com.ireasoning.protocol.snmp.*;
import javax.management.*;


/**
 * Class for registering MBeans
 */
public class AgentMib
{
    // MBeanServer reference
    private static MBeanServer _server;
    // Root node of OID tree
    private static OIDTreeNode _root;
    
    /**
     * Registers all necessary MBeans
     */
    public static void registerMBeans(MBeanServer server, OIDTreeNode root)
    {
        _server = server;
        _root = root;
        try
        {
            registerIcmpGroup();
            registerSnmpGroup();
            registerInterfacesGroup();
            registerUdpGroup();
            registerTcpGroup();
            registerSystemGroup();
            registerIpGroup();
            registerIfTable();
            registerAtTable();
            registerIpAddrTable();
            registerIpRouteTable();
            registerIpNetToMediaTable();
            registerTcpConnTable();
            registerUdpTable();
        }
        catch(Exception e)
        {
            Logger.error(e);
            throw new SnmpException(e.toString());
        }
    }
    
    /**
     * Unregisters MBeans
     */
    public static void unregisterMBeans()
    {
        try
        {
            unregisterIcmpGroup();
            unregisterSnmpGroup();
            unregisterInterfacesGroup();
            unregisterUdpGroup();
            unregisterTcpGroup();
            unregisterSystemGroup();
            unregisterIpGroup();
            unregisterIfTable();
            unregisterAtTable();
            unregisterIpAddrTable();
            unregisterIpRouteTable();
            unregisterIpNetToMediaTable();
            unregisterTcpConnTable();
            unregisterUdpTable();
        }
        catch(Exception e)
        {
            Logger.error(e);
            throw new SnmpException(e.toString());
        }
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerIcmpGroup() throws Exception
    {
        if ( _icmpGroup == null )
        {
            _icmpGroup = new IcmpGroup(_root, ".1.3.6.1.2.1.5", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=IcmpGroup,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_icmpGroup, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterIcmpGroup() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=IcmpGroup,noPdu=true"));
        _icmpGroup = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerSnmpGroup() throws Exception
    {
        if ( _snmpGroup == null )
        {
            _snmpGroup = new SnmpGroup(_root, ".1.3.6.1.2.1.11", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=SnmpGroup,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_snmpGroup, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterSnmpGroup() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=SnmpGroup,noPdu=true"));
        _snmpGroup = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerInterfacesGroup() throws Exception
    {
        if ( _interfacesGroup == null )
        {
            _interfacesGroup = new InterfacesGroup(_root, ".1.3.6.1.2.1.2", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=InterfacesGroup,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_interfacesGroup, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterInterfacesGroup() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=InterfacesGroup,noPdu=true"));
        _interfacesGroup = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerUdpGroup() throws Exception
    {
        if ( _udpGroup == null )
        {
            _udpGroup = new UdpGroup(_root, ".1.3.6.1.2.1.7", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=UdpGroup,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_udpGroup, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterUdpGroup() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=UdpGroup,noPdu=true"));
        _udpGroup = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerTcpGroup() throws Exception
    {
        if ( _tcpGroup == null )
        {
            _tcpGroup = new TcpGroup(_root, ".1.3.6.1.2.1.6", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=TcpGroup,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_tcpGroup, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterTcpGroup() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=TcpGroup,noPdu=true"));
        _tcpGroup = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerSystemGroup() throws Exception
    {
        if ( _systemGroup == null )
        {
            _systemGroup = new SystemGroup(_root, ".1.3.6.1.2.1.1", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=SystemGroup,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_systemGroup, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterSystemGroup() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=SystemGroup,noPdu=true"));
        _systemGroup = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerIpGroup() throws Exception
    {
        if ( _ipGroup == null )
        {
            _ipGroup = new IpGroup(_root, ".1.3.6.1.2.1.4", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=IpGroup,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_ipGroup, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterIpGroup() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=IpGroup,noPdu=true"));
        _ipGroup = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerIfTable() throws Exception
    {
        if ( _ifTable == null )
        {
            _ifTable = new IfTable(_root, ".1.3.6.1.2.1.2.2", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=IfTable,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_ifTable, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterIfTable() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=IfTable,noPdu=true"));
        _ifTable = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerAtTable() throws Exception
    {
        if ( _atTable == null )
        {
            _atTable = new AtTable(_root, ".1.3.6.1.2.1.3.1", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=AtTable,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_atTable, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterAtTable() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=AtTable,noPdu=true"));
        _atTable = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerIpAddrTable() throws Exception
    {
        if ( _ipAddrTable == null )
        {
            _ipAddrTable = new IpAddrTable(_root, ".1.3.6.1.2.1.4.20", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=IpAddrTable,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_ipAddrTable, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterIpAddrTable() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=IpAddrTable,noPdu=true"));
        _ipAddrTable = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerIpRouteTable() throws Exception
    {
        if ( _ipRouteTable == null )
        {
            _ipRouteTable = new IpRouteTable(_root, ".1.3.6.1.2.1.4.21", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=IpRouteTable,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_ipRouteTable, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterIpRouteTable() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=IpRouteTable,noPdu=true"));
        _ipRouteTable = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerIpNetToMediaTable() throws Exception
    {
        if ( _ipNetToMediaTable == null )
        {
            _ipNetToMediaTable = new IpNetToMediaTable(_root, ".1.3.6.1.2.1.4.22", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=IpNetToMediaTable,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_ipNetToMediaTable, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterIpNetToMediaTable() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=IpNetToMediaTable,noPdu=true"));
        _ipNetToMediaTable = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerTcpConnTable() throws Exception
    {
        if ( _tcpConnTable == null )
        {
            _tcpConnTable = new TcpConnTable(_root, ".1.3.6.1.2.1.6.13", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=TcpConnTable,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_tcpConnTable, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterTcpConnTable() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=TcpConnTable,noPdu=true"));
        _tcpConnTable = null;
    }
    
    /**
     * Creates instance and registers MBean
     */
    public static void registerUdpTable() throws Exception
    {
        if ( _udpTable == null )
        {
            _udpTable = new UdpTable(_root, ".1.3.6.1.2.1.7.5", null);
        }
        ObjectName objName = new ObjectName("iReasoning:name=UdpTable,noPdu=true");
        if(!_server.isRegistered(objName))
        {
            _server.registerMBean(_udpTable, objName);
        }
    }
    
    /**
     * Unregisters MBean
     */
    public static void unregisterUdpTable() throws Exception
    {
        _server.unregisterMBean(new ObjectName("iReasoning:name=UdpTable,noPdu=true"));
        _udpTable = null;
    }
    
    /**
     * Returns IcmpGroup object. If IcmpGroup is null, it 'll register the corresponding MBean first
     */
    public static IcmpGroup getIcmpGroup( )
    {
        if( _icmpGroup == null )
        {
            try
            {
                registerIcmpGroup();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _icmpGroup;
    }
    
    /**
     * Returns SnmpGroup object. If SnmpGroup is null, it 'll register the corresponding MBean first
     */
    public static SnmpGroup getSnmpGroup( )
    {
        if( _snmpGroup == null )
        {
            try
            {
                registerSnmpGroup();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _snmpGroup;
    }
    
    /**
     * Returns InterfacesGroup object. If InterfacesGroup is null, it 'll register the corresponding MBean first
     */
    public static InterfacesGroup getInterfacesGroup( )
    {
        if( _interfacesGroup == null )
        {
            try
            {
                registerInterfacesGroup();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _interfacesGroup;
    }
    
    /**
     * Returns UdpGroup object. If UdpGroup is null, it 'll register the corresponding MBean first
     */
    public static UdpGroup getUdpGroup( )
    {
        if( _udpGroup == null )
        {
            try
            {
                registerUdpGroup();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _udpGroup;
    }
    
    /**
     * Returns TcpGroup object. If TcpGroup is null, it 'll register the corresponding MBean first
     */
    public static TcpGroup getTcpGroup( )
    {
        if( _tcpGroup == null )
        {
            try
            {
                registerTcpGroup();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _tcpGroup;
    }
    
    /**
     * Returns SystemGroup object. If SystemGroup is null, it 'll register the corresponding MBean first
     */
    public static SystemGroup getSystemGroup( )
    {
        if( _systemGroup == null )
        {
            try
            {
                registerSystemGroup();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _systemGroup;
    }
    
    /**
     * Returns IpGroup object. If IpGroup is null, it 'll register the corresponding MBean first
     */
    public static IpGroup getIpGroup( )
    {
        if( _ipGroup == null )
        {
            try
            {
                registerIpGroup();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _ipGroup;
    }
    
    /**
     * Returns IfTable object. If IfTable is null, it 'll register the corresponding MBean first
     */
    public static IfTable getIfTable( )
    {
        if( _ifTable == null )
        {
            try
            {
                registerIfTable();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _ifTable;
    }
    
    /**
     * Returns AtTable object. If AtTable is null, it 'll register the corresponding MBean first
     */
    public static AtTable getAtTable( )
    {
        if( _atTable == null )
        {
            try
            {
                registerAtTable();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _atTable;
    }
    
    /**
     * Returns IpAddrTable object. If IpAddrTable is null, it 'll register the corresponding MBean first
     */
    public static IpAddrTable getIpAddrTable( )
    {
        if( _ipAddrTable == null )
        {
            try
            {
                registerIpAddrTable();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _ipAddrTable;
    }
    
    /**
     * Returns IpRouteTable object. If IpRouteTable is null, it 'll register the corresponding MBean first
     */
    public static IpRouteTable getIpRouteTable( )
    {
        if( _ipRouteTable == null )
        {
            try
            {
                registerIpRouteTable();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _ipRouteTable;
    }
    
    /**
     * Returns IpNetToMediaTable object. If IpNetToMediaTable is null, it 'll register the corresponding MBean first
     */
    public static IpNetToMediaTable getIpNetToMediaTable( )
    {
        if( _ipNetToMediaTable == null )
        {
            try
            {
                registerIpNetToMediaTable();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _ipNetToMediaTable;
    }
    
    /**
     * Returns TcpConnTable object. If TcpConnTable is null, it 'll register the corresponding MBean first
     */
    public static TcpConnTable getTcpConnTable( )
    {
        if( _tcpConnTable == null )
        {
            try
            {
                registerTcpConnTable();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _tcpConnTable;
    }
    
    /**
     * Returns UdpTable object. If UdpTable is null, it 'll register the corresponding MBean first
     */
    public static UdpTable getUdpTable( )
    {
        if( _udpTable == null )
        {
            try
            {
                registerUdpTable();
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        return _udpTable;
    }
    
    
    static IcmpGroup _icmpGroup;
    static SnmpGroup _snmpGroup;
    static InterfacesGroup _interfacesGroup;
    static UdpGroup _udpGroup;
    static TcpGroup _tcpGroup;
    static SystemGroup _systemGroup;
    static IpGroup _ipGroup;
    static IfTable _ifTable;
    static AtTable _atTable;
    static IpAddrTable _ipAddrTable;
    static IpRouteTable _ipRouteTable;
    static IpNetToMediaTable _ipNetToMediaTable;
    static TcpConnTable _tcpConnTable;
    static UdpTable _udpTable;
}