<commit_before>/*
 * This file is part of the OpenNMS(R) Application.
 *
 * OpenNMS(R) is Copyright (C) 2009 The OpenNMS Group, Inc.  All rights reserved.
 * OpenNMS(R) is a derivative work, containing both original code, included code and modified
 * code that was published under the GNU General Public License. Copyrights for modified
 * and included code are below.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * Modifications:
 * 
 * Created: January 22, 2009
 *
 * Copyright (C) 2009 The OpenNMS Group, Inc.  All rights reserved.
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * For more information contact:
 *      OpenNMS Licensing       <<EMAIL>>
 *      http://www.opennms.org/
 *      http://www.opennms.com/
 */
package org.opennms.web.rest;

import java.text.ParseException;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;

import org.joda.time.Duration;
import org.opennms.netmgt.dao.NodeDao;
import org.opennms.netmgt.model.OnmsNodeList;
import org.opennms.netmgt.provision.persist.ForeignSourceRepository;
import org.opennms.netmgt.provision.persist.ForeignSourceService;
import org.opennms.netmgt.provision.persist.StringIntervalPropertyEditor;
import org.opennms.netmgt.provision.persist.foreignsource.DetectorCollection;
import org.opennms.netmgt.provision.persist.foreignsource.DetectorWrapper;
import org.opennms.netmgt.provision.persist.foreignsource.ForeignSource;
import org.opennms.netmgt.provision.persist.foreignsource.ForeignSourceCollection;
import org.opennms.netmgt.provision.persist.foreignsource.PluginConfig;
import org.opennms.netmgt.provision.persist.foreignsource.PolicyCollection;
import org.opennms.netmgt.provision.persist.foreignsource.PolicyWrapper;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sun.jersey.spi.resource.PerRequest;

/**
<p>RESTful service to the OpenNMS Provisioning Foreign Source definitions.  Foreign source
definitions are used to control the scanning (service detection) of services for SLA monitoring
as well as the data collection settings for physical interfaces (resources).</p>
<p>This API supports CRUD operations for managing the Provisioner's foreign source definitions. Foreign
source definitions are first POSTed into a bin called pending and <b>must</b> be deployed before
they are used with they're correspondingly named requisition (provisioning group). 
<ul>
<li>GET/PUT/POST pending and deployed foreign sources</li>
<li>GET pending and deployed count</li>
</ul>
</p>
<p>Example 1: Create a new foreign source<i>Note: The foreign-source attribute typically has a 1 to 1 
relationship to a provisioning group (a.k.a. requisition).  The relationship is only 
implied by name and it is a best practice to use the same name for all three.  If a requisition exists with 
the same name as a foreign source, it will be used during the provisioning (import) operations in lieu
of the default foreign source.</i></p>
<pre>
curl -X POST \
     -H "Content-Type: application/xml" \
     -d &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
         &lt;foreign-source date-stamp="2009-03-07T20:22:45.625-05:00" name="Cisco"
           xmlns:ns2="http://xmlns.opennms.org/xsd/config/model-import" 
           xmlns="http://xmlns.opennms.org/xsd/config/foreign-source"&gt;
           &lt;scan-interval&gt;1d&lt;/scan-interval&gt;
           &lt;detectors&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.dhcp.DhcpDetector" name="DHCP"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.datagram.DnsDetector" name="DNS"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.simple.FtpDetector" name="FTP"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.simple.HttpDetector" name="HTTP"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.simple.HttpsDetector" name="HTTPS"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.icmp.IcmpDetector" name="ICMP"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.simple.LdapDetector" name="LDAP"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.radius.RadiusDetector" name="Radius"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.snmp.SnmpDetector" name="SNMP"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.ssh.SshDetector" name="SSH"/&gt;
           &lt;/detectors&gt;
           &lt;policies&gt;
             &lt;policy class="org.opennms.netmgt.provision.persist.policies.MatchingInterfacePolicy" name="policy1"&gt;
               &lt;parameter value="~10\.*\.*\.*" key="ipAddress"/&gt;
             &lt;/policy&gt;
           &lt;/policies&gt;
         &lt;/foreign-source&gt; \
     -u admin:admin \
     http://localhost:8980/opennms/rest/foreignSources/pending
</pre>
<p>Example 2: Query SNMP community string.</p>
<pre>
curl -X GET \
     -H "Content-Type: application/xml" \
     -u admin:admin \
        http://localhost:8980/opennms/rest/foreignSources/deployed \
        2>/dev/null \
        |xmllint --format -</pre>
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
@Component
@PerRequest
@Scope("prototype")
@Path("foreignSources")
public class ForeignSourceRestService extends OnmsRestService {
    
    @Autowired
    @Qualifier("pending")
    private ForeignSourceRepository m_pendingForeignSourceRepository;
    
    @Autowired
    @Qualifier("deployed")
    private ForeignSourceRepository m_deployedForeignSourceRepository;

    @Autowired
    ForeignSourceService m_foreignSourceService;
    
    @Autowired
    private NodeDao m_nodeDao;
    
    @Context
    UriInfo m_uriInfo;

    @Context
    HttpHeaders m_headers;

    @Context
    SecurityContext m_securityContext;

    @GET
    @Path("default")
    @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public ForeignSource getDefaultForeignSource() throws ParseException {
        return m_deployedForeignSourceRepository.getDefaultForeignSource();
    }

    /**
     * Returns all the deployed foreign sources
     * 
     * @return Collection of OnmsForeignSources (ready to be XML-ified)
     * @throws ParseException
     */<commit_msg>whups, didn't mean to commit that line<commit_after>/*
 * This file is part of the OpenNMS(R) Application.
 *
 * OpenNMS(R) is Copyright (C) 2009 The OpenNMS Group, Inc.  All rights reserved.
 * OpenNMS(R) is a derivative work, containing both original code, included code and modified
 * code that was published under the GNU General Public License. Copyrights for modified
 * and included code are below.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * Modifications:
 * 
 * Created: January 22, 2009
 *
 * Copyright (C) 2009 The OpenNMS Group, Inc.  All rights reserved.
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * For more information contact:
 *      OpenNMS Licensing       <<EMAIL>>
 *      http://www.opennms.org/
 *      http://www.opennms.com/
 */
