/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License).  You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the license at
 * https://glassfish.dev.java.net/public/CDDLv1.0.html or
 * glassfish/bootstrap/legal/CDDLv1.0.txt.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at glassfish/bootstrap/legal/CDDLv1.0.txt.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * Copyright (c) Ericsson AB, 2004-2007. All rights reserved.
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 */

package org.jvnet.glassfish.comms.admin.management.extensions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.sun.enterprise.management.support.TypeData;
import com.sun.appserv.management.ext.support.ExtendedAMX;

import org.jvnet.glassfish.comms.admin.management.extensions.SIPXTypes;
import org.jvnet.glassfish.comms.management.config.SIPServiceConfig;
import org.jvnet.glassfish.comms.management.config.SIPListenerConfig;
import org.jvnet.glassfish.comms.management.config.SIPProtocolConfig;
import org.jvnet.glassfish.comms.management.config.SIPLinkConfig;
import org.jvnet.glassfish.comms.management.config.SIPTimersConfig;

import org.jvnet.glassfish.comms.management.monitor.SIPServiceMonitor;
import org.jvnet.glassfish.comms.management.monitor.NetworkManagerMonitor;
import org.jvnet.glassfish.comms.management.monitor.SessionManagerMonitor;
import org.jvnet.glassfish.comms.management.monitor.TransactionManagerMonitor;
import org.jvnet.glassfish.comms.management.monitor.OverloadProtectionManagerMonitor;

/**
 * Pluggable AMX Support feature for SIP interfaces.
 * This is the implementation class for the extended AMX support features.
 *
 * The following data will be returned to the AMX support layer
 * 1. List of the MBean interface classes for config and monitor types
 * 2. Mapping of the interfaces and the implementation packages
 * 3. The new config types (DATA) 
 * 4. The new monitor types (MONITOR_DATA)
 * 5. The new config types children 
 * 6. The new monitor types children 
 * 7. The old config types
 * 8. The old monitor types
 *
 * @author: Yamini K B
 */
public class SIPAMXSupport implements ExtendedAMX {

    private static final String CONFIG_INTERFACE_PACKAGE = "org.jvnet.glassfish.comms.management.config.";
    private static final String CONFIG_IMPL_PACKAGE = "org.jvnet.glassfish.comms.admin.management.extensions.config.";
    private static final String MONITOR_INTERFACE_PACKAGE = "org.jvnet.glassfish.comms.management.monitor.";
    private static final String MONITOR_IMPL_PACKAGE = "org.jvnet.glassfish.comms.admin.management.extensions.monitor.";

    private static final Class[] EXT_MBEAN_INTERFACES = {
        SIPServiceConfig.class,
        SIPListenerConfig.class,
        SIPProtocolConfig.class,
        SIPLinkConfig.class,
        SIPTimersConfig.class,
        SIPServiceMonitor.class,
        NetworkManagerMonitor.class,
        SessionManagerMonitor.class,
        TransactionManagerMonitor.class,
        OverloadProtectionManagerMonitor.class,
    };

    /*
     * Child types
     */
    private static final TypeData[] DATA  = new TypeData[]
    {
        new SIPServiceChild( SIPXTypes.SIP_LISTENER_CONFIG ),
        new SIPServiceChild( SIPXTypes.SIP_PROTOCOL_CONFIG ),
    };

    /*
     * Monitor child types
     */
    private static TypeData[] MONITOR_DATA  = new TypeData[]
    {
        new SIPServiceMonitorChild( SIPXTypes.NETWORK_MANAGER_MONITOR ),
        new SIPServiceMonitorChild( SIPXTypes.SESSION_MANAGER_MONITOR ),
        new SIPServiceMonitorChild( SIPXTypes.TRANSACTION_MANAGER_MONITOR ),
        new SIPServiceMonitorChild( SIPXTypes.OVERLOAD_PROTECTION_MANAGER_MONITOR ),
    };

    public SIPAMXSupport() {
    }
    
    /*
     * Implementation to return new MBean interfaces
     */
    public Class[] getExternalMbeanInterfaces() {
        return EXT_MBEAN_INTERFACES;
    }

    /*
     * Get the new config types
     */
    public String[] getAllConfigTypes() {

        return new String[] {SIPXTypes.SIP_SERVICE_CONFIG};
    }

