Search in sources :

Example 6 with IncludeUrl

use of org.opennms.netmgt.config.discovery.IncludeUrl in project opennms by OpenNMS.

the class ActionDiscoveryServlet method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    LOG.info("Loading Discovery configuration.");
    HttpSession sess = request.getSession(true);
    DiscoveryConfiguration config = (DiscoveryConfiguration) sess.getAttribute(ATTRIBUTE_DISCOVERY_CONFIGURATION);
    if (config == null) {
        config = getDiscoveryConfig();
        sess.setAttribute(ATTRIBUTE_DISCOVERY_CONFIGURATION, config);
    }
    //Update general settings from the incoming request parameters
    config = GeneralSettingsLoader.load(request, config);
    String action = request.getParameter("action");
    LOG.debug("action: {}", action);
    //add a Specific
    if (action.equals(addSpecificAction)) {
        LOG.debug("Adding Specific");
        String ipAddr = request.getParameter("specificipaddress");
        String timeout = request.getParameter("specifictimeout");
        String retries = request.getParameter("specificretries");
        String foreignSource = request.getParameter("specificforeignsource");
        String location = request.getParameter("specificlocation");
        Specific newSpecific = new Specific();
        newSpecific.setAddress(ipAddr);
        if (timeout != null && !"".equals(timeout.trim()) && !timeout.equals(String.valueOf(config.getTimeout().orElse(null)))) {
            newSpecific.setTimeout(WebSecurityUtils.safeParseLong(timeout));
        }
        if (retries != null && !"".equals(retries.trim()) && !retries.equals(String.valueOf(config.getRetries().orElse(null)))) {
            newSpecific.setRetries(WebSecurityUtils.safeParseInt(retries));
        }
        if (foreignSource != null && !"".equals(foreignSource.trim()) && !foreignSource.equals(config.getForeignSource().orElse(null))) {
            newSpecific.setForeignSource(foreignSource);
        }
        if (location != null && !"".equals(location.trim()) && !location.equals(config.getLocation().orElse(MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID))) {
            newSpecific.setLocation(location);
        }
        config.addSpecific(newSpecific);
    }
    //remove 'Specific' from configuration
    if (action.equals(removeSpecificAction)) {
        LOG.debug("Removing Specific");
        String specificIndex = request.getParameter("index");
        int index = WebSecurityUtils.safeParseInt(specificIndex);
        final int index1 = index;
        Specific spec = config.getSpecifics().get(index1);
        boolean result = config.removeSpecific(spec);
        LOG.debug("Removing Specific result = {}", result);
    }
    //add an 'Include Range'
    if (action.equals(addIncludeRangeAction)) {
        LOG.debug("Adding Include Range");
        String ipAddrBase = request.getParameter("irbase");
        String ipAddrEnd = request.getParameter("irend");
        String timeout = request.getParameter("irtimeout");
        String retries = request.getParameter("irretries");
        String foreignSource = request.getParameter("irforeignsource");
        String location = request.getParameter("irlocation");
        IncludeRange newIR = new IncludeRange();
        newIR.setBegin(ipAddrBase);
        newIR.setEnd(ipAddrEnd);
        if (timeout != null && !"".equals(timeout.trim()) && !timeout.equals(String.valueOf(config.getTimeout().orElse(null)))) {
            newIR.setTimeout(WebSecurityUtils.safeParseLong(timeout));
        }
        if (retries != null && !"".equals(retries.trim()) && !retries.equals(String.valueOf(config.getRetries().orElse(null)))) {
            newIR.setRetries(WebSecurityUtils.safeParseInt(retries));
        }
        if (foreignSource != null && !"".equals(foreignSource.trim()) && !foreignSource.equals(config.getForeignSource().orElse(null))) {
            newIR.setForeignSource(foreignSource);
        }
        if (location != null && !"".equals(location.trim()) && !location.equals(config.getLocation().orElse(MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID))) {
            newIR.setLocation(location);
        }
        config.addIncludeRange(newIR);
    }
    //remove 'Include Range' from configuration
    if (action.equals(removeIncludeRangeAction)) {
        LOG.debug("Removing Include Range");
        String specificIndex = request.getParameter("index");
        int index = WebSecurityUtils.safeParseInt(specificIndex);
        final int index1 = index;
        IncludeRange ir = config.getIncludeRanges().get(index1);
        boolean result = config.removeIncludeRange(ir);
        LOG.debug("Removing Include Range result = {}", result);
    }
    //add an 'Include URL'
    if (action.equals(addIncludeUrlAction)) {
        LOG.debug("Adding Include URL");
        String url = request.getParameter("iuurl");
        String timeout = request.getParameter("iutimeout");
        String retries = request.getParameter("iuretries");
        String foreignSource = request.getParameter("iuforeignsource");
        String location = request.getParameter("iulocation");
        IncludeUrl iu = new IncludeUrl();
        iu.setUrl(url);
        if (timeout != null && !"".equals(timeout.trim()) && !timeout.equals(String.valueOf(config.getTimeout().orElse(null)))) {
            iu.setTimeout(WebSecurityUtils.safeParseLong(timeout));
        }
        if (retries != null && !"".equals(retries.trim()) && !retries.equals(String.valueOf(config.getRetries().orElse(null)))) {
            iu.setRetries(WebSecurityUtils.safeParseInt(retries));
        }
        if (foreignSource != null && !"".equals(foreignSource.trim()) && !foreignSource.equals(config.getForeignSource().orElse(null))) {
            iu.setForeignSource(foreignSource);
        }
        if (location != null && !"".equals(location.trim()) && !location.equals(config.getLocation().orElse(MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID))) {
            iu.setLocation(location);
        }
        config.addIncludeUrl(iu);
    }
    //remove 'Include URL' from configuration
    if (action.equals(removeIncludeUrlAction)) {
        LOG.debug("Removing Include URL");
        String specificIndex = request.getParameter("index");
        int index = WebSecurityUtils.safeParseInt(specificIndex);
        final int index1 = index;
        IncludeUrl iu = config.getIncludeUrls().get(index1);
        boolean result = config.removeIncludeUrl(iu);
        LOG.debug("Removing Include URL result = {}", result);
    }
    //add an 'Exclude Range'
    if (action.equals(addExcludeRangeAction)) {
        LOG.debug("Adding Exclude Range");
        String ipAddrBegin = request.getParameter("erbegin");
        String ipAddrEnd = request.getParameter("erend");
        ExcludeRange newER = new ExcludeRange();
        newER.setBegin(ipAddrBegin);
        newER.setEnd(ipAddrEnd);
        config.addExcludeRange(newER);
    }
    //remove 'Exclude Range' from configuration
    if (action.equals(removeExcludeRangeAction)) {
        LOG.debug("Removing Exclude Range");
        String specificIndex = request.getParameter("index");
        int index = WebSecurityUtils.safeParseInt(specificIndex);
        final int index1 = index;
        ExcludeRange er = config.getExcludeRanges().get(index1);
        boolean result = config.removeExcludeRange(er);
        LOG.debug("Removing Exclude Range result = {}", result);
    }
    //save configuration and restart discovery service
    if (action.equals(saveAndRestartAction)) {
        DiscoveryConfigFactory dcf = null;
        try {
            StringWriter configString = new StringWriter();
            JaxbUtils.marshal(config, configString);
            LOG.debug(configString.toString().trim());
            dcf = DiscoveryConfigFactory.getInstance();
            dcf.saveConfiguration(config);
        } catch (Throwable ex) {
            LOG.error("Error while saving configuration. {}", ex);
            throw new ServletException(ex);
        }
        EventProxy proxy = null;
        try {
            proxy = Util.createEventProxy();
        } catch (Throwable me) {
            LOG.error(me.getMessage());
        }
        EventBuilder bldr = new EventBuilder(EventConstants.DISCOVERYCONFIG_CHANGED_EVENT_UEI, "ActionDiscoveryServlet");
        bldr.setHost("host");
        try {
            proxy.send(bldr.getEvent());
        } catch (Throwable me) {
            LOG.error(me.getMessage());
        }
        LOG.info("Restart Discovery requested!");
        sess.removeAttribute(ATTRIBUTE_DISCOVERY_CONFIGURATION);
        response.sendRedirect(Util.calculateUrlBase(request, "admin/discovery/config-done.jsp"));
        return;
    }
    sess.setAttribute(ATTRIBUTE_DISCOVERY_CONFIGURATION, config);
    RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/admin/discovery/edit-config.jsp");
    dispatcher.forward(request, response);
}
Also used : IncludeRange(org.opennms.netmgt.config.discovery.IncludeRange) IncludeUrl(org.opennms.netmgt.config.discovery.IncludeUrl) HttpSession(javax.servlet.http.HttpSession) DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) Specific(org.opennms.netmgt.config.discovery.Specific) RequestDispatcher(javax.servlet.RequestDispatcher) ServletException(javax.servlet.ServletException) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) StringWriter(java.io.StringWriter) DiscoveryConfigFactory(org.opennms.netmgt.config.DiscoveryConfigFactory) ExcludeRange(org.opennms.netmgt.config.discovery.ExcludeRange) EventProxy(org.opennms.netmgt.events.api.EventProxy)

