Search in sources :

Example 1 with IpNetToMedia

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

the class EnLinkdSnmpIT method testIpNetToMediaTableWalk.

@Test
@JUnitSnmpAgents(value = { @JUnitSnmpAgent(host = MIKROTIK_IP, port = 161, resource = MIKROTIK_SNMP_RESOURCE) })
public void testIpNetToMediaTableWalk() throws Exception {
    final List<IpNetToMedia> rows = new ArrayList<IpNetToMedia>();
    String trackerName = "ipNetToMediaTable";
    SnmpAgentConfig config = SnmpPeerFactory.getInstance().getAgentConfig(InetAddress.getByName(MIKROTIK_IP));
    IpNetToMediaTableTracker tracker = new IpNetToMediaTableTracker() {

        public void processIpNetToMediaRow(final IpNetToMediaRow row) {
            rows.add(row.getIpNetToMedia());
        }
    };
    try {
        m_client.walk(config, tracker).withDescription(trackerName).withLocation(null).execute().get();
    } catch (final InterruptedException e) {
        LOG.error("run: collection interrupted, exiting", e);
        return;
    }
    assertEquals(6, rows.size());
    for (final IpNetToMedia row : rows) {
        assertEquals(IpNetToMediaType.IPNETTOMEDIA_TYPE_DYNAMIC, row.getIpNetToMediaType());
        if (row.getPhysAddress().equals("00901a4222f8")) {
            assertEquals(InetAddressUtils.addr("10.129.16.1"), row.getNetAddress());
            assertEquals(1, row.getSourceIfIndex().intValue());
        } else if (row.getPhysAddress().equals("0013c8f1d242")) {
            assertEquals(InetAddressUtils.addr("10.129.16.164"), row.getNetAddress());
            assertEquals(1, row.getSourceIfIndex().intValue());
        } else if (row.getPhysAddress().equals("f0728c99994d")) {
            assertEquals(InetAddressUtils.addr("192.168.0.13"), row.getNetAddress());
            assertEquals(2, row.getSourceIfIndex().intValue());
        } else if (row.getPhysAddress().equals("0015999f07ef")) {
            assertEquals(InetAddressUtils.addr("192.168.0.14"), row.getNetAddress());
            assertEquals(2, row.getSourceIfIndex().intValue());
        } else if (row.getPhysAddress().equals("60334b0817a8")) {
            assertEquals(InetAddressUtils.addr("192.168.0.16"), row.getNetAddress());
            assertEquals(2, row.getSourceIfIndex().intValue());
        } else if (row.getPhysAddress().equals("001b63cda9fd")) {
            assertEquals(InetAddressUtils.addr("192.168.0.17"), row.getNetAddress());
            assertEquals(2, row.getSourceIfIndex().intValue());
        } else {
            assertEquals(false, true);
        }
    }
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) IpNetToMedia(org.opennms.netmgt.model.IpNetToMedia) ArrayList(java.util.ArrayList) IpNetToMediaTableTracker(org.opennms.netmgt.enlinkd.snmp.IpNetToMediaTableTracker) Test(org.junit.Test) JUnitSnmpAgents(org.opennms.core.test.snmp.annotations.JUnitSnmpAgents)

Example 2 with IpNetToMedia

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

the class Nms4930EnIT method setUpNetwork4930.

@Before
public void setUpNetwork4930() throws Exception {
    builder.setNodeDao(m_nodeDao);
    builder.setIpNetToMediaDao(m_ipNetToMediaDao);
    builder.buildNetwork4930();
    // Adding a "node" with mac address 001e58a6aed7 found both on dlink1 port 24 and dlink2 port 10 
    builder.addMacNodeWithSnmpInterface("001e58a6aed7", "10.100.1.7", 101);
    // Adding a "node" with mac address 000ffeb10e26 found on dlink1 port 6
    builder.addMacNode("000ffeb10e26", "10.100.2.6");
    assertEquals(4, m_nodeDao.countAll());
    assertEquals(2, m_ipNetToMediaDao.countAll());
    IpNetToMedia at0 = m_ipNetToMediaDao.findByPhysAddress("001e58a6aed7").get(0);
    assertNotNull(at0);
    assertEquals("10.100.1.7", at0.getNetAddress().getHostAddress());
    IpNetToMedia at1 = m_ipNetToMediaDao.findByPhysAddress("000ffeb10e26").get(0);
    assertNotNull(at1);
    assertEquals("10.100.2.6", at1.getNetAddress().getHostAddress());
}
Also used : IpNetToMedia(org.opennms.netmgt.model.IpNetToMedia) Before(org.junit.Before)

Example 3 with IpNetToMedia

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

the class EnLinkdElementFactory method convertFromModel.

