fix import extra separations
[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.opendaylight.transportpce.common.device.DeviceTransactionManager;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
28
29     private static final Logger LOG = LoggerFactory.getLogger(DeviceInventory.class);
30
31     private final DataSource dataSource;
32     private final INode inode;
33     private final DeviceTransactionManager deviceTransactionManager;
34
35     public DeviceInventory(DataSource dataSource, INode inode,
36                            DeviceTransactionManager deviceTransactionManager) {
37         this.dataSource = dataSource;
38         this.inode = inode;
39         this.deviceTransactionManager = deviceTransactionManager;
40     }
41
42     public void init() {
43         LOG.info("Initializing {}", DeviceInventory.class.getName());
44     }
45
46     public void initializeDevice(String deviceId, String openRoadmVersion)
47         throws InterruptedException, ExecutionException {
48
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);
53         }
54     }
55
56     /**
57      * Stores the alarm into DB using {@link PreparedStatement}.
58      *
59      * @param alarmString an alarm
60      * @return number of rows inserted
61      */
62     public int storeAlarm(String alarmString) {
63         String delimiter = "|";
64         String[] splitAlarmString = alarmString.split(Pattern.quote(delimiter));
65         int count = 0;
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);
72
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);
77             }
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(e.getMessage(), e);
86         }
87         return count;
88     }
89 }