Search in sources :

Example 81 with QueryParametersBase

use of org.ovirt.engine.core.common.queries.QueryParametersBase in project ovirt-engine by oVirt.

the class Frontend method runMultipleQueries.

/**
 * Run multiple queries in a single request to the back-end.
 * @param queryTypeList A list of {@code QueryType}s.
 * @param queryParamsList A list of parameters associated with each query.
 * @param callback The callback to call when the query completes.
 * @param state The state object.
 */
public void runMultipleQueries(final List<QueryType> queryTypeList, final List<QueryParametersBase> queryParamsList, final IFrontendMultipleQueryAsyncCallback callback, final Object state) {
    VdcOperationCallbackList<VdcOperation<QueryType, QueryParametersBase>, List<QueryReturnValue>> multiCallback = new VdcOperationCallbackList<VdcOperation<QueryType, QueryParametersBase>, List<QueryReturnValue>>() {

        @Override
        public void onSuccess(final List<VdcOperation<QueryType, QueryParametersBase>> operationList, final List<QueryReturnValue> resultObject) {
            // $NON-NLS-1$
            logger.finer("Succesful returned result from runMultipleQueries!");
            FrontendMultipleQueryAsyncResult f = new FrontendMultipleQueryAsyncResult(queryTypeList, queryParamsList, resultObject);
            callback.executed(f);
            fireAsyncQuerySucceededEvent(state);
        }

        @Override
        public void onFailure(final List<VdcOperation<QueryType, QueryParametersBase>> operationList, final Throwable caught) {
            try {
                if (ignoreFailure(caught)) {
                    return;
                }
                // $NON-NLS-1$
                logger.log(Level.SEVERE, "Failed to execute runMultipleQueries: " + caught, caught);
                FrontendMultipleQueryAsyncResult f = new FrontendMultipleQueryAsyncResult(queryTypeList, queryParamsList, null);
                failureEventHandler(caught);
                callback.executed(f);
            } finally {
                fireAsyncQueryFailedEvent(state);
            }
        }
    };
    List<VdcOperation<?, ?>> operationList = new ArrayList<>();
    for (int i = 0; i < queryTypeList.size(); i++) {
        QueryParametersBase parameters = queryParamsList.get(i);
        // Why do we do this?
        parameters.setRefresh(false);
        initQueryParamsFilter(parameters);
        operationList.add(new VdcOperation<>(queryTypeList.get(i), parameters, true, multiCallback, false));
    }
    fireAsyncOperationStartedEvent(state);
    getOperationManager().addOperationList(operationList);
}
Also used : VdcOperation(org.ovirt.engine.ui.frontend.communication.VdcOperation) ArrayList(java.util.ArrayList) FrontendMultipleQueryAsyncResult(org.ovirt.engine.ui.uicompat.FrontendMultipleQueryAsyncResult) QueryReturnValue(org.ovirt.engine.core.common.queries.QueryReturnValue) VdcOperationCallbackList(org.ovirt.engine.ui.frontend.communication.VdcOperationCallbackList) QueryParametersBase(org.ovirt.engine.core.common.queries.QueryParametersBase) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) VdcOperationCallbackList(org.ovirt.engine.ui.frontend.communication.VdcOperationCallbackList) QueryType(org.ovirt.engine.core.common.queries.QueryType)

Example 82 with QueryParametersBase

use of org.ovirt.engine.core.common.queries.QueryParametersBase in project ovirt-engine by oVirt.

the class SearchQuery method initQueryData.

