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;
18 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public class DeviceInventory {
23 private static final String INSERT_ALARM_STRING =
24 "insert into inv_alarm_info(nodeid, probablecause, direction,extension,location,"
25 + "notificationid,type,raisetime,severity,circuitid,circuitpack,connection,degree,iface,"
26 + "internallink,physicallink,service,shelf,sharedriskgroup,port,portcircuitpack, create_date, update_date) "
27 + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
29 private static final Logger LOG = LoggerFactory.getLogger(DeviceInventory.class);
31 private final DataSource dataSource;
32 private final INode inode;
33 private final DeviceTransactionManager deviceTransactionManager;
35 public DeviceInventory(DataSource dataSource, INode inode,
36 DeviceTransactionManager deviceTransactionManager) {
37 this.dataSource = dataSource;
39 this.deviceTransactionManager = deviceTransactionManager;
43 LOG.info("Initializing {}", DeviceInventory.class.getName());
46 public void initializeDevice(String deviceId, String openRoadmVersion)
47 throws InterruptedException, ExecutionException {
49 LOG.info("Creating Device Inventory for device {} with version {}", deviceId, openRoadmVersion);
50 if (!inode.dataExists("inv_dev_info", " node_id = '" + deviceId + "'")) {
51 LOG.info("Adding node {} to inventory", deviceId);
52 inode.addNode(deviceId, openRoadmVersion);
57 * Stores the alarm into DB using {@link PreparedStatement}.
59 * @param alarmString an alarm
60 * @return number of rows inserted
62 public int storeAlarm(String alarmString) {
63 String delimiter = "|";
64 String[] splitAlarmString = alarmString.split(Pattern.quote(delimiter));
66 try (Connection connection = dataSource.getConnection();
67 PreparedStatement statement = connection.prepareStatement(INSERT_ALARM_STRING)) {
68 LOG.debug("Inserting prepared stmt for {} query", INSERT_ALARM_STRING);
69 SimpleDateFormat myTimeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
70 Date startTimetamp = new Date();
71 String startTimetampStr = myTimeStamp.format(startTimetamp);
73 for (int i = 0; i < 21; i++) {
74 String value = (splitAlarmString.length >= i + 1) ? splitAlarmString[i] : "";
75 LOG.debug("Setting parameter {}, to {} in the insert alarm query", i + 1, value);
76 statement.setString(i + 1, value);
78 statement.setString(22, startTimetampStr);
79 statement.setString(23, startTimetampStr);
80 LOG.debug("Setting current time and edited time to {}", startTimetampStr);
81 count = statement.executeUpdate();
82 LOG.debug("Statment {}, returned {}", INSERT_ALARM_STRING, count);
83 statement.clearParameters();
84 } catch (SQLException e) {
85 LOG.error("Something wrong when storing Alarm into DB", e);