use of org.graylog2.contentpacks.model.entities.NativeEntity in project graylog2-server by Graylog2.
the class LookupDataAdapterFacade method decode.
private NativeEntity<DataAdapterDto> decode(EntityV1 entity, final Map<String, ValueReference> parameters) {
final LookupDataAdapterEntity lookupDataAdapterEntity = objectMapper.convertValue(entity.data(), LookupDataAdapterEntity.class);
final LookupDataAdapterConfiguration configuration = objectMapper.convertValue(toValueMap(lookupDataAdapterEntity.configuration(), parameters), LookupDataAdapterConfiguration.class);
final DataAdapterDto dataAdapterDto = DataAdapterDto.builder().name(lookupDataAdapterEntity.name().asString(parameters)).title(lookupDataAdapterEntity.title().asString(parameters)).description(lookupDataAdapterEntity.description().asString(parameters)).config(configuration).build();
final DataAdapterDto savedDataAdapterDto = dataAdapterService.save(dataAdapterDto);
return NativeEntity.create(entity.id(), savedDataAdapterDto.id(), TYPE_V1, savedDataAdapterDto.title(), savedDataAdapterDto);
}
use of org.graylog2.contentpacks.model.entities.NativeEntity in project graylog2-server by Graylog2.
the class NotificationFacadeTest method loadNativeEntity.
@Test
@MongoDBFixtures("NotificationFacadeTest.json")
public void loadNativeEntity() {
final NativeEntityDescriptor nativeEntityDescriptor = NativeEntityDescriptor.create(ModelId.of("content-pack-id"), ModelId.of("5d4d33753d27460ad18e0c4d"), ModelTypes.NOTIFICATION_V1, "title");
final Optional<NativeEntity<NotificationDto>> optionalNativeEntity = facade.loadNativeEntity(nativeEntityDescriptor);
assertThat(optionalNativeEntity).isPresent();
final NativeEntity<NotificationDto> nativeEntity = optionalNativeEntity.get();
assertThat(nativeEntity.entity()).isNotNull();
final NotificationDto notificationDto = nativeEntity.entity();
assertThat(notificationDto.id()).isEqualTo("5d4d33753d27460ad18e0c4d");
}
use of org.graylog2.contentpacks.model.entities.NativeEntity in project graylog2-server by Graylog2.
the class PipelineFacade method decode.
private NativeEntity<PipelineDao> decode(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Object> nativeEntities) {
final DateTime now = Tools.nowUTC();
final PipelineEntity pipelineEntity = objectMapper.convertValue(entity.data(), PipelineEntity.class);
final ValueReference description = pipelineEntity.description();
final PipelineDao pipelineDao = PipelineDao.builder().title(pipelineEntity.title().asString(parameters)).description(description == null ? null : description.asString(parameters)).source(pipelineEntity.source().asString(parameters)).createdAt(now).modifiedAt(now).build();
final PipelineDao savedPipelineDao = pipelineService.save(pipelineDao);
final String pipelineId = requireNonNull(savedPipelineDao.id(), "Saved pipeline ID must not be null");
final Set<EntityDescriptor> connectedStreamEntities = pipelineEntity.connectedStreams().stream().map(valueReference -> valueReference.asString(parameters)).map(streamId -> EntityDescriptor.create(streamId, ModelTypes.STREAM_V1)).collect(Collectors.toSet());
final Set<Stream> connectedStreams = connectedStreams(connectedStreamEntities, nativeEntities);
createPipelineConnections(pipelineId, connectedStreams);
return NativeEntity.create(entity.id(), pipelineId, TYPE_V1, savedPipelineDao.title(), savedPipelineDao);
}
use of org.graylog2.contentpacks.model.entities.NativeEntity 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.NativeEntity in project graylog2-server by Graylog2.
the class EventDefinitionFacadeTest method createNativeEntity.
@Test
public void createNativeEntity() {
final EntityV1 entityV1 = createTestEntity();
final NotificationDto notificationDto = NotificationDto.builder().config(HTTPEventNotificationConfig.builder().url("https://hulud.net").build()).title("Notify me Senpai").description("A notification for senpai").id("dead-beef").build();
final EntityDescriptor entityDescriptor = EntityDescriptor.create("123123", ModelTypes.NOTIFICATION_V1);
final ImmutableMap<EntityDescriptor, Object> nativeEntities = ImmutableMap.of(entityDescriptor, notificationDto);
final JobDefinitionDto jobDefinitionDto = mock(JobDefinitionDto.class);
final JobTriggerDto jobTriggerDto = mock(JobTriggerDto.class);
when(jobDefinitionDto.id()).thenReturn("job-123123");
when(jobSchedulerClock.nowUTC()).thenReturn(DateTime.now(DateTimeZone.UTC));
when(jobDefinitionService.save(any(JobDefinitionDto.class))).thenReturn(jobDefinitionDto);
when(jobTriggerService.create(any(JobTriggerDto.class))).thenReturn(jobTriggerDto);
final UserImpl kmerzUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()), ImmutableMap.of("username", "kmerz"));
when(userService.load("kmerz")).thenReturn(kmerzUser);
final NativeEntity<EventDefinitionDto> nativeEntity = facade.createNativeEntity(entityV1, ImmutableMap.of(), nativeEntities, "kmerz");
assertThat(nativeEntity).isNotNull();
final EventDefinitionDto eventDefinitionDto = nativeEntity.entity();
assertThat(eventDefinitionDto.title()).isEqualTo("title");
assertThat(eventDefinitionDto.description()).isEqualTo("description");
assertThat(eventDefinitionDto.config().type()).isEqualTo("aggregation-v1");
// verify that ownership was registered for this entity
verify(entityOwnershipService, times(1)).registerNewEventDefinition(nativeEntity.entity().id(), kmerzUser);
}
Aggregations