Example 7 with IncludeUrl

use of org.opennms.netmgt.config.discovery.IncludeUrl in project opennms by OpenNMS.

the class DiscoveryRestService method getDiscoveryConfig.

private DiscoveryConfiguration getDiscoveryConfig(DiscoveryConfigurationDTO discoveryConfigurationDTO) {
    DiscoveryConfiguration discoveryConfiguration = new DiscoveryConfiguration();
    discoveryConfiguration.setTimeout(discoveryConfigurationDTO.getTimeout());
    discoveryConfiguration.setRetries(discoveryConfigurationDTO.getRetries());
    discoveryConfiguration.setForeignSource(discoveryConfigurationDTO.getForeignSource());
    discoveryConfiguration.setLocation(discoveryConfigurationDTO.getLocation());
    discoveryConfiguration.setChunkSize(discoveryConfigurationDTO.getChunkSize());
    for (DiscoveryConfigurationDTO.SpecificDTO specificDTO : discoveryConfigurationDTO.getSpecificDTOList()) {
        Specific specific = new Specific();
        specific.setAddress(specificDTO.getContent());
        specific.setTimeout(specificDTO.getTimeout());
        specific.setRetries(specificDTO.getRetries());
        specific.setForeignSource(specificDTO.getForeignSource());
        specific.setLocation(specificDTO.getLocation());
        discoveryConfiguration.addSpecific(specific);
    }
    for (DiscoveryConfigurationDTO.IncludeUrlDTO includeUrlDTO : discoveryConfigurationDTO.getIncludeUrlDTOList()) {
        IncludeUrl includeUrl = new IncludeUrl();
        includeUrl.setUrl(includeUrlDTO.getContent());
        includeUrl.setTimeout(includeUrlDTO.getTimeout());
        includeUrl.setRetries(includeUrlDTO.getRetries());
        includeUrl.setForeignSource(includeUrlDTO.getForeignSource());
        includeUrl.setLocation(includeUrlDTO.getLocation());
        discoveryConfiguration.addIncludeUrl(includeUrl);
    }
    for (DiscoveryConfigurationDTO.IncludeRangeDTO includeRangeDTO : discoveryConfigurationDTO.getIncludeRangeDTOList()) {
        IncludeRange includeRange = new IncludeRange();
        includeRange.setBegin(includeRangeDTO.getBegin());
        includeRange.setEnd(includeRangeDTO.getEnd());
        includeRange.setTimeout(includeRangeDTO.getTimeout());
        includeRange.setRetries(includeRangeDTO.getRetries());
        includeRange.setForeignSource(includeRangeDTO.getForeignSource());
        includeRange.setLocation(includeRangeDTO.getLocation());
        discoveryConfiguration.addIncludeRange(includeRange);
    }
    for (DiscoveryConfigurationDTO.ExcludeRangeDTO excludeRangeDTO : discoveryConfigurationDTO.getExcludeRangeDTOList()) {
        ExcludeRange excludeRange = new ExcludeRange();
        excludeRange.setBegin(excludeRangeDTO.getBegin());
        excludeRange.setEnd(excludeRangeDTO.getEnd());
        discoveryConfiguration.addExcludeRange(excludeRange);
    }
    return discoveryConfiguration;
}
Also used : IncludeRange(org.opennms.netmgt.config.discovery.IncludeRange) IncludeUrl(org.opennms.netmgt.config.discovery.IncludeUrl) DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) Specific(org.opennms.netmgt.config.discovery.Specific) ExcludeRange(org.opennms.netmgt.config.discovery.ExcludeRange)

Aggregations

IncludeUrl (org.opennms.netmgt.config.discovery.IncludeUrl)7 IncludeRange (org.opennms.netmgt.config.discovery.IncludeRange)6 Specific (org.opennms.netmgt.config.discovery.Specific)6 DiscoveryConfiguration (org.opennms.netmgt.config.discovery.DiscoveryConfiguration)5 ExcludeRange (org.opennms.netmgt.config.discovery.ExcludeRange)4 RequestDispatcher (javax.servlet.RequestDispatcher)2 ServletException (javax.servlet.ServletException)2 HttpSession (javax.servlet.http.HttpSession)2 Test (org.junit.Test)2 File (java.io.File)1 FileReader (java.io.FileReader)1 StringWriter (java.io.StringWriter)1 LinkedList (java.util.LinkedList)1 ServiceRegistry (org.opennms.core.soa.ServiceRegistry)1 DiscoveryConfigFactory (org.opennms.netmgt.config.DiscoveryConfigFactory)1 DiscoveryTaskExecutor (org.opennms.netmgt.discovery.DiscoveryTaskExecutor)1 EventProxy (org.opennms.netmgt.events.api.EventProxy)1 IPPollAddress (org.opennms.netmgt.model.discovery.IPPollAddress)1 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1