Search in sources :

Example 21 with Requisition

use of org.opennms.netmgt.provision.persist.requisition.Requisition in project opennms by OpenNMS.

the class FasterFilesystemForeignSourceRepository method importResourceRequisition.

/* (non-Javadoc)
     * @see org.opennms.netmgt.provision.persist.AbstractForeignSourceRepository#importResourceRequisition(org.springframework.core.io.Resource)
     */
@Override
public Requisition importResourceRequisition(final Resource resource) throws ForeignSourceRepositoryException {
    Assert.notNull(resource);
    try {
        boolean isLocal = true;
        try {
            resource.getFile();
        } catch (Exception e) {
            isLocal = false;
            LOG.debug("importResourceRequisition: resource {} is not local, ignoring cache", resource);
        }
        // Trust whatever is on the cache if exist for local resources only.
        if (isLocal) {
            LOG.debug("importResourceRequisition: saving cached requisition to disk");
            final Requisition req = getRequisitionsDirectoryWatcher().getContents(resource.getFilename());
            if (req != null) {
                req.setResource(resource);
                save(req);
                return req;
            }
        }
    } catch (Exception e) {
        LOG.error("importResourceRequisition: can't save cached requisition associated with {}", resource, e);
    }
    // Use the default implementation if the cache doesn't contain the requisition or the requisition comes from an external source.
    return super.importResourceRequisition(resource);
}
Also used : MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition)

Example 22 with Requisition

use of org.opennms.netmgt.provision.persist.requisition.Requisition in project opennms by OpenNMS.

the class RequisitionsMigratorOffline method execute.

/* (non-Javadoc)
     * @see org.opennms.upgrade.api.OnmsUpgrade#execute()
     */
