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 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.CldsMonitoringDetails;
39 import org.onap.clamp.clds.model.CldsDbServiceCache;
40 import org.onap.clamp.clds.model.CldsEvent;
41 import org.onap.clamp.clds.model.CldsModel;
42 import org.onap.clamp.clds.model.CldsModelInstance;
43 import org.onap.clamp.clds.model.CldsModelProp;
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 static final String HEALTHCHECK = "Select 1";
74 * Log message when instantiating
77 logger.info("CldsDao instantiating...");
81 * When dataSource is provided, instantiate spring jdbc objects.
83 public void setDataSource(DataSource dataSource) {
84 this.jdbcTemplateObject = new JdbcTemplate(dataSource);
85 this.procGetModel = new SimpleJdbcCall(dataSource).withProcedureName("get_model");
86 this.procGetModelTemplate = new SimpleJdbcCall(dataSource).withProcedureName("get_model_template");
87 this.procSetModel = new SimpleJdbcCall(dataSource).withProcedureName("set_model");
88 this.procInsEvent = new SimpleJdbcCall(dataSource).withProcedureName("ins_event");
89 this.procUpdEvent = new SimpleJdbcCall(dataSource).withProcedureName("upd_event");
90 this.procGetTemplate = new SimpleJdbcCall(dataSource).withProcedureName("get_template");
91 this.procSetTemplate = new SimpleJdbcCall(dataSource).withProcedureName("set_template");
92 this.procInsModelInstance = new SimpleJdbcCall(dataSource).withProcedureName("ins_model_instance");
93 this.procDelAllModelInstances = new SimpleJdbcCall(dataSource).withProcedureName("del_all_model_instances");
97 * Get a model from the database given the model name.
99 public CldsModel getModel(String modelName) {
100 return getModel(modelName, null);
104 * Get a model from the database given the controlNameUuid.
106 public CldsModel getModelByUuid(String controlNameUuid) {
107 return getModel(null, controlNameUuid);
110 // Get a model from the database given the model name or a controlNameUuid.
111 private CldsModel getModel(String modelName, String controlNameUuid) {
112 CldsModel model = new CldsModel();
113 model.setName(modelName);
114 SqlParameterSource in = new MapSqlParameterSource().addValue("v_model_name", modelName)
115 .addValue("v_control_name_uuid", controlNameUuid);
116 Map<String, Object> out = logSqlExecution(procGetModel, in);
117 model.setControlNamePrefix((String) out.get("v_control_name_prefix"));
118 model.setControlNameUuid((String) out.get("v_control_name_uuid"));
119 model.setId((String) (out.get("v_model_id")));
120 model.setTemplateId((String) (out.get("v_template_id")));
121 model.setTemplateName((String) (out.get("v_template_name")));
122 model.setBpmnText((String) out.get("v_template_bpmn_text"));
123 model.setPropText((String) out.get("v_model_prop_text"));
124 model.setImageText((String) out.get("v_template_image_text"));
125 model.setDocText((String) out.get("v_template_doc_text"));
126 model.setBlueprintText((String) out.get("v_model_blueprint_text"));
127 model.getEvent().setId((String) (out.get("v_event_id")));
128 model.getEvent().setActionCd((String) out.get("v_action_cd"));
129 model.getEvent().setActionStateCd((String) out.get("v_action_state_cd"));
130 model.getEvent().setProcessInstanceId((String) out.get("v_event_process_instance_id"));
131 model.getEvent().setUserid((String) out.get("v_event_user_id"));
132 model.setTypeId((String) out.get("v_service_type_id"));
133 model.setDeploymentId((String) out.get("v_deployment_id"));
138 * Get a model and template information from the database given the model
144 public CldsModel getModelTemplate(String modelName) {
145 CldsModel model = new CldsModel();
146 model.setName(modelName);
147 SqlParameterSource in = new MapSqlParameterSource().addValue("v_model_name", modelName);
148 Map<String, Object> out = logSqlExecution(procGetModelTemplate, in);
149 // todo : rationalize
150 model.setControlNamePrefix((String) out.get("v_control_name_prefix"));
151 model.setControlNameUuid((String) out.get("v_control_name_uuid"));
152 model.setId((String) (out.get("v_model_id")));
153 model.setTemplateId((String) (out.get("v_template_id")));
154 model.setTemplateName((String) (out.get("v_template_name")));
155 model.setBpmnText((String) out.get("v_template_bpmn_text"));
156 model.setPropText((String) out.get("v_model_prop_text"));
157 model.setImageText((String) out.get("v_template_image_text"));
158 model.setDocText((String) out.get("v_template_doc_text"));
159 model.setBlueprintText((String) out.get("v_model_blueprint_text"));
160 model.getEvent().setId((String) (out.get("v_event_id")));
161 model.getEvent().setActionCd((String) out.get("v_action_cd"));
162 model.getEvent().setActionStateCd((String) out.get("v_action_state_cd"));
163 model.getEvent().setProcessInstanceId((String) out.get("v_event_process_instance_id"));
164 model.getEvent().setUserid((String) out.get("v_event_user_id"));
165 model.setTypeId((String) out.get("v_service_type_id"));
166 model.setDeploymentId((String) out.get("v_deployment_id"));
167 Map<String, Object> modelResults = logSqlExecution(procGetModel, in);
168 Object modelResultObject = modelResults.get("#result-set-1");
169 if (modelResultObject != null && modelResultObject instanceof ArrayList) {
170 List<Object> modelInstanceRs = (List<Object>) modelResultObject;
171 for (Object currModelInstance : modelInstanceRs) {
172 if (currModelInstance != null && currModelInstance instanceof HashMap) {
173 HashMap<String, String> modelInstanceMap = (HashMap<String, String>) currModelInstance;
174 CldsModelInstance modelInstance = new CldsModelInstance();
175 modelInstance.setModelInstanceId(modelInstanceMap.get("model_instance_id"));
176 modelInstance.setVmName(modelInstanceMap.get("vm_name"));
177 modelInstance.setLocation(modelInstanceMap.get("location"));
178 model.getCldsModelInstanceList().add(modelInstance);
179 logger.info("value of currModel: {}", currModelInstance);
187 * Update model in the database using parameter values and return updated
194 public CldsModel setModel(CldsModel model, String userid) {
195 SqlParameterSource in = new MapSqlParameterSource().addValue("v_model_name", model.getName())
196 .addValue("v_template_id", model.getTemplateId()).addValue("v_user_id", userid)
197 .addValue("v_model_prop_text", model.getPropText())
198 .addValue("v_model_blueprint_text", model.getBlueprintText())
199 .addValue("v_service_type_id", model.getTypeId()).addValue("v_deployment_id", model.getDeploymentId())
200 .addValue("v_control_name_prefix", model.getControlNamePrefix())
201 .addValue("v_control_name_uuid", model.getControlNameUuid());
202 Map<String, Object> out = logSqlExecution(procSetModel, in);
203 model.setControlNamePrefix((String) out.get("v_control_name_prefix"));
204 model.setControlNameUuid((String) out.get("v_control_name_uuid"));
205 model.setId((String) (out.get("v_model_id")));
206 model.getEvent().setId((String) (out.get("v_event_id")));
207 model.getEvent().setActionCd((String) out.get("v_action_cd"));
208 model.getEvent().setActionStateCd((String) out.get("v_action_state_cd"));
209 model.getEvent().setProcessInstanceId((String) out.get("v_event_process_instance_id"));
210 model.getEvent().setUserid((String) out.get("v_event_user_id"));
215 * Inserts new modelInstance in the database using parameter values and
216 * return updated model object.
219 * @param modelInstancesList
222 public void insModelInstance(CldsModel model, List<CldsModelInstance> modelInstancesList) {
223 // Delete all existing model instances for given controlNameUUID
224 logger.debug("deleting instances for: {}", model.getControlNameUuid());
225 delAllModelInstances(model.getControlNameUuid());
226 if (modelInstancesList == null) {
227 logger.debug("modelInstancesList == null");
229 for (CldsModelInstance currModelInstance : modelInstancesList) {
230 logger.debug("v_control_name_uuid={}", model.getControlNameUuid());
231 logger.debug("v_vm_name={}", currModelInstance.getVmName());
232 logger.debug("v_location={}", currModelInstance.getLocation());
233 SqlParameterSource in = new MapSqlParameterSource()
234 .addValue("v_control_name_uuid", model.getControlNameUuid())
235 .addValue("v_vm_name", currModelInstance.getVmName())
236 .addValue("v_location", currModelInstance.getLocation());
237 Map<String, Object> out = logSqlExecution(procInsModelInstance, in);
238 model.setId((String) (out.get("v_model_id")));
239 CldsModelInstance modelInstance = new CldsModelInstance();
240 modelInstance.setLocation(currModelInstance.getLocation());
241 modelInstance.setVmName(currModelInstance.getVmName());
242 modelInstance.setModelInstanceId((String) (out.get("v_model_instance_id")));
243 model.getCldsModelInstanceList().add(modelInstance);
249 * Insert an event in the database - require either modelName or
250 * controlNamePrefix/controlNameUuid.
253 * @param controlNamePrefix
254 * @param controlNameUuid
258 public CldsEvent insEvent(String modelName, String controlNamePrefix, String controlNameUuid, CldsEvent cldsEvent) {
259 CldsEvent event = new CldsEvent();
260 SqlParameterSource in = new MapSqlParameterSource().addValue("v_model_name", modelName)
261 .addValue("v_control_name_prefix", controlNamePrefix).addValue("v_control_name_uuid", controlNameUuid)
262 .addValue("v_user_id", cldsEvent.getUserid()).addValue("v_action_cd", cldsEvent.getActionCd())
263 .addValue("v_action_state_cd", cldsEvent.getActionStateCd())
264 .addValue("v_process_instance_id", cldsEvent.getProcessInstanceId());
265 Map<String, Object> out = logSqlExecution(procInsEvent, in);
266 event.setId((String) (out.get("v_event_id")));
270 private String delAllModelInstances(String controlNameUUid) {
271 SqlParameterSource in = new MapSqlParameterSource().addValue("v_control_name_uuid", controlNameUUid);
272 Map<String, Object> out = logSqlExecution(procDelAllModelInstances, in);
273 return (String) (out.get("v_model_id"));
277 * Update event with process instance id.
280 * @param processInstanceId
282 public void updEvent(String eventId, String processInstanceId) {
283 SqlParameterSource in = new MapSqlParameterSource().addValue("v_event_id", eventId)
284 .addValue("v_process_instance_id", processInstanceId);
285 logSqlExecution(procUpdEvent, in);
289 * Return list of model names
291 * @return model names
293 public List<ValueItem> getBpmnNames() {
294 String sql = "SELECT model_name FROM model ORDER BY 1;";
295 return jdbcTemplateObject.query(sql, new ValueItemMapper());
299 * Update template in the database using parameter values and return updated
305 public void setTemplate(CldsTemplate template, String userid) {
306 SqlParameterSource in = new MapSqlParameterSource().addValue("v_template_name", template.getName())
307 .addValue("v_user_id", userid).addValue("v_template_bpmn_text", template.getBpmnText())
308 .addValue("v_template_image_text", template.getImageText())
309 .addValue("v_template_doc_text", template.getPropText());
310 Map<String, Object> out = logSqlExecution(procSetTemplate, in);
311 template.setId((String) (out.get("v_template_id")));
312 template.setBpmnUserid((String) (out.get("v_template_bpmn_user_id")));
313 template.setBpmnId((String) (out.get("v_template_bpmn_id")));
314 template.setBpmnText((String) (out.get("v_template_bpmn_text")));
315 template.setImageId((String) (out.get("v_template_image_id")));
316 template.setImageUserid((String) out.get("v_template_image_user_id"));
317 template.setImageText((String) out.get("v_template_image_text"));
318 template.setPropId((String) (out.get("v_template_doc_id")));
319 template.setPropUserid((String) out.get("v_template_doc_user_id"));
320 template.setPropText((String) out.get("v_template_doc_text"));
324 * Return list of template names
326 * @return template names
328 public List<ValueItem> getTemplateNames() {
329 String sql = "SELECT template_name FROM template ORDER BY 1;";
330 return jdbcTemplateObject.query(sql, new ValueItemMapper());
334 * Get a template from the database given the model name.
336 * @param templateName
339 public CldsTemplate getTemplate(String templateName) {
340 CldsTemplate template = new CldsTemplate();
341 template.setName(templateName);
342 SqlParameterSource in = new MapSqlParameterSource().addValue("v_template_name", templateName);
343 Map<String, Object> out = logSqlExecution(procGetTemplate, in);
344 template.setId((String) (out.get("v_template_id")));
345 template.setBpmnUserid((String) (out.get("v_template_bpmn_user_id")));
346 template.setBpmnId((String) (out.get("v_template_bpmn_id")));
347 template.setBpmnText((String) (out.get("v_template_bpmn_text")));
348 template.setImageId((String) (out.get("v_template_image_id")));
349 template.setImageUserid((String) out.get("v_template_image_user_id"));
350 template.setImageText((String) out.get("v_template_image_text"));
351 template.setPropId((String) (out.get("v_template_doc_id")));
352 template.setPropUserid((String) out.get("v_template_doc_user_id"));
353 template.setPropText((String) out.get("v_template_doc_text"));
357 public void clearServiceCache() {
358 String clearCldsServiceCacheSql = "TRUNCATE clds_service_cache";
359 jdbcTemplateObject.execute(clearCldsServiceCacheSql);
362 public CldsServiceData getCldsServiceCache(String invariantUUID) {
363 CldsServiceData cldsServiceData = null;
365 String getCldsServiceSQL = "SELECT * , TIMESTAMPDIFF(SECOND, timestamp, CURRENT_TIMESTAMP()) FROM clds_service_cache where invariant_service_id = ? ";
366 cldsServiceData = jdbcTemplateObject.queryForObject(getCldsServiceSQL, new Object[] {
368 }, new CldsServiceDataMapper());
369 if (cldsServiceData != null) {
370 logger.info("CldsServiceData found in cache for Service Invariant ID:"
371 + cldsServiceData.getServiceInvariantUUID());
372 return cldsServiceData;
374 logger.warn("CldsServiceData not found in cache for Service Invariant ID:" + invariantUUID);
377 } catch (EmptyResultDataAccessException e) {
378 logger.info("CldsServiceData not found in cache for Service Invariant ID: " + invariantUUID);
379 logger.debug("CldsServiceData not found in cache for Service Invariant ID: " + invariantUUID, e);
384 public void setCldsServiceCache(CldsDbServiceCache cldsDBServiceCache) {
385 if (cldsDBServiceCache != null && cldsDBServiceCache.getInvariantId() != null
386 && cldsDBServiceCache.getServiceId() != null) {
387 String invariantUuid = cldsDBServiceCache.getInvariantId();
388 String serviceUuid = cldsDBServiceCache.getServiceId();
389 InputStream is = cldsDBServiceCache.getCldsDataInstream();
390 String insertCldsServiceCacheSql = "INSERT INTO clds_service_cache"
391 + "(invariant_service_id,service_id,timestamp,object_data) VALUES"
392 + "(?,?,CURRENT_TIMESTAMP,?) ON DUPLICATE KEY UPDATE invariant_service_id = VALUES(invariant_service_id) , timestamp = CURRENT_TIMESTAMP , object_data = VALUES(object_data) ";
393 jdbcTemplateObject.update(insertCldsServiceCacheSql, invariantUuid, serviceUuid, is);
397 private static Map<String, Object> logSqlExecution(SimpleJdbcCall call, SqlParameterSource source) {
399 return call.execute(source);
400 } catch (Exception e) {
401 logger.error("Exception occured in " + source.getClass().getCanonicalName() + ": " + e);
406 public void doHealthCheck() {
407 jdbcTemplateObject.execute(HEALTHCHECK);
411 * Method to get deployed/active models with model properties.
413 * @return list of CldsModelProp
415 public List<CldsModelProp> getDeployedModelProperties() {
416 List<CldsModelProp> cldsModelPropList = new ArrayList<>();
417 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 "
418 + "WHERE m.model_prop_id = mp.model_prop_id and m.event_id = e.event_id and e.action_cd = 'DEPLOY'";
419 List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(modelsSql);
420 CldsModelProp cldsModelProp = null;
421 for (Map<String, Object> row : rows) {
422 cldsModelProp = new CldsModelProp();
423 cldsModelProp.setId((String) row.get("model_id"));
424 cldsModelProp.setName((String) row.get("model_name"));
425 cldsModelProp.setPropId((String) row.get("model_prop_id"));
426 cldsModelProp.setPropText((String) row.get("model_prop_text"));
427 cldsModelPropList.add(cldsModelProp);
429 return cldsModelPropList;
433 * Method to get deployed/active models with model properties.
435 * @return list of CLDS-Monitoring-Details: CLOSELOOP_NAME | Close loop name
436 * used in the CLDS application (prefix: ClosedLoop- + unique
437 * ClosedLoop ID) MODEL_NAME | Model Name in CLDS application
438 * SERVICE_TYPE_ID | TypeId returned from the DCAE application when
439 * the ClosedLoop is submitted (DCAEServiceTypeRequest generated in
440 * DCAE application). DEPLOYMENT_ID | Id generated when the
441 * ClosedLoop is deployed in DCAE. TEMPLATE_NAME | Template used to
442 * generate the ClosedLoop model. ACTION_CD | Current state of the
443 * ClosedLoop in CLDS application.
445 public List<CldsMonitoringDetails> getCLDSMonitoringDetails() {
446 SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
447 List<CldsMonitoringDetails> cldsMonitoringDetailsList = new ArrayList<CldsMonitoringDetails>();
448 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 "
449 + "FROM MODEL M, TEMPLATE T, EVENT E "
450 + "WHERE M.TEMPLATE_ID = T.TEMPLATE_ID AND M.EVENT_ID = E.EVENT_ID " + "ORDER BY ACTION_CD";
451 List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(modelsSql);
452 CldsMonitoringDetails cldsMonitoringDetails = null;
453 for (Map<String, Object> row : rows) {
454 cldsMonitoringDetails = new CldsMonitoringDetails();
455 cldsMonitoringDetails.setCloseloopName((String) row.get("CLOSELOOP_NAME"));
456 cldsMonitoringDetails.setModelName((String) row.get("MODEL_NAME"));
457 cldsMonitoringDetails.setServiceTypeId((String) row.get("SERVICE_TYPE_ID"));
458 cldsMonitoringDetails.setDeploymentId((String) row.get("DEPLOYMENT_ID"));
459 cldsMonitoringDetails.setTemplateName((String) row.get("TEMPLATE_NAME"));
460 cldsMonitoringDetails.setAction((String) row.get("ACTION_CD"));
461 cldsMonitoringDetails.setUserid((String) row.get("USER_ID"));
462 cldsMonitoringDetails.setTimestamp(sdf.format(row.get("TIMESTAMP")));
463 cldsMonitoringDetailsList.add(cldsMonitoringDetails);
465 return cldsMonitoringDetailsList;