Search in sources :

Example 1 with CollectionSetWithAgent

use of org.opennms.netmgt.telemetry.adapters.collection.CollectionSetWithAgent in project opennms by OpenNMS.

the class NxosGpbAdapter method handleMessage.

@Override
public Optional<CollectionSetWithAgent> handleMessage(TelemetryMessage message, TelemetryMessageLog messageLog) throws Exception {
    final TelemetryBis.Telemetry msg = tryParsingTelemetryMessage(message.getByteArray());
    CollectionAgent agent = null;
    try {
        LOG.debug(" Telemetry message content : {} ", msg);
        final InetAddress inetAddress = InetAddress.getByName(msg.getNodeIdStr());
        final Optional<Integer> nodeId = interfaceToNodeCache.getFirstNodeId(messageLog.getLocation(), inetAddress);
        if (nodeId.isPresent()) {
            // NOTE: This will throw a IllegalArgumentException if the nodeId/inetAddress pair does not exist in the database
            agent = collectionAgentFactory.createCollectionAgent(Integer.toString(nodeId.get()), inetAddress);
        }
    } catch (UnknownHostException e) {
        LOG.debug("Could not convert system id to address: {}", msg.getNodeIdStr());
    }
    if (agent == null) {
        // We were unable to build the agent by resolving the systemId, try finding
        // a node with a matching label
        agent = transactionTemplate.execute(new TransactionCallback<CollectionAgent>() {

            @Override
            public CollectionAgent doInTransaction(TransactionStatus status) {
                OnmsNode node = Iterables.getFirst(nodeDao.findByLabelForLocation(msg.getNodeIdStr(), messageLog.getLocation()), null);
                if (node == null) {
                    // If there is no matching label , Try matching with foreignId
                    node = Iterables.getFirst(nodeDao.findByForeignIdForLocation(msg.getNodeIdStr(), messageLog.getLocation()), null);
                }
                if (node != null) {
                    final OnmsIpInterface primaryInterface = node.getPrimaryInterface();
                    return collectionAgentFactory.createCollectionAgent(primaryInterface);
                }
                return null;
            }
        });
    }
    if (agent == null) {
        LOG.warn("Unable to find node and interface for system id: {}", msg.getNodeIdStr());
        return Optional.empty();
    }
    final ScriptedCollectionSetBuilder builder = scriptedCollectionSetBuilders.get();
    if (builder == null) {
        throw new Exception(String.format("Error compiling script '%s'. See logs for details.", script));
    }
    final CollectionSet collectionSet = builder.build(agent, msg);
    return Optional.of(new CollectionSetWithAgent(agent, collectionSet));
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) UnknownHostException(java.net.UnknownHostException) TransactionStatus(org.springframework.transaction.TransactionStatus) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) UnknownHostException(java.net.UnknownHostException) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) TransactionCallback(org.springframework.transaction.support.TransactionCallback) ScriptedCollectionSetBuilder(org.opennms.netmgt.telemetry.adapters.collection.ScriptedCollectionSetBuilder) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) TelemetryBis(org.opennms.netmgt.telemetry.adapters.nxos.proto.TelemetryBis) CollectionSetWithAgent(org.opennms.netmgt.telemetry.adapters.collection.CollectionSetWithAgent) Telemetry(org.opennms.netmgt.telemetry.adapters.nxos.proto.TelemetryBis.Telemetry) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) InetAddress(java.net.InetAddress)

Example 2 with CollectionSetWithAgent

use of org.opennms.netmgt.telemetry.adapters.collection.CollectionSetWithAgent in project opennms by OpenNMS.

the class JtiGpbAdapter method handleMessage.

@Override
public Optional<CollectionSetWithAgent> handleMessage(TelemetryMessage message, TelemetryMessageLog messageLog) throws Exception {
    final TelemetryTop.TelemetryStream jtiMsg = TelemetryTop.TelemetryStream.parseFrom(message.getByteArray(), s_registry);
    CollectionAgent agent = null;
    try {
        // Attempt to resolve the systemId to an InetAddress
        final InetAddress inetAddress = InetAddress.getByName(jtiMsg.getSystemId());
        final Optional<Integer> nodeId = interfaceToNodeCache.getFirstNodeId(messageLog.getLocation(), inetAddress);
        if (nodeId.isPresent()) {
            // NOTE: This will throw a IllegalArgumentException if the
            // nodeId/inetAddress pair does not exist in the database
            agent = collectionAgentFactory.createCollectionAgent(Integer.toString(nodeId.get()), inetAddress);
        }
    } catch (UnknownHostException e) {
        LOG.debug("Could not convert system id to address: {}", jtiMsg.getSystemId());
    }
    if (agent == null) {
        // We were unable to build the agent by resolving the systemId,
        // try finding
        // a node with a matching label
        agent = transactionTemplate.execute(new TransactionCallback<CollectionAgent>() {

            @Override
            public CollectionAgent doInTransaction(TransactionStatus status) {
                final OnmsNode node = Iterables.getFirst(nodeDao.findByLabel(jtiMsg.getSystemId()), null);
                if (node != null) {
                    final OnmsIpInterface primaryInterface = node.getPrimaryInterface();
                    return collectionAgentFactory.createCollectionAgent(primaryInterface);
                }
                return null;
            }
        });
    }
    if (agent == null) {
        LOG.warn("Unable to find node and inteface for system id: {}", jtiMsg.getSystemId());
        return Optional.empty();
    }
    final ScriptedCollectionSetBuilder builder = scriptedCollectionSetBuilders.get();
    if (builder == null) {
        throw new Exception(String.format("Error compiling script '%s'. See logs for details.", script));
    }
    final CollectionSet collectionSet = builder.build(agent, jtiMsg);
    return Optional.of(new CollectionSetWithAgent(agent, collectionSet));
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) UnknownHostException(java.net.UnknownHostException) TransactionStatus(org.springframework.transaction.TransactionStatus) UnknownHostException(java.net.UnknownHostException) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) TransactionCallback(org.springframework.transaction.support.TransactionCallback) ScriptedCollectionSetBuilder(org.opennms.netmgt.telemetry.adapters.collection.ScriptedCollectionSetBuilder) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) CollectionSetWithAgent(org.opennms.netmgt.telemetry.adapters.collection.CollectionSetWithAgent) TelemetryTop(org.opennms.netmgt.telemetry.adapters.jti.proto.TelemetryTop) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) InetAddress(java.net.InetAddress)

Aggregations

InetAddress (java.net.InetAddress)2 UnknownHostException (java.net.UnknownHostException)2 CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)2 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)2 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 CollectionSetWithAgent (org.opennms.netmgt.telemetry.adapters.collection.CollectionSetWithAgent)2 ScriptedCollectionSetBuilder (org.opennms.netmgt.telemetry.adapters.collection.ScriptedCollectionSetBuilder)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2 TransactionCallback (org.springframework.transaction.support.TransactionCallback)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 TelemetryTop (org.opennms.netmgt.telemetry.adapters.jti.proto.TelemetryTop)1 TelemetryBis (org.opennms.netmgt.telemetry.adapters.nxos.proto.TelemetryBis)1 Telemetry (org.opennms.netmgt.telemetry.adapters.nxos.proto.TelemetryBis.Telemetry)1