}
return cfg;
}
private Answer execute(AggregationControlCommand cmd) {
Action action = cmd.getAction();
String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
assert routerName != null;
assert cmd.getRouterAccessIp() != null;
if (action == Action.Start) {
assert (!_vrAggregateCommandsSet.containsKey(routerName));
Queue<NetworkElementCommand> queue = new LinkedBlockingQueue<>();
_vrAggregateCommandsSet.put(routerName, queue);
return new Answer(cmd);
} else if (action == Action.Finish) {
Queue<NetworkElementCommand> queue = _vrAggregateCommandsSet.get(routerName);
int answerCounts = 0;
try {
StringBuilder sb = new StringBuilder();
sb.append("#Apache CloudStack Virtual Router Config File\n");
sb.append("<version>\n" + _cfgVersion + "\n</version>\n");
for (NetworkElementCommand command : queue) {
answerCounts += command.getAnswersCount();
List<ConfigItem> cfg = generateCommandCfg(command);
if (cfg == null) {
s_logger.warn("Unknown commands for VirtualRoutingResource, but continue: " + cmd.toString());
continue;
}
for (ConfigItem c : cfg) {
if (c.isFile()) {
sb.append("<file>\n");
sb.append(c.getFilePath() + c.getFileName() + "\n");
sb.append(c.getFileContents() + "\n");
sb.append("</file>\n");
} else {
sb.append("<script>\n");
sb.append("/opt/cloud/bin/" + c.getScript() + " " + c.getArgs() + "\n");
sb.append("</script>\n");
}
}
}
String cfgFilePath = "/var/cache/cloud/";
String cfgFileName = "VR-"+ UUID.randomUUID().toString() + ".cfg";
ExecutionResult result = _vrDeployer.createFileInVR(cmd.getRouterAccessIp(), cfgFilePath, cfgFileName, sb.toString());
if (!result.isSuccess()) {
return new Answer(cmd, false, result.getDetails());
}
// 120s is the minimal timeout
int timeout = answerCounts * _eachTimeout;
if (timeout < 120) {
timeout = 120;
}
result = _vrDeployer.executeInVR(cmd.getRouterAccessIp(), VRScripts.VR_CFG, "-c " + cfgFilePath + cfgFileName, timeout);
if (!result.isSuccess()) {
return new Answer(cmd, false, result.getDetails());
}
return new Answer(cmd);
} finally {
queue.clear();
_vrAggregateCommandsSet.remove(routerName);
}
} else if (action == Action.Cleanup) {
assert (_vrAggregateCommandsSet.containsKey(routerName));
Queue<NetworkElementCommand> queue = _vrAggregateCommandsSet.get(routerName);
if (queue != null) {
queue.clear();
}
_vrAggregateCommandsSet.remove(routerName);
return new Answer(cmd);
}
return new Answer(cmd, false, "Fail to recongize aggregation action " + action.toString());
}