Search in sources :

Example 51 with ForeignSource

use of org.opennms.netmgt.provision.persist.foreignsource.ForeignSource in project opennms by OpenNMS.

the class ForeignSourceConfigRestService method getServices.

/**
 * Gets the services.
 * <p>It will include all the configured service monitors from poller-configuration.xml.</p>
 * <p>If the groupName is not null, it will include the services defined on the foreign source.</p>
 *
 * @param groupName the group name
 * @return the services
 */
@GET
@Path("services/{groupName}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
public ElementList getServices(@PathParam("groupName") String groupName) {
    ElementList elements = new ElementList(m_pollerConfig.getServiceMonitors().keySet());
    m_collectdConfigFactory.getCollectdConfig().getCollectors().forEach(c -> {
        if (!elements.contains(c.getService())) {
            elements.add(c.getService());
        }
    });
    if (groupName != null) {
        final SortedSet<String> serviceNames = new TreeSet<>();
        final ForeignSource pendingForeignSource = m_foreignSourceService.getForeignSource(groupName);
        serviceNames.addAll(pendingForeignSource.getDetectorNames());
        for (final OnmsServiceType type : m_serviceTypeDao.findAll()) {
            serviceNames.add(type.getName());
        }
        // Include all of the service names defined in the poller configuration
        if (m_pollerConfig != null && m_pollerConfig.getServiceMonitors() != null && !m_pollerConfig.getServiceMonitors().isEmpty()) {
            serviceNames.addAll(m_pollerConfig.getServiceMonitors().keySet());
        }
        serviceNames.forEach(s -> {
            if (!elements.contains(s)) {
                elements.add(s);
            }
        });
    }
    return elements;
}
Also used : TreeSet(java.util.TreeSet) ForeignSource(org.opennms.netmgt.provision.persist.foreignsource.ForeignSource) OnmsServiceType(org.opennms.netmgt.model.OnmsServiceType) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 52 with ForeignSource

use of org.opennms.netmgt.provision.persist.foreignsource.ForeignSource in project opennms by OpenNMS.

the class ForeignSourceRestService method deletePolicy.

/**
 * <p>deletePolicy</p>
 *
 * @param foreignSource a {@link java.lang.String} object.
 * @param policy a {@link java.lang.String} object.
 * @return a {@link javax.ws.rs.core.Response} object.
 */
@DELETE
@Path("{foreignSource}/policies/{policy}")
@Transactional
public Response deletePolicy(@PathParam("foreignSource") final String foreignSource, @PathParam("policy") final String policy) {
    writeLock();
    try {
        ForeignSource fs = getActiveForeignSource(foreignSource);
        List<PluginConfig> policies = fs.getPolicies();
        PluginConfig removed = removeEntry(policies, policy);
        if (removed != null) {
            fs.updateDateStamp();
            fs.setPolicies(policies);
            m_pendingForeignSourceRepository.save(fs);
            return Response.accepted().build();
        }
        return Response.notModified().build();
    } finally {
        writeUnlock();
    }
}
Also used : PluginConfig(org.opennms.netmgt.provision.persist.foreignsource.PluginConfig) ForeignSource(org.opennms.netmgt.provision.persist.foreignsource.ForeignSource) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Transactional(org.springframework.transaction.annotation.Transactional)

Example 53 with ForeignSource

use of org.opennms.netmgt.provision.persist.foreignsource.ForeignSource in project opennms by OpenNMS.

the class ForeignSourceRestService method addPolicy.

/**
 * <p>addPolicy</p>
 *
 * @param foreignSource a {@link java.lang.String} object.
 * @param policy a {@link org.opennms.netmgt.provision.persist.foreignsource.PolicyWrapper} object.
 * @return a {@link javax.ws.rs.core.Response} object.
 */
@POST
@Path("{foreignSource}/policies")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
@Transactional
public Response addPolicy(@Context final UriInfo uriInfo, @PathParam("foreignSource") String foreignSource, PolicyWrapper policy) {
    writeLock();
    try {
        LOG.debug("addPolicy: Adding policy {}", policy.getName());
        ForeignSource fs = getActiveForeignSource(foreignSource);
        fs.updateDateStamp();
        fs.addPolicy(policy);
        m_pendingForeignSourceRepository.save(fs);
        return Response.accepted().header("Location", getRedirectUri(uriInfo, policy.getName())).build();
    } finally {
        writeUnlock();
    }
}
Also used : ForeignSource(org.opennms.netmgt.provision.persist.foreignsource.ForeignSource) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Transactional(org.springframework.transaction.annotation.Transactional)

Example 54 with ForeignSource

use of org.opennms.netmgt.provision.persist.foreignsource.ForeignSource in project opennms by OpenNMS.

the class ForeignSourceRestService method deleteDetector.

/**
 * <p>deleteDetector</p>
 *
 * @param foreignSource a {@link java.lang.String} object.
 * @param detector a {@link java.lang.String} object.
 * @return a {@link javax.ws.rs.core.Response} object.
 */
@DELETE
@Path("{foreignSource}/detectors/{detector}")
@Transactional
public Response deleteDetector(@PathParam("foreignSource") final String foreignSource, @PathParam("detector") final String detector) {
    writeLock();
    try {
        ForeignSource fs = getActiveForeignSource(foreignSource);
        List<PluginConfig> detectors = fs.getDetectors();
        PluginConfig removed = removeEntry(detectors, detector);
        if (removed != null) {
            fs.updateDateStamp();
            fs.setDetectors(detectors);
            m_pendingForeignSourceRepository.save(fs);
            return Response.accepted().build();
        }
        return Response.notModified().build();
    } finally {
        writeUnlock();
    }
}
Also used : PluginConfig(org.opennms.netmgt.provision.persist.foreignsource.PluginConfig) ForeignSource(org.opennms.netmgt.provision.persist.foreignsource.ForeignSource) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Transactional(org.springframework.transaction.annotation.Transactional)

Example 55 with ForeignSource

use of org.opennms.netmgt.provision.persist.foreignsource.ForeignSource in project opennms by OpenNMS.

the class ForeignSourceRestService method updateForeignSource.

/**
 * <p>updateForeignSource</p>
 *
 * @param foreignSource a {@link java.lang.String} object.
 * @param params a {@link org.opennms.web.rest.support.MultivaluedMapImpl} object.
 * @return a {@link javax.ws.rs.core.Response} object.
 */
@PUT
@Path("{foreignSource}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Transactional
public Response updateForeignSource(@Context final UriInfo uriInfo, @PathParam("foreignSource") String foreignSource, MultivaluedMapImpl params) {
    writeLock();
    try {
        ForeignSource fs = getActiveForeignSource(foreignSource);
        LOG.debug("updateForeignSource: updating foreign source {}", foreignSource);
        if (params.isEmpty())
            return Response.notModified().build();
        boolean modified = false;
        final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(fs);
        wrapper.registerCustomEditor(Duration.class, new StringIntervalPropertyEditor());
        for (final String key : params.keySet()) {
            if (wrapper.isWritableProperty(key)) {
                Object value = null;
                String stringValue = params.getFirst(key);
                value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
                wrapper.setPropertyValue(key, value);
                modified = true;
            }
        }
        if (modified) {
            LOG.debug("updateForeignSource: foreign source {} updated", foreignSource);
            fs.updateDateStamp();
            m_pendingForeignSourceRepository.save(fs);
            return Response.accepted().header("Location", getRedirectUri(uriInfo)).build();
        } else {
            return Response.notModified().build();
        }
    } finally {
        writeUnlock();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) ForeignSource(org.opennms.netmgt.provision.persist.foreignsource.ForeignSource) StringIntervalPropertyEditor(org.opennms.netmgt.provision.persist.StringIntervalPropertyEditor) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ForeignSource (org.opennms.netmgt.provision.persist.foreignsource.ForeignSource)72 PluginConfig (org.opennms.netmgt.provision.persist.foreignsource.PluginConfig)31 Test (org.junit.Test)23 Requisition (org.opennms.netmgt.provision.persist.requisition.Requisition)11 Before (org.junit.Before)10 MockForeignSourceRepository (org.opennms.netmgt.provision.persist.MockForeignSourceRepository)9 ArrayList (java.util.ArrayList)8 Path (javax.ws.rs.Path)8 Transactional (org.springframework.transaction.annotation.Transactional)8 TreeSet (java.util.TreeSet)6 File (java.io.File)4 DELETE (javax.ws.rs.DELETE)4 Date (java.util.Date)3 Consumes (javax.ws.rs.Consumes)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2 Produces (javax.ws.rs.Produces)2 DateTime (org.joda.time.DateTime)2 Duration (org.joda.time.Duration)2