2 * Copyright © 2016 AT&T and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.transportpce.inventory;
10 import java.sql.Connection;
11 import java.sql.PreparedStatement;
12 import java.sql.SQLException;
13 import java.text.SimpleDateFormat;
14 import java.util.Date;
15 import java.util.concurrent.ExecutionException;
16 import java.util.regex.Pattern;
17 import javax.sql.DataSource;
19 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class DeviceInventory {
24 private static final String INSERT_ALARM_STRING =
25 "insert into inv_alarm_info(nodeid, probablecause, direction,extension,location,"
26 + "notificationid,type,raisetime,severity,circuitid,circuitpack,connection,degree,iface,"
27 + "internallink,physicallink,service,shelf,sharedriskgroup,port,portcircuitpack, create_date, update_date) "
28 + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
30 private static final Logger LOG = LoggerFactory.getLogger(DeviceInventory.class);
32 private final DataSource dataSource;
33 private final INode inode;
34 private final DeviceTransactionManager deviceTransactionManager;
36 public DeviceInventory(DataSource dataSource, INode inode,
37 DeviceTransactionManager deviceTransactionManager) {
38 this.dataSource = dataSource;
40 this.deviceTransactionManager = deviceTransactionManager;
44 LOG.info("Initializing {}", DeviceInventory.class.getName());
47 public void initializeDevice(String deviceId, String openRoadmVersion)
48 throws InterruptedException, ExecutionException {
50 LOG.info("Creating Device Inventory for device {} with version {}", deviceId, openRoadmVersion);
51 if (!inode.dataExists("inv_dev_info", " node_id = '" + deviceId + "'")) {
52 LOG.info("Adding node {} to inventory", deviceId);
53 inode.addNode(deviceId, openRoadmVersion);
58 * Stores the alarm into DB using {@link PreparedStatement}.
60 * @param alarmString an alarm
61 * @return number of rows inserted
63 public int storeAlarm(String alarmString) {
64 String delimiter = "|";
65 String[] splitAlarmString = alarmString.split(Pattern.quote(delimiter));
67 try (Connection connection = dataSource.getConnection();
68 PreparedStatement statement = connection.prepareStatement(INSERT_ALARM_STRING)) {
69 LOG.debug("Inserting prepared stmt for {} query", INSERT_ALARM_STRING);
70 SimpleDateFormat myTimeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
71 Date startTimetamp = new Date();
72 String startTimetampStr = myTimeStamp.format(startTimetamp);
74 for (int i = 0; i < 21; i++) {
75 String value = (splitAlarmString.length >= i + 1) ? splitAlarmString[i] : "";
76 LOG.debug("Setting parameter {}, to {} in the insert alarm query", i + 1, value);
77 statement.setString(i + 1, value);
79 statement.setString(22, startTimetampStr);
80 statement.setString(23, startTimetampStr);
81 LOG.debug("Setting current time and edited time to {}", startTimetampStr);
82 count = statement.executeUpdate();
83 LOG.debug("Statment {}, returned {}", INSERT_ALARM_STRING, count);
84 statement.clearParameters();
85 } catch (SQLException e) {
86 LOG.error(e.getMessage(), e);