Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / logging / routes.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 var router = require('express').Router();
19 var cors = require('cors');
20 var utils = require('../../framework/core/api_utils/utils.js')
21
22 var loggingAPI = require('./api/logging.js');
23
24 var loggingRoutes = [
25 // Config methods
26 // config GET methods
27 {
28 method: 'GET',
29 endpoint: '/api/aggregate',
30 apiHandler: loggingAPI['aggregate'].get
31 },
32 {
33 method: 'GET',
34 endpoint: '/api/config',
35 apiHandler: loggingAPI['config'].get
36 },
37 // Config modify methods
38 {
39 method: 'PUT',
40 endpoint: '/api/config',
41 apiHandler: loggingAPI['config'].set
42 },
43 {
44 method: 'PUT',
45 endpoint: '/api/aggregate',
46 apiHandler: loggingAPI['aggregate'].set
47 },
48 {
49 method: 'PUT',
50 endpoint: '/api/config/console',
51 apiHandler: loggingAPI['config'].setConsole
52 },
53 {
54 method: 'PUT',
55 endpoint: '/api/config/filter',
56 apiHandler: loggingAPI['config'].setFilter
57 },
58 {
59 method: 'PUT',
60 endpoint: '/api/config/default-severity',
61 apiHandler: loggingAPI['config'].setDefaultSeverity
62 },{
63 method: 'DELETE',
64 endpoint: '/api/config/default-severity',
65 apiHandler: loggingAPI['config'].deleteDefaultSeverity
66 },
67 {
68 method: 'PUT',
69 endpoint: '/api/config/allow-duplicate-events',
70 apiHandler: loggingAPI['config'].setAllowDuplicateEvents
71 },
72 {
73 method: 'PUT',
74 endpoint: '/api/config/deny-events',
75 apiHandler: loggingAPI['config'].setAllowDuplicateEvents
76 },
77 {
78 method: 'PUT',
79 endpoint: '/api/config/sinks',
80 apiHandler: loggingAPI['config'].setSinks
81 },
82 {
83 method: 'PUT',
84 endpoint: '/api/config/syslog-viewer',
85 apiHandler: loggingAPI['config'].setSyslogViewer
86 },
87 // Operational methods
88 {
89 method: 'GET',
90 endpoint: '/api/operational',
91 apiHandler: loggingAPI['operational'].get
92 },
93
94 // Development/testing methods
95 {
96 method: 'GET',
97 endpoint: '/api/test/roundtrip',
98 apiHandler: loggingAPI['test'].roundtrip
99 }
100 ];
101
102 // Logging routes. Initial refactoring pass at reducing code duplication
103 loggingRoutes.forEach(function(route) {
104 registerRoute(router, route.method, route.endpoint, route.apiHandler);
105 });
106
107 module.exports = router;
108
109
110 /**
111 * Default route callback function
112 */
113 function routeCallback(apiHandler) {
114 return function(req, res) {
115 apiHandler(req).then(function(data) {
116 utils.sendSuccessResponse(data, res);
117 }, function(error) {
118 utils.sendErrorResponse(error, res);
119 });
120 };
121 }
122
123
124 /**
125 * register the route
126 */
127 function registerRoute(app, method, endpoint, apiHandler) {
128 var methodUp = method.toUpperCase();
129 // This is the explict version that does not use reflection to cast the
130 // HTTP method to the corresponding express app function
131 if (methodUp === 'GET') {
132 app.get(endpoint, cors(), routeCallback(apiHandler));
133 } else if (methodUp === 'PUT') {
134 app.put(endpoint, cors(), routeCallback(apiHandler));
135 } else if (methodUp === 'POST') {
136 app.post(endpoint, cors(), routeCallback(apiHandler));
137 } else if (methodUp === 'DELETE') {
138 app.delete(endpoint, cors(), routeCallback(apiHandler));
139 } else {
140 console.log("ERROR: Unsupported HTTP method: %s", method);
141 }
142
143 }
144
145
146