Search in sources :

Example 11 with ExecutionException

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);
        }
    }
}
Also used : StaticNatRuleTO(com.cloud.agent.api.to.StaticNatRuleTO) Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) ExternalNetworkResourceUsageAnswer(com.cloud.agent.api.ExternalNetworkResourceUsageAnswer) ArrayList(java.util.ArrayList) ExecutionException(com.cloud.utils.exception.ExecutionException)

Example 12 with ExecutionException

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;
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) HashMap(java.util.HashMap) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Document(org.w3c.dom.Document) ExecutionException(com.cloud.utils.exception.ExecutionException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 13 with ExecutionException

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);
        }
    }
}
Also used : Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) ExternalNetworkResourceUsageAnswer(com.cloud.agent.api.ExternalNetworkResourceUsageAnswer) ArrayList(java.util.ArrayList) FirewallRuleTO(com.cloud.agent.api.to.FirewallRuleTO) ExecutionException(com.cloud.utils.exception.ExecutionException)

Example 14 with ExecutionException

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;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) HashMap(java.util.HashMap) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ExecutionException(com.cloud.utils.exception.ExecutionException) Document(org.w3c.dom.Document)

Example 15 with ExecutionException

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();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) ExecutionException(com.cloud.utils.exception.ExecutionException)

Aggregations

ExecutionException (com.cloud.utils.exception.ExecutionException)83 ConfigurationException (javax.naming.ConfigurationException)31 IOException (java.io.IOException)30 ArrayList (java.util.ArrayList)23 IpAssocAnswer (com.cloud.agent.api.routing.IpAssocAnswer)20 RemoteException (java.rmi.RemoteException)20 ExternalNetworkResourceUsageAnswer (com.cloud.agent.api.ExternalNetworkResourceUsageAnswer)19 Answer (com.cloud.agent.api.Answer)17 MaintainAnswer (com.cloud.agent.api.MaintainAnswer)16 ReadyAnswer (com.cloud.agent.api.ReadyAnswer)16 com.citrix.netscaler.nitro.exception.nitro_exception (com.citrix.netscaler.nitro.exception.nitro_exception)13 Document (org.w3c.dom.Document)12 XPathExpressionException (javax.xml.xpath.XPathExpressionException)11 HashMap (java.util.HashMap)10 XPath (javax.xml.xpath.XPath)8 XPathExpression (javax.xml.xpath.XPathExpression)8 NodeList (org.w3c.dom.NodeList)7 com.citrix.netscaler.nitro.resource.config.gslb.gslbvserver (com.citrix.netscaler.nitro.resource.config.gslb.gslbvserver)5 com.citrix.netscaler.nitro.resource.config.lb.lbvserver (com.citrix.netscaler.nitro.resource.config.lb.lbvserver)5 com.citrix.netscaler.nitro.resource.config.ns.nsconfig (com.citrix.netscaler.nitro.resource.config.ns.nsconfig)5