Merge "Fixed TOSCA parsing bugs"
[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     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     function extend(obj, src) {
116         for (var key in src) {
117             if (src.hasOwnProperty(key)) obj[key] = src[key];
118         }
119         return obj;
120     }
121     
122     $scope.viewFWRule = function(policy){
123         console.log(policy);
124         var uuu = "policyController/ViewFWPolicyRule.htm";
125         var postData={policyData: policy};
126         $.ajax({
127             type : 'POST',
128             url : uuu,
129             dataType: 'json',
130             contentType: 'application/json',
131             data: JSON.stringify(postData),
132             success : function(data){
133                 $scope.$apply(function(){
134                         window.alert(data.policyData);
135                 });
136             },
137             error : function(data){
138                 Notification.error("Error Occured while Showing Rule.");
139             }
140         });
141     };
142     
143     
144     $scope.saveFWPolicy = function(policy){
145         if(policy.itemContent != undefined){
146                 $scope.refreshCheck = true; 
147                 $scope.policyNavigator = policy.itemContent;
148                 policy.itemContent = "";
149         }
150         $scope.savebutton = false;
151         console.log(policy);
152         var uuu = "policycreation/save_policy";
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.data=data.policyData;
163                                         if($scope.data == 'success'){
164                                                 $scope.temp.policy.readOnly = 'true';
165                                                 $scope.pushStatus=data.policyData.split("&")[1];
166                                                 if($scope.pushStatus=="successPush"){
167                                                         Notification.success("Policy pushed successfully");
168                                                 }
169                                                 Notification.success("Policy Saved Successfully.");     
170                                         }else if ($scope.data == 'PolicyExists'){
171                                                 $scope.savebutton = true;
172                                                 Notification.error("Policy Already Exists with Same Name in Scope.");
173                                         }
174                                 });
175                                 console.log($scope.data);
176                         },
177                         error : function(data){
178                                 Notification.error("Error Occured while saving Policy.");
179                         }
180                 });
181     };
182     
183     $scope.validatePolicy = function(policy){
184         console.log(policy);
185         document.getElementById("validate").innerHTML = "";
186          var uuu = "policyController/validate_policy.htm";
187                 var postData={policyData: policy};
188                 $.ajax({
189                         type : 'POST',
190                         url : uuu,
191                         dataType: 'json',
192                         contentType: 'application/json',
193                         data: JSON.stringify(postData),
194                         success : function(data){
195                                 $scope.$apply(function(){
196                                         $scope.validateData = data.data.replace(/\"/g, "");
197                                                 $scope.data=data.data.substring(1,8);
198                                                 var size = data.data.length;
199                                                 if($scope.data == 'success'){
200                                                         Notification.success("Validation Success.");
201                                                         $scope.savebutton = false;
202                                                         if (size > 18){
203                                                                 var displayWarning = data.data.substring(19,size);
204                                                                 document.getElementById("validate").innerHTML = "Safe Policy Warning Message  :  "+displayWarning;
205                                                                 document.getElementById("validate").style.color = "white";
206                                                                 document.getElementById("validate").style.backgroundColor = "skyblue";
207                                                         }       
208                                                 }else{
209                                                         Notification.error("Validation Failed.");
210                                                         document.getElementById("validate").innerHTML = $scope.validateData;
211                                                         document.getElementById("validate").style.color = "white";
212                                                         document.getElementById("validate").style.backgroundColor = "red";
213                                                         $scope.savebutton = true;
214                                                 }
215                                                 
216                                 });
217                                 console.log($scope.data);
218                                 
219                         },
220                         error : function(data){
221                                 Notification.error("Validation Failed.");
222                                 $scope.savebutton = true; 
223                         }
224                 });
225     };
226     
227     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
228         $scope.temp.policy.attributes = [];
229         $scope.temp.policy.fwattributes = [];
230     }else{
231            if($scope.temp.policy.attributes.length == 0){
232                    $scope.temp.policy.attributes = [];
233            }
234            if($scope.temp.policy.fwPolicyType == 'Parent Policy'){
235                    if($scope.temp.policy.fwattributes.length == 0){
236                            $scope.temp.policy.fwattributes = [];
237                    }
238            }
239    }
240     
241     $scope.attributeDatas = [{"ruleSetup" : $scope.temp.policy.attributes}];
242     $scope.addNewChoice = function() {
243       var newItemNo = $scope.temp.policy.attributes.length+1;
244       $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
245     };    
246     $scope.removeChoice = function() {
247       var lastItem = $scope.temp.policy.attributes.length-1;
248       $scope.temp.policy.attributes.splice(lastItem);
249     };
250     
251     $scope.fwattributeDatas = [{"parentSetup" : $scope.temp.policy.fwattributes}];
252     $scope.addNewFWDictList = function() {
253       var newItemNo = $scope.temp.policy.fwattributes.length+1;
254       $scope.temp.policy.fwattributes.push({'id':'choice'+newItemNo});
255     };    
256     $scope.removefwDictChoice = function() {
257       var lastItem = $scope.temp.policy.fwattributes.length-1;
258       $scope.temp.policy.fwattributes.splice(lastItem);
259     };
260         
261 }]);