@Override
public void execute() throws OnmsUpgradeException {
    try {
        for (File req : FileUtils.listFiles(getRequisitionDir(), new String[] { "xml" }, true)) {
            log("Processing %s\n", req);
            String content = IOUtils.toString(new FileInputStream(req), StandardCharsets.UTF_8);
            String output = content.replaceAll(" non-ip-(snmp-primary|interfaces)=\"[^\"]+\"", "");
            if (content.length() != output.length()) {
                log("  Updating and parsing the requisition\n", req);
                IOUtils.write(output, new FileOutputStream(req), StandardCharsets.UTF_8);
                Requisition requisition = JaxbUtils.unmarshal(Requisition.class, req, true);
                if (requisition == null) {
                    throw new OnmsUpgradeException("Can't parse requisition " + req);
                }
            }
        }
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't upgrade requisitions because " + e.getMessage(), e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Example 23 with Requisition

use of org.opennms.netmgt.provision.persist.requisition.Requisition in project opennms by OpenNMS.

the class HeartbeatConsumer method provision.

private void provision(final OnmsMinion minion, final String prevLocation, final String nextLocation) {
    // Return fast if automatic provisioning is disabled
    if (!PROVISIONING) {
        return;
    }
    // Return fast until the provisioner is running to pick up the events sent below
    if (!this.eventSubscriptionService.hasEventListener(EventConstants.RELOAD_IMPORT_UEI)) {
        return;
    }
    final String prevForeignSource = String.format(PROVISIONING_FOREIGN_SOURCE_PATTERN, prevLocation);
    final String nextForeignSource = String.format(PROVISIONING_FOREIGN_SOURCE_PATTERN, nextLocation);
    final Set<String> alteredForeignSources = Sets.newHashSet();
    // Remove the node from the previous requisition, if location has changed
    if (!Objects.equals(prevForeignSource, nextForeignSource)) {
        final Requisition prevRequisition = this.deployedForeignSourceRepository.getRequisition(prevForeignSource);
        if (prevRequisition != null && prevRequisition.getNode(minion.getId()) != null) {
            prevRequisition.deleteNode(minion.getId());
            prevRequisition.updateDateStamp();
            deployedForeignSourceRepository.save(prevRequisition);
            deployedForeignSourceRepository.flush();
            alteredForeignSources.add(prevForeignSource);
        }
    }
    Requisition nextRequisition = deployedForeignSourceRepository.getRequisition(nextForeignSource);
    if (nextRequisition == null) {
        nextRequisition = new Requisition(nextForeignSource);
        nextRequisition.updateDateStamp();
        // We have to save the requisition before we can alter the according foreign source definition
        deployedForeignSourceRepository.save(nextRequisition);
        // Remove all policies and detectors from the foreign source
        final ForeignSource foreignSource = deployedForeignSourceRepository.getForeignSource(nextForeignSource);
        foreignSource.setDetectors(Collections.emptyList());
        foreignSource.setPolicies(Collections.emptyList());
        deployedForeignSourceRepository.save(foreignSource);
        alteredForeignSources.add(nextForeignSource);
    }
    RequisitionNode requisitionNode = nextRequisition.getNode(minion.getId());
    if (requisitionNode == null) {
        final RequisitionInterface requisitionInterface = new RequisitionInterface();
        requisitionInterface.setIpAddr(MINION_INTERFACE);
        ensureServicesAreOnInterface(requisitionInterface);
        requisitionNode = new RequisitionNode();
        requisitionNode.setNodeLabel(minion.getId());
        requisitionNode.setForeignId(minion.getLabel() != null ? minion.getLabel() : minion.getId());
        requisitionNode.setLocation(minion.getLocation());
        requisitionNode.putInterface(requisitionInterface);
        nextRequisition.putNode(requisitionNode);
        nextRequisition.setDate(new Date());
        deployedForeignSourceRepository.save(nextRequisition);
        deployedForeignSourceRepository.flush();
        alteredForeignSources.add(nextForeignSource);
    } else {
        // The node already exists in the requisition
        RequisitionInterface requisitionInterface = requisitionNode.getInterface(MINION_INTERFACE);
        if (requisitionInterface == null) {
            // The interface was deleted, add it again
            requisitionInterface = new RequisitionInterface();
            requisitionInterface.setIpAddr(MINION_INTERFACE);
            requisitionNode.putInterface(requisitionInterface);
        }
        if (ensureServicesAreOnInterface(requisitionInterface)) {
            // We've altered the set of services on the interface
            nextRequisition.setDate(new Date());
            deployedForeignSourceRepository.save(nextRequisition);
            deployedForeignSourceRepository.flush();
            alteredForeignSources.add(nextForeignSource);
        }
    }
    for (final String alteredForeignSource : alteredForeignSources) {
        final EventBuilder eventBuilder = new EventBuilder(EventConstants.RELOAD_IMPORT_UEI, "Web");
        eventBuilder.addParam(EventConstants.PARM_URL, String.valueOf(deployedForeignSourceRepository.getRequisitionURL(alteredForeignSource)));
        try {
            eventProxy.send(eventBuilder.getEvent());
        } catch (final EventProxyException e) {
            throw new DataAccessResourceFailureException("Unable to send event to import group " + alteredForeignSource, e);
        }
    }
}
Also used : RequisitionNode(org.opennms.netmgt.provision.persist.requisition.RequisitionNode) RequisitionInterface(org.opennms.netmgt.provision.persist.requisition.RequisitionInterface) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) ForeignSource(org.opennms.netmgt.provision.persist.foreignsource.ForeignSource) EventProxyException(org.opennms.netmgt.events.api.EventProxyException) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition) Date(java.util.Date)

Example 24 with Requisition

use of org.opennms.netmgt.provision.persist.requisition.Requisition in project opennms by OpenNMS.

the class VmwareRequisitionUrlConnection method getInputStream.

/**
 * {@inheritDoc}
 * <p/>
 * Creates a ByteArrayInputStream implementation of InputStream of the XML
 * marshaled version of the Requisition class. Calling close on this stream
 * is safe.
 */
@Override
public InputStream getInputStream() throws IOException {
    InputStream stream = null;
    try {
        final Requisition existingRequisition = getExistingRequisition(importRequest.getForeignSource());
        importRequest.setExistingRequisition(existingRequisition);
        final VmwareImporter importer = new VmwareImporter(importRequest);
        stream = new ByteArrayInputStream(jaxBMarshal(importer.getRequisition()).getBytes());
    } catch (Throwable e) {
        logger.warn("Problem getting input stream: '{}'", e);
        throw new IOExceptionWithCause("Problem getting input stream: " + e, e);
    }
    return stream;
}
Also used : IOExceptionWithCause(org.apache.commons.io.IOExceptionWithCause) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition)

Example 25 with Requisition

