3 * Copyright 2016 RIFT.IO Inc
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 var _
= require('lodash');
22 Support
.globalDefaultSeverity = function() {
26 Support
.severities = function() {
40 * Class to convert RESTConf data to logging plugin
45 LoggingConfigDecoder = function(debugMode
) {
46 this.debugMode
= debugMode
|| false
49 LoggingConfigDecoder
.prototype.categories = function(data
) {
50 if (data
&& data
.categories
&& data
.categories
.category
) {
51 return data
.categories
.category
;
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
})
63 { category
: name
, severity
: null };
66 throw("Logging categories not available");
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');
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'];
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
) {
101 LoggingConfigDecoder
.prototype.decode = function(loggingConfig
, loggingOperational
) {
102 // tack on raw retrieved config and operational data while we are in
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
109 if (loggingOperational
.data
) {
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
)
122 if (this.debugMode
) {
123 // carry-on original request data
124 results
.loggingConfig
= loggingConfig
;
125 results
.loggingOperational
= loggingOperational
;
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
142 LoggingConfigEncoder = function() {
146 LoggingConfigEncoder
.prototype.denyEvents = function(data
) {
148 if (data
.denyEventIDs
) {
150 // TODO: sort keys and filter nulls out
151 data
.denyEventIDs
.forEach(function(eventID
) {
153 events
.push({ "event-Id": eventID
})
156 if (events
.length
> 0) {
157 return { event
: events
};
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
170 LoggingConfigEncoder
.prototype.allow = function(data
) {
171 if (data
.allowDuplicateEvents
&& data
.allowDuplicateEvents
.toUpperCase() == "TRUE") {
172 return { duplicate
: "events" };
177 LoggingConfigEncoder
.prototype.consoleData = function(data
) {
178 if (data
.console
&& data
.console
.on
) {
185 LoggingConfigEncoder
.prototype.encode = function(apiData
) {
187 var restConfData
= {};
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
);
193 restConfData
['deny'] = denyData
;
196 var allowData
= this.allow(apiData
);
198 restConfData
['allow'] = allowData
;
200 var consoleData
= this.consoleData(apiData
);
202 restConfData
['console'] = consoleData
;
204 restConfData
['sink'] = apiData
.sinks
;
205 restConfData
['syslog-viewer'] = apiData
.syslogviewer
;
206 restConfData
['default-severity'] = apiData
.defaultSeverities
;
212 LoggingConfigDecoder
: LoggingConfigDecoder
,
213 LoggingConfigEncoder
: LoggingConfigEncoder
,