private QueryData initQueryData(boolean useCache) {
    final String ASTR = "*";
    QueryData data = null;
    boolean isExistsValue = false;
    boolean IsFromYesterday = false;
    boolean isSafe = false;
    String searchKey = "";
    try {
        if (getParameters().getMaxCount() < 0) {
            throw new RuntimeException(String.format("Illegal max count value for query : %s", getParameters().getMaxCount()));
        }
        String searchText = getParameters().getSearchPattern();
        // do not cache expressions with '*' since it is translated to specific IDs that might be changed
        useCache = useCache && !searchText.contains(ASTR);
        if (useCache) {
            // first lets check the cache of queries.
            searchKey = String.format("%1$s,%2$s,%3$s", searchText, getParameters().getMaxCount(), getParameters().getCaseSensitive());
            data = queriesCache.get(searchKey);
            isExistsValue = data != null;
            if (isExistsValue) {
                TimeSpan span = DateTime.getNow().subtract(new Date(data.getDate()));
                if (span.Days >= 1) {
                    IsFromYesterday = true;
                }
            }
        }
        // search text.
        if (!isExistsValue || IsFromYesterday) {
            log.debug("ResourceManager::searchBusinessObjects(''{}'') - entered", searchText);
            final char AT = '@';
            String queryAuthz = null;
            String queryNamespace = null;
            ISyntaxChecker curSyntaxChecker;
            Matcher m = adSearchPattern.matcher(searchText);
            // checks if this is a AD query, if it is, verify given profile and namespace and pass the query
            if (m.matches()) {
                final String COLON = ":";
                String prefix = m.group("prefix");
                searchText = m.group("content");
                // get profile
                List<String> profiles = backend.runInternalQuery(QueryType.GetDomainList, new QueryParametersBase(getParameters().getSessionId())).getReturnValue();
                for (String profile : profiles) {
                    if (searchText.startsWith(profile + COLON)) {
                        queryAuthz = profile;
                        searchText = searchText.replaceFirst(profile + COLON, StringUtils.EMPTY);
                        break;
                    }
                }
                if (queryAuthz == null) {
                    queryAuthz = getDefaultAuthz();
                }
                // get namespace
                Map<String, List<String>> namespacesMap = backend.runInternalQuery(QueryType.GetAvailableNamespaces, new QueryParametersBase(getParameters().getSessionId())).getReturnValue();
                List<String> namespaces = namespacesMap.get(queryAuthz);
                for (String namespace : namespaces) {
                    if (searchText.startsWith(namespace + COLON)) {
                        queryNamespace = namespace;
                        searchText = searchText.replace(namespace + COLON, StringUtils.EMPTY);
                        break;
                    }
                }
                // ADUSER/ADGROUP<profile>::<query>
                if (searchText.startsWith(COLON)) {
                    searchText = prefix + searchText;
                } else {
                    searchText = prefix + COLON + searchText;
                }
                curSyntaxChecker = SyntaxCheckerFactory.createADSyntaxChecker(LDAP);
            } else {
                curSyntaxChecker = SyntaxCheckerFactory.createBackendSyntaxChecker(LDAP);
            }
            SyntaxContainer searchObj = curSyntaxChecker.analyzeSyntaxState(searchText, true);
            // set the case-sensitive flag
            searchObj.setCaseSensitive(getParameters().getCaseSensitive());
            // If a number > maxValue is given then maxValue will be used
            searchObj.setMaxCount(Math.min(Integer.MAX_VALUE, getParameters().getMaxCount()));
            // setting FromSearch value
            searchObj.setSearchFrom(getParameters().getSearchFrom());
            if (searchObj.getError() != SyntaxError.NO_ERROR) {
                int startPos = searchObj.getErrorStartPos();
                int endPos = searchObj.getErrorEndPos();
                int length = endPos - startPos;
                String error = (length > 0 && ((startPos + 1 + length) < searchText.length()) && (endPos + 1 < searchText.length())) ? searchText.substring(0, startPos) + "$" + searchText.substring(startPos + 1, startPos + 1 + length) + "$" + searchText.substring(endPos + 1) : searchObj.getError().toString();
                getQueryReturnValue().setExceptionString(error);
                log.info("ResourceManager::searchBusinessObjects - erroneous search text - ''{}'' error - ''{}''", searchText, error);
                return null;
            }
            if (!searchObj.getvalid()) {
                log.warn("ResourceManager::searchBusinessObjects - Invalid search text - ''{}''", searchText);
                return null;
            }
            // find if this is a trivial search expression (like 'Vms:' etc).
            isSafe = SearchObjects.isSafeExpression(searchText);
            // An expression is considered safe if matches a trivial search.
            data = new QueryData(curSyntaxChecker.generateQueryFromSyntaxContainer(searchObj, isSafe), DateTime.getNow().getTime(), queryAuthz, queryNamespace);
            // query from scratch.
            if (!containsStaticInValues(data.getQuery())) {
                queriesCache.put(searchKey, data);
            }
        }
    } catch (SearchEngineIllegalCharacterException e) {
        log.error("Search expression can not end with ESCAPE character: {}", getParameters().getSearchPattern());
        data = null;
    } catch (SqlInjectionException e) {
        log.error("Sql Injection in search: {}", getParameters().getSearchPattern());
        data = null;
    } catch (RuntimeException ex) {
        log.warn("Illegal search: {}: {}", getParameters().getSearchPattern(), ex.getMessage());
        log.debug("Exception", ex);
        throw ex;
    }
    return data;
}
Also used : QueryData(org.ovirt.engine.core.aaa.QueryData) Matcher(java.util.regex.Matcher) SearchEngineIllegalCharacterException(org.ovirt.engine.core.common.errors.SearchEngineIllegalCharacterException) ISyntaxChecker(org.ovirt.engine.core.searchbackend.ISyntaxChecker) Date(java.util.Date) TimeSpan(org.ovirt.engine.core.compat.TimeSpan) SyntaxContainer(org.ovirt.engine.core.searchbackend.SyntaxContainer) SqlInjectionException(org.ovirt.engine.core.common.errors.SqlInjectionException) QueryParametersBase(org.ovirt.engine.core.common.queries.QueryParametersBase) List(java.util.List) ArrayList(java.util.ArrayList)

