use of org.graylog2.contentpacks.model.entities.StreamEntity in project graylog2-server by Graylog2.
the class PipelineFacade method resolveForInstallation.
private Graph<Entity> resolveForInstallation(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Entity> entities) {
final MutableGraph<Entity> mutableGraph = GraphBuilder.directed().build();
mutableGraph.addNode(entity);
final PipelineEntity pipelineEntity = objectMapper.convertValue(entity.data(), PipelineEntity.class);
final String source = pipelineEntity.source().asString(parameters);
final Collection<String> referencedRules = referencedRules(source);
referencedRules.stream().map(ModelId::of).map(modelId -> EntityDescriptor.create(modelId, ModelTypes.PIPELINE_RULE_V1)).map(entities::get).filter(Objects::nonNull).forEach(ruleEntity -> mutableGraph.putEdge(entity, ruleEntity));
pipelineEntity.connectedStreams().stream().map(valueReference -> valueReference.asString(parameters)).map(ModelId::of).map(modelId -> EntityDescriptor.create(modelId, ModelTypes.STREAM_V1)).map(entities::get).filter(Objects::nonNull).forEach(streamEntity -> mutableGraph.putEdge(entity, streamEntity));
return ImmutableGraph.copyOf(mutableGraph);
}
use of org.graylog2.contentpacks.model.entities.StreamEntity in project graylog2-server by Graylog2.
the class StreamFacade method decode.
private NativeEntity<Stream> decode(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Object> nativeEntities, User user) {
final StreamEntity streamEntity = objectMapper.convertValue(entity.data(), StreamEntity.class);
final CreateStreamRequest createStreamRequest = CreateStreamRequest.create(streamEntity.title().asString(parameters), streamEntity.description().asString(parameters), // ignored
null, null, streamEntity.matchingType().asString(parameters), streamEntity.removeMatches().asBoolean(parameters), indexSetService.getDefault().id());
final Stream stream = streamService.create(createStreamRequest, user.getName());
final List<StreamRule> streamRules = streamEntity.streamRules().stream().map(streamRuleEntity -> createStreamRuleRequest(streamRuleEntity, parameters)).map(request -> streamRuleService.create(DUMMY_STREAM_ID, request)).collect(Collectors.toList());
// TODO: The creation of legacy alert conditions should be avoided and a new event definition should be created instead
final List<AlertCondition> alertConditions = streamEntity.alertConditions().stream().map(alertCondition -> createStreamAlertConditionRequest(alertCondition, parameters)).map(request -> {
try {
return streamAlertService.fromRequest(request, stream, user.getName());
} catch (ConfigurationException e) {
throw new ContentPackException("Couldn't create entity " + entity.toEntityDescriptor(), e);
}
}).collect(Collectors.toList());
// TODO: The creation of legacy alarm callback should be avoided and a new event notification should be created instead
final List<AlarmCallbackConfiguration> alarmCallbacks = streamEntity.alarmCallbacks().stream().map(alarmCallback -> createStreamAlarmCallbackRequest(alarmCallback, parameters)).map(request -> alarmCallbackConfigurationService.create(stream.getId(), request, user.getName())).collect(Collectors.toList());
final String savedStreamId;
try {
savedStreamId = streamService.saveWithRulesAndOwnership(stream, streamRules, user);
for (final AlertCondition alertCondition : alertConditions) {
streamService.addAlertCondition(stream, alertCondition);
}
for (final AlarmCallbackConfiguration alarmCallback : alarmCallbacks) {
alarmCallbackConfigurationService.save(alarmCallback);
}
} catch (ValidationException e) {
throw new ContentPackException("Couldn't create entity " + entity.toEntityDescriptor(), e);
}
final Set<ObjectId> outputIds = streamEntity.outputs().stream().map(valueReference -> valueReference.asString(parameters)).map(ModelId::of).map(modelId -> EntityDescriptor.create(modelId, ModelTypes.OUTPUT_V1)).map(descriptor -> findOutput(descriptor, nativeEntities)).map(Output::getId).map(ObjectId::new).collect(Collectors.toSet());
streamService.addOutputs(new ObjectId(savedStreamId), outputIds);
if (!alertConditions.isEmpty() || !alarmCallbacks.isEmpty()) {
// TODO: Remove migration call once we updated the above code to directly create event definitions and notifications
try {
legacyAlertsMigration.upgrade();
} catch (Exception e) {
LOG.error("Couldn't run migration for newly created legacy alert conditions and/or alarm callbacks", e);
}
}
return NativeEntity.create(entity.id(), savedStreamId, TYPE_V1, stream.getTitle(), stream);
}
use of org.graylog2.contentpacks.model.entities.StreamEntity in project graylog2-server by Graylog2.
the class StreamFacade method exportNativeEntity.
@VisibleForTesting
Entity exportNativeEntity(Stream stream, EntityDescriptorIds entityDescriptorIds) {
final List<StreamRuleEntity> streamRules = stream.getStreamRules().stream().map(this::encodeStreamRule).collect(Collectors.toList());
final Set<ValueReference> outputIds = stream.getOutputs().stream().map(output -> entityDescriptorIds.getOrThrow(output.getId(), ModelTypes.OUTPUT_V1)).map(ValueReference::of).collect(Collectors.toSet());
final StreamEntity streamEntity = StreamEntity.create(ValueReference.of(stream.getTitle()), ValueReference.of(stream.getDescription()), ValueReference.of(stream.getDisabled()), ValueReference.of(stream.getMatchingType()), streamRules, // Kept for backwards compatibility
Collections.emptyList(), // Kept for backwards compatibility
Collections.emptyList(), outputIds, ValueReference.of(stream.isDefaultStream()), ValueReference.of(stream.getRemoveMatchesFromDefaultStream()));
final JsonNode data = objectMapper.convertValue(streamEntity, JsonNode.class);
return EntityV1.builder().id(ModelId.of(entityDescriptorIds.getOrThrow(stream.getId(), ModelTypes.STREAM_V1))).type(ModelTypes.STREAM_V1).data(data).build();
}
use of org.graylog2.contentpacks.model.entities.StreamEntity in project graylog2-server by Graylog2.
the class StreamFacade method resolveForInstallation.
private Graph<Entity> resolveForInstallation(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Entity> entities) {
final MutableGraph<Entity> mutableGraph = GraphBuilder.directed().build();
mutableGraph.addNode(entity);
final StreamEntity streamEntity = objectMapper.convertValue(entity.data(), StreamEntity.class);
streamEntity.outputs().stream().map(valueReference -> valueReference.asString(parameters)).map(ModelId::of).map(modelId -> EntityDescriptor.create(modelId, ModelTypes.OUTPUT_V1)).map(entities::get).filter(Objects::nonNull).forEach(outputEntity -> mutableGraph.putEdge(entity, outputEntity));
return ImmutableGraph.copyOf(mutableGraph);
}
use of org.graylog2.contentpacks.model.entities.StreamEntity in project graylog2-server by Graylog2.
the class StreamCatalogTest method encode.
@Test
public void encode() {
final ImmutableMap<String, Object> streamFields = ImmutableMap.of(StreamImpl.FIELD_TITLE, "Stream Title", StreamImpl.FIELD_DESCRIPTION, "Stream Description", StreamImpl.FIELD_DISABLED, false);
final ImmutableMap<String, Object> streamRuleFields = ImmutableMap.<String, Object>builder().put("_id", "1234567890").put(StreamRuleImpl.FIELD_TYPE, StreamRuleType.EXACT.getValue()).put(StreamRuleImpl.FIELD_DESCRIPTION, "description").put(StreamRuleImpl.FIELD_FIELD, "field").put(StreamRuleImpl.FIELD_VALUE, "value").put(StreamRuleImpl.FIELD_INVERTED, false).put(StreamRuleImpl.FIELD_STREAM_ID, "1234567890").build();
final ImmutableList<StreamRule> streamRules = ImmutableList.of(new StreamRuleMock(streamRuleFields));
final ImmutableSet<Output> outputs = ImmutableSet.of();
final ObjectId streamId = new ObjectId();
final StreamImpl stream = new StreamImpl(streamId, streamFields, streamRules, outputs, null);
final EntityDescriptor descriptor = EntityDescriptor.create(stream.getId(), ModelTypes.STREAM_V1);
final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
final Entity entity = facade.exportNativeEntity(stream, entityDescriptorIds);
assertThat(entity).isInstanceOf(EntityV1.class);
assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
assertThat(entity.type()).isEqualTo(ModelTypes.STREAM_V1);
final EntityV1 entityV1 = (EntityV1) entity;
final StreamEntity streamEntity = objectMapper.convertValue(entityV1.data(), StreamEntity.class);
assertThat(streamEntity.title()).isEqualTo(ValueReference.of("Stream Title"));
assertThat(streamEntity.description()).isEqualTo(ValueReference.of("Stream Description"));
assertThat(streamEntity.disabled()).isEqualTo(ValueReference.of(false));
assertThat(streamEntity.streamRules()).hasSize(1);
}
Aggregations