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
9 package org.opendaylight.transportpce.inventory;
11 import java.sql.Connection;
12 import java.sql.PreparedStatement;
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import javax.sql.DataSource;
16 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
21 value = "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING",
22 justification = "TODO review the SQL statement generation process")
24 private static final Logger LOG = LoggerFactory.getLogger(INode.class);
26 private final DataSource dataSource;
27 private final DeviceTransactionManager deviceTransactionManager;
28 //private final INode221 inode221;
29 private final INode121 inode121;
31 public INode(DataSource dataSource, DeviceTransactionManager deviceTransactionManager,
35 this.dataSource = dataSource;
36 this.deviceTransactionManager = deviceTransactionManager;
37 this.inode121 = inode121;
38 //this.inode221 = inode221;
41 public boolean addNode(String deviceId, String openROADMversion) {
42 boolean sqlResult = false;
43 return inode121.addNode(deviceId);
47 public boolean nodeExists(String nodeId) {
48 String selectTableSQL = "select count(*) node_exists from inv_dev_info where node_id = ?";
50 LOG.info("Checking if {} exists in DB", nodeId);
51 try (Connection connection = dataSource.getConnection();
52 PreparedStatement preparedStmt = connection.prepareStatement(selectTableSQL)) {
53 preparedStmt.setString(1, nodeId);
54 try (ResultSet rs = preparedStmt.executeQuery()) {
56 nodeExists = rs.getInt("node_exists");
57 LOG.debug("Found {} devices matching {}", nodeExists, nodeId);
60 } catch (SQLException e) {
61 LOG.error("Something wrong when fetching node in DB", e);
63 return nodeExists == 0 ? false : true;
66 public boolean dataExists(String tableName, String searchKeys) {
67 String selectTableSQL = "select count(*) data_exists from " + tableName + " where " + searchKeys;
69 LOG.info("Checking if {} exists in DB", searchKeys);
70 try (Connection connection = dataSource.getConnection();
71 PreparedStatement preparedStmt = connection.prepareStatement(selectTableSQL)) {
73 try (ResultSet rs = preparedStmt.executeQuery()) {
75 dataExists = rs.getInt("data_exists");
76 LOG.debug("Found {} devices matching {}", dataExists, searchKeys);
79 } catch (SQLException e) {
80 LOG.error("Something wrong when fetching data in DB", e);
82 return dataExists == 0 ? false : true;
85 /* public void getRoadmShelves(String nodeId, String openRoadmVersion)
86 throws InterruptedException, ExecutionException {
88 LOG.info("ROADMSHELVES");
89 if (openRoadmVersion.equalsIgnoreCase(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1)) {
90 inode121.getRoadmShelves(nodeId);
92 else if (openRoadmVersion.equalsIgnoreCase(StringConstants.OPENROADM_DEVICE_VERSION_2_2)) {
93 inode22.getRoadmShelves(nodeId);
98 public void getCircuitPacks(String nodeId, String openRoadmVersion)
99 throws InterruptedException, ExecutionException {
101 LOG.info("ROADMCircuitPacks");
102 if (openRoadmVersion.equalsIgnoreCase(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1)) {
103 inode121.getCircuitPacks(nodeId);
105 else if (openRoadmVersion.equalsIgnoreCase(StringConstants.OPENROADM_DEVICE_VERSION_2_2)) {
106 inode22.getCircuitPacks(nodeId);