package org.opennms.web.rest;

import java.text.ParseException;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;

import org.joda.time.Duration;
import org.opennms.netmgt.dao.NodeDao;
import org.opennms.netmgt.model.OnmsNodeList;
import org.opennms.netmgt.provision.persist.ForeignSourceRepository;
import org.opennms.netmgt.provision.persist.StringIntervalPropertyEditor;
import org.opennms.netmgt.provision.persist.foreignsource.DetectorCollection;
import org.opennms.netmgt.provision.persist.foreignsource.DetectorWrapper;
import org.opennms.netmgt.provision.persist.foreignsource.ForeignSource;
import org.opennms.netmgt.provision.persist.foreignsource.ForeignSourceCollection;
import org.opennms.netmgt.provision.persist.foreignsource.PluginConfig;
import org.opennms.netmgt.provision.persist.foreignsource.PolicyCollection;
import org.opennms.netmgt.provision.persist.foreignsource.PolicyWrapper;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sun.jersey.spi.resource.PerRequest;

/**
<p>RESTful service to the OpenNMS Provisioning Foreign Source definitions.  Foreign source
definitions are used to control the scanning (service detection) of services for SLA monitoring
as well as the data collection settings for physical interfaces (resources).</p>
<p>This API supports CRUD operations for managing the Provisioner's foreign source definitions. Foreign
source definitions are first POSTed into a bin called pending and <b>must</b> be deployed before
they are used with they're correspondingly named requisition (provisioning group). 
<ul>
<li>GET/PUT/POST pending and deployed foreign sources</li>
<li>GET pending and deployed count</li>
</ul>
</p>
<p>Example 1: Create a new foreign source<i>Note: The foreign-source attribute typically has a 1 to 1 
relationship to a provisioning group (a.k.a. requisition).  The relationship is only 
implied by name and it is a best practice to use the same name for all three.  If a requisition exists with 
the same name as a foreign source, it will be used during the provisioning (import) operations in lieu
of the default foreign source.</i></p>
<pre>
curl -X POST \
     -H "Content-Type: application/xml" \
     -d &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
         &lt;foreign-source date-stamp="2009-03-07T20:22:45.625-05:00" name="Cisco"
           xmlns:ns2="http://xmlns.opennms.org/xsd/config/model-import" 
           xmlns="http://xmlns.opennms.org/xsd/config/foreign-source"&gt;
           &lt;scan-interval&gt;1d&lt;/scan-interval&gt;
           &lt;detectors&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.dhcp.DhcpDetector" name="DHCP"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.datagram.DnsDetector" name="DNS"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.simple.FtpDetector" name="FTP"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.simple.HttpDetector" name="HTTP"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.simple.HttpsDetector" name="HTTPS"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.icmp.IcmpDetector" name="ICMP"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.simple.LdapDetector" name="LDAP"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.radius.RadiusDetector" name="Radius"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.snmp.SnmpDetector" name="SNMP"/&gt;
             &lt;detector class="org.opennms.netmgt.provision.detector.ssh.SshDetector" name="SSH"/&gt;
           &lt;/detectors&gt;
           &lt;policies&gt;
             &lt;policy class="org.opennms.netmgt.provision.persist.policies.MatchingInterfacePolicy" name="policy1"&gt;
               &lt;parameter value="~10\.*\.*\.*" key="ipAddress"/&gt;
             &lt;/policy&gt;
           &lt;/policies&gt;
         &lt;/foreign-source&gt; \
     -u admin:admin \
     http://localhost:8980/opennms/rest/foreignSources/pending
</pre>
<p>Example 2: Query SNMP community string.</p>
<pre>
curl -X GET \
     -H "Content-Type: application/xml" \
     -u admin:admin \
        http://localhost:8980/opennms/rest/foreignSources/deployed \
        2>/dev/null \
        |xmllint --format -</pre>
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
@Component
@PerRequest
@Scope("prototype")
@Path("foreignSources")
public class ForeignSourceRestService extends OnmsRestService {
    
    @Autowired
    @Qualifier("pending")
    private ForeignSourceRepository m_pendingForeignSourceRepository;
    
    @Autowired
    @Qualifier("deployed")
    private ForeignSourceRepository m_deployedForeignSourceRepository;

    @Autowired
    private NodeDao m_nodeDao;
    
    @Context
    UriInfo m_uriInfo;

    @Context
    HttpHeaders m_headers;

    @Context
    SecurityContext m_securityContext;

    @GET
    @Path("default")
    @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public ForeignSource getDefaultForeignSource() throws ParseException {
        return m_deployedForeignSourceRepository.getDefaultForeignSource();
    }

    /**
     * Returns all the deployed foreign sources
     * 
     * @return Collection of OnmsForeignSources (ready to be XML-ified)
     * @throws ParseException
     */
{'fraction_non_alphanumeric': 0.10936476751800916, 'fraction_numerical': 0.009299279633267845, 'mean_word_length': 3.938874514877102, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 2, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 16}, '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': '1376576', 'n_tokens_mistral': 4871, 'n_tokens_neox': 4704, 'n_words': 1171}