Search in sources :

Example 1 with ServiceTypeDao

use of org.opennms.netmgt.dao.api.ServiceTypeDao in project opennms by OpenNMS.

the class OnmsMonitoredServiceResource method addService.

/**
     * <p>addService</p>
     *
     * @param nodeCriteria a {@link java.lang.String} object.
     * @param ipAddress a {@link java.lang.String} object.
     * @param service a {@link org.opennms.netmgt.model.OnmsMonitoredService} object.
     * @return a {@link javax.ws.rs.core.Response} object.
     */
@POST
@Consumes(MediaType.APPLICATION_XML)
public Response addService(@Context final UriInfo uriInfo, @PathParam("nodeCriteria") final String nodeCriteria, @PathParam("ipAddress") final String ipAddress, final OnmsMonitoredService service) {
    writeLock();
    try {
        OnmsNode node = m_nodeDao.get(nodeCriteria);
        if (node == null)
            throw getException(Status.BAD_REQUEST, "Node {} was not found.", nodeCriteria);
        final OnmsIpInterface intf = node.getIpInterfaceByIpAddress(ipAddress);
        if (intf == null)
            throw getException(Status.BAD_REQUEST, "IP Interface {} was not found on node {}.", ipAddress, nodeCriteria);
        if (service == null)
            throw getException(Status.BAD_REQUEST, "Service object cannot be null");
        if (service.getServiceName() == null)
            throw getException(Status.BAD_REQUEST, "Service must have a name");
        final OnmsServiceType serviceType = new CreateIfNecessaryTemplate<OnmsServiceType, ServiceTypeDao>(m_transactionManager, m_serviceTypeDao) {

            @Override
            protected OnmsServiceType query() {
                return m_dao.findByName(service.getServiceName());
            }

            @Override
            protected OnmsServiceType doInsert() {
                LOG.info("addService: creating service type {}", service.getServiceName());
                final OnmsServiceType s = new OnmsServiceType(service.getServiceName());
                m_dao.saveOrUpdate(s);
                return s;
            }
        }.execute();
        service.setServiceType(serviceType);
        service.setIpInterface(intf);
        LOG.debug("addService: adding service {}", service);
        m_serviceDao.save(service);
        Event e = EventUtils.createNodeGainedServiceEvent("ReST", node.getId(), intf.getIpAddress(), service.getServiceName(), node.getLabel(), node.getLabelSource(), node.getSysName(), node.getSysDescription());
        sendEvent(e);
        return Response.created(getRedirectUri(uriInfo, service.getServiceName())).build();
    } finally {
        writeUnlock();
    }
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) OnmsServiceType(org.opennms.netmgt.model.OnmsServiceType) Event(org.opennms.netmgt.xml.event.Event) ServiceTypeDao(org.opennms.netmgt.dao.api.ServiceTypeDao) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 ServiceTypeDao (org.opennms.netmgt.dao.api.ServiceTypeDao)1 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 OnmsServiceType (org.opennms.netmgt.model.OnmsServiceType)1 Event (org.opennms.netmgt.xml.event.Event)1