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));
242 }else if($scope.temp.policy.ruleProvider=="GUARD_YAML" || $scope.temp.policy.ruleProvider=="GUARD_MIN_MAX"){
243 if($scope.temp.policy.yamlparams.targets.length==0){
244 $scope.temp.policy.yamlparams.targets = [];
246 } else if ($scope.temp.policy.ruleProvider == "Rainy_Day") {
247 if ($scope.temp.policy.rainyday.treatmentTableChoices == null || $scope.temp.policy.rainyday.treatmentTableChoices.length == 0) {
248 $scope.temp.policy.rainyday.treatmentTableChoices = [];
251 $scope.attributeDatas = [ {
252 "attributes" : $scope.temp.policy.attributes
254 $scope.addNewChoice = function() {
255 var newItemNo = $scope.temp.policy.attributes.length + 1;
256 $scope.temp.policy.attributes.push({
257 'id' : 'choice' + newItemNo
260 $scope.removeChoice = function() {
261 var lastItem = $scope.temp.policy.attributes.length - 1;
262 $scope.temp.policy.attributes.splice(lastItem);
265 $scope.settingsDatas = [ {
266 "settings" : $scope.temp.policy.settings
268 $scope.addNewSettingsChoice = function() {
269 var newItemNo = $scope.temp.policy.settings.length + 1;
270 $scope.temp.policy.settings.push({
271 'id' : 'choice' + newItemNo
274 $scope.removeSettingsChoice = function() {
275 var lastItem = $scope.temp.policy.settings.length - 1;
276 $scope.temp.policy.settings.splice(lastItem);
279 $scope.addNewTarget = function() {
280 $scope.temp.policy.yamlparams.targets.push('');
282 $scope.removeTarget = function() {
283 var lastItem = $scope.temp.policy.yamlparams.targets.length - 1;
284 $scope.temp.policy.yamlparams.targets.splice(lastItem);
287 $scope.addNewBL = function() {
288 $scope.temp.policy.yamlparams.blackList.push('');
291 $scope.removeBL = function(id) {
292 $scope.temp.policy.yamlparams.blackList = $scope.temp.policy.yamlparams.blackList.filter(function (obj){
297 $scope.treatmentDatas = [ {
298 "treatmentValues" : $scope.temp.policy.rainyday.treatmentTableChoices
301 $scope.addNewTreatment = function() {
302 $scope.temp.policy.rainyday.treatmentTableChoices.push({});
305 $scope.removeTreatment = function() {
306 var lastItem = $scope.temp.policy.rainyday.treatmentTableChoices.length - 1;
307 $scope.temp.policy.rainyday.treatmentTableChoices.splice(lastItem);
310 $scope.workstepDictionaryDatas = [];
311 $scope.getWorkstepValues = function(bbidValue) {
312 for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
313 var obj = $scope.rainyDayDictionaryDataEntity[i];
314 if (obj.bbid == bbidValue) {
315 $scope.workstepDictionaryDatas.push(obj.workstep);
320 $scope.allowedTreatmentsDatas = [];
321 $scope.getTreatmentValues = function(bbidValue, workstepValue) {
322 for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
323 var obj = $scope.rainyDayDictionaryDataEntity[i];
324 if (obj.bbid == bbidValue && obj.workstep == workstepValue) {
325 var splitAlarm = obj.treatments.split(',');
326 for (var j = 0; j < splitAlarm.length; ++j) {
327 $scope.allowedTreatmentsDatas.push(splitAlarm[j]);
334 $scope.ruleAlgorithmDatas = [ {
335 "ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices
338 $scope.addNewRuleAlgorithm = function() {
339 if ($scope.temp.policy.ruleAlgorithmschoices != null) {
340 var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length + 1;
345 var value = newItemNo - 1;
346 $scope.attributeDictionaryDatas.push('A' + value);
348 $scope.temp.policy.ruleAlgorithmschoices.push({
349 'id' : 'A' + newItemNo
353 $scope.removeRuleAlgorithm = function() {
354 var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length - 1;
355 $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
358 $scope.providerListener = function(ruleProvider) {
359 if (ruleProvider != "Custom") {
360 $scope.temp.policy.ruleAlgorithmschoices = [];
361 $scope.temp.policy.settings = [];
362 $scope.temp.policy.attributes = [];
364 if (ruleProvider === "Raw") {
365 $scope.notRawPolicy = false;
369 $scope.importButton = true;
371 $scope.uploadBLFile = function(files) {
373 fd.append("file", files[0]);
374 var fileExtension = files[0].name.split(".")[1];
375 if (fileExtension == "xls") {
376 $scope.importButton = false;
379 Notification.error("Upload the BlackList file which extends with .xls format.");
383 function arrayUnique(array) {
384 var a = array.concat();
385 for (var i = 0; i < a.length; ++i) {
386 for (var j = i + 1; j < a.length; ++j) {
394 $scope.submitUpload = function() {
395 $http.post("policycreation/importBlackListForDecisionPolicy", fd, {
396 withCredentials : false,
398 'Content-Type' : undefined
400 transformRequest : angular.identity
401 }).success(function(data) {
402 $scope.data = JSON.parse(data.data);
403 $scope.temp.policy.blackListEntries = $scope.data.blackListEntries;
404 if ($scope.temp.policy.blackListEntries[0] !== "error") {
405 $scope.blackListEntries = arrayUnique($scope.temp.policy.blackListEntries.concat($scope.temp.policy.yamlparams.blackList));
406 $scope.temp.policy.appendBlackListEntries = $scope.data.appendBlackListEntries;
407 $scope.blackListEntries = $scope.blackListEntries.filter(function(obj) {
408 return !$scope.temp.policy.appendBlackListEntries.includes(obj);
410 if ($scope.blackListEntries.length == 0) {
411 $scope.validateButton = true;
412 Notification.error("Black Lists are empty. Minimum one entry required.");
414 $scope.temp.policy.blackListEntries = $scope.blackListEntries;
415 Notification.success("Blacklist File Uploaded Successfully.");
416 $scope.validateButton = false;
417 $scope.importButton = true;
420 Notification.error("Blacklist File Upload Failed." + $scope.temp.policy.blackListEntries[1]);
422 }).error(function(data) {
423 Notification.error("Blacklist File Upload Failed.");
427 $scope.initializeBlackList = function() {
428 if ($scope.temp.policy.blackListEntryType === "Use File Upload") {
429 $scope.validateButton = true;
431 $scope.validateButton = false;
433 $("#importFile").val('');
436 $scope.exportBlackListEntries = function() {
437 var uuu = "policycreation/exportDecisionBlackListEntries";
439 policyData : $scope.temp.policy,
440 date : $scope.temp.model.modifiedDate,
441 version : $scope.temp.model.version
447 contentType : 'application/json',
448 data : JSON.stringify(postData),
449 success : function(data) {
450 $scope.$apply(function() {
451 $scope.data = data.data;
452 var url = '../' + $scope.data;
453 window.location = url;
454 Notification.success("BlackList Entries Exported Successfully.");
456 console.log($scope.data);
458 error : function(data) {
459 Notification.error("Error Occured while Exporting BlackList Entries.");