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