    /*
     * Get the misc child types
     */
    public String[][] getAllMiscChildTypes() {

        return new String[][] {
                                {SIPXTypes.SIP_LINK_CONFIG, SIPXTypes.SIP_PROTOCOL_CONFIG},
                                {SIPXTypes.SIP_TIMERS_CONFIG, SIPXTypes.SIP_PROTOCOL_CONFIG},
                              };
    }

    private static final class SIPServiceChild extends TypeData
    {
        public SIPServiceChild( final String    j2eeType  )
        {
            super( j2eeType, SIPXTypes.SIP_SERVICE_CONFIG );
        }
    }

    /*
     * Get all the child types
     */
    public Object[] getAllChildTypes() {
        return (Object[])DATA;
    }

    /*
     * Get the old config types 
     */
    public Map<String,String> getOldConfigTypes() {
        Map<String,String> m = new HashMap<String,String>();
        m.put(SIPXTypes.SIP_SERVICE_CONFIG, "sip-service");
        m.put(SIPXTypes.SIP_LISTENER_CONFIG, "sip-listener");
        m.put(SIPXTypes.SIP_PROTOCOL_CONFIG, "sip-protocol");
        m.put(SIPXTypes.SIP_LINK_CONFIG, "sip-link");
        m.put(SIPXTypes.SIP_TIMERS_CONFIG, "sip-timers");
        return m;
    }

    /*
     * Get a mapping of interface and implementation class
     */
    public Map<String,String> getInterfaceImpls() {
        Map<String,String> m = new HashMap<String,String>();
        m.put(CONFIG_INTERFACE_PACKAGE+"SIPServiceConfig", CONFIG_IMPL_PACKAGE+"SIPServiceConfigImpl");
        m.put(CONFIG_INTERFACE_PACKAGE+"SIPListenerConfig", CONFIG_IMPL_PACKAGE+"SIPListenerConfigImpl");
        m.put(CONFIG_INTERFACE_PACKAGE+"SIPProtocolConfig", CONFIG_IMPL_PACKAGE+"SIPProtocolConfigImpl");
        m.put(CONFIG_INTERFACE_PACKAGE+"SIPLinkConfig", CONFIG_IMPL_PACKAGE+"SIPLinkConfigImpl");
        m.put(CONFIG_INTERFACE_PACKAGE+"SIPTimersConfig", CONFIG_IMPL_PACKAGE+"SIPTimersConfigImpl");
        m.put(MONITOR_INTERFACE_PACKAGE+"SIPServiceMonitor", MONITOR_IMPL_PACKAGE+"SIPServiceMonitorImpl");
        m.put(MONITOR_INTERFACE_PACKAGE+"NetworkManagerMonitor", MONITOR_IMPL_PACKAGE+"NetworkManagerMonitorImpl");
        m.put(MONITOR_INTERFACE_PACKAGE+"SessionManagerMonitor", MONITOR_IMPL_PACKAGE+"SessionManagerMonitorImpl");
        m.put(MONITOR_INTERFACE_PACKAGE+"TransactionManagerMonitor", MONITOR_IMPL_PACKAGE+"TransactionManagerMonitorImpl");
        m.put(MONITOR_INTERFACE_PACKAGE+"OverloadProtectionManagerMonitor", MONITOR_IMPL_PACKAGE+"OverloadProtectionManagerMonitorImpl");
        return m;
    }

    /*
     * Get all the old monitor types 
     */
    public Map<String,String> getOldMonitorTypes() {
        Map<String,String> m = new HashMap<String,String>();
        m.put(SIPXTypes.SIP_SERVICE_MONITOR, "sip-service");
        m.put(SIPXTypes.NETWORK_MANAGER_MONITOR, "network-manager");
        m.put(SIPXTypes.SESSION_MANAGER_MONITOR, "session-manager");
        m.put(SIPXTypes.TRANSACTION_MANAGER_MONITOR, "transaction-manager");
        m.put(SIPXTypes.OVERLOAD_PROTECTION_MANAGER_MONITOR, "overload-protection-manager");
        return m;
    }

    private static final class SIPServiceMonitorChild extends TypeData {
                public SIPServiceMonitorChild( final String    j2eeType  )
                {
                        super( j2eeType, SIPXTypes.SIP_SERVICE_MONITOR );
                }
    }

    /*
     * Get the server root monitor types
     */
    public String[] getAllServerRootMonitorTypes() {

        return new String[] {SIPXTypes.SIP_SERVICE_MONITOR};
    }

    /*
     * Get all the child monitor types 
     */
    public Object[] getAllMonitorChildTypes() {
        return (Object[])MONITOR_DATA;
    }

}