@Transactional
@SuppressWarnings("deprecation")
private NodeLinkBridge convertFromModel(String mac, SharedSegment segment, String port) {
    final NodeLinkBridge linknode = new NodeLinkBridge();
    linknode.setNodeLocalPort(port);
    for (BridgePort link : segment.getBridgePortsOnSegment()) {
        final BridgeLinkRemoteNode remlinknode = new BridgeLinkRemoteNode();
        final Integer rempnodeId = link.getNode().getId();
        final Integer rembridgePortIfIndex = link.getBridgePortIfIndex();
        remlinknode.setBridgeRemoteNode(link.getNode().getLabel());
        remlinknode.setBridgeRemoteUrl(getNodeUrl(rempnodeId));
        final OnmsSnmpInterface remiface = rembridgePortIfIndex == null ? null : m_snmpInterfaceDao.findByNodeIdAndIfIndex(rempnodeId, rembridgePortIfIndex);
        if (remiface != null) {
            remlinknode.setBridgeRemotePort(getPortString(rembridgePortIfIndex, remiface.getIfName(), remiface.getIfAlias()));
        } else {
            remlinknode.setBridgeRemotePort(getPortString(rembridgePortIfIndex, null, null));
        }
        remlinknode.setBridgeRemotePortUrl(getSnmpInterfaceUrl(rempnodeId, rembridgePortIfIndex));
        remlinknode.setBridgeRemoteVlan(link.getVlan());
        linknode.getBridgeLinkRemoteNodes().add(remlinknode);
    }
    for (BridgeMacLink link : segment.getBridgeMacLinks()) {
        if (link.getMacAddress().equals(mac)) {
            linknode.setBridgeLinkCreateTime(Util.formatDateToUIString(link.getBridgeMacLinkCreateTime()));
            linknode.setBridgeLinkLastPollTime(Util.formatDateToUIString(link.getBridgeMacLinkLastPollTime()));
            break;
        }
    }
    Map<String, List<IpNetToMedia>> sharedmacs = new HashMap<String, List<IpNetToMedia>>();
    for (String shredmac : segment.getMacsOnSegment()) {
        if (shredmac.equals(mac))
            continue;
        sharedmacs.put(shredmac, new ArrayList<IpNetToMedia>());
        sharedmacs.get(shredmac).addAll(m_ipNetToMediaDao.findByPhysAddress(shredmac));
    }
    Map<String, List<OnmsIpInterface>> sharedhosts = new HashMap<String, List<OnmsIpInterface>>();
    for (String shredmac : sharedmacs.keySet()) {
        if (sharedmacs.get(shredmac).isEmpty()) {
            BridgeLinkSharedHost remlinknode = new BridgeLinkSharedHost();
            OnmsSnmpInterface snmp = getFromPhysAddress(shredmac);
            if (snmp == null) {
                remlinknode.setSharedHost(shredmac + " No ip address found");
            } else {
                remlinknode.setSharedHost(snmp.getNode().getLabel());
                remlinknode.setSharedHostUrl(getNodeUrl(snmp.getNode().getId()));
                remlinknode.setSharedHostPort(getPortString(snmp.getIfIndex(), snmp.getIfName(), snmp.getIfAlias()));
                remlinknode.setSharedHostPortUrl(getSnmpInterfaceUrl(snmp.getNode().getId(), snmp.getIfIndex()));
            }
            linknode.getBridgeLinkSharedHost().add(remlinknode);
            continue;
        }
        sharedhosts.put(shredmac, new ArrayList<OnmsIpInterface>());
        for (IpNetToMedia ipnettomedia : sharedmacs.get(shredmac)) sharedhosts.get(shredmac).addAll(m_ipInterfaceDao.findByIpAddress(ipnettomedia.getNetAddress().getHostAddress()));
    }
    for (String shredmac : sharedhosts.keySet()) {
        BridgeLinkSharedHost remlinknode = new BridgeLinkSharedHost();
        Set<InetAddress> ips = new HashSet<InetAddress>();
        if (sharedhosts.get(shredmac).isEmpty()) {
            for (IpNetToMedia ipnettomedia : sharedmacs.get(shredmac)) {
                ips.add(ipnettomedia.getNetAddress());
            }
            remlinknode.setSharedHost(getNodePortString(ips, shredmac) + " No node found");
            linknode.getBridgeLinkSharedHost().add(remlinknode);
            continue;
        }
        OnmsIpInterface first = null;
        boolean multiplenodeids = false;
        for (OnmsIpInterface ip : sharedhosts.get(shredmac)) {
            if (first == null)
                first = ip;
            if (first.getNode().getId().intValue() != ip.getNode().getId().intValue())
                multiplenodeids = true;
            ips.add(ip.getIpAddress());
        }
        if (multiplenodeids) {
            remlinknode.setSharedHost(getNodePortString(ips, shredmac) + " duplicated ip multiple node associated in db");
        } else {
            remlinknode.setSharedHost(first.getNode().getLabel());
            remlinknode.setSharedHostUrl(getNodeUrl(first.getNode().getId()));
        }
        remlinknode.setSharedHostPort(getNodePortString(ips, shredmac));
        if (ips.size() == 1) {
            remlinknode.setSharedHostPortUrl(getIpInterfaceUrl(first));
        }
        linknode.getBridgeLinkSharedHost().add(remlinknode);
    }
    return linknode;
}
Also used : BridgePort(org.opennms.netmgt.model.topology.BridgePort) HashMap(java.util.HashMap) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) IpNetToMedia(org.opennms.netmgt.model.IpNetToMedia) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) List(java.util.List) ArrayList(java.util.ArrayList) BridgeMacLink(org.opennms.netmgt.model.BridgeMacLink) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with IpNetToMedia

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

