Search in sources :

Example 1 with RpcTarget

use of org.opennms.core.rpc.api.RpcTarget in project opennms by OpenNMS.

the class RpcTargetHelperTest method overridesGivenLocationAndSystemIdUsingAttributes.

@Test
public void overridesGivenLocationAndSystemIdUsingAttributes() {
    final Map<String, Object> attributes = new HashMap<>();
    attributes.put(RpcTargetHelper.LOCATION_KEY, "xx");
    attributes.put(RpcTargetHelper.SYSTEM_ID_KEY, "yy");
    final RpcTarget target = new RpcTargetHelper().target().withLocation("x").withSystemId("y").withServiceAttributes(attributes).build();
    assertThat(target.getLocation(), equalTo("xx"));
    assertThat(target.getSystemId(), equalTo("yy"));
}
Also used : HashMap(java.util.HashMap) RpcTarget(org.opennms.core.rpc.api.RpcTarget) Test(org.junit.Test)

Example 2 with RpcTarget

use of org.opennms.core.rpc.api.RpcTarget in project opennms by OpenNMS.

the class RpcTargetHelperTest method canUseForeignIdAsSystemId.

@Test
public void canUseForeignIdAsSystemId() {
    final Map<String, Object> attributes = new HashMap<>();
    attributes.put(RpcTargetHelper.SYSTEM_ID_KEY, "yy");
    attributes.put(RpcTargetHelper.USE_FOREIGN_ID_AS_SYSTEM_ID_KEY, Boolean.TRUE.toString());
    final OnmsNode node = mock(OnmsNode.class);
    when(node.getForeignId()).thenReturn("aa");
    final NodeDao nodeDao = mock(NodeDao.class);
    when(nodeDao.get(1)).thenReturn(node);
    final RpcTarget target = new RpcTargetHelper().target().withSystemId("y").withServiceAttributes(attributes).withNodeId(1).withNodeDao(nodeDao).build();
    assertThat(target.getLocation(), nullValue());
    assertThat(target.getSystemId(), equalTo("aa"));
}
Also used : NodeDao(org.opennms.netmgt.dao.api.NodeDao) OnmsNode(org.opennms.netmgt.model.OnmsNode) HashMap(java.util.HashMap) RpcTarget(org.opennms.core.rpc.api.RpcTarget) Test(org.junit.Test)

Example 3 with RpcTarget

use of org.opennms.core.rpc.api.RpcTarget in project opennms by OpenNMS.

the class RpcTargetHelperTest method overridesGivenLocationUsingCallback.

@Test
public void overridesGivenLocationUsingCallback() {
    final Map<String, Object> attributes = new HashMap<>();
    attributes.put(RpcTargetHelper.LOCATION_KEY, "xx");
    final RpcTarget target = new RpcTargetHelper().target().withLocation("x").withServiceAttributes(attributes).withLocationOverride(l -> "xx".equals(l) ? "aa" : "bb").build();
    assertThat(target.getLocation(), equalTo("aa"));
    assertThat(target.getSystemId(), nullValue());
}
Also used : NodeDao(org.opennms.netmgt.dao.api.NodeDao) Map(java.util.Map) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.nullValue(org.hamcrest.Matchers.nullValue) RpcTarget(org.opennms.core.rpc.api.RpcTarget) Test(org.junit.Test) HashMap(java.util.HashMap) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Mockito.when(org.mockito.Mockito.when) OnmsNode(org.opennms.netmgt.model.OnmsNode) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) RpcTarget(org.opennms.core.rpc.api.RpcTarget) Test(org.junit.Test)

Example 4 with RpcTarget

use of org.opennms.core.rpc.api.RpcTarget in project opennms by OpenNMS.

the class PollerRequestBuilderImpl method execute.

@Override
public CompletableFuture<PollerResponse> execute() {
    if (serviceMonitor == null) {
        throw new IllegalArgumentException("Monitor or monitor class name is required.");
    } else if (service == null) {
        throw new IllegalArgumentException("Monitored service is required.");
    }
    final RpcTarget target = client.getRpcTargetHelper().target().withNodeId(service.getNodeId()).withLocation(service.getNodeLocation()).withSystemId(systemId).withServiceAttributes(attributes).withLocationOverride((s) -> serviceMonitor.getEffectiveLocation(s)).build();
    final PollerRequestDTO request = new PollerRequestDTO();
    request.setLocation(target.getLocation());
    request.setSystemId(target.getSystemId());
    request.setClassName(serviceMonitor.getClass().getCanonicalName());
    request.setServiceName(service.getSvcName());
    request.setAddress(service.getAddress());
    request.setNodeId(service.getNodeId());
    request.setNodeLabel(service.getNodeLabel());
    request.setNodeLocation(service.getNodeLocation());
    request.setTimeToLiveMs(ttlInMs);
    request.addAttributes(attributes);
    // Retrieve the runtime attributes, which may include attributes
    // such as the agent details and other state related attributes
    // which should be included in the request
    final Map<String, Object> parameters = request.getMonitorParameters();
    request.addAttributes(serviceMonitor.getRuntimeAttributes(request, parameters));
    // Execute the request
    return client.getDelegate().execute(request).thenApply(results -> {
        PollStatus pollStatus = results.getPollStatus();
        // Invoke the adapters in the same order as which they were added
        for (ServiceMonitorAdaptor adaptor : adaptors) {
            // The adapters may update the status
            pollStatus = adaptor.handlePollResult(service, attributes, pollStatus);
        }
        results.setPollStatus(pollStatus);
        return results;
    });
}
Also used : ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) ServiceMonitorAdaptor(org.opennms.netmgt.poller.ServiceMonitorAdaptor) List(java.util.List) PollerResponse(org.opennms.netmgt.poller.PollerResponse) Map(java.util.Map) MonitoredService(org.opennms.netmgt.poller.MonitoredService) RpcTarget(org.opennms.core.rpc.api.RpcTarget) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) LinkedList(java.util.LinkedList) PollerRequestBuilder(org.opennms.netmgt.poller.PollerRequestBuilder) PollStatus(org.opennms.netmgt.poller.PollStatus) PollStatus(org.opennms.netmgt.poller.PollStatus) ServiceMonitorAdaptor(org.opennms.netmgt.poller.ServiceMonitorAdaptor) RpcTarget(org.opennms.core.rpc.api.RpcTarget)

Example 5 with RpcTarget

use of org.opennms.core.rpc.api.RpcTarget in project opennms by OpenNMS.

the class RpcTargetHelperTest method usesGivenLocationAndSystemId.

@Test
public void usesGivenLocationAndSystemId() {
    final RpcTarget target = new RpcTargetHelper().target().withLocation("x").withSystemId("y").build();
    assertThat(target.getLocation(), equalTo("x"));
    assertThat(target.getSystemId(), equalTo("y"));
}
Also used : RpcTarget(org.opennms.core.rpc.api.RpcTarget) Test(org.junit.Test)

Aggregations

RpcTarget (org.opennms.core.rpc.api.RpcTarget)7 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 Map (java.util.Map)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 NodeDao (org.opennms.netmgt.dao.api.NodeDao)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Objects (java.util.Objects)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.nullValue (org.hamcrest.Matchers.nullValue)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.when (org.mockito.Mockito.when)1 CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)1 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)1 CollectorRequestBuilder (org.opennms.netmgt.collection.api.CollectorRequestBuilder)1 ServiceCollector (org.opennms.netmgt.collection.api.ServiceCollector)1 CollectionAgentDTO (org.opennms.netmgt.collection.dto.CollectionAgentDTO)1