* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
+ * Modifications Copyright (c) 2019 Samsung
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
-
import javax.persistence.EntityManager;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
-
import org.onap.so.db.request.beans.InfraActiveRequests;
-import org.onap.so.logger.MsoLogger;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
@Autowired
private EntityManager entityManager;
- protected static MsoLogger msoLogger =
- MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL, InfraActiveRequestsRepositoryImpl.class);
+ protected static Logger logger = LoggerFactory.getLogger(InfraActiveRequestsRepositoryImpl.class);
protected static final String REQUEST_STATUS = "requestStatus";
protected static final String SOURCE = "source";
protected static final String REQUEST_ID = "requestId";
protected static final String REQUESTOR_ID = "requestorId";
protected static final String ACTION = "action";
+ protected static final String OPENV = "operationalEnvironment";
private static final List<String> VALID_COLUMNS =
- Arrays.asList(REQUEST_ID, SERVICE_INSTANCE_ID, SERVICE_INSTANCE_NAME, ACTION, REQUEST_STATUS);
+ Arrays.asList(REQUEST_ID, SERVICE_INSTANCE_ID, SERVICE_INSTANCE_NAME, ACTION, REQUEST_STATUS,
+ VFMODULE_INSTANCE_ID, VNF_INSTANCE_ID, NETWORK_INSTANCE_ID, VOLUME_GROUP_INSTANCE_ID);
/*
final List<Predicate> predicates, final Order order) {
final long startTime = System.currentTimeMillis();
- msoLogger.debug("Execute query on infra active request table");
+ logger.debug("Execute query on infra active request table");
List<InfraActiveRequests> results = new ArrayList<InfraActiveRequests>();
- try {
- final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
- crit.where(cb.and(predicates.toArray(new Predicate[0])));
- crit.orderBy(order);
- results = entityManager.createQuery(crit).getResultList();
+ final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
+ crit.where(cb.and(predicates.toArray(new Predicate[0])));
+ crit.orderBy(order);
+ results = entityManager.createQuery(crit).getResultList();
- } finally {
- msoLogger.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc,
- "Successfully", "RequestDB", "getInfraActiveRequest", null);
- }
return results;
}
/*
* (non-Javadoc)
*
- * @see
- * org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#getRequestFromInfraActive(java.
- * lang.String)
+ * @see org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#getRequestFromInfraActive(java. lang.String)
*/
@Override
public InfraActiveRequests getRequestFromInfraActive(final String requestId) {
final long startTime = System.currentTimeMillis();
- msoLogger.debug("Get request " + requestId + " from InfraActiveRequests DB");
+ logger.debug("Get request {} from InfraActiveRequests DB", requestId);
InfraActiveRequests ar = null;
- try {
- final Query query = entityManager.createQuery(
- "from InfraActiveRequests where requestId = :requestId OR clientRequestId = :requestId");
- query.setParameter(REQUEST_ID, requestId);
- ar = this.getSingleResult(query);
- } finally {
-
- msoLogger.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc,
- "Successfully", "InfraRequestDB", "getRequestFromInfraActive", null);
- }
+ final Query query = entityManager
+ .createQuery("from InfraActiveRequests where requestId = :requestId OR clientRequestId = :requestId");
+ query.setParameter(REQUEST_ID, requestId);
+ ar = this.getSingleResult(query);
return ar;
}
/*
* (non-Javadoc)
*
- * @see
- * org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#checkInstanceNameDuplicate(java.
- * util.HashMap, java.lang.String, java.lang.String)
+ * @see org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#checkInstanceNameDuplicate(java. util.HashMap,
+ * java.lang.String, java.lang.String)
*/
@Override
- public InfraActiveRequests checkInstanceNameDuplicate(final HashMap<String, String> instanceIdMap,
+ public InfraActiveRequests checkInstanceNameDuplicate(final Map<String, String> instanceIdMap,
final String instanceName, final String requestScope) {
final List<Predicate> predicates = new LinkedList<>();
predicates.add(cb.equal(tableRoot.get(NETWORK_INSTANCE_NAME), instanceName));
} else if (requestScope.equals("configuration")) {
predicates.add(cb.equal(tableRoot.get(CONFIGURATION_INSTANCE_NAME), instanceName));
- } else if (requestScope.equals("operationalEnvironment")) {
+ } else if (requestScope.equals(OPENV)) {
predicates.add(cb.equal(tableRoot.get(OPERATIONAL_ENV_NAME), instanceName));
}
} else {
if (instanceIdMap != null) {
- if ("service".equals(requestScope) && instanceIdMap.get("serviceInstanceId") != null) {
+ if ("service".equals(requestScope) && instanceIdMap.get(SERVICE_INSTANCE_ID) != null) {
predicates
.add(cb.equal(tableRoot.get(SERVICE_INSTANCE_ID), instanceIdMap.get("serviceInstanceId")));
}
instanceIdMap.get("configurationInstanceId")));
}
- if (requestScope.equals("operationalEnvironment")
- && instanceIdMap.get("operationalEnvironmentId") != null) {
+ if (requestScope.equals(OPENV) && instanceIdMap.get("operationalEnvironmentId") != null) {
predicates.add(
cb.equal(tableRoot.get(OPERATIONAL_ENV_ID), instanceIdMap.get("operationalEnvironmentId")));
}
}
}
- if(!predicates.isEmpty()){
- predicates.add(tableRoot.get(REQUEST_STATUS)
- .in(Arrays.asList("PENDING", "IN_PROGRESS", "TIMEOUT", "PENDING_MANUAL_TASK")));
-
- final Order order = cb.desc(tableRoot.get(START_TIME));
-
- final List<InfraActiveRequests> dupList = executeInfraQuery(crit, predicates, order);
-
- if (dupList != null && !dupList.isEmpty()) {
- infraActiveRequests = dupList.get(0);
- }
+ if (!predicates.isEmpty()) {
+ predicates.add(tableRoot.get(REQUEST_STATUS)
+ .in(Arrays.asList("PENDING", "IN_PROGRESS", "TIMEOUT", "PENDING_MANUAL_TASK")));
+
+ final Order order = cb.desc(tableRoot.get(START_TIME));
+
+ final List<InfraActiveRequests> dupList = executeInfraQuery(crit, predicates, order);
+
+ if (dupList != null && !dupList.isEmpty()) {
+ infraActiveRequests = dupList.get(0);
+ }
}
return infraActiveRequests;
predicates.add(cb.between(tableRoot.get(mapKey), minTime, maxTime));
}
} catch (final Exception e) {
- msoLogger.debug("Exception in getOrchestrationFiltersFromInfraActive(): + " + e.getMessage(), e);
+ logger.debug("Exception in getOrchestrationFiltersFromInfraActive(): {}", e.getMessage(), e);
return null;
}
} else if ("DOES_NOT_EQUAL".equalsIgnoreCase(entry.getValue().get(0))) {
// as the same requestorId can also match on different API methods
final String resourceType = orchestrationMap.get("resourceType");
if (resourceType == null) {
- predicates.add(cb.equal(tableRoot.get("requestScope"), "operationalEnvironment"));
+ predicates.add(cb.equal(tableRoot.get("requestScope"), OPENV));
}
for (final Map.Entry<String, String> entry : orchestrationMap.entrySet()) {
predicates.add(cb.between(tableRoot.get(mapKey), minTime, maxTime));
} catch (final Exception e) {
- msoLogger.debug("Exception in getCloudOrchestrationFiltersFromInfraActive(): + " + e.getMessage());
+ logger.debug("Exception in getCloudOrchestrationFiltersFromInfraActive(): {}", e.getMessage());
return null;
}
} else {
/*
* (non-Javadoc)
*
- * @see
- * org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#getRequestListFromInfraActive(java
- * .lang.String, java.lang.String, java.lang.String)
+ * @see org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#getRequestListFromInfraActive(java .lang.String,
+ * java.lang.String, java.lang.String)
*/
@Override
public List<InfraActiveRequests> getRequestListFromInfraActive(final String queryAttributeName,
final String queryValue, final String requestType) {
- msoLogger.debug("Get list of infra requests from DB with " + queryAttributeName + " = " + queryValue);
+ logger.debug("Get list of infra requests from DB with {} = {}", queryAttributeName, queryValue);
try {
return arList;
}
} catch (final Exception exception) {
- msoLogger.error("Unable to execute query", exception);
+ logger.error("Unable to execute query", exception);
}
return Collections.emptyList();
}
/*
* (non-Javadoc)
*
- * @see
- * org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#getRequestFromInfraActive(java.
- * lang.String, java.lang.String)
+ * @see org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#getRequestFromInfraActive(java. lang.String,
+ * java.lang.String)
*/
@Override
public InfraActiveRequests getRequestFromInfraActive(final String requestId, final String requestType) {
final long startTime = System.currentTimeMillis();
- msoLogger.debug("Get infra request from DB with id " + requestId);
+ logger.debug("Get infra request from DB with id {}", requestId);
InfraActiveRequests ar = null;
- try {
- final Query query = entityManager.createQuery(
- "from InfraActiveRequests where (requestId = :requestId OR clientRequestId = :requestId) and requestType = :requestType");
- query.setParameter(REQUEST_ID, requestId);
- query.setParameter(REQUEST_TYPE, requestType);
- ar = this.getSingleResult(query);
- } finally {
- msoLogger.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc,
- "Successfully", "RequestDB", "getRequestFromInfraActive", null);
- }
+
+ final Query query = entityManager.createQuery(
+ "from InfraActiveRequests where (requestId = :requestId OR clientRequestId = :requestId) and requestType = :requestType");
+ query.setParameter(REQUEST_ID, requestId);
+ query.setParameter(REQUEST_TYPE, requestType);
+ ar = this.getSingleResult(query);
return ar;
}
/*
* (non-Javadoc)
*
- * @see
- * org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#checkDuplicateByVnfName(java.lang.
- * String, java.lang.String, java.lang.String)
+ * @see org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#checkDuplicateByVnfName(java.lang. String,
+ * java.lang.String, java.lang.String)
*/
@Override
public InfraActiveRequests checkDuplicateByVnfName(final String vnfName, final String action,
final String requestType) {
final long startTime = System.currentTimeMillis();
- msoLogger.debug("Get infra request from DB for VNF " + vnfName + " and action " + action + " and requestType "
- + requestType);
+ logger.debug("Get infra request from DB for VNF {} and action {} and requestType {}", vnfName, action,
+ requestType);
InfraActiveRequests ar = null;
- try {
- final Query query = entityManager.createQuery(
- "from InfraActiveRequests where vnfName = :vnfName and action = :action and (requestStatus = 'PENDING' or requestStatus = 'IN_PROGRESS' or requestStatus = 'TIMEOUT' or requestStatus = 'PENDING_MANUAL_TASK') and requestType = :requestType ORDER BY startTime DESC");
- query.setParameter("vnfName", vnfName);
- query.setParameter("action", action);
- query.setParameter(REQUEST_TYPE, requestType);
- @SuppressWarnings("unchecked")
- final List<InfraActiveRequests> results = query.getResultList();
- if (!results.isEmpty()) {
- ar = results.get(0);
- }
- } finally {
- msoLogger.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc,
- "Successfully", "RequestDB", "checkDuplicateByVnfName", null);
+
+ final Query query = entityManager.createQuery(
+ "from InfraActiveRequests where vnfName = :vnfName and action = :action and (requestStatus = 'PENDING' or requestStatus = 'IN_PROGRESS' or requestStatus = 'TIMEOUT' or requestStatus = 'PENDING_MANUAL_TASK') and requestType = :requestType ORDER BY startTime DESC");
+ query.setParameter("vnfName", vnfName);
+ query.setParameter("action", action);
+ query.setParameter(REQUEST_TYPE, requestType);
+ @SuppressWarnings("unchecked")
+ final List<InfraActiveRequests> results = query.getResultList();
+ if (!results.isEmpty()) {
+ ar = results.get(0);
}
return ar;
/*
* (non-Javadoc)
*
- * @see
- * org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#checkDuplicateByVnfId(java.lang.
- * String, java.lang.String, java.lang.String)
+ * @see org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#checkDuplicateByVnfId(java.lang. String,
+ * java.lang.String, java.lang.String)
*/
@Override
public InfraActiveRequests checkDuplicateByVnfId(final String vnfId, final String action,
final String requestType) {
final long startTime = System.currentTimeMillis();
- msoLogger.debug("Get list of infra requests from DB for VNF " + vnfId + " and action " + action);
+ logger.debug("Get list of infra requests from DB for VNF {} and action {}", vnfId, action);
InfraActiveRequests ar = null;
- try {
- final Query query = entityManager.createQuery(
- "from InfraActiveRequests where vnfId = :vnfId and action = :action and (requestStatus = 'PENDING' or requestStatus = 'IN_PROGRESS' or requestStatus = 'TIMEOUT' or requestStatus = 'PENDING_MANUAL_TASK') and requestType = :requestType ORDER BY startTime DESC");
- query.setParameter("vnfId", vnfId);
- query.setParameter("action", action);
- query.setParameter(REQUEST_TYPE, requestType);
- @SuppressWarnings("unchecked")
- final List<InfraActiveRequests> results = query.getResultList();
- if (!results.isEmpty()) {
- ar = results.get(0);
- }
- } finally {
- msoLogger.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc,
- "Successfully", "RequestDB", "checkDuplicateByVnfId", null);
+
+ final Query query = entityManager.createQuery(
+ "from InfraActiveRequests where vnfId = :vnfId and action = :action and (requestStatus = 'PENDING' or requestStatus = 'IN_PROGRESS' or requestStatus = 'TIMEOUT' or requestStatus = 'PENDING_MANUAL_TASK') and requestType = :requestType ORDER BY startTime DESC");
+ query.setParameter("vnfId", vnfId);
+ query.setParameter("action", action);
+ query.setParameter(REQUEST_TYPE, requestType);
+ @SuppressWarnings("unchecked")
+ final List<InfraActiveRequests> results = query.getResultList();
+ if (!results.isEmpty()) {
+ ar = results.get(0);
}
return ar;
/*
* (non-Javadoc)
*
- * @see
- * org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#checkVnfIdStatus(java.lang.String)
+ * @see org.onap.so.requestsdb.InfraActiveRequestsRepositoryCustom#checkVnfIdStatus(java.lang.String)
*/
@Override
public InfraActiveRequests checkVnfIdStatus(final String operationalEnvironmentId) {
final long startTime = System.currentTimeMillis();
- msoLogger.debug("Get Infra request from DB for OperationalEnvironmentId " + operationalEnvironmentId);
+ logger.debug("Get Infra request from DB for OperationalEnvironmentId {}", operationalEnvironmentId);
InfraActiveRequests ar = null;
- try {
- final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
- final CriteriaQuery<InfraActiveRequests> crit = cb.createQuery(InfraActiveRequests.class);
- final Root<InfraActiveRequests> candidateRoot = crit.from(InfraActiveRequests.class);
- final Predicate operationalEnvEq =
- cb.equal(candidateRoot.get("operationalEnvId"), operationalEnvironmentId);
- final Predicate requestStatusNotEq = cb.notEqual(candidateRoot.get(REQUEST_STATUS), "COMPLETE");
- final Predicate actionEq = cb.equal(candidateRoot.get("action"), "create");
- final Order startTimeOrder = cb.desc(candidateRoot.get("startTime"));
- crit.select(candidateRoot);
- crit.where(cb.and(operationalEnvEq, requestStatusNotEq, actionEq));
- crit.orderBy(startTimeOrder);
- final TypedQuery<InfraActiveRequests> query = entityManager.createQuery(crit);
- final List<InfraActiveRequests> results = query.getResultList();
- if (!results.isEmpty()) {
- ar = results.get(0);
- }
- } finally {
- msoLogger.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc,
- "Successfully", "RequestDB", "checkDuplicateByVnfName", null);
+
+ final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
+ final CriteriaQuery<InfraActiveRequests> crit = cb.createQuery(InfraActiveRequests.class);
+ final Root<InfraActiveRequests> candidateRoot = crit.from(InfraActiveRequests.class);
+ final Predicate operationalEnvEq = cb.equal(candidateRoot.get("operationalEnvId"), operationalEnvironmentId);
+ final Predicate requestStatusNotEq = cb.notEqual(candidateRoot.get(REQUEST_STATUS), "COMPLETE");
+ final Predicate actionEq = cb.equal(candidateRoot.get("action"), "create");
+ final Order startTimeOrder = cb.desc(candidateRoot.get("startTime"));
+ crit.select(candidateRoot);
+ crit.where(cb.and(operationalEnvEq, requestStatusNotEq, actionEq));
+ crit.orderBy(startTimeOrder);
+ final TypedQuery<InfraActiveRequests> query = entityManager.createQuery(crit);
+ final List<InfraActiveRequests> results = query.getResultList();
+ if (!results.isEmpty()) {
+ ar = results.get(0);
}
return ar;
}
try {
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
+
final CriteriaQuery<InfraActiveRequests> criteriaQuery =
criteriaBuilder.createQuery(InfraActiveRequests.class);
final Root<InfraActiveRequests> tableRoot = criteriaQuery.from(InfraActiveRequests.class);
final Timestamp minTime = new Timestamp(startTime);
final Timestamp maxTime = new Timestamp(endTime);
- predicates.add(criteriaBuilder.greaterThanOrEqualTo(tableRoot.get(START_TIME), minTime));
- predicates.add(criteriaBuilder.lessThanOrEqualTo(tableRoot.get(END_TIME), maxTime));
+ final Predicate basePredicate = criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
+ final Predicate additionalPredicate =
+ criteriaBuilder.and(criteriaBuilder.greaterThanOrEqualTo(tableRoot.get(START_TIME), minTime),
+ criteriaBuilder.or(tableRoot.get(END_TIME).isNull(),
+ criteriaBuilder.lessThanOrEqualTo(tableRoot.get(END_TIME), maxTime)));
- criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
+ criteriaQuery.where(criteriaBuilder.and(basePredicate, additionalPredicate));
if (maxResult != null) {
return entityManager.createQuery(criteriaQuery).setMaxResults(maxResult).getResultList();
}
return entityManager.createQuery(criteriaQuery).getResultList();
} catch (final Exception exception) {
- msoLogger.error("Unable to execute query using filters: " + filters, exception);
+ logger.error("Unable to execute query using filters: {}", filters, exception);
return Collections.emptyList();
}
}
- private List<Predicate> getPredicates(final Map<String, String[]> filters, final CriteriaBuilder criteriaBuilder,
+ protected List<Predicate> getPredicates(final Map<String, String[]> filters, final CriteriaBuilder criteriaBuilder,
final Root<InfraActiveRequests> tableRoot) {
final List<Predicate> predicates = new LinkedList<>();
for (final Entry<String, String[]> entry : filters.entrySet()) {