the class EnLinkdElementFactory method convertFromModel.

@Transactional
@SuppressWarnings("deprecation")
private BridgeLinkNode convertFromModel(int nodeid, SharedSegment segment) {
    final BridgeLinkNode linknode = new BridgeLinkNode();
    for (BridgePort link : segment.getBridgePortsOnSegment()) {
        final Integer rempnodeId = link.getNode().getId();
        final Integer rembridgePortIfIndex = link.getBridgePortIfIndex();
        final OnmsSnmpInterface remiface = rembridgePortIfIndex == null ? null : m_snmpInterfaceDao.findByNodeIdAndIfIndex(rempnodeId, rembridgePortIfIndex);
        if (link.getNode().getId().intValue() == nodeid) {
            if (remiface != null) {
                linknode.setNodeLocalPort(getPortString(rembridgePortIfIndex, remiface.getIfName(), remiface.getIfAlias()));
            } else {
                linknode.setNodeLocalPort(getPortString(rembridgePortIfIndex, null, null));
            }
            linknode.setBridgeLocalVlan(link.getVlan());
            continue;
        }
        final BridgeLinkRemoteNode remlinknode = new BridgeLinkRemoteNode();
        remlinknode.setBridgeRemoteNode(link.getNode().getLabel());
        remlinknode.setBridgeRemoteUrl(getNodeUrl(rempnodeId));
        if (remiface != null) {
            remlinknode.setBridgeRemotePort(getPortString(rembridgePortIfIndex, remiface.getIfName(), remiface.getIfAlias()));
        } else {
            remlinknode.setBridgeRemotePort(getPortString(rembridgePortIfIndex, null, null));
        }
        remlinknode.setBridgeRemotePortUrl(getSnmpInterfaceUrl(rempnodeId, rembridgePortIfIndex));
        remlinknode.setBridgeRemoteVlan(link.getVlan());
        linknode.getBridgeLinkRemoteNodes().add(remlinknode);
    }
    if (segment.getBridgeBridgeLinks().isEmpty()) {
        for (BridgeMacLink link : segment.getBridgeMacLinks()) {
            if (link.getNode().getId().intValue() == nodeid) {
                linknode.setBridgeLinkCreateTime(Util.formatDateToUIString(link.getBridgeMacLinkCreateTime()));
                linknode.setBridgeLinkLastPollTime(Util.formatDateToUIString(link.getBridgeMacLinkLastPollTime()));
                break;
            }
        }
    } else {
        for (BridgeBridgeLink link : segment.getBridgeBridgeLinks()) {
            if (link.getNode().getId().intValue() == nodeid || link.getDesignatedNode().getId().intValue() == nodeid) {
                linknode.setBridgeLinkCreateTime(Util.formatDateToUIString(link.getBridgeBridgeLinkCreateTime()));
                linknode.setBridgeLinkLastPollTime(Util.formatDateToUIString(link.getBridgeBridgeLinkLastPollTime()));
                break;
            }
        }
    }
    Map<String, List<IpNetToMedia>> sharedmacs = new HashMap<String, List<IpNetToMedia>>();
    for (String shredmac : segment.getMacsOnSegment()) {
        sharedmacs.put(shredmac, new ArrayList<IpNetToMedia>());
        sharedmacs.get(shredmac).addAll(m_ipNetToMediaDao.findByPhysAddress(shredmac));
    }
    Map<String, List<OnmsIpInterface>> sharedhosts = new HashMap<String, List<OnmsIpInterface>>();
    for (String shredmac : sharedmacs.keySet()) {
        if (sharedmacs.get(shredmac).isEmpty()) {
            BridgeLinkSharedHost remlinknode = new BridgeLinkSharedHost();
            OnmsSnmpInterface snmp = getFromPhysAddress(shredmac);
            if (snmp == null) {
                remlinknode.setSharedHost(shredmac + " No ip address found");
            } else {
                remlinknode.setSharedHost(snmp.getNode().getLabel());
                remlinknode.setSharedHostUrl(getNodeUrl(snmp.getNode().getId()));
                remlinknode.setSharedHostPort(getPortString(snmp.getIfIndex(), snmp.getIfName(), snmp.getIfAlias()));
                remlinknode.setSharedHostPortUrl(getSnmpInterfaceUrl(snmp.getNode().getId(), snmp.getIfIndex()));
            }
            linknode.getBridgeLinkSharedHost().add(remlinknode);
            continue;
        }
        sharedhosts.put(shredmac, new ArrayList<OnmsIpInterface>());
        for (IpNetToMedia ipnettomedia : sharedmacs.get(shredmac)) sharedhosts.get(shredmac).addAll(m_ipInterfaceDao.findByIpAddress(ipnettomedia.getNetAddress().getHostAddress()));
    }
    for (String shredmac : sharedhosts.keySet()) {
        BridgeLinkSharedHost remlinknode = new BridgeLinkSharedHost();
        Set<InetAddress> ips = new HashSet<InetAddress>();
        if (sharedhosts.get(shredmac).isEmpty()) {
            for (IpNetToMedia ipnettomedia : sharedmacs.get(shredmac)) {
                ips.add(ipnettomedia.getNetAddress());
            }
            remlinknode.setSharedHost(getNodePortString(ips, shredmac) + " No node found");
            linknode.getBridgeLinkSharedHost().add(remlinknode);
            continue;
        }
        OnmsIpInterface first = null;
        boolean multiplenodeids = false;
        for (OnmsIpInterface ip : sharedhosts.get(shredmac)) {
            if (first == null)
                first = ip;
            if (first.getNode().getId().intValue() != ip.getNode().getId().intValue())
                multiplenodeids = true;
            ips.add(ip.getIpAddress());
        }
        if (multiplenodeids) {
            remlinknode.setSharedHost(getNodePortString(ips, shredmac) + " duplicated ip multiple node associated in db");
        } else {
            remlinknode.setSharedHost(first.getNode().getLabel());
            remlinknode.setSharedHostUrl(getNodeUrl(first.getNode().getId()));
        }
        remlinknode.setSharedHostPort(getNodePortString(ips, shredmac));
        if (ips.size() == 1) {
            remlinknode.setSharedHostPortUrl(getIpInterfaceUrl(first));
        }
        linknode.getBridgeLinkSharedHost().add(remlinknode);
    }
    return linknode;
}
Also used : BridgePort(org.opennms.netmgt.model.topology.BridgePort) HashMap(java.util.HashMap) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) IpNetToMedia(org.opennms.netmgt.model.IpNetToMedia) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) BridgeBridgeLink(org.opennms.netmgt.model.BridgeBridgeLink) List(java.util.List) ArrayList(java.util.ArrayList) BridgeMacLink(org.opennms.netmgt.model.BridgeMacLink) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with IpNetToMedia

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

