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));
}
}
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);
}
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());
}
}
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;
}
Aggregations