2 * Copyright © 2017 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.Optional;
16 import java.util.concurrent.ExecutionException;
17 import java.util.regex.Pattern;
19 import javax.sql.DataSource;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.transportpce.common.Timeouts;
23 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Info;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class DeviceInventory {
31 private static final String INSERT_ALARM_STRING =
32 "insert into inv_alarm_info(nodeid, probablecause, direction,extension,location,"
33 + "notificationid,type,raisetime,severity,circuitid,circuitpack,connection,degree,iface,"
34 + "internallink,physicallink,service,shelf,sharedriskgroup,port,portcircuitpack, create_date, update_date) "
35 + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
37 private static final Logger LOG = LoggerFactory.getLogger(DeviceInventory.class);
39 private final DataSource dataSource;
40 private final INode inode;
41 private final DeviceTransactionManager deviceTransactionManager;
43 public DeviceInventory(DataSource dataSource, INode inode,
44 DeviceTransactionManager deviceTransactionManager) {
45 this.dataSource = dataSource;
47 this.deviceTransactionManager = deviceTransactionManager;
51 LOG.info("Initializing {}", DeviceInventory.class.getName());
54 public void initializeDevice(String deviceId) throws InterruptedException, ExecutionException {
55 InstanceIdentifier<Info> infoIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Info.class);
56 Optional<Info> infoOpt =
57 this.deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.OPERATIONAL, infoIID,
58 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
61 if (infoOpt.isPresent()) {
62 deviceInfo = infoOpt.get();
64 LOG.warn("Could not get device info from DataBrooker");
67 LOG.info("Creating Device Inventory {}", deviceInfo);
68 if (!this.inode.nodeExists(deviceId)) {
69 LOG.info("Adding node {} to inventory", deviceId);
70 this.inode.addNode(deviceInfo);
71 this.inode.getRoadmShelves(deviceId);
72 this.inode.getCircuitPacks(deviceId);
77 * Stores the alarm into DB using {@link PreparedStatement}.
79 * @param alarmString an alarm string
80 * @return number of rows inserted
82 public int storeAlarm(String alarmString) {
83 String delimiter = "|";
84 String[] splitAlarmString = alarmString.split(Pattern.quote(delimiter));
86 try (Connection connection = this.dataSource.getConnection();
87 PreparedStatement statement = connection.prepareStatement(INSERT_ALARM_STRING)) {
88 LOG.debug("Inserting prepared stmt for {} query", INSERT_ALARM_STRING);
89 SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
90 java.util.Date startTimetamp = new Date();
91 String startTimetampStr = simpleDate.format(startTimetamp);
93 for (int i = 0; i < 21; i++) {
94 String value = (splitAlarmString.length >= (i + 1)) ? splitAlarmString[i] : "";
95 LOG.debug("Setting parameter {}, to {} in the insert alarm query", i + 1, value);
96 statement.setString(i + 1, value);
98 statement.setString(22, startTimetampStr);
99 statement.setString(23, startTimetampStr);
100 LOG.debug("Setting current time and edited time to {}", startTimetampStr);
101 count = statement.executeUpdate();
102 LOG.debug("Statment {}, returned {}", INSERT_ALARM_STRING, count);
103 statement.clearParameters();
104 } catch (SQLException e) {
105 LOG.error(e.getMessage(), e);