Abort power setup if setting gainloss fails
[transportpce.git] / inventory / src / main / java / org / opendaylight / transportpce / inventory / INode.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
9 package org.opendaylight.transportpce.inventory;
10
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.sql.Connection;
13 import java.sql.PreparedStatement;
14 import java.sql.ResultSet;
15 import java.sql.SQLException;
16 import javax.sql.DataSource;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 @SuppressFBWarnings(
21     value = "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING",
22     justification = "TODO review the SQL statement generation process")
23 public class INode {
24     private static final Logger LOG = LoggerFactory.getLogger(INode.class);
25
26     private final DataSource dataSource;
27     //private final INode221 inode221;
28     private final INode121 inode121;
29
30     public INode(DataSource dataSource, INode121 inode121
31         //, INode221 inode221
32     ) {
33         this.dataSource = dataSource;
34         this.inode121 = inode121;
35         //this.inode221 = inode221;
36     }
37
38     public boolean addNode(String deviceId, String openROADMversion) {
39         //boolean sqlResult = false;
40         return inode121.addNode(deviceId);
41
42     }
43
44     public boolean nodeExists(String nodeId) {
45         String selectTableSQL = "select count(*) node_exists from inv_dev_info where node_id = ?";
46         int nodeExists = 0;
47         LOG.info("Checking if {} exists in DB", nodeId);
48         try (Connection connection = dataSource.getConnection();
49                 PreparedStatement preparedStmt = connection.prepareStatement(selectTableSQL)) {
50             preparedStmt.setString(1, nodeId);
51             try (ResultSet rs = preparedStmt.executeQuery()) {
52                 while (rs.next()) {
53                     nodeExists = rs.getInt("node_exists");
54                     LOG.debug("Found {} devices matching {}", nodeExists, nodeId);
55                 }
56             }
57         } catch (SQLException e) {
58             LOG.error("Something wrong when fetching node in DB", e);
59         }
60         return nodeExists != 0;
61     }
62
63     public boolean dataExists(String tableName, String searchKeys) {
64         String selectTableSQL = "select count(*) data_exists from " + tableName + " where " + searchKeys;
65         int dataExists = 0;
66         LOG.info("Checking if {} exists in DB", searchKeys);
67         try (Connection connection = dataSource.getConnection();
68              PreparedStatement preparedStmt = connection.prepareStatement(selectTableSQL)) {
69
70             try (ResultSet rs = preparedStmt.executeQuery()) {
71                 while (rs.next()) {
72                     dataExists = rs.getInt("data_exists");
73                     LOG.debug("Found {} devices matching {}", dataExists, searchKeys);
74                 }
75             }
76         } catch (SQLException e) {
77             LOG.error("Something wrong when fetching data in DB", e);
78         }
79         return dataExists != 0;
80     }
81
82   /*  public void getRoadmShelves(String nodeId, String openRoadmVersion)
83         throws InterruptedException, ExecutionException {
84
85         LOG.info("ROADMSHELVES");
86         if (openRoadmVersion.equalsIgnoreCase(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1)) {
87             inode121.getRoadmShelves(nodeId);
88         }
89         else if (openRoadmVersion.equalsIgnoreCase(StringConstants.OPENROADM_DEVICE_VERSION_2_2)) {
90             inode22.getRoadmShelves(nodeId);
91         }
92         return;
93     }
94
95     public void getCircuitPacks(String nodeId, String openRoadmVersion)
96         throws InterruptedException, ExecutionException {
97
98         LOG.info("ROADMCircuitPacks");
99         if (openRoadmVersion.equalsIgnoreCase(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1)) {
100             inode121.getCircuitPacks(nodeId);
101         }
102         else if (openRoadmVersion.equalsIgnoreCase(StringConstants.OPENROADM_DEVICE_VERSION_2_2)) {
103             inode22.getCircuitPacks(nodeId);
104         }
105         return;
106     }
107 */
108
109
110 }