2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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"
37 $scope.refresh = function() {
38 if ($scope.refreshCheck) {
39 $scope.policyNavigator.refresh();
41 $scope.modal('createNewPolicy', true);
42 $scope.temp.policy = "";
45 $scope.modal = function(id, hide) {
46 return $('#' + id).modal(hide ? 'hide' : 'show');
49 if ($scope.temp.policy.ruleProvider == undefined) {
50 $scope.temp.policy.ruleProvider = "Custom";
53 if ($scope.temp.policy.blackListEntryType == undefined) {
54 $scope.temp.policy.blackListEntryType = "Use Manual Entry";
57 PolicyAppService.getData('getDictionary/get_OnapNameDataByName').then(function(data) {
59 $scope.data = JSON.parse(j.data);
60 console.log($scope.data);
61 $scope.onapNameDictionaryDatas = JSON.parse($scope.data.onapNameDictionaryDatas);
62 console.log($scope.onapNameDictionaryDatas);
64 console.log("failed");
67 PolicyAppService.getData('getDictionary/get_SettingsDictionaryDataByName').then(function(data) {
69 $scope.data = JSON.parse(j.data);
70 console.log($scope.data);
71 $scope.settingsDictionaryDatas = JSON.parse($scope.data.settingsDictionaryDatas);
72 console.log($scope.settingsDictionaryDatas);
74 console.log("failed");
77 PolicyAppService.getData('get_FunctionDefinitionDataByName').then(function(data) {
79 $scope.data = JSON.parse(j.data);
80 console.log($scope.data);
81 $scope.functionDefinitionDatas = JSON.parse($scope.data.functionDefinitionDatas);
82 console.log($scope.functionDefinitionDatas);
84 console.log("failed");
87 PolicyAppService.getData('getDictionary/get_AttributeDatabyAttributeName').then(function(data) {
89 $scope.data = JSON.parse(j.data);
90 console.log($scope.data);
91 $scope.attributeDictionaryDatas = JSON.parse($scope.data.attributeDictionaryDatas);
92 console.log($scope.attributeDictionaryDatas);
94 console.log("failed");
97 PolicyAppService.getData('getDictionary/get_RainyDayDictionaryDataByName').then(function(data) {
99 $scope.data = JSON.parse(j.data);
100 console.log($scope.data);
101 $scope.rainyDayDictionaryDatas = JSON.parse($scope.data.rainyDayDictionaryDatas);
102 console.log($scope.rainyDayDictionaryDatas);
104 console.log("failed");
107 PolicyAppService.getData('getDictionary/get_RainyDayDictionaryData').then(function(data) {
109 $scope.data = JSON.parse(j.data);
110 console.log($scope.data);
111 $scope.rainyDayDictionaryDataEntity = JSON.parse($scope.data.rainyDayDictionaryDatas);
112 console.log($scope.rainyDayDictionaryDatasEntity);
114 console.log("failed");
117 function extend(obj, src) {
118 for (var key in src) {
119 if (src.hasOwnProperty(key))
125 $scope.saveDecisionPolicy = function(policy) {
126 if (policy.itemContent != undefined) {
127 $scope.refreshCheck = true;
128 $scope.policyNavigator = policy.itemContent;
129 policy.itemContent = "";
131 $scope.savebutton = false;
133 var uuu = "policycreation/save_policy";
141 contentType : 'application/json',
142 data : JSON.stringify(postData),
143 success : function(data) {
144 $scope.$apply(function() {
145 $scope.data = data.policyData;
146 if ($scope.data == 'success') {
147 $scope.temp.policy.readOnly = 'true';
148 Notification.success("Policy Saved Successfully.");
149 } else if ($scope.data == 'PolicyExists') {
150 $scope.savebutton = true;
151 Notification.error("Policy Already Exists with Same Name in Scope.");
154 console.log($scope.data);
157 error : function(data) {
158 Notification.error("Error Occured while saving Policy.");
163 $scope.validatePolicy = function(policy) {
165 document.getElementById("validate").innerHTML = "";
166 var uuu = "policyController/validate_policy.htm";
174 contentType : 'application/json',
175 data : JSON.stringify(postData),
176 success : function(data) {
177 $scope.$apply(function() {
178 $scope.validateData = data.data.replace(/\"/g, "");
179 $scope.data = data.data;
180 if ($scope.data == '"success"') {
181 Notification.success("Validation Success.");
182 $scope.savebutton = false;
184 Notification.error("Validation Failed.");
185 document.getElementById("validate").innerHTML = $scope.validateData;
186 document.getElementById("validate").style.color = "white";
187 document.getElementById("validate").style.backgroundColor = "red";
188 $scope.savebutton = true;
192 console.log($scope.data);
194 error : function(data) {
195 Notification.error("Validation Failed.");
196 $scope.savebutton = true;
201 if (!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly) {
202 $scope.temp.policy.attributes = [];
203 $scope.temp.policy.settings = [];
204 $scope.temp.policy.ruleAlgorithmschoices = [];
205 if (!$scope.temp.policy.yamlparams) {
206 $scope.temp.policy.yamlparams = {};
208 if (!$scope.temp.policy.yamlparams.targets) {
209 $scope.temp.policy.yamlparams.targets = [];
211 if (!$scope.temp.policy.yamlparams.blackList) {
212 $scope.temp.policy.yamlparams.blackList = [];
214 if (!$scope.temp.policy.rainyday) {
215 $scope.temp.policy.rainyday = {};
217 if (!$scope.temp.policy.rainyday.treatmentTableChoices) {
218 $scope.temp.policy.rainyday.treatmentTableChoices = [];
221 } else if ($scope.temp.policy.ruleProvider == "Custom") {
222 if ($scope.temp.policy.attributes.length == 0) {
223 $scope.temp.policy.attributes = [];
225 if ($scope.temp.policy.settings.length == 0) {
226 $scope.temp.policy.settings = [];
228 if ($scope.temp.policy.ruleAlgorithmschoices == null || $scope.temp.policy.ruleAlgorithmschoices.length == 0) {
229 $scope.temp.policy.ruleAlgorithmschoices = [];
231 } else if ($scope.temp.policy.ruleProvider == "GUARD_BL_YAML") {
232 if ($scope.temp.policy.yamlparams.blackList == null || $scope.temp.policy.yamlparams.blackList.length == 0) {
233 $scope.temp.policy.yamlparams.blackList = [];
235 if ($scope.temp.policy.blackListEntries == null || $scope.temp.policy.blackListEntries.length == 0) {
236 $scope.temp.policy.blackListEntries = [];
238 $scope.blackListEntries = [];
239 $scope.temp.policy.appendBlackListEntries = [];
240 $scope.blackListEntries = arrayUnique($scope.temp.policy.blackListEntries.concat($scope.temp.policy.yamlparams.blackList));
241 } else if ($scope.temp.policy.ruleProvider == "GUARD_YAML") {
242 if ($scope.temp.policy.yamlparams.targets.length == 0) {
243 $scope.temp.policy.yamlparams.targets = [];
245 } else if ($scope.temp.policy.ruleProvider == "Rainy_Day") {
246 if ($scope.temp.policy.rainyday.treatmentTableChoices == null || $scope.temp.policy.rainyday.treatmentTableChoices.length == 0) {
247 $scope.temp.policy.rainyday.treatmentTableChoices = [];
250 $scope.attributeDatas = [ {
251 "attributes" : $scope.temp.policy.attributes
253 $scope.addNewChoice = function() {
254 var newItemNo = $scope.temp.policy.attributes.length + 1;
255 $scope.temp.policy.attributes.push({
256 'id' : 'choice' + newItemNo
259 $scope.removeChoice = function() {
260 var lastItem = $scope.temp.policy.attributes.length - 1;
261 $scope.temp.policy.attributes.splice(lastItem);
264 $scope.settingsDatas = [ {
265 "settings" : $scope.temp.policy.settings
267 $scope.addNewSettingsChoice = function() {
268 var newItemNo = $scope.temp.policy.settings.length + 1;
269 $scope.temp.policy.settings.push({
270 'id' : 'choice' + newItemNo
273 $scope.removeSettingsChoice = function() {
274 var lastItem = $scope.temp.policy.settings.length - 1;
275 $scope.temp.policy.settings.splice(lastItem);
278 $scope.addNewTarget = function() {
279 $scope.temp.policy.yamlparams.targets.push('');
281 $scope.removeTarget = function() {
282 var lastItem = $scope.temp.policy.yamlparams.targets.length - 1;
283 $scope.temp.policy.yamlparams.targets.splice(lastItem);
286 $scope.addNewBL = function() {
287 $scope.temp.policy.yamlparams.blackList.push('');
290 $scope.removeBL = function(id) {
291 $scope.temp.policy.yamlparams.blackList = $scope.temp.policy.yamlparams.blackList.filter(function(obj) {
296 $scope.treatmentDatas = [ {
297 "treatmentValues" : $scope.temp.policy.rainyday.treatmentTableChoices
299 $scope.addNewTreatment = function() {
300 $scope.temp.policy.rainyday.treatmentTableChoices.push({});
302 $scope.removeTreatment = function() {
303 var lastItem = $scope.temp.policy.rainyday.treatmentTableChoices.length - 1;
304 $scope.temp.policy.rainyday.treatmentTableChoices.splice(lastItem);
307 $scope.workstepDictionaryDatas = [];
308 $scope.getWorkstepValues = function(bbidValue) {
309 for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
310 var obj = $scope.rainyDayDictionaryDataEntity[i];
311 if (obj.bbid == bbidValue) {
312 $scope.workstepDictionaryDatas.push(obj.workstep);
317 $scope.allowedTreatmentsDatas = [];
318 $scope.getTreatmentValues = function(bbidValue, workstepValue) {
319 for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
320 var obj = $scope.rainyDayDictionaryDataEntity[i];
321 if (obj.bbid == bbidValue && obj.workstep == workstepValue) {
322 var splitAlarm = obj.treatments.split(',');
323 for (var j = 0; j < splitAlarm.length; ++j) {
324 $scope.allowedTreatmentsDatas.push(splitAlarm[j]);
331 $scope.ruleAlgorithmDatas = [ {
332 "ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices
335 $scope.addNewRuleAlgorithm = function() {
336 if ($scope.temp.policy.ruleAlgorithmschoices != null) {
337 var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length + 1;
342 var value = newItemNo - 1;
343 $scope.attributeDictionaryDatas.push('A' + value);
345 $scope.temp.policy.ruleAlgorithmschoices.push({
346 'id' : 'A' + newItemNo
350 $scope.removeRuleAlgorithm = function() {
351 var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length - 1;
352 $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
355 $scope.providerListener = function(ruleProvider) {
356 if (ruleProvider != "Custom") {
357 $scope.temp.policy.ruleAlgorithmschoices = [];
358 $scope.temp.policy.settings = [];
359 $scope.temp.policy.attributes = [];
361 if (ruleProvider === "Raw") {
362 $scope.notRawPolicy = false;
366 $scope.importButton = true;
368 $scope.uploadBLFile = function(files) {
370 fd.append("file", files[0]);
371 var fileExtension = files[0].name.split(".")[1];
372 if (fileExtension == "xls") {
373 $scope.importButton = false;
376 Notification.error("Upload the BlackList file which extends with .xls format.");
380 function arrayUnique(array) {
381 var a = array.concat();
382 for (var i = 0; i < a.length; ++i) {
383 for (var j = i + 1; j < a.length; ++j) {
391 $scope.submitUpload = function() {
392 $http.post("policycreation/importBlackListForDecisionPolicy", fd, {
393 withCredentials : false,
395 'Content-Type' : undefined
397 transformRequest : angular.identity
398 }).success(function(data) {
399 $scope.data = JSON.parse(data.data);
400 $scope.temp.policy.blackListEntries = $scope.data.blackListEntries;
401 if ($scope.temp.policy.blackListEntries[0] !== "error") {
402 $scope.blackListEntries = arrayUnique($scope.temp.policy.blackListEntries.concat($scope.temp.policy.yamlparams.blackList));
403 $scope.temp.policy.appendBlackListEntries = $scope.data.appendBlackListEntries;
404 $scope.blackListEntries = $scope.blackListEntries.filter(function(obj) {
405 return !$scope.temp.policy.appendBlackListEntries.includes(obj);
407 if ($scope.blackListEntries.length == 0) {
408 $scope.validateButton = true;
409 Notification.error("Black Lists are empty. Minimum one entry required.");
411 $scope.temp.policy.blackListEntries = $scope.blackListEntries;
412 Notification.success("Blacklist File Uploaded Successfully.");
413 $scope.validateButton = false;
414 $scope.importButton = true;
417 Notification.error("Blacklist File Upload Failed." + $scope.temp.policy.blackListEntries[1]);
419 }).error(function(data) {
420 Notification.error("Blacklist File Upload Failed.");
424 $scope.initializeBlackList = function() {
425 if ($scope.temp.policy.blackListEntryType === "Use File Upload") {
426 $scope.validateButton = true;
428 $scope.validateButton = false;
430 $("#importFile").val('');
433 $scope.exportBlackListEntries = function() {
434 var uuu = "policycreation/exportDecisionBlackListEntries";
436 policyData : $scope.temp.policy,
437 date : $scope.temp.model.modifiedDate,
438 version : $scope.temp.model.version
444 contentType : 'application/json',
445 data : JSON.stringify(postData),
446 success : function(data) {
447 $scope.$apply(function() {
448 $scope.data = data.data;
449 var url = '../' + $scope.data;
450 window.location = url;
451 Notification.success("BlackList Entries Exported Successfully.");
453 console.log($scope.data);
455 error : function(data) {
456 Notification.error("Error Occured while Exporting BlackList Entries.");