use of org.apache.nifi.web.api.dto.ControllerDTO in project nifi by apache.
the class StandardRemoteProcessGroup method refreshFlowContents.
@Override
public void refreshFlowContents() throws CommunicationsException {
if (!initialized) {
return;
}
try {
// perform the request
final ControllerDTO dto;
try (final SiteToSiteRestApiClient apiClient = getSiteToSiteRestApiClient()) {
dto = apiClient.getController(targetUris);
} catch (IOException e) {
throw new CommunicationsException("Unable to communicate with Remote NiFi at URI " + targetUris + " due to: " + e.getMessage());
}
writeLock.lock();
try {
if (dto.getInputPorts() != null) {
setInputPorts(convertRemotePort(dto.getInputPorts()), true);
}
if (dto.getOutputPorts() != null) {
setOutputPorts(convertRemotePort(dto.getOutputPorts()), true);
}
// set the controller details
setTargetId(dto.getId());
setName(dto.getName());
setComments(dto.getComments());
// get the component counts
int inputPortCount = 0;
if (dto.getInputPortCount() != null) {
inputPortCount = dto.getInputPortCount();
}
int outputPortCount = 0;
if (dto.getOutputPortCount() != null) {
outputPortCount = dto.getOutputPortCount();
}
this.listeningPort = dto.getRemoteSiteListeningPort();
this.listeningHttpPort = dto.getRemoteSiteHttpListeningPort();
this.destinationSecure = dto.isSiteToSiteSecure();
final RemoteProcessGroupCounts newCounts = new RemoteProcessGroupCounts(inputPortCount, outputPortCount);
setCounts(newCounts);
this.refreshContentsTimestamp = System.currentTimeMillis();
} finally {
writeLock.unlock();
}
} catch (final IOException e) {
throw new CommunicationsException(e);
}
}
use of org.apache.nifi.web.api.dto.ControllerDTO in project nifi by apache.
the class ControllerEndpointMerger method mergeResponses.
@Override
protected void mergeResponses(ControllerDTO clientDto, Map<NodeIdentifier, ControllerDTO> dtoMap, Set<NodeResponse> successfulResponses, Set<NodeResponse> problematicResponses) {
// map of port id to map of node id to port dto
final Map<String, Map<NodeIdentifier, PortDTO>> inputPortMap = new HashMap<>();
// map of port id to map of node id to port dto
final Map<String, Map<NodeIdentifier, PortDTO>> outputPortMap = new HashMap<>();
for (final Map.Entry<NodeIdentifier, ControllerDTO> entry : dtoMap.entrySet()) {
final NodeIdentifier nodeId = entry.getKey();
final ControllerDTO nodeController = entry.getValue();
// gather all input and output ports for merging, including the ports from clientDto
nodeController.getInputPorts().stream().forEach(inputPort -> inputPortMap.computeIfAbsent(inputPort.getId(), nodeIdToInputPort -> new HashMap<>()).put(nodeId, inputPort));
nodeController.getOutputPorts().stream().forEach(outputPort -> outputPortMap.computeIfAbsent(outputPort.getId(), nodeIdToOutputPort -> new HashMap<>()).put(nodeId, outputPort));
}
// merge input ports
for (Map<NodeIdentifier, PortDTO> inputPortByNodeId : inputPortMap.values()) {
final Collection<PortDTO> nodeInputPorts = inputPortByNodeId.values();
if (!nodeInputPorts.isEmpty()) {
final PortDTO inputPort = nodeInputPorts.iterator().next();
final PortDTO clientInputPort = clientDto.getInputPorts().stream().filter(p -> p.getId().equals(inputPort.getId())).findFirst().orElse(null);
if (clientInputPort != null) {
PortEntityMerger.mergeDtos(clientInputPort, inputPortByNodeId);
}
}
}
// merge output ports
for (Map<NodeIdentifier, PortDTO> outputPortByNodeId : outputPortMap.values()) {
final Collection<PortDTO> nodeOutputPorts = outputPortByNodeId.values();
if (!nodeOutputPorts.isEmpty()) {
final PortDTO outputPort = nodeOutputPorts.iterator().next();
final PortDTO clientOutputPort = clientDto.getInputPorts().stream().filter(p -> p.getId().equals(outputPort.getId())).findFirst().orElse(null);
if (clientOutputPort != null) {
PortEntityMerger.mergeDtos(clientOutputPort, outputPortByNodeId);
}
}
}
// get intersection of input and output ports
final Set<PortDTO> clientInputPorts = Sets.newHashSet(clientDto.getInputPorts());
final Set<PortDTO> clientOutputPorts = Sets.newHashSet(clientDto.getOutputPorts());
dtoMap.values().forEach(controller -> {
clientInputPorts.retainAll(controller.getInputPorts());
clientOutputPorts.retainAll(controller.getOutputPorts());
});
clientDto.setInputPorts(clientInputPorts);
clientDto.setInputPortCount(clientInputPorts.size());
clientDto.setOutputPorts(clientOutputPorts);
clientDto.setOutputPortCount(clientOutputPorts.size());
}
use of org.apache.nifi.web.api.dto.ControllerDTO in project nifi by apache.
the class SiteToSiteRestApiClient method getController.
private ControllerDTO getController() throws IOException {
// from he canvas, etc. We want to ensure that we avoid memory leaks, even if they are likely to not cause a problem.
if (System.currentTimeMillis() > lastPruneTimestamp + TimeUnit.MINUTES.toMillis(5)) {
pruneCache();
}
final String internedUrl = baseUrl.intern();
synchronized (internedUrl) {
final RemoteGroupContents groupContents = contentsMap.get(internedUrl);
if (groupContents == null || groupContents.getContents() == null || groupContents.isOlderThan(cacheExpirationMillis)) {
logger.debug("No Contents for remote group at URL {} or contents have expired; will refresh contents", internedUrl);
final ControllerDTO refreshedContents;
try {
refreshedContents = fetchController();
} catch (final Exception e) {
// we failed to refresh contents, but we don't want to constantly poll the remote instance, failing.
// So we put the ControllerDTO back but use a new RemoteGroupContents so that we get a new timestamp.
final ControllerDTO existingController = groupContents == null ? null : groupContents.getContents();
final RemoteGroupContents updatedContents = new RemoteGroupContents(existingController);
contentsMap.put(internedUrl, updatedContents);
throw e;
}
logger.debug("Successfully retrieved contents for remote group at URL {}", internedUrl);
final RemoteGroupContents updatedContents = new RemoteGroupContents(refreshedContents);
contentsMap.put(internedUrl, updatedContents);
return refreshedContents;
}
logger.debug("Contents for remote group at URL {} have already been fetched and have not yet expired. Will return the cached value.", internedUrl);
return groupContents.getContents();
}
}
use of org.apache.nifi.web.api.dto.ControllerDTO in project nifi by apache.
the class TestSiteInfoProvider method testSecure.
@Test
public void testSecure() throws Exception {
final Set<String> expectedClusterUrl = new LinkedHashSet<>(Arrays.asList(new String[] { "https://node1:8443", "https://node2:8443" }));
final String expectedActiveClusterUrl = "https://node2:8443/nifi-api";
final SSLContext expectedSslConText = mock(SSLContext.class);
final HttpProxy expectedHttpProxy = mock(HttpProxy.class);
final SiteInfoProvider siteInfoProvider = spy(new SiteInfoProvider());
siteInfoProvider.setClusterUrls(expectedClusterUrl);
siteInfoProvider.setSslContext(expectedSslConText);
siteInfoProvider.setProxy(expectedHttpProxy);
final ControllerDTO controllerDTO = new ControllerDTO();
final PortDTO inputPort1 = new PortDTO();
inputPort1.setName("input-one");
inputPort1.setId("input-0001");
final PortDTO inputPort2 = new PortDTO();
inputPort2.setName("input-two");
inputPort2.setId("input-0002");
final PortDTO outputPort1 = new PortDTO();
outputPort1.setName("output-one");
outputPort1.setId("output-0001");
final PortDTO outputPort2 = new PortDTO();
outputPort2.setName("output-two");
outputPort2.setId("output-0002");
final Set<PortDTO> inputPorts = new HashSet<>();
inputPorts.add(inputPort1);
inputPorts.add(inputPort2);
final Set<PortDTO> outputPorts = new HashSet<>();
outputPorts.add(outputPort1);
outputPorts.add(outputPort2);
controllerDTO.setInputPorts(inputPorts);
controllerDTO.setOutputPorts(outputPorts);
controllerDTO.setRemoteSiteListeningPort(8081);
controllerDTO.setRemoteSiteHttpListeningPort(8443);
controllerDTO.setSiteToSiteSecure(true);
// SiteInfoProvider uses SiteToSIteRestApiClient to get ControllerDTO.
doAnswer(invocation -> {
final SSLContext sslContext = invocation.getArgumentAt(0, SSLContext.class);
final HttpProxy httpProxy = invocation.getArgumentAt(1, HttpProxy.class);
assertEquals(expectedSslConText, sslContext);
assertEquals(expectedHttpProxy, httpProxy);
final SiteToSiteRestApiClient apiClient = mock(SiteToSiteRestApiClient.class);
when(apiClient.getController(eq(expectedClusterUrl))).thenReturn(controllerDTO);
when(apiClient.getBaseUrl()).thenReturn(expectedActiveClusterUrl);
return apiClient;
}).when(siteInfoProvider).createSiteToSiteRestApiClient(any(), any());
// siteInfoProvider should expose correct information of the remote NiFi cluster.
assertEquals(controllerDTO.getRemoteSiteListeningPort(), siteInfoProvider.getSiteToSitePort());
assertEquals(controllerDTO.getRemoteSiteHttpListeningPort(), siteInfoProvider.getSiteToSiteHttpPort());
assertEquals(controllerDTO.isSiteToSiteSecure(), siteInfoProvider.isSecure());
assertTrue(siteInfoProvider.isWebInterfaceSecure());
assertEquals(inputPort1.getId(), siteInfoProvider.getInputPortIdentifier(inputPort1.getName()));
assertEquals(inputPort2.getId(), siteInfoProvider.getInputPortIdentifier(inputPort2.getName()));
assertEquals(outputPort1.getId(), siteInfoProvider.getOutputPortIdentifier(outputPort1.getName()));
assertEquals(outputPort2.getId(), siteInfoProvider.getOutputPortIdentifier(outputPort2.getName()));
assertNull(siteInfoProvider.getInputPortIdentifier("not-exist"));
assertNull(siteInfoProvider.getOutputPortIdentifier("not-exist"));
assertEquals(inputPort1.getId(), siteInfoProvider.getPortIdentifier(inputPort1.getName(), TransferDirection.SEND));
assertEquals(outputPort1.getId(), siteInfoProvider.getPortIdentifier(outputPort1.getName(), TransferDirection.RECEIVE));
assertEquals(expectedActiveClusterUrl, siteInfoProvider.getActiveClusterUrl().toString());
}
use of org.apache.nifi.web.api.dto.ControllerDTO in project nifi by apache.
the class TestSiteInfoProvider method testPlain.
@Test
public void testPlain() throws Exception {
final Set<String> expectedClusterUrl = new LinkedHashSet<>(Arrays.asList(new String[] { "http://node1:8443, http://node2:8443" }));
final String expectedActiveClusterUrl = "http://node2:8443/nifi-api";
final SiteInfoProvider siteInfoProvider = spy(new SiteInfoProvider());
siteInfoProvider.setClusterUrls(expectedClusterUrl);
final ControllerDTO controllerDTO = new ControllerDTO();
controllerDTO.setInputPorts(Collections.emptySet());
controllerDTO.setOutputPorts(Collections.emptySet());
controllerDTO.setRemoteSiteListeningPort(8081);
controllerDTO.setRemoteSiteHttpListeningPort(8080);
controllerDTO.setSiteToSiteSecure(false);
// SiteInfoProvider uses SiteToSIteRestApiClient to get ControllerDTO.
doAnswer(invocation -> {
final SiteToSiteRestApiClient apiClient = mock(SiteToSiteRestApiClient.class);
when(apiClient.getController(eq(expectedClusterUrl))).thenReturn(controllerDTO);
when(apiClient.getBaseUrl()).thenReturn(expectedActiveClusterUrl);
return apiClient;
}).when(siteInfoProvider).createSiteToSiteRestApiClient(any(), any());
// siteInfoProvider should expose correct information of the remote NiFi cluster.
assertEquals(controllerDTO.getRemoteSiteListeningPort(), siteInfoProvider.getSiteToSitePort());
assertEquals(controllerDTO.getRemoteSiteHttpListeningPort(), siteInfoProvider.getSiteToSiteHttpPort());
assertEquals(controllerDTO.isSiteToSiteSecure(), siteInfoProvider.isSecure());
assertFalse(siteInfoProvider.isWebInterfaceSecure());
assertEquals(expectedActiveClusterUrl, siteInfoProvider.getActiveClusterUrl().toString());
}
Aggregations