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