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;
}