Search in sources :

Example 1 with AddEventVisitor

use of org.opennms.netmgt.model.events.AddEventVisitor in project opennms by OpenNMS.

the class OnmsIpInterface method mergeMonitoredServices.

/**
     * <p>mergeMonitoredServices</p>
     *
     * @param scannedIface a {@link org.opennms.netmgt.model.OnmsIpInterface} object.
     * @param eventForwarder a {@link org.opennms.netmgt.events.api.EventForwarder} object.
     * @param deleteMissing a boolean.
     */
public void mergeMonitoredServices(OnmsIpInterface scannedIface, EventForwarder eventForwarder, boolean deleteMissing) {
    // create map of services to serviceType
    Map<OnmsServiceType, OnmsMonitoredService> serviceTypeMap = new HashMap<OnmsServiceType, OnmsMonitoredService>();
    for (OnmsMonitoredService svc : scannedIface.getMonitoredServices()) {
        serviceTypeMap.put(svc.getServiceType(), svc);
    }
    // for each service in the database
    for (Iterator<OnmsMonitoredService> it = getMonitoredServices().iterator(); it.hasNext(); ) {
        OnmsMonitoredService svc = it.next();
        // find the corresponding scanned service
        OnmsMonitoredService imported = serviceTypeMap.get(svc.getServiceType());
        if (imported == null) {
            if (deleteMissing) {
                // there is no scanned service... delete it from the database 
                it.remove();
                svc.visit(new DeleteEventVisitor(eventForwarder));
            }
        } else {
            // otherwice update the service attributes
            svc.mergeServiceAttributes(imported);
        }
        // mark the service is updated
        serviceTypeMap.remove(svc.getServiceType());
    }
    // for any services not found in the database, add them
    Collection<OnmsMonitoredService> newServices = serviceTypeMap.values();
    for (OnmsMonitoredService svc : newServices) {
        svc.setIpInterface(this);
        getMonitoredServices().add(svc);
        svc.visit(new AddEventVisitor(eventForwarder));
    }
}
Also used : AddEventVisitor(org.opennms.netmgt.model.events.AddEventVisitor) HashMap(java.util.HashMap) DeleteEventVisitor(org.opennms.netmgt.model.events.DeleteEventVisitor)

Example 2 with AddEventVisitor

use of org.opennms.netmgt.model.events.AddEventVisitor in project opennms by OpenNMS.

the class DefaultProvisionService method insertNode.

/** {@inheritDoc} */
@Transactional
@Override
public void insertNode(final OnmsNode node) {
    updateLocation(node);
    m_nodeDao.save(node);
    m_nodeDao.flush();
    final EntityVisitor visitor = new AddEventVisitor(m_eventForwarder);
    node.visit(visitor);
}
Also used : AddEventVisitor(org.opennms.netmgt.model.events.AddEventVisitor) AbstractEntityVisitor(org.opennms.netmgt.model.AbstractEntityVisitor) EntityVisitor(org.opennms.netmgt.model.EntityVisitor) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with AddEventVisitor

use of org.opennms.netmgt.model.events.AddEventVisitor in project opennms by OpenNMS.

the class OnmsNode method mergeIpInterfaces.

/**
     * <p>mergeIpInterfaces</p>
     *
     * @param scannedNode a {@link org.opennms.netmgt.model.OnmsNode} object.
     * @param eventForwarder a {@link org.opennms.netmgt.events.api.EventForwarder} object.
     * @param deleteMissing a boolean.
     */
public void mergeIpInterfaces(OnmsNode scannedNode, EventForwarder eventForwarder, boolean deleteMissing) {
    OnmsIpInterface oldPrimaryInterface = null;
    OnmsIpInterface scannedPrimaryIf = null;
    // build a map of ipAddrs to ipInterfaces for the scanned node
    Map<InetAddress, OnmsIpInterface> ipInterfaceMap = new HashMap<InetAddress, OnmsIpInterface>();
    for (OnmsIpInterface iface : scannedNode.getIpInterfaces()) {
        if (scannedPrimaryIf == null && iface.isPrimary()) {
            scannedPrimaryIf = iface;
        } else if (iface.isPrimary()) {
            iface.setIsSnmpPrimary(PrimaryType.SECONDARY);
        }
        ipInterfaceMap.put(iface.getIpAddress(), iface);
    }
    // for each ipInterface from the database
    for (Iterator<OnmsIpInterface> it = getIpInterfaces().iterator(); it.hasNext(); ) {
        OnmsIpInterface dbIface = it.next();
        // find the corresponding scanned Interface
        OnmsIpInterface scannedIface = ipInterfaceMap.get(dbIface.getIpAddress());
        // if we can't find a scanned interface remove from the database
        if (scannedIface == null) {
            if (deleteMissing) {
                it.remove();
                dbIface.visit(new DeleteEventVisitor(eventForwarder));
            } else if (scannedPrimaryIf != null && dbIface.isPrimary()) {
                dbIface.setIsSnmpPrimary(PrimaryType.SECONDARY);
                oldPrimaryInterface = dbIface;
            }
        } else {
            // else update the database with scanned info
            dbIface.mergeInterface(scannedIface, eventForwarder, deleteMissing);
            if (scannedPrimaryIf != null && dbIface.isPrimary() && scannedPrimaryIf != scannedIface) {
                dbIface.setIsSnmpPrimary(PrimaryType.SECONDARY);
                oldPrimaryInterface = dbIface;
            }
        }
        // now remove the interface from the map to indicate it was processed
        ipInterfaceMap.remove(dbIface.getIpAddress());
    }
    // for any remaining scanned interfaces, add them to the database
    for (OnmsIpInterface iface : ipInterfaceMap.values()) {
        addIpInterface(iface);
        if (iface.getIfIndex() != null) {
            iface.setSnmpInterface(getSnmpInterfaceWithIfIndex(iface.getIfIndex()));
        }
        iface.visit(new AddEventVisitor(eventForwarder));
    }
    if (oldPrimaryInterface != null && scannedPrimaryIf != null) {
        EventBuilder bldr = new EventBuilder(EventConstants.PRIMARY_SNMP_INTERFACE_CHANGED_EVENT_UEI, "Provisiond");
        bldr.setIpInterface(scannedPrimaryIf);
        bldr.setService("SNMP");
        bldr.addParam(EventConstants.PARM_OLD_PRIMARY_SNMP_ADDRESS, InetAddressUtils.str(oldPrimaryInterface.getIpAddress()));
        bldr.addParam(EventConstants.PARM_NEW_PRIMARY_SNMP_ADDRESS, InetAddressUtils.str(scannedPrimaryIf.getIpAddress()));
        eventForwarder.sendNow(bldr.getEvent());
    }
}
Also used : AddEventVisitor(org.opennms.netmgt.model.events.AddEventVisitor) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) HashMap(java.util.HashMap) InetAddress(java.net.InetAddress) DeleteEventVisitor(org.opennms.netmgt.model.events.DeleteEventVisitor)

Example 4 with AddEventVisitor

use of org.opennms.netmgt.model.events.AddEventVisitor 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

AddEventVisitor (org.opennms.netmgt.model.events.AddEventVisitor)4 HashMap (java.util.HashMap)3 DeleteEventVisitor (org.opennms.netmgt.model.events.DeleteEventVisitor)3 InetAddress (java.net.InetAddress)2 AbstractEntityVisitor (org.opennms.netmgt.model.AbstractEntityVisitor)2 EntityVisitor (org.opennms.netmgt.model.EntityVisitor)2 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)2 Strings (com.google.common.base.Strings)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)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