Merge "Policy 1707 commit to LF"
[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', function ($scope, $window, PolicyAppService, modalService, $modal, Notification) {
21     $("#dialog").hide();
22     
23     $scope.savebutton = true;
24     $scope.finalPath = null;
25                 
26     PolicyAppService.getData('getDictionary/get_SecurityZoneDataByName').then(function (data) {
27         var j = data;
28         $scope.data = JSON.parse(j.data);
29         console.log($scope.data);
30         $scope.securityZoneDictionaryDatas = JSON.parse($scope.data.securityZoneDictionaryDatas);
31         console.log($scope.securityZoneDictionaryDatas);
32     }, function (error) {
33         console.log("failed");
34     });
35
36     PolicyAppService.getData('getDictionary/get_TermListDataByName').then(function (data) {
37         var j = data;
38         $scope.data = JSON.parse(j.data);
39         console.log($scope.data);
40         $scope.termListDictionaryDatas = JSON.parse($scope.data.termListDictionaryDatas);
41         console.log($scope.termListDictionaryDatas);
42     }, function (error) {
43         console.log("failed");
44     });
45
46     PolicyAppService.getData('getDictionary/get_FWDictionaryListDataByName').then(function (data) {
47         var j = data;
48         $scope.data = JSON.parse(j.data);
49         console.log($scope.data);
50         $scope.fwDictListDictionaryDatas = JSON.parse($scope.data.fwDictListDictionaryDatas);
51         console.log($scope.fwDictListDictionaryDatas);
52     }, function (error) {
53         console.log("failed");
54     });
55
56     PolicyAppService.getData('getDictionary/get_FWParentListDataByName').then(function (data) {
57         var j = data;
58         $scope.data = JSON.parse(j.data);
59         console.log($scope.data);
60         $scope.fwParentListDictionaryDatas = JSON.parse($scope.data.fwParentListDictionaryDatas);
61         console.log($scope.fwParentListDictionaryDatas);
62     }, function (error) {
63         console.log("failed");
64     });
65
66     PolicyAppService.getData('getDictionary/get_TagPickerNameByName').then(function (data) {
67         var j = data;
68         $scope.data = JSON.parse(j.data);
69         console.log($scope.data);
70         $scope.fwTagPickerDictionaryDatas = JSON.parse($scope.data.fwTagPickerDictionaryDatas);
71         console.log($scope.fwTagPickerDictionaryDatas);
72     }, function (error) {
73         console.log("failed");
74     });
75
76     PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
77         var j = data;
78         $scope.data = JSON.parse(j.data);
79         console.log($scope.data);
80         $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
81         console.log($scope.riskTypeDictionaryDatas);
82     }, function (error) {
83         console.log("failed");
84     });
85
86     function extend(obj, src) {
87         for (var key in src) {
88             if (src.hasOwnProperty(key)) obj[key] = src[key];
89         }
90         return obj;
91     }
92     
93     $scope.viewFWRule = function(policy){
94         console.log(policy);
95         var uuu = "policyController/ViewFWPolicyRule.htm";
96         var postData={policyData: policy};
97         $.ajax({
98             type : 'POST',
99             url : uuu,
100             dataType: 'json',
101             contentType: 'application/json',
102             data: JSON.stringify(postData),
103             success : function(data){
104                 $scope.$apply(function(){
105                         window.alert(data.policyData);
106                 });
107             },
108             error : function(data){
109                 Notification.error("Error Occured while Showing Rule.");
110             }
111         });
112     };
113     
114     
115     $scope.saveFWPolicy = function(policy){
116         $scope.savebutton = false;
117         console.log(policy);
118         var uuu = "policycreation/save_policy";
119                 var postData={policyData: policy};
120                 $.ajax({
121                         type : 'POST',
122                         url : uuu,
123                         dataType: 'json',
124                         contentType: 'application/json',
125                         data: JSON.stringify(postData),
126                         success : function(data){
127                                 $scope.$apply(function(){
128                                         $scope.data=data.policyData;
129                                         if($scope.data == 'success'){
130                                                 $scope.temp.policy.readOnly = 'true';
131                                                 $scope.pushStatus=data.policyData.split("&")[1];
132                                                 if($scope.pushStatus=="successPush"){
133                                                         Notification.success("Policy pushed successfully");
134                                                 }
135                                                 Notification.success("Policy Saved Successfully.");     
136                                         }else if ($scope.data == 'PolicyExists'){
137                                                 $scope.savebutton = true;
138                                                 Notification.error("Policy Already Exists with Same Name in Scope.");
139                                         }
140                                 });
141                                 console.log($scope.data);
142                         },
143                         error : function(data){
144                                 Notification.error("Error Occured while saving Policy.");
145                         }
146                 });
147     };
148     
149     $scope.validatePolicy = function(policy){
150         console.log(policy);
151         document.getElementById("validate").innerHTML = "";
152          var uuu = "policyController/validate_policy.htm";
153                 var postData={policyData: policy};
154                 $.ajax({
155                         type : 'POST',
156                         url : uuu,
157                         dataType: 'json',
158                         contentType: 'application/json',
159                         data: JSON.stringify(postData),
160                         success : function(data){
161                                 $scope.$apply(function(){
162                                         $scope.validateData = data.data.replace(/\"/g, "");
163                                                 $scope.data=data.data.substring(1,8);
164                                                 var size = data.data.length;
165                                                 if($scope.data == 'success'){
166                                                         Notification.success("Validation Success.");
167                                                         $scope.savebutton = false;
168                                                         if (size > 18){
169                                                                 var displayWarning = data.data.substring(19,size);
170                                                                 document.getElementById("validate").innerHTML = "Safe Policy Warning Message  :  "+displayWarning;
171                                                                 document.getElementById("validate").style.color = "white";
172                                                                 document.getElementById("validate").style.backgroundColor = "skyblue";
173                                                         }       
174                                                 }else{
175                                                         Notification.error("Validation Failed.");
176                                                         document.getElementById("validate").innerHTML = $scope.validateData;
177                                                         document.getElementById("validate").style.color = "white";
178                                                         document.getElementById("validate").style.backgroundColor = "red";
179                                                         $scope.savebutton = true;
180                                                 }
181                                                 
182                                 });
183                                 console.log($scope.data);
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 });