use of com.cloud.utils.exception.ExecutionException in project cloudstack by apache.
the class PaloAltoResource method execute.
private Answer execute(SetStaticNatRulesCommand cmd, int numRetries) {
StaticNatRuleTO[] rules = cmd.getRules();
try {
ArrayList<IPaloAltoCommand> commandList = new ArrayList<IPaloAltoCommand>();
for (StaticNatRuleTO rule : rules) {
if (!rule.revoked()) {
manageStcNatRule(commandList, PaloAltoPrimative.ADD, rule);
} else {
manageStcNatRule(commandList, PaloAltoPrimative.DELETE, rule);
}
}
boolean status = requestWithCommit(commandList);
return new Answer(cmd);
} catch (ExecutionException e) {
s_logger.error(e);
if (numRetries > 0 && refreshPaloAltoConnection()) {
int numRetriesRemaining = numRetries - 1;
s_logger.debug("Retrying SetStaticNatRulesCommand. Number of retries remaining: " + numRetriesRemaining);
return execute(cmd, numRetriesRemaining);
} else {
return new Answer(cmd, e);
}
}
}
use of com.cloud.utils.exception.ExecutionException in project cloudstack by apache.
the class PaloAltoResource method requestWithCommit.
/* Runs a sequence of commands and attempts to commit at the end. */
/* Uses the Command pattern to enable overriding of the response handling if needed. */
private synchronized boolean requestWithCommit(ArrayList<IPaloAltoCommand> commandList) throws ExecutionException {
boolean result = true;
if (commandList.size() > 0) {
// CHECK IF THERE IS PENDING CHANGES THAT HAVE NOT BEEN COMMITTED...
String pending_changes;
Map<String, String> check_params = new HashMap<String, String>();
check_params.put("type", "op");
check_params.put("cmd", "<check><pending-changes></pending-changes></check>");
String check_response = request(PaloAltoMethod.GET, check_params);
Document check_doc = getDocument(check_response);
XPath check_xpath = XPathFactory.newInstance().newXPath();
try {
XPathExpression expr = check_xpath.compile("/response[@status='success']/result/text()");
pending_changes = (String) expr.evaluate(check_doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
throw new ExecutionException(e.getCause().getMessage());
}
if (pending_changes.equals("yes")) {
throw new ExecutionException("The Palo Alto has uncommited changes, so no changes can be made. Try again later or contact your administrator.");
} else {
// ADD A CONFIG LOCK TO CAPTURE THE PALO ALTO RESOURCE
String add_lock_status;
Map<String, String> add_lock_params = new HashMap<String, String>();
add_lock_params.put("type", "op");
add_lock_params.put("cmd", "<request><config-lock><add></add></config-lock></request>");
String add_lock_response = request(PaloAltoMethod.GET, add_lock_params);
Document add_lock_doc = getDocument(add_lock_response);
XPath add_lock_xpath = XPathFactory.newInstance().newXPath();
try {
XPathExpression expr = add_lock_xpath.compile("/response[@status='success']/result/text()");
add_lock_status = (String) expr.evaluate(add_lock_doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
throw new ExecutionException(e.getCause().getMessage());
}
if (add_lock_status.length() == 0) {
throw new ExecutionException("The Palo Alto is locked, no changes can be made at this time.");
}
try {
// RUN THE SEQUENCE OF COMMANDS
for (IPaloAltoCommand command : commandList) {
// run commands and modify result boolean
result = (result && command.execute());
}
// COMMIT THE CHANGES (ALSO REMOVES CONFIG LOCK)
String commit_job_id;
Map<String, String> commit_params = new HashMap<String, String>();
commit_params.put("type", "commit");
commit_params.put("cmd", "<commit></commit>");
String commit_response = requestWithPolling(PaloAltoMethod.GET, commit_params);
Document commit_doc = getDocument(commit_response);
XPath commit_xpath = XPathFactory.newInstance().newXPath();
try {
XPathExpression expr = commit_xpath.compile("/response[@status='success']/result/job/id/text()");
commit_job_id = (String) expr.evaluate(commit_doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
throw new ExecutionException(e.getCause().getMessage());
}
if (commit_job_id.length() == 0) {
// no commit was done, so release the lock...
// REMOVE THE CONFIG LOCK TO RELEASE THE PALO ALTO RESOURCE
String remove_lock_status;
Map<String, String> remove_lock_params = new HashMap<String, String>();
remove_lock_params.put("type", "op");
remove_lock_params.put("cmd", "<request><config-lock><remove></remove></config-lock></request>");
String remove_lock_response = request(PaloAltoMethod.GET, remove_lock_params);
Document remove_lock_doc = getDocument(remove_lock_response);
XPath remove_lock_xpath = XPathFactory.newInstance().newXPath();
try {
XPathExpression expr = remove_lock_xpath.compile("/response[@status='success']/result/text()");
remove_lock_status = (String) expr.evaluate(remove_lock_doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
throw new ExecutionException(e.getCause().getMessage());
}
if (remove_lock_status.length() == 0) {
throw new ExecutionException("Could not release the Palo Alto device. Please notify an administrator!");
}
}
} catch (ExecutionException ex) {
// REVERT TO RUNNING
String revert_job_id;
Map<String, String> revert_params = new HashMap<String, String>();
revert_params.put("type", "op");
revert_params.put("cmd", "<load><config><from>running-config.xml</from></config></load>");
requestWithPolling(PaloAltoMethod.GET, revert_params);
// REMOVE THE CONFIG LOCK TO RELEASE THE PALO ALTO RESOURCE
String remove_lock_status;
Map<String, String> remove_lock_params = new HashMap<String, String>();
remove_lock_params.put("type", "op");
remove_lock_params.put("cmd", "<request><config-lock><remove></remove></config-lock></request>");
String remove_lock_response = request(PaloAltoMethod.GET, remove_lock_params);
Document remove_lock_doc = getDocument(remove_lock_response);
XPath remove_lock_xpath = XPathFactory.newInstance().newXPath();
try {
XPathExpression expr = remove_lock_xpath.compile("/response[@status='success']/result/text()");
remove_lock_status = (String) expr.evaluate(remove_lock_doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
throw new ExecutionException(e.getCause().getMessage());
}
if (remove_lock_status.length() == 0) {
throw new ExecutionException("Could not release the Palo Alto device. Please notify an administrator!");
}
// Bubble up the reason we reverted...
throw ex;
}
return result;
}
} else {
// nothing to do
return true;
}
}
use of com.cloud.utils.exception.ExecutionException in project cloudstack by apache.
the class PaloAltoResource method execute.
private Answer execute(SetFirewallRulesCommand cmd, int numRetries) {
FirewallRuleTO[] rules = cmd.getRules();
try {
ArrayList<IPaloAltoCommand> commandList = new ArrayList<IPaloAltoCommand>();
for (FirewallRuleTO rule : rules) {
if (!rule.revoked()) {
manageFirewallRule(commandList, PaloAltoPrimative.ADD, rule);
} else {
manageFirewallRule(commandList, PaloAltoPrimative.DELETE, rule);
}
}
boolean status = requestWithCommit(commandList);
return new Answer(cmd);
} catch (ExecutionException e) {
s_logger.error(e);
if (numRetries > 0 && refreshPaloAltoConnection()) {
int numRetriesRemaining = numRetries - 1;
s_logger.debug("Retrying SetFirewallRulesCommand. Number of retries remaining: " + numRetriesRemaining);
return execute(cmd, numRetriesRemaining);
} else {
return new Answer(cmd, e);
}
}
}
use of com.cloud.utils.exception.ExecutionException in project cloudstack by apache.
the class PaloAltoResource method login.
private boolean login(String username, String password) throws ExecutionException {
Map<String, String> params = new HashMap<String, String>();
params.put("type", "keygen");
params.put("user", username);
params.put("password", password);
String keygenBody;
try {
keygenBody = request(PaloAltoMethod.GET, params);
} catch (ExecutionException e) {
return false;
}
Document keygen_doc = getDocument(keygenBody);
XPath xpath = XPathFactory.newInstance().newXPath();
try {
XPathExpression expr = xpath.compile("/response[@status='success']/result/key/text()");
_key = (String) expr.evaluate(keygen_doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
throw new ExecutionException(e.getCause().getMessage());
}
if (_key != null) {
return true;
}
return false;
}
use of com.cloud.utils.exception.ExecutionException in project cloudstack by apache.
the class PaloAltoResource method nodeToString.
// return an xml node as a string
private String nodeToString(Node node) throws ExecutionException {
StringWriter sw = new StringWriter();
try {
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
} catch (Throwable t) {
throw new ExecutionException("XML convert error when modifying PA config: " + t.getMessage());
}
return sw.toString();
}
Aggregations