fix odl patches
[ccsdk/distribution.git] / dgbuilder / red / server.js
1 /**
2  * Copyright 2013 IBM Corp.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  **/
16
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;
22
23 var createUI = require("./ui");
24 var redNodes = require("./nodes");
25 var comms = require("./comms");
26 var storage = require("./storage");
27 var fs=require('fs');
28 var path = require("path");
29 var app = null;
30 var nodeApp = null;
31 var server = null;
32 var settings = null;
33
34 var flowShareUsers = require("../flowShareUsers");
35         
36 //console.dir(flowShareUsers);
37
38 function createServer(_server,_settings) {
39     server = _server;
40     settings = _settings;
41
42     comms.init(_server,_settings);
43     
44     nodeApp = express();
45     app = express();
46         
47     if (settings.httpAdminRoot !== false) {
48         
49         
50         if (!settings.disableEditor) {
51             createUI(settings,app);
52         }
53         
54         var slaActions = require("./sla");
55
56         app.get("/flows",function(req,res) {
57             res.json(redNodes.getFlows());
58         });
59
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.
69                         //console.dir(files);
70                         var sliValuesObj =[];
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);
74                                 try{
75                                         sliValuesObj.push(require(files[i]));
76                                         //console.dir(sliValuesObj);
77                                 }catch(err){
78                                         console.log("Error:Could not load file " + files[i]);
79                                 }
80                         }
81                         res.json({"sliValuesObj" : sliValuesObj});
82                 });
83         });
84
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";
89                 //console.dir(req);
90                 var selectedModulesStr = req.query.selectedModules;
91                 var selectedModules = [];
92                 if(selectedModulesStr != undefined && selectedModulesStr != null){ 
93                         selectedModules = selectedModulesStr.split(",");                
94                 }
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);
99                 try{
100                         fs.writeFileSync(file, content, 'utf8');
101                 }catch(err){
102                         console.log("could not write to file " + file);
103                 }
104                 var sliValuesObj =[];
105                 for(var i=0;selectedModules!= null && i<selectedModules.length;i++){
106                         var f = generatedJSDir + "/" + selectedModules[i] + "_inputs.js";
107                         try{
108                                 delete require.cache[require.resolve(f)]
109                                 require.resolve();
110                         }catch(err){
111                                 console.log("error deleting loaded module " + f + " from cache");
112                         }
113                         //console.log("loading file " + f);
114                         try{
115                                 sliValuesObj.push(require(f));
116                         }catch(err){
117                                 console.log("Error:Could not load file " + f);
118                         }
119                 }
120                 //console.dir(sliValuesObj);
121                 res.json({"sliValuesObj" : sliValuesObj});
122         });
123
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 = [];
131                 var selectedModules;
132                 try{
133                         selectedModules = require(file);        
134                         selected_modules=selectedModules["selected_modules"];
135                         //console.log("selected_modules are ");
136                         //console.dir(selected_modules);
137                 }catch(err){
138                         console.log("Could not load the file " + file);
139                 }
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);
143                         try{
144                                 sliValuesObj.push(require(f));
145                         }catch(err){
146                                 console.log("Error:Could not load file " + f);
147                         }
148                 }
149                 res.json({"sliValuesObj" : sliValuesObj});
150         });
151
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 = [];
159                 var selectedModules;
160                 try{
161                         delete require.cache[require.resolve(file)]
162                         require.resolve();
163                 }catch(err){
164                         console.log("error deleting loaded module " + file + " from cache");
165                 }
166                 try{
167                         selectedModules = require(file);        
168                         selected_modules=selectedModules["selected_modules"];
169                         console.log("selected_modules are ");
170                         //console.dir(selected_modules);
171                 }catch(err){
172                         console.log("Could not load the file " + file);
173                 }
174                 glob(generatedJSDir + "/**/*.js", null, function (er, files) {
175                         var filesList =[];
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");
181                                 }else{
182                                         filesList.push(f + ":unchecked");
183                                 }
184                         }
185                         res.json({"files" : filesList});
186                 });
187         });
188
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);
195         });
196
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);
203         });
204
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);
211         });
212
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);
219         });
220
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);
227         });
228
229
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);
236                 res.json(jsonObj);
237         });
238
239         app.get("/getCommitsInfo", function(req,res) {
240                 var appDir = path.dirname(require.main.filename);
241                 var userDir = appDir + "/" + settings.userDir;
242                 //console.dir(req);
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){
250                 if(error){
251                         console.log("Error occured:" + error);
252                         if(stderr){
253                                 //console.log("stderr:" + stderr);
254                                 res.send(500,{'error':error,'stderr':stderr});
255                         }else{
256                                 res.send(500,{'error':error});
257                         }
258                         //console.log("stdout :" + stdout);
259                 }else{
260                         if(stderr){
261                                 console.log("stderr:" + stderr);
262                         }
263                         if(stdout){
264                                 //console.log("output:" + stdout);
265                                 res.send(200,{'stdout':stdout,'stderr':stderr});
266                         }
267                 }
268                 });
269         });
270
271         app.get("/importCodeCloudFlow",
272                  function(req,res) {
273                 var appDir = path.dirname(require.main.filename);
274                 var userDir = appDir + "/" + settings.userDir;
275                 //console.dir(req);
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){
284                 if(error){
285                         console.log("Error occured:" + error);
286                         if(stderr){
287                                 //console.log("stderr:" + stderr);
288                                 res.send(500,{'error':error,'stderr':stderr});
289                         }else{
290                                 res.send(500,{'error':error});
291                         }
292                 }else{
293                         if(stderr){
294                                 console.log("stderr:" + stderr);
295                         }
296                         if(stdout){
297                                 //console.log("output:" + stdout);
298                                 res.send(200,{'stdout':stdout,'stderr':stderr});
299                         }
300                 }
301                 });
302         });
303
304         app.get("/importGitLocalFlow",
305                 function(req,res) {
306                 var appDir = path.dirname(require.main.filename);
307                 var gitLocalRepository =  settings.gitLocalRepository;
308                 /*
309                 var userDir=settings.userDir;
310                 var outputDir  = appDir + "/" +  userDir + "/orig_dgs";
311                 if (!fs.existsSync(outputDir)){
312                         fs.mkdirSync(outputDir);
313                 }
314                 */
315                 //console.dir(req);
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){
324                 if(error){
325                         console.log("Error occured:" + error);
326                         if(stderr){
327                                 //console.log("stderr:" + stderr);
328                                 res.send(500,{'error':error,'stderr':stderr});
329                         }else{
330                                 res.send(500,{'error':error});
331                         }
332                 }else{
333                         if(stderr){
334                                 console.log("stderr:" + stderr);
335                         }
336                         if(stdout){
337                                 /*
338                                 var jsonStr= stdout;
339                                 var jsonStrFormatted=[];
340                                 try{
341                                         jsonStrFormatted= JSON.parse(jsonStr);
342                                 }catch(e){
343                                 }
344                                 fs.writeFileSync( outputDir + "/" +currTabId,JSON.stringify(jsonStrFormatted,null,4) ); 
345                                 */
346                                 //console.log("output:" + stdout);
347                                 res.send(200,{'stdout':stdout,'stderr':stderr});
348                         }
349                 }
350                 });
351         });
352
353         app.post("/saveImportedDG",
354             express.json(),
355             function(req,res) {
356                 var qs = require('querystring');
357                 var body = '';
358                 req.on('data', function (data) {
359                         body += data;
360                 });
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);
367                         }
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"});
373                 });
374         });
375
376         app.post("/saveImportedDG",
377             express.json(),
378             function(req,res) {
379                 var qs = require('querystring');
380                 var body = '';
381                 req.on('data', function (data) {
382                         body += data;
383                 });
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);
390                         }
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"});
396                 });
397         });
398
399         app.get("/gitcheckout", function(req,res) {
400                 var appDir = path.dirname(require.main.filename);
401                 var gitLocalRepository =  settings.gitLocalRepository;
402                 //console.dir(req);
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){
409                                 if(error){
410                                         console.log("Error occured:" + error);
411                                         if(stderr){
412                                                 console.log("stderr:" + stderr);
413                                                 res.json({"output":stderr});
414                                         }else{
415                                                 res.json({"output":error});
416                                         }
417                                 }else{
418                                         if(stderr){
419                                                 console.log("stderr:" + stderr);
420                                         }
421                                         if(stdout){
422                                                 res.json({"output": stderr + " " + stdout});
423                                         }
424                                 }
425                         });
426         });
427
428         app.get("/gitpull", function(req,res) {
429                 var appDir = path.dirname(require.main.filename);
430                 var gitLocalRepository =  settings.gitLocalRepository;
431                 //console.dir(req);
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){
438                                 if(error){
439                                         console.log("Error occured:" + error);
440                                         if(stderr){
441                                                 console.log("stderr:" + stderr);
442                                                 res.json({"output":stderr});
443                                         }else{
444                                                 res.json({"output":error});
445                                         }
446                                 }else{
447                                         if(stderr){
448                                                 console.log("stderr:" + stderr);
449                                         }
450                                         if(stdout){
451                                                 res.json({"output": stderr + " " + stdout});
452                                         }
453                                 }
454                         });
455         });
456
457         app.get("/gitstatus", function(req,res) {
458                 var appDir = path.dirname(require.main.filename);
459                 var gitLocalRepository =  settings.gitLocalRepository;
460                 //console.dir(req);
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){
467                                 if(error){
468                                         console.log("Error occured:" + error);
469                                         if(stderr){
470                                                 console.log("stderr:" + stderr);
471                                                 res.json({"output":stderr});
472                                         }else{
473                                                 res.json({"output":error});
474                                         }
475                                 }else{
476                                         if(stderr){
477                                                 console.log("stderr:" + stderr);
478                                         }
479                                         if(stdout){
480                                                 res.json({"output": stderr + " " + stdout});
481                                         }
482                                 }
483                         });
484         });
485         
486         app.post("/getSharedFlow",
487             express.json(),
488             function(req,res) {
489                 var qs = require('querystring');
490                 var body = '';
491                 req.on('data', function (data) {
492                         body += data;
493                 });
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) {
498                                 if (err) {
499                                         return console.log(err);
500                                 }
501                                 res.json(data);
502                                 //console.log(data);
503                         });
504                 //res.sendFile(body.filePath);
505                 });
506         });
507         
508         app.post("/downloadYang",
509             express.json(),
510             function(req,res) {
511                 var qs = require('querystring');
512                 var body = '';
513                 req.on('data', function (data) {
514                         body += data;
515                 });
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);
525                 });
526         });
527
528         function writeToFile(fullPathToFileName,str){
529                 try{
530                         fs.writeFileSync(fullPathToFileName,str);
531                 }catch(e){
532                         console.log("Error:" + e);
533                 }
534         }
535
536         function getCurrentDate(){
537                 var d = new Date();
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;
551         }
552
553
554         app.post("/downloadXml",
555             express.json({'limit':'16mb'}),
556             function(req,res) {
557                 //console.log("Received request and processing:" + new Date());
558                 var qs = require('querystring');
559                 var body = '';
560                 //var msecs1= Date.now();
561                 req.on('data', function (data) {
562                         body += data;
563                 });
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."});
580                         }else{
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;
586
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');
593                                 res.end(xml);
594                                 //console.log("Response sent:" + new Date());
595                         }
596                 });
597         });
598
599         app.post("/downloadJson",
600             express.json({'limit':'16mb'}),
601             function(req,res) {
602                         var appDir = path.dirname(require.main.filename);
603                         var sharedDir = appDir + "/" + settings.sharedDir;
604                         var qs = require('querystring');
605                         var body = '';
606                         req.on('data', function (data) {
607                                 body += data;
608                         });
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."});
617                                 }else{
618                                         var formatted_date = getCurrentDate();
619                                         //console.log("moduleName:" + moduleName);
620                                         //console.log("methodName:" + methodName);
621
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) {
632                                                         if(err){
633                                                                 console.log('Error :' + err);
634                                                         }
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);
640                                                         res.end(jsonStr);
641                                                 });
642                                         }else{
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);
648                                                 res.end(jsonStr);
649                                         }
650                                 }
651                         });
652         });
653
654         app.post("/flows",
655             express.json({'limit':'16mb'}),
656             function(req,res) {
657                 //console.log("Processing Request");
658                 var flows = req.body;
659                 redNodes.setFlows(flows).then(function() {
660                     res.send(204);
661                 }).otherwise(function(err) {
662                     util.log("[red] Error saving flows : "+err);
663                     res.send(500,err.message);
664                 });
665             },
666             function(error,req,res,next) {
667                 res.send(400,"Invalid Flow.  Error " + error);
668             }
669         );
670             
671         app.get("/nodes",function(req,res) {
672             if (req.get("accept") == "application/json") {
673                 res.json(redNodes.getNodeList());
674             } else {
675                 res.send(redNodes.getNodeConfigs());
676             }
677         });
678         
679         app.post("/nodes",
680             express.json(),
681             function(req,res) {
682                 if (!settings.available()) {
683                     res.send(400,new Error("Settings unavailable").toString());
684                     return;
685                 }
686                 var node = req.body;
687                 var promise;
688                 if (node.file) {
689                     promise = redNodes.addNode(node.file).then(reportAddedModules);
690                 } else if (node.module) {
691                     var module = redNodes.getNodeModuleInfo(node.module);
692                     if (module) {
693                         res.send(400,"Module already loaded");
694                         return;
695                     }
696                     promise = installModule(node.module);
697                 } else {
698                     res.send(400,"Invalid request");
699                     return;
700                 }
701                 promise.then(function(info) {
702                     res.json(info);
703                 }).otherwise(function(err) {
704                     if (err.code === 404) {
705                         res.send(404);
706                     } else {
707                         res.send(400,err.toString());
708                     }
709                 });
710             },
711             function(err,req,res,next) {
712                 console.log(err.toString());
713                 res.send(400,err);
714             }
715         );
716         
717         app.delete("/nodes/:id",
718             function(req,res) {
719                 if (!settings.available()) {
720                     res.send(400,new Error("Settings unavailable").toString());
721                     return;
722                 }
723                 var id = req.params.id;
724                 var removedNodes = [];
725                 try {
726                     var node = redNodes.getNodeInfo(id);
727                     var promise = null;
728                     if (!node) {
729                         var module = redNodes.getNodeModuleInfo(id);
730                         if (!module) {
731                             res.send(404);
732                             return;
733                         } else {
734                             promise = uninstallModule(id);
735                         }
736                     } else {
737                         promise = when.resolve([redNodes.removeNode(id)]).then(reportRemovedModules);
738                     }
739                     
740                     promise.then(function(removedNodes) {
741                         res.json(removedNodes);
742                     }).otherwise(function(err) {
743                         console.log(err.stack);
744                         res.send(400,err.toString());
745                     });
746                 } catch(err) {
747                     res.send(400,err.toString());
748                 }
749             },
750             function(err,req,res,next) {
751                 res.send(400,err);
752             }
753         );
754         
755         app.get("/nodes/:id", function(req,res) {
756             var id = req.params.id;
757             var result = null;
758             if (req.get("accept") == "application/json") {
759                 result = redNodes.getNodeInfo(id);
760             } else {
761                 result = redNodes.getNodeConfig(id);
762             }
763             if (result) {
764                 res.send(result);
765             } else {
766                 res.send(404);
767             }
768         });
769         
770         app.put("/nodes/:id", 
771             express.json(),
772             function(req,res) {
773                 if (!settings.available()) {
774                     res.send(400,new Error("Settings unavailable").toString());
775                     return;
776                 }
777                 var body = req.body;
778                 if (!body.hasOwnProperty("enabled")) {
779                     res.send(400,"Invalid request");
780                     return;
781                 }
782                 try {
783                     var info;
784                     var id = req.params.id;
785                     var node = redNodes.getNodeInfo(id);
786                     if (!node) {
787                         res.send(404);
788                     } else if (!node.err && node.enabled === body.enabled) {
789                         res.json(node);
790                     } else {
791                         if (body.enabled) {
792                             info = redNodes.enableNode(id);
793                         } else {
794                             info = redNodes.disableNode(id);
795                         }
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]);
801                             }
802                         } else if (body.enabled && info.err) {
803                             util.log("[red] Failed to enable node:");
804                             util.log("[red] - "+info.name+" : "+info.err);
805                         }
806                         res.json(info);
807                     }
808                 } catch(err) {
809                     res.send(400,err.toString());
810                 }            
811             }
812         );
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);
823                         var filesList =[];
824                         for(var i=0;files!= null && i<files.length;i++){
825                                 var f = files[i].replace( new RegExp(codeCloudDir + "/", "g" ), "" );
826                                 filesList.push(f);
827
828                         }
829                         res.json({"files" : filesList});
830                 });
831         });
832
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"});
842                         return;
843                 }
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){
848                                 if(error){
849                                         console.log("Error occured:" + error);
850                                         if(stderr){
851                                                 console.log("stderr:" + stderr);
852                                                 res.json({"output":stderr});
853                                         }else{
854                                                 res.json({"output":error});
855                                         }
856                                 }else{
857                                         if(stderr){
858                                                 console.log("stderr:" + stderr);
859                                         }
860                                         if(stdout){
861                                                 res.json({"output":stdout});
862                                         }
863                                 }
864                         });
865                                 
866         });
867
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) {
876                         performGitPull="N";
877                 }
878                 var gitLocalRepository=jsonObj.gitLocalRepository;
879                 if(gitLocalRepository == undefined || gitLocalRepository == null || gitLocalRepository == ''){
880                         res.json({"files" : ["GIT_LOCAL_REPOSITORY_NOT_SET"]});
881                         return;
882                                 
883                    }
884
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){
890                                 if(error){
891                                         console.log("Error occured:" + error);
892                                         if(stderr){
893                                                 console.log("stderr:" + stderr);
894                                                 res.json({"files":[]});
895                                         }else{
896                                                 res.json({"files":[]});
897                                         }
898                                 }else{
899                                         if(stderr){
900                                                 console.log("stderr:" + stderr);
901                                         }
902                                         if(stdout){
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);
910                                                 var filesList =[];
911                                                 for(var i=0;files!= null && i<files.length;i++){
912                                                         var f = files[i].replace( new RegExp(gitLocalRepository + "/", "g" ), "" );
913                                                         filesList.push(f);
914
915                                                 }
916                                                 res.json({"files" : filesList});
917                                                 });
918                                         }
919                                 }
920                         });
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);
929                         var filesList =[];
930                         for(var i=0;files!= null && i<files.length;i++){
931                                 var f = files[i].replace( new RegExp(gitLocalRepository + "/", "g" ), "" );
932                                 filesList.push(f);
933
934                         }
935                         res.json({"files" : filesList});
936                         });
937                 }
938         
939         });
940
941         app.get("/flowShareUsers",function(req,res) {
942             res.json(flowShareUsers);
943         });
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});
948         });
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 });
954         });
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){
962                         if(err){
963                                 res.json({"files": []});
964                         }else{
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){
973                                                         return 1;
974                                                 }else if(fileStat1.mtime < fileStat2.mtime){
975                                                         return -1;
976                                                 }else{
977                                                         return 0;
978                                                 }
979                                         });
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]});
984                                                 }
985                                         }
986                                         res.json(onlyFilesArr);
987                                 }else{
988                                         res.json({"files": []});
989                                 }
990                         }
991                 });
992         });
993
994         app.post("/updateConfiguration",
995             express.json(),
996             function(req,res) {
997                 var qs = require('querystring');
998                 //console.log("Received the request:");
999                 var body ="";
1000                  req.on('data', function (data) {
1001                         body += data;
1002                 });
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);
1021                         try{
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;
1039
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;
1049                                 
1050                                 //create svclogic.properties file in the conf dir
1051                                 var svcPropFile = userDir + "/conf/svclogic.properties";
1052                                 fs.writeFileSync(svcPropFile,svcLogicPropStr);
1053
1054                                 res.send({"status": "success"});
1055                         }catch(e){
1056                                 console.log("Error:" + e);
1057                                 res.send({"status": "error"});
1058                         }
1059                 });
1060             }
1061         );
1062
1063         app.post("/deleteYangFile",
1064             express.json(),
1065             function(req,res) {
1066                 var qs = require('querystring');
1067                 //console.log("Received the request:");
1068                 var body ="";
1069                  req.on('data', function (data) {
1070                         body += data;
1071                 });
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;
1078                         try{
1079                                 fs.unlinkSync(yangFilePath);
1080                                 res.send({"status" :"SUCCESS"});
1081                         }catch(err){
1082                                 console.log("error" + err);
1083                                 res.send({"status" :"ERROR"});
1084                         }
1085                         //console.log("prevPassword:" + settings.httpAuth.pass );
1086                 });
1087             }
1088         );
1089
1090         app.post("/updatePassword",
1091             express.json(),
1092             function(req,res) {
1093                 var qs = require('querystring');
1094                 //console.log("Received the request:");
1095                 var body ="";
1096                  req.on('data', function (data) {
1097                         body += data;
1098                 });
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);
1115                         */
1116                         try{
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;
1127                                 /*
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;
1136                                 */
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"});
1142                         }catch(e){
1143                                 console.log("Error:" + e);
1144                                 res.send({"status": "error"});
1145                         }
1146                 });
1147             }
1148         );
1149
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);
1155                         },
1156                         filename: function (req, file, callback) {
1157                                 //callback(null, file.fieldname + '-' + Date.now());
1158                                 callback(null, file.originalname);
1159                         }
1160         });
1161         var upload = multer({ storage : diskStorage}).single('yangFile');
1162
1163         app.post('/api/uploadyang',function(req,res){
1164                 upload(req,res,function(err) {
1165                         if(err) {
1166                                 console.log(err);
1167                                 return res.end("Error uploading file." + err);
1168                         }
1169                         //console.dir(req);     
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 ;
1179                                 }else{
1180                                         commandToExec = appDir + "/tools/generate_props_from_yang.sh " + yangFileFullPath ;
1181                                         console.log("uploaded file" + fileName);
1182                                 }
1183                         }
1184                         var exec = require('child_process').exec;
1185                         console.log("commandToExec:" + commandToExec);
1186                         var child = exec(commandToExec ,function (error,stdout,stderr){
1187                                 if(error){
1188                                         console.log("Error occured:" + error);
1189                                         var msg = "File " + fileName + " could not be processed successfully.";
1190                                         if(stderr){
1191                                                 console.log("stderr:" + stderr);
1192                                                 res.json({"sliValuesObj" : [],"message":msg});
1193                                         }else{
1194                                                 res.json({"sliValuesObj" : [],"message":msg});
1195                                         }
1196                                 }else{
1197                                         if(stderr){
1198                                                 console.log("stderr:" + stderr);
1199                                         }
1200                                         if(stdout){
1201                                                 console.log("stdout:" + stdout);
1202                                         }
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) {
1208                                         /*
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);
1213                                                         try{
1214                                                                 sliValuesObj.push(require(files[i]));
1215                                                                 //console.dir(sliValuesObj);
1216                                                         }catch(err){
1217                                                                 console.log("Error:Could not load file " + files[i]);
1218                                                         }
1219                                                 } 
1220                                                 res.json({"sliValuesObj" : sliValuesObj,"message":msg});
1221                                         });
1222                                         */
1223                                         res.json({"sliValuesObj" : sliValuesObj,"message":msg});
1224                                 }
1225                         });
1226                 });
1227         });
1228
1229         function getFormattedDate(){
1230                 var d = new Date();
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;
1244         }
1245
1246
1247         app.post("/saveTestDGInput",
1248             express.json({'limit':'16mb'}),
1249             function(req,res) {
1250                 var qs = require('querystring');
1251                 var body = '';
1252                 req.on('data', function (data) {
1253                         body += data;
1254                 });
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);
1260                         }
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);
1267                         res.end();
1268                 });
1269         });
1270
1271         app.post("/getInputFiles",function(req,res) {
1272                         var qs = require('querystring');
1273                         var body = '';
1274                         req.on('data', function (data) {
1275                                 body += data;
1276                         });
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);
1282                                 }
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) {
1289                                         var filesList =[];
1290                                         for(var i=0;files!= null && i<files.length;i++){
1291                                                 var f = files[i].replace( new RegExp(inputFilesDir + "/", "g" ), "" );
1292                                                 filesList.push(f); 
1293                                         }
1294                                         res.json({"files" : filesList});
1295                                 });
1296                         });
1297         });
1298
1299         app.post("/deleteInputFile",
1300             express.json(),
1301             function(req,res) {
1302                 var qs = require('querystring');
1303                 var body ="";
1304                  req.on('data', function (data) {
1305                         body += data;
1306                 });
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;
1312                         try{
1313                                 fs.unlinkSync(filePath);
1314                                 res.send({"status" :"SUCCESS"});
1315                         }catch(err){
1316                                 console.log("error" + err);
1317                                 res.send({"status" :"ERROR"});
1318                         }
1319                         //console.log("prevPassword:" + settings.httpAuth.pass );
1320                 });
1321             });
1322
1323         app.post("/loadInputFile",
1324             express.json(),
1325             function(req,res) {
1326                 var qs = require('querystring');
1327                 var body ="";
1328                  req.on('data', function (data) {
1329                         body += data;
1330                 });
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);
1338                         try{
1339                                 fs.readFile(filePath, 'utf8', function (err,data) {
1340                                         if (err) {
1341                                                 return console.log(err);
1342                                         }
1343                                         res.json({'input':data});
1344                                 });     
1345                         }catch(err){
1346                                 console.log("error" + err);
1347                                 res.json({"status" :"ERROR"});
1348                         }
1349                 });
1350             });
1351
1352
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) {
1358                         var filesList =[];
1359                         for(var i=0;files!= null && i<files.length;i++){
1360                                 var f = files[i].replace( new RegExp(yangFilesDir + "/", "g" ), "" );
1361                                 filesList.push(f);
1362
1363                         }
1364                         res.json({"files" : filesList});
1365                 });
1366         });
1367         }
1368 }
1369
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+":":"")+
1378                     info[i].types[j]+
1379                     (info[i].err?" : "+info[i].err:"")
1380                     );
1381             }
1382         }
1383     }
1384     return info;
1385 }
1386
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]);
1393         }
1394     }
1395     return removedNodes;
1396 }
1397
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"));
1403             return;
1404         }
1405         util.log("[red] Installing module: "+module);
1406         var child = exec('npm install --production '+module, function(err, stdin, stdout) {
1407             if (err) {
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();
1412                     e.code = 404;
1413                     reject(e);
1414                 } else {
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"));
1420                 }
1421             } else {
1422                 util.log("[red] Installed module: "+module);
1423                 resolve(redNodes.addModule(module).then(reportAddedModules));
1424             }
1425         });
1426     });
1427 }
1428
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"));
1434             return;
1435         }
1436         util.log("[red] Removing module: "+module);
1437         var child = exec('npm remove '+module, function(err, stdin, stdout) {
1438             if (err) {
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"));
1444             } else {
1445                 util.log("[red] Removed module: "+module);
1446                 reportRemovedModules(list);
1447                 resolve(list);
1448             }
1449         });
1450     });
1451 }
1452
1453 function start() {
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){
1467                 if(error){
1468                         console.log("Error occured:" + error);
1469                         if(stderr){
1470                                 console.log("stderr:" + stderr);
1471                         }else{
1472                                 console.log("error:" + error);
1473                         }
1474                 }else{
1475                         if(stderr){
1476                                 console.log("stderr:" + stderr);
1477                         }
1478                         if(stdout){
1479                                 console.log("output:" + stdout);
1480                         }
1481                 }
1482                 });
1483                 
1484     
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);
1490             }
1491             util.log("[red] Loading palette nodes");
1492             redNodes.init(settings,storage);
1493             redNodes.load().then(function() {
1494                 var i;
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);
1503                         }
1504                     } else {
1505                         util.log("[red] Failed to register "+nodeErrors.length+" node type"+(nodeErrors.length==1?"":"s"));
1506                         util.log("[red] Run with -v for details");
1507                     }
1508                     util.log("------------------------------------------");
1509                 }
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);
1516                     }
1517                     var promises = [];
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
1525                                 });
1526                             }
1527                         }
1528                     }
1529                     if (!settings.autoInstallModules) {
1530                         util.log("[red] Removing modules from config");
1531                         redNodes.cleanNodeList();
1532                     }
1533                 }
1534                 defer.resolve();
1535                 
1536                 redNodes.loadFlows();
1537             }).otherwise(function(err) {
1538                 console.log(err);
1539             });
1540             comms.start();
1541         });
1542     }).otherwise(function(err) {
1543         defer.reject(err);
1544     });
1545     
1546     return defer.promise;
1547 }
1548
1549 function stop() {
1550     redNodes.stopFlows();
1551     comms.stop();
1552 }
1553
1554 module.exports = { 
1555     init: createServer,
1556     start: start,
1557     stop: stop
1558 }
1559
1560 module.exports.__defineGetter__("app", function() { return app });
1561 module.exports.__defineGetter__("nodeApp", function() { return nodeApp });
1562 module.exports.__defineGetter__("server", function() { return server });