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
43 var LoggingConfigDecoder
= {};
44 var LoggingConfigEncoder
= {};
46 LoggingConfigDecoder = function(debugMode
) {
47 this.debugMode
= debugMode
|| false
50 LoggingConfigDecoder
.prototype.categories = function(data
) {
51 if (data
&& data
.categories
&& data
.categories
.category
) {
52 return data
.categories
.category
;
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
})
64 { category
: name
, severity
: null };
67 throw("Logging categories not available");
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');
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'];
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
) {
102 LoggingConfigDecoder
.prototype.decode = function(loggingConfig
, loggingOperational
) {
103 // tack on raw retrieved config and operational data while we are in
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
110 if (loggingOperational
.data
) {
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
)
123 if (this.debugMode
) {
124 // carry-on original request data
125 results
.loggingConfig
= loggingConfig
;
126 results
.loggingOperational
= loggingOperational
;
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
143 LoggingConfigEncoder = function() {
147 LoggingConfigEncoder
.prototype.denyEvents = function(data
) {
149 if (data
.denyEventIDs
) {
151 // TODO: sort keys and filter nulls out
152 data
.denyEventIDs
.forEach(function(eventID
) {
154 events
.push({ "event-Id": eventID
})
157 if (events
.length
> 0) {
158 return { event
: events
};
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
171 LoggingConfigEncoder
.prototype.allow = function(data
) {
172 if (data
.allowDuplicateEvents
&& data
.allowDuplicateEvents
.toUpperCase() == "TRUE") {
173 return { duplicate
: "events" };
178 LoggingConfigEncoder
.prototype.consoleData = function(data
) {
179 if (data
.console
&& data
.console
.on
) {
186 LoggingConfigEncoder
.prototype.encode = function(apiData
) {
188 var restConfData
= {};
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
);
194 restConfData
['deny'] = denyData
;
197 var allowData
= this.allow(apiData
);
199 restConfData
['allow'] = allowData
;
201 var consoleData
= this.consoleData(apiData
);
203 restConfData
['console'] = consoleData
;
205 restConfData
['sink'] = apiData
.sinks
;
206 restConfData
['syslog-viewer'] = apiData
.syslogviewer
;
207 restConfData
['default-severity'] = apiData
.defaultSeverities
;
213 LoggingConfigDecoder
: LoggingConfigDecoder
,
214 LoggingConfigEncoder
: LoggingConfigEncoder
,