ed25a70718161ca17adddc5f8153c5edd302c066
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : DataLake
4  * ================================================================================
5  * Copyright 2019 QCT
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
21 /**
22  *
23  * @author Ekko Chang
24  *
25  * @contributor Chunmeng Guo
26  *
27  */
28
29 import { Component, OnInit, ViewChild, ElementRef } from "@angular/core";
30 import { Db } from "src/app/core/models/db.model";
31 import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
32 import { DatabaseAddModalComponent } from "src/app/views/database/database-list/database-add-modal/database-add-modal.component";
33
34 // DB modal components
35 import { RestApiService } from "src/app/core/services/rest-api.service";
36
37 // Modal
38 import { AlertComponent } from "src/app/shared/components/alert/alert.component";
39
40 // Notify
41 import { ToastrNotificationService } from "src/app/shared/components/toastr-notification/toastr-notification.service";
42
43 // Loading spinner
44 import { NgxSpinnerService } from "ngx-spinner";
45 import {CouchbaseComponent} from "src/app/views/database/database-list/dbs-modal/couchbase/couchbase.component";
46 import {DruidComponent} from "src/app/views/database/database-list/dbs-modal/druid/druid.component";
47 import {ElasticsearchComponent} from "src/app/views/database/database-list/dbs-modal/elasticsearch/elasticsearch.component";
48 import {MongodbComponent} from "src/app/views/database/database-list/dbs-modal/mongodb/mongodb.component";
49 import {HdfsComponent} from "src/app/views/database/database-list/dbs-modal/hdfs/hdfs.component";
50
51 @Component({
52   selector: "app-database-list",
53   templateUrl: "./database-list.component.html",
54   styleUrls: ["./database-list.component.css"]
55 })
56 export class DatabaseListComponent implements OnInit {
57   pageFinished: Boolean = false;
58
59   dbList: any = [];
60   dbs: Db[] = [];
61   dbNew: Db;
62   db_NewBody: Db;
63   loading: Boolean = true;
64   flag: Boolean = true;
65   loadingIndicator: boolean = true;
66
67   mesgNoData = {
68     emptyMessage: `
69       <div class="d-flex justify-content-center">
70         <div class="p-2">
71           <label class="dl-nodata">No Data</label>
72         </div>
73       </div>
74     `
75   };
76
77   @ViewChild("searchText") searchText: ElementRef;
78
79   constructor(
80       private dbApiService: RestApiService,
81       private notificationService: ToastrNotificationService,
82       private modalService: NgbModal,
83       private spinner: NgxSpinnerService
84   ) {
85     this.initData().then(data => {
86       this.initDbsList(this.dbList).then(data => {
87         this.dbs = data;
88       });
89     });
90   }
91
92   ngOnInit() {
93     this.spinner.show();
94   }
95
96   async initData() {
97       this.dbList = [];
98       this.dbList = await this.getDbList(this.flag);
99       setTimeout(() => {
100           this.spinner.hide();
101       }, 500);
102   }
103
104   getDbList(flag) {
105     return this.dbApiService.getDbEncryptList(flag).toPromise();
106
107   }
108
109   async initDbsList(dbList: []) {
110     var d: Db[] = [];
111
112     for (var i = 0; i < dbList.length; i++) {
113       let data = dbList[i];
114       let feed = {
115         id: data["id"],
116         name: data["name"],
117         enabled: data["enabled"],
118         host: data["host"],
119         port: data["port"],
120         database: data["database"],
121         encrypt: data["encrypt"],
122         login: data["login"],
123         pass: data["pass"],
124         dbTypeId: data["dbTypeId"],
125       };
126       d.push(feed);
127     }
128     return d;
129   }
130
131   // getDbDetail(name: string) {
132   //   return this.restApiService.getDbDetail(name).toPromise();
133   // }
134
135   openAddModal() {
136     const modalRef = this.modalService.open(DatabaseAddModalComponent, {
137       windowClass: "dl-md-modal",
138       centered: true
139     });
140
141     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
142       if (receivedEntry) {
143         modalRef.close();
144         //this.openDetailModal(receivedEntry);
145       }
146     });
147   }
148
149   updateFilter(searchValue) {
150     const val = searchValue.toLowerCase();
151     // filter our data
152     const temps = this.dbList.filter(function (d) {
153       return d.name.toLowerCase().indexOf(val) != -1 || !val;
154     });
155     // update the rows
156     this.dbList = temps;
157   }
158
159   newDbModal() {
160     const modalRef = this.modalService.open(DatabaseAddModalComponent, {
161       windowClass: "dl-md-modal dbs",
162       centered: true
163     });
164   }
165
166   deleteDbModel(id: number) {
167
168     console.log("delete id", id);
169     const index = this.dbList.findIndex(t => t.id === id);
170     const modalRef = this.modalService.open(AlertComponent, {
171       size: "sm",
172       centered: true
173     });
174     modalRef.componentInstance.message = "ARE_YOU_SURE_DELETE";
175     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
176       // Delete db
177       this.dbApiService.deleteDb(id).subscribe(
178         res => {
179           console.log(res);
180           if (JSON.stringify(res).length <= 2) {
181             this.dbList.splice(index, 1);
182             this.dbList = [...this.dbList];
183             this.initData();
184             this.notificationService.success("SUCCESSFULLY_DELETED");
185
186           } else {
187             this.initData();
188             this.notificationService.error("FAILED_DELETED");
189           }
190
191           modalRef.close();
192         },
193         err => {
194           this.notificationService.error(err);
195           modalRef.close();
196         }
197       );
198     });
199   }
200
201   updateDbModel(id: number, dbType: string) {
202     var modalRef;
203     console.log(dbType, "dbType");
204     switch (dbType) {
205       case "CB": {
206         modalRef = this.modalService.open(CouchbaseComponent, {
207           size: "lg",
208           centered: true
209         });
210         this.editDbModal(id, modalRef);
211         break;
212       }
213       case "DRUID": {
214         modalRef = this.modalService.open(DruidComponent, {
215           size: "lg",
216           centered: true
217         });
218         this.editDbModal(id, modalRef);
219         break;
220       }
221       case "ES": {
222         modalRef = this.modalService.open(ElasticsearchComponent, {
223           size: "lg",
224           centered: true
225         });
226         this.editDbModal(id, modalRef);
227         break;
228       }
229       case "MONGO": {
230         modalRef = this.modalService.open(MongodbComponent, {
231           size: "lg",
232           centered: true
233         });
234         this.editDbModal(id, modalRef);
235         break;
236       }
237       case "HDFS": {
238         modalRef = this.modalService.open(HdfsComponent, {
239           size: "lg",
240           centered: true
241         });
242         this.editDbModal(id, modalRef);
243         break;
244       }
245       default: {
246         break;
247       }
248     }
249   }
250
251   editDbModal(id: number, modalRef) {
252     console.log("id", id);
253     const index = this.dbList.findIndex(t => t.id === id);
254     modalRef.componentInstance.editDb = this.dbList[index];
255     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
256       this.dbNew = receivedEntry;
257       this.dbApiService
258         .updateDb(this.dbNew)
259         .subscribe(
260           res => {
261             if (res.statusCode == 200) {
262               this.dbList[index] = this.dbNew;
263               this.dbList = [...this.dbList];
264               this.notificationService.success("SUCCESSFULLY_UPDATED");
265               this.initData();
266             } else {
267               this.notificationService.error("FAILED_UPDATED");
268               this.initData();
269             }
270             modalRef.close();
271           },
272           err => {
273             this.notificationService.error(err);
274             modalRef.close();
275           }
276         );
277     })
278   }
279
280   onActivate(event) {
281
282   }
283 }