-
Notifications
You must be signed in to change notification settings - Fork 44
/
userDefinedCountersModel.js
180 lines (166 loc) · 7.83 KB
/
userDefinedCountersModel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
*/
define([
'underscore',
'contrail-model'
], function (_, ContrailModel) {
var userDefinedCountersModel = ContrailModel.extend({
defaultConfig: {
name : '',
pattern : ''
},
validations: {
userDefinedCounterValidation: {
'name' : function(value, attr, finalObj) {
if(value === null || value.trim() === '') {
return "Enter a Name";
}
},
'pattern': function(value, attr, finalObj) {
if(value === null || value.trim() === '') {
return "Enter a Pattern";
}
}
}
},
getCurrentGlobalSystemConfigData : function (callbackObj,deferredObj) {
var ajaxConfig = {
url : ctwc.URL_GET_CONFIG_DETAILS,
type : 'POST',
data : JSON.stringify({data:
[{type: 'global-system-configs'}]})
};
contrail.ajaxHandler(ajaxConfig, null, function(response) {
//Got the current global system config, send back
deferredObj.resolve(response);
},function(error) {
//Got error return to caller
callbackObj.error(error);
}
);
},
configureUserDefinedCounter : function (callbackObj,
currentGlobalSystemConfigData,currentUserDefinedCounterList) {
//Call the configure function in the parent model
var userDefinedCountersData = {};
userDefinedCountersData['global-system-config'] = {};
userDefinedCountersData['global-system-config']['user_defined_log_statistics'] = {};
if ((null != currentUserDefinedCounterList) &
(currentUserDefinedCounterList.length > 0)) {
userDefinedCountersData['global-system-config']['user_defined_log_statistics']
['statlist'] = currentUserDefinedCounterList;
} else {
userDefinedCountersData['global-system-config']['user_defined_log_statistics']['statlist'] = [];
}
if (null != currentGlobalSystemConfigData['uuid']) {
userDefinedCountersData['global-system-config']['uuid'] =
currentGlobalSystemConfigData['uuid'];
putData = {
"data" : [ {
"data" : {
"global-system-config" : userDefinedCountersData["global-system-config"]
},
"reqUrl" : ctwc.GLOBAL_SYSTEM_CONFIG
+ currentGlobalSystemConfigData['uuid']
} ]
}
}
var ajaxConfig = {};
ajaxConfig.type = "POST";
ajaxConfig.data = JSON.stringify(putData);
ajaxConfig.url = ctwc.URL_UPDATE_CONFIG_OBJECT;
contrail.ajaxHandler(ajaxConfig, function () {
if (contrail.checkIfFunction(callbackObj.init)) {
callbackObj.init();
}
}, function (response) {
if (contrail.checkIfFunction(callbackObj.success)) {
callbackObj.success();
}
}, function (error) {
if (contrail.checkIfFunction(callbackObj.error)) {
callbackObj.error(error);
}
});
},
deleteUserDefinedCounters : function (callbackObj,options) {
var self = this, currentGlobalSystemConfigData = {};
var deferredObj = $.Deferred();
var namesTobeDeleted = [];
var rowIndecesTobeDeleted = options.rowIndexes;
var gridDataList = options.gridData;
for (var i = 0; i < rowIndecesTobeDeleted.length ; i++) {
namesTobeDeleted.push(gridDataList[rowIndecesTobeDeleted[i]]);
}
self.getCurrentGlobalSystemConfigData(callbackObj, deferredObj);
deferredObj.done(function(response) {
currentGlobalSystemConfigData = getValueByJsonPath(response,
"0;global-system-configs;0;global-system-config", {});
var currentUserDefinedCounterList =
getValueByJsonPath(currentGlobalSystemConfigData,
"user_defined_log_statistics;statlist",[]);
currentUserDefinedCounterList = currentUserDefinedCounterList.filter(function(val) {
for(var i = 0; i < namesTobeDeleted.length ; i++) {
if(val.name === namesTobeDeleted[i].name) {
return false;
}
}
return true;
});
self.configureUserDefinedCounter(callbackObj,
currentGlobalSystemConfigData,
currentUserDefinedCounterList);
});
},
editCreateUserDefinedCounter : function (callbackObj) {
//Read the data from the page
//Get the parent/full object for global-system-config
//Modify the line to be edited and call the update function in parent model
var self = this, currentGlobalSystemConfigData = {},
newUserDefinedCounterConfig;
if(self.model().isValid(true, "userDefinedCounterValidation")) {
newUserDefinedCounterConfig =
$.extend({}, true, self.model().attributes);
newUserDefinedCounterConfig['pattern'] = newUserDefinedCounterConfig['pattern'].toString().trim();
ctwu.deleteCGridData(newUserDefinedCounterConfig);
//Fetch the current global-system-config
var deferredObj = $.Deferred();
self.getCurrentGlobalSystemConfigData(callbackObj, deferredObj);
deferredObj.done (function(response) {
currentGlobalSystemConfigData = getValueByJsonPath(response,
"0;global-system-configs;0;global-system-config", {});
var currentUserDefinedCounterList =
getValueByJsonPath(currentGlobalSystemConfigData,
"user_defined_log_statistics;statlist",[]);
if(currentUserDefinedCounterList.length === 0){
currentUserDefinedCounterList.push(newUserDefinedCounterConfig);
}
else{
var found = false;
for (var i = 0; i < currentUserDefinedCounterList.length; i++) {
if (currentUserDefinedCounterList[i]['name'] ==
newUserDefinedCounterConfig['name']) {
found = true;
currentUserDefinedCounterList[i]['pattern'] =
newUserDefinedCounterConfig['pattern'];
}
}
if (!found) {
currentUserDefinedCounterList.
push(newUserDefinedCounterConfig);
}
}
self.configureUserDefinedCounter(callbackObj,
currentGlobalSystemConfigData,
currentUserDefinedCounterList);
});
} else {
if (contrail.checkIfFunction(callbackObj.error)) {
callbackObj.error(this.getFormErrorText(ctwc.GLOBAL_COUNTERS_PREFIX_ID));
}
}
}
});
return userDefinedCountersModel;
});