Search in sources :

Example 6 with IpNetToMedia

use of org.opennms.netmgt.model.IpNetToMedia in project opennms by OpenNMS.

the class EnhancedLinkdTopologyProvider method loadCompleteTopology.

private void loadCompleteTopology() throws MalformedURLException, JAXBException {
    try {
        resetContainer();
    } catch (Exception e) {
        LOG.error("Exception reset Container: " + e.getMessage(), e);
    }
    Map<Integer, OnmsNode> nodemap = new HashMap<Integer, OnmsNode>();
    Map<Integer, List<OnmsIpInterface>> nodeipmap = new HashMap<Integer, List<OnmsIpInterface>>();
    Map<Integer, OnmsIpInterface> nodeipprimarymap = new HashMap<Integer, OnmsIpInterface>();
    Map<String, List<OnmsIpInterface>> macipmap = new HashMap<String, List<OnmsIpInterface>>();
    Map<InetAddress, OnmsIpInterface> ipmap = new HashMap<InetAddress, OnmsIpInterface>();
    Map<Integer, List<OnmsSnmpInterface>> nodesnmpmap = new HashMap<Integer, List<OnmsSnmpInterface>>();
    Timer.Context context = m_loadNodesTimer.time();
    try {
        LOG.info("Loading nodes");
        for (OnmsNode node : m_nodeDao.findAll()) {
            nodemap.put(node.getId(), node);
        }
        LOG.info("Nodes loaded");
    } catch (Exception e) {
        LOG.error("Exception getting node list: " + e.getMessage(), e);
    } finally {
        context.stop();
    }
    context = m_loadIpInterfacesTimer.time();
    try {
        LOG.info("Loading Ip Interface");
        Set<InetAddress> duplicatedips = new HashSet<InetAddress>();
        for (OnmsIpInterface ip : m_ipInterfaceDao.findAll()) {
            if (!nodeipmap.containsKey(ip.getNode().getId())) {
                nodeipmap.put(ip.getNode().getId(), new ArrayList<OnmsIpInterface>());
                nodeipprimarymap.put(ip.getNode().getId(), ip);
            }
            nodeipmap.get(ip.getNode().getId()).add(ip);
            if (ip.getIsSnmpPrimary().equals(PrimaryType.PRIMARY)) {
                nodeipprimarymap.put(ip.getNode().getId(), ip);
            }
            if (duplicatedips.contains(ip.getIpAddress())) {
                LOG.info("Loading ip Interface, found duplicated ip {}, skipping ", InetAddressUtils.str(ip.getIpAddress()));
                continue;
            }
            if (ipmap.containsKey(ip.getIpAddress())) {
                LOG.info("Loading ip Interface, found duplicated ip {}, skipping ", InetAddressUtils.str(ip.getIpAddress()));
                duplicatedips.add(ip.getIpAddress());
                continue;
            }
            ipmap.put(ip.getIpAddress(), ip);
        }
        for (InetAddress duplicated : duplicatedips) ipmap.remove(duplicated);
        LOG.info("Ip Interface loaded");
    } catch (Exception e) {
        LOG.error("Exception getting ip list: " + e.getMessage(), e);
    } finally {
        context.stop();
    }
    context = m_loadSnmpInterfacesTimer.time();
    try {
        LOG.info("Loading Snmp Interface");
        for (OnmsSnmpInterface snmp : m_snmpInterfaceDao.findAll()) {
            // Index the SNMP interfaces by node id
            final int nodeId = snmp.getNode().getId();
            List<OnmsSnmpInterface> snmpinterfaces = nodesnmpmap.get(nodeId);
            if (snmpinterfaces == null) {
                snmpinterfaces = new ArrayList<>();
                nodesnmpmap.put(nodeId, snmpinterfaces);
            }
            snmpinterfaces.add(snmp);
        }
        LOG.info("Snmp Interface loaded");
    } catch (Exception e) {
        LOG.error("Exception getting snmp interface list: " + e.getMessage(), e);
    } finally {
        context.stop();
    }
    context = m_loadIpNetToMediaTimer.time();
    try {
        Set<String> duplicatednodemac = new HashSet<String>();
        Map<String, Integer> mactonodemap = new HashMap<String, Integer>();
        LOG.info("Loading ip net to media");
        for (IpNetToMedia ipnettomedia : m_ipNetToMediaDao.findAll()) {
            if (duplicatednodemac.contains(ipnettomedia.getPhysAddress())) {
                LOG.info("load ip net media: different nodeid found for ip: {} mac: {}. Skipping...", InetAddressUtils.str(ipnettomedia.getNetAddress()), ipnettomedia.getPhysAddress());
                continue;
            }
            OnmsIpInterface ip = ipmap.get(ipnettomedia.getNetAddress());
            if (ip == null) {
                LOG.info("load ip net media: no nodeid found for ip: {} mac: {}. Skipping...", InetAddressUtils.str(ipnettomedia.getNetAddress()), ipnettomedia.getPhysAddress());
                continue;
            }
            if (mactonodemap.containsKey(ipnettomedia.getPhysAddress())) {
                if (mactonodemap.get(ipnettomedia.getPhysAddress()).intValue() != ip.getNode().getId().intValue()) {
                    LOG.info("load ip net media: different nodeid found for ip: {} mac: {}. Skipping...", InetAddressUtils.str(ipnettomedia.getNetAddress()), ipnettomedia.getPhysAddress());
                    duplicatednodemac.add(ipnettomedia.getPhysAddress());
                    continue;
                }
            }
            if (!macipmap.containsKey(ipnettomedia.getPhysAddress())) {
                macipmap.put(ipnettomedia.getPhysAddress(), new ArrayList<OnmsIpInterface>());
                mactonodemap.put(ipnettomedia.getPhysAddress(), ip.getNode().getId());
            }
            macipmap.get(ipnettomedia.getPhysAddress()).add(ip);
        }
        for (String dupmac : duplicatednodemac) macipmap.remove(dupmac);
        LOG.info("Ip net to media loaded");
    } catch (Exception e) {
        LOG.error("Exception getting ip net to media list: " + e.getMessage(), e);
    } finally {
        context.stop();
    }
    context = m_loadLldpLinksTimer.time();
    try {
        LOG.info("Loading Lldp link");
        getLldpLinks(nodemap, nodesnmpmap, nodeipprimarymap);
        LOG.info("Lldp link loaded");
    } catch (Exception e) {
        LOG.error("Exception getting Lldp link: " + e.getMessage(), e);
    } finally {
        context.stop();
    }
    context = m_loadOspfLinksTimer.time();
    try {
        LOG.info("Loading Ospf link");
        getOspfLinks(nodemap, nodesnmpmap, nodeipprimarymap);
        LOG.info("Ospf link loaded");
    } catch (Exception e) {
        LOG.error("Exception getting Ospf link: " + e.getMessage(), e);
    } finally {
        context.stop();
    }
    context = m_loadIsisLinksTimer.time();
    try {
        LOG.info("Loading Cdp link");
        getCdpLinks(nodemap, nodesnmpmap, nodeipprimarymap, ipmap);
        LOG.info("Cdp link loaded");
    } catch (Exception e) {
        LOG.error("Exception getting Cdp link: " + e.getMessage(), e);
    } finally {
        context.stop();
    }
    context = m_loadCdpLinksTimer.time();
    try {
        LOG.info("Loading IsIs link");
        getIsIsLinks(nodesnmpmap, nodeipprimarymap);
        LOG.info("IsIs link loaded");
    } catch (Exception e) {
        LOG.error("Exception getting IsIs link: " + e.getMessage(), e);
    } finally {
        context.stop();
    }
    context = m_loadBridgeLinksTimer.time();
    try {
        LOG.info("Loading Bridge link");
        getBridgeLinks(nodemap, nodesnmpmap, macipmap, nodeipmap, nodeipprimarymap);
        LOG.info("Bridge link loaded");
    } catch (Exception e) {
        LOG.error("Exception getting Bridge link: " + e.getMessage(), e);
    } finally {
        context.stop();
    }
    context = m_loadNoLinksTimer.time();
    try {
        LOG.debug("loadtopology: adding nodes without links: " + isAddNodeWithoutLink());
        if (isAddNodeWithoutLink()) {
            addNodesWithoutLinks(nodemap, nodeipmap, nodeipprimarymap);
        }
    } finally {
        context.stop();
    }
    LOG.debug("Found {} groups", getGroups().size());
    LOG.debug("Found {} vertices", getVerticesWithoutGroups().size());
    LOG.debug("Found {} edges", getEdges().size());
}
Also used : HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) JAXBException(javax.xml.bind.JAXBException) MalformedURLException(java.net.MalformedURLException) IpNetToMedia(org.opennms.netmgt.model.IpNetToMedia) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) Timer(com.codahale.metrics.Timer) InetAddress(java.net.InetAddress)

