Security/ Package Name changes
[portal.git] / ecomp-portal-FE-common / client / app / views / microservice-onboarding / microservice-add-details / microservice-add-details.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 'use strict';
39 (function () {
40     class MicroserviceAddDetailsCtrl {
41         constructor($scope, $log, $interval, widgetsCatalogService, applicationsService, adminsService, microserviceService, errorMessageByCode, ECOMP_URL_REGEX, $window,userProfileService, confirmBoxService, $cookies,items) {
42             
43                 $scope.ngDialogData=items;
44             let getAvailableApps = () => {       
45                 applicationsService.getAppsForSuperAdminAndAccountAdmin().then(apps => {
46                         this.availableApps=[];
47                         apps.unshift({
48                         id: 1,
49                         name: "ECOMP Portal"
50                     });
51                         for(var i = 0; i < apps.length; i++) {
52                         this.availableApps.push({
53                             id: apps[i].id,
54                             name: apps[i].name
55                         });
56                         if(this.isEditMode == true && this.service.appId == apps[i].id){
57                                 this.service.application = this.availableApps[i];
58                         }
59                     }
60                 }).catch(err => {
61                     $log.error(err);
62                 });
63             };
64             
65             let getAvailableWidgets = () => {       
66                 microserviceService.getWidgetListByService(this.service.id).then(widgets =>{
67                         this.availableWidgets = [];
68                         for(var i = 0; i < widgets.length; i++){
69                                 this.availableWidgets.push({
70                                         name: widgets[i]
71                                 })
72                         }
73                 }).catch(err => {
74                     $log.error(err);
75                 });
76             };
77             
78             
79             let getAvailableSecurityTypes = () => {   
80                 this.availableSecurityTypes = [];
81                 this.availableSecurityTypes.push({
82                         id: 0,
83                         name: 'No Authentication'
84                 });
85                 this.availableSecurityTypes.push({
86                         id: 1,
87                         name: 'Basic Authentication'
88                 });
89                 this.availableSecurityTypes.push({
90                         id: 2,
91                         name: 'Cookie based Authentication'
92                 });
93             }
94
95             let init = () => {
96                 $log.info('MicroserviceAddDetailsCtrl::init');
97                 this.service = [];
98                 this.availableApps=[];
99                 this.service.parameterList = [];
100                 this.service.active = true;
101                 this.emptyServiceName = false;
102                 this.emptyServiceDesc = false;
103                 this.emptyServiceURL = false;
104                 this.emptyServiceApp = false;
105                 this.dupliateName = false;
106                 this.serviceList = $scope.ngDialogData.list;
107                 
108                 if ($scope.ngDialogData && $scope.ngDialogData.service) {
109                         
110                     this.isEditMode = true;
111                     this.service = _.clone($scope.ngDialogData.service);
112                     
113                     this.originalName  = this.service.name;
114                     if(this.service.active == 'Y')
115                         this.service.active = true;
116                     else
117                         this.service.active = false;
118                 } else {
119                     this.isEditMode = false;
120                 } 
121                 getAvailableApps();
122                 getAvailableSecurityTypes();
123                 //getAvailableWidgets();
124                 
125                 /**
126                                  * 0: Basic Authentication
127                                  * 
128                                  * TODO: change the structure
129                                  */
130                 if(this.service.securityType == "No Authentication"){
131                         this.service.security = this.availableSecurityTypes[0];
132                 }else if(this.service.securityType == "Basic Authentication"){
133                         this.service.security = this.availableSecurityTypes[1];
134                 }else if(this.service.securityType == "Cookie based Authentication"){
135                         this.service.security = this.availableSecurityTypes[2];
136                 }
137             };
138              
139             this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;
140             this.conflictMessages = {};
141             this.scrollApi = {};
142
143             let resetConflict = fieldName => {
144                 delete this.conflictMessages[fieldName];
145                 if($scope.widgetForm[fieldName]){
146                     $scope.widgetForm[fieldName].$setValidity('conflict', true);
147                 }
148             };
149
150             this.addParameter = () => {
151                 this.service.parameterList.push({}); 
152             }
153             
154             this.closeThisDialog = () => {
155                 $scope.closeThisDialog(true);
156             }
157             
158             this.removeParamItem = (parameter) => {
159                 microserviceService.getUserParameterById(parameter.id).then((res) => {
160                         if(res.length > 0){
161                                 var message = res.length + " users have their own widget parameters. Are you sure you want to delete?";
162                                 confirmBoxService.editItem(message).then(isConfirmed => {
163                                         if(isConfirmed){
164                                                 microserviceService.deleteUserParameterById(parameter.id).then((res) => {
165                                                         for(var i = 0; i < this.service.parameterList.length; i++){
166                                                 if(this.service.parameterList[i].para_key == parameter.para_key
167                                                 && this.service.parameterList[i].para_value == parameter.para_value){
168                                                         this.service.parameterList.splice(i, 1);
169                                                         return;
170                                                 }
171                                         }
172                                                 });
173                                         }
174                                 });
175                         }
176                         else{
177                                 for(var i = 0; i < this.service.parameterList.length; i++){
178                                 if(this.service.parameterList[i].para_key == parameter.para_key
179                                 && this.service.parameterList[i].para_value == parameter.para_value){
180                                         this.service.parameterList.splice(i, 1);
181                                         return;
182                                 }
183                         }
184                         }
185                 });
186                 
187                 
188             }
189             
190             this.updateServiceName = () => {
191                 this.dupliateName = false;
192                 for(var i = 0; i < this.serviceList.length; i++){
193                         if(this.serviceList[i].name == this.service.name){
194                                 if(this.isEditMode && this.service.name == this.originalName)
195                                         continue;
196                                 this.dupliateName = true;
197                                 return;
198                         }
199                 }
200             }
201             
202             this.updateDesc = () => {
203                 this.emptyServiceDesc = false;
204             }
205             
206             this.updateURL = () => {
207                 this.emptyServiceURL  = false;
208             }
209             
210             this.updateApp = () => {
211                 this.emptyServiceApp = false;
212             }
213             
214           //This is a fix for dropdown selection, due to b2b dropdown only update value field
215                 $scope.$watch('microserviceAddDetails.service.application.name', (newVal, oldVal) => {
216                         for(var i=0;i<$scope.microserviceAddDetails.availableApps.length;i++){                  
217                                 if($scope.microserviceAddDetails.availableApps[i].name==newVal){
218                                         $scope.microserviceAddDetails.service.application=angular.copy($scope.microserviceAddDetails.availableApps[i]);
219                                 }
220                         }
221                 });
222                 $scope.$watch('microserviceAddDetails.service.security.name', (newVal, oldVal) => {
223                         for(var i=0;i<$scope.microserviceAddDetails.availableSecurityTypes.length;i++){                         
224                                 if($scope.microserviceAddDetails.availableSecurityTypes[i].name==newVal){
225                                         $scope.microserviceAddDetails.service.security=angular.copy($scope.microserviceAddDetails.availableSecurityTypes[i]);
226                                 }
227                         }
228                 });
229             
230             let emptyCookies = () => {
231                 userProfileService.getUserProfile()
232                 .then(profile=> {
233                     $log.info('AppDetailsModalCtrl::emptyCookies profile: ', profile);
234                     $scope.orgUserId = profile.orgUserId;
235                     $log.info('user has the following orgUserId: ' + profile.orgUserId);
236                     if ($cookies.getObject($scope.orgUserId + '_widget') != undefined && $cookies.getObject($scope.orgUserId + '_widget') != null) {
237                         $cookies.remove($scope.orgUserId + '_widget');
238                     }
239                 });
240             };
241             
242             this.testServiceURL = () =>{
243                 widgetsCatalogService.getServiceJSON(this.service.id).then(res => {
244                         document.getElementById("microservice-details-input-json").innerHTML = (JSON.stringify(res));
245                                 });
246             }
247             
248             this.saveChanges = () => {     
249                 /* TODO: add form validation */
250                 
251                 var isValid = true;
252                 this.updateServiceName();
253                 
254                 if(this.service.name == ''
255                 || this.service.name == undefined){
256                         this.emptyServiceName = true;
257                         isValid = false;
258                 }
259                 
260                 if(this.dupliateName == true){
261                         isValid = false;
262                 }
263                         
264                 
265                 if(this.service.desc == ''
266                 || this.service.desc == undefined){
267                         this.emptyServiceDesc = true;
268                         isValid = false;
269                 }
270                 
271                 if(this.service.url == ''
272                 || this.service.url == undefined){
273                         this.emptyServiceURL = true;
274                         isValid = false;
275                 }
276                         
277                 if(this.service.application == undefined
278                 || this.service.application == null){
279                         this.emptyServiceApp = true;
280                         isValid = false;
281                 }
282                 
283                 if(!isValid)
284                         return;
285                 
286                 /*
287                                  * Check the parameter list, delete those parameters that don't
288                                  * have key
289                                  */
290                 for(var i = 0; i < this.service.parameterList.length; i++){
291                         if(this.service.parameterList[i].para_key == undefined
292                         || this.service.parameterList[i].para_key == null
293                         || this.service.parameterList[i].para_key == ""){
294                                 this.service.parameterList.splice(i, 1);
295                                 i--;
296                         }
297                 }
298       
299                 var securityType;
300                 var username;
301                 var password;
302                 if(this.service.security == undefined ||
303                 this.service.security == null)
304                         securityType = "No Authentication";
305                 else{
306                         securityType = this.service.security.name;
307                         username = this.service.username;
308                         password = this.service.password;
309                 }
310                 
311                 var active = 'N';
312                 if(this.service.active == true)
313                         active = 'Y';
314                 
315                 var newService = {
316                                 name: this.service.name,
317                                 desc: this.service.desc,
318                                 appId: this.service.application.id,
319                                 url: this.service.url,
320                                 securityType: securityType,
321                                 username: username,
322                                 password: password,
323                                 active: active,
324                                 parameterList: this.service.parameterList
325                 };
326                 
327                 if(this.isEditMode){
328                         // console.log(this.service.parameterList);
329                         var message = "Are you sure you want to change '" + this.service.name + "'?"
330                         confirmBoxService.editItem(message).then(isConfirmed => {
331                                 if(isConfirmed){
332                                         microserviceService.updateService(this.service.id, newService).then(() => {
333                                                 // TODO: result validation check
334                                                 //this.closeThisDialog(); 
335                                                 $scope.$dismiss('cancel');
336                                         });
337                                 }
338                         });
339                 }else{
340                         microserviceService.createService(newService).then(() => {
341                                 // TODO: result validation check
342                                 //$scope.closeThisDialog(true);
343                                 $scope.$dismiss('cancel');
344                         });
345                 }
346                 
347             };
348             init();
349             $scope.$on('$stateChangeStart', e => {
350                 e.preventDefault();
351             });
352         }
353     }
354     MicroserviceAddDetailsCtrl.$inject = ['$scope', '$log', '$interval', 'widgetsCatalogService', 'applicationsService', 'adminsService', 'microserviceService', 'errorMessageByCode', 'ECOMP_URL_REGEX', '$window','userProfileService', 'confirmBoxService', '$cookies','items'];
355     angular.module('ecompApp').controller('MicroserviceAddDetailsCtrl', MicroserviceAddDetailsCtrl);
356 })();