<commit_before>/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2012 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <<EMAIL>>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.netmgt.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.InetAddress;
import java.util.concurrent.locks.Lock;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.opennms.core.utils.ByteArrayComparator;
import org.opennms.core.utils.ConfigFileConstants;
import org.opennms.core.utils.FileReloadCallback;
import org.opennms.core.utils.FileReloadContainer;
import org.opennms.core.utils.IPLike;
import org.opennms.core.utils.InetAddressUtils;
import org.opennms.core.utils.LogUtils;
import org.opennms.core.xml.JaxbUtils;
import org.opennms.netmgt.config.snmp.Definition;
import org.opennms.netmgt.config.snmp.Range;
import org.opennms.netmgt.config.snmp.SnmpConfig;
import org.opennms.netmgt.snmp.SnmpAgentConfig;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import com.googlecode.concurentlocks.ReadWriteUpdateLock;
import com.googlecode.concurentlocks.ReentrantReadWriteUpdateLock;

/**
 * This class is the main repository for SNMP configuration information used by
 * the capabilities daemon. When this class is loaded it reads the snmp
 * configuration into memory, and uses the configuration to find the
 * {@link org.opennms.netmgt.snmp.SnmpAgentConfig SnmpAgentConfig} objects for specific
 * addresses. If an address cannot be located in the configuration then a
 * default peer instance is returned to the caller.
 *
 * <strong>Note: </strong>Users of this class should make sure the
 * <em>init()</em> is called before calling any other method to ensure the
 * config is loaded before accessing other convenience methods.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>">Weave</a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class SnmpPeerFactory implements SnmpAgentConfigFactory {
    private static final int DEFAULT_SNMP_PORT = 161;
    private static final ReadWriteUpdateLock m_globalLock = new ReentrantReadWriteUpdateLock();
    private static final Lock m_readLock = m_globalLock.updateLock();
    private static final Lock m_writeLock = m_globalLock.writeLock();

    /**
     * The singleton instance of this factory
     */
    private static SnmpPeerFactory m_singleton = null;

    private static File m_configFile;

    /**
     * The config class loaded from the config file
     */
    private SnmpConfig m_config;

    private static FileReloadContainer<SnmpConfig> m_container;
    private static FileReloadCallback<SnmpConfig> m_callback;

    /**
     * This member is set to true if the configuration file has been loaded.
     */
    private static volatile boolean m_loaded = false;

    private static final int VERSION_UNSPECIFIED = -1;

    /**
     * <p>Constructor for SnmpPeerFactory.</p>
     *
     * @param resource a {@link org.springframework.core.io.Resource} object.
     */
    public SnmpPeerFactory(final Resource resource) {
        SnmpPeerFactory.getWriteLock().lock();

        try {
            final SnmpConfig config = JaxbUtils.unmarshal(SnmpConfig.class, resource);

            try {
                final File file = resource.getFile();
                if (file != null) {
                    m_callback = new FileReloadCallback<SnmpConfig>() {
                        @Override
                        public SnmpConfig reload(final SnmpConfig object, final Resource resource) throws IOException {
                            return JaxbUtils.unmarshal(SnmpConfig.class, resource);
                        }

                    };
                    m_container = new FileReloadContainer<SnmpConfig>(config, resource, m_callback);
                    return;
                }
            } catch (final IOException e) {
                LogUtils.debugf(this, "No file associated with resource %s, skipping reload container initialization.", resource);
            }

            // if we fall through to here, then the file was null, or something else went wrong store the config directly
            m_config = config;
        } finally {
            SnmpPeerFactory.getWriteLock().unlock();
        }
    }

    public static Lock getReadLock() {
        return m_readLock;
    }

    public static Lock getWriteLock() {
        return m_writeLock;
    }

    public static synchronized void init() throws IOException {
        final File cfgFile = getFile();
        LogUtils.debugf(SnmpPeerFactory.class, "init: config file path: %s", cfgFile.getPath());
        final FileSystemResource resource = new FileSystemResource(cfgFile);

        m_singleton = new SnmpPeerFactory(resource);
        m_loaded = true;
    }

    /**
     * Load the config from the default config file and create the singleton instance of this factory.
     *
     * @exception java.io.IOException Thrown if the specified config file cannot be read
     */
    public static synchronized SnmpPeerFactory getInstance() {
        if (!m_loaded) {
            try {
                init();
            } catch (final IOException e) {
                LogUtils.errorf(SnmpPeerFactory.class, e, "Failed to initialize SnmpPeerFactory instance!");
            }
        }
        return m_singleton;
    }

    /**
     * <p>setInstance</p>
     *
     * @param singleton a {@link org.opennms.netmgt.config.SnmpPeerFactory} object.
     */
    public static synchronized void setInstance(final SnmpPeerFactory singleton) {
        m_singleton = singleton;
        m_loaded = true;
    }

    public static synchronized File getFile() throws IOException {
        if (m_configFile == null) {
            setFile(ConfigFileConstants.getFile(ConfigFileConstants.SNMP_CONF_FILE_NAME));
        }
        return m_configFile;
    }

    /**
     * <p>setFile</p>
     *
     * @param configFile a {@link java.io.File} object.
     */
    public static synchronized void setFile(final File configFile) {
        final File oldFile = m_configFile;
        m_configFile = configFile;

        // if the file changed then we need to reload the config
        if (oldFile == null || m_configFile == null || !oldFile.equals(m_configFile)) {
            m_singleton = null;
            m_loaded = false;
        }
    }

    /**
     * Saves the current settings to disk
     *
     * @throws java.io.IOException if any.
     * @throws org.exolab.castor.xml.MarshalException if any.
     * @throws org.exolab.castor.xml.ValidationException if any.
     */
    public void saveCurrent() throws IOException {
        saveToFile(getFile());
    }

    public void saveToFile(final File file) throws UnsupportedEncodingException, FileNotFoundException, IOException {
        // Marshal to a string first, then write the string to the file. This
        // way the original config isn't lost if the XML from the marshal is hosed.
        SnmpPeerFactory.getWriteLock().lock();

        final String marshalledConfig = getSnmpConfigAsString();

        FileOutputStream out = null;
        Writer fileWriter = null;
        try {
            if (marshalledConfig != null) {
                out = new FileOutputStream(file);
                fileWriter = new OutputStreamWriter(out, "UTF-8");
                fileWriter.write(marshalledConfig);
                fileWriter.flush();
                fileWriter.close();
                if (m_container != null) {
                    m_container.reload();
                }
            }
        } finally {
            IOUtils.closeQuietly(fileWriter);
            IOUtils.closeQuietly(out);
            SnmpPeerFactory.getWriteLock().unlock();
        }
    }

    /** {@inheritDoc} */
    public SnmpAgentConfig getAgentConfig(final InetAddress agentAddress) {
        return getAgentConfig(agentAddress, VERSION_UNSPECIFIED);
    }

    private SnmpAgentConfig getAgentConfig(final InetAddress agentInetAddress, final int requestedSnmpVersion) {
        if (getSnmpConfig() == null) {
            final SnmpAgentConfig agentConfig = new SnmpAgentConfig(agentInetAddress);
            if (requestedSnmpVersion == VERSION_UNSPECIFIED) {
                agentConfig.setVersion(SnmpAgentConfig.DEFAULT_VERSION);
            } else {
                agentConfig.setVersion(requestedSnmpVersion);
            }

            return agentConfig;
        }

        final SnmpAgentConfig agentConfig = new SnmpAgentConfig(agentInetAddress);

        // Now set the defaults from the getSnmpConfig()
        setSnmpAgentConfig(agentConfig, new Definition(), requestedSnmpVersion);

        // Attempt to locate the node
        DEFLOOP: for (final Definition def : getSnmpConfig().getDefinitionCollection()) {
            // check the specifics first
            for (final String saddr : def.getSpecificCollection()) {
                try {
                    final InetAddress addr = InetAddressUtils.addr(saddr);
                    if (addr != null && addr.equals(agentConfig.getAddress())) {
                        setSnmpAgentConfig(agentConfig, def, requestedSnmpVersion);
                        break DEFLOOP;
                    }
                } catch (final IllegalArgumentException e) {
                    LogUtils.debugf(this, e, "Error while reading SNMP config <specific> tag: %s", saddr);
                }
            }

            // check the ranges
            //
            final ByteArrayComparator comparator = new ByteArrayComparator();

            for (final Range rng : def.getRangeCollection()) {
                final byte[] addr = agentConfig.getAddress().getAddress();
                final byte[] begin = InetAddressUtils.toIpAddrBytes(rng.getBegin());
                final byte[] end = InetAddressUtils.toIpAddrBytes(rng.getEnd());

                boolean inRange = InetAddressUtils.isInetAddressInRange(addr, begin, end);
                if (comparator.compare(begin, end) <= 0) {
                    inRange = InetAddressUtils.isInetAddressInRange(addr, begin, end);
                } else {
                    LogUtils.warnf(this, "%s has an 'end' that is earlier than its 'beginning'!", rng);
                    inRange = InetAddressUtils.isInetAddressInRange(addr, end, begin);
                }
                if (inRange) {
                    setSnmpAgentConfig(agentConfig, def, requestedSnmpVersion);
                    break DEFLOOP;
                }
            }

            // check the matching ip expressions
            for (final String ipMatch : def.getIpMatchCollection()) {
                if (IPLike.matches(agentInetAddress, ipMatch)) {
                    setSnmpAgentConfig(agentConfig, def, requestedSnmpVersion);
                    break DEFLOOP;
                }
            }

        } // end DEFLOOP
        return agentConfig;
    }

    private void setSnmpAgentConfig(final SnmpAgentConfig agentConfig, final Definition def, final int requestedSnmpVersion) {
        int version = determineVersion(def, requestedSnmpVersion);

        setCommonAttributes(agentConfig, def, version);
        agentConfig.setSecurityLevel(determineSecurityLevel(def));
        agentConfig.setSecurityName(determineSecurityName(def));
        agentConfig.setAuthProtocol(determineAuthProtocol(def));
        agentConfig.setAuthPassPhrase(determineAuthPassPhrase(def));
        agentConfig.setPrivPassPhrase(determinePrivPassPhrase(def));
        agentConfig.setPrivProtocol(determinePrivProtocol(def));
        agentConfig.setReadCommunity(determineReadCommunity(def));
        agentConfig.setWriteCommunity(determineWriteCommunity(def));
        agentConfig.setEngineId(determineEngineId(def));
        agentConfig.setEnterpriseId(determineEnterpriseId(def));
        agentConfig.setContextEngineId(determineContextEngineId(def));
        agentConfig.setContextName(determineContextName(def));
    }

    /**
     * This is a helper method to set all the common attributes in the agentConfig.
     * 
     * @param agentConfig
     * @param def
     * @param version
     */
    private void setCommonAttributes(final SnmpAgentConfig agentConfig, final Definition def, final int version) {
        agentConfig.setVersion(version);
        agentConfig.setPort(determinePort(def));
        agentConfig.setRetries(determineRetries(def));
        agentConfig.setTimeout((int)determineTimeout(def));
        agentConfig.setMaxRequestSize(determineMaxRequestSize(def));
        agentConfig.setMaxVarsPerPdu(determineMaxVarsPerPdu(def));
        agentConfig.setMaxRepetitions(determineMaxRepetitions(def));
        InetAddress proxyHost = determineProxyHost(def);

        if (proxyHost != null) {
            agentConfig.setProxyFor(agentConfig.getAddress());
            agentConfig.setAddress(determineProxyHost(def));
        }
    }

    private int determineMaxRepetitions(final Definition def) {
        return (!def.hasMaxRepetitions() ? 
            (!getSnmpConfig().hasMaxRepetitions() ?
                SnmpAgentConfig.DEFAULT_MAX_REPETITIONS : getSnmpConfig().getMaxRepetitions()) : def.getMaxRepetitions());
    }

    private InetAddress determineProxyHost(final Definition def) {
        InetAddress inetAddr = null;
        final String address = def.getProxyHost() == null ? (getSnmpConfig().getProxyHost() == null ? null : getSnmpConfig().getProxyHost()) : def.getProxyHost();
        if (address != null) {
            try {
                inetAddr =  InetAddressUtils.addr(address);
            } catch (final IllegalArgumentException e) {
                LogUtils.debugf(this, e, "Error while reading SNMP config proxy host: %s", address);
            }
        }
        return inetAddr;
    }

    private int determineMaxVarsPerPdu(final Definition def) {
        return (!def.hasMaxVarsPerPdu() ? 
            (!getSnmpConfig().hasMaxVarsPerPdu() ?
                SnmpAgentConfig.DEFAULT_MAX_VARS_PER_PDU : getSnmpConfig().getMaxVarsPerPdu()) : def.getMaxVarsPerPdu());
    }
    /**
     * Helper method to search the snmp-config for the appropriate read
     * community string.
     * @param def
     * @return
     */
    private String determineReadCommunity(final Definition def) {
        return (def.getReadCommunity() == null ? (getSnmpConfig().getReadCommunity() == null ? SnmpAgentConfig.DEFAULT_READ_COMMUNITY :getSnmpConfig().getReadCommunity()) : def.getReadCommunity());
    }

    /**
     * Helper method to search the snmp-config for the appropriate write
     * community string.
     * @param def
     * @return
     */
    private String determineWriteCommunity(final Definition def) {
        return (def.getWriteCommunity() == null ? (getSnmpConfig().getWriteCommunity() == null ? SnmpAgentConfig.DEFAULT_WRITE_COMMUNITY :getSnmpConfig().getWriteCommunity()) : def.getWriteCommunity());
    }

    /**
     * Helper method to search the snmp-config for the appropriate maximum
     * request size.  The default is the minimum necessary for a request.
     * @param def
     * @return
     */
    private int determineMaxRequestSize(final Definition def) {
        return (!def.hasMaxRequestSize() ? (!getSnmpConfig().hasMaxRequestSize() ? SnmpAgentConfig.DEFAULT_MAX_REQUEST_SIZE : getSnmpConfig().getMaxRequestSize()) : def.getMaxRequestSize());
    }

    /**
     * Helper method to find a security name to use in the snmp-config.  If v3 has
     * been specified and one can't be found, then a default is used for this
     * is a required option for v3 operations.
     * @param def
     * @return
     */
    private String determineSecurityName(final Definition def) {
        final String securityName = (def.getSecurityName() == null ? getSnmpConfig().getSecurityName() : def.getSecurityName() );
        if (securityName == null) {
            return SnmpAgentConfig.DEFAULT_SECURITY_NAME;
        }
        return securityName;
    }

    /**
     * Helper method to find a security name to use in the snmp-config.  If v3 has
     * been specified and one can't be found, then a default is used for this
     * is a required option for v3 operations.
     * @param def
     * @return
     */
    private String determineAuthProtocol(final Definition def) {
        final String authProtocol = (def.getAuthProtocol() == null ? getSnmpConfig().getAuthProtocol() : def.getAuthProtocol());
        if (authProtocol == null && determineAuthPassPhrase(def) != null) {
            return SnmpAgentConfig.DEFAULT_AUTH_PROTOCOL;
        }
        return authProtocol;
    }

    /**
     * Helper method to find a authentication passphrase to use from the snmp-config.  If v3 has
     * been specified and one can't be found, then a default is used for this
     * is a required option for v3 operations.
     * @param def
     * @return
     */
    private String determineAuthPassPhrase(final Definition def) {
        final String authPassPhrase = (def.getAuthPassphrase() == null ? getSnmpConfig().getAuthPassphrase() : def.getAuthPassphrase());
        // Forcing a default is wrong, because if it is not explicitly defined, probably it means that it should not be used, and SNMP4J expect null for optional parameters.
        /*
        if (authPassPhrase == null) {
            return SnmpAgentConfig.DEFAULT_AUTH_PASS_PHRASE;
        }
         */
        return authPassPhrase;
    }

    /**
     * Helper method to find a privacy passphrase to use from the snmp-config.  If v3 has
     * been specified and one can't be found, then a default is used for this
     * is a required option for v3 operations.
     * @param def
     * @return
     */
    private String determinePrivPassPhrase(final Definition def) {
        final String privPassPhrase = (def.getPrivacyPassphrase() == null ? getSnmpConfig().getPrivacyPassphrase() : def.getPrivacyPassphrase());
        // Forcing a default is wrong, because if it is not explicitly defined, probably it means that it should not be used, and SNMP4J expect null for optional parameters.
        /*
        if (privPassPhrase == null) {
            return SnmpAgentConfig.DEFAULT_PRIV_PASS_PHRASE;
        }
         */
        return privPassPhrase;
    }

    /**
     * Helper method to find a privacy protocol to use from the snmp-config.  If v3 has
     * been specified and one can't be found, then a default is used for this
     * is a required option for v3 operations.
     * @param def
     * @return
     */
    private String determinePrivProtocol(final Definition def) {
        final String authPrivProtocol = (def.getPrivacyProtocol() == null ? getSnmpConfig().getPrivacyProtocol() : def.getPrivacyProtocol());
        if (authPrivProtocol == null && determinePrivPassPhrase(def) != null) {
            return SnmpAgentConfig.DEFAULT_PRIV_PROTOCOL;
        }
        return authPrivProtocol;
    }

    /**
     * Helper method to set the security level in v3 operations.  The default is
     * noAuthNoPriv if there is no authentication passphrase.  From there, if
     * there is a privacy passphrase supplied, then the security level is set to
     * authPriv else it falls out to authNoPriv.  There are only these 3 possible
     * security levels.
     * default 
     * @param def
     * @return
     */
    private int determineSecurityLevel(final Definition def) {

        // use the def security level first
        if (def.hasSecurityLevel()) {
            return def.getSecurityLevel();
        }

        // use a configured default security level next
        if (getSnmpConfig().hasSecurityLevel()) {
            return getSnmpConfig().getSecurityLevel();
        }

        // if no security level configuration exists use
        int securityLevel = SnmpAgentConfig.NOAUTH_NOPRIV;

        final String authPassPhrase = (StringUtils.isBlank(def.getAuthPassphrase()) ? getSnmpConfig().getAuthPassphrase() : def.getAuthPassphrase());
        final String privPassPhrase = (StringUtils.isBlank(def.getPrivacyPassphrase()) ? getSnmpConfig().getPrivacyPassphrase() : def.getPrivacyPassphrase());

        if (authPassPhrase == null) {
            securityLevel = SnmpAgentConfig.NOAUTH_NOPRIV;
        } else {
            if (privPassPhrase == null) {
                securityLevel = SnmpAgentConfig.AUTH_NOPRIV;
            } else {
                securityLevel = SnmpAgentConfig.AUTH_PRIV;
            }
        }

        return securityLevel;
    }

    /**
     * Helper method to search the snmp-config for a port
     * @param def
     * @return
     */
    private int determinePort(final Definition def) {
        return (def.getPort() == 0 ? (getSnmpConfig().getPort() == 0 ? DEFAULT_SNMP_PORT : getSnmpConfig().getPort()) : def.getPort());
    }

    /**
     * Helper method to search the snmp-config for a engineId
     * @param def
     * @return
     */
    private String determineEngineId(Definition def) {
        if (def.getEngineId() != null) return def.getEngineId();
        if (getSnmpConfig().getEngineId() != null) return getSnmpConfig().getEngineId();
        return null;
    }

    /**
     * Helper method to search the snmp-config for a contextEngineId
     * @param def
     * @return 
     */
    private String determineContextEngineId(Definition def) {
        if (def.getContextEngineId() != null) return def.getContextEngineId();
        if (getSnmpConfig().getContextEngineId() != null) return getSnmpConfig().getContextEngineId();
        return null;
    }

    /**
     * Helper method to search the snmp-config for a contextName
     * @param def
     * @return 
     */
    private String determineContextName(Definition def) {
        if (def.getContextName() != null) return def.getContextName();
        if (getSnmpConfig().getContextName() != null) return getSnmpConfig().getContextName();
        return null;
    }

    /**
     * Helper method to search the snmp-config for a enterpriseId
     * @param def
     * @return 
     */
    private String determineEnterpriseId(Definition def) {
        if (def.getEnterpriseId() != null) return def.getEnterpriseId();
        if (getSnmpConfig().getEnterpriseId() != null) return getSnmpConfig().getEnterpriseId();
        return null;
    }

    /**
     * Helper method to search the snmp-config 
     * @param def
     * @return
     */
    private long determineTimeout(final Definition def) {
        final long timeout = SnmpAgentConfig.DEFAULT_TIMEOUT;
        return (def.getTimeout() == 0 ? (getSnmpConfig().getTimeout() == 0 ? timeout : getSnmpConfig().getTimeout()) : def.getTimeout());
    }

    private int determineRetries(final Definition def) {        
        final int retries = SnmpAgentConfig.DEFAULT_RETRIES;
        return (def.getRetry() == 0 ? (getSnmpConfig().getRetry() == 0 ? retries : getSnmpConfig().getRetry()) : def.getRetry());
    }

    /**
     * This method determines the configured SNMP version.
     * the order of operations is:
     * 1st: return a valid requested version
     * 2nd: return a valid version defined in a definition within the snmp-config
     * 3rd: return a valid version in the snmp-config
     * 4th: return the default version
     * 
     * @param def
     * @param requestedSnmpVersion
     * @return
     */
    private int determineVersion(final Definition def, final int requestedSnmpVersion) {

        int version = SnmpAgentConfig.VERSION1;

        String cfgVersion = "v1";
        if (requestedSnmpVersion == VERSION_UNSPECIFIED) {
            if (def.getVersion() == null) {
                if (getSnmpConfig().getVersion() == null) {
                    return version;
                } else {
                    cfgVersion = getSnmpConfig().getVersion();
                }
            } else {
                cfgVersion = def.getVersion();
            }
        } else {
            return requestedSnmpVersion;
        }

        if (cfgVersion.equals("v1")) {
            version = SnmpAgentConfig.VERSION1;
        } else if (cfgVersion.equals("v2c")) {
            version = SnmpAgentConfig.VERSION2C;
        } else if (cfgVersion.equals("v3")) {
            version = SnmpAgentConfig.VERSION3;
        }

        return version;
    }

    /**
     * <p>getSnmpConfig</p>
     *
     * @return a {@link org.opennms.netmgt.config.snmp.SnmpConfig} object.
     */
    public SnmpConfig getSnmpConfig() {
        SnmpPeerFactory.getReadLock().lock();
        try {
            if (m_container == null) {
                return m_config;
            } else {
                return m_container.getObject();
            }
        } finally {
            SnmpPeerFactory.getReadLock().unlock();
        }
    }

    /**
     * Enhancement: Allows specific or ranges to be merged into SNMP configuration
     * with many other attributes.  Uses new classes the wrap Castor-generated code to
     * help with merging, comparing, and optimizing definitions.  Thanks for your
     * initial work on this Gerald.
     *
     * Puts a specific IP address with associated read-community string into
     * the currently loaded snmp-config.xml.
     *
     * @param info a {@link org.opennms.netmgt.config.SnmpEventInfo} object.
     */
    public void define(final SnmpEventInfo info) {
        getWriteLock().lock();
        try {
            final SnmpConfigManager mgr = new SnmpConfigManager(getSnmpConfig());
            mgr.mergeIntoConfig(info.createDef());
        } finally {
            getWriteLock().unlock();
        }
    }

    /**
     * Creates a string containing the XML of the current SnmpConfig
     *
     * @return Marshalled SnmpConfig
     */
    public String getSnmpConfigAsString() {
        String marshalledConfig = null;
        StringWriter writer = null;
        try {
            writer = new StringWriter();
            JaxbUtils.marshal(getSnmpConfig(), writer);
            marshalledConfig = writer.toString();
        } finally {
            IOUtils.closeQuietly(writer);
        }
        return marshalledConfig;
    }
}
<commit_msg>fix init clobbering the already-initialized peer factory<commit_after>/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2012 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <<EMAIL>>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.netmgt.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.InetAddress;
import java.util.concurrent.locks.Lock;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.opennms.core.utils.ByteArrayComparator;
import org.opennms.core.utils.ConfigFileConstants;
import org.opennms.core.utils.FileReloadCallback;
import org.opennms.core.utils.FileReloadContainer;
import org.opennms.core.utils.IPLike;
import org.opennms.core.utils.InetAddressUtils;
import org.opennms.core.utils.LogUtils;
import org.opennms.core.xml.JaxbUtils;
import org.opennms.netmgt.config.snmp.Definition;
import org.opennms.netmgt.config.snmp.Range;
import org.opennms.netmgt.config.snmp.SnmpConfig;
import org.opennms.netmgt.snmp.SnmpAgentConfig;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import com.googlecode.concurentlocks.ReadWriteUpdateLock;
import com.googlecode.concurentlocks.ReentrantReadWriteUpdateLock;

