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