2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 angular.module('abs').controller('decisionPolicyController', [ '$scope', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification', '$http', function($scope, PolicyAppService, PolicyNavigator, modalService, $modal, Notification, $http) {
23 $scope.policyNavigator;
24 $scope.savebutton = true;
25 $scope.refreshCheck = false;
26 $scope.disableOnCreate = false;
27 $scope.notRawPolicy = true;
29 if (!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly) {
30 $scope.disableOnCreate = true;
31 $scope.temp.policy = {
32 policyType : "Decision"
36 $scope.refresh = function() {
37 if ($scope.refreshCheck) {
38 $scope.policyNavigator.refresh();
40 $scope.modal('createNewPolicy', true);
41 $scope.temp.policy = "";
44 $scope.modal = function(id, hide) {
45 return $('#' + id).modal(hide ? 'hide' : 'show');
48 if ($scope.temp.policy.ruleProvider == undefined) {
49 $scope.temp.policy.ruleProvider = "Custom";
52 if ($scope.temp.policy.blackListEntryType == undefined) {
53 $scope.temp.policy.blackListEntryType = "Use Manual Entry";
56 PolicyAppService.getData('getDictionary/get_OnapNameDataByName').then(function(data) {
58 $scope.data = JSON.parse(j.data);
59 console.log($scope.data);
60 $scope.onapNameDictionaryDatas = JSON.parse($scope.data.onapNameDictionaryDatas);
61 console.log($scope.onapNameDictionaryDatas);
63 console.log("failed");
66 PolicyAppService.getData('getDictionary/get_SettingsDictionaryDataByName').then(function(data) {
68 $scope.data = JSON.parse(j.data);
69 console.log($scope.data);
70 $scope.settingsDictionaryDatas = JSON.parse($scope.data.settingsDictionaryDatas);
71 console.log($scope.settingsDictionaryDatas);
73 console.log("failed");
76 PolicyAppService.getData('get_FunctionDefinitionDataByName').then(function(data) {
78 $scope.data = JSON.parse(j.data);
79 console.log($scope.data);
80 $scope.functionDefinitionDatas = JSON.parse($scope.data.functionDefinitionDatas);
81 console.log($scope.functionDefinitionDatas);
83 console.log("failed");
86 PolicyAppService.getData('getDictionary/get_AttributeDatabyAttributeName').then(function(data) {
88 $scope.data = JSON.parse(j.data);
89 console.log($scope.data);
90 $scope.attributeDictionaryDatas = JSON.parse($scope.data.attributeDictionaryDatas);
91 console.log($scope.attributeDictionaryDatas);
93 console.log("failed");
96 PolicyAppService.getData('getDictionary/get_RainyDayDictionaryDataByName').then(function(data) {
98 $scope.data = JSON.parse(j.data);
99 console.log($scope.data);
100 $scope.rainyDayDictionaryDatas = JSON.parse($scope.data.rainyDayDictionaryDatas);
101 console.log($scope.rainyDayDictionaryDatas);
103 console.log("failed");
106 PolicyAppService.getData('getDictionary/get_RainyDayDictionaryData').then(function(data) {
108 $scope.data = JSON.parse(j.data);
109 console.log($scope.data);
110 $scope.rainyDayDictionaryDataEntity = JSON.parse($scope.data.rainyDayDictionaryDatas);
111 console.log($scope.rainyDayDictionaryDatasEntity);
113 console.log("failed");
116 $scope.saveDecisionPolicy = function(policy) {
117 if (policy.itemContent != undefined) {
118 $scope.refreshCheck = true;
119 $scope.policyNavigator = policy.itemContent;
120 policy.itemContent = "";
122 $scope.savebutton = false;
124 var uuu = "policycreation/save_policy";
132 contentType : 'application/json',
133 data : JSON.stringify(postData),
134 success : function(data) {
135 $scope.$apply(function() {
136 $scope.data = data.policyData;
137 if ($scope.data == 'success') {
138 $scope.temp.policy.readOnly = 'true';
139 Notification.success("Policy Saved Successfully.");
140 } else if ($scope.data == 'PolicyExists') {
141 $scope.savebutton = true;
142 Notification.error("Policy Already Exists with Same Name in Scope.");
145 console.log($scope.data);
148 error : function(data) {
149 Notification.error("Error Occured while saving Policy.");
154 $scope.validatePolicy = function(policy) {
156 document.getElementById("validate").innerHTML = "";
157 var uuu = "policyController/validate_policy.htm";
165 contentType : 'application/json',
166 data : JSON.stringify(postData),
167 success : function(data) {
168 $scope.$apply(function() {
169 $scope.validateData = data.data.replace(/\"/g, "");
170 $scope.data = data.data;
171 if ($scope.data == '"success"') {
172 Notification.success("Validation Success.");
173 $scope.savebutton = false;
175 Notification.error("Validation Failed.");
176 document.getElementById("validate").innerHTML = $scope.validateData;
177 document.getElementById("validate").style.color = "white";
178 document.getElementById("validate").style.backgroundColor = "red";
179 $scope.savebutton = true;
183 console.log($scope.data);
185 error : function(data) {
186 Notification.error("Validation Failed.");
187 $scope.savebutton = true;
192 if (!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly) {
193 $scope.temp.policy.attributes = [];
194 $scope.temp.policy.settings = [];
195 $scope.temp.policy.ruleAlgorithmschoices = [];
196 if (!$scope.temp.policy.yamlparams) {
197 $scope.temp.policy.yamlparams = {};
199 if (!$scope.temp.policy.yamlparams.targets) {
200 $scope.temp.policy.yamlparams.targets = [];
202 if (!$scope.temp.policy.yamlparams.blackList) {
203 $scope.temp.policy.yamlparams.blackList = [];
205 if (!$scope.temp.policy.rainyday) {
206 $scope.temp.policy.rainyday = {};
208 if (!$scope.temp.policy.rainyday.treatmentTableChoices) {
209 $scope.temp.policy.rainyday.treatmentTableChoices = [];
212 } else if ($scope.temp.policy.ruleProvider == "Custom") {
213 if ($scope.temp.policy.attributes.length == 0) {
214 $scope.temp.policy.attributes = [];
216 if ($scope.temp.policy.settings.length == 0) {
217 $scope.temp.policy.settings = [];
219 if ($scope.temp.policy.ruleAlgorithmschoices == null || $scope.temp.policy.ruleAlgorithmschoices.length == 0) {
220 $scope.temp.policy.ruleAlgorithmschoices = [];
222 } else if ($scope.temp.policy.ruleProvider == "GUARD_BL_YAML") {
223 if ($scope.temp.policy.yamlparams.blackList == null || $scope.temp.policy.yamlparams.blackList.length == 0) {
224 $scope.temp.policy.yamlparams.blackList = [];
226 if ($scope.temp.policy.blackListEntries == null || $scope.temp.policy.blackListEntries.length == 0) {
227 $scope.temp.policy.blackListEntries = [];
229 $scope.blackListEntries = [];
230 $scope.temp.policy.appendBlackListEntries = [];
231 $scope.blackListEntries = arrayUnique($scope.temp.policy.blackListEntries.concat($scope.temp.policy.yamlparams.blackList));
233 }else if($scope.temp.policy.ruleProvider=="GUARD_YAML" || $scope.temp.policy.ruleProvider=="GUARD_MIN_MAX"){
234 if($scope.temp.policy.yamlparams.targets.length==0){
235 $scope.temp.policy.yamlparams.targets = [];
237 } else if ($scope.temp.policy.ruleProvider == "Rainy_Day") {
238 if ($scope.temp.policy.rainyday.treatmentTableChoices == null || $scope.temp.policy.rainyday.treatmentTableChoices.length == 0) {
239 $scope.temp.policy.rainyday.treatmentTableChoices = [];
242 $scope.attributeDatas = [ {
243 "attributes" : $scope.temp.policy.attributes
245 $scope.addNewChoice = function() {
246 var newItemNo = $scope.temp.policy.attributes.length + 1;
247 $scope.temp.policy.attributes.push({
248 'id' : 'choice' + newItemNo
251 $scope.removeChoice = function() {
252 var lastItem = $scope.temp.policy.attributes.length - 1;
253 $scope.temp.policy.attributes.splice(lastItem);
256 $scope.settingsDatas = [ {
257 "settings" : $scope.temp.policy.settings
259 $scope.addNewSettingsChoice = function() {
260 var newItemNo = $scope.temp.policy.settings.length + 1;
261 $scope.temp.policy.settings.push({
262 'id' : 'choice' + newItemNo
265 $scope.removeSettingsChoice = function() {
266 var lastItem = $scope.temp.policy.settings.length - 1;
267 $scope.temp.policy.settings.splice(lastItem);
270 $scope.addNewTarget = function() {
271 $scope.temp.policy.yamlparams.targets.push('');
273 $scope.removeTarget = function() {
274 var lastItem = $scope.temp.policy.yamlparams.targets.length - 1;
275 $scope.temp.policy.yamlparams.targets.splice(lastItem);
278 $scope.addNewBL = function() {
279 $scope.temp.policy.yamlparams.blackList.push('');
282 $scope.removeBL = function(id) {
283 $scope.temp.policy.yamlparams.blackList = $scope.temp.policy.yamlparams.blackList.filter(function (obj){
288 $scope.treatmentDatas = [ {
289 "treatmentValues" : $scope.temp.policy.rainyday.treatmentTableChoices
292 $scope.addNewTreatment = function() {
293 $scope.temp.policy.rainyday.treatmentTableChoices.push({});
296 $scope.removeTreatment = function() {
297 var lastItem = $scope.temp.policy.rainyday.treatmentTableChoices.length - 1;
298 $scope.temp.policy.rainyday.treatmentTableChoices.splice(lastItem);
301 $scope.workstepDictionaryDatas = [];
302 $scope.getWorkstepValues = function(bbidValue) {
303 for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
304 var obj = $scope.rainyDayDictionaryDataEntity[i];
305 if (obj.bbid == bbidValue) {
306 $scope.workstepDictionaryDatas.push(obj.workstep);
311 $scope.allowedTreatmentsDatas = [];
312 $scope.getTreatmentValues = function(bbidValue, workstepValue) {
313 for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
314 var obj = $scope.rainyDayDictionaryDataEntity[i];
315 if (obj.bbid == bbidValue && obj.workstep == workstepValue) {
316 var splitAlarm = obj.treatments.split(',');
317 for (var j = 0; j < splitAlarm.length; ++j) {
318 $scope.allowedTreatmentsDatas.push(splitAlarm[j]);
325 $scope.ruleAlgorithmDatas = [ {
326 "ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices
329 $scope.addNewRuleAlgorithm = function() {
330 if ($scope.temp.policy.ruleAlgorithmschoices != null) {
331 var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length + 1;
336 var value = newItemNo - 1;
337 $scope.attributeDictionaryDatas.push('A' + value);
339 $scope.temp.policy.ruleAlgorithmschoices.push({
340 'id' : 'A' + newItemNo
344 $scope.removeRuleAlgorithm = function() {
345 var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length - 1;
346 $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
349 $scope.providerListener = function(ruleProvider) {
350 if (ruleProvider != "Custom") {
351 $scope.temp.policy.ruleAlgorithmschoices = [];
352 $scope.temp.policy.settings = [];
353 $scope.temp.policy.attributes = [];
355 if (ruleProvider === "Raw") {
356 $scope.notRawPolicy = false;
360 $scope.importButton = true;
362 $scope.uploadBLFile = function(files) {
364 fd.append("file", files[0]);
365 var fileExtension = files[0].name.split(".")[1];
366 if (fileExtension == "xls") {
367 $scope.importButton = false;
370 Notification.error("Upload the BlackList file which extends with .xls format.");
374 function arrayUnique(array) {
375 var a = array.concat();
376 for (var i = 0; i < a.length; ++i) {
377 for (var j = i + 1; j < a.length; ++j) {
385 $scope.submitUpload = function() {
386 $http.post("policycreation/importBlackListForDecisionPolicy", fd, {
387 withCredentials : false,
389 'Content-Type' : undefined
391 transformRequest : angular.identity
392 }).success(function(data) {
393 $scope.data = JSON.parse(data.data);
394 $scope.temp.policy.blackListEntries = $scope.data.blackListEntries;
395 if ($scope.temp.policy.blackListEntries[0] !== "error") {
396 $scope.blackListEntries = arrayUnique($scope.temp.policy.blackListEntries.concat($scope.temp.policy.yamlparams.blackList));
397 $scope.temp.policy.appendBlackListEntries = $scope.data.appendBlackListEntries;
398 $scope.blackListEntries = $scope.blackListEntries.filter(function(obj) {
399 return !$scope.temp.policy.appendBlackListEntries.includes(obj);
401 if ($scope.blackListEntries.length == 0) {
402 $scope.validateButton = true;
403 Notification.error("Black Lists are empty. Minimum one entry required.");
405 $scope.temp.policy.blackListEntries = $scope.blackListEntries;
406 Notification.success("Blacklist File Uploaded Successfully.");
407 $scope.validateButton = false;
408 $scope.importButton = true;
411 Notification.error("Blacklist File Upload Failed." + $scope.temp.policy.blackListEntries[1]);
413 }).error(function(data) {
414 Notification.error("Blacklist File Upload Failed.");
418 $scope.initializeBlackList = function() {
419 if ($scope.temp.policy.blackListEntryType === "Use File Upload") {
420 $scope.validateButton = true;
422 $scope.validateButton = false;
424 $("#importFile").val('');
427 $scope.exportBlackListEntries = function() {
428 var uuu = "policycreation/exportDecisionBlackListEntries";
430 policyData : $scope.temp.policy,
431 date : $scope.temp.model.modifiedDate,
432 version : $scope.temp.model.version
438 contentType : 'application/json',
439 data : JSON.stringify(postData),
440 success : function(data) {
441 $scope.$apply(function() {
442 $scope.data = data.data;
443 var url = '../' + $scope.data;
444 window.location = url;
445 Notification.success("BlackList Entries Exported Successfully.");
447 console.log($scope.data);
449 error : function(data) {
450 Notification.error("Error Occured while Exporting BlackList Entries.");