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