/** {@inheritDoc} */
  @Override
  public void foundInterface(
      final String ipAddr,
      final Object descr,
      final InterfaceSnmpPrimaryType snmpPrimary,
      final boolean managed,
      final int status) {

    if ("".equals(ipAddr)) {
      LOG.error("Found interface on node {} with an empty ipaddr! Ignoring!", m_node.getLabel());
      // create a bogus OnmsIpInterface and set it to current to services we run across get ignored
      // as well
      m_currentInterface = new OnmsIpInterface();
      return;
    }

    m_currentInterface = new OnmsIpInterface(ipAddr, m_node);
    m_currentInterface.setIsManaged(status == 3 ? "U" : "M");
    m_currentInterface.setIsSnmpPrimary(PrimaryType.get(snmpPrimary.toString()));
    // m_currentInterface.setIpStatus(status == 3 ? new Integer(3) : new Integer(1));

    if (InterfaceSnmpPrimaryType.P.equals(snmpPrimary)) {
      final InetAddress addr = InetAddressUtils.addr(ipAddr);
      if (addr == null) {
        LOG.error(
            "Unable to resolve address of snmpPrimary interface for node {}", m_node.getLabel());
      }
      m_collector = new IfSnmpCollector(addr);
    }

    // FIXME: verify this doesn't conflict with constructor.  The constructor already adds this
    // interface to the node.
    m_node.addIpInterface(m_currentInterface);
  }
Exemple #2
0
  /**
   * mergeIpInterfaces
   *
   * @param scannedNode a {@link org.opennms.netmgt.model.OnmsNode} object.
   * @param eventForwarder a {@link org.opennms.netmgt.model.events.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());
    }
  }