Search in sources :

Example 1 with PeerDTO

use of org.apache.nifi.web.api.dto.remote.PeerDTO in project nifi by apache.

the class SiteToSiteResource method getPeers.

/**
 * Returns the available Peers and its status of this NiFi.
 *
 * @return A peersEntity.
 */
@GET
@Path("/peers")
@Consumes(MediaType.WILDCARD)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@ApiOperation(value = "Returns the available Peers and its status of this NiFi", response = PeersEntity.class, authorizations = { @Authorization(value = "Read - /site-to-site") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response getPeers(@Context HttpServletRequest req) {
    authorizeSiteToSite();
    if (!properties.isSiteToSiteHttpEnabled()) {
        return responseCreator.httpSiteToSiteIsNotEnabledResponse();
    }
    final Integer transportProtocolVersion;
    try {
        transportProtocolVersion = negotiateTransportProtocolVersion(req, transportProtocolVersionNegotiator);
    } catch (BadRequestException e) {
        return responseCreator.badRequestResponse(e);
    }
    final List<PeerDTO> peers = new ArrayList<>();
    if (properties.isNode()) {
        try {
            final Map<NodeIdentifier, NodeWorkload> clusterWorkload = clusterCoordinator.getClusterWorkload();
            clusterWorkload.entrySet().stream().forEach(entry -> {
                final PeerDTO peer = new PeerDTO();
                final NodeIdentifier nodeId = entry.getKey();
                final String siteToSiteAddress = nodeId.getSiteToSiteAddress();
                peer.setHostname(siteToSiteAddress == null ? nodeId.getApiAddress() : siteToSiteAddress);
                peer.setPort(nodeId.getSiteToSiteHttpApiPort() == null ? nodeId.getApiPort() : nodeId.getSiteToSiteHttpApiPort());
                peer.setSecure(nodeId.isSiteToSiteSecure());
                peer.setFlowFileCount(entry.getValue().getFlowFileCount());
                peers.add(peer);
            });
        } catch (IOException e) {
            throw new RuntimeException("Failed to retrieve cluster workload due to " + e, e);
        }
    } else {
        // Standalone mode.
        final PeerDTO peer = new PeerDTO();
        // Private IP address or hostname may not be accessible from client in some environments.
        // So, use the value defined in nifi.properties instead when it is defined.
        final String remoteInputHost = properties.getRemoteInputHost();
        String localName;
        try {
            // Get local host name using InetAddress if available, same as RAW socket does.
            localName = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to get local host name using InetAddress.", e);
            }
            localName = req.getLocalName();
        }
        peer.setHostname(isEmpty(remoteInputHost) ? localName : remoteInputHost);
        peer.setPort(properties.getRemoteInputHttpPort());
        peer.setSecure(properties.isSiteToSiteSecure());
        // doesn't matter how many FlowFiles we have, because we're the only host.
        peer.setFlowFileCount(0);
        peers.add(peer);
    }
    final PeersEntity entity = new PeersEntity();
    entity.setPeers(peers);
    return noCache(setCommonHeaders(Response.ok(entity), transportProtocolVersion, transactionManager)).build();
}
Also used : UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) NodeWorkload(org.apache.nifi.cluster.coordination.node.NodeWorkload) IOException(java.io.IOException) PeerDTO(org.apache.nifi.web.api.dto.remote.PeerDTO) PeersEntity(org.apache.nifi.web.api.entity.PeersEntity) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) BadRequestException(org.apache.nifi.remote.exception.BadRequestException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with PeerDTO

use of org.apache.nifi.web.api.dto.remote.PeerDTO in project nifi by apache.

the class HttpClient method fetchRemotePeerStatuses.

@Override
public Set<PeerStatus> fetchRemotePeerStatuses(PeerDescription peerDescription) throws IOException {
    // Each node should has the same URL structure and network reach-ability with the proxy configuration.
    try (final SiteToSiteRestApiClient apiClient = new SiteToSiteRestApiClient(config.getSslContext(), config.getHttpProxy(), config.getEventReporter())) {
        final String scheme = peerDescription.isSecure() ? "https" : "http";
        apiClient.setBaseUrl(scheme, peerDescription.getHostname(), peerDescription.getPort());
        final int timeoutMillis = (int) config.getTimeout(TimeUnit.MILLISECONDS);
        apiClient.setConnectTimeoutMillis(timeoutMillis);
        apiClient.setReadTimeoutMillis(timeoutMillis);
        apiClient.setCacheExpirationMillis(config.getCacheExpiration(TimeUnit.MILLISECONDS));
        apiClient.setLocalAddress(config.getLocalAddress());
        final Collection<PeerDTO> peers = apiClient.getPeers();
        if (peers == null || peers.size() == 0) {
            throw new IOException("Couldn't get any peer to communicate with. " + apiClient.getBaseUrl() + " returned zero peers.");
        }
        // was added in NiFi 1.0.0, which means that peer-to-peer queries are always allowed.
        return peers.stream().map(p -> new PeerStatus(new PeerDescription(p.getHostname(), p.getPort(), p.isSecure()), p.getFlowFileCount(), true)).collect(Collectors.toSet());
    }
}
Also used : PortNotRunningException(org.apache.nifi.remote.exception.PortNotRunningException) LoggerFactory(org.slf4j.LoggerFactory) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) StringUtils(org.apache.commons.lang3.StringUtils) SiteToSiteClientConfig(org.apache.nifi.remote.client.SiteToSiteClientConfig) HashSet(java.util.HashSet) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) PeerStatus(org.apache.nifi.remote.PeerStatus) PeerStatusProvider(org.apache.nifi.remote.client.PeerStatusProvider) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) URI(java.net.URI) PeerSelector(org.apache.nifi.remote.client.PeerSelector) ThreadFactory(java.util.concurrent.ThreadFactory) CommunicationsSession(org.apache.nifi.remote.protocol.CommunicationsSession) AbstractSiteToSiteClient(org.apache.nifi.remote.client.AbstractSiteToSiteClient) TransferDirection(org.apache.nifi.remote.TransferDirection) Logger(org.slf4j.Logger) PeerDescription(org.apache.nifi.remote.PeerDescription) UnknownPortException(org.apache.nifi.remote.exception.UnknownPortException) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Transaction(org.apache.nifi.remote.Transaction) Executors(java.util.concurrent.Executors) ProtocolException(org.apache.nifi.remote.exception.ProtocolException) TimeUnit(java.util.concurrent.TimeUnit) HttpClientTransaction(org.apache.nifi.remote.protocol.http.HttpClientTransaction) PeerDTO(org.apache.nifi.web.api.dto.remote.PeerDTO) Peer(org.apache.nifi.remote.Peer) Collections(java.util.Collections) HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession) PeerDescription(org.apache.nifi.remote.PeerDescription) PeerStatus(org.apache.nifi.remote.PeerStatus) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) IOException(java.io.IOException) PeerDTO(org.apache.nifi.web.api.dto.remote.PeerDTO)

