Fixed Sonar issues for Console Logs
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / dictionaryController / FWTermListDictController.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 app.controller('editFWTermListController' , function ($scope, $modalInstance, message, PolicyAppService, UserInfoServiceDS2, Notification){
21          $scope.fromZonechoices = [];
22      $scope.toZonechoices = [];
23      $scope.sourceListchoices = [];
24      $scope.destinationListchoices = [];
25      $scope.sourceServicechoices = [];
26      $scope.destinationServicechoices = [];
27      $scope.actionListchoices = [];
28      $scope.groupAddresses=[];
29      $scope.groupServices=[];
30     if(message.termListDictionaryData==null){
31         $scope.label='Add Term List Name'
32     }else{
33         $scope.label='Edit Term List Name'
34         $scope.disableCd=true; 
35         var fromZoneheaders = message.termListDictionaryData.fromZone;
36         var splitFromZone = ',';
37         if(fromZoneheaders != null){
38                 if (fromZoneheaders.indexOf(splitFromZone) >= 0) {
39                         var splitFromZoneValue = fromZoneheaders.split(splitFromZone);
40                         for(i = 0; i < splitFromZoneValue.length; i++){
41                                 var key  = splitFromZoneValue[i];
42                                 $scope.fromZonechoices.push({'id':'choice'+i+1, 'option': key});
43                         }
44                 }else{
45                  var key  = fromZoneheaders;
46                  $scope.fromZonechoices.push({'id':'choice'+1, 'option': key});
47             }
48         }
49         
50         var toZoneheaders = message.termListDictionaryData.toZone;
51         var splitToZone = ',';
52         if(toZoneheaders != null){
53                 if (toZoneheaders.indexOf(splitToZone) >= 0) {
54                         var splitToZoneValue = toZoneheaders.split(splitToZone);
55                         for(i = 0; i < splitToZoneValue.length; i++){
56                                 var key  = splitToZoneValue[i];
57                                 $scope.toZonechoices.push({'id':'choice'+i+1, 'option': key});
58                         }
59                 }else{
60                  var key  = toZoneheaders;
61                  $scope.toZonechoices.push({'id':'choice'+1, 'option': key});
62             }
63         }
64         
65         var srcIPheaders = message.termListDictionaryData.srcIPList;
66         var splitSrcIP = ',';
67         if(srcIPheaders != null){
68                 if (srcIPheaders.indexOf(splitSrcIP) >= 0) {
69                         var splitSrcIPValue = srcIPheaders.split(splitSrcIP);
70                         for(i = 0; i < splitSrcIPValue.length; i++){
71                                 var key  = splitSrcIPValue[i];
72                                 $scope.sourceListchoices.push({'id':'choice'+i+1, 'option': key});
73                         }
74                 }else{
75                  var key  = srcIPheaders;
76                  $scope.sourceListchoices.push({'id':'choice'+1, 'option': key});
77             }
78         }
79         
80         var desIPheaders = message.termListDictionaryData.destIPList;
81         var splitDesIP = ',';
82         if(desIPheaders != null){
83                 if (desIPheaders.indexOf(splitDesIP) >= 0) {
84                         var splitDestIPValue = desIPheaders.split(splitDesIP);
85                         for(i = 0; i < splitDestIPValue.length; i++){
86                                 var key  = splitDestIPValue[i];
87                                 $scope.destinationListchoices.push({'id':'choice'+i+1, 'option': key});
88                         }
89                 }else{
90                  var key  = desIPheaders;
91                  $scope.destinationListchoices.push({'id':'choice'+1, 'option': key});
92             }
93         }
94         
95         var srcServheaders = message.termListDictionaryData.srcPortList;
96         var splitSrcServ = ',';
97         if(srcServheaders != null){
98                 if (srcServheaders.indexOf(splitSrcServ) >= 0) {
99                         var splitSrcServValue = srcServheaders.split(splitSrcServ);
100                         for(i = 0; i < splitSrcServValue.length; i++){
101                                 var key  = splitSrcServValue[i];
102                                 $scope.sourceServicechoices.push({'id':'choice'+i+1, 'option': key});
103                         }
104                 }else{
105                  var key  = srcServheaders;
106                  $scope.sourceServicechoices.push({'id':'choice'+1, 'option': key});
107             }
108         }
109         
110         var desServheaders = message.termListDictionaryData.destPortList;
111         var splitdesSer = ',';
112         if(desServheaders != null){
113                 if (desServheaders.indexOf(splitdesSer) >= 0) {
114                         var splitDesSerValue = desServheaders.split(splitdesSer);
115                         for(i = 0; i < splitDesSerValue.length; i++){
116                                 var key  = splitDesSerValue[i];
117                                 $scope.destinationServicechoices.push({'id':'choice'+i+1, 'option': key});
118                         }
119                 }else{
120                  var key  = desServheaders;
121                  $scope.destinationServicechoices.push({'id':'choice'+1, 'option': key});
122             }
123         }
124         
125         var actionheaders = message.termListDictionaryData.action;
126         var splitAction = ',';
127         if(actionheaders != null){
128                 if (actionheaders.indexOf(splitAction) >= 0) {
129                         var splitActionValue = actionheaders.split(splitAction);
130                         for(i = 0; i < splitActionValue.length; i++){
131                                 var key  = splitActionValue[i];
132                                 $scope.actionListchoices.push({'id':'choice'+i+1, 'option': key});
133                         }
134                 }else{
135                  var key  = actionheaders;
136                  $scope.actionListchoices.push({'id':'choice'+1, 'option': key});
137             }
138         }
139     }
140
141                 
142     PolicyAppService.getData('getDictionary/get_PrefixListDataByName').then(function (data) {
143         var j = data;
144         $scope.data = JSON.parse(j.data);
145         $scope.prefixListDictionaryDatas = JSON.parse($scope.data.prefixListDictionaryDatas);
146         for(i = 0; i < $scope.prefixListDictionaryDatas.length; i++){
147                         var key  = $scope.prefixListDictionaryDatas[i];
148                         $scope.groupAddresses.push(key);
149                 }
150     });
151
152     PolicyAppService.getData('getDictionary/get_ZoneDictionaryDataByName').then(function (data) {
153         var j = data;
154         $scope.data = JSON.parse(j.data);
155         $scope.zoneDictionaryDatas = JSON.parse($scope.data.zoneDictionaryDatas);
156     });
157
158     PolicyAppService.getData('getDictionary/get_AddressGroupDictionaryDataByName').then(function (data) {
159         var j = data;
160         $scope.data = JSON.parse(j.data);
161         $scope.addressGroupDictionaryDatas = JSON.parse($scope.data.addressGroupDictionaryDatas);
162         for(i = 0; i < $scope.addressGroupDictionaryDatas.length; i++){
163                         var key  = $scope.addressGroupDictionaryDatas[i];
164                         $scope.groupAddresses.push(key);
165                 }
166     });
167
168     PolicyAppService.getData('getDictionary/get_ServiceListDictionaryDataByName').then(function (data) {
169         var j = data;
170         $scope.data = JSON.parse(j.data);
171         $scope.serviceListDictionaryDatas = JSON.parse($scope.data.serviceListDictionaryDatas);
172         for(i = 0; i < $scope.serviceListDictionaryDatas.length; i++){
173                         var key  = $scope.serviceListDictionaryDatas[i];
174                         $scope.groupServices.push(key);
175                 }
176     });
177
178     PolicyAppService.getData('getDictionary/get_ServiceGroupDictionaryDataByName').then(function (data) {
179         var j = data;
180         $scope.data = JSON.parse(j.data);
181         $scope.serviceGroupDictionaryDatas = JSON.parse($scope.data.serviceGroupDictionaryDatas);
182         for(i = 0; i < $scope.serviceGroupDictionaryDatas.length; i++){
183                         var key  = $scope.serviceGroupDictionaryDatas[i];
184                         $scope.groupServices.push(key);
185                 }
186     }, function (error) {
187     });
188
189     PolicyAppService.getData('getDictionary/get_ActionListDictionaryDataByName').then(function (data) {
190         var j = data;
191         $scope.data = JSON.parse(j.data);
192         $scope.actionListDictionaryDatas = JSON.parse($scope.data.actionListDictionaryDatas);
193     });
194
195         
196         /*getting user info from session*/
197         var userid = null;
198         UserInfoServiceDS2.getFunctionalMenuStaticDetailSession()
199                 .then(function (response) {                     
200                         userid = response.userid;               
201          });
202         
203     $scope.editTermList = message.termListDictionaryData;
204
205     $scope.saveTermName = function(termListDictionaryData) {
206         var regex = new RegExp("^[a-zA-Z0-9_]*$");
207         if(!regex.test(termListDictionaryData.termName)) {
208                 Notification.error("Enter Valid Term Name without spaces or special characters");
209         }else{
210                 var mergeFromZoneData = extend(termListDictionaryData, $scope.fromZoneDatas[0]);
211                 var mergeToData = extend(mergeFromZoneData, $scope.toZoneDatas[0]);
212                 var mergeSourceListData = extend(mergeToData, $scope.sourceListDatas[0]);
213                 var mergeDListData = extend(mergeSourceListData, $scope.destinationListDatas[0]);
214                 var mergeSServicesData = extend(mergeDListData, $scope.sourceServicesDatas[0]);
215                 var mergeDServicesData = extend(mergeSServicesData, $scope.destinationServicesDatas[0]);
216                 var finalData = extend(mergeDServicesData, $scope.actionListDatas[0]);
217                 var uuu = "saveDictionary/fw_dictionary/save_termList";
218                 var postData={termListDictionaryData: finalData, userid: userid};
219                 $.ajax({
220                         type : 'POST',
221                         url : uuu,
222                         dataType: 'json',
223                         contentType: 'application/json',
224                         data: JSON.stringify(postData),
225                         success : function(data){
226                                 $scope.$apply(function(){
227                                         $scope.termListDictionaryDatas=data.termListDictionaryDatas;});
228                                 if($scope.termListDictionaryDatas == "Duplicate"){
229                                         Notification.error("FW TermList Dictionary exists with Same Term Name.")
230                                 }else{      
231                                         $modalInstance.close({termListDictionaryDatas:$scope.termListDictionaryDatas});
232                                 }
233                         },
234                         error : function(data){
235                                 Notification.error("Error while saving.");
236                         }
237                 });
238         }
239     };
240
241     $scope.close = function() {
242         $modalInstance.close();
243     };
244     
245     
246     function extend(obj, src) {
247         for (var key in src) {
248             if (src.hasOwnProperty(key)) obj[key] = src[key];
249         }
250         return obj;
251     }
252     
253     $scope.fromZoneDatas = [{"fromZoneDatas" : $scope.fromZonechoices}];
254     $scope.addFromZoneNewChoice = function() {
255         var newItemNo = $scope.fromZonechoices.length+1;
256         $scope.fromZonechoices.push({'id':'choice'+newItemNo});
257     };
258     $scope.removeFromZoneChoice = function() {
259         var lastItem = $scope.fromZonechoices.length-1;
260         $scope.fromZonechoices.splice(lastItem);
261     };
262     
263     $scope.toZoneDatas = [{"toZoneDatas" : $scope.toZonechoices}];
264     $scope.addToZoneNewChoice = function() {
265         var newItemNo = $scope.toZonechoices.length+1;
266         $scope.toZonechoices.push({'id':'choice'+newItemNo});
267     };
268     $scope.removeToZoneChoice = function() {
269         var lastItem = $scope.toZonechoices.length-1;
270         $scope.toZonechoices.splice(lastItem);
271     };
272     
273     $scope.sourceListDatas = [{"sourceListDatas" : $scope.sourceListchoices}];
274     $scope.addSourceListNewChoice = function() {
275         var newItemNo = $scope.sourceListchoices.length+1;
276         $scope.sourceListchoices.push({'id':'choice'+newItemNo});
277     };
278     $scope.removeSourceListChoice = function() {
279         var lastItem = $scope.sourceListchoices.length-1;
280         $scope.sourceListchoices.splice(lastItem);
281     };
282     
283     $scope.destinationListDatas = [{"destinationListDatas" : $scope.destinationListchoices}];
284     $scope.addDListNewChoice = function() {
285         var newItemNo = $scope.destinationListchoices.length+1;
286         $scope.destinationListchoices.push({'id':'choice'+newItemNo});
287     };
288     $scope.removeDlistChoice = function() {
289         var lastItem = $scope.destinationListchoices.length-1;
290         $scope.destinationListchoices.splice(lastItem);
291     };
292     
293     $scope.sourceServicesDatas = [{"sourceServiceDatas" : $scope.sourceServicechoices}];
294     $scope.addSourceServiceNewChoice = function() {
295         var newItemNo = $scope.sourceServicechoices.length+1;
296         $scope.sourceServicechoices.push({'id':'choice'+newItemNo});
297     };
298     $scope.removeSourceServiceChoice = function() {
299         var lastItem = $scope.sourceServicechoices.length-1;
300         $scope.sourceServicechoices.splice(lastItem);
301     };
302     
303     $scope.destinationServicesDatas = [{"destinationServiceDatas" : $scope.destinationServicechoices}];
304     $scope.addDServicesNewChoice = function() {
305         var newItemNo = $scope.destinationServicechoices.length+1;
306         $scope.destinationServicechoices.push({'id':'choice'+newItemNo});
307     };
308     $scope.removeDServicesChoice = function() {
309         var lastItem = $scope.destinationServicechoices.length-1;
310         $scope.destinationServicechoices.splice(lastItem);
311     };
312     
313     $scope.actionListDatas = [{"actionListDatas" : $scope.actionListchoices}];
314     $scope.addActionListNewChoice = function() {
315         var newItemNo = $scope.actionListchoices.length+1;
316         $scope.actionListchoices.push({'id':'choice'+newItemNo});
317     };
318     $scope.removeActionListChoice = function() {
319         var lastItem = $scope.actionListchoices.length-1;
320         $scope.actionListchoices.splice(lastItem);
321     };
322 });