From 0ddfff32a990dda5a487229641aa189161f3d25a Mon Sep 17 00:00:00 2001 From: Kumar Rishabh Date: Wed, 15 Mar 2017 01:02:11 +0530 Subject: Add vnf, tag create & Associate Plugins Adds support for accessing database, plugin to create tags and vnfs and make association between them. Also adds autocomplete feature to vnf-tag association. Change-Id: Id55f998df68ae5e6e6fd298c6393b3500777468a Signed-off-by: Kumar Rishabh --- .../VNF_Catalogue/routes/add_project.js | 116 +++++++++++++++++++++ .../vnfcatalogue/VNF_Catalogue/routes/add_tag.js | 52 +++++++++ .../VNF_Catalogue/routes/project_profile.js | 19 ++++ .../VNF_Catalogue/routes/search_projects.js | 79 +++++++++++++- .../VNF_Catalogue/routes/search_tag.js | 36 +++++++ .../VNF_Catalogue/routes/search_vnf.js | 36 +++++++ .../VNF_Catalogue/routes/vnf_tag_association.js | 55 ++++++++++ 7 files changed, 391 insertions(+), 2 deletions(-) create mode 100644 utils/test/vnfcatalogue/VNF_Catalogue/routes/add_project.js create mode 100644 utils/test/vnfcatalogue/VNF_Catalogue/routes/add_tag.js create mode 100644 utils/test/vnfcatalogue/VNF_Catalogue/routes/project_profile.js create mode 100644 utils/test/vnfcatalogue/VNF_Catalogue/routes/search_tag.js create mode 100644 utils/test/vnfcatalogue/VNF_Catalogue/routes/search_vnf.js create mode 100644 utils/test/vnfcatalogue/VNF_Catalogue/routes/vnf_tag_association.js (limited to 'utils/test/vnfcatalogue/VNF_Catalogue/routes') diff --git a/utils/test/vnfcatalogue/VNF_Catalogue/routes/add_project.js b/utils/test/vnfcatalogue/VNF_Catalogue/routes/add_project.js new file mode 100644 index 000000000..229620d20 --- /dev/null +++ b/utils/test/vnfcatalogue/VNF_Catalogue/routes/add_project.js @@ -0,0 +1,116 @@ +/******************************************************************************* + * 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 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() + '.jpg'); + } +}); + +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, 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'; + 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.send({'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/utils/test/vnfcatalogue/VNF_Catalogue/routes/add_tag.js b/utils/test/vnfcatalogue/VNF_Catalogue/routes/add_tag.js new file mode 100644 index 000000000..511f4ccb0 --- /dev/null +++ b/utils/test/vnfcatalogue/VNF_Catalogue/routes/add_tag.js @@ -0,0 +1,52 @@ +/******************************************************************************* + * 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; + } + + 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. + res.end('{"success" : "Updated Successfully", "status" : 200}'); + return; + connection.release(); + // Handle error after the release. + 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/utils/test/vnfcatalogue/VNF_Catalogue/routes/project_profile.js b/utils/test/vnfcatalogue/VNF_Catalogue/routes/project_profile.js new file mode 100644 index 000000000..be0664276 --- /dev/null +++ b/utils/test/vnfcatalogue/VNF_Catalogue/routes/project_profile.js @@ -0,0 +1,19 @@ +/******************************************************************************* + * 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.get('/', function(req, res) { + var tags = req.param('tags'); + console.log(tags); + res.render('project_profile', { title: 'Express' }); +}); + +module.exports = router; diff --git a/utils/test/vnfcatalogue/VNF_Catalogue/routes/search_projects.js b/utils/test/vnfcatalogue/VNF_Catalogue/routes/search_projects.js index 49fceeb3c..96f68db7a 100644 --- a/utils/test/vnfcatalogue/VNF_Catalogue/routes/search_projects.js +++ b/utils/test/vnfcatalogue/VNF_Catalogue/routes/search_projects.js @@ -9,11 +9,86 @@ var express = require('express'); var router = express.Router(); +var async = require('async'); + + +var renderer = function(res, err, results) { + console.log(results); + res.render('search_projects', { title: 'Express', json: 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; + callback(null, result); + //connection.release(); + 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; + callback(null, result); + //connection.release(); + if (error) throw error; + }); + }); +} + +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) { + condition = 'SELECT * FROM vnf as v'; + 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'] + ')'; + } + + connection.query(condition, function (error, results, fields) { + console.log(results); + async.map(results, get_images, function(error, results) { + async.map(results, get_tags, renderer.bind(null, res)); + }); + //connection.release(); + if (error) throw error; + }); + + connection.release(); + if (error) throw error; + }); + }); + +} router.get('/', function(req, res) { + + console.log(typeof(req.param('tags'))); var tags = req.param('tags'); - console.log(tags); - res.render('search_projects', { title: 'Express' }); + + 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/utils/test/vnfcatalogue/VNF_Catalogue/routes/search_tag.js b/utils/test/vnfcatalogue/VNF_Catalogue/routes/search_tag.js new file mode 100644 index 000000000..cbe8caefd --- /dev/null +++ b/utils/test/vnfcatalogue/VNF_Catalogue/routes/search_tag.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.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 + '%" limit 5'; + // TODO find why it works and not above + connection.query(sql_query, function (error, results, fields) { + console.log(results); + + 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/utils/test/vnfcatalogue/VNF_Catalogue/routes/search_vnf.js b/utils/test/vnfcatalogue/VNF_Catalogue/routes/search_vnf.js new file mode 100644 index 000000000..a5cf09ca7 --- /dev/null +++ b/utils/test/vnfcatalogue/VNF_Catalogue/routes/search_vnf.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 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); + + 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/utils/test/vnfcatalogue/VNF_Catalogue/routes/vnf_tag_association.js b/utils/test/vnfcatalogue/VNF_Catalogue/routes/vnf_tag_association.js new file mode 100644 index 000000000..d1a3d726e --- /dev/null +++ b/utils/test/vnfcatalogue/VNF_Catalogue/routes/vnf_tag_association.js @@ -0,0 +1,55 @@ +/******************************************************************************* + * 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'); + var vnf_name = req.param('vnf_name'); + + db_pool.getConnection(function(err, connection) { + // 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 + '\'), (select tag_id from tag where tag_name = \'' + tag_name + '\'))'; + console.log(sql_query); + connection.query(sql_query, function (error, results, fields) { + // And done with the connection. + + connection.release(); + res.end('{"success" : "Updated Successfully", "status" : 200}'); + + // Handle error after the release. + if (error) throw error; + // 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; -- cgit 1.2.3-korg