2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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 $scope.guard_ids = [];
42 $scope.duplicated = false;
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 add_one_more() {
62 console.log("add one more");
63 $("#nav_Tabs li").removeClass("active");
64 // FormSpan contains a block of the form that is not being
65 // displayed. We will create clones of that and add them to tabs
66 var form = $("#formSpan").clone(true, true)
68 // Each new tab will have the formId class attached to it. This way
69 // we can track how many forms we currently have out there and
70 // assign listeners to them
71 if ($(".formId").length > 0) {
74 for (var i = 0; i < s.length; i++) {
75 if (parseInt($(s[i]).attr("id").substring(6)) > greatest) {
76 greatest = parseInt($(s[i]).attr("id").substring(6))
80 $("#properties_tab").append(('<span class="formId" id="formId' + count + '"></span>'));
83 $("#properties_tab").append('<span class="formId" id="formId1"></span>');
88 ' <li class="active"><a id="go_properties_tab'
90 + '">new</a><button id="tab_close'
92 + '" type="button" class="close tab-close-popup" aria-hidden="true" style="margin-top: -30px;margin-right: 5px">×</button></li>');
93 $("#formId" + count).append(form.children());
94 $(".formId").not($("#formId" + count)).css("display", "none");
96 $("#formId" + count + " #id").val("new");
97 $("#formId" + count + " #clname").val($scope.clname);
100 function addCustListen(count) {
102 $('#go_properties_tab' + count).click(function(event) {
104 $("#nav_Tabs li").removeClass("active");
105 $(this).parent().addClass("active");
106 $("#formId" + count).css("display", "")
107 $(".formId").not($("#formId" + count)).css("display", "none")
109 $('#tab_close' + count).click(function(event) {
111 $(this).parent().remove();
112 $scope.policy_ids.splice($scope.policy_ids.indexOf($("#formId" + count + " #id").val()), 1);
113 $("#formId" + count).remove();
116 function greatestIdNum() {
119 var s = $(".formId");
120 for (var i = 0; i < s.length; i++) {
121 if (parseInt($(s[i]).attr("id").substring(6)) > greatest) {
122 greatest = parseInt($(s[i]).attr("id").substring(6))
127 function serializeElement(element) {
130 var a = element.serializeArray();
131 $.each(a, function() {
134 if (!o[this.name].push) {
135 o[this.name] = [ o[this.name] ];
137 o[this.name].push(this.value || '');
139 o[this.name] = this.value || '';
144 function savePolicyLocally() {
148 clPolForm = serializeElement($("#operationalPolicyHeaderForm"));
149 allPolicies['guard_policies'] = {};
150 var policiesArray = []
151 $.each($(".formId"), function() {
153 var policyProperties = serializeElement($("#" + this.id + " .policyProperties"));
154 policyProperties["target"] = serializeElement($("#" + this.id + " .policyTarget"))
155 policiesArray.push(policyProperties);
157 if ($("#" + this.id + " #enableGuardPolicy").is(':checked')) {
158 var guardPolicyBody = serializeElement($("#" + this.id + " .guardProperties"));
159 var guardPolicyId = guardPolicyBody['id'];
160 delete guardPolicyBody['id'];
161 allPolicies['guard_policies'][guardPolicyId] = guardPolicyBody;
162 $scope.guard_ids.push(guardPolicyId);
165 polForm['controlLoop'] = clPolForm;
166 polForm['policies'] = policiesArray;
167 allPolicies['operational_policy'] = polForm;
169 function add_new_policy() {
171 console.log("add new policy");
172 // remove old gui forms
173 for (var i = 1; i < ($(".formId").length + 1); i++) {
174 $("#go_properties_tab" + i).parent().remove();
176 $(".formId").remove();
177 $("#add_one_more").click();
179 function configureComponents(allPolicies) {
181 console.log("load properties to op policy");
183 $.each($('#operationalPolicyHeaderForm').find('.form-control'), function() {
184 $(this).val(allPolicies['operational_policy']['controlLoop'][this.id]);
186 // Set the sub-policies
187 $.each(allPolicies['operational_policy']['policies'], function(opPolicyElemIndex, opPolicyElemValue) {
189 var formNum = add_one_more();
190 $.each($('.policyProperties').find('.form-control'), function(opPolicyPropIndex, opPolicyPropValue) {
192 $("#formId" + formNum + " .policyProperties").find("#" + opPolicyPropValue.id).val(
193 allPolicies['operational_policy']['policies'][opPolicyElemIndex][opPolicyPropValue.id]);
196 // Initial TargetResourceId options
197 initTargetResourceIdOptions(allPolicies['operational_policy']['policies'][opPolicyElemIndex]['target']['type'], formNum);
198 $.each($('.policyTarget').find('.form-control'), function(opPolicyTargetPropIndex, opPolicyTargetPropValue) {
200 $("#formId" + formNum + " .policyTarget").find("#" + opPolicyTargetPropValue.id).val(
201 allPolicies['operational_policy']['policies'][opPolicyElemIndex]['target'][opPolicyTargetPropValue.id]);
204 // update the current tab label
205 $("#go_properties_tab" + formNum).text(
206 allPolicies['operational_policy']['policies'][opPolicyElemIndex]['id']);
207 // Check if there is a guard set for it
208 $.each(allPolicies['guard_policies'], function(guardElemId, guardElemValue) {
210 if (guardElemValue.recipe === $($("#formId" + formNum + " #recipe")[0]).val()) {
211 // Found one, set all guard prop
212 $.each($('.guardProperties').find('.form-control'), function(guardPropElemIndex,
213 guardPropElemValue) {
215 guardElemValue['id'] = guardElemId;
216 $("#formId" + formNum + " .guardProperties").find("#" + guardPropElemValue.id).val(
217 guardElemValue[guardPropElemValue.id]);
219 iniGuardPolicyType(guardElemId, formNum);
220 // And finally enable the flag
221 $("#formId" + formNum + " #enableGuardPolicy").prop("checked", true);
226 function iniGuardPolicyType (guardPolicyId, formNum) {
227 if(guardPolicyId.indexOf('guard.minmax.') !== -1) {
228 $("#formId" + formNum + " #minMaxGuardPolicyDiv").show();
229 $("#formId" + formNum + " #frequencyLimiterGuardPolicyDiv").hide();
230 $("#formId" + formNum + " #guardPolicyType").val("GUARD_MIN_MAX");
231 } else if (guardPolicyId.indexOf('guard.frequency.') !== -1) {
232 $("#formId" + formNum + " #minMaxGuardPolicyDiv").hide();
233 $("#formId" + formNum + " #frequencyLimiterGuardPolicyDiv").show();
234 $("#formId" + formNum + " #guardPolicyType").val("GUARD_YAML");
237 function initTargetResourceIdOptions (targetType, formNum) {
238 var recipe = $("#formId" + formNum + "#recipe").val();
239 $("#formId" + formNum + " #resourceID").empty();
240 $("#formId" + formNum + " #resourceID").append($('<option></option>').val("").html("-- choose an option --"));
242 var resourceVnf = getResourceDetailsVfProperty();
243 if (targetType == "VNF" && (null !== resourceVnf || undefined !== resourceVnf)) {
244 for ( var prop in resourceVnf) {
245 var name = resourceVnf[prop]["name"];
246 $("#formId" + formNum + " #resourceID").append($('<option></option>').val(name).html(name));
249 var resourceVFModule = getResourceDetailsVfModuleProperty();
250 if (targetType == "VFMODULE" && (null !== resourceVFModule || undefined !== resourceVFModule)) {
251 if (recipe == 'VF Module Create' || recipe == 'VF Module Delete') {
252 for ( var prop in resourceVFModule) {
253 if (resourceVFModule[prop]["isBase"] == false) {
254 $("#formId" + formNum + " #resourceID").append($('<option></option>').val(resourceVFModule[prop]["vfModuleModelName"]).html(resourceVFModule[prop]["vfModuleModelName"]));
258 for ( var prop in resourceVFModule) {
259 $("#formId" + formNum + " #resourceID").append($('<option></option>').val(resourceVFModule[prop]["vfModuleModelName"]).html(resourceVFModule[prop]["vfModuleModelName"]));
265 function initTargetModelAttributes (formNum) {
266 $("#formId" + formNum + " #modelName").val("");
267 $("#formId" + formNum + " #modelInvariantId").val("");
268 $("#formId" + formNum + " #modelVersionId").val("");
269 $("#formId" + formNum + " #modelVersion").val("");
270 $("#formId" + formNum + " #modelCustomizationId").val("");
273 $scope.initTargetResourceId = function(event) {
274 var formNum = $(event.target).closest('.formId').attr('id').substring(6);
275 initTargetModelAttributes(formNum);
276 var type = $(event.target).val();
277 initTargetResourceIdOptions(type, formNum);
280 $scope.changeGuardId = function(formItemActive) {
281 if (formItemActive === undefined) {
282 formItemActive = searchActiveFormId();
283 if (formItemActive === undefined) {
287 var oldValue = $("#" + formItemActive.id + " .guardProperties #id").val();
289 if(oldValue.indexOf('guard.minmax.') !== -1) {
290 oldValue = oldValue.substr(oldValue.indexOf('guard.minmax.') + 13);
291 } else if (oldValue.indexOf('guard.frequency.') !== -1) {
292 oldValue = oldValue.substr(oldValue.indexOf('guard.frequency.') + 16);
295 var prefix = "guard.";
296 if ($("#" + formItemActive.id + " #guardPolicyType").val() === "GUARD_MIN_MAX") {
297 prefix = prefix + "minmax.";
298 } else if ($("#" + formItemActive.id + " #guardPolicyType").val() === "GUARD_YAML") {
299 prefix = prefix + "frequency.";
301 $("#" + formItemActive.id + " .guardProperties #id").val(prefix+oldValue);
304 $scope.changeTargetResourceId = function(event) {
305 var formNum = $(event.target).closest('.formId').attr('id').substring(6);
306 initTargetModelAttributes(formNum);
308 var resourceVFModule = getResourceDetailsVfModuleProperty();
309 var type = $("#formId" + formNum +" #type").val();
310 var recipe = $("#formId" + formNum +" #recipe").val();
311 if (type == "VFMODULE" && (null !== resourceVFModule || undefined !== resourceVFModule)
312 && (recipe == 'VF Module Create' || recipe == 'VF Module Delete')) {
313 for ( var prop in resourceVFModule) {
314 if (prop == $(event.target).val()) {
315 $("#formId" + formNum +" #modelName").val(resourceVFModule[prop]["vfModuleModelName"]);
316 $("#formId" + formNum +" #modelInvariantId").val(resourceVFModule[prop]["vfModuleModelInvariantUUID"]);
317 $("#formId" + formNum +" #modelVersionId").val(resourceVFModule[prop]["vfModuleModelUUID"]);
318 $("#formId" + formNum +" #modelVersion").val(resourceVFModule[prop]["vfModuleModelVersion"]);
319 $("#formId" + formNum +" #modelCustomizationId").val(resourceVFModule[prop]["vfModuleModelCustomizationUUID"]);
320 $("#formId" + formNum +" #metadata *").prop('disabled', false);
324 $("#formId" + formNum +" #metadata *").prop('disabled', true);
327 $scope.changeGuardPolicyType = function() {
329 var formItemActive = searchActiveFormId();
330 if (formItemActive === undefined)
332 if ($("#" + formItemActive.id + " #guardPolicyType").val() === "GUARD_MIN_MAX") {
333 $("#" + formItemActive.id + " #minMaxGuardPolicyDiv").show();
334 $("#" + formItemActive.id + " #frequencyLimiterGuardPolicyDiv").hide();
335 } else if ($("#" + formItemActive.id + " #guardPolicyType").val() === "GUARD_YAML") {
336 $("#" + formItemActive.id + " #minMaxGuardPolicyDiv").hide();
337 $("#" + formItemActive.id + " #frequencyLimiterGuardPolicyDiv").show();
339 $scope.changeGuardId(formItemActive);
341 $scope.initPolicySelect = function() {
343 if (allPolicies['operational_policy'] === undefined || allPolicies['operational_policy'] === null) {
344 allPolicies = getOperationalPolicyProperty();
346 // Provision all policies ID first
347 if ($scope.policy_ids.length == 0 && allPolicies['operational_policy'] != undefined) {
348 $.each(allPolicies['operational_policy']['policies'], function() {
350 $scope.policy_ids.push(this['id']);
354 $scope.init = function() {
358 $scope.clname = getLoopName();
359 $("#add_one_more").click(function(event) {
361 console.log("add one more");
362 event.preventDefault();
363 $scope.policy_ids.push($("#formId" + add_one_more() + " #id").val());
365 if (allPolicies['operational_policy'] !== undefined && allPolicies['operational_policy'] !== null) {
367 console.log("load properties");
368 configureComponents(allPolicies);
370 console.log("create new policy");
373 $("#savePropsBtn").click(function(event) {
375 console.log("save properties triggered");
377 for (var i = 0; i <= $scope.guard_ids.length; i++) {
378 for (var j = i; j <= $scope.guard_ids.length; j++) {
379 if (i != j && $scope.guard_ids[i] == $scope.guard_ids[j]) {
380 // duplacated guard policy id exist
381 alert("The guard policy ID should be unique.");
386 angular.element(document.getElementById('formSpan')).scope().submitForm(allPolicies);
387 $("#close_button").click();
392 $scope.updateGuardRecipe = function(event) {
394 var formNum = $(event.target).closest('.formId').attr('id').substring(6);
395 // Get the second recipe (guard one) and update it
396 $($("#formId" + formNum + " #recipe")[1]).val($(event.target).val());
398 $scope.updateGuardActor = function(event) {
400 var formNum = $(event.target).closest('.formId').attr('id').substring(6);
401 // Get the second actor (guard one) and update it
402 $($("#formId" + formNum + " #actor")[1]).val($(event.target).val());
404 // When we change the name of a policy
405 $scope.updateTabLabel = function (event) {
407 // update policy id structure
408 var formNum = $(event.target).closest('.formId').attr('id').substring(6);
409 var policyId = $(event.target).val();
410 if ($scope.policy_ids.includes(policyId)) {
411 console.log("Duplicated ID, cannot proceed");
412 $scope.duplicated = true;
414 $scope.duplicated = false;
415 $scope.policy_ids.splice($scope.policy_ids.indexOf($("#formId" + formNum + " #id").val()), 1);
416 $scope.policy_ids.push($(event.target).val());
417 // Update the tab now
418 $("#go_properties_tab" + formNum).text($(event.target).val());
421 $scope.close = function() {
423 console.log("close");
424 $uibModalInstance.close("closed");
426 $scope.submitForm = function(obj) {
428 var operationalPolicies = getOperationalPolicies();
430 operationalPolicies[0]["configurationsJson"] = obj;
432 operationalPolicyService.saveOpPolicyProperties(operationalPolicies).then(function(pars) {
434 updateOpPolicyProperties(operationalPolicies);