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