[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / dictionaryController / FWServiceListDictController.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 app.controller('editFWServiceListController' ,  function ($scope, $modalInstance, message, PolicyAppService, UserInfoServiceDS2, Notification){
21         $scope.protocolListDictionaryDatas =[];
22
23         $scope.tpchoices = [];
24         $scope.apchoices = [];
25         if(message.serviceListDictionaryData==null){
26                 $scope.label='Add Service List'    
27         }else{
28                 $scope.label='Edit Service List'
29                         $scope.disableCd=true;
30                 var tcpheaders = message.serviceListDictionaryData.serviceTransProtocol;
31                 var splitEqual = ',';
32                 if(tcpheaders != null){
33                         if (tcpheaders.indexOf(splitEqual) >= 0) {
34                                 var splitValue = tcpheaders.split(splitEqual);
35                                 for(i = 0; i < splitValue.length; i++){
36                                         var key  = splitValue[i];
37                                         $scope.tpchoices.push({'id':'choice'+i+1, 'option': key});
38                                 }
39                         }else{
40                                 var key  = tcpheaders;
41                                 $scope.tpchoices.push({'id':'choice'+1, 'option': key});
42                         }
43                 }
44                 var appheaders = message.serviceListDictionaryData.serviceAppProtocol;
45                 var splitEqual1 = ',';
46                 if(appheaders != null){
47                         if (appheaders.indexOf(splitEqual1) >= 0) {
48                                 var splitValue1 = appheaders.split(splitEqual1);
49                                 for(i = 0; i < splitValue1.length; i++){
50                                         var key1  = splitValue1[i];
51                                         $scope.apchoices.push({'id':'choice'+i+1, 'option': key1});
52                                 }
53                         }else{
54                                 var key1  = appheaders;
55                                 $scope.apchoices.push({'id':'choice'+1, 'option': key1});
56                         }
57                 }
58         }
59
60                 
61         PolicyAppService.getData('getDictionary/get_ProtocolListDataByName').then(function (data) {
62                 var j = data;
63                 $scope.data = JSON.parse(j.data);
64                 console.log($scope.data);
65                 $scope.protocolListDictionaryDatas = JSON.parse($scope.data.protocolListDictionaryDatas);
66                 console.log($scope.protocolListDictionaryDatas);
67         }, function (error) {
68                 console.log("failed");
69         });
70
71         
72         /*getting user info from session*/
73         var userid = null;
74         UserInfoServiceDS2.getFunctionalMenuStaticDetailSession()
75                 .then(function (response) {                     
76                         userid = response.userid;               
77          });
78         
79         $scope.editServiceList = message.serviceListDictionaryData;
80
81         $scope.saveFWServiceList = function(serviceListDictionaryData) {
82                 var regex = new RegExp("^[a-zA-Z0-9_]*$");
83                 if(!regex.test(serviceListDictionaryData.serviceName)) {
84                         Notification.error("Enter Valid ServiceList Name without spaces or special characters");
85                 }else{
86                         var addtcpData = extend(serviceListDictionaryData, $scope.attributeTCPDatas[0]);
87                         var finalData = extend(addtcpData, $scope.attributeAPPDatas[0]);
88                         var uuu = "saveDictionary/fw_dictionary/save_serviceList";
89                         var postData={serviceListDictionaryData: finalData, userid: userid};
90                         $.ajax({
91                                 type : 'POST',
92                                 url : uuu,
93                                 dataType: 'json',
94                                 contentType: 'application/json',
95                                 data: JSON.stringify(postData),
96                                 success : function(data){
97                                         $scope.$apply(function(){
98                                                 $scope.serviceListDictionaryDatas=data.serviceListDictionaryDatas;});
99                                         if($scope.serviceListDictionaryDatas == "Duplicate"){
100                                                 Notification.error("FW ServiceList Dictionary exists with Same ServiceList Name.")
101                                         }else{      
102                                                 console.log($scope.serviceListDictionaryDatas);
103                                                 $modalInstance.close({serviceListDictionaryDatas:$scope.serviceListDictionaryDatas});
104                                         }
105                                 },
106                                 error : function(data){
107                                         alert("Error while saving.");
108                                 }
109                         });
110                 }
111         };
112
113         $scope.close = function() {
114                 $modalInstance.close();
115         };
116
117         function extend(obj, src) {
118                 for (var key in src) {
119                         if (src.hasOwnProperty(key)) obj[key] = src[key];
120                 }
121                 return obj;
122         }
123
124         $scope.attributeTCPDatas = [{"transportProtocols" : $scope.tpchoices}];
125         $scope.addTPNewChoice = function() {
126                 var newItemNo = $scope.tpchoices.length+1;
127                 $scope.tpchoices.push({'id':'choice'+newItemNo});
128         };
129         $scope.removeTPChoice = function() {
130                 var lastItem = $scope.tpchoices.length-1;
131                 $scope.tpchoices.splice(lastItem);
132         };
133
134
135         $scope.attributeAPPDatas = [{"appProtocols" : $scope.apchoices}];
136         $scope.addAPNewChoice = function() {
137                 var newItemNo = $scope.apchoices.length+1;
138                 $scope.apchoices.push({'id':'choice'+newItemNo});
139         };
140         $scope.removeAPChoice = function() {
141                 var lastItem = $scope.apchoices.length-1;
142                 $scope.apchoices.splice(lastItem);
143         };
144 });