Merge branch 'enable-patterns' into 'master'
Disabling patterns See merge request packmate/Packmate!4
This commit is contained in:
@@ -23,13 +23,14 @@ public class PatternController {
|
|||||||
return service.findAll();
|
return service.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@PostMapping("/{id}")
|
||||||
public void deletePattern(@PathVariable int id) {
|
public void enable(@PathVariable int id, @RequestParam boolean enabled) {
|
||||||
service.deleteById(id);
|
service.enable(id, enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Pattern addPattern(@RequestBody Pattern pattern) {
|
public Pattern addPattern(@RequestBody Pattern pattern) {
|
||||||
|
pattern.setEnabled(true);
|
||||||
return service.save(pattern);
|
return service.save(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ public class Pattern {
|
|||||||
@GeneratedValue(generator = "pattern_generator")
|
@GeneratedValue(generator = "pattern_generator")
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
|
private boolean enabled;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private String value;
|
private String value;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
package ru.serega6531.packmate.model.enums;
|
package ru.serega6531.packmate.model.enums;
|
||||||
|
|
||||||
public enum SubscriptionMessageType {
|
public enum SubscriptionMessageType {
|
||||||
SAVE_SERVICE, SAVE_PATTERN, DELETE_SERVICE, DELETE_PATTERN, NEW_STREAM, COUNTERS_UPDATE
|
SAVE_SERVICE, SAVE_PATTERN, DELETE_SERVICE, ENABLE_PATTERN, DISABLE_PATTERN, NEW_STREAM, COUNTERS_UPDATE
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,8 @@ package ru.serega6531.packmate.service;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
import ru.serega6531.packmate.model.FoundPattern;
|
import ru.serega6531.packmate.model.FoundPattern;
|
||||||
import ru.serega6531.packmate.model.Pattern;
|
import ru.serega6531.packmate.model.Pattern;
|
||||||
import ru.serega6531.packmate.model.Stream;
|
|
||||||
import ru.serega6531.packmate.model.enums.PatternDirectionType;
|
import ru.serega6531.packmate.model.enums.PatternDirectionType;
|
||||||
import ru.serega6531.packmate.model.enums.PatternSearchType;
|
import ru.serega6531.packmate.model.enums.PatternSearchType;
|
||||||
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
|
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
|
||||||
@@ -52,28 +50,24 @@ public class PatternService {
|
|||||||
return new PatternMatcher(bytes, list).findMatches();
|
return new PatternMatcher(bytes, list).findMatches();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
public void enable(int id, boolean enabled) {
|
||||||
public void deleteById(int id) {
|
final Pattern pattern = find(id);
|
||||||
final Optional<Pattern> optional = repository.findById(id);
|
if (pattern != null) {
|
||||||
if (optional.isPresent()) {
|
pattern.setEnabled(enabled);
|
||||||
final Pattern pattern = optional.get();
|
repository.save(pattern);
|
||||||
log.info("Removed pattern {} with value {}", pattern.getName(), pattern.getValue());
|
|
||||||
|
|
||||||
for (Stream stream : pattern.getMatchedStreams()) {
|
if (enabled) {
|
||||||
stream.getFoundPatterns().remove(pattern);
|
log.info("Включен паттерн {} со значением {}", pattern.getName(), pattern.getValue());
|
||||||
stream.getPackets().forEach(p ->
|
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.ENABLE_PATTERN, id));
|
||||||
p.getMatches().removeIf(m ->
|
} else {
|
||||||
m.getPatternId() == pattern.getId()));
|
log.info("Выключен паттерн {} со значением {}", pattern.getName(), pattern.getValue());
|
||||||
|
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.DISABLE_PATTERN, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
patterns.remove(id);
|
|
||||||
repository.delete(pattern);
|
|
||||||
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.DELETE_PATTERN, id));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pattern save(Pattern pattern) {
|
public Pattern save(Pattern pattern) {
|
||||||
if(pattern.getSearchType() == PatternSearchType.REGEX) {
|
if (pattern.getSearchType() == PatternSearchType.REGEX) {
|
||||||
try {
|
try {
|
||||||
PatternMatcher.compilePattern(pattern);
|
PatternMatcher.compilePattern(pattern);
|
||||||
} catch (PatternSyntaxException e) {
|
} catch (PatternSyntaxException e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user