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