use of org.graylog2.migrations.Migration in project graylog2-server by Graylog2.
the class V20161124104700_AddRetentionRotationAndDefaultFlagToIndexSetMigration method upgrade.
@Override
public void upgrade() {
if (clusterConfigService.get(MigrationCompleted.class) != null) {
LOG.debug("Migration already completed!");
return;
}
final ImmutableSet.Builder<String> updatedIds = ImmutableSet.builder();
final ImmutableSet.Builder<String> skippedIds = ImmutableSet.builder();
final IndexManagementConfig indexManagementConfig = clusterConfigService.get(IndexManagementConfig.class);
checkState(indexManagementConfig != null, "Couldn't find index management configuration");
for (final IndexSetConfig indexSetConfig : indexSetService.findAll()) {
final IndexSetConfig.Builder updated = indexSetConfig.toBuilder();
if (isNullOrEmpty(indexSetConfig.rotationStrategyClass())) {
// Paranoia
checkState(indexSetConfig.rotationStrategy().type().startsWith(indexManagementConfig.rotationStrategy()), "rotation strategy config type <%s> does not match rotation strategy <%s>", indexSetConfig.rotationStrategy().type(), indexManagementConfig.rotationStrategy());
LOG.info("Adding rotation_strategy_class <{}> to index set <{}>", indexManagementConfig.rotationStrategy(), indexSetConfig.id());
updated.rotationStrategyClass(indexManagementConfig.rotationStrategy());
}
if (isNullOrEmpty(indexSetConfig.retentionStrategyClass())) {
// Paranoia
checkState(indexSetConfig.retentionStrategy().type().startsWith(indexManagementConfig.retentionStrategy()), "retention strategy config type <%s> does not match retention strategy <%s>", indexSetConfig.retentionStrategy().type(), indexManagementConfig.retentionStrategy());
LOG.info("Adding retention_strategy_class <{}> to index set <{}>", indexManagementConfig.retentionStrategy(), indexSetConfig.id());
updated.retentionStrategyClass(indexManagementConfig.retentionStrategy());
}
if (!indexSetConfig.equals(updated.build())) {
indexSetService.save(updated.build());
updatedIds.add(Optional.ofNullable(indexSetConfig.id()).orElseThrow(() -> new IllegalStateException("no id??")));
} else {
skippedIds.add(Optional.ofNullable(indexSetConfig.id()).orElseThrow(() -> new IllegalStateException("no id??")));
}
}
// Mark the oldest index set (there should be only one at this point, though) as default.
final IndexSetConfig defaultIndexSetConfig = indexSetService.findAll().stream().sorted(Comparator.comparing(IndexSetConfig::creationDate)).findFirst().orElseThrow(() -> new IllegalStateException("Unable to find any index set - this should not happen!"));
LOG.info("Setting index set <{}> as default", defaultIndexSetConfig.id());
clusterConfigService.write(DefaultIndexSetConfig.create(defaultIndexSetConfig.id()));
clusterConfigService.write(MigrationCompleted.create(updatedIds.build(), skippedIds.build(), defaultIndexSetConfig.id()));
}
use of org.graylog2.migrations.Migration 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.migrations.Migration in project graylog2-server by Graylog2.
the class InputServiceImpl method getExtractors.
@Override
@SuppressWarnings("unchecked")
public List<Extractor> getExtractors(Input input) {
if (input.getFields().get(InputImpl.EMBEDDED_EXTRACTORS) == null) {
return Collections.emptyList();
}
final ImmutableList.Builder<Extractor> listBuilder = ImmutableList.builder();
final BasicDBList mEx = (BasicDBList) input.getFields().get(InputImpl.EMBEDDED_EXTRACTORS);
for (final Object element : mEx) {
final DBObject ex = (BasicDBObject) element;
// SOFT MIGRATION: does this extractor have an order set? Implemented for issue: #726
Long order = 0L;
if (ex.containsField(Extractor.FIELD_ORDER)) {
/* We use json format to describe our test fixtures
This format will only return Integer on this place,
which can't be converted to long. So I first cast
it to Number and eventually to long */
Number num = (Number) ex.get(Extractor.FIELD_ORDER);
// mongodb driver gives us a java.lang.Long
order = num.longValue();
}
try {
final Extractor extractor = extractorFactory.factory((String) ex.get(Extractor.FIELD_ID), (String) ex.get(Extractor.FIELD_TITLE), order.intValue(), Extractor.CursorStrategy.valueOf(((String) ex.get(Extractor.FIELD_CURSOR_STRATEGY)).toUpperCase(Locale.ENGLISH)), Extractor.Type.valueOf(((String) ex.get(Extractor.FIELD_TYPE)).toUpperCase(Locale.ENGLISH)), (String) ex.get(Extractor.FIELD_SOURCE_FIELD), (String) ex.get(Extractor.FIELD_TARGET_FIELD), (Map<String, Object>) ex.get(Extractor.FIELD_EXTRACTOR_CONFIG), (String) ex.get(Extractor.FIELD_CREATOR_USER_ID), getConvertersOfExtractor(ex), Extractor.ConditionType.valueOf(((String) ex.get(Extractor.FIELD_CONDITION_TYPE)).toUpperCase(Locale.ENGLISH)), (String) ex.get(Extractor.FIELD_CONDITION_VALUE));
listBuilder.add(extractor);
} catch (Exception e) {
LOG.error("Cannot build extractor from persisted data. Skipping.", e);
}
}
return listBuilder.build();
}
use of org.graylog2.migrations.Migration in project graylog2-server by Graylog2.
the class UserPermissionsToGrantsMigrationTest method setUp.
@BeforeEach
void setUp(MongoDBTestService mongodb, MongoJackObjectMapperProvider mongoJackObjectMapperProvider, GRNRegistry grnRegistry, TestUserService userService, @Mock ViewService viewService) {
this.grnRegistry = grnRegistry;
this.viewService = viewService;
this.userSelfEditPermissionCount = new Permissions(ImmutableSet.of()).userSelfEditPermissions("dummy").size();
dbGrantService = new DBGrantService(mongodb.mongoConnection(), mongoJackObjectMapperProvider, grnRegistry);
this.userService = userService;
DBGrantService dbGrantService = new DBGrantService(mongodb.mongoConnection(), mongoJackObjectMapperProvider, grnRegistry);
migration = new UserPermissionsToGrantsMigration(userService, dbGrantService, grnRegistry, viewService, "admin");
}
use of org.graylog2.migrations.Migration in project graylog2-server by Graylog2.
the class V20200102140000_UnifyEventSeriesIdTestIT method setUp.
@Before
public void setUp() throws Exception {
final ObjectMapperProvider objectMapperProvider = new ObjectMapperProvider();
final ObjectMapper objectMapper = objectMapperProvider.get();
objectMapper.registerSubtypes(new NamedType(AggregationEventProcessorConfig.class, AggregationEventProcessorConfig.TYPE_NAME));
objectMapper.registerSubtypes(new NamedType(TemplateFieldValueProvider.Config.class, TemplateFieldValueProvider.Config.TYPE_NAME));
objectMapper.registerSubtypes(new NamedType(PersistToStreamsStorageHandler.Config.class, PersistToStreamsStorageHandler.Config.TYPE_NAME));
final MongoJackObjectMapperProvider mapperProvider = new MongoJackObjectMapperProvider(objectMapper);
eventDefinitionService = new DBEventDefinitionService(mongodb.mongoConnection(), mapperProvider, dbEventProcessorStateService, mock(EntityOwnershipService.class));
migration = new V20200102140000_UnifyEventSeriesId(clusterConfigService, eventDefinitionService, objectMapperProvider);
}
Aggregations