the class EnLinkdElementFactory method getNodeLinks.

@Override
public Collection<NodeLinkBridge> getNodeLinks(int nodeId) {
    Map<String, Set<InetAddress>> mactoIpMap = new HashMap<String, Set<InetAddress>>();
    for (OnmsIpInterface ip : m_ipInterfaceDao.findByNodeId(nodeId)) {
        for (IpNetToMedia ipnetomedia : m_ipNetToMediaDao.findByNetAddress(ip.getIpAddress())) {
            if (!mactoIpMap.containsKey(ipnetomedia.getPhysAddress()))
                mactoIpMap.put(ipnetomedia.getPhysAddress(), new HashSet<InetAddress>());
            mactoIpMap.get(ipnetomedia.getPhysAddress()).add(ip.getIpAddress());
        }
    }
    List<NodeLinkBridge> nodelinks = new ArrayList<NodeLinkBridge>();
    for (String mac : mactoIpMap.keySet()) {
        SharedSegment segment = m_bridgetopologyDao.getHostNodeSharedSegment(m_bridgeBridgeLinkDao, m_bridgeMacLinkDao, mac);
        if (segment.isEmpty())
            continue;
        if (!segment.containsMac(mac))
            continue;
        nodelinks.add(convertFromModel(mac, segment, getNodePortString(mactoIpMap.get(mac), mac)));
    }
    Collections.sort(nodelinks);
    return nodelinks;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) IpNetToMedia(org.opennms.netmgt.model.IpNetToMedia) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SharedSegment(org.opennms.netmgt.model.topology.SharedSegment) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet)

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