Example 83 with QueryParametersBase

use of org.ovirt.engine.core.common.queries.QueryParametersBase in project ovirt-engine by oVirt.

the class SsoPostLoginFilter method runQuery.

protected Object runQuery(QueryType queryType, String sessionId, InitialContext ctx) {
    QueryParametersBase queryParams = new QueryParametersBase();
    queryParams.setSessionId(sessionId);
    queryParams.setFiltered(FILTER_QUERIES);
    QueryReturnValue result = FiltersHelper.getBackend(ctx).runQuery(queryType, queryParams);
    return result != null && result.getSucceeded() ? result.getReturnValue() : null;
}
Also used : QueryReturnValue(org.ovirt.engine.core.common.queries.QueryReturnValue) QueryParametersBase(org.ovirt.engine.core.common.queries.QueryParametersBase)

Example 84 with QueryParametersBase

use of org.ovirt.engine.core.common.queries.QueryParametersBase in project ovirt-engine by oVirt.

the class GetAllVmsAndVmPoolsQuery method executeQueryCommand.

@Override
protected void executeQueryCommand() {
    boolean isSucceeded = true;
    List<Object> retValList = new ArrayList<>();
    // Add all VMs that the user has direct or indirect privileges on
    // that do not belong to a VM Pool
    QueryReturnValue queryResult = runInternalQuery(QueryType.GetAllVms, getParameters());
    if (queryResult != null && queryResult.getSucceeded()) {
        for (VM vm : queryResult.<List<VM>>getReturnValue()) {
            if (Guid.isNullOrEmpty(vm.getVmPoolId())) {
                retValList.add(vm);
            }
        }
    } else {
        isSucceeded = false;
    }
    // Query for all VMs the user has direct permissions on, if
    // a user has taken a VM from VM Pool it should be returned here
    queryResult = runInternalQuery(QueryType.GetAllVmsForUser, getParameters());
    if (queryResult != null && queryResult.getSucceeded()) {
        for (VM vm : queryResult.<List<VM>>getReturnValue()) {
            if (!retValList.contains(vm)) {
                retValList.add(vm);
            }
        }
    } else {
        isSucceeded = false;
    }
    if (isSucceeded) {
        queryResult = runInternalQuery(QueryType.GetAllVmPoolsAttachedToUser, new QueryParametersBase());
        if (queryResult != null && queryResult.getSucceeded()) {
            retValList.addAll(queryResult.<List<VmPool>>getReturnValue());
        } else {
            isSucceeded = false;
        }
    }
    if (!isSucceeded) {
        getQueryReturnValue().setSucceeded(false);
        return;
    }
    getQueryReturnValue().setReturnValue(retValList);
}
Also used : QueryReturnValue(org.ovirt.engine.core.common.queries.QueryReturnValue) VmPool(org.ovirt.engine.core.common.businessentities.VmPool) VM(org.ovirt.engine.core.common.businessentities.VM) ArrayList(java.util.ArrayList) QueryParametersBase(org.ovirt.engine.core.common.queries.QueryParametersBase) List(java.util.List) ArrayList(java.util.ArrayList)

Example 85 with QueryParametersBase

use of org.ovirt.engine.core.common.queries.QueryParametersBase in project ovirt-engine by oVirt.

the class VmInterfaceListModel method updateNetworkFilterParameterMap.

