2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
25 'operationalPolicyCtrl',
31 'operationalPolicyService',
33 function($scope, $rootScope, $uibModalInstance, data, operationalPolicyService, dialogs) {
35 console.log("//////operationalPolicyCtrl");
36 var parent_policy = {}
38 var loadingId = false;
42 $scope.guardType="GUARD_MIN_MAX";
43 $scope.targetResource="";
44 function getAllFormId() {
46 return Array.from(document.getElementsByClassName("formId"));
48 function searchActiveFormId() {
50 var formArray = getAllFormId();
51 for (var i = 0, max = formArray.length; i < max; i++) {
52 console.log("Search active FormId, current element " + formArray[i].id);
53 if (formArray[i].style.display !== "none") {
54 console.log("Active form is:" + formArray[i].id);
58 console.log("No active formId found !");
60 function initTargetResourceId() {
62 if (vf_Services !== null && vf_Services !== undefined) {
63 // Set all the Resource Invariant UUID in the target resource ID
64 // list (+Empty and other)
65 Object.keys(vf_Services["shared"]["byVf"]).forEach(function(key) {
67 $("#targetResourceId").append($('<option></option>').val(key).html(key));
71 // load recipes for a chosen policy
72 function disperseConfig(policyObj, id) {
74 console.log("disperseConfig with id:" + id);
76 // remove old gui forms
77 for (var i = 1; i < ($(".formId").length + 1); i++) {
78 $("#go_properties_tab" + i).parent().remove();
80 $(".formId").remove();
81 if (policyObj !== undefined && policyObj[id] !== undefined) {
82 var el = policyObj[id][3]['policyConfigurations']
83 for (var i = 0; i < el.length; i++) {
85 var num = add_one_more();
86 console.log("number is=:" + num);
88 for (var j = 0; j < el[i].length; j++) {
89 console.log("attr:" + el[i][j].name + "; value is:" + el[i][j].value);
90 if (el[i][j].hasOwnProperty("name")) {
91 $("#formId" + num + " #" + el[i][j].name).val(el[i][j].value);
92 if (el[i][j].name === "_id") {
93 console.log("formId num:" + num + "; value is:" + el[i][j].value);
94 policy_ids["#formId" + num] = el[i][j].value
96 if (el[i][j].name === 'parentPolicy')
97 parent_policy[num] = el[i][j].value
98 if (el[i][j].name === 'recipe' && el[i][j].value.toString() !== '') {
99 $("#go_properties_tab" + num).text(el[i][j].value)
101 if (el[i][j].name === "targetResourceIdOther" && el[i][j].value.toString() !== '') {
102 // Add the entry and set it
103 $("#formId" + num + " #targetResourceId").append(
104 $('<option></option>').val($("#formId" + num + " #targetResourceIdOther").val()).html(
105 $("#formId" + num + " #targetResourceIdOther").val()));
106 $("#formId" + num + " #targetResourceId").val(
107 $("#formId" + num + " #targetResourceIdOther").val());
109 $scope.changeGuardPolicyType();
113 // Adding all the ids for parent policy options
114 for (var i = 1; i <= $(".formId").length; i++) {
115 for (k in policy_ids) {
116 if ($("#formId" + i + " #_id").val() !== policy_ids[k].toString()
117 && $(k + " #recipe").val() !== undefined && $(k + " #recipe").val() !== "") {
118 $("#formId" + i + " #parentPolicy").append(
119 "<option value=\"" + policy_ids[k] + "\">" + $(k + " #recipe").val() + "</option>");
123 for (k in parent_policy) {
124 $("#formId" + k + " #parentPolicy").val(parent_policy[k]);
125 if ($("#formId" + k + " #parentPolicy").val() == "") {
126 $("#formId" + k + " #parentPolicyConditions").multiselect("disable");
128 $("#formId" + k + " #parentPolicyConditions").multiselect("enable");
130 // force the change event
131 $("#formId" + k + " #parentPolicy").change();
133 // Now load all component with the right value defined in
135 for (headInd in policyObj[id]) {
136 if (!(policyObj[id][headInd].hasOwnProperty("policyConfigurations"))) {
137 $("#" + policyObj[id][headInd].name).val(policyObj[id][headInd].value);
143 $('select[multiple] option').each(function() {
145 var input = $('input[value="' + $(this).val() + '"]');
146 input.prop('disabled', true);
147 input.parent('li').addClass('disabled');
149 $('input[value="multiselect-all"]').prop('disabled', true).parent('li').addClass('disabled');
152 function addSelectListen(count) {
154 var onSelectChange = function() {
156 var opselected = this.selectedOptions[0].text;
157 if (this.id == "recipe") {
158 if (opselected !== "") {
159 var polCount = $(this).closest("[id^='formId']").attr("id").substring(6);
160 $(this).closest(".policyPanel").find("#go_properties_tab" + polCount).text(opselected);
162 $(this).closest("[id^='go_properties_tab']").text("Policy");
165 if (this.id == "parentPolicy") {
166 var ppCond = $(this).closest("[id^='formId']").find("#parentPolicyConditions");
167 if (opselected == "") {
168 ppCond.multiselect("clearSelection");
169 ppCond.multiselect("disable");
171 ppCond.multiselect("enable");
175 $("#formId" + count + " select").each(function() {
177 this.change = onSelectChange;
180 // This is ensure there are no repeated keys in the map
181 function noRepeats(form) {
183 // triggered per policy.
185 for (var i = 0; i < form.length; i++) {
186 if (select[form[i].name] === undefined)
187 select[form[i].name] = []
188 select[form[i].name].push(form[i].value);
199 function add_one_more() {
201 console.log("add one more");
203 $("#nav_Tabs li").removeClass("active");
204 // FormSpan contains a block of the form that is not being
205 // displayed. We will create clones of that and add them to tabs
206 var form = $($("#formSpan").children()[0]).clone()
208 // Each new tab will have the formId class attached to it. This way
209 // we can track how many forms we currently have out there and
210 // assign listeners to them
211 if ($(".formId").length > 0) {
213 var s = $(".formId");
214 for (var i = 0; i < s.length; i++) {
215 if (parseInt($(s[i]).attr("id").substring(6)) > greatest) {
216 greatest = parseInt($(s[i]).attr("id").substring(6))
219 count = greatest + 1;
220 $("#properties_tab").append(('<span class="formId" id="formId' + count + '"></span>'));
223 $("#properties_tab").append('<span class="formId" id="formId1"></span>');
225 // $(form).find("#policyName").val("Recipe "+makid(2))
226 // TODO change up how we auto assign policyName. There could be the
227 // case where we do this and it will have repeats
228 // alert($(form).find("#_id").val())
229 // policyNameChangeListener(form)
233 ' <li class="active"><a id="go_properties_tab'
235 + '">Policy</a><button id="tab_close'
237 + '" type="button" class="close tab-close-popup" aria-hidden="true" style="margin-top: -30px;margin-right: 5px">×</button></li>');
238 $("#formId" + count).append(form);
239 $(".formId").not($("#formId" + count)).css("display", "none")
241 addSelectListen(count);
242 // This is for when the process is not loading from map but being
246 $(form).find("#_id").val(l)
247 policy_ids["#formId" + count] = l
249 for (var i = 1; i <= greatestIdNum(); i++) {
250 if ($("#formId" + i).length > 0) {
251 answers["#formId" + i + " #parentPolicy"] = $("#formId" + i + " #parentPolicy").val();
252 $("#formId" + i + " #parentPolicy").empty();
253 for (k in policy_ids) {
254 if (($("#formId" + i + " #_id").val() !== policy_ids[k].toString())
255 && $(k + " #recipe").val() !== undefined && $(k + " #recipe").val() !== "") {
256 $("#formId" + i + " #parentPolicy").append(
257 "<option value='" + policy_ids[k] + "'>" + $(k + " #recipe").val() + "</option>")
260 $("#formId" + i + " #parentPolicy").prepend("<option value=''></option>")
263 $("#formId" + count + " #parentPolicyConditions").multiselect("disable");
264 // re-populate parent policy with chosen options
271 function addCustListen(count) {
273 $('#go_properties_tab' + count).click(function(event) {
275 $("#nav_Tabs li").removeClass("active");
276 $(this).parent().addClass("active");
277 $("#formId" + count).css("display", "")
278 $(".formId").not($("#formId" + count)).css("display", "none")
280 $('#tab_close' + count).click(
283 if (document.getElementById("timeout").disabled) {
286 $(this).parent().remove();
287 for (var i = 1; i <= greatestIdNum(); i++) {
288 if ($("#formId" + i).length > 0) {
290 if ($("#formId" + i + " #parentPolicy").val() === $("#formId" + count + " #_id").val()
292 $("#formId" + i + " #parentPolicy").val("")
294 "#formId" + i + " #parentPolicy option[value='"
295 + $("#formId" + count + " #_id").val().toString() + "']").remove();
299 delete policy_ids["#formId" + count + " #_id"]
300 $("#formId" + count).remove();
303 function greatestIdNum() {
306 var s = $(".formId");
307 for (var i = 0; i < s.length; i++) {
308 if (parseInt($(s[i]).attr("id").substring(6)) > greatest) {
309 greatest = parseInt($(s[i]).attr("id").substring(6))
314 // Generate random id for each policy
315 // Also made sure ids couldnt be repeated
316 function makeid(num) {
319 var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
322 for (var i = 0; i < 7; i++)
323 text += possible.charAt(Math.floor(Math.random() * possible.length));
324 var hasValue = false;
325 for (k in policy_ids) {
326 if (text === policy_ids[k])
334 // var ParentPolicy = function(id, name) {
339 function saveLastPolicyLocally(lastPolicyId) {
341 console.log("last policy id is:" + lastPolicyId);
343 var properties = $(".saveProps").not("#formSpan .saveProps");
344 var timeoutForm = $("#Timeoutform").serializeArray();
345 for (var i = 0; i < timeoutForm.length; i++) {
346 polForm.push(timeoutForm[i]);
349 d["policyConfigurations"] = [];
350 for (var i = 0; i < properties.length; i++) {
351 var ser = $(properties[i]).serializeArray();
352 var s = noRepeats(ser)
353 d["policyConfigurations"].push(s);
356 for ( var x in allPolicies) {
357 if (x !== lastPolicyId) {
358 delete allPolicies[x];
359 console.log("remove old policy" + x);
362 allPolicies[lastPolicyId] = polForm;
364 function startNextItem() {
366 console.log("start next Item, policyname is:" + $("#pname").val());
367 // save last item before transitioning
368 var lastItem = $("#policyTable .highlight");
369 console.log("start next Item, lastitem is:" + lastItem);
370 if (lastItem.length > 0) {
371 console.log("start next Item length > 0");
372 saveLastPolicyLocally($("#pname").val());
373 // lastItem.attr("id", $("#pname").val());
374 lastItem.find("td").html($("#pname").val());
377 function add_new_policy(issueNewNames) {
379 console.log("add new policy");
380 // remove old gui forms
381 for (var i = 1; i < ($(".formId").length + 1); i++) {
382 $("#go_properties_tab" + i).parent().remove();
384 $(".formId").remove();
385 $("#pname").val("New_Policy");
386 $("#timeout").val(getOperationalPolicyProperty().timeout);
387 $("#add_one_more").click();
389 $scope.changeTargetResourceIdOther = function() {
391 var formItemActive = searchActiveFormId();
392 if (formItemActive === undefined)
394 if ($("#" + formItemActive.id + " #targetResourceId").val() === "Other:") {
395 $("#" + formItemActive.id + " #targetResourceIdOther").show();
397 $("#" + formItemActive.id + " #targetResourceIdOther").hide();
398 $("#" + formItemActive.id + " #targetResourceIdOther").val("");
401 $scope.changeGuardPolicyType = function() {
403 var formItemActive = searchActiveFormId();
404 if (formItemActive === undefined)
406 if ($("#" + formItemActive.id + " #guardPolicyType").val() === "GUARD_MIN_MAX") {
407 $("#" + formItemActive.id + " #minMaxGuardPolicyDiv").show();
408 $("#" + formItemActive.id + " #frequencyLimiterGuardPolicyDiv").hide();
409 } else if ($("#" + formItemActive.id + " #guardPolicyType").val() === "GUARD_YAML") {
410 $("#" + formItemActive.id + " #minMaxGuardPolicyDiv").hide();
411 $("#" + formItemActive.id + " #frequencyLimiterGuardPolicyDiv").show();
414 $scope.init = function() {
418 $("#add_one_more").click(function(event) {
420 console.log("add one more");
421 event.preventDefault();
422 var num = add_one_more();
425 var obj = getOperationalPolicyProperty();
427 console.log("lastElementSelected :" + lastElementSelected);
428 if (!($.isEmptyObject(obj))) {
429 allPolicies = jQuery.extend({}, obj);
430 for ( var x in allPolicies) {
431 $("#policyTable").prepend("<tr><td>" + x + "</td></tr>");
432 if (allPolicies[x][1]['value']) {
433 allPolIds.push(parseInt(allPolicies[x][1]['value']));
435 console.log("policies found :" + x);
439 if (loadPolicy !== undefined && loadPolicy !== null) {
441 console.log("load properties");
442 disperseConfig(allPolicies, loadPolicy);
444 console.log("create new policy");
447 $("#savePropsBtn").click(
450 console.log("save properties triggered");
451 if ($("#targetResourceIdOther").is(":visible")) {
452 $('#targetResourceId').append(
453 $('<option></option>').val($("#targetResourceIdOther").val()).html(
454 $("#targetResourceIdOther").val()))
455 $("#targetResourceId").val($("#targetResourceIdOther").val());
457 $(".idError").hide();
458 console.log("save properties triggered2");
460 console.log("get all policies");
461 var finalSaveList = {};
462 $("#policyTable td").each(function() {
464 console.log("enter policy table each loop");
465 var tableVal = $(this).text();
466 if (tableVal in allPolicies) {
467 finalSaveList[tableVal] = allPolicies[tableVal];
469 console.log("save properties; add tableVal to policies: " + tableVal);
471 var scope = angular.element(document.getElementById('formSpan')).scope();
472 scope.submitForm(finalSaveList);
473 $("#close_button").click();
475 $('#policyTable').on('click', 'tr', function(event) {
477 console.log("click on policyTable");
478 $(".idError").hide();
482 $(this).addClass('highlight').siblings().removeClass('highlight');
483 disperseConfig(allPolicies, $(this).find("td").html());
485 $('#pname').on('keypress', function(e) {
488 * var newVal = $(this).val() +
489 * String.fromCharCode(e.which); if ((newVal>99) ||
490 * (($(this).val().length>2) && e.keyCode != 46 && e.keyCode
491 * !=8)){ e.preventDefault(); }
493 if (e.keyCode == 32) {
494 $("#spaceError").show();
498 console.log("start next Item on 796");
500 if (("#policyTable .highlight").length > 0) {
501 $('#policyTable tr.highlight').removeClass('highlight');
503 $("#policyTable").prepend("<tr class='highlight'><td>New_Policy</td></tr>");
505 initTargetResourceId();
506 // load metrics dropdown
507 if (elementMap["global"]) {
508 for (var i = 0; i < (elementMap["global"].length); i++) {
509 if ((elementMap["global"][i]["name"]) == "actionSet") {
510 var asSel = elementMap["global"][i]["value"];
511 if (asSel == "vnfRecipe" && vf_Services !== null && vf_Services !== undefined) {
512 if (vf_Services["policy"][asSel]) {
513 $.each((vf_Services["policy"][asSel]), function(val, text) {
515 $('#recipe').append($('<option></option>').val(val).html(text));
526 $scope.isNumberKey = function(event) {
528 var charCode = (event.which) ? event.which : event.keyCode
529 if (charCode > 31 && (charCode < 48 || charCode > 57)) {
534 // setTimeout(function() {
536 // console.log("setTimeout");
539 $scope.close = function() {
541 console.log("close");
542 $uibModalInstance.close("closed");
544 $scope.submitForm = function(obj) {
546 var operationalPolicies = JSON.parse(JSON.stringify(getOperationalPolicies()));
548 operationalPolicies[0]["configurationsJson"] = obj;
550 operationalPolicyService.saveOpPolicyProperties(operationalPolicies).then(function(pars) {
552 updateOpPolicyProperties(operationalPolicies);