- Edge 디바이스에서 받는 MQTT Message Listener 분기 처리

This commit is contained in:
hyunjujeong 2023-09-04 11:14:44 +09:00
parent 5401b6687f
commit c625560cc6
13 changed files with 170 additions and 47 deletions

View File

@ -1,10 +1,15 @@
package inc.sdt.blokworks.devicedeployer.application;
import inc.sdt.blokworks.devicedeployer.domain.OutboundMessage;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
@Component
public class BashCommand implements CommandInfo{
@Override
public LinkedHashMap<String, Object> put() {
return null;
public LinkedHashMap<String, Object> put(OutboundMessage message, LinkedHashMap<String, Object> map) {
map.put("cmd", message.getCommand());
return map;
}
}

View File

@ -1,7 +1,9 @@
package inc.sdt.blokworks.devicedeployer.application;
import inc.sdt.blokworks.devicedeployer.domain.OutboundMessage;
import java.util.LinkedHashMap;
public interface CommandInfo {
LinkedHashMap<String, Object> put();
LinkedHashMap<String, Object> put(OutboundMessage message, LinkedHashMap<String, Object> map);
}

View File

@ -0,0 +1,43 @@
package inc.sdt.blokworks.devicedeployer.application;
import inc.sdt.blokworks.devicedeployer.domain.OutboundMessage;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
@Component
public class CommandInvoker {
private final BashCommand bashCommand;
private final SystemdCommand systemdCommand;
private final DockerCommand dockerCommand;
private final JsonCommand jsonCommand;
private final DeployCommandInvoker deployCommandInvoker;
public CommandInvoker(BashCommand bashCommand, SystemdCommand systemdCommand, DockerCommand dockerCommand, JsonCommand jsonCommand, DeployCommandInvoker deployCommandInvoker) {
this.bashCommand = bashCommand;
this.systemdCommand = systemdCommand;
this.dockerCommand = dockerCommand;
this.jsonCommand = jsonCommand;
this.deployCommandInvoker = deployCommandInvoker;
}
public LinkedHashMap<String, Object> invoke(OutboundMessage message, LinkedHashMap<String, Object> map) {
switch (message.getCommandType()) {
case bash -> {
return bashCommand.put(message, map);
}
case systemd -> {
return systemdCommand.put(message, map);
}
case docker -> {
return dockerCommand.put(message, map);
}
case deploy -> {
return deployCommandInvoker.invoke(message, map);
}
default -> {
return jsonCommand.put(message, map);
}
}
}
}

View File

@ -25,55 +25,29 @@ public class DefaultDeployerService implements DeployerService{
private final ObjectMapper objectMapper;
private final DeployerRepositoryDelegate deployerRepositoryDelegate;
private final DeployRequestRepositoryDelegate requestRepositoryDelegate;
private final String filePath;
private final CommandInvoker commandInvoker;
public DefaultDeployerService(IMqttClient mqttClient,
ObjectMapper objectMapper,
DeployerRepositoryDelegate deployerRepositoryDelegate,
DeployRequestRepositoryDelegate requestRepositoryDelegate,
@Value("${stackbase.api.host}") String filePath) {
CommandInvoker commandInvoker) {
this.log = LoggerFactory.getLogger(this.getClass());
this.mqttClient = mqttClient;
this.objectMapper = objectMapper;
this.deployerRepositoryDelegate = deployerRepositoryDelegate;
this.requestRepositoryDelegate = requestRepositoryDelegate;
this.filePath = filePath;
this.commandInvoker = commandInvoker;
}
@Override
public void publish(OutboundMessage outboundMessage, String assetCode) {
log.info("[publish] deployMessage = {}, assetCode = {}", outboundMessage, assetCode);
final String url = filePath + outboundMessage.getFileId();
final DeviceType deviceType = outboundMessage.getDeviceType();
LinkedHashMap<String, Object> commandInfo = new LinkedHashMap<>();
try {
switch (outboundMessage.getCommandType()) {
case bash -> commandInfo.put("cmd", outboundMessage.getCommand());
case systemd -> {
commandInfo.put("cmd", outboundMessage.getCommand());
commandInfo.put("service", outboundMessage.getName());
}
case docker -> {
commandInfo.put("cmd", "run");
commandInfo.put("appName", "");
commandInfo.put("name", outboundMessage.getName());
commandInfo.put("image", outboundMessage.getImage());
commandInfo.put("options", outboundMessage.getOptions());
}
case deploy -> {
commandInfo.put("cmd", outboundMessage.getCommand());
commandInfo.put("appName", outboundMessage.getName());
commandInfo.put("fileUrl", url);
commandInfo.put("fileType", outboundMessage.getFileType());
}
case json -> {
commandInfo.put("cmd", String.valueOf(outboundMessage.getCommandType()));
commandInfo.put("appName", outboundMessage.getName());
commandInfo.put("parameter", outboundMessage.getParameters());
}
}
commandInfo = commandInvoker.invoke(outboundMessage, commandInfo);
OutboundMessagePayload payload = new OutboundMessagePayload(
commandInfo,
@ -121,12 +95,4 @@ public class DefaultDeployerService implements DeployerService{
return deployerRepositoryDelegate.findAllByAssetCode(assetCode, page, size);
}
private String convertToJson(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
}catch (JsonProcessingException e) {
throw new IllegalArgumentException();
}
}
}

View File

@ -0,0 +1,27 @@
package inc.sdt.blokworks.devicedeployer.application;
import inc.sdt.blokworks.devicedeployer.domain.OutboundMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
@Component
public class DeployCommand implements CommandInfo {
private final String filePath;
public DeployCommand(@Value("${stackbase.api.host}") String filePath) {
this.filePath = filePath;
}
@Override
public LinkedHashMap<String, Object> put(OutboundMessage message, LinkedHashMap<String, Object> map) {
final String url = filePath + message.getFileId();
map.put("cmd", message.getCommand());
map.put("appName", message.getAppName());
map.put("name", message.getName());
map.put("fileUrl", url);
map.put("fileType", message.getFileType());
return map;
}
}

View File

@ -0,0 +1,28 @@
package inc.sdt.blokworks.devicedeployer.application;
import inc.sdt.blokworks.devicedeployer.domain.OutboundMessage;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
@Component
public class DeployCommandInvoker {
private final DockerCommand dockerCommand;
private final DeployCommand deployCommand;
public DeployCommandInvoker(DockerCommand dockerCommand, DeployCommand deployCommand) {
this.dockerCommand = dockerCommand;
this.deployCommand = deployCommand;
}
public LinkedHashMap<String, Object> invoke(OutboundMessage message, LinkedHashMap<String, Object> map) {
switch (message.getSubCommandType()) {
case systemd -> {
return deployCommand.put(message, map);
}
default -> {
return dockerCommand.put(message, map);
}
}
}
}

View File

@ -0,0 +1,19 @@
package inc.sdt.blokworks.devicedeployer.application;
import inc.sdt.blokworks.devicedeployer.domain.OutboundMessage;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
@Component
public class DockerCommand implements CommandInfo {
@Override
public LinkedHashMap<String, Object> put(OutboundMessage message, LinkedHashMap<String, Object> map) {
map.put("cmd", message.getCommand());
map.put("name", message.getName()); // container 이름
map.put("image", message.getImage());
map.put("options", message.getOptions());
return map;
}
}

View File

@ -0,0 +1,17 @@
package inc.sdt.blokworks.devicedeployer.application;
import inc.sdt.blokworks.devicedeployer.domain.OutboundMessage;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
@Component
public class JsonCommand implements CommandInfo {
@Override
public LinkedHashMap<String, Object> put(OutboundMessage message, LinkedHashMap<String, Object> map) {
map.put("cmd", String.valueOf(message.getCommandType()));
map.put("appName", message.getAppName());
map.put("parameter", message.getParameters());
return map;
}
}

View File

@ -0,0 +1,16 @@
package inc.sdt.blokworks.devicedeployer.application;
import inc.sdt.blokworks.devicedeployer.domain.OutboundMessage;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
@Component
public class SystemdCommand implements CommandInfo {
@Override
public LinkedHashMap<String, Object> put(OutboundMessage message, LinkedHashMap<String, Object> map) {
map.put("cmd", message.getCommand());
map.put("service", message.getAppName());
return map;
}
}

View File

@ -5,8 +5,8 @@ import java.util.LinkedHashMap;
public class OutboundMessage {
private String fileId;
private String fileType;
private String appName;
private String name;
private String appName; // 사용자가 정한 파일 이름
private String name; // stackbase 에 저장된 파일 이름
private String image;
private LinkedHashMap<String, Object> options;
private String command;

View File

@ -39,7 +39,7 @@ public class DeployerController {
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/assets/{assetCode}/apps")
public void deploy(@PathVariable String assetCode,
@Valid @RequestBody OutboundMessageResource resource) {
@RequestBody OutboundMessageResource resource) {
log.info("[deploy] assetCode = {}, resource = {}", assetCode, resource);
String requestId = UUID.randomUUID().toString();
OutboundMessage outboundMessage = outboundMessageResourceConverter.fromResource(resource);

View File

@ -8,11 +8,10 @@ import org.wildfly.common.annotation.NotNull;
import java.util.LinkedHashMap;
record OutboundMessageResource(
@NotNull
String fileId,
String fileType,
@NotNull
String name,
String appName, // 사용자가 정한 파일 이름
String name, // stackbase 에 저장된 파일 이름
String image,
String command,
DeviceType deviceType,

View File

@ -13,6 +13,7 @@ public class OutboundMessageResourceConverter {
return OutboundMessage.builder()
.fileId(resource.fileId())
.fileType(resource.fileType())
.appName(resource.appName() == null ? "" : resource.appName())
.name(resource.name())
.image(resource.image() == null ? "" : resource.image())
.command(resource.command())