private void updateNetworkFilterParameterMap(List<VmNetworkInterface> vmInterfaces) {
    List<QueryType> queryTypes = new ArrayList<>();
    List<QueryParametersBase> queryParametersBases = new ArrayList<>();
    vmInterfaces.stream().forEach(iface -> {
        queryTypes.add(QueryType.GetVmInterfaceFilterParametersByVmInterfaceId);
        queryParametersBases.add(new IdQueryParameters(iface.getId()));
    });
    final IFrontendMultipleQueryAsyncCallback callback = multiResult -> {
        Map<Guid, List<VmNicFilterParameter>> networkFilterMap = new HashMap<>(vmInterfaces.size());
        for (int i = 0; i < multiResult.getReturnValues().size(); i++) {
            List<VmNicFilterParameter> params = multiResult.getReturnValues().get(i).getReturnValue();
            networkFilterMap.put(vmInterfaces.get(i).getId(), params);
        }
        setMapNicFilterParameter(networkFilterMap);
        setItems(vmInterfaces);
    };
    if (vmInterfaces.isEmpty()) {
        setItems(vmInterfaces);
    }
    Frontend.getInstance().runMultipleQueries(queryTypes, queryParametersBases, callback);
}
Also used : QueryType(org.ovirt.engine.core.common.queries.QueryType) Guid(org.ovirt.engine.core.compat.Guid) HelpTag(org.ovirt.engine.ui.uicommonweb.help.HelpTag) HashMap(java.util.HashMap) SearchableListModel(org.ovirt.engine.ui.uicommonweb.models.SearchableListModel) IdQueryParameters(org.ovirt.engine.core.common.queries.IdQueryParameters) ArrayList(java.util.ArrayList) VmNicFilterParameter(org.ovirt.engine.core.common.businessentities.network.VmNicFilterParameter) ActionType(org.ovirt.engine.core.common.action.ActionType) Frontend(org.ovirt.engine.ui.frontend.Frontend) Map(java.util.Map) ConstantsManager(org.ovirt.engine.ui.uicompat.ConstantsManager) AsyncDataProvider(org.ovirt.engine.ui.uicommonweb.dataprovider.AsyncDataProvider) QueryParametersBase(org.ovirt.engine.core.common.queries.QueryParametersBase) ActionUtils(org.ovirt.engine.core.common.ActionUtils) VmNetworkInterface(org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface) Collection(java.util.Collection) UICommand(org.ovirt.engine.ui.uicommonweb.UICommand) Objects(java.util.Objects) VmGuestAgentInterface(org.ovirt.engine.core.common.businessentities.VmGuestAgentInterface) Scheduler(com.google.gwt.core.client.Scheduler) List(java.util.List) VM(org.ovirt.engine.core.common.businessentities.VM) IFrontendMultipleQueryAsyncCallback(org.ovirt.engine.ui.uicompat.IFrontendMultipleQueryAsyncCallback) VMStatus(org.ovirt.engine.core.common.businessentities.VMStatus) PropertyChangedEventArgs(org.ovirt.engine.ui.uicompat.PropertyChangedEventArgs) IdQueryParameters(org.ovirt.engine.core.common.queries.IdQueryParameters) ArrayList(java.util.ArrayList) QueryParametersBase(org.ovirt.engine.core.common.queries.QueryParametersBase) IFrontendMultipleQueryAsyncCallback(org.ovirt.engine.ui.uicompat.IFrontendMultipleQueryAsyncCallback) ArrayList(java.util.ArrayList) List(java.util.List) QueryType(org.ovirt.engine.core.common.queries.QueryType) HashMap(java.util.HashMap) Map(java.util.Map) VmNicFilterParameter(org.ovirt.engine.core.common.businessentities.network.VmNicFilterParameter)

Aggregations

QueryParametersBase (org.ovirt.engine.core.common.queries.QueryParametersBase)89 ArrayList (java.util.ArrayList)41 QueryReturnValue (org.ovirt.engine.core.common.queries.QueryReturnValue)40 QueryType (org.ovirt.engine.core.common.queries.QueryType)37 Test (org.junit.Test)33 StorageServerConnectionQueryParametersBase (org.ovirt.engine.core.common.queries.StorageServerConnectionQueryParametersBase)22 SearchParameters (org.ovirt.engine.core.common.queries.SearchParameters)18 List (java.util.List)15 IdQueryParameters (org.ovirt.engine.core.common.queries.IdQueryParameters)15 Guid (org.ovirt.engine.core.compat.Guid)13 HashMap (java.util.HashMap)11 StatusCodeException (com.google.gwt.user.client.rpc.StatusCodeException)9 HashSet (java.util.HashSet)7 Map (java.util.Map)6 Cluster (org.ovirt.engine.core.common.businessentities.Cluster)6 Frontend (org.ovirt.engine.ui.frontend.Frontend)6 Collection (java.util.Collection)5 Quota (org.ovirt.engine.core.common.businessentities.Quota)5 DbUser (org.ovirt.engine.core.common.businessentities.aaa.DbUser)5 EntityModel (org.ovirt.engine.ui.uicommonweb.models.EntityModel)5