Remove SDC query
[clamp.git] / src / main / java / org / onap / clamp / clds / dao / CldsDao.java
index d3e89fb..eef6353 100644 (file)
@@ -29,13 +29,9 @@ import com.att.eelf.configuration.EELFManager;
 import java.io.InputStream;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Optional;
-import java.util.stream.Collectors;
 
 import javax.sql.DataSource;
 
@@ -86,6 +82,7 @@ public class CldsDao {
     private SimpleJdbcCall procInsertDictionaryElement;
 
     private static final String DATE_FORMAT = "MM-dd-yyyy HH:mm:ss";
+
     /**
      * Log message when instantiating
      */
@@ -184,6 +181,7 @@ public class CldsDao {
             .addValue("v_model_prop_text", model.getPropText())
             .addValue("v_model_blueprint_text", model.getBlueprintText())
             .addValue("v_service_type_id", model.getTypeId()).addValue("v_deployment_id", model.getDeploymentId())
+            .addValue("v_deployment_status_url", model.getDeploymentStatusUrl())
             .addValue("v_control_name_prefix", model.getControlNamePrefix())
             .addValue(V_CONTROL_NAME_UUID, model.getControlNameUuid());
         Map<String, Object> out = logSqlExecution(procSetModel, in);
@@ -338,45 +336,6 @@ public class CldsDao {
         return template;
     }
 
-    public void clearServiceCache() {
-        String clearCldsServiceCacheSql = "TRUNCATE clds_service_cache";
-        jdbcTemplateObject.execute(clearCldsServiceCacheSql);
-    }
-
-    public CldsServiceData getCldsServiceCache(String invariantUUID) {
-        CldsServiceData cldsServiceData = null;
-        try {
-            String getCldsServiceSQL = "SELECT * , TIMESTAMPDIFF(SECOND, timestamp, CURRENT_TIMESTAMP()) FROM clds_service_cache where invariant_service_id  = ? ";
-            cldsServiceData = jdbcTemplateObject.queryForObject(getCldsServiceSQL, new Object[] { invariantUUID },
-                new CldsServiceDataMapper());
-            if (cldsServiceData != null) {
-                logger.info("CldsServiceData found in cache for Service Invariant ID:"
-                    + cldsServiceData.getServiceInvariantUUID());
-                return cldsServiceData;
-            } else {
-                logger.warn("CldsServiceData not found in cache for Service Invariant ID:" + invariantUUID);
-                return null;
-            }
-        } catch (EmptyResultDataAccessException e) {
-            logger.info("CldsServiceData not found in cache for Service Invariant ID: " + invariantUUID);
-            logger.debug("CldsServiceData not found in cache for Service Invariant ID: " + invariantUUID, e);
-            return null;
-        }
-    }
-
-    public void setCldsServiceCache(CldsDbServiceCache cldsDBServiceCache) {
-        if (cldsDBServiceCache != null && cldsDBServiceCache.getInvariantId() != null
-            && cldsDBServiceCache.getServiceId() != null) {
-            String invariantUuid = cldsDBServiceCache.getInvariantId();
-            String serviceUuid = cldsDBServiceCache.getServiceId();
-            InputStream is = cldsDBServiceCache.getCldsDataInstream();
-            String insertCldsServiceCacheSql = "INSERT INTO clds_service_cache"
-                + "(invariant_service_id,service_id,timestamp,object_data) VALUES"
-                + "(?,?,CURRENT_TIMESTAMP,?) ON DUPLICATE KEY UPDATE invariant_service_id = VALUES(invariant_service_id) , timestamp = CURRENT_TIMESTAMP , object_data = VALUES(object_data) ";
-            jdbcTemplateObject.update(insertCldsServiceCacheSql, invariantUuid, serviceUuid, is);
-        }
-    }
-
     private static Map<String, Object> logSqlExecution(SimpleJdbcCall call, SqlParameterSource source) {
         try {
             return call.execute(source);
@@ -475,6 +434,7 @@ public class CldsDao {
         model.getEvent().setUserid((String) out.get("v_event_user_id"));
         model.setTypeId((String) out.get("v_service_type_id"));
         model.setDeploymentId((String) out.get("v_deployment_id"));
+        model.setDeploymentStatusUrl((String) out.get("v_deployment_status_url"));
     }
 
     /**
@@ -512,31 +472,34 @@ public class CldsDao {
     private List<CldsToscaModel> getToscaModel(String toscaModelName, String policyType) {
         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
         List<CldsToscaModel> cldsToscaModels = new ArrayList<>();
-        MapSqlParameterSource params = new MapSqlParameterSource();
 
-        String toscaModelSql = "SELECT tm.tosca_model_name, tm.tosca_model_id, tm.policy_type, tmr.tosca_model_revision_id, tmr.version, tmr.user_id, tmr.createdTimestamp, tmr.lastUpdatedTimestamp, tmr.tosca_model_yaml FROM tosca_model tm, tosca_model_revision tmr WHERE tm.tosca_model_id = tmr.tosca_model_id ";
-        if (toscaModelName != null) {
-            toscaModelSql += " AND tm.tosca_model_name = :toscaModelName";
-            params.addValue("toscaModelName", toscaModelName);
-        }
-        if (policyType != null) {
-            toscaModelSql += " AND tm.policy_type = :policyType";
-            params.addValue("policyType", policyType);
+        String toscaModelSql = "SELECT tm.tosca_model_name, tm.tosca_model_id, tm.policy_type, tmr.tosca_model_revision_id, tmr.tosca_model_json, tmr.version, tmr.user_id, tmr.createdTimestamp, tmr.lastUpdatedTimestamp "
+            + ((toscaModelName != null) ? (", tmr.tosca_model_yaml ") : " ")
+            + "FROM tosca_model tm, tosca_model_revision tmr WHERE tm.tosca_model_id = tmr.tosca_model_id "
+            + ((toscaModelName != null) ? (" AND tm.tosca_model_name = '" + toscaModelName + "'") : " ")
+            + ((policyType != null) ? (" AND tm.policy_type = '" + policyType + "'") : " ")
+            + "AND tmr.version = (select max(version) from tosca_model_revision st where tmr.tosca_model_id=st.tosca_model_id)";
+
+        List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(toscaModelSql);
+
+        if (rows != null) {
+            rows.forEach(row -> {
+                CldsToscaModel cldsToscaModel = new CldsToscaModel();
+                cldsToscaModel.setId((String) row.get("tosca_model_id"));
+                cldsToscaModel.setPolicyType((String) row.get("policy_type"));
+                cldsToscaModel.setToscaModelName((String) row.get("tosca_model_name"));
+                cldsToscaModel.setUserId((String) row.get("user_id"));
+                cldsToscaModel.setRevisionId((String) row.get("tosca_model_revision_id"));
+                cldsToscaModel.setToscaModelJson((String) row.get("tosca_model_json"));
+                cldsToscaModel.setVersion(((Double) row.get("version")));
+                cldsToscaModel.setCreatedDate(sdf.format(row.get("createdTimestamp")));
+                cldsToscaModel.setLastUpdatedDate(sdf.format(row.get("lastUpdatedTimestamp")));
+                if (toscaModelName != null) {
+                    cldsToscaModel.setToscaModelYaml((String) row.get("tosca_model_yaml"));
+                }
+                cldsToscaModels.add(cldsToscaModel);
+            });
         }
-        toscaModelSql += " AND tmr.version = (select max(version) from tosca_model_revision st where tmr.tosca_model_id=st.tosca_model_id)";
-
-        Optional.ofNullable(jdbcTemplateObject.queryForList(toscaModelSql, params)).orElse(Collections.emptyList()).forEach(row -> {
-            CldsToscaModel cldsToscaModel = new CldsToscaModel();
-            cldsToscaModel.setId((String) row.get("tosca_model_id"));
-            cldsToscaModel.setPolicyType((String) row.get("policy_type"));
-            cldsToscaModel.setToscaModelName((String) row.get("tosca_model_name"));
-            cldsToscaModel.setUserId((String) row.get("user_id"));
-            cldsToscaModel.setRevisionId((String) row.get("tosca_model_revision_id"));
-            cldsToscaModel.setVersion(((Double) row.get("version")));
-            cldsToscaModel.setCreatedDate(sdf.format(row.get("createdTimestamp")));
-            cldsToscaModel.setToscaModelYaml((String) row.get("tosca_model_yaml"));
-            cldsToscaModels.add(cldsToscaModel);
-        });
         return cldsToscaModels;
     }
 
@@ -600,11 +563,10 @@ public class CldsDao {
      * @param userId
      */
     public void updateDictionary(String dictionaryId, CldsDictionary cldsDictionary, String userId) {
-        String dictionarySql = "UPDATE dictionary SET dictionary_name = :dictionary_name, modified_by = :modified_by WHERE dictionary_id = :dictionary_id";
-        SqlParameterSource namedParameters = new MapSqlParameterSource()
-            .addValue("dictionary_name", cldsDictionary.getDictionaryName()).addValue("modified_by", userId)
-            .addValue("dictionary_id", dictionaryId);
-        jdbcTemplateObject.update(dictionarySql, namedParameters);
+
+        String dictionarySql = "UPDATE dictionary " + "SET dictionary_name = '" + cldsDictionary.getDictionaryName()
+            + "', modified_by = '" + userId + "'" + "WHERE dictionary_id = '" + dictionaryId + "'";
+        jdbcTemplateObject.update(dictionarySql);
         cldsDictionary.setUpdatedBy(userId);
     }
 
@@ -618,23 +580,26 @@ public class CldsDao {
     public List<CldsDictionary> getDictionary(String dictionaryId, String dictionaryName) {
         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
         List<CldsDictionary> dictionaries = new ArrayList<>();
-        String dictionarySql = "SELECT dictionary_id, dictionary_name, created_by, modified_by, timestamp FROM dictionary WHERE ";
-        MapSqlParameterSource namedParameters = new MapSqlParameterSource();
-        Optional.ofNullable(dictionaryName).ifPresent(dn -> namedParameters.addValue("dictionary_name", dn));
-        Optional.ofNullable(dictionaryId).ifPresent(dn -> namedParameters.addValue("dictionary_id", dn));
-        dictionarySql += Optional.ofNullable(namedParameters.getParameterNames()).filter(a -> a.length > 0)
-            .map(Arrays::stream).map(s -> s.map(param -> param + " = :" + param).collect(Collectors.joining(" AND ")))
-            .orElse("1");
-
-        Optional.ofNullable(jdbcTemplateObject.queryForList(dictionarySql, namedParameters)).orElse(Collections.emptyList()).forEach(row -> {
-            CldsDictionary cldsDictionary = new CldsDictionary();
-            cldsDictionary.setDictionaryId((String) row.get("dictionary_id"));
-            cldsDictionary.setDictionaryName((String) row.get("dictionary_name"));
-            cldsDictionary.setCreatedBy((String) row.get("created_by"));
-            cldsDictionary.setUpdatedBy((String) row.get("modified_by"));
-            cldsDictionary.setLastUpdatedDate(sdf.format(row.get("timestamp")));
-            dictionaries.add(cldsDictionary);
-        });
+        String dictionarySql = "SELECT dictionary_id, dictionary_name, created_by, modified_by, timestamp FROM dictionary"
+            + ((dictionaryId != null || dictionaryName != null)
+                ? (" WHERE " + ((dictionaryName != null) ? ("dictionary_name = '" + dictionaryName + "'") : "")
+                    + ((dictionaryId != null && dictionaryName != null) ? (" AND ") : "")
+                    + ((dictionaryId != null) ? ("dictionary_id = '" + dictionaryId + "'") : ""))
+                : "");
+
+        List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(dictionarySql);
+
+        if (rows != null) {
+            rows.forEach(row -> {
+                CldsDictionary cldsDictionary = new CldsDictionary();
+                cldsDictionary.setDictionaryId((String) row.get("dictionary_id"));
+                cldsDictionary.setDictionaryName((String) row.get("dictionary_name"));
+                cldsDictionary.setCreatedBy((String) row.get("created_by"));
+                cldsDictionary.setUpdatedBy((String) row.get("modified_by"));
+                cldsDictionary.setLastUpdatedDate(sdf.format(row.get("timestamp")));
+                dictionaries.add(cldsDictionary);
+            });
+        }
         return dictionaries;
     }
 
@@ -666,15 +631,13 @@ public class CldsDao {
     public void updateDictionaryElements(String dictionaryElementId, CldsDictionaryItem cldsDictionaryItem,
         String userId) {
 
-        String dictionarySql = "UPDATE dictionary_elements SET dict_element_name = :dict_element_name, dict_element_short_name = :dict_element_short_name, dict_element_description = :dict_element_description,dict_element_type=:dict_element_type, modified_by = :modified_by WHERE dict_element_id = :dict_element_id";
-        SqlParameterSource namedParameters = new MapSqlParameterSource()
-            .addValue("dict_element_name", cldsDictionaryItem.getDictElementName())
-            .addValue("dict_element_short_name", cldsDictionaryItem.getDictElementShortName())
-            .addValue("dict_element_description", cldsDictionaryItem.getDictElementDesc())
-            .addValue("dict_element_type", cldsDictionaryItem.getDictElementType())
-            .addValue("modified_by", userId)
-            .addValue("dict_element_id", dictionaryElementId);
-        jdbcTemplateObject.update(dictionarySql, namedParameters);
+        String dictionarySql = "UPDATE dictionary_elements SET dict_element_name = '"
+            + cldsDictionaryItem.getDictElementName() + "', dict_element_short_name = '"
+            + cldsDictionaryItem.getDictElementShortName() + "', dict_element_description= '"
+            + cldsDictionaryItem.getDictElementDesc() + "', dict_element_type = '"
+            + cldsDictionaryItem.getDictElementType() + "', modified_by = '" + userId + "' "
+            + "WHERE dict_element_id = '" + dictionaryElementId + "'";
+        jdbcTemplateObject.update(dictionarySql);
         cldsDictionaryItem.setUpdatedBy(userId);
     }
 
@@ -691,35 +654,53 @@ public class CldsDao {
         String dictElementShortName) {
         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
         List<CldsDictionaryItem> dictionaryItems = new ArrayList<>();
-        MapSqlParameterSource namedParameters = new MapSqlParameterSource();
         String dictionarySql = "SELECT de.dict_element_id, de.dictionary_id, de.dict_element_name, de.dict_element_short_name, de.dict_element_description, de.dict_element_type, de.created_by, de.modified_by, de.timestamp  "
-            + "FROM dictionary_elements de, dictionary d WHERE de.dictionary_id = d.dictionary_id ";
-        if (dictionaryId != null) {
-            dictionarySql+=" AND d.dictionary_id = :dictionaryId";
-            namedParameters.addValue("dictionaryId", dictionaryId);
-        }
-        if (dictElementShortName!=null) {
-            dictionarySql+=" AND de.dict_element_short_name = :dictElementShortName";
-            namedParameters.addValue("dictElementShortName", dictElementShortName);
-        }
-        if (dictionaryName!=null) {
-            dictionarySql+=" AND dictionary_name = :dictionaryName";
-            namedParameters.addValue("dictionaryName", dictionaryName);
+            + "FROM dictionary_elements de, dictionary d WHERE de.dictionary_id = d.dictionary_id "
+            + ((dictionaryId != null) ? (" AND d.dictionary_id = '" + dictionaryId + "'") : "")
+            + ((dictElementShortName != null) ? (" AND de.dict_element_short_name = '" + dictElementShortName + "'")
+                : "")
+            + ((dictionaryName != null) ? (" AND dictionary_name = '" + dictionaryName + "'") : "");
+
+        List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(dictionarySql);
+
+        if (rows != null) {
+            rows.forEach(row -> {
+                CldsDictionaryItem dictionaryItem = new CldsDictionaryItem();
+                dictionaryItem.setDictElementId((String) row.get("dict_element_id"));
+                dictionaryItem.setDictionaryId((String) row.get("dictionary_id"));
+                dictionaryItem.setDictElementName((String) row.get("dict_element_name"));
+                dictionaryItem.setDictElementShortName((String) row.get("dict_element_short_name"));
+                dictionaryItem.setDictElementDesc((String) row.get("dict_element_description"));
+                dictionaryItem.setDictElementType((String) row.get("dict_element_type"));
+                dictionaryItem.setCreatedBy((String) row.get("created_by"));
+                dictionaryItem.setUpdatedBy((String) row.get("modified_by"));
+                dictionaryItem.setLastUpdatedDate(sdf.format(row.get("timestamp")));
+                dictionaryItems.add(dictionaryItem);
+            });
         }
+        return dictionaryItems;
+    }
 
-        Optional.ofNullable(jdbcTemplateObject.queryForList(dictionarySql,namedParameters)).orElse(Collections.emptyList()).forEach(row -> {
-            CldsDictionaryItem dictionaryItem = new CldsDictionaryItem();
-            dictionaryItem.setDictElementId((String) row.get("dict_element_id"));
-            dictionaryItem.setDictionaryId((String) row.get("dictionary_id"));
-            dictionaryItem.setDictElementName((String) row.get("dict_element_name"));
-            dictionaryItem.setDictElementShortName((String) row.get("dict_element_short_name"));
-            dictionaryItem.setDictElementDesc((String) row.get("dict_element_description"));
-            dictionaryItem.setDictElementType((String) row.get("dict_element_type"));
-            dictionaryItem.setCreatedBy((String) row.get("created_by"));
-            dictionaryItem.setUpdatedBy((String) row.get("modified_by"));
-            dictionaryItem.setLastUpdatedDate(sdf.format(row.get("timestamp")));
-            dictionaryItems.add(dictionaryItem);
-        });
+    /**
+     * Method to get Map of all dictionary elements with key as dictionary short
+     * name and value as the full name
+     *
+     * @param dictionaryElementType
+     * @return Map of dictionary elements as key value pair
+     */
+    public Map<String, String> getDictionaryElementsByType(String dictionaryElementType) {
+        Map<String, String> dictionaryItems = new HashMap<>();
+        String dictionarySql = "SELECT dict_element_name, dict_element_short_name " + "FROM dictionary_elements "
+            + "WHERE dict_element_type = '" + dictionaryElementType + "'";
+
+        List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(dictionarySql);
+
+        if (rows != null) {
+            rows.forEach(row -> {
+                dictionaryItems.put(((String) row.get("dict_element_short_name")),
+                    ((String) row.get("dict_element_name")));
+            });
+        }
         return dictionaryItems;
     }
 }