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