Support for FIQL queries on REST retreive operations
[controller.git] / opendaylight / northbound / commons / src / main / java / org / opendaylight / controller / northbound / commons / query / QueryContextImpl.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.northbound.commons.query;
9
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 /*package*/ class QueryContextImpl implements QueryContext {
14
15     private static final Logger LOGGER = LoggerFactory.getLogger(QueryContext.class);
16
17     @Override
18     public <T> Query<T> createQuery(String queryString, Class<T> type) throws QueryException {
19         if (queryString == null || queryString.trim().length() == 0) return null;
20         try {
21             if (LOGGER.isDebugEnabled()) LOGGER.debug("Processing query: {}", queryString);
22             // FiqlParser is a parser generated by javacc
23             Expression expression = FiqlParser.parse(queryString);
24             if (LOGGER.isDebugEnabled()) LOGGER.debug("Query expression: {}", expression);
25             // create Query and return;
26             return new QueryImpl<T>(type, expression);
27         } catch (Exception ex) {
28             if (LOGGER.isDebugEnabled()) LOGGER.error("Query processing failed = {}",
29                     queryString, ex);
30             throw new QueryException("Unable to parse query.", ex);
31         }
32     }
33 }