org.bidib.wizard.common.script.node.FeatureGetAllCommand Maven / Gradle / Ivy
package org.bidib.wizard.common.script.node;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.apache.commons.collections4.CollectionUtils;
import org.bidib.jbidibc.messages.utils.ThreadFactoryBuilder;
import org.bidib.wizard.api.context.ApplicationContext;
import org.bidib.wizard.api.model.NodeInterface;
import org.bidib.wizard.api.service.console.ConsoleService;
import org.bidib.wizard.common.script.AbstractScriptCommand;
import org.bidib.wizard.common.script.DefaultScriptContext;
import org.bidib.wizard.common.script.ScriptExecutionException;
import org.bidib.wizard.common.script.ScriptUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FeatureGetAllCommand extends AbstractScriptCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(FeatureGetAllCommand.class);
public static final String KEY = "featureGetAll";
public static final String HELP =
CODE_BLOCK_START + KEY + prepareHelpHtml(" [--V:P] [--all] [--parallel=]") + DIV_END;
private int[] vidPid;
private boolean processAllNodes;
private boolean processParallel;
public FeatureGetAllCommand(final ConsoleService consoleService) {
super(consoleService, KEY, HELP);
}
@Override
public void parse(String commandLine) {
try (Scanner scanner = new Scanner(commandLine)) {
if (!getKey().equals(scanner.next())) {
LOGGER.info("Invalid command is scanned, key does not match.");
throw new IllegalArgumentException("Invalid command is scanned, key does not match.");
}
line = commandLine.trim();
String arguments = line.substring(KEY.length() + 1);
try {
vidPid = ScriptUtils.scanVidPid(scanner, true);
if (vidPid != null) {
LOGGER.info("Current vidPid: {}:{}", vidPid[0], vidPid[1]);
}
}
catch (IllegalArgumentException ex) {
LOGGER.info("Parse VID/PID failed.", ex);
throw new IllegalArgumentException("Parse VID/PID failed.\n" + getHelpHtml());
}
if (arguments.indexOf("--all") > -1) {
LOGGER.info("Process all nodes is activated.");
processAllNodes = true;
}
try {
String parallel = ScriptUtils.parseStringParam("--parallel=\\w+", arguments);
if (Boolean.parseBoolean(parallel) || "yes".equalsIgnoreCase(parallel)) {
LOGGER.info("Process the requests parallel.");
processParallel = true;
}
}
catch (IllegalArgumentException ex) {
LOGGER.info("The parallel argument is not provided.");
}
}
LOGGER.info("Parsed command.");
}
public int[] getVidPid() {
return vidPid;
}
public boolean isProcessAllNodes() {
return processAllNodes;
}
@Override
protected void internalExecute(final T scripting, final ApplicationContext context) {
LOGGER.info("Get all features. Execute the requests parallel: {}", processParallel);
final List nodes = new LinkedList<>();
if (vidPid != null) {
List currentNodes = scripting.getNodesByVidAndPid(vidPid[0], vidPid[1]);
if (CollectionUtils.isNotEmpty(currentNodes)) {
if (processAllNodes) {
nodes.addAll(currentNodes);
}
else {
nodes.add(currentNodes.get(0));
}
}
else {
addError(context, "No matching node with provided VID/PID found.");
return;
}
}
else if (processAllNodes) {
final List currentNodes = scripting.getAllNodes();
nodes.addAll(currentNodes);
}
else {
NodeInterface node = context.get(DefaultScriptContext.KEY_SELECTED_NODE, NodeInterface.class);
LOGGER.info("Fetched the registered selected node: {}", node);
nodes.add(node);
}
if (CollectionUtils.isEmpty(nodes)) {
LOGGER.warn("No node selected and registered in context. Skip set CV values.");
throw new ScriptExecutionException("No registered node in execution context.", this);
}
if (this.processParallel) {
// we want at least 2 workers
final ThreadFactory namedThreadFactory =
new ThreadFactoryBuilder().setNameFormat("featureGetAllWorkers-thread-%d").build();
ExecutorService worker = Executors.newScheduledThreadPool(nodes.size(), namedThreadFactory);
LOGGER.info("Fetch the features from the nodes with parallel execution.");
try {
for (NodeInterface node : nodes) {
worker.submit(() -> scripting.featuresGetAll(node.getUniqueId(), true));
}
}
finally {
try {
worker.shutdown();
worker.awaitTermination(30, TimeUnit.SECONDS);
}
catch (Exception ex) {
LOGGER.warn("Wait for shutdown of worker threadpool failed.", ex);
}
}
}
else {
for (NodeInterface node : nodes) {
scripting.featuresGetAll(node.getUniqueId(), true);
}
}
}
}