2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.dao;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.io.InputStream;
30 import java.text.SimpleDateFormat;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
36 import javax.sql.DataSource;
38 import org.onap.clamp.clds.model.CldsDbServiceCache;
39 import org.onap.clamp.clds.model.CldsEvent;
40 import org.onap.clamp.clds.model.CldsModel;
41 import org.onap.clamp.clds.model.CldsModelInstance;
42 import org.onap.clamp.clds.model.CldsModelProp;
43 import org.onap.clamp.clds.model.CldsMonitoringDetails;
44 import org.onap.clamp.clds.model.CldsServiceData;
45 import org.onap.clamp.clds.model.CldsTemplate;
46 import org.onap.clamp.clds.model.ValueItem;
47 import org.springframework.dao.EmptyResultDataAccessException;
48 import org.springframework.jdbc.core.JdbcTemplate;
49 import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
50 import org.springframework.jdbc.core.namedparam.SqlParameterSource;
51 import org.springframework.jdbc.core.simple.SimpleJdbcCall;
52 import org.springframework.stereotype.Repository;
55 * Data Access for CLDS Model tables.
57 @Repository("cldsDao")
58 public class CldsDao {
60 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDao.class);
61 private JdbcTemplate jdbcTemplateObject;
62 private SimpleJdbcCall procGetModel;
63 private SimpleJdbcCall procGetModelTemplate;
64 private SimpleJdbcCall procSetModel;
65 private SimpleJdbcCall procInsEvent;
66 private SimpleJdbcCall procUpdEvent;
67 private SimpleJdbcCall procSetTemplate;
68 private SimpleJdbcCall procGetTemplate;
69 private SimpleJdbcCall procDelAllModelInstances;
70 private SimpleJdbcCall procInsModelInstance;
71 private SimpleJdbcCall procDeleteModel;
72 private static final String HEALTHCHECK = "Select 1";
73 private static final String V_CONTROL_NAME_PREFIX = "v_control_name_prefix";
74 private static final String V_CONTROL_NAME_UUID = "v_control_name_uuid";
77 * Log message when instantiating
80 logger.info("CldsDao instantiating...");
84 * When dataSource is provided, instantiate spring jdbc objects.
86 public void setDataSource(DataSource dataSource) {
87 this.jdbcTemplateObject = new JdbcTemplate(dataSource);
88 this.procGetModel = new SimpleJdbcCall(dataSource).withProcedureName("get_model");
89 this.procGetModelTemplate = new SimpleJdbcCall(dataSource).withProcedureName("get_model_template");
90 this.procSetModel = new SimpleJdbcCall(dataSource).withProcedureName("set_model");
91 this.procInsEvent = new SimpleJdbcCall(dataSource).withProcedureName("ins_event");
92 this.procUpdEvent = new SimpleJdbcCall(dataSource).withProcedureName("upd_event");
93 this.procGetTemplate = new SimpleJdbcCall(dataSource).withProcedureName("get_template");
94 this.procSetTemplate = new SimpleJdbcCall(dataSource).withProcedureName("set_template");
95 this.procInsModelInstance = new SimpleJdbcCall(dataSource).withProcedureName("ins_model_instance");
96 this.procDelAllModelInstances = new SimpleJdbcCall(dataSource).withProcedureName("del_all_model_instances");
97 this.procDeleteModel = new SimpleJdbcCall(dataSource).withProcedureName("del_model");
101 * Get a model from the database given the model name.
103 public CldsModel getModel(String modelName) {
104 return getModel(modelName, null);
108 * Get a model from the database given the controlNameUuid.
110 public CldsModel getModelByUuid(String controlNameUuid) {
111 return getModel(null, controlNameUuid);
114 // Get a model from the database given the model name or a controlNameUuid.
115 private CldsModel getModel(String modelName, String controlNameUuid) {
116 CldsModel model = new CldsModel();
117 model.setName(modelName);
118 SqlParameterSource in = new MapSqlParameterSource().addValue("v_model_name", modelName)
119 .addValue(V_CONTROL_NAME_UUID, controlNameUuid);
120 Map<String, Object> out = logSqlExecution(procGetModel, in);
121 populateModelProperties(model, out);
126 * Get a model and template information from the database given the model
132 public CldsModel getModelTemplate(String modelName) {
133 CldsModel model = new CldsModel();
134 model.setName(modelName);
135 SqlParameterSource in = new MapSqlParameterSource().addValue("v_model_name", modelName);
136 Map<String, Object> out = logSqlExecution(procGetModelTemplate, in);
137 // todo : rationalize
138 populateModelProperties(model, out);
139 Map<String, Object> modelResults = logSqlExecution(procGetModel, in);
140 Object modelResultObject = modelResults.get("#result-set-1");
141 if (modelResultObject != null && modelResultObject instanceof ArrayList) {
142 List<Object> modelInstanceRs = (List<Object>) modelResultObject;
143 for (Object currModelInstance : modelInstanceRs) {
144 if (currModelInstance != null && currModelInstance instanceof HashMap) {
145 HashMap<String, String> modelInstanceMap = (HashMap<String, String>) currModelInstance;
146 CldsModelInstance modelInstance = new CldsModelInstance();
147 modelInstance.setModelInstanceId(modelInstanceMap.get("model_instance_id"));
148 modelInstance.setVmName(modelInstanceMap.get("vm_name"));
149 modelInstance.setLocation(modelInstanceMap.get("location"));
150 model.getCldsModelInstanceList().add(modelInstance);
151 logger.info("value of currModel: {}", currModelInstance);
159 * Update model in the database using parameter values and return updated
166 public CldsModel setModel(CldsModel model, String userid) {
167 SqlParameterSource in = new MapSqlParameterSource().addValue("v_model_name", model.getName())
168 .addValue("v_template_id", model.getTemplateId()).addValue("v_user_id", userid)
169 .addValue("v_model_prop_text", model.getPropText())
170 .addValue("v_model_blueprint_text", model.getBlueprintText())
171 .addValue("v_service_type_id", model.getTypeId()).addValue("v_deployment_id", model.getDeploymentId())
172 .addValue("v_control_name_prefix", model.getControlNamePrefix())
173 .addValue(V_CONTROL_NAME_UUID, model.getControlNameUuid());
174 Map<String, Object> out = logSqlExecution(procSetModel, in);
175 model.setControlNamePrefix((String) out.get(V_CONTROL_NAME_PREFIX));
176 model.setControlNameUuid((String) out.get(V_CONTROL_NAME_UUID));
177 model.setId((String) (out.get("v_model_id")));
178 model.getEvent().setId((String) (out.get("v_event_id")));
179 model.getEvent().setActionCd((String) out.get("v_action_cd"));
180 model.getEvent().setActionStateCd((String) out.get("v_action_state_cd"));
181 model.getEvent().setProcessInstanceId((String) out.get("v_event_process_instance_id"));
182 model.getEvent().setUserid((String) out.get("v_event_user_id"));
187 * Inserts new modelInstance in the database using parameter values and
188 * return updated model object.
191 * @param modelInstancesList
194 public void insModelInstance(CldsModel model, List<CldsModelInstance> modelInstancesList) {
195 // Delete all existing model instances for given controlNameUUID
196 logger.debug("deleting instances for: {}", model.getControlNameUuid());
197 delAllModelInstances(model.getControlNameUuid());
198 if (modelInstancesList == null) {
199 logger.debug("modelInstancesList == null");
201 for (CldsModelInstance currModelInstance : modelInstancesList) {
202 logger.debug("v_control_name_uuid={}", model.getControlNameUuid());
203 logger.debug("v_vm_name={}", currModelInstance.getVmName());
204 logger.debug("v_location={}", currModelInstance.getLocation());
205 SqlParameterSource in = new MapSqlParameterSource()
206 .addValue(V_CONTROL_NAME_UUID, model.getControlNameUuid())
207 .addValue("v_vm_name", currModelInstance.getVmName())
208 .addValue("v_location", currModelInstance.getLocation());
209 Map<String, Object> out = logSqlExecution(procInsModelInstance, in);
210 model.setId((String) (out.get("v_model_id")));
211 CldsModelInstance modelInstance = new CldsModelInstance();
212 modelInstance.setLocation(currModelInstance.getLocation());
213 modelInstance.setVmName(currModelInstance.getVmName());
214 modelInstance.setModelInstanceId((String) (out.get("v_model_instance_id")));
215 model.getCldsModelInstanceList().add(modelInstance);
221 * Insert an event in the database - require either modelName or
222 * controlNamePrefix/controlNameUuid.
225 * @param controlNamePrefix
226 * @param controlNameUuid
230 public CldsEvent insEvent(String modelName, String controlNamePrefix, String controlNameUuid, CldsEvent cldsEvent) {
231 CldsEvent event = new CldsEvent();
232 SqlParameterSource in = new MapSqlParameterSource().addValue("v_model_name", modelName)
233 .addValue(V_CONTROL_NAME_PREFIX, controlNamePrefix).addValue(V_CONTROL_NAME_UUID, controlNameUuid)
234 .addValue("v_user_id", cldsEvent.getUserid()).addValue("v_action_cd", cldsEvent.getActionCd())
235 .addValue("v_action_state_cd", cldsEvent.getActionStateCd())
236 .addValue("v_process_instance_id", cldsEvent.getProcessInstanceId());
237 Map<String, Object> out = logSqlExecution(procInsEvent, in);
238 event.setId((String) (out.get("v_event_id")));
242 private String delAllModelInstances(String controlNameUUid) {
243 SqlParameterSource in = new MapSqlParameterSource().addValue(V_CONTROL_NAME_UUID, controlNameUUid);
244 Map<String, Object> out = logSqlExecution(procDelAllModelInstances, in);
245 return (String) (out.get("v_model_id"));
249 * Update event with process instance id.
252 * @param processInstanceId
254 public void updEvent(String eventId, String processInstanceId) {
255 SqlParameterSource in = new MapSqlParameterSource().addValue("v_event_id", eventId)
256 .addValue("v_process_instance_id", processInstanceId);
257 logSqlExecution(procUpdEvent, in);
261 * Return list of model names
263 * @return model names
265 public List<ValueItem> getBpmnNames() {
266 String sql = "SELECT model_name FROM model ORDER BY 1;";
267 return jdbcTemplateObject.query(sql, new ValueItemMapper());
271 * Update template in the database using parameter values and return updated
277 public void setTemplate(CldsTemplate template, String userid) {
278 SqlParameterSource in = new MapSqlParameterSource().addValue("v_template_name", template.getName())
279 .addValue("v_user_id", userid).addValue("v_template_bpmn_text", template.getBpmnText())
280 .addValue("v_template_image_text", template.getImageText())
281 .addValue("v_template_doc_text", template.getPropText());
282 Map<String, Object> out = logSqlExecution(procSetTemplate, in);
283 template.setId((String) (out.get("v_template_id")));
284 template.setBpmnUserid((String) (out.get("v_template_bpmn_user_id")));
285 template.setBpmnId((String) (out.get("v_template_bpmn_id")));
286 template.setImageId((String) (out.get("v_template_image_id")));
287 template.setImageUserid((String) out.get("v_template_image_user_id"));
288 template.setPropId((String) (out.get("v_template_doc_id")));
289 template.setPropUserid((String) out.get("v_template_doc_user_id"));
293 * Return list of template names
295 * @return template names
297 public List<ValueItem> getTemplateNames() {
298 String sql = "SELECT template_name FROM template ORDER BY 1;";
299 return jdbcTemplateObject.query(sql, new ValueItemMapper());
303 * Get a template from the database given the model name.
305 * @param templateName
308 public CldsTemplate getTemplate(String templateName) {
309 CldsTemplate template = new CldsTemplate();
310 template.setName(templateName);
311 SqlParameterSource in = new MapSqlParameterSource().addValue("v_template_name", templateName);
312 Map<String, Object> out = logSqlExecution(procGetTemplate, in);
313 template.setId((String) (out.get("v_template_id")));
314 template.setBpmnUserid((String) (out.get("v_template_bpmn_user_id")));
315 template.setBpmnId((String) (out.get("v_template_bpmn_id")));
316 template.setBpmnText((String) (out.get("v_template_bpmn_text")));
317 template.setImageId((String) (out.get("v_template_image_id")));
318 template.setImageUserid((String) out.get("v_template_image_user_id"));
319 template.setImageText((String) out.get("v_template_image_text"));
320 template.setPropId((String) (out.get("v_template_doc_id")));
321 template.setPropUserid((String) out.get("v_template_doc_user_id"));
322 template.setPropText((String) out.get("v_template_doc_text"));
326 public void clearServiceCache() {
327 String clearCldsServiceCacheSql = "TRUNCATE clds_service_cache";
328 jdbcTemplateObject.execute(clearCldsServiceCacheSql);
331 public CldsServiceData getCldsServiceCache(String invariantUUID) {
332 CldsServiceData cldsServiceData = null;
334 String getCldsServiceSQL = "SELECT * , TIMESTAMPDIFF(SECOND, timestamp, CURRENT_TIMESTAMP()) FROM clds_service_cache where invariant_service_id = ? ";
335 cldsServiceData = jdbcTemplateObject.queryForObject(getCldsServiceSQL, new Object[] { invariantUUID },
336 new CldsServiceDataMapper());
337 if (cldsServiceData != null) {
338 logger.info("CldsServiceData found in cache for Service Invariant ID:"
339 + cldsServiceData.getServiceInvariantUUID());
340 return cldsServiceData;
342 logger.warn("CldsServiceData not found in cache for Service Invariant ID:" + invariantUUID);
345 } catch (EmptyResultDataAccessException e) {
346 logger.info("CldsServiceData not found in cache for Service Invariant ID: " + invariantUUID);
347 logger.debug("CldsServiceData not found in cache for Service Invariant ID: " + invariantUUID, e);
352 public void setCldsServiceCache(CldsDbServiceCache cldsDBServiceCache) {
353 if (cldsDBServiceCache != null && cldsDBServiceCache.getInvariantId() != null
354 && cldsDBServiceCache.getServiceId() != null) {
355 String invariantUuid = cldsDBServiceCache.getInvariantId();
356 String serviceUuid = cldsDBServiceCache.getServiceId();
357 InputStream is = cldsDBServiceCache.getCldsDataInstream();
358 String insertCldsServiceCacheSql = "INSERT INTO clds_service_cache"
359 + "(invariant_service_id,service_id,timestamp,object_data) VALUES"
360 + "(?,?,CURRENT_TIMESTAMP,?) ON DUPLICATE KEY UPDATE invariant_service_id = VALUES(invariant_service_id) , timestamp = CURRENT_TIMESTAMP , object_data = VALUES(object_data) ";
361 jdbcTemplateObject.update(insertCldsServiceCacheSql, invariantUuid, serviceUuid, is);
365 private static Map<String, Object> logSqlExecution(SimpleJdbcCall call, SqlParameterSource source) {
367 return call.execute(source);
368 } catch (Exception e) {
369 logger.error("Exception occured in " + source.getClass().getCanonicalName() + ": " + e);
374 public void doHealthCheck() {
375 jdbcTemplateObject.execute(HEALTHCHECK);
379 * Method to get deployed/active models with model properties.
381 * @return list of CldsModelProp
383 public List<CldsModelProp> getDeployedModelProperties() {
384 List<CldsModelProp> cldsModelPropList = new ArrayList<>();
385 String modelsSql = "select m.model_id, m.model_name, mp.model_prop_id, mp.model_prop_text FROM model m, model_properties mp, event e "
386 + "WHERE m.model_prop_id = mp.model_prop_id and m.event_id = e.event_id and e.action_cd = 'DEPLOY'";
387 List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(modelsSql);
388 CldsModelProp cldsModelProp = null;
389 for (Map<String, Object> row : rows) {
390 cldsModelProp = new CldsModelProp();
391 cldsModelProp.setId((String) row.get("model_id"));
392 cldsModelProp.setName((String) row.get("model_name"));
393 cldsModelProp.setPropId((String) row.get("model_prop_id"));
394 cldsModelProp.setPropText((String) row.get("model_prop_text"));
395 cldsModelPropList.add(cldsModelProp);
397 return cldsModelPropList;
401 * Method to get deployed/active models with model properties.
403 * @return list of CLDS-Monitoring-Details: CLOSELOOP_NAME | Close loop name
404 * used in the CLDS application (prefix: ClosedLoop- + unique
405 * ClosedLoop ID) MODEL_NAME | Model Name in CLDS application
406 * SERVICE_TYPE_ID | TypeId returned from the DCAE application when
407 * the ClosedLoop is submitted (DCAEServiceTypeRequest generated in
408 * DCAE application). DEPLOYMENT_ID | Id generated when the
409 * ClosedLoop is deployed in DCAE. TEMPLATE_NAME | Template used to
410 * generate the ClosedLoop model. ACTION_CD | Current state of the
411 * ClosedLoop in CLDS application.
413 public List<CldsMonitoringDetails> getCLDSMonitoringDetails() {
414 SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
415 List<CldsMonitoringDetails> cldsMonitoringDetailsList = new ArrayList<CldsMonitoringDetails>();
416 String modelsSql = "SELECT CONCAT(M.CONTROL_NAME_PREFIX, M.CONTROL_NAME_UUID) AS CLOSELOOP_NAME , M.MODEL_NAME, M.SERVICE_TYPE_ID, M.DEPLOYMENT_ID, T.TEMPLATE_NAME, E.ACTION_CD, E.USER_ID, E.TIMESTAMP "
417 + "FROM MODEL M, TEMPLATE T, EVENT E "
418 + "WHERE M.TEMPLATE_ID = T.TEMPLATE_ID AND M.EVENT_ID = E.EVENT_ID " + "ORDER BY ACTION_CD";
419 List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(modelsSql);
420 CldsMonitoringDetails cldsMonitoringDetails = null;
421 for (Map<String, Object> row : rows) {
422 cldsMonitoringDetails = new CldsMonitoringDetails();
423 cldsMonitoringDetails.setCloseloopName((String) row.get("CLOSELOOP_NAME"));
424 cldsMonitoringDetails.setModelName((String) row.get("MODEL_NAME"));
425 cldsMonitoringDetails.setServiceTypeId((String) row.get("SERVICE_TYPE_ID"));
426 cldsMonitoringDetails.setDeploymentId((String) row.get("DEPLOYMENT_ID"));
427 cldsMonitoringDetails.setTemplateName((String) row.get("TEMPLATE_NAME"));
428 cldsMonitoringDetails.setAction((String) row.get("ACTION_CD"));
429 cldsMonitoringDetails.setUserid((String) row.get("USER_ID"));
430 cldsMonitoringDetails.setTimestamp(sdf.format(row.get("TIMESTAMP")));
431 cldsMonitoringDetailsList.add(cldsMonitoringDetails);
433 return cldsMonitoringDetailsList;
437 * Method to delete model from database.
441 public void deleteModel(String modelName) {
442 SqlParameterSource in = new MapSqlParameterSource().addValue("v_model_name", modelName);
443 logSqlExecution(procDeleteModel, in);
446 private void populateModelProperties(CldsModel model, Map out) {
447 // todo : rationalize
448 model.setControlNamePrefix((String) out.get(V_CONTROL_NAME_PREFIX));
449 model.setControlNameUuid((String) out.get(V_CONTROL_NAME_UUID));
450 model.setId((String) (out.get("v_model_id")));
451 model.setTemplateId((String) (out.get("v_template_id")));
452 model.setTemplateName((String) (out.get("v_template_name")));
453 model.setBpmnText((String) out.get("v_template_bpmn_text"));
454 model.setPropText((String) out.get("v_model_prop_text"));
455 model.setImageText((String) out.get("v_template_image_text"));
456 model.setDocText((String) out.get("v_template_doc_text"));
457 model.setBlueprintText((String) out.get("v_model_blueprint_text"));
458 model.getEvent().setId((String) (out.get("v_event_id")));
459 model.getEvent().setActionCd((String) out.get("v_action_cd"));
460 model.getEvent().setActionStateCd((String) out.get("v_action_state_cd"));
461 model.getEvent().setProcessInstanceId((String) out.get("v_event_process_instance_id"));
462 model.getEvent().setUserid((String) out.get("v_event_user_id"));
463 model.setTypeId((String) out.get("v_service_type_id"));
464 model.setDeploymentId((String) out.get("v_deployment_id"));