Fixed Sonar issues for Console Logs
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / FirewallPolicyController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 angular.module('abs').controller('fwPolicyController', 
21     ['$scope', '$window', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification', 
22     function ($scope, $window, PolicyAppService, PolicyNavigator, modalService, $modal, Notification) {
23     $("#dialog").hide();
24     
25     $scope.policyNavigator;
26     $scope.savebutton = true;
27     $scope.refreshCheck = false;
28     
29     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
30     $scope.temp.policy = {
31         policyType : "Config",
32         configPolicyType : "Firewall Config"
33     }
34     };
35     
36     $scope.refresh = function(){
37     if($scope.refreshCheck){
38         $scope.policyNavigator.refresh();
39     }
40     $scope.modal('createNewPolicy', true);
41     $scope.temp.policy = "";
42     };
43     
44     $scope.modal = function(id, hide) {
45         return $('#' + id).modal(hide ? 'hide' : 'show');
46     };
47     
48     $('#ttlDate').datepicker({
49     dateFormat: 'dd/mm/yy',
50     changeMonth: true,
51     changeYear: true,
52     onSelect: function(date) {
53         angular.element($('#ttlDate')).triggerHandler('input');
54     }
55     });
56     
57     PolicyAppService.getData('getDictionary/get_SecurityZoneDataByName').then(function (data) {
58     var j = data;
59     $scope.data = JSON.parse(j.data);
60     $scope.securityZoneDictionaryDatas = JSON.parse($scope.data.securityZoneDictionaryDatas);
61     });
62
63     PolicyAppService.getData('getDictionary/get_TermListDataByName').then(function (data) {
64     var j = data;
65     $scope.data = JSON.parse(j.data);
66     $scope.termListDictionaryDatas = JSON.parse($scope.data.termListDictionaryDatas);
67     });
68
69     PolicyAppService.getData('getDictionary/get_FWDictionaryListDataByName').then(function (data) {
70     var j = data;
71     $scope.data = JSON.parse(j.data);
72     $scope.fwDictListDictionaryDatas = JSON.parse($scope.data.fwDictListDictionaryDatas);
73     });
74
75     PolicyAppService.getData('getDictionary/get_FWParentListDataByName').then(function (data) {
76     var j = data;
77     $scope.data = JSON.parse(j.data);
78     $scope.fwParentListDictionaryDatas = JSON.parse($scope.data.fwParentListDictionaryDatas);
79     });
80
81     PolicyAppService.getData('getDictionary/get_TagPickerNameByName').then(function (data) {
82     var j = data;
83     $scope.data = JSON.parse(j.data);
84     $scope.fwTagPickerDictionaryDatas = JSON.parse($scope.data.fwTagPickerDictionaryDatas);
85     });
86
87     PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
88     var j = data;
89     $scope.data = JSON.parse(j.data);
90     $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
91     });
92
93     $scope.viewFWRule = function(policy){
94         var uuu = "policyController/ViewFWPolicyRule.htm";
95         var postData={policyData: policy};
96         $.ajax({
97             type : 'POST',
98             url : uuu,
99             dataType: 'json',
100             contentType: 'application/json',
101             data: JSON.stringify(postData),
102             success : function(data){
103                 $scope.$apply(function(){
104                 window.alert(data.policyData);
105                 });
106             },
107             error : function(data){
108             Notification.error("Error Occured while Showing Rule.");
109             }
110         });
111     };
112     
113     
114     $scope.saveFWPolicy = function(policy){
115     if(policy.itemContent != undefined){
116         $scope.refreshCheck = true; 
117         $scope.policyNavigator = policy.itemContent;
118         policy.itemContent = "";
119     }
120     $scope.savebutton = false;
121         var uuu = "policycreation/save_policy";
122     var postData={policyData: policy};
123     $.ajax({
124     type : 'POST',
125     url : uuu,
126     dataType: 'json',
127     contentType: 'application/json',
128     data: JSON.stringify(postData),
129     success : function(data){
130         $scope.$apply(function(){
131         $scope.data=data.policyData;
132         if($scope.data == 'success'){
133             $scope.temp.policy.readOnly = 'true';
134             $scope.pushStatus=data.policyData.split("&")[1];
135             if($scope.pushStatus=="successPush"){
136             Notification.success("Policy pushed successfully");
137             }
138             Notification.success("Policy Saved Successfully."); 
139         }else if ($scope.data == 'PolicyExists'){
140             $scope.savebutton = true;
141             Notification.error("Policy Already Exists with Same Name in Scope.");
142         }
143         });
144     },
145     error : function(data){
146         Notification.error("Error Occured while saving Policy.");
147     }
148     });
149     };
150     
151     $scope.validatePolicy = function(policy){
152     document.getElementById("validate").innerHTML = "";
153          var uuu = "policyController/validate_policy.htm";
154      var postData={policyData: policy};
155      $.ajax({
156      type : 'POST',
157      url : uuu,
158      dataType: 'json',
159      contentType: 'application/json',
160      data: JSON.stringify(postData),
161      success : function(data){
162          $scope.$apply(function(){
163          $scope.validateData = data.data.replace(/\"/g, "");
164             $scope.data=data.data.substring(1,8);
165              var size = data.data.length;
166              if($scope.data == 'success'){
167              Notification.success("Validation Success.");
168              $scope.savebutton = false;
169              if (size > 18){
170                  var displayWarning = data.data.substring(19,size);
171                  document.getElementById("validate").innerHTML = "Safe Policy Warning Message  :  "+displayWarning;
172                  document.getElementById("validate").style.color = "white";
173                  document.getElementById("validate").style.backgroundColor = "skyblue";
174              }  
175              }else{
176              Notification.error("Validation Failed.");
177              document.getElementById("validate").innerHTML = $scope.validateData;
178              document.getElementById("validate").style.color = "white";
179              document.getElementById("validate").style.backgroundColor = "red";
180              $scope.savebutton = true;
181              }
182              
183          });
184          
185      },
186      error : function(data){
187          Notification.error("Validation Failed.");
188          $scope.savebutton = true; 
189      }
190      });
191     };
192     
193     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
194     $scope.temp.policy.attributes = [];
195     $scope.temp.policy.fwattributes = [];
196     }else{
197            if($scope.temp.policy.attributes.length == 0){
198        $scope.temp.policy.attributes = [];
199            }
200            if($scope.temp.policy.fwPolicyType == 'Parent Policy'){
201        if($scope.temp.policy.fwattributes.length == 0){
202        $scope.temp.policy.fwattributes = [];
203        }
204            }
205    }
206     
207     $scope.attributeDatas = [{"ruleSetup" : $scope.temp.policy.attributes}];
208     $scope.addNewChoice = function() {
209       var newItemNo = $scope.temp.policy.attributes.length+1;
210       $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
211     };    
212     $scope.removeChoice = function() {
213       var lastItem = $scope.temp.policy.attributes.length-1;
214       $scope.temp.policy.attributes.splice(lastItem);
215     };
216     
217     $scope.fwattributeDatas = [{"parentSetup" : $scope.temp.policy.fwattributes}];
218     $scope.addNewFWDictList = function() {
219       var newItemNo = $scope.temp.policy.fwattributes.length+1;
220       $scope.temp.policy.fwattributes.push({'id':'choice'+newItemNo});
221     };    
222     $scope.removefwDictChoice = function() {
223       var lastItem = $scope.temp.policy.fwattributes.length-1;
224       $scope.temp.policy.fwattributes.splice(lastItem);
225     };
226         
227 }]);