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',
21 [ '$scope', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification', '$http',
22 function($scope, PolicyAppService, PolicyNavigator, modalService, $modal, Notification, $http) {
25 $scope.policyNavigator;
26 $scope.savebutton = true;
27 $scope.refreshCheck = false;
28 $scope.disableOnCreate = false;
29 $scope.notRawPolicy = true;
31 if (!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly) {
32 $scope.disableOnCreate = true;
33 $scope.temp.policy = {
34 policyType : "Decision"
38 $scope.refresh = function() {
39 if ($scope.refreshCheck) {
40 $scope.policyNavigator.refresh();
42 $scope.modal('createNewPolicy', true);
43 $scope.temp.policy = "";
46 $scope.modal = function(id, hide) {
47 return $('#' + id).modal(hide ? 'hide' : 'show');
50 if ($scope.temp.policy.ruleProvider == undefined) {
51 $scope.temp.policy.ruleProvider = "Custom";
54 if ($scope.temp.policy.blackListEntryType == undefined) {
55 $scope.temp.policy.blackListEntryType = "Use Manual Entry";
58 PolicyAppService.getData('getDictionary/get_OnapNameDataByName').then(function(data) {
60 $scope.data = JSON.parse(j.data);
61 $scope.onapNameDictionaryDatas = JSON.parse($scope.data.onapNameDictionaryDatas);
64 PolicyAppService.getData('getDictionary/get_SettingsDictionaryDataByName').then(function(data) {
66 $scope.data = JSON.parse(j.data);
67 $scope.settingsDictionaryDatas = JSON.parse($scope.data.settingsDictionaryDatas);
70 PolicyAppService.getData('get_FunctionDefinitionDataByName').then(function(data) {
72 $scope.data = JSON.parse(j.data);
73 $scope.functionDefinitionDatas = JSON.parse($scope.data.functionDefinitionDatas);
76 PolicyAppService.getData('getDictionary/get_AttributeDatabyAttributeName').then(function(data) {
78 $scope.data = JSON.parse(j.data);
79 $scope.attributeDictionaryDatas = JSON.parse($scope.data.attributeDictionaryDatas);
82 PolicyAppService.getData('getDictionary/get_RainyDayDictionaryDataByName').then(function(data) {
84 $scope.data = JSON.parse(j.data);
85 $scope.rainyDayDictionaryDatas = JSON.parse($scope.data.rainyDayDictionaryDatas);
88 PolicyAppService.getData('getDictionary/get_RainyDayDictionaryData').then(function(data) {
90 $scope.data = JSON.parse(j.data);
91 $scope.rainyDayDictionaryDataEntity = JSON.parse($scope.data.rainyDayDictionaryDatas);
94 $scope.saveDecisionPolicy = function(policy) {
95 if (policy.itemContent != undefined) {
96 $scope.refreshCheck = true;
97 $scope.policyNavigator = policy.itemContent;
98 policy.itemContent = "";
100 $scope.savebutton = false;
101 var uuu = "policycreation/save_policy";
109 contentType : 'application/json',
110 data : JSON.stringify(postData),
111 success : function(data) {
112 $scope.$apply(function() {
113 $scope.data = data.policyData;
114 if ($scope.data == 'success') {
115 $scope.temp.policy.readOnly = 'true';
116 Notification.success("Policy Saved Successfully.");
117 } else if ($scope.data == 'PolicyExists') {
118 $scope.savebutton = true;
119 Notification.error("Policy Already Exists with Same Name in Scope.");
124 error : function(data) {
125 Notification.error("Error Occured while saving Policy.");
130 $scope.validatePolicy = function(policy) {
131 document.getElementById("validate").innerHTML = "";
132 var uuu = "policyController/validate_policy.htm";
140 contentType : 'application/json',
141 data : JSON.stringify(postData),
142 success : function(data) {
143 $scope.$apply(function() {
144 $scope.validateData = data.data.replace(/\"/g, "");
145 $scope.data = data.data;
146 if ($scope.data == '"success"') {
147 Notification.success("Validation Success.");
148 $scope.savebutton = false;
150 Notification.error("Validation Failed.");
151 document.getElementById("validate").innerHTML = $scope.validateData;
152 document.getElementById("validate").style.color = "white";
153 document.getElementById("validate").style.backgroundColor = "red";
154 $scope.savebutton = true;
159 error : function(data) {
160 Notification.error("Validation Failed.");
161 $scope.savebutton = true;
166 if (!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly) {
167 $scope.temp.policy.attributes = [];
168 $scope.temp.policy.settings = [];
169 $scope.temp.policy.ruleAlgorithmschoices = [];
170 if (!$scope.temp.policy.yamlparams) {
171 $scope.temp.policy.yamlparams = {};
173 if (!$scope.temp.policy.yamlparams.targets) {
174 $scope.temp.policy.yamlparams.targets = [];
176 if (!$scope.temp.policy.yamlparams.blackList) {
177 $scope.temp.policy.yamlparams.blackList = [];
179 if (!$scope.temp.policy.rainyday) {
180 $scope.temp.policy.rainyday = {};
182 if (!$scope.temp.policy.rainyday.treatmentTableChoices) {
183 $scope.temp.policy.rainyday.treatmentTableChoices = [];
186 } else if ($scope.temp.policy.ruleProvider == "Custom") {
187 if ($scope.temp.policy.attributes.length == 0) {
188 $scope.temp.policy.attributes = [];
190 if ($scope.temp.policy.settings.length == 0) {
191 $scope.temp.policy.settings = [];
193 if ($scope.temp.policy.ruleAlgorithmschoices == null || $scope.temp.policy.ruleAlgorithmschoices.length == 0) {
194 $scope.temp.policy.ruleAlgorithmschoices = [];
196 } else if ($scope.temp.policy.ruleProvider == "GUARD_BL_YAML") {
197 if ($scope.temp.policy.yamlparams.blackList == null || $scope.temp.policy.yamlparams.blackList.length == 0) {
198 $scope.temp.policy.yamlparams.blackList = [];
200 if ($scope.temp.policy.blackListEntries == null || $scope.temp.policy.blackListEntries.length == 0) {
201 $scope.temp.policy.blackListEntries = [];
203 $scope.blackListEntries = [];
204 $scope.temp.policy.appendBlackListEntries = [];
205 $scope.blackListEntries = arrayUnique($scope.temp.policy.blackListEntries.concat($scope.temp.policy.yamlparams.blackList));
207 }else if($scope.temp.policy.ruleProvider=="GUARD_YAML" || $scope.temp.policy.ruleProvider=="GUARD_MIN_MAX"){
208 if($scope.temp.policy.yamlparams.targets.length==0){
209 $scope.temp.policy.yamlparams.targets = [];
211 } else if ($scope.temp.policy.ruleProvider == "Rainy_Day") {
212 if ($scope.temp.policy.rainyday.treatmentTableChoices == null || $scope.temp.policy.rainyday.treatmentTableChoices.length == 0) {
213 $scope.temp.policy.rainyday.treatmentTableChoices = [];
216 $scope.attributeDatas = [ {
217 "attributes" : $scope.temp.policy.attributes
219 $scope.addNewChoice = function() {
220 var newItemNo = $scope.temp.policy.attributes.length + 1;
221 $scope.temp.policy.attributes.push({
222 'id' : 'choice' + newItemNo
225 $scope.removeChoice = function() {
226 var lastItem = $scope.temp.policy.attributes.length - 1;
227 $scope.temp.policy.attributes.splice(lastItem);
230 $scope.settingsDatas = [ {
231 "settings" : $scope.temp.policy.settings
233 $scope.addNewSettingsChoice = function() {
234 var newItemNo = $scope.temp.policy.settings.length + 1;
235 $scope.temp.policy.settings.push({
236 'id' : 'choice' + newItemNo
239 $scope.removeSettingsChoice = function() {
240 var lastItem = $scope.temp.policy.settings.length - 1;
241 $scope.temp.policy.settings.splice(lastItem);
244 $scope.addNewTarget = function() {
245 $scope.temp.policy.yamlparams.targets.push('');
247 $scope.removeTarget = function() {
248 var lastItem = $scope.temp.policy.yamlparams.targets.length - 1;
249 $scope.temp.policy.yamlparams.targets.splice(lastItem);
252 $scope.addNewBL = function() {
253 $scope.temp.policy.yamlparams.blackList.push('');
256 $scope.removeBL = function(id) {
257 $scope.temp.policy.yamlparams.blackList = $scope.temp.policy.yamlparams.blackList.filter(function (obj){
262 $scope.treatmentDatas = [ {
263 "treatmentValues" : $scope.temp.policy.rainyday.treatmentTableChoices
266 $scope.addNewTreatment = function() {
267 $scope.temp.policy.rainyday.treatmentTableChoices.push({});
270 $scope.removeTreatment = function() {
271 var lastItem = $scope.temp.policy.rainyday.treatmentTableChoices.length - 1;
272 $scope.temp.policy.rainyday.treatmentTableChoices.splice(lastItem);
275 $scope.workstepDictionaryDatas = [];
276 $scope.getWorkstepValues = function(bbidValue) {
277 for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
278 var obj = $scope.rainyDayDictionaryDataEntity[i];
279 if (obj.bbid == bbidValue) {
280 $scope.workstepDictionaryDatas.push(obj.workstep);
285 $scope.allowedTreatmentsDatas = [];
286 $scope.getTreatmentValues = function(bbidValue, workstepValue) {
287 for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
288 var obj = $scope.rainyDayDictionaryDataEntity[i];
289 if (obj.bbid == bbidValue && obj.workstep == workstepValue) {
290 var splitAlarm = obj.treatments.split(',');
291 for (var j = 0; j < splitAlarm.length; ++j) {
292 $scope.allowedTreatmentsDatas.push(splitAlarm[j]);
299 $scope.ruleAlgorithmDatas = [ {
300 "ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices
303 $scope.addNewRuleAlgorithm = function() {
304 if ($scope.temp.policy.ruleAlgorithmschoices != null) {
305 var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length + 1;
310 var value = newItemNo - 1;
311 $scope.attributeDictionaryDatas.push('A' + value);
313 $scope.temp.policy.ruleAlgorithmschoices.push({
314 'id' : 'A' + newItemNo
318 $scope.removeRuleAlgorithm = function() {
319 var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length - 1;
320 $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
323 $scope.providerListener = function(ruleProvider) {
324 if (ruleProvider != "Custom") {
325 $scope.temp.policy.ruleAlgorithmschoices = [];
326 $scope.temp.policy.settings = [];
327 $scope.temp.policy.attributes = [];
329 if (ruleProvider === "Raw") {
330 $scope.notRawPolicy = false;
334 $scope.importButton = true;
336 $scope.uploadBLFile = function(files) {
338 fd.append("file", files[0]);
339 var fileExtension = files[0].name.split(".")[1];
340 if (fileExtension == "xls") {
341 $scope.importButton = false;
344 Notification.error("Upload the BlackList file which extends with .xls format.");
348 function arrayUnique(array) {
349 var a = array.concat();
350 for (var i = 0; i < a.length; ++i) {
351 for (var j = i + 1; j < a.length; ++j) {
359 $scope.submitUpload = function() {
360 $http.post("policycreation/importBlackListForDecisionPolicy", fd, {
361 withCredentials : false,
363 'Content-Type' : undefined
365 transformRequest : angular.identity
366 }).success(function(data) {
367 $scope.data = JSON.parse(data.data);
368 $scope.temp.policy.blackListEntries = $scope.data.blackListEntries;
369 if ($scope.temp.policy.blackListEntries[0] !== "error") {
370 $scope.blackListEntries = arrayUnique($scope.temp.policy.blackListEntries.concat($scope.temp.policy.yamlparams.blackList));
371 $scope.temp.policy.appendBlackListEntries = $scope.data.appendBlackListEntries;
372 $scope.blackListEntries = $scope.blackListEntries.filter(function(obj) {
373 return !$scope.temp.policy.appendBlackListEntries.includes(obj);
375 if ($scope.blackListEntries.length == 0) {
376 $scope.validateButton = true;
377 Notification.error("Black Lists are empty. Minimum one entry required.");
379 $scope.temp.policy.blackListEntries = $scope.blackListEntries;
380 Notification.success("Blacklist File Uploaded Successfully.");
381 $scope.validateButton = false;
382 $scope.importButton = true;
385 Notification.error("Blacklist File Upload Failed." + $scope.temp.policy.blackListEntries[1]);
387 }).error(function(data) {
388 Notification.error("Blacklist File Upload Failed.");
392 $scope.initializeBlackList = function() {
393 if ($scope.temp.policy.blackListEntryType === "Use File Upload") {
394 $scope.validateButton = true;
396 $scope.validateButton = false;
398 $("#importFile").val('');
401 $scope.exportBlackListEntries = function() {
402 var uuu = "policycreation/exportDecisionBlackListEntries";
404 policyData : $scope.temp.policy,
405 date : $scope.temp.model.modifiedDate,
406 version : $scope.temp.model.version
412 contentType : 'application/json',
413 data : JSON.stringify(postData),
414 success : function(data) {
415 $scope.$apply(function() {
416 $scope.data = data.data;
417 var url = '../' + $scope.data;
418 window.location = url;
419 Notification.success("BlackList Entries Exported Successfully.");
422 error : function(data) {
423 Notification.error("Error Occured while Exporting BlackList Entries.");