Search in sources :

Example 1 with UpsertTemplate

use of org.opennms.netmgt.dao.support.UpsertTemplate in project opennms by OpenNMS.

the class DefaultUpsertService method upsert.

@Override
public OnmsSnmpInterface upsert(final int nodeId, final OnmsSnmpInterface snmpInterface, final int sleep) {
    UpsertTemplate<OnmsSnmpInterface, SnmpInterfaceDao> upzerter = new UpsertTemplate<OnmsSnmpInterface, SnmpInterfaceDao>(m_transactionManager, m_snmpInterfaceDao) {

        @Override
        public OnmsSnmpInterface query() {
            OnmsSnmpInterface dbSnmpIface = m_snmpInterfaceDao.findByNodeIdAndIfIndex(nodeId, snmpInterface.getIfIndex());
            sleep(sleep);
            return dbSnmpIface;
        }

        @Override
        public OnmsSnmpInterface doUpdate(OnmsSnmpInterface dbSnmpIface) {
            // update the interface that was found
            LOG.debug("nodeId = {}, ifIndex = {}, dbSnmpIface = {}", nodeId, snmpInterface.getIfIndex(), dbSnmpIface);
            dbSnmpIface.mergeSnmpInterfaceAttributes(snmpInterface);
            LOG.info("Updating SnmpInterface {}", dbSnmpIface);
            m_snmpInterfaceDao.update(dbSnmpIface);
            m_snmpInterfaceDao.flush();
            return dbSnmpIface;
        }

        @Override
        public OnmsSnmpInterface doInsert() {
            // add the interface to the node, if it wasn't found
            final OnmsNode dbNode = m_nodeDao.get(nodeId);
            // for performance reasons we don't add the snmp interface to the node so we avoid loading all the interfaces
            // setNode only sets the node in the interface
            snmpInterface.setNode(dbNode);
            LOG.info("Saving SnmpInterface {}", snmpInterface);
            m_snmpInterfaceDao.save(snmpInterface);
            m_snmpInterfaceDao.flush();
            return snmpInterface;
        }
    };
    return upzerter.execute();
}
Also used : UpsertTemplate(org.opennms.netmgt.dao.support.UpsertTemplate) OnmsNode(org.opennms.netmgt.model.OnmsNode) SnmpInterfaceDao(org.opennms.netmgt.dao.api.SnmpInterfaceDao) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface)

Example 2 with UpsertTemplate

use of org.opennms.netmgt.dao.support.UpsertTemplate in project opennms by OpenNMS.

the class DefaultProvisionService method createUndiscoveredNode.

