c7299c835b230c05d60ca0d7e821627c3b159407
[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 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;
19
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")
23 public class INode {
24     private static final Logger LOG = LoggerFactory.getLogger(INode.class);
25
26     private final DataSource dataSource;
27     private final DeviceTransactionManager deviceTransactionManager;
28     //private final INode221 inode221;
29     private final INode121 inode121;
30
31     public INode(DataSource dataSource, DeviceTransactionManager deviceTransactionManager,
32         INode121 inode121
33         //, INode221 inode221
34     ) {
35         this.dataSource = dataSource;
36         this.deviceTransactionManager = deviceTransactionManager;
37         this.inode121 = inode121;
38         //this.inode221 = inode221;
39     }
40
41     public boolean addNode(String deviceId, String openROADMversion) {
42         //boolean sqlResult = false;
43         return inode121.addNode(deviceId);
44
45     }
46
47     public boolean nodeExists(String nodeId) {
48         String selectTableSQL = "select count(*) node_exists from inv_dev_info where node_id = ?";
49         int nodeExists = 0;
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()) {
55                 while (rs.next()) {
56                     nodeExists = rs.getInt("node_exists");
57                     LOG.debug("Found {} devices matching {}", nodeExists, nodeId);
58                 }
59             }
60         } catch (SQLException e) {
61             LOG.error("Something wrong when fetching node in DB", e);
62         }
63         return nodeExists != 0;
64     }
65
66     public boolean dataExists(String tableName, String searchKeys) {
67         String selectTableSQL = "select count(*) data_exists from " + tableName + " where " + searchKeys;
68         int dataExists = 0;
69         LOG.info("Checking if {} exists in DB", searchKeys);
70         try (Connection connection = dataSource.getConnection();
71              PreparedStatement preparedStmt = connection.prepareStatement(selectTableSQL)) {
72
73             try (ResultSet rs = preparedStmt.executeQuery()) {
74                 while (rs.next()) {
75                     dataExists = rs.getInt("data_exists");
76                     LOG.debug("Found {} devices matching {}", dataExists, searchKeys);
77                 }
78             }
79         } catch (SQLException e) {
80             LOG.error("Something wrong when fetching data in DB", e);
81         }
82         return dataExists != 0;
83     }
84
85   /*  public void getRoadmShelves(String nodeId, String openRoadmVersion)
86         throws InterruptedException, ExecutionException {
87
88         LOG.info("ROADMSHELVES");
89         if (openRoadmVersion.equalsIgnoreCase(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1)) {
90             inode121.getRoadmShelves(nodeId);
91         }
92         else if (openRoadmVersion.equalsIgnoreCase(StringConstants.OPENROADM_DEVICE_VERSION_2_2)) {
93             inode22.getRoadmShelves(nodeId);
94         }
95         return;
96     }
97
98     public void getCircuitPacks(String nodeId, String openRoadmVersion)
99         throws InterruptedException, ExecutionException {
100
101         LOG.info("ROADMCircuitPacks");
102         if (openRoadmVersion.equalsIgnoreCase(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1)) {
103             inode121.getCircuitPacks(nodeId);
104         }
105         else if (openRoadmVersion.equalsIgnoreCase(StringConstants.OPENROADM_DEVICE_VERSION_2_2)) {
106             inode22.getCircuitPacks(nodeId);
107         }
108         return;
109     }
110 */
111
112
113 }