-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from JNU-econovation/validator
[REFACTOR] Validator 패키지
- Loading branch information
Showing
13 changed files
with
245 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...ucture/log-writer/src/main/java/com/whoz_in/log_writer/common/validation/BiValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.whoz_in.log_writer.common.validation; | ||
|
||
@FunctionalInterface | ||
public interface BiValidator<T, U>{ | ||
ValidationResult getValidationResult(T t, U u); | ||
|
||
default void validate(T target1, U target2){ | ||
ValidationResult validationResult = getValidationResult(target1, target2); | ||
if (validationResult.hasErrors()) { | ||
throw new ValidationException(validationResult); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...og-writer/src/main/java/com/whoz_in/log_writer/common/validation/ValidationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.whoz_in.log_writer.common.validation; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
public class ValidationException extends RuntimeException{ | ||
|
||
public ValidationException(String message) { | ||
super(message); | ||
} | ||
|
||
public ValidationException(ValidationResult result) { | ||
super(String.join(", ", result.getErrors())); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...e/log-writer/src/main/java/com/whoz_in/log_writer/common/validation/ValidationResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.whoz_in.log_writer.common.validation; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public class ValidationResult { | ||
private final List<String> errors; | ||
|
||
public ValidationResult() { | ||
this.errors = new ArrayList<>(); | ||
} | ||
|
||
public ValidationResult(Collection<String> errors) { | ||
this.errors = new ArrayList<>(errors); | ||
} | ||
|
||
public void addError(String error) { | ||
errors.add(error); | ||
} | ||
|
||
public void addErrors(Collection<String> errors) { | ||
this.errors.addAll(errors); | ||
} | ||
|
||
public boolean hasErrors() { | ||
return !errors.isEmpty(); | ||
} | ||
|
||
public List<String> getErrors() { | ||
return errors; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.join(", ", errors); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...tructure/log-writer/src/main/java/com/whoz_in/log_writer/common/validation/Validator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.whoz_in.log_writer.common.validation; | ||
|
||
|
||
//검증 원인을 여러 개 담고 싶었는데, Spring Validator는 단일 객체를 대상으로 하는 필드 중심 검증이기 때문에 만듦 | ||
@FunctionalInterface | ||
public interface Validator<T> { | ||
ValidationResult getValidationResult(T t); | ||
|
||
default void validate(T target){ | ||
ValidationResult validationResult = getValidationResult(target); | ||
if (validationResult.hasErrors()) { | ||
throw new ValidationException(validationResult); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...iter/src/main/java/com/whoz_in/log_writer/system_validator/CommandInstalledValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.whoz_in.log_writer.system_validator; | ||
|
||
import com.whoz_in.log_writer.common.validation.ValidationResult; | ||
import com.whoz_in.log_writer.common.validation.Validator; | ||
import com.whoz_in.log_writer.common.process.TransientProcess; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CommandInstalledValidator implements Validator<String> { | ||
|
||
@Override | ||
public ValidationResult getValidationResult(String command){ | ||
ValidationResult validationResult = new ValidationResult(); | ||
|
||
List<String> results = new TransientProcess("which " + command).resultList(); | ||
if (results.isEmpty() || !results.get(0).contains("/")) { | ||
validationResult.addError(command + "가 설치되지 않았습니다."); | ||
} | ||
return validationResult; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...iter/src/main/java/com/whoz_in/log_writer/system_validator/NetworkInterfaceValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.whoz_in.log_writer.system_validator; | ||
|
||
import com.whoz_in.log_writer.common.NetworkInterface; | ||
import com.whoz_in.log_writer.common.validation.BiValidator; | ||
import com.whoz_in.log_writer.common.validation.ValidationResult; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
//세팅된 NetworkInterface들이 시스템에 존재하는 NetworkInterface인지 확인 | ||
@Component | ||
@RequiredArgsConstructor | ||
public class NetworkInterfaceValidator implements BiValidator<List<NetworkInterface>, List<NetworkInterface>> { | ||
|
||
@Override | ||
public ValidationResult getValidationResult(List<NetworkInterface> system, List<NetworkInterface> setting) { | ||
ValidationResult validationResult = new ValidationResult(); | ||
|
||
List<NetworkInterface> unmatchedNIs = setting.stream() | ||
.filter(ni -> !system.contains(ni)) | ||
.toList(); | ||
|
||
if (!unmatchedNIs.isEmpty()) { | ||
validationResult.addError( | ||
"설정된 네트워크 인터페이스가 시스템에 존재하지 않거나 상태가 올바르지 않습니다: \n" + | ||
unmatchedNIs.stream() | ||
.map(Object::toString) | ||
.collect(Collectors.joining("\n")) | ||
); | ||
} | ||
|
||
return validationResult; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...ure/log-writer/src/main/java/com/whoz_in/log_writer/system_validator/SystemValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.whoz_in.log_writer.system_validator; | ||
|
||
|
||
import com.whoz_in.log_writer.common.NetworkInterface; | ||
import com.whoz_in.log_writer.common.validation.ValidationException; | ||
import com.whoz_in.log_writer.common.validation.ValidationResult; | ||
import com.whoz_in.log_writer.common.validation.Validator; | ||
import com.whoz_in.log_writer.config.NetworkConfig; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
|
||
//시스템의 전체적인 검증을 진행하는 클래스 | ||
//SystemValidatorConfig에 의해 빈으로 등록됩니다 | ||
@Slf4j | ||
public final class SystemValidator { | ||
private final NetworkConfig config; | ||
private final SystemNetworkInterfaces systemNetworkInterfaces; | ||
private final NetworkInterfaceValidator networkInterfaceValidator; | ||
|
||
//서버 시작 시 검증 (실패 시 예외가 발생하여 서버 시작이 실패하게 됨) | ||
public SystemValidator( | ||
NetworkConfig config, | ||
SystemNetworkInterfaces systemNetworkInterfaces, | ||
CommandInstalledValidator commandInstalledValidator, | ||
NetworkInterfaceValidator networkInterfaceValidator) { | ||
this.config = config; | ||
this.systemNetworkInterfaces = systemNetworkInterfaces; | ||
this.networkInterfaceValidator = networkInterfaceValidator; | ||
|
||
log.info("시스템 검증을 수행합니다"); | ||
|
||
//커맨드 설치 여부 검증 | ||
List<String> commands = List.of("tshark", "arp-scan", "iwconfig", "nmcli"); | ||
commands.forEach(commandInstalledValidator::validate); | ||
|
||
//네트워크 인터페이스 정보 | ||
List<NetworkInterface> system = systemNetworkInterfaces.getLatest(); | ||
List<NetworkInterface> setting = config.getNetworkInterfaces(); | ||
|
||
//네트워크 인터페이스 출력 | ||
log.info("\n시스템 네트워크 인터페이스 - \n{}\n설정된 네트워크 인터페이스 - \n{}", | ||
system.stream() | ||
.map(Object::toString) | ||
.collect(Collectors.joining("\n")), | ||
setting.stream() | ||
.map(Object::toString) | ||
.collect(Collectors.joining("\n"))); | ||
|
||
//네트워크 인터페이스 상태 검증 | ||
networkInterfaceValidator.validate(system, setting); | ||
|
||
log.info("시스템 검증 완료"); | ||
} | ||
|
||
//정기적으로 시스템 상태를 검사함 | ||
//예외를 띄우지 않고 로깅만 하기 | ||
@Scheduled(fixedDelay = 30000) | ||
private void checkRegularly() { | ||
log.info("시스템 검증 시작.."); | ||
//네트워크 인터페이스 상태 검증 | ||
ValidationResult result = networkInterfaceValidator.getValidationResult( | ||
systemNetworkInterfaces.getLatest(), config.getNetworkInterfaces()); | ||
//더 많은 검증하면 result에 추가하기.. | ||
|
||
if (result.hasErrors()) { | ||
log.error("시스템 검증 실패 : [{}]", result); | ||
return; | ||
} | ||
log.info("시스템 검증 완료"); | ||
} | ||
|
||
} |
Oops, something went wrong.