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