Microservice Onboarding Issue resolved
[portal.git] / portal-FE-common / src / app / pages / microservice-onboarding / microservice-add-details / microservice-add-details.component.ts
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 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  * 
37  */
38 import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
39 import { IMircroservies } from 'src/app/shared/model/microservice-onboarding/microservices';
40 import { MicroserviceService, WidgetOnboardingService } from '../../../shared/services/index';
41 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
42 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
43
44 @Component({
45   selector: 'app-microservice-add-details',
46   templateUrl: './microservice-add-details.component.html',
47   styleUrls: ['./microservice-add-details.component.scss']
48 })
49 export class MicroserviceAddDetailsComponent implements OnInit {
50
51   @Input() public ms: IMircroservies;
52   @Output() passEntry: EventEmitter<any> = new EventEmitter();
53   result: any;
54   selected: any;
55   isEditMode: any;
56   originalName: string;
57   dupliateName = false;
58   emptyServiceName = false;
59   emptyServiceDesc = false;
60   emptyServiceURL = false;
61   emptyServiceApp = false;
62   availableSecurityTypes = [];
63   availableWidgets = [];
64   applicationList: Array<Object> = [];
65
66   constructor(public microservice: MicroserviceService, public widgetOnboardingService: WidgetOnboardingService, 
67     public activeModal: NgbActiveModal, public ngbModal: NgbModal) { }
68
69   ngOnInit() {
70     if(this.ms.name){
71       this.isEditMode = true;
72     }else{
73       this.isEditMode = false;
74     }
75     this.ms.parameterList = [];
76     this.ms.active = true;
77     this.populateAvailableApps();
78     this.getAvailableSecurityTypes();
79   }
80
81   getAvailableWidgets(serviceId){
82     console.log("getAvailableWidgets called");
83     this.microservice.getWidgetListByService(serviceId)
84     .subscribe(data => {
85         this.result = data;
86         if (this.result == null || this.result) {
87               console.log('MicroserviceService::getServiceList Failed: Result or result.data is null');
88           }else {
89             this.availableWidgets = [];
90             for(var i = 0; i < data.length; i++){
91               this.availableWidgets.push({
92                 name: data[i]
93               })
94             }
95           }
96     }, error =>{
97       console.log(error);
98     });
99   };
100   
101   getAvailableSecurityTypes(): Array<String> {
102     this.availableSecurityTypes = [];
103     this.availableSecurityTypes.push({
104       id: 0,
105       name: 'No Authentication'
106     });
107     this.availableSecurityTypes.push({
108       id: 1,
109       name: 'Basic Authentication'
110     });
111     this.availableSecurityTypes.push({
112       id: 2,
113       name: 'Cookie based Authentication'
114     });
115
116     return this.availableSecurityTypes;
117   };
118   
119   populateAvailableApps(){
120     this.widgetOnboardingService.populateAvailableApps()
121       .subscribe( _data => {
122         let allPortalsFilterObject = {index: 0, title: 'Select Application', value: ''};
123         this.applicationList = [allPortalsFilterObject];
124         var realAppIndex = 1;
125         for (let i = 1; i <= _data.length; i++) {
126             if (!_data[i-1].restrictedApp) {
127                 this.applicationList.push({
128                     index: realAppIndex,
129                     title: _data[i - 1].name,
130                     value: _data[i - 1].id
131                 })
132                 realAppIndex = realAppIndex + 1;
133             }
134         }
135       }, error => {
136         console.log(error);
137     }); 
138   };
139
140   addParameter() {
141     this.ms.parameterList.push({}); 
142   }
143
144   testServiceURL(){
145     console.log("testServiceURL called  id is :: ",this.ms.id)
146     this.microservice.getServiceJSON(this.ms.id)
147       .subscribe( _data => {
148         this.result = _data;
149         console.log("testServiceURL response :: ",this.result);
150         document.getElementById("microservice-details-input-json").innerHTML = (JSON.stringify(this.result));
151       }, error => {
152         document.getElementById("microservice-details-input-json").innerHTML = "Something went wrong. Please go back to the previous page or try again later.";
153         console.log(error);
154     });
155   }
156
157   removeParamItem(parameter: any){
158     console.log("removeParamItem called", parameter);
159     this.ms.parameterList.splice(parameter.para_key, 1);
160     this.microservice.getUserParameterById(parameter)
161     .subscribe(data => {
162         this.result = data;
163         if (this.result == null || this.result) {
164               console.log('MicroserviceService::removeParamItem Failed: Result or result.data is null');
165         }else if(this.result && this.result.length > 0) {
166           this.microservice.deleteUserParameterById(parameter.id)
167           .subscribe(__data => {
168             for(var i = 0; i < this.ms.parameterList.length; i++){
169               if(this.ms.parameterList[i].para_key == parameter.para_key
170               && this.ms.parameterList[i].para_value == parameter.para_value){
171                 this.ms.parameterList.splice(i, 1);
172                 return;
173               }
174             }
175               
176           }, error =>{
177             console.log(error);
178           });
179         }else{
180           for(var i = 0; i < this.ms.parameterList.length; i++){
181             if(this.ms.parameterList[i].para_key == parameter.para_key
182             && this.ms.parameterList[i].para_value == parameter.para_value){
183               this.ms.parameterList.splice(i, 1);
184               return;
185             }
186           }
187         }
188     }, error =>{
189       console.log(error);
190     });
191   }
192
193   //Add Or Update Microservices.
194   saveChanges(){
195     console.log("saveChanges..",this.ms);
196     if(this.ms && this.ms.id && this.ms.id !='undefined'){
197       this.isEditMode = true;
198     }
199     var isValid = true;
200     
201     if(this.ms.name == ''
202       || this.ms.name == undefined){
203       this.emptyServiceName = true;
204       isValid = false;
205     }
206     console.log("a >",isValid);
207     if(this.dupliateName == true){
208       isValid = false;
209     }
210     console.log("b> ",isValid);  
211     if(this.ms.desc == ''
212     || this.ms.desc == undefined){
213       this.emptyServiceDesc = true;
214       isValid = false;
215     }
216     console.log("c> ",isValid); 
217     
218     if(this.ms.url == ''
219       || this.ms.url == undefined){
220         this.emptyServiceURL = true;
221         isValid = false;
222     }
223     console.log("d> ",isValid); 
224       
225     if(this.ms.appId == undefined
226     || this.ms.appId == null){
227       this.emptyServiceApp = true;
228       isValid = false;
229     }
230     
231     console.log("IsValid flag add/update microservices ",isValid ) 
232
233     if(!isValid)
234       return;
235     /*
236     * Check the parameter list, delete those parameters that don't
237     * have key
238     */
239     if(this.ms && this.ms.parameterList){
240       for(var i = 0; i < this.ms.parameterList.length; i++){
241         if(this.ms.parameterList[i].para_key == undefined
242         || this.ms.parameterList[i].para_key == null
243         || this.ms.parameterList[i].para_key == ""){
244           this.ms.parameterList.splice(i, 1);
245           i--;
246         }
247       }
248     }
249     if(this.ms.securityType == undefined ||
250     this.ms.securityType == null)
251       this.ms.securityType = "No Authentication";
252     else{
253       this.ms.securityType = this.ms.securityType;
254       this.ms.username = this.ms.username;
255       this.ms.password = this.ms.password;
256     }
257     
258     var active = 'N';
259     if(this.ms.active == true)
260       active = 'Y';
261     
262     let paramList = [];
263     if(this.ms.parameterList && this.ms.parameterList.length >0){
264       paramList = this.ms.parameterList;
265     }  
266     var newService = {
267       name: this.ms.name,
268       desc: this.ms.desc,
269       appId: this.ms.appId,
270       url: this.ms.url,
271       securityType: this.ms.securityType,
272       username: this.ms.username,
273       password: this.ms.password,
274       active: active,
275       parameterList: paramList
276     };
277
278     if(this.isEditMode){
279       console.log("Edit microservice mode called");
280       this.microservice.updateService(this.ms.id , newService)
281         .subscribe( data => {
282           this.result = data;
283           console.log("update microservice response :: ",this.result);
284           this.passEntry.emit(this.result);
285           this.ngbModal.dismissAll();
286         }, error => {
287           console.log(error);
288           this.ngbModal.dismissAll();
289        }); 
290     }else{  
291       console.log("Add microservice mode called")
292       this.microservice.createService(newService)
293         .subscribe( data => {
294           this.result = data;
295           if(this.result.status === 'OK'){
296             console.log("add microservice response :: ",this.result);
297             this.passEntry.emit(this.result);
298             this.ngbModal.dismissAll();
299           }
300           else {
301             const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
302             modalErrorRef.componentInstance.title = 'Error';
303             modalErrorRef.componentInstance.message = this.result.response;
304           }
305         }); 
306     }
307   }
308 }