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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
/*-
* ============LICENSE_START=======================================================
* ONAP CLAMP
* ================================================================================
* Copyright (C) 2017-2019 AT&T Intellectual Property. All rights
* reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END============================================
* ===================================================================
*
*/
app
.controller(
'operationalPolicyCtrl',
[
'$scope',
'$rootScope',
'$uibModalInstance',
'data',
'operationalPolicyService',
'dialogs',
function($scope, $rootScope, $uibModalInstance, data, operationalPolicyService, dialogs) {
console.log("//////operationalPolicyCtrl");
$scope.policy_ids = []
var allPolicies = {};
$scope.guardType = "GUARD_MIN_MAX";
$scope.number = 0;
$scope.clname = "";
$scope.guard_ids = [];
function getAllFormId() {
return Array.from(document.getElementsByClassName("formId"));
}
function searchActiveFormId() {
var formArray = getAllFormId();
for (var i = 0, max = formArray.length; i < max; i++) {
console.log("Search active FormId, current element " + formArray[i].id);
if (formArray[i].style.display !== "none") {
console.log("Active form is:" + formArray[i].id);
return formArray[i];
}
}
console.log("No active formId found !");
}
function add_one_more() {
console.log("add one more");
$("#nav_Tabs li").removeClass("active");
// FormSpan contains a block of the form that is not being
// displayed. We will create clones of that and add them to tabs
var form = $("#formSpan").clone(true, true)
var count = 0;
// Each new tab will have the formId class attached to it. This way
// we can track how many forms we currently have out there and
// assign listeners to them
if ($(".formId").length > 0) {
var greatest = 0;
var s = $(".formId");
for (var i = 0; i < s.length; i++) {
if (parseInt($(s[i]).attr("id").substring(6)) > greatest) {
greatest = parseInt($(s[i]).attr("id").substring(6))
}
}
count = greatest + 1;
$("#properties_tab").append(('<span class="formId" id="formId' + count + '"></span>'));
} else {
count++;
$("#properties_tab").append('<span class="formId" id="formId1"></span>');
}
$("#add_one_more")
.parent()
.before(
' <li class="active"><a id="go_properties_tab'
+ count
+ '">new</a><button id="tab_close'
+ count
+ '" type="button" class="close tab-close-popup" aria-hidden="true" style="margin-top: -30px;margin-right: 5px">×</button></li>');
$("#formId" + count).append(form.children());
$(".formId").not($("#formId" + count)).css("display", "none");
addCustListen(count);
$("#formId" + count + " #id").val("new");
$("#formId" + count + " #clname").val($scope.clname);
return count;
}
function addCustListen(count) {
$('#go_properties_tab' + count).click(function(event) {
$("#nav_Tabs li").removeClass("active");
$(this).parent().addClass("active");
$("#formId" + count).css("display", "")
$(".formId").not($("#formId" + count)).css("display", "none")
})
$('#tab_close' + count).click(function(event) {
$(this).parent().remove();
$scope.policy_ids.splice($scope.policy_ids.indexOf($("#formId" + count + " #id").val()), 1);
$("#formId" + count).remove();
})
}
function greatestIdNum() {
var greatest = 0;
var s = $(".formId");
for (var i = 0; i < s.length; i++) {
if (parseInt($(s[i]).attr("id").substring(6)) > greatest) {
greatest = parseInt($(s[i]).attr("id").substring(6))
}
}
return greatest;
}
function serializeElement(element) {
var o = {};
var a = element.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [ o[this.name] ];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
}
function savePolicyLocally() {
var polForm = {};
var clPolForm = {};
clPolForm = serializeElement($("#operationalPolicyHeaderForm"));
allPolicies['guard_policies'] = {};
var policiesArray = []
$.each($(".formId"), function() {
var policyProperties = serializeElement($("#" + this.id + " .policyProperties"));
policyProperties["target"] = serializeElement($("#" + this.id + " .policyTarget"))
policiesArray.push(policyProperties);
// Now get the Guard
if ($("#" + this.id + " #enableGuardPolicy").is(':checked')) {
var guardPolicyBody = serializeElement($("#" + this.id + " .guardProperties"));
var guardPolicyId = guardPolicyBody['id'];
delete guardPolicyBody['id'];
allPolicies['guard_policies'][guardPolicyId] = guardPolicyBody;
$scope.guard_ids.push(guardPolicyId);
}
});
polForm['controlLoop'] = clPolForm;
polForm['policies'] = policiesArray;
allPolicies['operational_policy'] = polForm;
}
function add_new_policy() {
console.log("add new policy");
// remove old gui forms
for (var i = 1; i < ($(".formId").length + 1); i++) {
$("#go_properties_tab" + i).parent().remove();
}
$(".formId").remove();
$("#add_one_more").click();
}
function configureComponents(allPolicies) {
console.log("load properties to op policy");
// Set the header
$.each($('#operationalPolicyHeaderForm').find('.form-control'), function() {
$(this).val(allPolicies['operational_policy']['controlLoop'][this.id]);
});
// Set the sub-policies
$.each(allPolicies['operational_policy']['policies'], function(opPolicyElemIndex, opPolicyElemValue) {
var formNum = add_one_more();
$.each($('.policyProperties').find('.form-control'), function(opPolicyPropIndex, opPolicyPropValue) {
$("#formId" + formNum + " .policyProperties").find("#" + opPolicyPropValue.id).val(
allPolicies['operational_policy']['policies'][opPolicyElemIndex][opPolicyPropValue.id]);
});
// Initial TargetResourceId options
initTargetResourceIdOptions(allPolicies['operational_policy']['policies'][opPolicyElemIndex]['target']['type'], formNum);
$.each($('.policyTarget').find('.form-control'), function(opPolicyTargetPropIndex, opPolicyTargetPropValue) {
$("#formId" + formNum + " .policyTarget").find("#" + opPolicyTargetPropValue.id).val(
allPolicies['operational_policy']['policies'][opPolicyElemIndex]['target'][opPolicyTargetPropValue.id]);
});
// update the current tab label
$("#go_properties_tab" + formNum).text(
allPolicies['operational_policy']['policies'][opPolicyElemIndex]['id']);
// Check if there is a guard set for it
$.each(allPolicies['guard_policies'], function(guardElemId, guardElemValue) {
if (guardElemValue.recipe === $($("#formId" + formNum + " #recipe")[0]).val()) {
// Found one, set all guard prop
$.each($('.guardProperties').find('.form-control'), function(guardPropElemIndex,
guardPropElemValue) {
guardElemValue['id'] = guardElemId;
$("#formId" + formNum + " .guardProperties").find("#" + guardPropElemValue.id).val(
guardElemValue[guardPropElemValue.id]);
});
// And finally enable the flag
$("#formId" + formNum + " #enableGuardPolicy").prop("checked", true);
}
});
});
}
function initTargetResourceIdOptions (targetType, formNum) {
var recipe = $("#formId" + formNum + "#recipe").val();
$("#formId" + formNum + " #resourceId").empty();
$("#formId" + formNum + " #resourceId").append($('<option></option>').val("").html("-- choose an option --"));
var resourceVnf = getResourceDetailsVfProperty();
if (targetType == "VNF" && (null !== resourceVnf || undefined !== resourceVnf)) {
for ( var prop in resourceVnf) {
var name = resourceVnf[prop]["name"];
$("#formId" + formNum + " #resourceId").append($('<option></option>').val(name).html(name));
}
}
var resourceVFModule = getResourceDetailsVfModuleProperty();
if (targetType == "VFModule" && (null !== resourceVFModule || undefined !== resourceVFModule)) {
if (recipe == 'VF Module Create' || recipe == 'VF Module Delete') {
for ( var prop in resourceVFModule) {
if (resourceVFModule[prop]["isBase"] == false) {
$("#formId" + formNum + " #resourceId").append($('<option></option>').val(resourceVFModule[prop]["vfModuleModelName"]).html(resourceVFModule[prop]["vfModuleModelName"]));
}
}
} else {
for ( var prop in resourceVFModule) {
$("#formId" + formNum + " #resourceId").append($('<option></option>').val(resourceVFModule[prop]["vfModuleModelName"]).html(resourceVFModule[prop]["vfModuleModelName"]));
}
}
}
}
function initTargetModelAttributes (formNum) {
$("#formId" + formNum + " #modelName").val("");
$("#formId" + formNum + " #modelInvariantId").val("");
$("#formId" + formNum + " #modelVersionId").val("");
$("#formId" + formNum + " #modelVersion").val("");
$("#formId" + formNum + " #modelCustomizationId").val("");
}
$scope.initTargetResourceId = function(event) {
var formNum = $(event.target).closest('.formId').attr('id').substring(6);
initTargetModelAttributes(formNum);
var type = $(event.target).val();
initTargetResourceIdOptions(type, formNum);
}
$scope.changeTargetResourceId = function(event) {
var formNum = $(event.target).closest('.formId').attr('id').substring(6);
initTargetModelAttributes(formNum);
var resourceVFModule = getResourceDetailsVfModuleProperty();
var type = $("#formId" + formNum +" #type").val();
var recipe = $("#formId" + formNum +" #recipe").val();
if (type == "VFModule" && (null !== resourceVFModule || undefined !== resourceVFModule)
&& (recipe == 'VF Module Create' || recipe == 'VF Module Delete')) {
for ( var prop in resourceVFModule) {
if (prop == $(event.target).val()) {
$("#formId" + formNum +" #modelName").val(resourceVFModule[prop]["vfModuleModelName"]);
$("#formId" + formNum +" #modelInvariantId").val(resourceVFModule[prop]["vfModuleModelInvariantUUID"]);
$("#formId" + formNum +" #modelVersionId").val(resourceVFModule[prop]["vfModuleModelUUID"]);
$("#formId" + formNum +" #modelVersion").val(resourceVFModule[prop]["vfModuleModelVersion"]);
$("#formId" + formNum +" #modelCustomizationId").val(resourceVFModule[prop]["vfModuleModelCustomizationUUID"]);
$("#formId" + formNum +" #metadata *").prop('disabled', false);
}
}
} else {
$("#formId" + formNum +" #metadata *").prop('disabled', true);
}
}
$scope.changeGuardPolicyType = function() {
var formItemActive = searchActiveFormId();
if (formItemActive === undefined)
return;
if ($("#" + formItemActive.id + " #guardPolicyType").val() === "GUARD_MIN_MAX") {
$("#" + formItemActive.id + " #minMaxGuardPolicyDiv").show();
$("#" + formItemActive.id + " #frequencyLimiterGuardPolicyDiv").hide();
} else if ($("#" + formItemActive.id + " #guardPolicyType").val() === "GUARD_YAML") {
$("#" + formItemActive.id + " #minMaxGuardPolicyDiv").hide();
$("#" + formItemActive.id + " #frequencyLimiterGuardPolicyDiv").show();
}
}
$scope.initPolicySelect = function() {
if (allPolicies['operational_policy'] === undefined || allPolicies['operational_policy'] === null) {
allPolicies = getOperationalPolicyProperty();
}
// Provision all policies ID first
if ($scope.policy_ids.length == 0 && allPolicies['operational_policy'] != undefined) {
$.each(allPolicies['operational_policy']['policies'], function() {
$scope.policy_ids.push(this['id']);
});
}
}
$scope.init = function() {
$(function() {
$scope.clname = getLoopName();
$("#add_one_more").click(function(event) {
console.log("add one more");
event.preventDefault();
$scope.policy_ids.push($("#formId" + add_one_more() + " #id").val());
});
if (allPolicies['operational_policy'] !== undefined && allPolicies['operational_policy'] !== null) {
// load properties
console.log("load properties");
configureComponents(allPolicies);
} else {
console.log("create new policy");
add_new_policy();
}
$("#savePropsBtn").click(function(event) {
console.log("save properties triggered");
savePolicyLocally();
for (var i = 0; i <= $scope.guard_ids.length; i++) {
for (var j = i; j <= $scope.guard_ids.length; j++) {
if (i != j && $scope.guard_ids[i] == $scope.guard_ids[j]) {
// duplacated guard policy id exist
alert("The guard policy ID should be unique.");
return;
}
}
}
angular.element(document.getElementById('formSpan')).scope().submitForm(allPolicies);
$("#close_button").click();
});
});
}
$scope.init();
$scope.updateGuardRecipe = function(event) {
var formNum = $(event.target).closest('.formId').attr('id').substring(6);
// Get the second recipe (guard one) and update it
$($("#formId" + formNum + " #recipe")[1]).val($(event.target).val());
}
$scope.updateGuardActor = function(event) {
var formNum = $(event.target).closest('.formId').attr('id').substring(6);
// Get the second actor (guard one) and update it
$($("#formId" + formNum + " #actor")[1]).val($(event.target).val());
}
// When we change the name of a policy
$scope.updateTabLabel = function(event) {
// update policy id structure
var formNum = $(event.target).closest('.formId').attr('id').substring(6);
$scope.policy_ids.splice($scope.policy_ids.indexOf($("#formId" + formNum + " #id").val()), 1);
$scope.policy_ids.push($(event.target).val());
// Update the tab now
$("#go_properties_tab" + formNum).text($(event.target).val());
}
$scope.close = function() {
console.log("close");
$uibModalInstance.close("closed");
};
$scope.submitForm = function(obj) {
var operationalPolicies = getOperationalPolicies();
if (obj !== null) {
operationalPolicies[0]["configurationsJson"] = obj;
}
operationalPolicyService.saveOpPolicyProperties(operationalPolicies).then(function(pars) {
updateOpPolicyProperties(operationalPolicies);
}, function(data) {
});
};
} ]);
|