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 $scope.policy_ids = []
38 $scope.guardType = "GUARD_MIN_MAX";
41 function getAllFormId() {
43 return Array.from(document.getElementsByClassName("formId"));
45 function searchActiveFormId() {
47 var formArray = getAllFormId();
48 for (var i = 0, max = formArray.length; i < max; i++) {
49 console.log("Search active FormId, current element " + formArray[i].id);
50 if (formArray[i].style.display !== "none") {
51 console.log("Active form is:" + formArray[i].id);
55 console.log("No active formId found !");
57 function initTargetResourceId() {
59 if (vf_Services !== null && vf_Services !== undefined) {
60 // Set all the Resource Invariant UUID in the target resource ID
61 // list (+Empty and other)
62 Object.keys(vf_Services["shared"]["byVf"]).forEach(function(key) {
64 $("#targetResourceId").append($('<option></option>').val(key).html(key));
68 function add_one_more() {
70 console.log("add one more");
71 $("#nav_Tabs li").removeClass("active");
72 // FormSpan contains a block of the form that is not being
73 // displayed. We will create clones of that and add them to tabs
74 var form = $("#formSpan").clone(true, true)
76 // Each new tab will have the formId class attached to it. This way
77 // we can track how many forms we currently have out there and
78 // assign listeners to them
79 if ($(".formId").length > 0) {
82 for (var i = 0; i < s.length; i++) {
83 if (parseInt($(s[i]).attr("id").substring(6)) > greatest) {
84 greatest = parseInt($(s[i]).attr("id").substring(6))
88 $("#properties_tab").append(('<span class="formId" id="formId' + count + '"></span>'));
91 $("#properties_tab").append('<span class="formId" id="formId1"></span>');
96 ' <li class="active"><a id="go_properties_tab'
98 + '">new</a><button id="tab_close'
100 + '" type="button" class="close tab-close-popup" aria-hidden="true" style="margin-top: -30px;margin-right: 5px">×</button></li>');
101 $("#formId" + count).append(form.children());
102 $(".formId").not($("#formId" + count)).css("display", "none");
103 addCustListen(count);
104 $("#formId" + count + " #id").val("new");
107 function addCustListen(count) {
109 $('#go_properties_tab' + count).click(function(event) {
111 $("#nav_Tabs li").removeClass("active");
112 $(this).parent().addClass("active");
113 $("#formId" + count).css("display", "")
114 $(".formId").not($("#formId" + count)).css("display", "none")
116 $('#tab_close' + count).click(function(event) {
118 $(this).parent().remove();
119 $scope.policy_ids.splice($scope.policy_ids.indexOf($("#formId" + count + " #id").val()), 1);
120 $("#formId" + count).remove();
123 function greatestIdNum() {
126 var s = $(".formId");
127 for (var i = 0; i < s.length; i++) {
128 if (parseInt($(s[i]).attr("id").substring(6)) > greatest) {
129 greatest = parseInt($(s[i]).attr("id").substring(6))
134 function serializeElement(element) {
137 var a = element.serializeArray();
138 $.each(a, function() {
141 if (!o[this.name].push) {
142 o[this.name] = [ o[this.name] ];
144 o[this.name].push(this.value || '');
146 o[this.name] = this.value || '';
151 function savePolicyLocally() {
154 polForm = serializeElement($("#operationalPolicyHeaderForm"));
155 var policiesArray = []
156 allPolicies['guard_policies'] = [];
157 $.each($(".formId"), function() {
159 var policyProperties = serializeElement($("#" + this.id + " .policyProperties"));
160 policyProperties["target"] = serializeElement($("#" + this.id + " .policyTarget"))
161 policiesArray.push(policyProperties);
163 if ($("#" + this.id + " #enableGuardPolicy").is(':checked')) {
164 allPolicies['guard_policies'].push(serializeElement($("#" + this.id + " .guardProperties")));
167 polForm['policies'] = policiesArray;
168 allPolicies['operational_policy'] = polForm;
170 function add_new_policy() {
172 console.log("add new policy");
173 // remove old gui forms
174 for (var i = 1; i < ($(".formId").length + 1); i++) {
175 $("#go_properties_tab" + i).parent().remove();
177 $(".formId").remove();
178 $("#add_one_more").click();
180 function configureComponents(allPolicies) {
182 console.log("load properties to op policy");
184 $.each($('#operationalPolicyHeaderForm').find('.form-control'), function() {
186 $(this).val(allPolicies['operational_policy'][this.id]);
188 // Set the sub-policies
189 $.each(allPolicies['operational_policy']['policies'], function(opPolicyElemIndex, opPolicyElemValue) {
191 var formNum = add_one_more();
192 $.each($('.policyProperties').find('.form-control'), function(opPolicyPropIndex, opPolicyPropValue) {
194 $("#formId"+formNum+" .policyProperties").find("#"+opPolicyPropValue.id).val(
195 allPolicies['operational_policy']['policies'][opPolicyElemIndex][opPolicyPropValue.id]);
197 // update the current tab label
198 $("#go_properties_tab" + formNum).text(
199 allPolicies['operational_policy']['policies'][opPolicyElemIndex]['id']);
200 // Check if there is a guard set for it
201 $.each(allPolicies['guard_policies'], function(guardElemIndex, guardElemValue) {
203 if (guardElemValue.recipe === $($("#formId" + formNum + " #recipe")[0]).val()) {
204 // Found one, set all guard prop
205 $.each($('.guardProperties').find('.form-control'), function(guardPropElemIndex,guardPropElemValue) {
206 $("#formId"+formNum+" .guardProperties").find("#"+guardPropElemValue.id).val(
207 allPolicies['guard_policies'][guardElemIndex][guardPropElemValue.id]);
209 // And finally enable the flag
210 $("#formId" + formNum + " #enableGuardPolicy").prop("checked", true);
216 $scope.changeGuardPolicyType = function() {
218 var formItemActive = searchActiveFormId();
219 if (formItemActive === undefined)
221 if ($("#" + formItemActive.id + " #guardPolicyType").val() === "GUARD_MIN_MAX") {
222 $("#" + formItemActive.id + " #minMaxGuardPolicyDiv").show();
223 $("#" + formItemActive.id + " #frequencyLimiterGuardPolicyDiv").hide();
224 } else if ($("#" + formItemActive.id + " #guardPolicyType").val() === "GUARD_YAML") {
225 $("#" + formItemActive.id + " #minMaxGuardPolicyDiv").hide();
226 $("#" + formItemActive.id + " #frequencyLimiterGuardPolicyDiv").show();
229 $scope.initPolicySelect = function() {
231 if (allPolicies['operational_policy'] === undefined || allPolicies['operational_policy'] === null) {
232 allPolicies = getOperationalPolicyProperty();
234 // Provision all policies ID first
235 if ($scope.policy_ids.length == 0 && allPolicies['operational_policy'] != undefined) {
236 $.each(allPolicies['operational_policy']['policies'], function() {
238 $scope.policy_ids.push(this['id']);
242 $scope.init = function() {
245 $scope.clname=getLoopName();
246 $("#add_one_more").click(function(event) {
248 console.log("add one more");
249 event.preventDefault();
250 $scope.policy_ids.push($("#formId" + add_one_more() + " #id").val());
252 if (allPolicies['operational_policy'] !== undefined && allPolicies['operational_policy'] !== null) {
254 console.log("load properties");
255 configureComponents(allPolicies);
257 console.log("create new policy");
260 $("#savePropsBtn").click(function(event) {
262 console.log("save properties triggered");
264 angular.element(document.getElementById('formSpan')).scope().submitForm(allPolicies);
265 $("#close_button").click();
267 initTargetResourceId();
271 $scope.updateGuardRecipe = function(event) {
273 var formNum = $(event.target).closest('.formId').attr('id').substring(6);
274 // Get the second recipe (guard one) and update it
275 $($("#formId" + formNum + " #recipe")[1]).val($(event.target).val());
277 // When we change the name of a policy
278 $scope.updateTabLabel = function(event) {
280 // update policy id structure
281 var formNum = $(event.target).closest('.formId').attr('id').substring(6);
282 $scope.policy_ids.splice($scope.policy_ids.indexOf($("#formId" + formNum + " #id").val()), 1);
283 $scope.policy_ids.push($(event.target).val());
284 // Update the tab now
285 $("#go_properties_tab" + formNum).text($(event.target).val());
287 $scope.close = function() {
289 console.log("close");
290 $uibModalInstance.close("closed");
292 $scope.submitForm = function(obj) {
294 var operationalPolicies = getOperationalPolicies();
296 operationalPolicies[0]["configurationsJson"] = obj;
298 operationalPolicyService.saveOpPolicyProperties(operationalPolicies).then(function(pars) {
300 updateOpPolicyProperties(operationalPolicies);