private void validateTopologySource(TopologySource topologySource) { StorageUtils.ensureUnique(topologySource, this::listTopologySources, QueryParam.params(TopologySource.TOPOLOGYID, topologySource.getTopologyId().toString(), TopologySource.VERSIONID, topologySource.getVersionId().toString(), TopologySource.NAME, topologySource.getName())); }
public TopologySource addOrUpdateTopologySource(Long topologyId, Long sourceId, TopologySource topologySource) { Long currentTopologyVersionId = getCurrentVersionId(topologyId); topologySource.setId(sourceId); topologySource.setVersionId(currentTopologyVersionId); topologySource.setTopologyId(topologyId); validateTopologySource(topologySource); topologySource.setReconfigure(false); dao.addOrUpdate(topologySource); List<Long> newList = Collections.emptyList(); if (topologySource.getOutputStreamIds() != null) { newList = topologySource.getOutputStreamIds(); } else if (topologySource.getOutputStreams() != null) { newList = updateOutputStreams(topologySource); } List<Long> existing = getOutputStreamIds(topologySource); Sets.SetView<Long> streamIdsToRemove = Sets.difference(ImmutableSet.copyOf(existing), ImmutableSet.copyOf(newList)); Sets.SetView<Long> streamIdsToAdd = Sets.difference(ImmutableSet.copyOf(newList), ImmutableSet.copyOf(existing)); removeSourceStreamMapping(topologySource, Lists.newArrayList(streamIdsToRemove)); addSourceStreamMapping(topologySource, Lists.newArrayList(streamIdsToAdd)); TopologySource updatedSource = getTopologySource(topologyId, sourceId, currentTopologyVersionId); updatedSource.setVersionTimestamp(updateVersionTimestamp(currentTopologyVersionId).getTimestamp()); return updatedSource; }
public TopologySource getTopologySource(Long topologyId, Long sourceId, Long versionId) { TopologySource topologySource = new TopologySource(); topologySource.setId(sourceId); topologySource.setVersionId(versionId); TopologySource source = dao.get(new StorableKey(TOPOLOGY_SOURCE_NAMESPACE, topologySource.getPrimaryKey())); if (source == null || !source.getTopologyId().equals(topologyId)) { return null; } fillSourceStreams(source); return source; }
private void addSourceStreamMapping(TopologySource topologySource, List<Long> streamIds) { for (Long outputStreamId : streamIds) { dao.<TopologySourceStreamMap>add(new TopologySourceStreamMap(topologySource.getId(), topologySource.getVersionId(), outputStreamId)); } }
topologySource.setOutputStreamIds(importOutputStreams(newTopology.getId(), oldToNewStreamIds, topologySource.getOutputStreams())); topologySource.setOutputStreams(null); Long oldComponentId = topologySource.getId(); topologySource.setId(null); topologySource.setTopologyId(newTopology.getId()); TopologyComponentBundle bundle = getCurrentTopologyComponentBundle( TopologyComponentBundle.TopologyComponentType.SOURCE, topologyData.getBundleIdToType().get(topologySource.getTopologyComponentBundleId().toString())); topologySource.setTopologyComponentBundleId(bundle.getId()); addTopologySource(newTopology.getId(), topologySource); oldToNewComponentIds.put(oldComponentId, topologySource.getId());
public TopologySource addTopologySource(Long topologyId, Long versionId, TopologySource topologySource) { if (topologySource.getId() == null) { topologySource.setId(getNextTopologyComponentId()); } topologySource.setVersionId(versionId); topologySource.setTopologyId(topologyId); validateTopologySource(topologySource); List<TopologyStream> topologyStreams = addTopologyOutputComponent(topologySource); addSourceStreamMapping(topologySource, topologySource.getOutputStreamIds()); topologySource.setOutputStreams(topologyStreams); topologySource.setVersionTimestamp(updateVersionTimestamp(versionId).getTimestamp()); return topologySource; }
private List<TopologyStream> getOutputStreams(TopologySource topologySource) { List<TopologyStream> streams = new ArrayList<>(); if (topologySource != null) { QueryParam qp1 = new QueryParam(TopologySourceStreamMap.FIELD_SOURCE_ID, String.valueOf(topologySource.getId())); QueryParam qp2 = new QueryParam(TopologySourceStreamMap.FIELD_VERSION_ID, String.valueOf(topologySource.getVersionId())); for (TopologySourceStreamMap mapping : listTopologySourceStreamMapping(ImmutableList.of(qp1, qp2))) { TopologyStream topologyStream = getStreamInfo(topologySource.getTopologyId(), mapping.getStreamId(), topologySource.getVersionId()); if (topologyStream != null) { streams.add(topologyStream); } } } return streams; }
@POST @Path("/topologies/{topologyId}/testcases/{testCaseId}/sources") public Response addTestRunCaseSource(@PathParam("topologyId") Long topologyId, @PathParam("testCaseId") Long testCaseId, TopologyTestRunCaseSource testRunCaseSource, @Context SecurityContext securityContext) { SecurityUtil.checkRoleOrPermissions(authorizer, securityContext, Roles.ROLE_TOPOLOGY_USER, Topology.NAMESPACE, topologyId, WRITE); TopologySource topologySource = getAssociatedTopologySource(topologyId, testCaseId, testRunCaseSource.getSourceId()); testRunCaseSource.setVersionId(topologySource.getVersionId()); TopologyTestRunCaseSource addedCaseSource = catalogService.addTopologyTestRunCaseSource(testRunCaseSource); return WSUtils.respondEntity(addedCaseSource, CREATED); }
private void doValidationForTestRunCaseSource(Long topologyId, Long testCaseId, TopologyTestRunCaseSource testRunCaseSource) throws IOException { TopologySource topologySource = getAssociatedTopologySource(topologyId, testCaseId, testRunCaseSource.getSourceId()); Map<String, String> map = objectMapper.readValue(testRunCaseSource.getRecords(), new TypeReference<Map<String, String>>() {}); for (Map.Entry<String, String> entry: map.entrySet()) { List<Map<String, Object>> values = objectMapper.readValue(entry.getValue(), new TypeReference<List<Map<String, Object>>>(){}); values.forEach(v -> convertValueToConformStream(topologySource.getOutputStreams(), entry.getKey(), v)); } }
public TopologySource removeTopologySource(Long topologyId, Long sourceId, Long versionId, boolean removeEdges) { TopologySource topologySource = getTopologySource(topologyId, sourceId, versionId); if (topologySource != null) { if (removeEdges) { removeAllEdges(topologySource); } removeSourceStreamMapping(topologySource); removeAllTopologyTestSources(topologySource); topologySource = dao.<TopologySource>remove(new StorableKey(TOPOLOGY_SOURCE_NAMESPACE, topologySource.getPrimaryKey())); topologySource.setVersionTimestamp(updateVersionTimestamp(versionId).getTimestamp()); } return topologySource; }
private RuntimeException handleSchemaValidationFailedException(Long topologyId, TopologyTestRunCaseSource source, SchemaValidationFailedException e) { TopologySource topologySource; if (source.getVersionId() != null) { topologySource = catalogService.getTopologySource(topologyId, source.getSourceId(), source.getVersionId()); } else { topologySource = catalogService.getTopologySource(topologyId, source.getSourceId()); } if (topologySource != null) { throw BadRequestException.message("Schema validation failed in test case source name: " + topologySource.getName() + " - " + e.getMessage()); } else { throw BadRequestException.message("Schema validation failed in test case source ID: " + source.getId() + " - " + e.getMessage()); } }
private void removeAllTopologyTestSources(TopologySource topologySource) { QueryParam sourceIdQuery = new QueryParam("sourceId", String.valueOf(topologySource.getId())); Collection<TopologyTestRunCaseSource> sources = listTopologyTestRunCaseSource(Collections.singletonList(sourceIdQuery)); if (sources != null) { sources.forEach(s -> removeTopologyTestRunCaseSource(s.getId())); } }
private void removeSourceStreamMapping(TopologySource topologySource) { if (topologySource != null) { removeSourceStreamMapping(topologySource, topologySource.getOutputStreamIds()); } }
addTopologySource(topologyId, newVersionId, new TopologySource(source));
private void removeSourceStreamMapping(TopologySource topologySource, List<Long> streamIds) { if (topologySource != null) { for (Long outputStreamId : streamIds) { TopologySourceStreamMap mapping = new TopologySourceStreamMap( topologySource.getId(), topologySource.getVersionId(), outputStreamId); dao.<TopologySourceStreamMap>remove(mapping.getStorableKey()); } } }
@PUT @Path("/topologies/{topologyId}/testcases/{testCaseId}/sources/{id}") public Response addOrUpdateTestRunCaseSource(@PathParam("topologyId") Long topologyId, @PathParam("testCaseId") Long testCaseId, @PathParam("id") Long id, TopologyTestRunCaseSource testRunCaseSource, @Context SecurityContext securityContext) { SecurityUtil.checkRoleOrPermissions(authorizer, securityContext, Roles.ROLE_TOPOLOGY_USER, Topology.NAMESPACE, topologyId, WRITE); testRunCaseSource.setId(id); testRunCaseSource.setTestCaseId(testCaseId); TopologySource topologySource = getAssociatedTopologySource(topologyId, testCaseId, testRunCaseSource.getSourceId()); testRunCaseSource.setVersionId(topologySource.getVersionId()); TopologyTestRunCaseSource updatedCase = catalogService.addOrUpdateTopologyTestRunCaseSource(testRunCaseSource.getId(), testRunCaseSource); return WSUtils.respondEntity(updatedCase, OK); }
removeTopologySource(topologyId, source.getId(), versionId, false);
private List<Long> getOutputStreamIds(TopologySource topologySource) { List<Long> streamIds = new ArrayList<>(); if (topologySource != null) { QueryParam qp1 = new QueryParam(TopologySourceStreamMap.FIELD_SOURCE_ID, String.valueOf(topologySource.getId())); QueryParam qp2 = new QueryParam(TopologySourceStreamMap.FIELD_VERSION_ID, String.valueOf(topologySource.getVersionId())); for (TopologySourceStreamMap mapping : listTopologySourceStreamMapping(ImmutableList.of(qp1, qp2))) { streamIds.add(mapping.getStreamId()); } } return streamIds; }
private TopologySource getAssociatedTopologySource(Long topologyId, Long testCaseId, Long topologySourceId) { TopologyTestRunCase testCase = catalogService.getTopologyTestRunCase(topologyId, testCaseId); if (testCase == null) { throw EntityNotFoundException.byId("Topology test case with topology id " + topologyId + " and test case id " + testCaseId); } TopologySource topologySource = catalogService.getTopologySource(topologyId, topologySourceId, testCase.getVersionId()); if (topologySource == null) { throw EntityNotFoundException.byId("Topology source with topology id " + topologyId + " and version id " + testCase.getVersionId()); } else if (!testCase.getVersionId().equals(topologySource.getVersionId())) { throw new IllegalStateException("Test case and topology source point to the different version id: " + "version id of test case: " + testCase.getVersionId() + " / " + "version id of topology source: " + topologySource.getVersionId()); } return topologySource; }