org.bidib.wizard.common.script.test.SearchNetBidibDeviceAndConnectCommand Maven / Gradle / Ivy
package org.bidib.wizard.common.script.test;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.validator.routines.InetAddressValidator;
import org.bidib.wizard.api.context.ApplicationContext;
import org.bidib.wizard.api.service.console.ConsoleService;
import org.bidib.wizard.common.script.AbstractScriptCommand;
import org.bidib.wizard.common.script.ScriptExecutionException;
import org.bidib.wizard.common.script.ScriptUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SearchNetBidibDeviceAndConnectCommand extends AbstractScriptCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(SearchNetBidibDeviceAndConnectCommand.class);
public static final String KEY = "searchNetBidibDeviceAndConnect";
public static final String HELP =
CODE_BLOCK_START + KEY
+ prepareHelpHtml(
" --id= --startingIp= --addressCount= [--retry=]")
+ DIV_END
+ prepareHelpHtml("\nSearch a netBiDiB device by ping in the provided ip range and connect to the device.");
private String connectionId;
private String startingIp;
private int addressCount;
private int retryCount;
private final List ipList;
public SearchNetBidibDeviceAndConnectCommand(final ConsoleService consoleService) {
super(consoleService, KEY, HELP);
this.ipList = new ArrayList<>();
}
protected String getConnectionId() {
return connectionId;
}
protected String getStartingIp() {
return startingIp;
}
protected int getAddressCount() {
return addressCount;
}
protected int getRetryCount() {
return retryCount;
}
protected List getIpList() {
return ipList;
}
@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 {
connectionId = scanner.next("--id=(.+)").substring(5);
}
catch (Exception ex) {
LOGGER.warn("No connectionId found.", ex);
throw new RuntimeException("connect: No connectionId found.");
}
Pattern pattern = Pattern.compile("--startingIp=(\\d+|\\.?)+");
Matcher matcher = pattern.matcher(arguments);
if (matcher.find()) {
String cvPair = matcher.group();
String[] values = cvPair.split("=");
startingIp = values[1];
final InetAddressValidator validator = InetAddressValidator.getInstance();
if (!validator.isValid(startingIp)) {
LOGGER.warn("Validating provided startingIp failed: {}", startingIp);
throw new IllegalArgumentException("Validating provided startingIp failed: " + startingIp + ".\n");
}
}
else {
LOGGER.warn("Mandatory parameter startingIp is missing.");
throw new IllegalArgumentException("Mandatory parameter startingIp is missing.\n" + getHelpHtml());
}
try {
this.addressCount = ScriptUtils.parseIntParam(scanner, "--addressCount=\\d+");
}
catch (IllegalArgumentException iaEx) {
LOGGER.warn("Parse addressCount value failed.", iaEx);
throw new IllegalArgumentException("Mandatory parameter addressCount is missing.\n" + getHelpHtml());
}
try {
this.retryCount = ScriptUtils.parseIntParam(scanner, "--retryCount=(\\d+)");
}
catch (Exception ex) {
LOGGER.info("No retryCount found.");
}
LOGGER
.info("Parsed command, connectionId: {}, startingIp: {}, addressCount: {}, retryCount: {}",
connectionId, startingIp, addressCount, retryCount);
}
}
@Override
protected void internalExecute(final TestScripting scripting, final ApplicationContext context) {
prepareIpAddressList();
String ipToUse = scripting.findReachableDevice(ipList, retryCount);
if (ipToUse == null) {
LOGGER
.warn("No device found in provided address range, startingIp: {}, addressCount: {}", startingIp,
addressCount);
throw new ScriptExecutionException("No device found.");
}
// prepare the connection to the device
LOGGER.info("Prepare connection to address: {}", ipToUse);
scripting.connect("main", ipToUse);
}
protected void prepareIpAddressList() {
String[] addressParts = this.startingIp.split("\\.");
int startingAddr = Integer.parseInt(addressParts[3]);
// prepare the list of ip addresses to scan
for (int index = 0; index < addressCount; index++) {
String address =
String.format("%s.%s.%s.%d", addressParts[0], addressParts[1], addressParts[2], startingAddr + index);
this.ipList.add(address);
}
}
}