RIFT-14856: launchpad UI - Logging - default category severity
[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/default-syslog-severity',
70 apiHandler: loggingAPI['config'].setDefaultSyslogSeverity
71 },
72 {
73 method: 'DELETE',
74 endpoint: '/api/config/default-syslog-severity',
75 apiHandler: loggingAPI['config'].deleteDefaultSyslogSeverity
76 },
77 {
78 method: 'PUT',
79 endpoint: '/api/config/allow-duplicate-events',
80 apiHandler: loggingAPI['config'].setAllowDuplicateEvents
81 },
82 {
83 method: 'PUT',
84 endpoint: '/api/config/deny-events',
85 apiHandler: loggingAPI['config'].setAllowDuplicateEvents
86 },
87 {
88 method: 'PUT',
89 endpoint: '/api/config/sinks',
90 apiHandler: loggingAPI['config'].setSinks
91 },
92 {
93 method: 'PUT',
94 endpoint: '/api/config/syslog-viewer',
95 apiHandler: loggingAPI['config'].setSyslogViewer
96 },
97 // Operational methods
98 {
99 method: 'GET',
100 endpoint: '/api/operational',
101 apiHandler: loggingAPI['operational'].get
102 },
103
104 // Development/testing methods
105 {
106 method: 'GET',
107 endpoint: '/api/test/roundtrip',
108 apiHandler: loggingAPI['test'].roundtrip
109 }
110 ];
111
112 // Logging routes. Initial refactoring pass at reducing code duplication
113 loggingRoutes.forEach(function(route) {
114 registerRoute(router, route.method, route.endpoint, route.apiHandler);
115 });
116
117 module.exports = router;
118
119
120 /**
121 * Default route callback function
122 */
123 function routeCallback(apiHandler) {
124 return function(req, res) {
125 apiHandler(req).then(function(data) {
126 utils.sendSuccessResponse(data, res);
127 }, function(error) {
128 utils.sendErrorResponse(error, res);
129 });
130 };
131 }
132
133
134 /**
135 * register the route
136 */
137 function registerRoute(app, method, endpoint, apiHandler) {
138 var methodUp = method.toUpperCase();
139 // This is the explict version that does not use reflection to cast the
140 // HTTP method to the corresponding express app function
141 if (methodUp === 'GET') {
142 app.get(endpoint, cors(), routeCallback(apiHandler));
143 } else if (methodUp === 'PUT') {
144 app.put(endpoint, cors(), routeCallback(apiHandler));
145 } else if (methodUp === 'POST') {
146 app.post(endpoint, cors(), routeCallback(apiHandler));
147 } else if (methodUp === 'DELETE') {
148 app.delete(endpoint, cors(), routeCallback(apiHandler));
149 } else {
150 console.log("ERROR: Unsupported HTTP method: %s", method);
151 }
152
153 }
154
155
156