/**
 * This class is the main repository for SNMP configuration information used by
 * the capabilities daemon. When this class is loaded it reads the snmp
 * configuration into memory, and uses the configuration to find the
 * {@link org.opennms.netmgt.snmp.SnmpAgentConfig SnmpAgentConfig} objects for specific
 * addresses. If an address cannot be located in the configuration then a
 * default peer instance is returned to the caller.
 *
 * <strong>Note: </strong>Users of this class should make sure the
 * <em>init()</em> is called before calling any other method to ensure the
 * config is loaded before accessing other convenience methods.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>">Weave</a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class SnmpPeerFactory implements SnmpAgentConfigFactory {
    private static final int DEFAULT_SNMP_PORT = 161;
    private static final ReadWriteUpdateLock m_globalLock = new ReentrantReadWriteUpdateLock();
    private static final Lock m_readLock = m_globalLock.updateLock();
    private static final Lock m_writeLock = m_globalLock.writeLock();

    /**
     * The singleton instance of this factory
     */
    private static SnmpPeerFactory m_singleton = null;

    private static File m_configFile;

    /**
     * The config class loaded from the config file
     */
    private SnmpConfig m_config;

    private static FileReloadContainer<SnmpConfig> m_container;
    private static FileReloadCallback<SnmpConfig> m_callback;

    /**
     * This member is set to true if the configuration file has been loaded.
     */
    private static volatile boolean m_loaded = false;

    private static final int VERSION_UNSPECIFIED = -1;

    /**
     * <p>Constructor for SnmpPeerFactory.</p>
     *
     * @param resource a {@link org.springframework.core.io.Resource} object.
     */
    public SnmpPeerFactory(final Resource resource) {
        SnmpPeerFactory.getWriteLock().lock();

        try {
            final SnmpConfig config = JaxbUtils.unmarshal(SnmpConfig.class, resource);

            try {
                final File file = resource.getFile();
                if (file != null) {
                    m_callback = new FileReloadCallback<SnmpConfig>() {
                        @Override
                        public SnmpConfig reload(final SnmpConfig object, final Resource resource) throws IOException {
                            return JaxbUtils.unmarshal(SnmpConfig.class, resource);
                        }

                    };
                    m_container = new FileReloadContainer<SnmpConfig>(config, resource, m_callback);
                    return;
                }
            } catch (final IOException e) {
                LogUtils.debugf(this, "No file associated with resource %s, skipping reload container initialization.", resource);
            }

            // if we fall through to here, then the file was null, or something else went wrong store the config directly
            m_config = config;
        } finally {
            SnmpPeerFactory.getWriteLock().unlock();
        }
    }

    public static Lock getReadLock() {
        return m_readLock;
    }

    public static Lock getWriteLock() {
        return m_writeLock;
    }

    public static synchronized void init() throws IOException {
        if (!m_loaded) {
            final File cfgFile = getFile();
            LogUtils.debugf(SnmpPeerFactory.class, "init: config file path: %s", cfgFile.getPath());
            final FileSystemResource resource = new FileSystemResource(cfgFile);

            m_singleton = new SnmpPeerFactory(resource);
            m_loaded = true;
        }
    }

    /**
     * Load the config from the default config file and create the singleton instance of this factory.
     *
     * @exception java.io.IOException Thrown if the specified config file cannot be read
     */
    public static synchronized SnmpPeerFactory getInstance() {
        if (!m_loaded) {
            try {
                init();
            } catch (final IOException e) {
                LogUtils.errorf(SnmpPeerFactory.class, e, "Failed to initialize SnmpPeerFactory instance!");
            }
        }
        return m_singleton;
    }

    /**
     * <p>setInstance</p>
     *
     * @param singleton a {@link org.opennms.netmgt.config.SnmpPeerFactory} object.
     */
    public static synchronized void setInstance(final SnmpPeerFactory singleton) {
        m_singleton = singleton;
        m_loaded = true;
    }

    public static synchronized File getFile() throws IOException {
        if (m_configFile == null) {
            setFile(ConfigFileConstants.getFile(ConfigFileConstants.SNMP_CONF_FILE_NAME));
        }
        return m_configFile;
    }

    /**
     * <p>setFile</p>
     *
     * @param configFile a {@link java.io.File} object.
     */
    public static synchronized void setFile(final File configFile) {
        final File oldFile = m_configFile;
        m_configFile = configFile;

        // if the file changed then we need to reload the config
        if (oldFile == null || m_configFile == null || !oldFile.equals(m_configFile)) {
            m_singleton = null;
            m_loaded = false;
        }
    }

    /**
     * Saves the current settings to disk
     *
     * @throws java.io.IOException if any.
     * @throws org.exolab.castor.xml.MarshalException if any.
     * @throws org.exolab.castor.xml.ValidationException if any.
     */
    public void saveCurrent() throws IOException {
        saveToFile(getFile());
    }

    public void saveToFile(final File file) throws UnsupportedEncodingException, FileNotFoundException, IOException {
        // Marshal to a string first, then write the string to the file. This
        // way the original config isn't lost if the XML from the marshal is hosed.
        SnmpPeerFactory.getWriteLock().lock();

        final String marshalledConfig = getSnmpConfigAsString();

        FileOutputStream out = null;
        Writer fileWriter = null;
        try {
            if (marshalledConfig != null) {
                out = new FileOutputStream(file);
                fileWriter = new OutputStreamWriter(out, "UTF-8");
                fileWriter.write(marshalledConfig);
                fileWriter.flush();
                fileWriter.close();
                if (m_container != null) {
                    m_container.reload();
                }
            }
        } finally {
            IOUtils.closeQuietly(fileWriter);
            IOUtils.closeQuietly(out);
            SnmpPeerFactory.getWriteLock().unlock();
        }
    }

    /** {@inheritDoc} */
    public SnmpAgentConfig getAgentConfig(final InetAddress agentAddress) {
        return getAgentConfig(agentAddress, VERSION_UNSPECIFIED);
    }

    private SnmpAgentConfig getAgentConfig(final InetAddress agentInetAddress, final int requestedSnmpVersion) {
        if (getSnmpConfig() == null) {
            final SnmpAgentConfig agentConfig = new SnmpAgentConfig(agentInetAddress);
            if (requestedSnmpVersion == VERSION_UNSPECIFIED) {
                agentConfig.setVersion(SnmpAgentConfig.DEFAULT_VERSION);
            } else {
                agentConfig.setVersion(requestedSnmpVersion);
            }

            return agentConfig;
        }

        final SnmpAgentConfig agentConfig = new SnmpAgentConfig(agentInetAddress);

        // Now set the defaults from the getSnmpConfig()
        setSnmpAgentConfig(agentConfig, new Definition(), requestedSnmpVersion);

        // Attempt to locate the node
        DEFLOOP: for (final Definition def : getSnmpConfig().getDefinitionCollection()) {
            // check the specifics first
            for (final String saddr : def.getSpecificCollection()) {
                try {
                    final InetAddress addr = InetAddressUtils.addr(saddr);
                    if (addr != null && addr.equals(agentConfig.getAddress())) {
                        setSnmpAgentConfig(agentConfig, def, requestedSnmpVersion);
                        break DEFLOOP;
                    }
                } catch (final IllegalArgumentException e) {
                    LogUtils.debugf(this, e, "Error while reading SNMP config <specific> tag: %s", saddr);
                }
            }

            // check the ranges
            //
            final ByteArrayComparator comparator = new ByteArrayComparator();

            for (final Range rng : def.getRangeCollection()) {
                final byte[] addr = agentConfig.getAddress().getAddress();
                final byte[] begin = InetAddressUtils.toIpAddrBytes(rng.getBegin());
                final byte[] end = InetAddressUtils.toIpAddrBytes(rng.getEnd());

                boolean inRange = InetAddressUtils.isInetAddressInRange(addr, begin, end);
                if (comparator.compare(begin, end) <= 0) {
                    inRange = InetAddressUtils.isInetAddressInRange(addr, begin, end);
                } else {
                    LogUtils.warnf(this, "%s has an 'end' that is earlier than its 'beginning'!", rng);
                    inRange = InetAddressUtils.isInetAddressInRange(addr, end, begin);
                }
                if (inRange) {
                    setSnmpAgentConfig(agentConfig, def, requestedSnmpVersion);
                    break DEFLOOP;
                }
            }

            // check the matching ip expressions
            for (final String ipMatch : def.getIpMatchCollection()) {
                if (IPLike.matches(agentInetAddress, ipMatch)) {
                    setSnmpAgentConfig(agentConfig, def, requestedSnmpVersion);
                    break DEFLOOP;
                }
            }

        } // end DEFLOOP
        return agentConfig;
    }

    private void setSnmpAgentConfig(final SnmpAgentConfig agentConfig, final Definition def, final int requestedSnmpVersion) {
        int version = determineVersion(def, requestedSnmpVersion);

        setCommonAttributes(agentConfig, def, version);
        agentConfig.setSecurityLevel(determineSecurityLevel(def));
        agentConfig.setSecurityName(determineSecurityName(def));
        agentConfig.setAuthProtocol(determineAuthProtocol(def));
        agentConfig.setAuthPassPhrase(determineAuthPassPhrase(def));
        agentConfig.setPrivPassPhrase(determinePrivPassPhrase(def));
        agentConfig.setPrivProtocol(determinePrivProtocol(def));
        agentConfig.setReadCommunity(determineReadCommunity(def));
        agentConfig.setWriteCommunity(determineWriteCommunity(def));
        agentConfig.setEngineId(determineEngineId(def));
        agentConfig.setEnterpriseId(determineEnterpriseId(def));
        agentConfig.setContextEngineId(determineContextEngineId(def));
        agentConfig.setContextName(determineContextName(def));
    }

    /**
     * This is a helper method to set all the common attributes in the agentConfig.
     * 
     * @param agentConfig
     * @param def
     * @param version
     */
    private void setCommonAttributes(final SnmpAgentConfig agentConfig, final Definition def, final int version) {
        agentConfig.setVersion(version);
        agentConfig.setPort(determinePort(def));
        agentConfig.setRetries(determineRetries(def));
        agentConfig.setTimeout((int)determineTimeout(def));
        agentConfig.setMaxRequestSize(determineMaxRequestSize(def));
        agentConfig.setMaxVarsPerPdu(determineMaxVarsPerPdu(def));
        agentConfig.setMaxRepetitions(determineMaxRepetitions(def));
        InetAddress proxyHost = determineProxyHost(def);

        if (proxyHost != null) {
            agentConfig.setProxyFor(agentConfig.getAddress());
            agentConfig.setAddress(determineProxyHost(def));
        }
    }

    private int determineMaxRepetitions(final Definition def) {
        return (!def.hasMaxRepetitions() ? 
            (!getSnmpConfig().hasMaxRepetitions() ?
                SnmpAgentConfig.DEFAULT_MAX_REPETITIONS : getSnmpConfig().getMaxRepetitions()) : def.getMaxRepetitions());
    }

    private InetAddress determineProxyHost(final Definition def) {
        InetAddress inetAddr = null;
        final String address = def.getProxyHost() == null ? (getSnmpConfig().getProxyHost() == null ? null : getSnmpConfig().getProxyHost()) : def.getProxyHost();
        if (address != null) {
            try {
                inetAddr =  InetAddressUtils.addr(address);
            } catch (final IllegalArgumentException e) {
                LogUtils.debugf(this, e, "Error while reading SNMP config proxy host: %s", address);
            }
        }
        return inetAddr;
    }

    private int determineMaxVarsPerPdu(final Definition def) {
        return (!def.hasMaxVarsPerPdu() ? 
            (!getSnmpConfig().hasMaxVarsPerPdu() ?
                SnmpAgentConfig.DEFAULT_MAX_VARS_PER_PDU : getSnmpConfig().getMaxVarsPerPdu()) : def.getMaxVarsPerPdu());
    }
    /**
     * Helper method to search the snmp-config for the appropriate read
     * community string.
     * @param def
     * @return
     */
    private String determineReadCommunity(final Definition def) {
        return (def.getReadCommunity() == null ? (getSnmpConfig().getReadCommunity() == null ? SnmpAgentConfig.DEFAULT_READ_COMMUNITY :getSnmpConfig().getReadCommunity()) : def.getReadCommunity());
    }

    /**
     * Helper method to search the snmp-config for the appropriate write
     * community string.
     * @param def
     * @return
     */
    private String determineWriteCommunity(final Definition def) {
        return (def.getWriteCommunity() == null ? (getSnmpConfig().getWriteCommunity() == null ? SnmpAgentConfig.DEFAULT_WRITE_COMMUNITY :getSnmpConfig().getWriteCommunity()) : def.getWriteCommunity());
    }

    /**
     * Helper method to search the snmp-config for the appropriate maximum
     * request size.  The default is the minimum necessary for a request.
     * @param def
     * @return
     */
    private int determineMaxRequestSize(final Definition def) {
        return (!def.hasMaxRequestSize() ? (!getSnmpConfig().hasMaxRequestSize() ? SnmpAgentConfig.DEFAULT_MAX_REQUEST_SIZE : getSnmpConfig().getMaxRequestSize()) : def.getMaxRequestSize());
    }

    /**
     * Helper method to find a security name to use in the snmp-config.  If v3 has
     * been specified and one can't be found, then a default is used for this
     * is a required option for v3 operations.
     * @param def
     * @return
     */
    private String determineSecurityName(final Definition def) {
        final String securityName = (def.getSecurityName() == null ? getSnmpConfig().getSecurityName() : def.getSecurityName() );
        if (securityName == null) {
            return SnmpAgentConfig.DEFAULT_SECURITY_NAME;
        }
        return securityName;
    }

    /**
     * Helper method to find a security name to use in the snmp-config.  If v3 has
     * been specified and one can't be found, then a default is used for this
     * is a required option for v3 operations.
     * @param def
     * @return
     */
    private String determineAuthProtocol(final Definition def) {
        final String authProtocol = (def.getAuthProtocol() == null ? getSnmpConfig().getAuthProtocol() : def.getAuthProtocol());
        if (authProtocol == null && determineAuthPassPhrase(def) != null) {
            return SnmpAgentConfig.DEFAULT_AUTH_PROTOCOL;
        }
        return authProtocol;
    }

    /**
     * Helper method to find a authentication passphrase to use from the snmp-config.  If v3 has
     * been specified and one can't be found, then a default is used for this
     * is a required option for v3 operations.
     * @param def
     * @return
     */
    private String determineAuthPassPhrase(final Definition def) {
        final String authPassPhrase = (def.getAuthPassphrase() == null ? getSnmpConfig().getAuthPassphrase() : def.getAuthPassphrase());
        // Forcing a default is wrong, because if it is not explicitly defined, probably it means that it should not be used, and SNMP4J expect null for optional parameters.
        /*
        if (authPassPhrase == null) {
            return SnmpAgentConfig.DEFAULT_AUTH_PASS_PHRASE;
        }
         */
        return authPassPhrase;
    }

    /**
     * Helper method to find a privacy passphrase to use from the snmp-config.  If v3 has
     * been specified and one can't be found, then a default is used for this
     * is a required option for v3 operations.
     * @param def
     * @return
     */
    private String determinePrivPassPhrase(final Definition def) {
        final String privPassPhrase = (def.getPrivacyPassphrase() == null ? getSnmpConfig().getPrivacyPassphrase() : def.getPrivacyPassphrase());
        // Forcing a default is wrong, because if it is not explicitly defined, probably it means that it should not be used, and SNMP4J expect null for optional parameters.
        /*
        if (privPassPhrase == null) {
            return SnmpAgentConfig.DEFAULT_PRIV_PASS_PHRASE;
        }
         */
        return privPassPhrase;
    }

    /**
     * Helper method to find a privacy protocol to use from the snmp-config.  If v3 has
     * been specified and one can't be found, then a default is used for this
     * is a required option for v3 operations.
     * @param def
     * @return
     */
    private String determinePrivProtocol(final Definition def) {
        final String authPrivProtocol = (def.getPrivacyProtocol() == null ? getSnmpConfig().getPrivacyProtocol() : def.getPrivacyProtocol());
        if (authPrivProtocol == null && determinePrivPassPhrase(def) != null) {
            return SnmpAgentConfig.DEFAULT_PRIV_PROTOCOL;
        }
        return authPrivProtocol;
    }

    /**
     * Helper method to set the security level in v3 operations.  The default is
     * noAuthNoPriv if there is no authentication passphrase.  From there, if
     * there is a privacy passphrase supplied, then the security level is set to
     * authPriv else it falls out to authNoPriv.  There are only these 3 possible
     * security levels.
     * default 
     * @param def
     * @return
     */
    private int determineSecurityLevel(final Definition def) {

        // use the def security level first
        if (def.hasSecurityLevel()) {
            return def.getSecurityLevel();
        }

        // use a configured default security level next
        if (getSnmpConfig().hasSecurityLevel()) {
            return getSnmpConfig().getSecurityLevel();
        }

        // if no security level configuration exists use
        int securityLevel = SnmpAgentConfig.NOAUTH_NOPRIV;

        final String authPassPhrase = (StringUtils.isBlank(def.getAuthPassphrase()) ? getSnmpConfig().getAuthPassphrase() : def.getAuthPassphrase());
        final String privPassPhrase = (StringUtils.isBlank(def.getPrivacyPassphrase()) ? getSnmpConfig().getPrivacyPassphrase() : def.getPrivacyPassphrase());

        if (authPassPhrase == null) {
            securityLevel = SnmpAgentConfig.NOAUTH_NOPRIV;
        } else {
            if (privPassPhrase == null) {
                securityLevel = SnmpAgentConfig.AUTH_NOPRIV;
            } else {
                securityLevel = SnmpAgentConfig.AUTH_PRIV;
            }
        }

        return securityLevel;
    }

    /**
     * Helper method to search the snmp-config for a port
     * @param def
     * @return
     */
    private int determinePort(final Definition def) {
        return (def.getPort() == 0 ? (getSnmpConfig().getPort() == 0 ? DEFAULT_SNMP_PORT : getSnmpConfig().getPort()) : def.getPort());
    }

    /**
     * Helper method to search the snmp-config for a engineId
     * @param def
     * @return
     */
    private String determineEngineId(Definition def) {
        if (def.getEngineId() != null) return def.getEngineId();
        if (getSnmpConfig().getEngineId() != null) return getSnmpConfig().getEngineId();
        return null;
    }

    /**
     * Helper method to search the snmp-config for a contextEngineId
     * @param def
     * @return 
     */
    private String determineContextEngineId(Definition def) {
        if (def.getContextEngineId() != null) return def.getContextEngineId();
        if (getSnmpConfig().getContextEngineId() != null) return getSnmpConfig().getContextEngineId();
        return null;
    }

    /**
     * Helper method to search the snmp-config for a contextName
     * @param def
     * @return 
     */
    private String determineContextName(Definition def) {
        if (def.getContextName() != null) return def.getContextName();
        if (getSnmpConfig().getContextName() != null) return getSnmpConfig().getContextName();
        return null;
    }

    /**
     * Helper method to search the snmp-config for a enterpriseId
     * @param def
     * @return 
     */
    private String determineEnterpriseId(Definition def) {
        if (def.getEnterpriseId() != null) return def.getEnterpriseId();
        if (getSnmpConfig().getEnterpriseId() != null) return getSnmpConfig().getEnterpriseId();
        return null;
    }

    /**
     * Helper method to search the snmp-config 
     * @param def
     * @return
     */
    private long determineTimeout(final Definition def) {
        final long timeout = SnmpAgentConfig.DEFAULT_TIMEOUT;
        return (def.getTimeout() == 0 ? (getSnmpConfig().getTimeout() == 0 ? timeout : getSnmpConfig().getTimeout()) : def.getTimeout());
    }

    private int determineRetries(final Definition def) {        
        final int retries = SnmpAgentConfig.DEFAULT_RETRIES;
        return (def.getRetry() == 0 ? (getSnmpConfig().getRetry() == 0 ? retries : getSnmpConfig().getRetry()) : def.getRetry());
    }

    /**
     * This method determines the configured SNMP version.
     * the order of operations is:
     * 1st: return a valid requested version
     * 2nd: return a valid version defined in a definition within the snmp-config
     * 3rd: return a valid version in the snmp-config
     * 4th: return the default version
     * 
     * @param def
     * @param requestedSnmpVersion
     * @return
     */
    private int determineVersion(final Definition def, final int requestedSnmpVersion) {

        int version = SnmpAgentConfig.VERSION1;

        String cfgVersion = "v1";
        if (requestedSnmpVersion == VERSION_UNSPECIFIED) {
            if (def.getVersion() == null) {
                if (getSnmpConfig().getVersion() == null) {
                    return version;
                } else {
                    cfgVersion = getSnmpConfig().getVersion();
                }
            } else {
                cfgVersion = def.getVersion();
            }
        } else {
            return requestedSnmpVersion;
        }

        if (cfgVersion.equals("v1")) {
            version = SnmpAgentConfig.VERSION1;
        } else if (cfgVersion.equals("v2c")) {
            version = SnmpAgentConfig.VERSION2C;
        } else if (cfgVersion.equals("v3")) {
            version = SnmpAgentConfig.VERSION3;
        }

        return version;
    }

    /**
     * <p>getSnmpConfig</p>
     *
     * @return a {@link org.opennms.netmgt.config.snmp.SnmpConfig} object.
     */
    public SnmpConfig getSnmpConfig() {
        SnmpPeerFactory.getReadLock().lock();
        try {
            if (m_container == null) {
                return m_config;
            } else {
                return m_container.getObject();
            }
        } finally {
            SnmpPeerFactory.getReadLock().unlock();
        }
    }

    /**
     * Enhancement: Allows specific or ranges to be merged into SNMP configuration
     * with many other attributes.  Uses new classes the wrap Castor-generated code to
     * help with merging, comparing, and optimizing definitions.  Thanks for your
     * initial work on this Gerald.
     *
     * Puts a specific IP address with associated read-community string into
     * the currently loaded snmp-config.xml.
     *
     * @param info a {@link org.opennms.netmgt.config.SnmpEventInfo} object.
     */
    public void define(final SnmpEventInfo info) {
        getWriteLock().lock();
        try {
            final SnmpConfigManager mgr = new SnmpConfigManager(getSnmpConfig());
            mgr.mergeIntoConfig(info.createDef());
        } finally {
            getWriteLock().unlock();
        }
    }

    /**
     * Creates a string containing the XML of the current SnmpConfig
     *
     * @return Marshalled SnmpConfig
     */
    public String getSnmpConfigAsString() {
        String marshalledConfig = null;
        StringWriter writer = null;
        try {
            writer = new StringWriter();
            JaxbUtils.marshal(getSnmpConfig(), writer);
            marshalledConfig = writer.toString();
        } finally {
            IOUtils.closeQuietly(writer);
        }
        return marshalledConfig;
    }
}

{'fraction_non_alphanumeric': 0.09614258527302005, 'fraction_numerical': 0.0018664909969257795, 'mean_word_length': 2.5135013501350136, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6649817', 'n_tokens_mistral': 14896, 'n_tokens_neox': 14484, 'n_words': 4040}