diff options
author | Deepak S <deepak.s@linux.intel.com> | 2017-07-21 06:20:45 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-07-21 06:20:45 +0000 |
commit | 4eca0440aee462f842567d5ef8b8796c27f4dd1b (patch) | |
tree | 63935d03fb32782b0cefce01b7531cf65449d23e /VNF_Catalogue/routes | |
parent | d923180e4af2612361dd7d4bf1627092a4d552d8 (diff) | |
parent | 1f6b18a1974c1b53a079b21b6be39af86deb2432 (diff) |
Merge "VNF_Catalogue Codebase"
Diffstat (limited to 'VNF_Catalogue/routes')
-rw-r--r-- | VNF_Catalogue/routes/add_project.js | 185 | ||||
-rw-r--r-- | VNF_Catalogue/routes/add_tag.js | 60 | ||||
-rw-r--r-- | VNF_Catalogue/routes/edit_project.js | 117 | ||||
-rw-r--r-- | VNF_Catalogue/routes/index.js | 58 | ||||
-rw-r--r-- | VNF_Catalogue/routes/project_profile.js | 115 | ||||
-rw-r--r-- | VNF_Catalogue/routes/search_max.js | 36 | ||||
-rw-r--r-- | VNF_Catalogue/routes/search_projects.js | 119 | ||||
-rw-r--r-- | VNF_Catalogue/routes/search_projects_results.js | 120 | ||||
-rw-r--r-- | VNF_Catalogue/routes/search_tag.js | 41 | ||||
-rw-r--r-- | VNF_Catalogue/routes/search_vnf.js | 39 | ||||
-rw-r--r-- | VNF_Catalogue/routes/vnf_tag_association.js | 75 |
11 files changed, 965 insertions, 0 deletions
diff --git a/VNF_Catalogue/routes/add_project.js b/VNF_Catalogue/routes/add_project.js new file mode 100644 index 00000000..113d19b6 --- /dev/null +++ b/VNF_Catalogue/routes/add_project.js @@ -0,0 +1,185 @@ +/******************************************************************************* + * Copyright (c) 2017 Kumar Rishabh and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +var express = require('express'); +var router = express.Router(); +var multer = require('multer'); +var async = require('async'); + +var max_size = 1 * 1000 * 1000; // image size_limit + +var storage = multer.diskStorage({ + destination: function (req, file, callback) { + callback(null, './public/uploads'); + }, + filename: function (req, file, callback) { + console.log(file); + console.log(req.body); + callback(null, file.fieldname + '-' + Date.now() + '.png'); + } +}); + +var fileFilter = function (req, file, cb) { + if (file.mimetype !== 'image/png') { + //req.fileValidationError = 'goes wrong on the mimetype'; + cb(null, false); + } else { + cb(null, true); + } +} + +var upload = multer({ fileFilter: fileFilter, limits: { fileSize: max_size }, storage : storage}).single('file_upload'); + +var renderer = function(res, error, results) { + //res.render('project_profile', { title: 'Express', json: results }); + if(error) { + res.status(500); + res.send({'error': 'Adding VNF did not succeed'}); + return; + } else { + res.end('{"success" : "Updated Successfully", "status" : 200}'); + return; + } +} + + +var add_vnf_name_tag = function(vnf_name, vnf_id, cb) { + db_pool.getConnection(function(err, connection) { + sql_query = 'INSERT INTO tag(tag_name, is_vnf_name) values(\'' + vnf_name + '\', 1)\;SELECT LAST_INSERT_ID() tag_id'; + connection.query(sql_query, function (error, results, fields) { + tag_id = results[1][0].tag_id; + connection.release(); + if(error) { + cb(null, error, -1, -1); + } else { + cb(null, null, vnf_id, tag_id); + } + }); + }); +} + +var add_vnf_tag_relationship = function(err, vnf_id, tag_id, cb) { + console.log('here'); + console.log(err); console.log(vnf_id); console.log(tag_id); console.log(cb); + if(err) cb(null, err, -1); // err propagated from add_vnf_name_tag + db_pool.getConnection(function(err, connection) { + sql_query = 'INSERT INTO vnf_tags(tag_id, vnf_id) values(' + tag_id + ', ' + vnf_id + ')\;'; + console.log(sql_query); + connection.query(sql_query, function (error, results, fields) { + connection.release(); + console.log('here'); + if(error) { + cb(null, error, -1); + } else { + cb(null, null, results); + } + }); + }); +} + + +router.post('/', function(req, res) { + upload(req,res,function(err) { + console.log(req.body); + console.log(req.file) + if(req.file == null && req.body['file_url'] != '') { + response = 'File Upload error: wrong Filetype, only png supported as of now'; + res.status(500); + res.end(JSON.stringify({'error': response})); + return; + } + console.log(err); + if(err) { + console.log(err); + response = 'File Upload error: ' + err; + console.log(response); + //return res.end(req.fileValidationError); + res.status(500); + res.end(JSON.stringify({'error': response})); + console.log('here here here here'); + return; + } + console.log('here here'); + + console.log(req.file); + req.body['photo_url'] = (req.file) ? req.file['filename'] : 'logo.png'; + console.log(req.body); + + req.checkBody("vnf_name", "VNF Name must not be empty").notEmpty(); + req.checkBody("repo_url", "Repository URL must not be empty").notEmpty(); + req.checkBody("license", "Please select a License").notEmpty(); + req.checkBody("opnfv_indicator", "Please select an OPNFV Indicator").notEmpty(); + //req.checkBody("repo_url", "Must be a Github URL").matches('.*github\.com.*'); + + var errors = req.validationErrors(); + console.log(errors); + + var response = ''; for(var i = 0; i < errors.length; i++) { + console.log(errors[i]['msg']); + response = response + errors[i]['msg'] + '; '; + } + + if(errors) { res.status(500); + res.send({'error': response}); + return; + } + + req.body['vnf_name'] = req.body['vnf_name'].toLowerCase(); + + var vnf_details = req.body; + delete vnf_details.file_url; + + db_pool.getConnection(function(err, connection) { + // Use the connection + + sql_query = 'INSERT INTO photo(photo_url) values(\'' + req.body['photo_url'] + '\')\;SELECT LAST_INSERT_ID() photo_id'; + // TODO look above query prone to sql_injections + + console.log(sql_query); + connection.query(sql_query, function (error, results, fields) { + console.log('hola'); + console.log(results[1][0].photo_id); + //connection.query(sql_query, vnf_details, function (error, results, fields) { + delete vnf_details.photo_url; + vnf_details['photo_id'] = results[1][0].photo_id; + sql_query = 'INSERT INTO vnf SET ?; select last_insert_id() vnf_id;' + + connection.query(sql_query, vnf_details, function (error, results, fields) { + // And done with the connection. + connection.release(); + //if (error) throw error; + if(error) { + res.status(500); + res.send({'error': 'Adding VNF did not succeed'}); + return; + } else { + + console.log(results); + console.log(results[1][0].vnf_id); + vnf_id = results[1][0].vnf_id; + async.waterfall([ + async.apply(add_vnf_name_tag, vnf_details['vnf_name'], vnf_id), + add_vnf_tag_relationship, + ], renderer.bind(null, res)); + + // Handle error after the release. + //res.end('{"success" : "Updated Successfully", "status" : 200}'); + //return; + } + // Don't use the connection here, it has been returned to the pool. + }); + }); + }); + + + }); + +}); + +module.exports = router; diff --git a/VNF_Catalogue/routes/add_tag.js b/VNF_Catalogue/routes/add_tag.js new file mode 100644 index 00000000..574e67e6 --- /dev/null +++ b/VNF_Catalogue/routes/add_tag.js @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2017 Kumar Rishabh and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +var express = require('express'); +var router = express.Router(); + +router.post('/', function(req, res) { + console.log(req.body); + req.checkBody("tag_name", "TAG Name must not be empty").notEmpty(); + + var errors = req.validationErrors(); + console.log(errors); + + var response = ''; for(var i = 0; i < errors.length; i++) { + console.log(errors[i]['msg']); + response = response + errors[i]['msg'] + '; '; + } + + if(errors) { res.status(500); + res.send({'error': response}); + return; + } + + req.body['tag_name'] = req.body['tag_name'].toLowerCase(); + var tag_details = req.body; + + db_pool.getConnection(function(err, connection) { + // Use the connection + sql_query = 'INSERT INTO tag SET ?' + connection.query(sql_query, tag_details, function (error, results, fields) { + // And done with the connection. + // Handle error after the release. + console.log('here'); + if(error) { + res.end('{"error" : "Adding tag did not succeed", "status" : 500}'); + connection.release(); + return; + } else { + res.end('{"success" : "Updated Successfully", "status" : 200}'); + connection.release(); + return; + } + //if (error) throw error; + // Don't use the connection here, it has been returned to the pool. + }); + }); + + + //res.end('{"success" : "Updated Successfully", "status" : 200}'); + //return; + +}); + +module.exports = router; diff --git a/VNF_Catalogue/routes/edit_project.js b/VNF_Catalogue/routes/edit_project.js new file mode 100644 index 00000000..75e586f0 --- /dev/null +++ b/VNF_Catalogue/routes/edit_project.js @@ -0,0 +1,117 @@ +/******************************************************************************* + * Copyright (c) 2017 Kumar Rishabh and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +var express = require('express'); +var router = express.Router(); +var multer = require('multer'); + +var max_size = 1 * 1000 * 1000; // image size_limit + +var storage = multer.diskStorage({ + destination: function (req, file, callback) { + callback(null, './public/uploads'); + }, + filename: function (req, file, callback) { + console.log(file); + console.log(req.body); + callback(null, file.fieldname + '-' + Date.now() + '.png'); + } +}); + +var fileFilter = function (req, file, cb) { + if (file.mimetype !== 'image/png') { + //req.fileValidationError = 'goes wrong on the mimetype'; + cb(null, false); + } else { + cb(null, true); + } +} + +var upload = multer({ fileFilter: fileFilter, limits: { fileSize: max_size }, storage : storage}).single('file_upload'); + + +router.post('/', function(req, res) { + upload(req,res,function(err) { + console.log(req.body); + console.log(req.file) + if(req.file == null && req.body['file_url'] != '') { + response = 'File Upload error: wrong Filetype, only png supported as of now'; + res.status(500); + res.end(JSON.stringify({'error': response})); + + } + if(err) { + console.log(err); + response = 'File Upload error: ' + err; + console.log(response); + //return res.end(req.fileValidationError); + res.status(500); + res.end(JSON.stringify({'error': response})); + return; + } + + console.log(req.file); + req.body['photo_url'] = (req.file) ? req.file['filename'] : 'logo.png'; + console.log(req.body); + + req.checkBody("vnf_name", "VNF Name must not be empty").notEmpty(); + req.checkBody("repo_url", "Repository URL must not be empty").notEmpty(); + req.checkBody("license", "Please select a License").notEmpty(); + req.checkBody("opnfv_indicator", "Please select an OPNFV Indicator").notEmpty(); + req.checkBody("repo_url", "Must be a Github URL").matches('.*github\.com.*'); + + var errors = req.validationErrors(); + console.log(errors); + + var response = ''; for(var i = 0; i < errors.length; i++) { + console.log(errors[i]['msg']); + response = response + errors[i]['msg'] + '; '; + } + + if(errors) { res.status(500); + res.send({'error': response}); + return; + } + + var vnf_details = req.body; + delete vnf_details.file_url; + + db_pool.getConnection(function(err, connection) { + // Use the connection + + sql_query = 'INSERT INTO photo(photo_url) values(\'' + req.body['photo_url'] + '\')\;SELECT LAST_INSERT_ID() photo_id'; + // TODO look above query prone to sql_injections + + console.log(sql_query); + connection.query(sql_query, function (error, results, fields) { + console.log('hola'); + console.log(results[1][0].photo_id); + //connection.query(sql_query, vnf_details, function (error, results, fields) { + delete vnf_details.photo_url; + vnf_details['photo_id'] = results[1][0].photo_id; + sql_query = 'INSERT INTO vnf SET ?' + connection.query(sql_query, vnf_details, function (error, results, fields) { + // And done with the connection. + connection.release(); + if (error) throw error; + + // Handle error after the release. + res.end('{"success" : "Updated Successfully", "status" : 200}'); + return; + // Don't use the connection here, it has been returned to the pool. + }); + }); + }); + + + }); + +}); + +module.exports = router; diff --git a/VNF_Catalogue/routes/index.js b/VNF_Catalogue/routes/index.js new file mode 100644 index 00000000..e22f7c29 --- /dev/null +++ b/VNF_Catalogue/routes/index.js @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2017 Kumar Rishabh and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +var express = require('express'); +var router = express.Router(); +var async = require('async'); + +var renderer = function(res, err, results) { + console.log(results); + res.render('index', { title: 'Express', json: results }); +} + +var get_images = function(result, callback) { + db_pool.getConnection(function(err, connection) { + sql_query = 'select photo_url from photo where photo_id = ' + result['photo_id']; + // TODO find why it works here and not when declared outside the method + console.log(sql_query); + connection.query(sql_query, function (error, results, fields) { + console.log(results[0].photo_url); + //result['photo_url'] = results[0].photo_url; + connection.release(); + if (error) { + result['photo_url'] = false; + //throw error; + } else { + result['photo_url'] = results[0].photo_url; + } + callback(null, result); + //if (error) throw error; + }); + }); +} + +/* GET VNF_Catalogue Home Page. */ +router.get('/', function(req, res) { + db_pool.getConnection(function(err, connection) { + sql_query = 'select * from vnf order by lines_of_code desc limit 8'; + // TODO find why it works and not above + connection.query(sql_query, function (error, results, fields) { + //console.log(results); + connection.release(); + if (error) { + res.render('index', { title: 'Express', json: false}); + } else { + async.map(results, get_images, renderer.bind(null, res)); + //res.render('index', { title: 'Express', json: results}); + } + }); + }); +}); + +module.exports = router; diff --git a/VNF_Catalogue/routes/project_profile.js b/VNF_Catalogue/routes/project_profile.js new file mode 100644 index 00000000..20af2256 --- /dev/null +++ b/VNF_Catalogue/routes/project_profile.js @@ -0,0 +1,115 @@ +/******************************************************************************* + * Copyright (c) 2017 Kumar Rishabh and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +var express = require('express'); +var router = express.Router(); +var async = require('async'); + + +var renderer = function(res, err, results) { + console.log(results); + res.render('project_profile', { title: 'Express', json: results }); +} + +var renderer_post = function(res, err, results) { + console.log(results); + res.end(JSON.stringify(results)); +} + +var get_tags = function(result, callback) { + db_pool.getConnection(function(err, connection) { + sql_query = 'select tag_name from tag where tag_id in (select tag_id from vnf_tags where vnf_id = ' + result['vnf_id'] + ') limit 5'; + // TODO find why it works and not above + connection.query(sql_query, function (error, results, fields) { + console.log(results); + result['tags'] = results; + connection.release(); + if (error) { + result['tags'] = {}; + //throw error; + } else { + result['tags'] = results; + } + callback(null, result); + //if (error) throw error; + }); + }); +} + + +var get_images = function(result, callback) { + db_pool.getConnection(function(err, connection) { + sql_query = 'select photo_url from photo where photo_id = ' + result['photo_id']; + // TODO find why it works here and not when declared outside the method + console.log(sql_query); + connection.query(sql_query, function (error, results, fields) { + console.log(results[0].photo_url); + //result['photo_url'] = results[0].photo_url; + connection.release(); + if (error) { + result['photo_url'] = false; + //throw error; + } else { + result['photo_url'] = results[0].photo_url; + } + callback(null, result); + //if (error) throw error; + }); + }); +} + +var sql_data = function(vnf_id, renderer, res) { + db_pool.getConnection(function(err, connection) { + sql_query = 'select * from vnf where vnf_id = '+ vnf_id; + connection.query(sql_query, function (error, results, fields) { + console.log(results); + connection.release(); + if (error) { + console.log('connection error occurred'); + } else { + async.map(results, get_images, function(error, results) { + async.map(results, get_tags, renderer.bind(null, res)); + }); + } + //connection.release(); + //if (error) throw error; + }); + }); + +} + +router.get('/', function(req, res) { + console.log(typeof(req.param('vnf_id'))); + var vnf_id = req.param('vnf_id'); + + if(vnf_id) { + //tags = tags.toLowerCase().split(/[ ,]+/); + //console.log(tags); + sql_data(vnf_id, renderer, res); + } else { + res.render('project_profile', { title: 'Express', json: false}); + } +}); + +router.post('/', function(req, res) { + console.log(typeof(req.param('vnf_id'))); + var vnf_id = req.param('vnf_id'); + + if(vnf_id) { + //tags = tags.toLowerCase().split(/[ ,]+/); + //console.log(tags); + sql_data(vnf_id, renderer_post, res); + } else { + res.end('{"error" : "VNF Project could not get loaded", "status" : 500}'); + + } +}); + + +module.exports = router; diff --git a/VNF_Catalogue/routes/search_max.js b/VNF_Catalogue/routes/search_max.js new file mode 100644 index 00000000..5fdca19e --- /dev/null +++ b/VNF_Catalogue/routes/search_max.js @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2017 Kumar Rishabh and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +var express = require('express'); +var router = express.Router(); + +/* Post Controller for Tag autocomplete form */ +router.post('/', function(req, res) { + console.log('here here here here'); + order_key = req.param('order_key'); + db_pool.getConnection(function(err, connection) { + + sql_query = 'select * from vnf order by ' + order_key + ' desc limit 8'; + // TODO find why it works and not above + console.log(sql_query); + connection.query(sql_query, function (error, results, fields) { + console.log(results); + + console.log(results); + connection.release(); + if (error) { + res.end(JSON.stringify({})); + } else { + res.end(JSON.stringify(results)); + } + }); + }); +}); + +module.exports = router; diff --git a/VNF_Catalogue/routes/search_projects.js b/VNF_Catalogue/routes/search_projects.js new file mode 100644 index 00000000..71a6dfa7 --- /dev/null +++ b/VNF_Catalogue/routes/search_projects.js @@ -0,0 +1,119 @@ +/******************************************************************************* + * Copyright (c) 2017 Kumar Rishabh and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +var express = require('express'); +var router = express.Router(); +var async = require('async'); + + +var renderer = function(res, err, results) { + //console.log(results); + //console.log(results.length); + if(results.length >= 1) + res.render('search_projects', { title: 'Express', json: results }); + else + res.render('search_projects', { title: 'Express', json: false }); +} + +var get_tags = function(result, callback) { + db_pool.getConnection(function(err, connection) { + sql_query = 'select tag_name from tag where tag_id in (select tag_id from vnf_tags where vnf_id = ' + result['vnf_id'] + ') and is_vnf_name = 0 limit 5'; + // TODO find why it works and not above + connection.query(sql_query, function (error, results, fields) { + //console.log(results); + connection.release(); + if (error) { + result['tags'] = false; + //throw error; + } else { + result['tags'] = results; + } + callback(null, result); + }); + }); +} + + +var get_images = function(result, callback) { + db_pool.getConnection(function(err, connection) { + sql_query = 'select photo_url from photo where photo_id = ' + result['photo_id']; + // TODO find why it works here and not when declared outside the method + //console.log(sql_query); + connection.query(sql_query, function (error, results, fields) { + //console.log(results[0].photo_url); + //callback(null, result); + connection.release(); + if (error) { + result['photo_url'] = false; + //throw error; + } else { + result['photo_url'] = results[0].photo_url; + } + callback(null, result); + + }); + }); +} + +var sql_data = function(tags, renderer, res) { + var tag_array = "\'" + tags.map(function (item) { return item; }).join("\',\'") + "\'"; + console.log(tag_array); + var condition = ''; + db_pool.getConnection(function(err, connection) { + sql_query = 'select tag_id from tag where tag_name in (' + tag_array + ')'; + connection.query(sql_query, function (error, results, fields) { + console.log('tag_id'); + console.log(results); + condition = 'SELECT * FROM vnf as v'; + orig_condition = condition; + for (var i in results) { + condition += (i == 0) ? ' WHERE ' : ' AND '; + condition += 'v.vnf_id IN (SELECT vnf_id from vnf_tags where tag_id = ' + results[i]['tag_id'] + ')'; + } + if (condition === orig_condition) + condition += ' WHERE v.vnf_id = -1'; + condition += ' limit 5'; + console.log(condition); + + connection.query(condition, function (error, results, fields) { + //console.log(results); + connection.release(); + if (error) { + //throw error; + console.log('connection error occurred'); + } else { + async.map(results, get_images, function(error, results) { + async.map(results, get_tags, renderer.bind(null, res)); + }); + } + }); + + //connection.release(); + //if (error) throw error; + }); + }); + +} + +router.get('/', function(req, res) { + + console.log(typeof(req.param('tags'))); + var tags = req.param('tags'); + + if(tags) { + tags = tags.toLowerCase().split(/[ ,]+/); + console.log(tags); + sql_data(tags, renderer, res); + } else { + res.render('search_projects', { title: 'Express', json: false}); + } + +}); + +module.exports = router; diff --git a/VNF_Catalogue/routes/search_projects_results.js b/VNF_Catalogue/routes/search_projects_results.js new file mode 100644 index 00000000..dd5423b6 --- /dev/null +++ b/VNF_Catalogue/routes/search_projects_results.js @@ -0,0 +1,120 @@ +/******************************************************************************* + * Copyright (c) 2017 Kumar Rishabh and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +var express = require('express'); +var router = express.Router(); +var async = require('async'); + + +var renderer = function(res, err, results) { + //console.log(results); + //console.log(results.length); + if(results.length >= 1) + res.end(JSON.stringify(results)); + else + res.end(JSON.stringify({})); +} + +var get_tags = function(result, callback) { + db_pool.getConnection(function(err, connection) { + sql_query = 'select tag_name from tag where tag_id in (select tag_id from vnf_tags where vnf_id = ' + result['vnf_id'] + ') and is_vnf_name = 0 limit 5'; + // TODO find why it works and not above + connection.query(sql_query, function (error, results, fields) { + //console.log(results); + connection.release(); + if (error) { + result['tags'] = false; + //throw error; + } else { + result['tags'] = results; + } + callback(null, result); + }); + }); +} + + +var get_images = function(result, callback) { + db_pool.getConnection(function(err, connection) { + sql_query = 'select photo_url from photo where photo_id = ' + result['photo_id']; + // TODO find why it works here and not when declared outside the method + //console.log(sql_query); + connection.query(sql_query, function (error, results, fields) { + //console.log(results[0].photo_url); + //callback(null, result); + connection.release(); + if (error) { + result['photo_url'] = false; + //throw error; + } else { + result['photo_url'] = results[0].photo_url; + } + callback(null, result); + + }); + }); +} + +var sql_data = function(tags, previous_elements, renderer, res) { + var tag_array = "\'" + tags.map(function (item) { return item; }).join("\',\'") + "\'"; + console.log(tag_array); + var condition = ''; + db_pool.getConnection(function(err, connection) { + sql_query = 'select tag_id from tag where tag_name in (' + tag_array + ')'; + connection.query(sql_query, function (error, results, fields) { + console.log('tag_id'); + console.log(results); + condition = 'SELECT * FROM vnf as v'; + orig_condition = condition; + for (var i in results) { + condition += (i == 0) ? ' WHERE ' : ' AND '; + condition += 'v.vnf_id IN (SELECT vnf_id from vnf_tags where tag_id = ' + results[i]['tag_id'] + ')'; + } + if (condition === orig_condition) + condition += ' WHERE v.vnf_id = -1' + condition += ' limit ' + previous_elements.toString() + ', 5'; + console.log(condition); + + connection.query(condition, function (error, results, fields) { + //console.log(results); + connection.release(); + if (error) { + //throw error; + console.log('connection error occurred'); + } else { + async.map(results, get_images, function(error, results) { + async.map(results, get_tags, renderer.bind(null, res)); + }); + } + }); + + //connection.release(); + //if (error) throw error; + }); + }); + +} + +router.post('/', function(req, res) { + console.log(typeof(req.param('tags'))); + var tags = req.param('tags'); + var previous_elements = (parseInt(req.param('page')) - 1) * 5; + console.log(previous_elements); + + if(tags) { + tags = tags.toLowerCase().split(/[ ,]+/); + console.log(tags); + sql_data(tags, previous_elements, renderer, res); + } else { + res.render('search_projects', { title: 'Express', json: false}); + } + +}); + +module.exports = router; diff --git a/VNF_Catalogue/routes/search_tag.js b/VNF_Catalogue/routes/search_tag.js new file mode 100644 index 00000000..a3a4b946 --- /dev/null +++ b/VNF_Catalogue/routes/search_tag.js @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2017 Kumar Rishabh and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +var express = require('express'); +var router = express.Router(); + +/* Post Controller for Tag autocomplete form */ +router.get('/', function(req, res) { + tag_partial = req.param('key'); + db_pool.getConnection(function(err, connection) { + + sql_query = 'select tag_name from tag where tag_name like "%'+ tag_partial + '%" and is_vnf_name = false limit 5'; + // TODO find why it works and not above + console.log(sql_query); + connection.query(sql_query, function (error, results, fields) { + console.log(results); + + if(results == null) { + connection.release(); + res.end(JSON.stringify({})); + } else { + var data=[]; + for(i = 0; i < results.length; i++) { + data.push(results[i].tag_name.replace(/\r?\n|\r/g, '')); + } + console.log(results); + connection.release(); + res.end(JSON.stringify(results)); + } + //if (error) throw error; + }); + }); +}); + +module.exports = router; diff --git a/VNF_Catalogue/routes/search_vnf.js b/VNF_Catalogue/routes/search_vnf.js new file mode 100644 index 00000000..da524f58 --- /dev/null +++ b/VNF_Catalogue/routes/search_vnf.js @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2017 Kumar Rishabh and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +var express = require('express'); +var router = express.Router(); + +/* Post Controller for Search Vnf autocomplete form */ +router.get('/', function(req, res) { + tag_partial = req.param('key'); + db_pool.getConnection(function(err, connection) { + + sql_query = 'select vnf_name from vnf where vnf_name like "%'+ tag_partial + '%" limit 5'; + // TODO find why it works and not above + connection.query(sql_query, function (error, results, fields) { + console.log(results); + if(results == null) { + connection.release(); + res.end(JSON.stringify({})); + } else { + var data=[]; + for(i = 0; i < results.length; i++) { + data.push(results[i].vnf_name.replace(/\r?\n|\r/g, '')); + } + console.log(results); + connection.release(); + res.end(JSON.stringify(results)); + } + //if (error) throw error; + }); + }); +}); + +module.exports = router; diff --git a/VNF_Catalogue/routes/vnf_tag_association.js b/VNF_Catalogue/routes/vnf_tag_association.js new file mode 100644 index 00000000..24ad5995 --- /dev/null +++ b/VNF_Catalogue/routes/vnf_tag_association.js @@ -0,0 +1,75 @@ +/******************************************************************************* + * Copyright (c) 2017 Kumar Rishabh and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +var express = require('express'); +var router = express.Router(); + +/* Post controller for VNF_TAG Association */ +router.post('/', function(req, res) { + req.checkBody("tag_name", "TAG Name must not be empty").notEmpty(); + req.checkBody("vnf_name", "VNF Name must not be empty").notEmpty(); + + var errors = req.validationErrors(); + console.log(errors); + + var response = ''; for(var i = 0; i < errors.length; i++) { + console.log(errors[i]['msg']); + response = response + errors[i]['msg'] + '; '; + } + + if(errors) { res.status(500); + res.send({'error': response}); + return; + } + + var tag_name = req.param('tag_name').toLowerCase(); + var vnf_name = req.param('vnf_name').toLowerCase(); + + db_pool.getConnection(function(err, connection) { + sql_query = 'select vnf_tag_id from vnf_tags where vnf_id = (select vnf_id from vnf where vnf_name = \'' + vnf_name + '\' limit 1) and tag_id = (select tag_id from tag where tag_name = \'' + tag_name + '\' and is_vnf_name = 0 limit 1)'; + console.log(sql_query); + connection.query(sql_query, function (error, results, fields) { + console.log(results); + if (results.length >= 1) { + connection.release(); + res.status(500); + res.send('{"error" : "Tag VNF connection already exists...."}'); + return; + } else { + // Use the connection + //sql_query = 'INSERT INTO tag SET ?' + sql_query = 'insert into vnf_tags(vnf_id, tag_id) values ((select vnf_id from vnf where vnf_name = \'' + vnf_name + '\' limit 1), (select tag_id from tag where tag_name = \'' + tag_name + '\' and is_vnf_name = 0 limit 1))'; + console.log(sql_query); + connection.query(sql_query, function (error, results, fields) { + // And done with the connection. + + connection.release(); + + // Handle error after the release. + if (error) { + console.log(error); + res.status(500); + res.send({'error' : 'Adding vnf tag association did not succeed, Check names(rely on autocomplete)'}); + return; + } else { + res.end('{"success" : "Updated Successfully", "status" : 200}'); + return; + } + // Don't use the connection here, it has been returned to the pool. + }); + } + }); + + }); + + //res.end('{"success" : "Updated Successfully", "status" : 200}'); + //res.render('vnf_tag_association', { title: 'Express' }); +}); + +module.exports = router; |