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