summaryrefslogtreecommitdiffstats
path: root/mso-api-handlers/mso-requests-db
diff options
context:
space:
mode:
authorGary Wu <gary.i.wu@huawei.com>2017-05-09 16:03:15 -0700
committerGary Wu <gary.i.wu@huawei.com>2017-05-15 17:12:57 -0700
commit2937320f2c53273190d6c3c2d53d3a39e13cae28 (patch)
tree2868a9484acff1d8bd7b41b598f115c1673b5c76 /mso-api-handlers/mso-requests-db
parentb1e5734ef566af5d49ba17d05ca0ab7b56d6666d (diff)
Refactor RequestsDatabase method
Refactor RequestsDatabase.checkInstanceNameDuplicate(). Change-Id: I274f72c874f435acbc734523f06968368e35f283 Signed-off-by: Gary Wu <gary.i.wu@huawei.com>
Diffstat (limited to 'mso-api-handlers/mso-requests-db')
-rw-r--r--mso-api-handlers/mso-requests-db/src/main/java/org/openecomp/mso/requestsdb/RequestsDatabase.java77
1 files changed, 35 insertions, 42 deletions
diff --git a/mso-api-handlers/mso-requests-db/src/main/java/org/openecomp/mso/requestsdb/RequestsDatabase.java b/mso-api-handlers/mso-requests-db/src/main/java/org/openecomp/mso/requestsdb/RequestsDatabase.java
index e32c456295..3824df08db 100644
--- a/mso-api-handlers/mso-requests-db/src/main/java/org/openecomp/mso/requestsdb/RequestsDatabase.java
+++ b/mso-api-handlers/mso-requests-db/src/main/java/org/openecomp/mso/requestsdb/RequestsDatabase.java
@@ -22,23 +22,20 @@ package org.openecomp.mso.requestsdb;
import java.sql.Timestamp;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-import org.apache.http.HttpStatus;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
-import org.hibernate.persister.entity.AbstractEntityPersister;
import org.openecomp.mso.db.HibernateUtils;
-import org.openecomp.mso.requestsdb.HibernateUtilsRequestsDb;
import org.openecomp.mso.logger.MsoLogger;
-import org.openecomp.mso.logger.MessageEnum;
public class RequestsDatabase {
@@ -67,6 +64,26 @@ public class RequestsDatabase {
protected static final String REQUEST_ID = "requestId";
protected static MockRequestsDatabase mockDB = null;
+ protected static enum Scope {
+ SERVICE("service", SERVICE_INSTANCE_NAME, SERVICE_INSTANCE_ID, "serviceInstanceId"),
+ VNF_INSTANCE("vnf", VNF_INSTANCE_NAME, VNF_INSTANCE_ID, "vnfInstanceId"),
+ VOLUME_GROUP("volumeGroup", VOLUME_GROUP_INSTANCE_NAME, VOLUME_GROUP_INSTANCE_ID, "volumeGroupInstanceId"),
+ VFMODULE("vfModule", VFMODULE_INSTANCE_NAME, VFMODULE_INSTANCE_ID, "vfModuleInstanceId"),
+ NETWORK("network", NETWORK_INSTANCE_NAME, NETWORK_INSTANCE_ID, "networkInstanceId");
+
+ public final String type;
+ public final String nameColumn;
+ public final String idColumn;
+ public final String idMapKey;
+
+ private Scope(String type, String nameColumn, String idColumn, String idMapKey) {
+ this.type = type;
+ this.nameColumn = nameColumn;
+ this.idColumn = idColumn;
+ this.idMapKey = idMapKey;
+ }
+ }
+
/**
* Avoids creating an instance of this utility class.
*/
@@ -217,48 +234,24 @@ public class RequestsDatabase {
return ar;
}
- public static InfraActiveRequests checkInstanceNameDuplicate (HashMap<String,String> instanceIdMap, String instanceName, String requestScope) {
+ public static InfraActiveRequests checkInstanceNameDuplicate (Map<String,String> instanceIdMap, String instanceName, String requestScope) {
List <Criterion> criteria = new LinkedList <> ();
- if(instanceName != null && !instanceName.equals("")) {
-
- if(requestScope.equals("service")){
- criteria.add (Restrictions.eq (SERVICE_INSTANCE_NAME, instanceName));
- } else if(requestScope.equals("vnf")){
- criteria.add (Restrictions.eq (VNF_INSTANCE_NAME, instanceName));
- } else if(requestScope.equals("volumeGroup")){
- criteria.add (Restrictions.eq (VOLUME_GROUP_INSTANCE_NAME, instanceName));
- } else if(requestScope.equals("vfModule")){
- criteria.add (Restrictions.eq (VFMODULE_INSTANCE_NAME, instanceName));
- } else if(requestScope.equals("network")){
- criteria.add (Restrictions.eq (NETWORK_INSTANCE_NAME, instanceName));
- }
-
- } else {
- if(instanceIdMap != null){
- if(requestScope.equals("service") && instanceIdMap.get("serviceInstanceId") != null){
- criteria.add (Restrictions.eq (SERVICE_INSTANCE_ID, instanceIdMap.get("serviceInstanceId")));
- }
-
- if(requestScope.equals("vnf") && instanceIdMap.get("vnfInstanceId") != null){
- criteria.add (Restrictions.eq (VNF_INSTANCE_ID, instanceIdMap.get("vnfInstanceId")));
- }
-
- if(requestScope.equals("vfModule") && instanceIdMap.get("vfModuleInstanceId") != null){
- criteria.add (Restrictions.eq (VFMODULE_INSTANCE_ID, instanceIdMap.get("vfModuleInstanceId")));
- }
-
- if(requestScope.equals("volumeGroup") && instanceIdMap.get("volumeGroupInstanceId") != null){
- criteria.add (Restrictions.eq (VOLUME_GROUP_INSTANCE_ID, instanceIdMap.get("volumeGroupInstanceId")));
- }
-
- if(requestScope.equals("network") && instanceIdMap.get("networkInstanceId") != null){
- criteria.add (Restrictions.eq (NETWORK_INSTANCE_ID, instanceIdMap.get("networkInstanceId")));
- }
- }
+ if (instanceName != null && !instanceName.equals("")) {
+
+ Arrays.stream(Scope.values()) //
+ .filter(scope -> scope.type.equals(requestScope)) //
+ .forEach(scope -> criteria.add(Restrictions.eq(scope.nameColumn, instanceName)));
+
+ } else if (instanceIdMap != null) {
+
+ Arrays.stream(Scope.values()) //
+ .filter(scope -> scope.type.equals(requestScope) && instanceIdMap.get(scope.idMapKey) != null) //
+ .forEach(scope -> criteria.add(Restrictions.eq(scope.idColumn, instanceIdMap.get(scope.idMapKey))));
+
}
-
+
criteria.add (Restrictions.in ("requestStatus", new String[] { "PENDING", "IN_PROGRESS", "TIMEOUT" }));
Order order = Order.desc (START_TIME);