RIFT-14942: added check for loggingEvents
[osm/UI.git] / skyquake / plugins / logging / api / transforms.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 _ = require('lodash');
19
20 var Support={};
21
22 Support.globalDefaultSeverity = function() {
23 return 'error';
24 }
25
26 Support.severities = function() {
27 return [
28 null,
29 "emergency",
30 "alert",
31 "critical",
32 "error",
33 "warning",
34 "notice",
35 "info",
36 "debug"];
37 }
38
39 /**
40 * Class to convert RESTConf data to logging plugin
41 */
42
43
44
45 LoggingConfigDecoder = function(debugMode) {
46 this.debugMode = debugMode || false
47 }
48
49 LoggingConfigDecoder.prototype.categories = function(data) {
50 if (data && data.categories && data.categories.category) {
51 return data.categories.category;
52 } else {
53 return [];
54 }
55 }
56 LoggingConfigDecoder.prototype.defaultSeverities = function (data) {
57 var globalDefaultSeverity = Support.globalDefaultSeverity();
58 if (data.categories && data.categories.category) {
59 var defaultSeverities = data["default-severity"] || [];
60 return data.categories.category.map(function(name) {
61 return _.find(defaultSeverities, {category: name})
62 ||
63 { category: name, severity: null };
64 });
65 } else {
66 throw("Logging categories not available");
67 }
68 }
69
70 LoggingConfigDecoder.prototype.allowDuplicateEvents = function (data) {
71 // if the property does not exist, then value is false
72 // otherwise value is true
73 //return (data.hasOwnProperty('allow') && data.allow.duplicate == 'events');
74 return (data.allow && data.allow.duplicate == 'events');
75 }
76
77 // NOTE: confd can also set event ids in a range
78 LoggingConfigDecoder.prototype.denyEventIDs = function(data) {
79 if (data.deny && data.deny.event) {
80 return data.deny.event.map(function(event, index) {
81 return event['event-Id'];
82 });
83 } else {
84 return [];
85 }
86 }
87
88 LoggingConfigDecoder.prototype.consoleData = function(data) {
89 //console.log("LoggingConfigDecoder.consoleData=", data.console);
90 // NOTE: We may need to fill in the data.console.on|off if that is not
91 // present when filters are present
92 if (data && data.console) {
93 return data.console;
94 } else {
95 return {
96 off: [ null ]
97 };
98 }
99 }
100
101 LoggingConfigDecoder.prototype.decode = function(loggingConfig, loggingOperational) {
102 // tack on raw retrieved config and operational data while we are in
103 // development
104
105 // TODO: robustify: check inputs if they have a 'data' property, then set
106 // local var to the data property, else just use the passed in param
107 // this means we don't have to pass 'loggingOperational.data' to the methods
108 // we call
109 if (loggingOperational.data) {
110 var results = {
111 categories: this.categories(loggingOperational.data),
112 defaultSeverities: this.defaultSeverities(loggingOperational.data),
113 severities: Support.severities(),
114 globalDefaultSeverity: Support.globalDefaultSeverity(),
115 syslogviewer: loggingOperational.data['syslog-viewer'],
116 sinks: loggingOperational.data.sink,
117 allowDuplicateEvents: this.allowDuplicateEvents(loggingOperational.data),
118 denyEventIDs: this.denyEventIDs(loggingOperational.data),
119 console: this.consoleData(loggingOperational.data)
120 }
121
122 if (this.debugMode) {
123 // carry-on original request data
124 results.loggingConfig = loggingConfig;
125 results.loggingOperational = loggingOperational;
126 }
127 return results;
128 } else {
129 return {};
130 }
131 }
132
133 // Logging encoding
134
135 /**
136 * LoggingConfigEncoder transform the API logging configuration data to the
137 * format required to PUT to the restconf /api/config/logging endpoint
138 * The initial version is implemented in an imperative way: Explicit coding
139 * of specific fields. After this works, we can refactor to do a more
140 * declarative approach
141 */
142 LoggingConfigEncoder = function() {
143
144 }
145
146 LoggingConfigEncoder.prototype.denyEvents = function(data) {
147
148 if (data.denyEventIDs) {
149 var events = [];
150 // TODO: sort keys and filter nulls out
151 data.denyEventIDs.forEach(function(eventID) {
152 if (eventID) {
153 events.push({ "event-Id": eventID })
154 }
155 });
156 if (events.length > 0) {
157 return { event: events };
158 } else {
159 return null;
160 }
161 } else {
162 return null;
163 }
164 }
165
166 /**
167 * in the Yang model, allow duplicate events flag is triggers by the presence
168 * or absence of the { "allow": { "duplicate": "events" }} key/value hierarchy
169 */
170 LoggingConfigEncoder.prototype.allow = function(data) {
171 if (data.allowDuplicateEvents && data.allowDuplicateEvents.toUpperCase() == "TRUE") {
172 return { duplicate: "events" };
173 } else {
174 return null;
175 }
176 }
177 LoggingConfigEncoder.prototype.consoleData = function(data) {
178 if (data.console && data.console.on) {
179 return data.console;
180 } else {
181 return null;
182 }
183 }
184
185 LoggingConfigEncoder.prototype.encode = function(apiData) {
186
187 var restConfData = {};
188 var n = 1;
189 // Only assign to the following keys if we have values
190 // NOTE: This may change with the implementation of the RIFT REST PUT
191 var denyData = this.denyEvents(apiData);
192 if (denyData) {
193 restConfData['deny'] = denyData;
194 }
195 console.log(n++);
196 var allowData = this.allow(apiData);
197 if (allowData) {
198 restConfData['allow'] = allowData;
199 }
200 var consoleData = this.consoleData(apiData);
201 if (consoleData) {
202 restConfData['console'] = consoleData;
203 }
204 restConfData['sink'] = apiData.sinks;
205 restConfData['syslog-viewer'] = apiData.syslogviewer;
206 restConfData['default-severity'] = apiData.defaultSeverities;
207
208 return restConfData;
209 }
210
211 module.exports = {
212 LoggingConfigDecoder: LoggingConfigDecoder,
213 LoggingConfigEncoder: LoggingConfigEncoder,
214 Support: Support
215 }