5b937301265e6446d889f84abe8393be8bbc749c
[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  */
26
27 import { Component, OnInit, ViewChild, ElementRef } from "@angular/core";
28 import { Db } from "../../../core/models/db.model";
29 import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
30 import { DatabaseAddModalComponent } from "./database-add-modal/database-add-modal.component";
31
32 // DB modal components
33 import { RestApiService } from "src/app/core/services/rest-api.service";
34
35 // Modal
36 import { CouchbaseComponent } from "./dbs-modal/couchbase/couchbase.component";
37 import { DruidComponent } from "./dbs-modal/druid/druid.component";
38 import { ElasticsearchComponent } from "./dbs-modal/elasticsearch/elasticsearch.component";
39 import { MongodbComponent } from "./dbs-modal/mongodb/mongodb.component";
40 import { HdfsComponent } from "./dbs-modal/hdfs/hdfs.component";
41 import { AlertComponent } from "src/app/shared/components/alert/alert.component";
42
43 // Notify
44 import { ToastrNotificationService } from "src/app/shared/components/toastr-notification/toastr-notification.service";
45
46 // Loading spinner
47 import { NgxSpinnerService } from "ngx-spinner";
48
49 @Component({
50   selector: "app-database-list",
51   templateUrl: "./database-list.component.html",
52   styleUrls: ["./database-list.component.css"]
53 })
54 export class DatabaseListComponent implements OnInit {
55   pageFinished: Boolean = false;
56
57   dbList: any = [];
58   dbs: Db[] = [];
59
60   loading: Boolean = true;
61
62   tempDbDetail: Db;
63
64   constructor(
65     private restApiService: RestApiService,
66     private notificationService: ToastrNotificationService,
67     private modalService: NgbModal,
68     private spinner: NgxSpinnerService
69   ) {
70     this.initData().then(data => {
71       this.initDbsList(this.dbList).then(data => {
72         this.dbs = data;
73       });
74     });
75   }
76
77   ngOnInit() {
78     this.spinner.show();
79   }
80
81   async initData() {
82     this.dbList = [];
83     this.dbList = await this.getDbList();
84     setTimeout(() => {
85       this.spinner.hide();
86     }, 500);
87   }
88
89   getDbList() {
90     var data: any;
91
92     data = this.restApiService.getDbList().toPromise();
93
94     return data;
95   }
96
97   async initDbsList(dbList: []) {
98     var d: Db[] = [];
99
100     if (dbList.length > 0) {
101       for (var i = 0; i < dbList.length; i++) {
102         let data = await this.getDbDetail(dbList[i]);
103         let feed = {
104           name: dbList[i],
105           enabled: data.enabled,
106           host: data.host,
107           port: data.port,
108           database: data.database,
109           encrypt: data.encrypt,
110           login: data.login,
111           pass: data.pass
112         };
113         d.push(feed);
114       }
115     }
116     return d;
117   }
118
119   getDbDetail(name: string) {
120     return this.restApiService.getDbDetail(name).toPromise();
121   }
122
123   openAddModal() {
124     const modalRef = this.modalService.open(DatabaseAddModalComponent, {
125       windowClass: "dl-md-modal",
126       centered: true
127     });
128
129     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
130       if (receivedEntry) {
131         modalRef.close();
132         this.openDetailModal(receivedEntry);
133       }
134     });
135   }
136
137   openDetailModal(name: string) {
138     var modalRef, index;
139
140     switch (name) {
141       case "Couchbase": {
142         modalRef = this.modalService.open(CouchbaseComponent, {
143           size: "lg",
144           centered: true
145         });
146         break;
147       }
148       case "Druid": {
149         modalRef = this.modalService.open(DruidComponent, {
150           size: "lg",
151           centered: true
152         });
153         break;
154       }
155       case "Elasticsearch": {
156         modalRef = this.modalService.open(ElasticsearchComponent, {
157           size: "lg",
158           centered: true
159         });
160         break;
161       }
162       case "MongoDB": {
163         modalRef = this.modalService.open(MongodbComponent, {
164           size: "lg",
165           centered: true
166         });
167         break;
168       }
169       case "HDFS": {
170         modalRef = this.modalService.open(HdfsComponent, {
171           size: "lg",
172           centered: true
173         });
174         break;
175       }
176       default: {
177         break;
178       }
179     }
180
181     index = this.dbs.findIndex(d => d.name === name);
182     this.tempDbDetail = new Db();
183     if (index != -1) {
184       modalRef.componentInstance.db = this.dbs[index];
185     } else {
186       modalRef.componentInstance.db = this.tempDbDetail;
187     }
188     modalRef.componentInstance.passEntry.subscribe(receiveEntry => {
189       this.tempDbDetail = receiveEntry;
190       let enabled = receiveEntry.enabled;
191       console.log(this.tempDbDetail, "this.tempDbDetail");
192       if (enabled == true) {
193         this.restApiService.upadteDb(this.tempDbDetail).subscribe(
194           res => {
195             console.log(res);
196             if (res.statusCode == 200) {
197               this.dbs[index] = this.tempDbDetail;
198               this.notificationService.success("SUCCESSFULLY_UPDATED");
199             } else {
200               this.notificationService.error("FAILED_UPDATED");
201             }
202             modalRef.close();
203           },
204           err => {
205             this.notificationService.error(err);
206             modalRef.close();
207           }
208         );
209       } else {
210         this.restApiService.upadteDb(this.dbs[index]).subscribe(
211           res => {
212             console.log(res);
213             if (res.statusCode == 200) {
214               this.dbs[index] = this.tempDbDetail;
215               this.notificationService.success("SUCCESSFULLY_DELETED");
216             } else {
217               this.dbs[index].encrypt = true;
218             }
219             modalRef.close();
220           },
221           err => {
222             this.notificationService.error(err);
223             modalRef.close();
224           }
225         );
226       }
227     });
228   }
229 }