Example 3 with PeerDTO

use of org.apache.nifi.web.api.dto.remote.PeerDTO in project nifi by apache.

the class TestHttpClient method before.

@Before
public void before() throws Exception {
    System.setProperty("org.slf4j.simpleLogger.log.org.apache.nifi.remote", "TRACE");
    System.setProperty("org.slf4j.simpleLogger.log.org.apache.nifi.remote.protocol.http.HttpClientTransaction", "DEBUG");
    testCaseFinished = new CountDownLatch(1);
    final PeerDTO peer = new PeerDTO();
    peer.setHostname("localhost");
    peer.setPort(httpConnector.getLocalPort());
    peer.setFlowFileCount(10);
    peer.setSecure(false);
    peers = new HashSet<>();
    peers.add(peer);
    final PeerDTO peerSecure = new PeerDTO();
    peerSecure.setHostname("localhost");
    peerSecure.setPort(sslConnector.getLocalPort());
    peerSecure.setFlowFileCount(10);
    peerSecure.setSecure(true);
    peersSecure = new HashSet<>();
    peersSecure.add(peerSecure);
    inputPorts = new HashSet<>();
    final PortDTO runningInputPort = new PortDTO();
    runningInputPort.setName("input-running");
    runningInputPort.setId("input-running-id");
    runningInputPort.setType("INPUT_PORT");
    runningInputPort.setState(ScheduledState.RUNNING.name());
    inputPorts.add(runningInputPort);
    final PortDTO timeoutInputPort = new PortDTO();
    timeoutInputPort.setName("input-timeout");
    timeoutInputPort.setId("input-timeout-id");
    timeoutInputPort.setType("INPUT_PORT");
    timeoutInputPort.setState(ScheduledState.RUNNING.name());
    inputPorts.add(timeoutInputPort);
    final PortDTO timeoutDataExInputPort = new PortDTO();
    timeoutDataExInputPort.setName("input-timeout-data-ex");
    timeoutDataExInputPort.setId("input-timeout-data-ex-id");
    timeoutDataExInputPort.setType("INPUT_PORT");
    timeoutDataExInputPort.setState(ScheduledState.RUNNING.name());
    inputPorts.add(timeoutDataExInputPort);
    final PortDTO accessDeniedInputPort = new PortDTO();
    accessDeniedInputPort.setName("input-access-denied");
    accessDeniedInputPort.setId("input-access-denied-id");
    accessDeniedInputPort.setType("INPUT_PORT");
    accessDeniedInputPort.setState(ScheduledState.RUNNING.name());
    inputPorts.add(accessDeniedInputPort);
    outputPorts = new HashSet<>();
    final PortDTO runningOutputPort = new PortDTO();
    runningOutputPort.setName("output-running");
    runningOutputPort.setId("output-running-id");
    runningOutputPort.setType("OUTPUT_PORT");
    runningOutputPort.setState(ScheduledState.RUNNING.name());
    outputPorts.add(runningOutputPort);
    final PortDTO timeoutOutputPort = new PortDTO();
    timeoutOutputPort.setName("output-timeout");
    timeoutOutputPort.setId("output-timeout-id");
    timeoutOutputPort.setType("OUTPUT_PORT");
    timeoutOutputPort.setState(ScheduledState.RUNNING.name());
    outputPorts.add(timeoutOutputPort);
    final PortDTO timeoutDataExOutputPort = new PortDTO();
    timeoutDataExOutputPort.setName("output-timeout-data-ex");
    timeoutDataExOutputPort.setId("output-timeout-data-ex-id");
    timeoutDataExOutputPort.setType("OUTPUT_PORT");
    timeoutDataExOutputPort.setState(ScheduledState.RUNNING.name());
    outputPorts.add(timeoutDataExOutputPort);
}
Also used : PortDTO(org.apache.nifi.web.api.dto.PortDTO) CountDownLatch(java.util.concurrent.CountDownLatch) PeerDTO(org.apache.nifi.web.api.dto.remote.PeerDTO) Before(org.junit.Before)

