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