Example 7 with IpNetToMedia

use of org.opennms.netmgt.model.IpNetToMedia in project opennms by OpenNMS.

the class NodeDiscoveryIpNetToMedia method runCollection.

protected void runCollection() {
    final Date now = new Date();
    IpNetToMediaTableTracker ipNetToMediaTableTracker = new IpNetToMediaTableTracker() {

        public void processIpNetToMediaRow(final IpNetToMediaRow row) {
            IpNetToMedia macep = row.getIpNetToMedia();
            if (macep.getPhysAddress() == null && macep.getNetAddress() == null) {
                LOG.debug("processIpNetToMediaRow: node [{}], null:null:{}. ip and mac addresses null. skipping", getNodeId(), macep.getIpNetToMediaType());
            } else if (macep.getPhysAddress() == null) {
                LOG.debug("processIpNetToMediaRow: node [{}], null:{}:{}. mac address null. skipping", getNodeId(), str(macep.getNetAddress()), macep.getIpNetToMediaType());
            } else if (macep.getNetAddress() == null) {
                LOG.warn("processIpNetToMediaRow: node [{}], {}:null:{}. ip address null. skipping", getNodeId(), macep.getPhysAddress(), macep.getIpNetToMediaType());
            } else if (macep.getIpNetToMediaType() == IpNetToMediaType.IPNETTOMEDIA_TYPE_DYNAMIC || macep.getIpNetToMediaType() == IpNetToMediaType.IPNETTOMEDIA_TYPE_STATIC) {
                LOG.debug("processIpNetToMediaRow: node [{}], mac address {} and ip {} mediatype {}. saving", getNodeId(), macep.getPhysAddress(), str(macep.getNetAddress()), macep.getIpNetToMediaType());
                m_linkd.getQueryManager().store(getNodeId(), macep);
            } else {
                LOG.warn("processIpNetToMediaRow: node [{}],  {}:{}:{}. mediatype not valid. skipping", getNodeId(), macep.getPhysAddress(), str(macep.getNetAddress()), macep.getIpNetToMediaType());
            }
        }
    };
    SnmpAgentConfig peer = m_linkd.getSnmpAgentConfig(getPrimaryIpAddress(), getLocation());
    try {
        m_linkd.getLocationAwareSnmpClient().walk(peer, ipNetToMediaTableTracker).withDescription("ipNetToMedia").withLocation(getLocation()).execute().get();
    } catch (ExecutionException e) {
        LOG.info("run: node [{}]: Agent error while scanning the ipNetToMedia table", getNodeId(), e);
        return;
    } catch (final InterruptedException e) {
        LOG.info("run: [{}]: collection interrupted, exiting", getNodeId(), e);
        return;
    }
    m_linkd.getQueryManager().reconcileIpNetToMedia(getNodeId(), now);
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) IpNetToMedia(org.opennms.netmgt.model.IpNetToMedia) ExecutionException(java.util.concurrent.ExecutionException) IpNetToMediaTableTracker(org.opennms.netmgt.enlinkd.snmp.IpNetToMediaTableTracker) Date(java.util.Date)

