blob: 0122d07d2cd8c28848636ba7cb4cf41bab788f3b [file] [log] [blame]
susmit shannigrahi21009e32015-03-26 17:00:32 -06001#!/usr/bin/env python3
2# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
3#
4# Copyright (c) 2015, Colorado State University.
5#
6# This file is part of ndn-atmos.
7#
8# ndn-atmos is free software: you can redistribute it and/or modify it under the
9# terms of the GNU Lesser General Public License as published by the Free Software
10# Foundation, either version 3 of the License, or (at your option) any later version.
11#
12# ndn-atmos is distributed in the hope that it will be useful, but WITHOUT ANY
13# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15#
16# You should have received copies of the GNU General Public License and GNU Lesser
17# General Public License along with ndn-atmos, e.g., in COPYING.md file. If not, see
18# <http://www.gnu.org/licenses/>.
19#
20# See AUTHORS.md for complete list of ndn-atmos authors and contributors.
21
22'''This module is for inserting NDN names in underlying database'''
23
24import mysql.connector as sql
25from mysql.connector import errorcode
26import sys
27import getpass
28import hashlib
29from ndn_cmmap_translators import atmos_translator
30
31def createTables(cursor):
32 TABLES = {}
33 TABLES['cmip5'] = (
34 "CREATE TABLE `cmip5` ("
35 " `id` int(100) AUTO_INCREMENT NOT NULL,"
36 " `sha256` varchar(64) UNIQUE NOT NULL,"
37 " `name` varchar(1000) NOT NULL,"
38 " `activity` varchar(100) NOT NULL,"
39 " `product` varchar(100) NOT NULL,"
40 " `organization` varchar(100) NOT NULL,"
41 " `model` varchar(100) NOT NULL,"
42 " `experiment` varchar(100) NOT NULL,"
43 " `frequency` varchar(100) NOT NULL,"
44 " `modeling_realm` varchar(100) NOT NULL,"
45 " `variable_name` varchar(100) NOT NULL,"
46 " `ensemble` varchar(100) NOT NULL,"
47 " `time` varchar(100) NOT NULL,"
48 " PRIMARY KEY (`id`)"
49 ") ENGINE=InnoDB")
50
51 #check if tables exist, if not create them
52 #create tables per schema in the wiki
53 for tableName, query in TABLES.items():
54 try:
55 print("Creating table {}: ".format(tableName))
56 cursor.execute(query)
57 print("Created table {}: ".format(tableName))
58 return 0
59 except sql.Error as err:
60 if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
61 print("Table already exists: %s " %(tableName))
62 return 1
63 else:
64 print("Failed to create table: %s" %(err.msg))
65 return -1
66
67def insertNames(cursor, name):
68 if __debug__:
69 print("Name to insert %s " %(name))
70
71 #hashvalue is needed since name is too long for primary key (must be <767 bytes)
72 hashValue = hashlib.sha256(str(name).encode('utf-8')).hexdigest()
73 if __debug__:
74 print("Hash of name %s " %(hashValue))
75
76 ##NOTE:must use %s to prevent sql injection in all sql queries
77 splitName = list(filter(None, name.split("/")))
78 splitName.insert(0, hashValue)
79 splitName.insert(0, name)
80 splitName = tuple(splitName)
81 if __debug__:
82 print("Name to insert in database %s" %(splitName))
83
84 addRecord = ("INSERT INTO cmip5 "
85 "(name, sha256, activity, product, organization, model, experiment, frequency,\
86 modeling_realm, variable_name, ensemble, time) "
87 "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
88
89 try:
90 cursor.execute(addRecord, splitName)
91 print("Inserted record %s" %(name))
92 return 0
93 except sql.Error as err:
94 print("Error inserting name %s" %(err.msg))
95 return -1
96
97if __name__ == '__main__':
98 datafilePath = input("File/directory to translate: ")
99 configFilepath = input("Schema file path for the above file/directory: ")
100
101 #do the translation, the library should provide error messages, if any
102 ndnNames = atmos_translator.args_for_translation(datafilePath, configFilepath)
103 if len(ndnNames) == 0:
104 print("No name returned from the translator, exiting.")
105 sys.exit(-1)
106 if __debug__:
107 print("Returned NDN names: %s" %(ndnNames))
108
109 #open connection to db
110 dbName = input("Database to store NDN names?")
111 dbUsername = input("Database username?")
112 dbPasswd = getpass.getpass("Database password?")
113
114 try:
115 con = sql.connect(user=dbUsername, database=dbName, password=dbPasswd)
116 cursor = con.cursor()
117 if __debug__:
118 print("successfully connected to database")
119
120 #if tables do not exist, create tables
121 res = createTables(cursor)
122 if __debug__:
123 print("Return Code for create_tables: %s" %(res))
124
125 #if error, exit
126 if res == -1:
127 sys.exit(-1)
128
129 #if table already exists, or creation successful, continue
130 if res == 1 or res == 0:
131 for ndnName in ndnNames:
132 #get list of files and insert into database
133 res_insertNames = insertNames(cursor, ndnName)
134 if __debug__:
135 print("Return Code is %s for inserting name: %s" %(res_insertNames, ndnName))
136 con.commit()
137 except sql.Error as err:
138 if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
139 print("Incorrect username or password")
140 elif err.errno == errorcode.ER_BAD_DB_ERROR:
141 print("Database does not exist")
142 else:
143 print("Error connecting to Database: %s" %(err.msg))
144 finally:
145 con.close()