Example 4 with PeerDTO

use of org.apache.nifi.web.api.dto.remote.PeerDTO in project nifi by apache.

the class TestSiteToSiteResource method testPeers.

@Test
public void testPeers() throws Exception {
    final HttpServletRequest req = createCommonHttpServletRequest();
    final NiFiServiceFacade serviceFacade = mock(NiFiServiceFacade.class);
    final SiteToSiteResource resource = getSiteToSiteResource(serviceFacade);
    final Response response = resource.getPeers(req);
    PeersEntity resultEntity = (PeersEntity) response.getEntity();
    assertEquals(200, response.getStatus());
    assertEquals(1, resultEntity.getPeers().size());
    final PeerDTO peer = resultEntity.getPeers().iterator().next();
    assertEquals(8080, peer.getPort());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(javax.ws.rs.core.Response) NiFiServiceFacade(org.apache.nifi.web.NiFiServiceFacade) PeersEntity(org.apache.nifi.web.api.entity.PeersEntity) PeerDTO(org.apache.nifi.web.api.dto.remote.PeerDTO) Test(org.junit.Test)

Example 5 with PeerDTO

use of org.apache.nifi.web.api.dto.remote.PeerDTO in project nifi by apache.

the class TestSiteToSiteResource method testPeersPortForwarding.

@Test
public void testPeersPortForwarding() throws Exception {
    final HttpServletRequest req = createCommonHttpServletRequest();
    final NiFiServiceFacade serviceFacade = mock(NiFiServiceFacade.class);
    final Map<String, String> additionalProperties = new HashMap<>();
    additionalProperties.put(NiFiProperties.WEB_HTTP_PORT_FORWARDING, "80");
    final SiteToSiteResource resource = getSiteToSiteResource(serviceFacade, additionalProperties);
    final Response response = resource.getPeers(req);
    PeersEntity resultEntity = (PeersEntity) response.getEntity();
    assertEquals(200, response.getStatus());
    assertEquals(1, resultEntity.getPeers().size());
    final PeerDTO peer = resultEntity.getPeers().iterator().next();
    assertEquals(80, peer.getPort());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(javax.ws.rs.core.Response) NiFiServiceFacade(org.apache.nifi.web.NiFiServiceFacade) PeersEntity(org.apache.nifi.web.api.entity.PeersEntity) HashMap(java.util.HashMap) PeerDTO(org.apache.nifi.web.api.dto.remote.PeerDTO) Test(org.junit.Test)

Aggregations

PeerDTO (org.apache.nifi.web.api.dto.remote.PeerDTO)5 IOException (java.io.IOException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Response (javax.ws.rs.core.Response)2 NiFiServiceFacade (org.apache.nifi.web.NiFiServiceFacade)2 PeersEntity (org.apache.nifi.web.api.entity.PeersEntity)2 Test (org.junit.Test)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 URI (java.net.URI)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1