use of org.opennms.netmgt.provision.persist.requisition.Requisition in project opennms by OpenNMS.

the class NewSuspectScanIT method testScanNewSuspectWithForeignSourceAndLocation.

@Test(timeout = 300000)
public void testScanNewSuspectWithForeignSourceAndLocation() throws Exception {
    final int nextNodeId = m_nodeDao.getNextNodeId();
    // Verify empty database
    assertEquals(1, getDistPollerDao().countAll());
    assertEquals(0, getNodeDao().countAll());
    assertEquals(0, getInterfaceDao().countAll());
    assertEquals(0, getMonitoredServiceDao().countAll());
    assertEquals(0, getServiceTypeDao().countAll());
    assertEquals(0, getSnmpInterfaceDao().countAll());
    final EventAnticipator anticipator = m_eventSubscriber.getEventAnticipator();
    anticipator.anticipateEvent(new EventBuilder(EventConstants.NODE_ADDED_EVENT_UEI, "Provisiond").setNodeid(nextNodeId).getEvent());
    anticipator.anticipateEvent(new EventBuilder(EventConstants.NODE_GAINED_INTERFACE_EVENT_UEI, "Provisiond").setNodeid(nextNodeId).setInterface(addr("198.51.100.201")).getEvent());
    anticipator.anticipateEvent(new EventBuilder(EventConstants.PROVISION_SCAN_COMPLETE_UEI, "Provisiond").setNodeid(nextNodeId).getEvent());
    final String foreignSource = "Testie";
    final String locationName = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID;
    final NewSuspectScan scan = m_provisioner.createNewSuspectScan(addr("198.51.100.201"), foreignSource, locationName);
    runScan(scan);
    anticipator.verifyAnticipated(20000, 0, 0, 0, 0);
    // Verify distpoller count
    assertEquals(1, getDistPollerDao().countAll());
    // Verify node count
    assertEquals(1, getNodeDao().countAll());
    // Verify ipinterface count
    assertEquals("Unexpected number of interfaces found: " + getInterfaceDao().findAll(), 1, getInterfaceDao().countAll());
    // Verify ifservices count - discover snmp service on other if
    assertEquals("Unexpected number of services found: " + getMonitoredServiceDao().findAll(), 0, getMonitoredServiceDao().countAll());
    // Verify service count
    assertEquals(0, getServiceTypeDao().countAll());
    // Verify snmpInterface count
    assertEquals(0, getSnmpInterfaceDao().countAll());
    // HZN-960: Verify that the location name was properly set on the node in the requisition
    final Requisition requisition = m_foreignSourceRepository.getRequisition(foreignSource);
    final List<RequisitionNode> requisitionNodes = requisition.getNodes();
    assertEquals(1, requisitionNodes.size());
    final RequisitionNode requisitionNode = requisitionNodes.get(0);
    assertEquals(locationName, requisitionNode.getLocation());
}
Also used : RequisitionNode(org.opennms.netmgt.provision.persist.requisition.RequisitionNode) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition) EventAnticipator(org.opennms.netmgt.dao.mock.EventAnticipator) Test(org.junit.Test)

Aggregations

Requisition (org.opennms.netmgt.provision.persist.requisition.Requisition)70 Test (org.junit.Test)31 RequisitionNode (org.opennms.netmgt.provision.persist.requisition.RequisitionNode)16 ForeignSource (org.opennms.netmgt.provision.persist.foreignsource.ForeignSource)11 RequisitionInterface (org.opennms.netmgt.provision.persist.requisition.RequisitionInterface)11 UrlResource (org.springframework.core.io.UrlResource)10 File (java.io.File)9 OnmsNodeRequisition (org.opennms.netmgt.provision.persist.OnmsNodeRequisition)8 Resource (org.springframework.core.io.Resource)8 IOException (java.io.IOException)6 RequisitionCategory (org.opennms.netmgt.provision.persist.requisition.RequisitionCategory)6 ArrayList (java.util.ArrayList)5 TreeSet (java.util.TreeSet)5 InetAddress (java.net.InetAddress)4 MalformedURLException (java.net.MalformedURLException)4 URL (java.net.URL)4 FileSystemResource (org.springframework.core.io.FileSystemResource)4 InputStream (java.io.InputStream)3 RemoteException (java.rmi.RemoteException)3 Date (java.util.Date)3