Search in sources :

Example 1 with ServiceRegistry

use of org.opennms.core.soa.ServiceRegistry in project opennms by OpenNMS.

the class DiscoveryScanServlet 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 = new DiscoveryConfiguration();
    }
    //load current general settings
    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);
    }
    // Submit the discovery job
    if (action.equals(saveAndRestartAction)) {
        try {
            WebApplicationContext beanFactory = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
            // Fetch the ServiceRegistry
            ServiceRegistry registry = beanFactory.getBean(ServiceRegistry.class);
            // Use it to look up a DiscoveryTaskExecutor service
            DiscoveryTaskExecutor executor = registry.findProvider(DiscoveryTaskExecutor.class);
            // If the service exists...
            if (executor != null) {
                // Submit the job to the discovery queue
                executor.handleDiscoveryTask(config);
            } else {
                LOG.warn("No DiscoveryTaskExecutor service is available");
            }
        } catch (Throwable ex) {
            LOG.error("Error while submitting task", ex);
            throw new ServletException(ex);
        }
        // TODO: Send an event here when the scan is started? Or do it on the Camel side?
        /*
        	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/scan-done.jsp"));
        return;
    }
    sess.setAttribute(ATTRIBUTE_DISCOVERY_CONFIGURATION, config);
    RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/admin/discovery/edit-scan.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) WebApplicationContext(org.springframework.web.context.WebApplicationContext) DiscoveryTaskExecutor(org.opennms.netmgt.discovery.DiscoveryTaskExecutor) ServletException(javax.servlet.ServletException) ServiceRegistry(org.opennms.core.soa.ServiceRegistry) ExcludeRange(org.opennms.netmgt.config.discovery.ExcludeRange)

Example 2 with ServiceRegistry

use of org.opennms.core.soa.ServiceRegistry in project opennms by OpenNMS.

the class HttpRemotingWebAppIT method test.

@Test
public void test() throws Exception {
    WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    assertTrue(context.containsBean("serviceRegistry"));
    assertTrue(context.containsBean("serviceRegistryExporter"));
    ServiceRegistry registry = context.getBean("serviceRegistry", ServiceRegistry.class);
    ServiceRegistryHttpInvokerServiceExporter exporter = context.getBean("serviceRegistryExporter", ServiceRegistryHttpInvokerServiceExporter.class);
    assertEquals(exporter.getServiceRegistry(), registry);
    assertTrue(exporter.getServiceRegistry() == registry);
}
Also used : ServiceRegistryHttpInvokerServiceExporter(org.opennms.core.spring.web.ServiceRegistryHttpInvokerServiceExporter) ServiceRegistry(org.opennms.core.soa.ServiceRegistry) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 3 with ServiceRegistry

use of org.opennms.core.soa.ServiceRegistry in project opennms by OpenNMS.

the class RegistrationListenerBeanTest method testCallBindUnbindMethods.

@Test
public void testCallBindUnbindMethods() throws Exception {
    RegistrationListenerBean<Hello> listener = new RegistrationListenerBean<Hello>();
    listener.setServiceInterface(Hello.class);
    listener.setTarget(this);
    listener.setBindMethod("bind");
    listener.setUnbindMethod("unbind");
    listener.afterPropertiesSet();
    ServiceRegistry registry = new DefaultServiceRegistry();
    Registration reg1 = registry.register(new MyProvider("prov1"), Hello.class, Goodbye.class);
    Registration reg2 = registry.register(new MyProvider("prov2"), Hello.class, Goodbye.class);
    ReferenceListFactoryBean<Hello> bean = new ReferenceListFactoryBean<Hello>();
    bean.setServiceInterface(Hello.class);
    bean.setServiceRegistry(registry);
    bean.addListener(listener);
    bean.afterPropertiesSet();
    assertEquals(2, getTotalProvided());
    Registration reg3 = registry.register(new MyProvider("prov3"), Hello.class, Goodbye.class);
    assertEquals(3, getTotalProvided());
    reg2.unregister();
    assertEquals(2, getTotalProvided());
    reg1.unregister();
    reg3.unregister();
    assertEquals(0, getTotalProvided());
}
Also used : Registration(org.opennms.core.soa.Registration) ServiceRegistry(org.opennms.core.soa.ServiceRegistry) Test(org.junit.Test)

Example 4 with ServiceRegistry

use of org.opennms.core.soa.ServiceRegistry in project opennms by OpenNMS.

the class ReferenceListFactoryBeanTest method testListListeners.

@Test
public void testListListeners() throws Exception {
    ServiceRegistry registry = new DefaultServiceRegistry();
    Registration reg1 = registry.register(new MyProvider("prov1"), Hello.class, Goodbye.class);
    Registration reg2 = registry.register(new MyProvider("prov2"), Hello.class, Goodbye.class);
    ReferenceListFactoryBean<Hello> bean = new ReferenceListFactoryBean<Hello>();
    bean.setServiceInterface(Hello.class);
    bean.setServiceRegistry(registry);
    CountingListener listener = new CountingListener();
    bean.addListener(listener);
    bean.afterPropertiesSet();
    assertEquals(2, listener.getTotalProvided());
    Registration reg3 = registry.register(new MyProvider("prov3"), Hello.class, Goodbye.class);
    assertEquals(3, listener.getTotalProvided());
    reg2.unregister();
    assertEquals(2, listener.getTotalProvided());
    reg1.unregister();
    reg3.unregister();
    assertEquals(0, listener.getTotalProvided());
}
Also used : Registration(org.opennms.core.soa.Registration) ServiceRegistry(org.opennms.core.soa.ServiceRegistry) Test(org.junit.Test)

Example 5 with ServiceRegistry

use of org.opennms.core.soa.ServiceRegistry in project opennms by OpenNMS.

the class ReferenceListFactoryBeanTest method testDynamicList.

@Test
public void testDynamicList() throws Exception {
    ServiceRegistry registry = new DefaultServiceRegistry();
    Registration reg1 = registry.register(new MyProvider("prov1"), Hello.class, Goodbye.class);
    Registration reg2 = registry.register(new MyProvider("prov2"), Hello.class, Goodbye.class);
    ReferenceListFactoryBean<Hello> bean = new ReferenceListFactoryBean<Hello>();
    bean.setServiceInterface(Hello.class);
    bean.setServiceRegistry(registry);
    bean.afterPropertiesSet();
    List<Hello> helloList = getObject(bean);
    assertEquals(2, helloList.size());
    Registration reg3 = registry.register(new MyProvider("prov3"), Hello.class, Goodbye.class);
    assertEquals(3, helloList.size());
    reg2.unregister();
    assertEquals(2, helloList.size());
    reg1.unregister();
    reg3.unregister();
    assertTrue(helloList.isEmpty());
}
Also used : Registration(org.opennms.core.soa.Registration) ServiceRegistry(org.opennms.core.soa.ServiceRegistry) Test(org.junit.Test)

Aggregations

ServiceRegistry (org.opennms.core.soa.ServiceRegistry)7 Test (org.junit.Test)4 Registration (org.opennms.core.soa.Registration)3 WebApplicationContext (org.springframework.web.context.WebApplicationContext)2 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletException (javax.servlet.ServletException)1 HttpSession (javax.servlet.http.HttpSession)1 ServiceRegistryHttpInvokerServiceExporter (org.opennms.core.spring.web.ServiceRegistryHttpInvokerServiceExporter)1 InMemoryGraphmlRepository (org.opennms.features.graphml.service.InMemoryGraphmlRepository)1 DiscoveryConfiguration (org.opennms.netmgt.config.discovery.DiscoveryConfiguration)1 ExcludeRange (org.opennms.netmgt.config.discovery.ExcludeRange)1 IncludeRange (org.opennms.netmgt.config.discovery.IncludeRange)1 IncludeUrl (org.opennms.netmgt.config.discovery.IncludeUrl)1 Specific (org.opennms.netmgt.config.discovery.Specific)1 DiscoveryTaskExecutor (org.opennms.netmgt.discovery.DiscoveryTaskExecutor)1