Example 8 with IpNetToMedia

use of org.opennms.netmgt.model.IpNetToMedia in project opennms by OpenNMS.

the class Nms4930NetworkBuilder method addMacNodeWithSnmpInterface.

public void addMacNodeWithSnmpInterface(String mac, String ip, Integer ifindex) {
    NetworkBuilder nb = getNetworkBuilder();
    nb.addNode(ip).setForeignSource("linkd").setForeignId(ip).setType(NodeType.ACTIVE);
    nb.addInterface(ip).setIsSnmpPrimary("N").setIsManaged("M").addSnmpInterface(ifindex).setIfName("eth0").setIfType(6).setPhysAddr(mac).setIfDescr("eth0");
    m_nodeDao.save(nb.getCurrentNode());
    m_nodeDao.flush();
    IpNetToMedia at0 = new IpNetToMedia();
    at0.setSourceIfIndex(100);
    at0.setPhysAddress(mac);
    at0.setLastPollTime(at0.getCreateTime());
    at0.setSourceNode(m_nodeDao.findByForeignId("linkd", ip));
    try {
        at0.setNetAddress(InetAddress.getByName(ip));
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    at0.setIpNetToMediaType(IpNetToMediaType.IPNETTOMEDIA_TYPE_DYNAMIC);
    m_ipNetToMediaDao.saveOrUpdate(at0);
    m_ipNetToMediaDao.flush();
}
Also used : NetworkBuilder(org.opennms.netmgt.model.NetworkBuilder) IpNetToMedia(org.opennms.netmgt.model.IpNetToMedia) UnknownHostException(java.net.UnknownHostException)

Example 9 with IpNetToMedia

use of org.opennms.netmgt.model.IpNetToMedia in project opennms by OpenNMS.

the class EnLinkdBuilderITCase method tearDown.

@After
public void tearDown() throws Exception {
    for (final IpNetToMedia at : m_ipNetToMediaDao.findAll()) m_ipNetToMediaDao.delete(at);
    m_ipNetToMediaDao.flush();
    for (final BridgeBridgeLink bb : m_bridgeBridgeLinkDao.findAll()) m_bridgeBridgeLinkDao.delete(bb);
    m_bridgeBridgeLinkDao.flush();
    for (final OnmsNode node : m_nodeDao.findAll()) m_nodeDao.delete(node);
    m_nodeDao.flush();
}
Also used : IpNetToMedia(org.opennms.netmgt.model.IpNetToMedia) OnmsNode(org.opennms.netmgt.model.OnmsNode) BridgeBridgeLink(org.opennms.netmgt.model.BridgeBridgeLink) After(org.junit.After)

Example 10 with IpNetToMedia

use of org.opennms.netmgt.model.IpNetToMedia in project opennms by OpenNMS.

the class Nms4930NetworkBuilder method addMacNode.

public void addMacNode(String mac, String ip) {
    NetworkBuilder nb = getNetworkBuilder();
    nb.addNode(ip).setForeignSource("linkd").setForeignId(ip).setType(NodeType.ACTIVE);
    nb.addInterface(ip).setIsSnmpPrimary("N").setIsManaged("M");
    m_nodeDao.save(nb.getCurrentNode());
    m_nodeDao.flush();
    IpNetToMedia at0 = new IpNetToMedia();
    at0.setSourceIfIndex(100);
    at0.setPhysAddress(mac);
    at0.setLastPollTime(at0.getCreateTime());
    at0.setSourceNode(m_nodeDao.findByForeignId("linkd", ip));
    try {
        at0.setNetAddress(InetAddress.getByName(ip));
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    at0.setIpNetToMediaType(IpNetToMediaType.IPNETTOMEDIA_TYPE_DYNAMIC);
    m_ipNetToMediaDao.saveOrUpdate(at0);
    m_ipNetToMediaDao.flush();
}
Also used : NetworkBuilder(org.opennms.netmgt.model.NetworkBuilder) IpNetToMedia(org.opennms.netmgt.model.IpNetToMedia) UnknownHostException(java.net.UnknownHostException)

Aggregations

IpNetToMedia (org.opennms.netmgt.model.IpNetToMedia)10 ArrayList (java.util.ArrayList)5 InetAddress (java.net.InetAddress)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)4 List (java.util.List)3 OnmsSnmpInterface (org.opennms.netmgt.model.OnmsSnmpInterface)3 UnknownHostException (java.net.UnknownHostException)2 IpNetToMediaTableTracker (org.opennms.netmgt.enlinkd.snmp.IpNetToMediaTableTracker)2 BridgeBridgeLink (org.opennms.netmgt.model.BridgeBridgeLink)2 BridgeMacLink (org.opennms.netmgt.model.BridgeMacLink)2 NetworkBuilder (org.opennms.netmgt.model.NetworkBuilder)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 BridgePort (org.opennms.netmgt.model.topology.BridgePort)2 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Timer (com.codahale.metrics.Timer)1 MalformedURLException (java.net.MalformedURLException)1 Date (java.util.Date)1