/** {@inheritDoc} */
@Transactional
@Override
public OnmsNode createUndiscoveredNode(final String ipAddress, final String foreignSource, final String locationString) {
    final String effectiveForeignSource = foreignSource == null ? FOREIGN_SOURCE_FOR_DISCOVERED_NODES : foreignSource;
    final String effectiveLocationName = MonitoringLocationUtils.isDefaultLocationName(locationString) ? null : locationString;
    final OnmsNode node = new UpsertTemplate<OnmsNode, NodeDao>(m_transactionManager, m_nodeDao) {

        @Override
        protected OnmsNode query() {
            // Find all of the nodes in the target requisition with the given IP address
            return m_nodeDao.findByForeignSourceAndIpAddress(effectiveForeignSource, ipAddress).stream().filter(n -> {
                final String existingLocationName = MonitoringLocationUtils.getLocationNameOrNullIfDefault(n);
                return Objects.equals(existingLocationName, effectiveLocationName);
            }).findFirst().orElse(null);
        }

        @Override
        protected OnmsNode doUpdate(OnmsNode existingNode) {
            // we found an existing node so exit by returning null;
            return null;
        }

        @Override
        protected OnmsNode doInsert() {
            final Date now = new Date();
            OnmsMonitoringLocation location = createLocationIfNecessary(locationString);
            // Associate the location with the node
            final OnmsNode node = new OnmsNode(location);
            final String hostname = getHostnameResolver().getHostname(addr(ipAddress), locationString);
            if (hostname == null || ipAddress.equals(hostname)) {
                node.setLabel(ipAddress);
                node.setLabelSource(NodeLabelSource.ADDRESS);
            } else {
                node.setLabel(hostname);
                node.setLabelSource(NodeLabelSource.HOSTNAME);
            }
            node.setForeignSource(effectiveForeignSource);
            node.setType(NodeType.ACTIVE);
            node.setLastCapsdPoll(now);
            final OnmsIpInterface iface = new OnmsIpInterface(InetAddressUtils.addr(ipAddress), node);
            iface.setIsManaged("M");
            iface.setIpHostName(hostname);
            iface.setIsSnmpPrimary(PrimaryType.NOT_ELIGIBLE);
            iface.setIpLastCapsdPoll(now);
            m_nodeDao.save(node);
            m_nodeDao.flush();
            return node;
        }
    }.execute();
    if (node != null) {
        if (effectiveForeignSource != null) {
            node.setForeignId(node.getNodeId());
            createUpdateRequistion(ipAddress, node, effectiveLocationName, effectiveForeignSource);
        }
        // we do this here rather than in the doInsert method because
        // the doInsert may abort
        node.visit(new AddEventVisitor(m_eventForwarder));
    }
    return node;
}
Also used : NodeDao(org.opennms.netmgt.dao.api.NodeDao) OnmsMonitoringLocation(org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation) NodeType(org.opennms.netmgt.model.OnmsNode.NodeType) OnmsNodeRequisition(org.opennms.netmgt.provision.persist.OnmsNodeRequisition) AbstractEntityVisitor(org.opennms.netmgt.model.AbstractEntityVisitor) Date(java.util.Date) MonitoringLocationUtils(org.opennms.netmgt.dao.api.MonitoringLocationUtils) UpsertTemplate(org.opennms.netmgt.dao.support.UpsertTemplate) LocationAwareDnsLookupClient(org.opennms.netmgt.provision.LocationAwareDnsLookupClient) InetAddressUtils.str(org.opennms.core.utils.InetAddressUtils.str) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) EventUtils(org.opennms.netmgt.model.events.EventUtils) InetAddressUtils.addr(org.opennms.core.utils.InetAddressUtils.addr) IpInterfacePolicy(org.opennms.netmgt.provision.IpInterfacePolicy) MonitoredServiceDao(org.opennms.netmgt.dao.api.MonitoredServiceDao) ForeignSource(org.opennms.netmgt.provision.persist.foreignsource.ForeignSource) InetAddress(java.net.InetAddress) OnmsCategory(org.opennms.netmgt.model.OnmsCategory) SnmpInterfaceDao(org.opennms.netmgt.dao.api.SnmpInterfaceDao) NodeLabelSource(org.opennms.netmgt.model.OnmsNode.NodeLabelSource) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition) Map(java.util.Map) Resource(org.springframework.core.io.Resource) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) NodeDao(org.opennms.netmgt.dao.api.NodeDao) InetAddressUtils(org.opennms.core.utils.InetAddressUtils) Collection(java.util.Collection) UpdateEventVisitor(org.opennms.netmgt.model.events.UpdateEventVisitor) Set(java.util.Set) CreateIfNecessaryTemplate(org.opennms.netmgt.dao.support.CreateIfNecessaryTemplate) AddEventVisitor(org.opennms.netmgt.model.events.AddEventVisitor) BeanUtils(org.opennms.core.spring.BeanUtils) Objects(java.util.Objects) List(java.util.List) RequisitionCategory(org.opennms.netmgt.provision.persist.requisition.RequisitionCategory) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) RequisitionNode(org.opennms.netmgt.provision.persist.requisition.RequisitionNode) PathElement(org.opennms.netmgt.model.PathElement) LocationAwareSnmpClient(org.opennms.netmgt.snmp.proxy.LocationAwareSnmpClient) RequisitionedCategoryAssociation(org.opennms.netmgt.model.RequisitionedCategoryAssociation) DeleteEventVisitor(org.opennms.netmgt.model.events.DeleteEventVisitor) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) NodePolicy(org.opennms.netmgt.provision.NodePolicy) Duration(org.joda.time.Duration) EntityVisitor(org.opennms.netmgt.model.EntityVisitor) HashMap(java.util.HashMap) EventConstants(org.opennms.netmgt.events.api.EventConstants) RequisitionedCategoryAssociationDao(org.opennms.netmgt.dao.api.RequisitionedCategoryAssociationDao) RequisitionInterface(org.opennms.netmgt.provision.persist.requisition.RequisitionInterface) TreeSet(java.util.TreeSet) InitializingBean(org.springframework.beans.factory.InitializingBean) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) PrimaryType(org.opennms.netmgt.model.PrimaryType) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ServiceTypeDao(org.opennms.netmgt.dao.api.ServiceTypeDao) ForeignSourceRepository(org.opennms.netmgt.provision.persist.ForeignSourceRepository) PluginConfig(org.opennms.netmgt.provision.persist.foreignsource.PluginConfig) OnmsServiceType(org.opennms.netmgt.model.OnmsServiceType) OnmsNode(org.opennms.netmgt.model.OnmsNode) RequisitionFileUtils(org.opennms.netmgt.provision.persist.RequisitionFileUtils) CategoryDao(org.opennms.netmgt.dao.api.CategoryDao) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DateTime(org.joda.time.DateTime) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) LocationAwareDetectorClient(org.opennms.netmgt.provision.LocationAwareDetectorClient) MonitoringLocationDao(org.opennms.netmgt.dao.api.MonitoringLocationDao) EventForwarder(org.opennms.netmgt.events.api.EventForwarder) RequisitionInterfaceCollection(org.opennms.netmgt.provision.persist.requisition.RequisitionInterfaceCollection) Collections(java.util.Collections) ForeignSourceRepositoryException(org.opennms.netmgt.provision.persist.ForeignSourceRepositoryException) SnmpInterfacePolicy(org.opennms.netmgt.provision.SnmpInterfacePolicy) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) AddEventVisitor(org.opennms.netmgt.model.events.AddEventVisitor) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) Date(java.util.Date) OnmsMonitoringLocation(org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

SnmpInterfaceDao (org.opennms.netmgt.dao.api.SnmpInterfaceDao)2 UpsertTemplate (org.opennms.netmgt.dao.support.UpsertTemplate)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 Strings (com.google.common.base.Strings)1 InetAddress (java.net.InetAddress)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 DateTime (org.joda.time.DateTime)1 Duration (org.joda.time.Duration)1 BeanUtils (org.opennms.core.spring.BeanUtils)1