2 * Copyright 2013 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var express = require('express');
18 var util = require('util');
19 var multer = require('multer');
20 var when = require('when');
21 var exec = require('child_process').exec;
23 var createUI = require("./ui");
24 var redNodes = require("./nodes");
25 var comms = require("./comms");
26 var storage = require("./storage");
28 var path = require("path");
34 var flowShareUsers = require("../flowShareUsers");
36 //console.dir(flowShareUsers);
38 function createServer(_server,_settings) {
42 comms.init(_server,_settings);
47 if (settings.httpAdminRoot !== false) {
50 if (!settings.disableEditor) {
51 createUI(settings,app);
54 var slaActions = require("./sla");
56 app.get("/flows",function(req,res) {
57 res.json(redNodes.getFlows());
60 app.get("/loadJSFiles",function(req,res) {
61 var appDir = path.dirname(require.main.filename);
62 var generatedJSDir=appDir + "/generatedJS";
63 var glob = require("glob")
64 glob(generatedJSDir + "/**/*.js", null, function (er, files) {
65 // files is an array of filenames.
66 // If the `nonull` option is set, and nothing
67 // was found, then files is ["**/*.js"]
68 // er is an error object or null.
71 for(var i=0;files!= null && i<files.length;i++){
72 var f = files[i].replace( new RegExp(generatedJSDir + "/", "g" ), "" );
73 console.log("loading file " + f);
75 sliValuesObj.push(require(files[i]));
76 //console.dir(sliValuesObj);
78 console.log("Error:Could not load file " + files[i]);
81 res.json({"sliValuesObj" : sliValuesObj});
85 app.get("/loadSelectedModules",function(req,res) {
86 var appDir = path.dirname(require.main.filename);
87 var userDir = appDir + "/" + settings.userDir;
88 var generatedJSDir=appDir + "/generatedJS";
90 var selectedModulesStr = req.query.selectedModules;
91 var selectedModules = [];
92 if(selectedModulesStr != undefined && selectedModulesStr != null){
93 selectedModules = selectedModulesStr.split(",");
95 console.log(selectedModules);
96 var loaded_modules = {"selected_modules" :selectedModules};
97 var file = userDir + "/selected_modules";
98 var content = "module.exports=\n" + JSON.stringify(loaded_modules);
100 fs.writeFileSync(file, content, 'utf8');
102 console.log("could not write to file " + file);
104 var sliValuesObj =[];
105 for(var i=0;selectedModules!= null && i<selectedModules.length;i++){
106 var f = generatedJSDir + "/" + selectedModules[i] + "_inputs.js";
108 delete require.cache[require.resolve(f)]
111 console.log("error deleting loaded module " + f + " from cache");
113 //console.log("loading file " + f);
115 sliValuesObj.push(require(f));
117 console.log("Error:Could not load file " + f);
120 //console.dir(sliValuesObj);
121 res.json({"sliValuesObj" : sliValuesObj});
124 app.get("/initialLoadSelectedModules",function(req,res) {
125 var appDir = path.dirname(require.main.filename);
126 var userDir = appDir + "/" + settings.userDir;
127 var generatedJSDir=appDir + "/generatedJS";
128 var file = userDir + "/selected_modules";
129 var sliValuesObj =[];
130 var selected_modules = [];
133 selectedModules = require(file);
134 selected_modules=selectedModules["selected_modules"];
135 //console.log("selected_modules are ");
136 //console.dir(selected_modules);
138 console.log("Could not load the file " + file);
140 for(var i=0;selected_modules!= null && i<selected_modules.length;i++){
141 var f = generatedJSDir + "/" + selected_modules[i] + "_inputs.js";
142 console.log("loading file " + f);
144 sliValuesObj.push(require(f));
146 console.log("Error:Could not load file " + f);
149 res.json({"sliValuesObj" : sliValuesObj});
152 app.get("/listAvailableModules",function(req,res) {
153 var appDir = path.dirname(require.main.filename);
154 var userDir = appDir + "/" + settings.userDir;
155 var generatedJSDir=appDir + "/generatedJS";
156 var glob = require("glob")
157 var file = userDir + "/selected_modules";
158 var selected_modules = [];
161 delete require.cache[require.resolve(file)]
164 console.log("error deleting loaded module " + file + " from cache");
167 selectedModules = require(file);
168 selected_modules=selectedModules["selected_modules"];
169 console.log("selected_modules are ");
170 //console.dir(selected_modules);
172 console.log("Could not load the file " + file);
174 glob(generatedJSDir + "/**/*.js", null, function (er, files) {
176 for(var i=0;files!= null && i<files.length;i++){
177 var f = files[i].replace( new RegExp(generatedJSDir + "/", "g" ), "" );
178 f = f.replace("_inputs.js","");
179 if(selected_modules != undefined && selected_modules != null && selected_modules.indexOf(f) != -1){
180 filesList.push(f + ":checked");
182 filesList.push(f + ":unchecked");
185 res.json({"files" : filesList});
189 app.get("/listSLA",function(req,res) {
190 var appDir = path.dirname(require.main.filename);
191 var userDir = appDir + "/" + settings.userDir;
192 var settingsFile = userDir + "/customSettings.js";
193 var jsonObj = require(settingsFile);
194 slaActions.listSLA(jsonObj,req,res);
197 app.get("/listCurrentDGs",function(req,res) {
198 var appDir = path.dirname(require.main.filename);
199 var userDir = appDir + "/" + settings.userDir;
200 var settingsFile = userDir + "/customSettings.js";
201 var jsonObj = require(settingsFile);
202 slaActions.listCurrentDGs(jsonObj,req,res);
205 app.get("/activateDG",function(req,res) {
206 var appDir = path.dirname(require.main.filename);
207 var userDir = appDir + "/" + settings.userDir;
208 var settingsFile = userDir + "/customSettings.js";
209 var jsonObj = require(settingsFile);
210 slaActions.activateDG(jsonObj,req,res);
213 app.get("/deActivateDG",function(req,res) {
214 var appDir = path.dirname(require.main.filename);
215 var userDir = appDir + "/" + settings.userDir;
216 var settingsFile = userDir + "/customSettings.js";
217 var jsonObj = require(settingsFile);
218 slaActions.deActivateDG(jsonObj,req,res);
221 app.get("/deleteDG",function(req,res) {
222 var appDir = path.dirname(require.main.filename);
223 var userDir = appDir + "/" + settings.userDir;
224 var settingsFile = userDir + "/customSettings.js";
225 var jsonObj = require(settingsFile);
226 slaActions.deleteDG(jsonObj,req,res);
230 app.get("/getCurrentSettings",function(req,res) {
231 var appDir = path.dirname(require.main.filename);
232 var userDir = appDir + "/" + settings.userDir;
233 //console.log("userDir:" + userDir);
234 var settingsFile = userDir + "/customSettings.js";
235 var jsonObj = require(settingsFile);
239 app.get("/getCommitsInfo", function(req,res) {
240 var appDir = path.dirname(require.main.filename);
241 var userDir = appDir + "/" + settings.userDir;
243 var filePath = req.query.filePath;
244 var fullFilePath = userDir + "/codecloud/" + filePath ;
245 //console.log("fullFilePath:" + fullFilePath);
246 var exec = require('child_process').exec;
247 var commandToExec = appDir + "/git_scripts/gitlog " + fullFilePath ;
248 console.log("commandToExec:" + commandToExec);
249 var child = exec(commandToExec ,function (error,stdout,stderr){
251 console.log("Error occured:" + error);
253 //console.log("stderr:" + stderr);
254 res.send(500,{'error':error,'stderr':stderr});
256 res.send(500,{'error':error});
258 //console.log("stdout :" + stdout);
261 console.log("stderr:" + stderr);
264 //console.log("output:" + stdout);
265 res.send(200,{'stdout':stdout,'stderr':stderr});
271 app.get("/importCodeCloudFlow",
273 var appDir = path.dirname(require.main.filename);
274 var userDir = appDir + "/" + settings.userDir;
276 var commitId = req.query.commitId;
277 var filePath = req.query.filePath;
278 var fullFilePath = userDir + "/codecloud/" + filePath ;
279 //console.log("fullFilePath:" + fullFilePath);
280 var exec = require('child_process').exec;
281 var commandToExec = appDir + "/git_scripts/gitckout " + commitId + " " + fullFilePath ;
282 console.log("commandToExec:" + commandToExec);
283 var child = exec(commandToExec ,{maxBuffer: 1024 * 1024 * 16}, function (error,stdout,stderr){
285 console.log("Error occured:" + error);
287 //console.log("stderr:" + stderr);
288 res.send(500,{'error':error,'stderr':stderr});
290 res.send(500,{'error':error});
294 console.log("stderr:" + stderr);
297 //console.log("output:" + stdout);
298 res.send(200,{'stdout':stdout,'stderr':stderr});
304 app.get("/importGitLocalFlow",
306 var appDir = path.dirname(require.main.filename);
307 var gitLocalRepository = settings.gitLocalRepository;
309 var userDir=settings.userDir;
310 var outputDir = appDir + "/" + userDir + "/orig_dgs";
311 if (!fs.existsSync(outputDir)){
312 fs.mkdirSync(outputDir);
316 var filePath = req.query.filePath;
317 //var currTabId = req.query.currTabId;
318 var fullFilePath = gitLocalRepository +"/" + filePath ;
319 //console.log("fullFilePath:" + fullFilePath);
320 var exec = require('child_process').exec;
321 var commandToExec = "cat " + fullFilePath ;
322 console.log("commandToExec:" + commandToExec);
323 var child = exec(commandToExec ,{maxBuffer: 1024 * 1024 * 16}, function (error,stdout,stderr){
325 console.log("Error occured:" + error);
327 //console.log("stderr:" + stderr);
328 res.send(500,{'error':error,'stderr':stderr});
330 res.send(500,{'error':error});
334 console.log("stderr:" + stderr);
339 var jsonStrFormatted=[];
341 jsonStrFormatted= JSON.parse(jsonStr);
344 fs.writeFileSync( outputDir + "/" +currTabId,JSON.stringify(jsonStrFormatted,null,4) );
346 //console.log("output:" + stdout);
347 res.send(200,{'stdout':stdout,'stderr':stderr});
353 app.post("/saveImportedDG",
356 var qs = require('querystring');
358 req.on('data', function (data) {
361 req.on('end', function () {
362 var appDir = path.dirname(require.main.filename);
363 var userDir=settings.userDir;
364 var outputDir = appDir + "/" + userDir + "/orig_dgs";
365 if (!fs.existsSync(outputDir)){
366 fs.mkdirSync(outputDir);
368 var post = qs.parse(body);
369 var importedNodes = post.importedNodes;
370 var currTabId = post.currTabId;
371 fs.writeFileSync( outputDir + "/" +currTabId,importedNodes );
372 res.send(200,{"output":"SUCCESS"});
376 app.post("/saveImportedDG",
379 var qs = require('querystring');
381 req.on('data', function (data) {
384 req.on('end', function () {
385 var appDir = path.dirname(require.main.filename);
386 var userDir=settings.userDir;
387 var outputDir = appDir + "/" + userDir + "/orig_dgs";
388 if (!fs.existsSync(outputDir)){
389 fs.mkdirSync(outputDir);
391 var post = qs.parse(body);
392 var importedNodes = post.importedNodes;
393 var currTabId = post.currTabId;
394 fs.writeFileSync( outputDir + "/" +currTabId,importedNodes );
395 res.send(200,{"output":"SUCCESS"});
399 app.get("/gitcheckout", function(req,res) {
400 var appDir = path.dirname(require.main.filename);
401 var gitLocalRepository = settings.gitLocalRepository;
403 var branch = req.query.branch;
404 //console.log("fullFilePath:" + fullFilePath);
405 var exec = require('child_process').exec;
406 var commandToExec = appDir + "/git_scripts/gitcheckout " + gitLocalRepository + " " + branch ;
407 console.log("commandToExec:" + commandToExec);
408 var child = exec(commandToExec ,function (error,stdout,stderr){
410 console.log("Error occured:" + error);
412 console.log("stderr:" + stderr);
413 res.json({"output":stderr});
415 res.json({"output":error});
419 console.log("stderr:" + stderr);
422 res.json({"output": stderr + " " + stdout});
428 app.get("/gitpull", function(req,res) {
429 var appDir = path.dirname(require.main.filename);
430 var gitLocalRepository = settings.gitLocalRepository;
432 var branch = req.query.branch;
433 //console.log("fullFilePath:" + fullFilePath);
434 var exec = require('child_process').exec;
435 var commandToExec = appDir + "/git_scripts/gitpull " + gitLocalRepository ;
436 console.log("commandToExec:" + commandToExec);
437 var child = exec(commandToExec ,function (error,stdout,stderr){
439 console.log("Error occured:" + error);
441 console.log("stderr:" + stderr);
442 res.json({"output":stderr});
444 res.json({"output":error});
448 console.log("stderr:" + stderr);
451 res.json({"output": stderr + " " + stdout});
457 app.get("/gitstatus", function(req,res) {
458 var appDir = path.dirname(require.main.filename);
459 var gitLocalRepository = settings.gitLocalRepository;
461 var branch = req.query.branch;
462 //console.log("fullFilePath:" + fullFilePath);
463 var exec = require('child_process').exec;
464 var commandToExec = appDir + "/git_scripts/gitstatus " + gitLocalRepository ;
465 console.log("commandToExec:" + commandToExec);
466 var child = exec(commandToExec ,function (error,stdout,stderr){
468 console.log("Error occured:" + error);
470 console.log("stderr:" + stderr);
471 res.json({"output":stderr});
473 res.json({"output":error});
477 console.log("stderr:" + stderr);
480 res.json({"output": stderr + " " + stdout});
486 app.post("/getSharedFlow",
489 var qs = require('querystring');
491 req.on('data', function (data) {
494 req.on('end', function () {
495 var post = qs.parse(body);
496 //console.log("body:" + body);
497 fs.readFile(post.filePath, 'utf8', function (err,data) {
499 return console.log(err);
504 //res.sendFile(body.filePath);
508 app.post("/downloadYang",
511 var qs = require('querystring');
513 req.on('data', function (data) {
516 req.on('end', function () {
517 var post = qs.parse(body);
518 var fileName = post.fileName;
519 var appDir = path.dirname(require.main.filename);
520 var yangDir = appDir + "/yangFiles" ;
521 var fullPathToFile = yangDir + "/" + fileName;
522 res.setHeader('Content-disposition', 'attachment; filename=' + fileName);
523 res.setHeader('Content-type', 'application/yang');
524 res.download(fullPathToFile);
528 function writeToFile(fullPathToFileName,str){
530 fs.writeFileSync(fullPathToFileName,str);
532 console.log("Error:" + e);
536 function getCurrentDate(){
538 var mm = d.getMonth() + 1;
539 var dd = d.getDate();
540 var yyyy = d.getYear() + 1900;
541 var hr = d.getHours();
542 var min = d.getMinutes();
543 var sec = d.getSeconds();
544 if(mm<10) mm = "0" + mm;
545 if(dd<10) dd = "0" + dd;
546 if(hr<10) hr = "0" + hr;
547 if(min<10) min = "0" + min;
548 if(sec<10) sec = "0" + sec;
549 var formatedValue = mm + "-" + dd + "-" + yyyy + "_" + hr + "" + min + "" + sec;
550 return formatedValue;
554 app.post("/downloadXml",
555 express.json({'limit':'16mb'}),
557 //console.log("Received request and processing:" + new Date());
558 var qs = require('querystring');
560 //var msecs1= Date.now();
561 req.on('data', function (data) {
564 req.on('end', function () {
565 var appDir = path.dirname(require.main.filename);
566 var xmlDir = appDir + "/" + settings.xmlPath;
567 //var msecs2= Date.now();
568 //console.log("Time taken to get request body:" + (msecs2 - msecs1));
569 var msecs3= Date.now();
570 var post = qs.parse(body);
571 var msecs4= Date.now();
572 //console.log("Time taken to parse body:" + (msecs4 - msecs3));
573 var xml = post['flowXml'];
574 //var pd = require('pretty-data').pd;
575 //var formatted_xml = pd.xml(xml);
576 var moduleName = post['moduleName'];
577 var methodName = post['methodName'];
578 if(moduleName == "" || methodName == ""){
579 res.send({"ERROR":"ServiceLogic Module Name and method name are required."});
581 //var formatted_date = getCurrentDate();
582 //var fileNameForServer=moduleName + "_" +methodName+ "_" + formatted_date + ".xml";
583 //var fileName=moduleName + "_method_" +methodName+ ".xml";
584 var fileName=moduleName + "_" +methodName+ ".xml";
585 var file = xmlDir + "/" + fileName;
587 //var msecs2= Date.now();
588 writeToFile(file,xml);
589 //var msecs3= Date.now();
590 //console.log("Time taken to write File:" + (msecs3 - msecs2));
591 res.setHeader('Content-disposition', 'attachment; filename=' + fileName);
592 res.setHeader('Content-type', 'text/xml');
594 //console.log("Response sent:" + new Date());
599 app.post("/downloadJson",
600 express.json({'limit':'16mb'}),
602 var appDir = path.dirname(require.main.filename);
603 var sharedDir = appDir + "/" + settings.sharedDir;
604 var qs = require('querystring');
606 req.on('data', function (data) {
609 req.on('end', function () {
610 var post = qs.parse(body);
611 var jsonStr = post['flowJson'];
612 var moduleName = post['moduleName'];
613 var methodName = post['methodName'];
614 //console.log("jsonStr:" + jsonStr);
615 if(moduleName == "" || methodName == ""){
616 res.send({"ERROR":"ServiceLogic Module Name and method name are required."});
618 var formatted_date = getCurrentDate();
619 //console.log("moduleName:" + moduleName);
620 //console.log("methodName:" + methodName);
622 //var fileName=moduleName + "_method_" +methodName + ".json";
623 //var renameOldfileTo=moduleName + "_method_" +methodName+ "_" + formatted_date + ".json";
624 var fileName=moduleName + "_" +methodName + ".json";
625 var renameOldfileTo=moduleName + "_" +methodName+ "_" + formatted_date + ".json";
626 var file = sharedDir + "/" + fileName;
627 //console.log("fileName:" + fileName);
628 var renameFilePath = sharedDir + "/backups/" + renameOldfileTo;
629 //console.log("localfile:" + localfile);
630 if (fs.existsSync(file)) {
631 fs.rename(file,renameFilePath, function (err) {
633 console.log('Error :' + err);
635 //write the newer version
636 writeToFile(file,jsonStr);
637 res.setHeader('Content-disposition', 'attachment; filename=' + fileName);
638 res.setHeader('Content-type', 'application/json');
639 //res.download(file);
643 //write the newer version
644 writeToFile(file,jsonStr);
645 res.setHeader('Content-disposition', 'attachment; filename=' + fileName);
646 res.setHeader('Content-type', 'application/json');
647 //res.download(file);
655 express.json({'limit':'16mb'}),
657 //console.log("Processing Request");
658 var flows = req.body;
659 redNodes.setFlows(flows).then(function() {
661 }).otherwise(function(err) {
662 util.log("[red] Error saving flows : "+err);
663 res.send(500,err.message);
666 function(error,req,res,next) {
667 res.send(400,"Invalid Flow. Error " + error);
671 app.get("/nodes",function(req,res) {
672 if (req.get("accept") == "application/json") {
673 res.json(redNodes.getNodeList());
675 res.send(redNodes.getNodeConfigs());
682 if (!settings.available()) {
683 res.send(400,new Error("Settings unavailable").toString());
689 promise = redNodes.addNode(node.file).then(reportAddedModules);
690 } else if (node.module) {
691 var module = redNodes.getNodeModuleInfo(node.module);
693 res.send(400,"Module already loaded");
696 promise = installModule(node.module);
698 res.send(400,"Invalid request");
701 promise.then(function(info) {
703 }).otherwise(function(err) {
704 if (err.code === 404) {
707 res.send(400,err.toString());
711 function(err,req,res,next) {
712 console.log(err.toString());
717 app.delete("/nodes/:id",
719 if (!settings.available()) {
720 res.send(400,new Error("Settings unavailable").toString());
723 var id = req.params.id;
724 var removedNodes = [];
726 var node = redNodes.getNodeInfo(id);
729 var module = redNodes.getNodeModuleInfo(id);
734 promise = uninstallModule(id);
737 promise = when.resolve([redNodes.removeNode(id)]).then(reportRemovedModules);
740 promise.then(function(removedNodes) {
741 res.json(removedNodes);
742 }).otherwise(function(err) {
743 console.log(err.stack);
744 res.send(400,err.toString());
747 res.send(400,err.toString());
750 function(err,req,res,next) {
755 app.get("/nodes/:id", function(req,res) {
756 var id = req.params.id;
758 if (req.get("accept") == "application/json") {
759 result = redNodes.getNodeInfo(id);
761 result = redNodes.getNodeConfig(id);
770 app.put("/nodes/:id",
773 if (!settings.available()) {
774 res.send(400,new Error("Settings unavailable").toString());
778 if (!body.hasOwnProperty("enabled")) {
779 res.send(400,"Invalid request");
784 var id = req.params.id;
785 var node = redNodes.getNodeInfo(id);
788 } else if (!node.err && node.enabled === body.enabled) {
792 info = redNodes.enableNode(id);
794 info = redNodes.disableNode(id);
796 if (info.enabled == body.enabled && !info.err) {
797 comms.publish("node/"+(body.enabled?"enabled":"disabled"),info,false);
798 util.log("[red] "+(body.enabled?"Enabled":"Disabled")+" node types:");
799 for (var i=0;i<info.types.length;i++) {
800 util.log("[red] - "+info.types[i]);
802 } else if (body.enabled && info.err) {
803 util.log("[red] Failed to enable node:");
804 util.log("[red] - "+info.name+" : "+info.err);
809 res.send(400,err.toString());
813 app.get("/getCodeCloudFlows",function(req,res) {
814 var userDir=settings.userDir;
815 var codeCloudDir=userDir + "/codecloud";
816 var glob = require("glob")
817 glob(codeCloudDir + "/**/*.json", null, function (er, files) {
818 // files is an array of filenames.
819 // If the `nonull` option is set, and nothing
820 // was found, then files is ["**/*.js"]
821 // er is an error object or null.
822 //console.dir(files);
824 for(var i=0;files!= null && i<files.length;i++){
825 var f = files[i].replace( new RegExp(codeCloudDir + "/", "g" ), "" );
829 res.json({"files" : filesList});
833 app.get("/getCurrentGitBranch",function(req,res) {
834 var appDir = path.dirname(require.main.filename);
835 var userDir=settings.userDir;
836 var settingsFile = appDir + "/" + userDir + "/customSettings.js";
837 //console.log("settingsFile:" + settingsFile);
838 var jsonObj = require(settingsFile);
839 var gitLocalRepository=jsonObj.gitLocalRepository;
840 if(gitLocalRepository == undefined || gitLocalRepository == null || gitLocalRepository == ''){
841 res.json({"output" : "GIT_LOCAL_REPOSITORY_NOT_SET"});
844 var exec = require('child_process').exec;
845 var commandToExec = appDir + "/git_scripts/gitcurbranch " + gitLocalRepository ;
846 console.log("commandToExec:" + commandToExec);
847 var child = exec(commandToExec ,function (error,stdout,stderr){
849 console.log("Error occured:" + error);
851 console.log("stderr:" + stderr);
852 res.json({"output":stderr});
854 res.json({"output":error});
858 console.log("stderr:" + stderr);
861 res.json({"output":stdout});
868 app.get("/getGitLocalFlows",function(req,res) {
869 var appDir = path.dirname(require.main.filename);
870 var userDir=settings.userDir;
871 var settingsFile = appDir + "/" + userDir + "/customSettings.js";
872 //console.log("settingsFile:" + settingsFile);
873 var jsonObj = require(settingsFile);
874 var performGitPull = jsonObj.performGitPull;
875 if(performGitPull == undefined || performGitPull == null) {
878 var gitLocalRepository=jsonObj.gitLocalRepository;
879 if(gitLocalRepository == undefined || gitLocalRepository == null || gitLocalRepository == ''){
880 res.json({"files" : ["GIT_LOCAL_REPOSITORY_NOT_SET"]});
885 if(performGitPull == "Y"){
886 var exec = require('child_process').exec;
887 var commandToExec = appDir + "/git_scripts/gitpull " + gitLocalRepository ;
888 console.log("commandToExec:" + commandToExec);
889 var child = exec(commandToExec ,function (error,stdout,stderr){
891 console.log("Error occured:" + error);
893 console.log("stderr:" + stderr);
894 res.json({"files":[]});
896 res.json({"files":[]});
900 console.log("stderr:" + stderr);
903 var glob = require("glob")
904 glob(gitLocalRepository + "/**/*.json", null, function (er, files) {
905 // files is an array of filenames.
906 // If the `nonull` option is set, and nothing
907 // was found, then files is ["**/*.js"]
908 // er is an error object or null.
909 //console.dir(files);
911 for(var i=0;files!= null && i<files.length;i++){
912 var f = files[i].replace( new RegExp(gitLocalRepository + "/", "g" ), "" );
916 res.json({"files" : filesList});
921 }else{//git pull not requested
922 var glob = require("glob")
923 glob(gitLocalRepository + "/**/*.json", null, function (er, files) {
924 // files is an array of filenames.
925 // If the `nonull` option is set, and nothing
926 // was found, then files is ["**/*.js"]
927 // er is an error object or null.
928 //console.dir(files);
930 for(var i=0;files!= null && i<files.length;i++){
931 var f = files[i].replace( new RegExp(gitLocalRepository + "/", "g" ), "" );
935 res.json({"files" : filesList});
941 app.get("/flowShareUsers",function(req,res) {
942 res.json(flowShareUsers);
944 app.get("/getRelease",function(req,res) {
945 var userDir = settings.userDir;
946 //var release = userDir.replace(/releases/g,"release");
947 res.json({"release" : userDir});
949 app.get("/readFile",function(req,res) {
950 var userDir=settings.userDir;
951 var filePath = userDir + "/" + req.query.filePath;
952 var buf = fs.readFileSync(filePath, "utf8");
953 res.json({"output" :buf });
955 app.post("/getFiles/:id",function(req,res) {
956 var id = req.params.id;
957 //console.log("id:" + id);
958 var userDir=settings.userDir;
959 var flowDir= userDir + "/../" + id + "/flows/shared";
960 //console.log("flowDir:" + flowDir);
961 fs.readdir(flowDir,function(err, files){
963 res.json({"files": []});
965 var onlyFilesArr =[];
966 if(files != null && files.length>0){
967 files.sort(function(a,b){
968 //console.log("file1:" + a);
969 //console.log("file2:" + b);
970 var fileStat1=fs.statSync(flowDir+ "/" + a);
971 var fileStat2=fs.statSync(flowDir+ "/" + b);
972 if(fileStat1.mtime > fileStat2.mtime){
974 }else if(fileStat1.mtime < fileStat2.mtime){
980 for(var i=0;i<files.length;i++){
981 var fileStat=fs.statSync(flowDir+ "/" + files[i]);
982 if(fileStat.isFile()){
983 onlyFilesArr.push({"filePath":flowDir+ "/" + files[i],"name":files[i]});
986 res.json(onlyFilesArr);
988 res.json({"files": []});
994 app.post("/updateConfiguration",
997 var qs = require('querystring');
998 //console.log("Received the request:");
1000 req.on('data', function (data) {
1003 req.on('end',function(){
1004 var post = qs.parse(body);
1005 var dbHost = post["dbHost"];
1006 var dbPort = post["dbPort"];
1007 var dbName = post["dbName"];
1008 var dbUser = post["dbUser"];
1009 var dbPassword = post["dbPassword"];
1010 var gitLocalRepository = post["gitLocalRepository"];
1011 var performGitPull = post["performGitPull"];
1012 var restConfUrl = post["restConfUrl"];
1013 var restConfUser = post["restConfUser"];
1014 var restConfPassword = post["restConfPassword"];
1015 var emailAddress = post["emailAddress"];
1016 var formatXML = post["formatXML"];
1017 var formatJSON = post["formatJSON"];
1018 var appDir = path.dirname(require.main.filename);
1019 var userDir = appDir + "/" + settings.userDir;
1020 console.log("userDir:" + userDir);
1022 var settingsFile = userDir + "/customSettings.js";
1023 var jsonObj = require(settingsFile);
1024 jsonObj.emailAddress = emailAddress;
1025 jsonObj.flowFile = jsonObj.flowFile.replace(appDir + "/",'');
1026 jsonObj.dbHost = dbHost;
1027 jsonObj.dbPort = dbPort;
1028 jsonObj.dbName = dbName;
1029 jsonObj.dbUser = dbUser;
1030 jsonObj.dbPassword = dbPassword;
1031 jsonObj.gitLocalRepository = gitLocalRepository;
1032 jsonObj.performGitPull = performGitPull;
1033 jsonObj.restConfUrl = restConfUrl;
1034 jsonObj.restConfUser = restConfUser;
1035 jsonObj.restConfPassword = restConfPassword;
1036 jsonObj.formatXML = formatXML;
1037 jsonObj.formatJSON = formatJSON;
1038 var updatedSettings = jsonObj;
1040 var settingsStr= "module.exports=" + JSON.stringify(updatedSettings,null,4);
1041 //console.log("settingsStr:" + settingsStr);
1042 fs.writeFileSync(settingsFile,settingsStr);
1043 var svcLogicPropStr = "" ;
1044 svcLogicPropStr += "org.onap.ccsdk.sli.dbtype=jdbc" + "\n";
1045 svcLogicPropStr += "org.onap.ccsdk.sli.jdbc.url=jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName + "\n";
1046 svcLogicPropStr += "org.onap.ccsdk.sli.jdbc.database=" + dbName + "\n";
1047 svcLogicPropStr += "org.onap.ccsdk.sli.jdbc.user=" + dbUser + "\n";
1048 svcLogicPropStr += "org.onap.ccsdk.sli.jdbc.password=" + dbPassword;
1050 //create svclogic.properties file in the conf dir
1051 var svcPropFile = userDir + "/conf/svclogic.properties";
1052 fs.writeFileSync(svcPropFile,svcLogicPropStr);
1054 res.send({"status": "success"});
1056 console.log("Error:" + e);
1057 res.send({"status": "error"});
1063 app.post("/deleteYangFile",
1066 var qs = require('querystring');
1067 //console.log("Received the request:");
1069 req.on('data', function (data) {
1072 req.on('end',function(){
1073 var post = qs.parse(body);
1074 //console.dir(body);
1075 var fileName = post["fileName"];
1076 var appDir = path.dirname(require.main.filename);
1077 var yangFilePath = appDir + "/yangFiles/" + fileName;
1079 fs.unlinkSync(yangFilePath);
1080 res.send({"status" :"SUCCESS"});
1082 console.log("error" + err);
1083 res.send({"status" :"ERROR"});
1085 //console.log("prevPassword:" + settings.httpAuth.pass );
1090 app.post("/updatePassword",
1093 var qs = require('querystring');
1094 //console.log("Received the request:");
1096 req.on('data', function (data) {
1099 req.on('end',function(){
1100 var post = qs.parse(body);
1101 //console.dir(body);
1102 var password = post["password"];
1103 //console.log("prevPassword:" + settings.httpAuth.pass );
1104 //console.log("New password:" + password);
1105 var crypto = require("crypto");
1106 var cryptPasswd = crypto.createHash('md5').update(password,'utf8').digest('hex')
1107 var appDir = path.dirname(require.main.filename);
1108 var userDir = appDir + "/" + settings.userDir;
1109 //console.log("userDir:" + userDir);
1110 /*var newSettings = settings;
1111 newSettings.httpAuth.pass = cryptPasswd;
1112 var updatedSettings = JSON.stringify(settings,null,4);
1113 var settingsStr = "module.exports=" + updatedSettings;
1114 console.log(updatedSettings);
1117 var settingsFile = userDir + "/customSettings.js";
1118 //console.log("settingsFile:" + settingsFile);
1119 //var buf = fs.readFileSync(settingsFile, "utf8");
1120 var jsonObj = require(settingsFile);
1121 //console.log("jsonObj:" + JSON.stringify(jsonObj));
1122 jsonObj.httpAuth.pass = cryptPasswd;
1123 jsonObj.httpAdminAuth.pass = cryptPasswd;
1124 jsonObj.httpNodeAuth.pass = cryptPasswd;
1125 jsonObj.flowFile = jsonObj.flowFile.replace(appDir + "/",'');
1126 var updatedSettings = jsonObj;
1128 delete updatedSettings.httpRoot;
1129 delete updatedSettings.disableEditor;
1130 delete updatedSettings.httpAdminRoot;
1131 delete updatedSettings.httpAdminAuth;
1132 delete updatedSettings.httpNodeRoot;
1133 delete updatedSettings.httpNodeAuth;
1134 delete updatedSettings.uiHost;
1135 delete updatedSettings.version;
1137 var settingsStr= "module.exports=" + JSON.stringify(updatedSettings,null,4);
1138 //console.log("settingsStr:" + settingsStr);
1139 fs.writeFileSync(settingsFile,settingsStr);
1140 settings.httpAuth.pass = cryptPasswd;
1141 res.send({"status": "success"});
1143 console.log("Error:" + e);
1144 res.send({"status": "error"});
1150 var appDir = path.dirname(require.main.filename);
1151 var yangDir = appDir + "/yangFiles" ;
1152 var diskStorage = multer.diskStorage({
1153 destination: function (req, file, callback) {
1154 callback(null, yangDir);
1156 filename: function (req, file, callback) {
1157 //callback(null, file.fieldname + '-' + Date.now());
1158 callback(null, file.originalname);
1161 var upload = multer({ storage : diskStorage}).single('yangFile');
1163 app.post('/api/uploadyang',function(req,res){
1164 upload(req,res,function(err) {
1167 return res.end("Error uploading file." + err);
1170 var fileName = req.file.originalname;
1171 var yangFileFullPath = appDir + "/yangFiles/" + fileName;
1172 console.log("yangFileFullPath:" + yangFileFullPath);
1173 var commandToExec ="";
1174 if(fileName != null){
1175 var matchedArr = fileName.match(/.zip$/);
1176 if(matchedArr != null && matchedArr.length >0){
1177 console.log("uploaded zip file" + fileName);
1178 commandToExec = appDir + "/tools/generate_props_from_yangs_zip.sh " + yangFileFullPath ;
1180 commandToExec = appDir + "/tools/generate_props_from_yang.sh " + yangFileFullPath ;
1181 console.log("uploaded file" + fileName);
1184 var exec = require('child_process').exec;
1185 console.log("commandToExec:" + commandToExec);
1186 var child = exec(commandToExec ,function (error,stdout,stderr){
1188 console.log("Error occured:" + error);
1189 var msg = "File " + fileName + " could not be processed successfully.";
1191 console.log("stderr:" + stderr);
1192 res.json({"sliValuesObj" : [],"message":msg});
1194 res.json({"sliValuesObj" : [],"message":msg});
1198 console.log("stderr:" + stderr);
1201 console.log("stdout:" + stdout);
1203 var msg = "File " + fileName + " processed successfully.";
1204 var generatedJSDir=appDir + "/generatedJS";
1205 var sliValuesObj =[];
1206 //var glob = require("glob");
1207 //glob(generatedJSDir + "/**/*.js", null, function (er, files) {
1209 var sliValuesObj =[];
1210 for(var i=0;files!= null && i<files.length;i++){
1211 var f = files[i].replace( new RegExp(generatedJSDir + "/", "g" ), "" );
1212 console.log("loading file " + f);
1214 sliValuesObj.push(require(files[i]));
1215 //console.dir(sliValuesObj);
1217 console.log("Error:Could not load file " + files[i]);
1220 res.json({"sliValuesObj" : sliValuesObj,"message":msg});
1223 res.json({"sliValuesObj" : sliValuesObj,"message":msg});
1229 function getFormattedDate(){
1231 var mm = d.getMonth() + 1;
1232 var dd = d.getDate();
1233 var yyyy = d.getYear() + 1900;
1234 var hr = d.getHours();
1235 var min = d.getMinutes();
1236 var sec = d.getSeconds();
1237 if(mm<10) mm = "0" + mm;
1238 if(dd<10) dd = "0" + dd;
1239 if(hr<10) hr = "0" + hr;
1240 if(min<10) min = "0" + min;
1241 if(sec<10) sec = "0" + sec;
1242 var formatedValue = yyyy + "-" + mm + "-" + dd + "_" + hr + ":" + min + ":" + sec;
1243 return formatedValue;
1247 app.post("/saveTestDGInput",
1248 express.json({'limit':'16mb'}),
1250 var qs = require('querystring');
1252 req.on('data', function (data) {
1255 req.on('end', function () {
1256 var appDir = path.dirname(require.main.filename);
1257 var inputFilesDir = appDir + "/" + "inputFiles";
1258 if (!fs.existsSync(inputFilesDir)){
1259 fs.mkdirSync(inputFilesDir);
1261 var post = qs.parse(body);
1262 var moduleName = post['moduleName'];
1263 var rpcName = post['rpcName'];
1264 var fullFileName = inputFilesDir + "/" +moduleName + "_" + rpcName + "_" + getFormattedDate();
1265 var inputStr = post['inputStr'];
1266 writeToFile(fullFileName,inputStr);
1271 app.post("/getInputFiles",function(req,res) {
1272 var qs = require('querystring');
1274 req.on('data', function (data) {
1277 req.on('end', function () {
1278 var appDir = path.dirname(require.main.filename);
1279 var inputFilesDir = appDir + "/" + "inputFiles";
1280 if (!fs.existsSync(inputFilesDir)){
1281 fs.mkdirSync(inputFilesDir);
1283 var post = qs.parse(body);
1284 var moduleName = post['moduleName'];
1285 var rpcName = post['rpcName'];
1286 var glob = require("glob");
1287 var filePatt = "/**/" + moduleName + "_" + rpcName + "*";
1288 glob(inputFilesDir + filePatt, null, function (er, files) {
1290 for(var i=0;files!= null && i<files.length;i++){
1291 var f = files[i].replace( new RegExp(inputFilesDir + "/", "g" ), "" );
1294 res.json({"files" : filesList});
1299 app.post("/deleteInputFile",
1302 var qs = require('querystring');
1304 req.on('data', function (data) {
1307 req.on('end',function(){
1308 var post = qs.parse(body);
1309 var fileName = post["fileName"];
1310 var appDir = path.dirname(require.main.filename);
1311 var filePath = appDir + "/inputFiles/" + fileName;
1313 fs.unlinkSync(filePath);
1314 res.send({"status" :"SUCCESS"});
1316 console.log("error" + err);
1317 res.send({"status" :"ERROR"});
1319 //console.log("prevPassword:" + settings.httpAuth.pass );
1323 app.post("/loadInputFile",
1326 var qs = require('querystring');
1328 req.on('data', function (data) {
1331 req.on('end',function(){
1332 var post = qs.parse(body);
1333 var fileName = post["fileName"];
1334 //console.log("fileName" + fileName);
1335 var appDir = path.dirname(require.main.filename);
1336 var filePath = appDir + "/inputFiles/" + fileName;
1337 //console.log("filePath:" + filePath);
1339 fs.readFile(filePath, 'utf8', function (err,data) {
1341 return console.log(err);
1343 res.json({'input':data});
1346 console.log("error" + err);
1347 res.json({"status" :"ERROR"});
1353 app.get("/getYangFiles",function(req,res) {
1354 var appDir = path.dirname(require.main.filename);
1355 var yangFilesDir=appDir + "/yangFiles";
1356 var glob = require("glob")
1357 glob(yangFilesDir + "/**/*.yang", null, function (er, files) {
1359 for(var i=0;files!= null && i<files.length;i++){
1360 var f = files[i].replace( new RegExp(yangFilesDir + "/", "g" ), "" );
1364 res.json({"files" : filesList});
1370 function reportAddedModules(info) {
1371 comms.publish("node/added",info,false);
1372 if (info.length > 0) {
1373 util.log("[red] Added node types:");
1374 for (var i=0;i<info.length;i++) {
1375 for (var j=0;j<info[i].types.length;j++) {
1376 util.log("[red] - "+
1377 (info[i].module?info[i].module+":":"")+
1379 (info[i].err?" : "+info[i].err:"")
1387 function reportRemovedModules(removedNodes) {
1388 comms.publish("node/removed",removedNodes,false);
1389 util.log("[red] Removed node types:");
1390 for (var j=0;j<removedNodes.length;j++) {
1391 for (var i=0;i<removedNodes[j].types.length;i++) {
1392 util.log("[red] - "+(removedNodes[i].module?removedNodes[i].module+":":"")+removedNodes[j].types[i]);
1395 return removedNodes;
1398 function installModule(module) {
1399 //TODO: ensure module is 'safe'
1400 return when.promise(function(resolve,reject) {
1401 if (/[\s;]/.test(module)) {
1402 reject(new Error("Invalid module name"));
1405 util.log("[red] Installing module: "+module);
1406 var child = exec('npm install --production '+module, function(err, stdin, stdout) {
1408 var lookFor404 = new RegExp(" 404 .*"+module+"$","m");
1409 if (lookFor404.test(stdout)) {
1410 util.log("[red] Installation of module "+module+" failed: module not found");
1411 var e = new Error();
1415 util.log("[red] Installation of module "+module+" failed:");
1416 util.log("------------------------------------------");
1417 console.log(err.toString());
1418 util.log("------------------------------------------");
1419 reject(new Error("Install failed"));
1422 util.log("[red] Installed module: "+module);
1423 resolve(redNodes.addModule(module).then(reportAddedModules));
1429 function uninstallModule(module) {
1430 var list = redNodes.removeModule(module);
1431 return when.promise(function(resolve,reject) {
1432 if (/[\s;]/.test(module)) {
1433 reject(new Error("Invalid module name"));
1436 util.log("[red] Removing module: "+module);
1437 var child = exec('npm remove '+module, function(err, stdin, stdout) {
1439 util.log("[red] Removal of module "+module+" failed:");
1440 util.log("------------------------------------------");
1441 console.log(err.toString());
1442 util.log("------------------------------------------");
1443 reject(new Error("Removal failed"));
1445 util.log("[red] Removed module: "+module);
1446 reportRemovedModules(list);
1454 var defer = when.defer();
1455 //split and save startup dgs if any from flows.json file
1456 var appDir = path.dirname(require.main.filename);
1457 var userDir = appDir + "/" + settings.userDir;
1458 var flowFile = settings.flowFile;
1459 var outputDir = userDir + "/orig_dgs";
1460 console.log("appDir:" + appDir);
1461 console.log("flowFile:" + flowFile);
1462 var execFile = require('child_process').execFile;
1463 var commandToExec = appDir + "/tools/splitFlows.sh" ;
1464 console.log("commandToExec:" + commandToExec);
1465 var args = [flowFile,outputDir];
1466 var child = execFile(commandToExec ,args,function (error,stdout,stderr){
1468 console.log("Error occured:" + error);
1470 console.log("stderr:" + stderr);
1472 console.log("error:" + error);
1476 console.log("stderr:" + stderr);
1479 console.log("output:" + stdout);
1485 storage.init(settings).then(function() {
1486 settings.load(storage).then(function() {
1487 console.log("\nWelcome to Node-RED\n===================\n");
1488 if (settings.version) {
1489 util.log("[red] Version: "+settings.version);
1491 util.log("[red] Loading palette nodes");
1492 redNodes.init(settings,storage);
1493 redNodes.load().then(function() {
1495 var nodes = redNodes.getNodeList();
1496 var nodeErrors = nodes.filter(function(n) { return n.err!=null;});
1497 var nodeMissing = nodes.filter(function(n) { return n.module && n.enabled && !n.loaded && !n.err;});
1498 if (nodeErrors.length > 0) {
1499 util.log("------------------------------------------");
1500 if (settings.verbose) {
1501 for (i=0;i<nodeErrors.length;i+=1) {
1502 util.log("["+nodeErrors[i].name+"] "+nodeErrors[i].err);
1505 util.log("[red] Failed to register "+nodeErrors.length+" node type"+(nodeErrors.length==1?"":"s"));
1506 util.log("[red] Run with -v for details");
1508 util.log("------------------------------------------");
1510 if (nodeMissing.length > 0) {
1511 util.log("[red] Missing node modules:");
1512 var missingModules = {};
1513 for (i=0;i<nodeMissing.length;i++) {
1514 var missing = nodeMissing[i];
1515 missingModules[missing.module] = (missingModules[missing.module]||[]).concat(missing.types);
1518 for (i in missingModules) {
1519 if (missingModules.hasOwnProperty(i)) {
1520 util.log("[red] - "+i+": "+missingModules[i].join(", "));
1521 if (settings.autoInstallModules) {
1522 installModule(i).otherwise(function(err) {
1523 // Error already reported. Need the otherwise handler
1524 // to stop the error propagating any further
1529 if (!settings.autoInstallModules) {
1530 util.log("[red] Removing modules from config");
1531 redNodes.cleanNodeList();
1536 redNodes.loadFlows();
1537 }).otherwise(function(err) {
1542 }).otherwise(function(err) {
1546 return defer.promise;
1550 redNodes.stopFlows();
1560 module.exports.__defineGetter__("app", function() { return app });
1561 module.exports.__defineGetter__("nodeApp", function() { return nodeApp });
1562 module.exports.__defineGetter__("server", function() { return server });