Updates for ODL Neon
[ccsdk/distribution.git] / dgbuilder / red / library.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 util = require("util");
18
19 var redApp = null;
20 var storage = null;
21
22 function init() {
23     redApp = require("./server").app;
24     storage = require("./storage");
25     
26     // -------- Flow Library --------
27     redApp.post(new RegExp("/library/flows\/(.*)"), function(req,res) {
28             var fullBody = '';
29             req.on('data', function(chunk) {
30                 fullBody += chunk.toString();
31             });
32             req.on('end', function() {
33                 storage.saveFlow(req.params[0],fullBody).then(function() {
34                     res.send(204);
35                 }).otherwise(function(err) {
36                     util.log("[red] Error loading flow '"+req.params[0]+"' : "+err);
37                     if (err.message.indexOf('forbidden') === 0) {
38                         res.send(403);
39                         return;
40                     }
41                     res.send(500);
42                 });
43             });
44     });
45     
46     redApp.get("/library/flows",function(req,res) {
47             storage.getAllFlows().then(function(flows) {
48                 res.json(flows);
49             });
50     });
51     
52     redApp.get(new RegExp("/library/flows\/(.*)"), function(req,res) {
53             storage.getFlow(req.params[0]).then(function(data) {
54                 res.set('Content-Type', 'application/json');
55                 res.send(data);
56             }).otherwise(function(err) {
57                 if (err) {
58                     util.log("[red] Error loading flow '"+req.params[0]+"' : "+err);
59                     if (err.message.indexOf('forbidden') === 0) {
60                         res.send(403);
61                         return;
62                     }
63                 }
64                 res.send(404);
65             });
66     });
67     
68     // ------------------------------
69 }    
70
71 function createLibrary(type) {
72     
73     redApp.get(new RegExp("/library/"+type+"($|\/(.*))"),function(req,res) {
74             var path = req.params[1]||"";
75             storage.getLibraryEntry(type,path).then(function(result) {
76                 if (typeof result === "string") {
77                     res.writeHead(200, {'Content-Type': 'text/plain'});
78                     res.write(result);
79                     res.end(); 
80                 } else {
81                     res.json(result);
82                 }
83             }).otherwise(function(err) {
84                 if (err) {
85                     util.log("[red] Error loading library entry '"+path+"' : "+err);
86                     if (err.message.indexOf('forbidden') === 0) {
87                         res.send(403);
88                         return;
89                     }
90                 }
91                 res.send(404);
92             });
93     });
94     
95     redApp.post(new RegExp("/library/"+type+"\/(.*)"),function(req,res) {
96             var path = req.params[0];
97             var fullBody = '';
98             req.on('data', function(chunk) {
99                     fullBody += chunk.toString();
100             });
101             req.on('end', function() {
102                     storage.saveLibraryEntry(type,path,req.query,fullBody).then(function() {
103                         res.send(204);
104                     }).otherwise(function(err) {
105                         util.log("[red] Error saving library entry '"+path+"' : "+err);
106                         if (err.message.indexOf('forbidden') === 0) {
107                             res.send(403);
108                             return;
109                         }
110                         res.send(500);
111                     });
112             });
113     });
114 }
115
116 module.exports.init = init;
117 module.exports.register = createLibrary;