a2e44e97d9626a03a38db3b52ac190184053f68f
[dcaegen2/services.git] / components / datalake-handler / admin / src / src / app / core / services / rest-api.service.ts
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  * This service is to callback REST API from feeder
23  *
24  * @author Ekko Chang
25  *
26  */
27
28 import { Injectable } from "@angular/core";
29 import { HttpClient, HttpHeaders } from "@angular/common/http";
30 import { Observable, of } from "rxjs";
31 import { map, catchError, tap, retry } from "rxjs/operators";
32 import { throwError } from "rxjs";
33
34 import { Topic } from "src/app/core/models/topic.model";
35 import { Db } from "src/app/core/models/db.model";
36 import { Template } from "src/app/core/models/template.model";
37 import { Dashboard } from "src/app/core/models/dashboard.model";
38
39 const prefix = "/datalake/v1/";
40 const httpOptions = {
41   headers: new HttpHeaders({
42     "Content-Type": "application/json"
43   })
44 };
45
46 @Injectable({
47   providedIn: "root"
48 })
49 export class RestApiService {
50   constructor(private http: HttpClient) { }
51
52   private extractData(res: Response) {
53     if (res.status < 200 || res.status >= 300) {
54       throw new Error("Bad response status: " + res.status);
55     }
56     let body = res;
57     return body || {};
58   }
59
60   private handleError(error) {
61     let errorMessage = "";
62     if (error.error instanceof ErrorEvent) {
63       // Get client-side error
64       errorMessage = error.error.message;
65     } else {
66       // Get server-side error
67       errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
68       console.log(errorMessage);
69     }
70     return throwError(errorMessage);
71   }
72
73   private extractData2(res: Response) {
74     let body = res;
75     return body || {};
76   }
77
78   /*
79     Topic default config
80   */
81   getTopicDefaultConfig(): Observable<any> {
82     return this.http.get(prefix + "topics/_DL_DEFAULT_").pipe(
83       retry(1),
84       map(this.extractData),
85       catchError(this.handleError)
86     );
87   }
88
89   updateTopicDefaultConfig(t: Topic): Observable<any> {
90     return this.http
91       .put(prefix + "topics/_DL_DEFAULT_", JSON.stringify(t), httpOptions)
92       .pipe(
93         retry(1),
94         tap(_ => this.extractData),
95         catchError(this.handleError)
96       );
97   }
98
99   /*
100     Topics
101   */
102   getTopicsFromDmaap(): Observable<any> {
103     return this.http.get(prefix + "topics/dmaap").pipe(
104       retry(1),
105       map(this.extractData),
106       catchError(this.handleError)
107     );
108   }
109
110   getTopicsFromFeeder(): Observable<any> {
111     return this.http.get(prefix + "topics").pipe(
112       retry(1),
113       map(this.extractData),
114       catchError(this.handleError)
115     );
116   }
117
118   getTopicDetail(name: string): Observable<any> {
119     return this.http.get(prefix + "topics/" + name).pipe(
120       retry(1),
121       map(this.extractData),
122       catchError(this.handleError)
123     );
124   }
125
126   addNewTopic(t: Topic): Observable<any> {
127     return this.http
128       .post<any>(prefix + "topics", t)
129       .pipe(
130         retry(1),
131         tap(_ => console.log(`add topic name=${t.name}`)),
132         catchError(this.handleError)
133       );
134   }
135
136   addTopic(t: Topic): Observable<any> {
137     return this.http
138       .post<any>(prefix + "topics", t)
139       .pipe(
140         retry(1),
141         tap(_ => console.log(`add topic name=${t.name}`)),
142         catchError(this.handleError)
143       );
144   }
145
146   upadteTopic(t: Topic): Observable<any> {
147     return this.http
148       .put(prefix + "topics/" + t.name, t)
149       .pipe(
150         retry(1),
151         tap(_ => this.extractData),
152         catchError(this.handleError)
153       );
154   }
155
156   deleteTopic(name: string): Observable<any> {
157     return this.http.delete(prefix + "topics/" + name).pipe(
158       retry(1),
159       tap(_ => console.log(`deleted topic name=${name}`)),
160       catchError(this.handleError)
161     );
162   }
163
164   /*
165     Database
166   */
167   getDbList(): Observable<any> {
168     return this.http.get(prefix + "dbs").pipe(
169       retry(1),
170       map(this.extractData),
171       catchError(this.handleError)
172     );
173   }
174
175   getDbDetail(name: string): Observable<any> {
176     return this.http.get(prefix + "dbs/" + name).pipe(
177       retry(1),
178       map(this.extractData),
179       catchError(this.handleError)
180     );
181   }
182
183   upadteDb(d: Db): Observable<any> {
184     return this.http
185       .put(prefix + "dbs", d)
186       .pipe(
187         retry(1),
188         tap(_ => this.extractData),
189         catchError(this.handleError)
190       );
191   }
192
193   /*
194     Feeder
195   */
196   startFeeder() {
197     return this.http.post<any>(prefix + "feeder/start", "", httpOptions).pipe(
198       retry(1),
199       tap(_ => console.log(`start feeder`)),
200       catchError(this.handleError)
201     );
202   }
203
204   stopFeeder() {
205     return this.http.post<any>(prefix + "feeder/stop", "", httpOptions).pipe(
206       retry(1),
207       tap(_ => console.log(`stop feeder`)),
208       catchError(this.handleError)
209     );
210   }
211
212   getFeederstatus() {
213     return this.http.get(prefix + "feeder/status").pipe(
214       retry(1),
215       map(this.extractData),
216       catchError(this.handleError)
217     );
218   }
219
220
221   /*
222 Dashboard
223 */
224   getDashboardList(): Observable<any> {
225     let url = prefix + "portals"; //onilne
226     return this.http.get(url).pipe(
227       retry(1),
228       map(this.extractData),
229       catchError(this.handleError)
230     );
231   }
232
233   createUpadteDashboard(d: Dashboard): Observable<any> {
234     // let url = prefix +"/dashboard-list/successCreteOrEditDemo.json"; //local
235     let url = prefix + "portals";//onilne
236     return this.http
237       .put(url, d)
238       .pipe(
239         retry(1),
240         tap(_ => this.extractData),
241         catchError(this.handleError)
242       );
243   }
244
245   deleteDashboard(d: Dashboard): Observable<any> {
246     let url = prefix + "portals"; //onilne
247     return this.http
248       .put(url, d)
249       .pipe(
250         retry(1),
251         tap(_ => console.log(`deleted db name=${d.name}`)),
252         catchError(this.handleError)
253       );
254   }
255
256
257   /*
258   Template
259 */
260   getTemplateAll(): Observable<any> {
261     return this.http.get(prefix + "designs/").pipe( //onlin
262       retry(1),
263       map(this.extractData),
264       catchError(this.handleError)
265     );
266   }
267
268   createNewTemplate(t: Template): Observable<any> {
269     return this.http
270       .post(prefix + "designs", t)
271       .pipe(
272         retry(1),
273         tap(_ => this.extractData),
274         catchError(this.handleError)
275       );
276   }
277
278   updateNewTemplate(t: Template): Observable<any> {
279     let id = t.id;
280     return this.http
281       .put(prefix + "designs/" + id, t)
282       .pipe(
283         retry(1),
284         tap(_ => this.extractData),
285         catchError(this.handleError)
286       );
287   }
288
289   // getTopicName(): Observable<any> {
290   //   return this.http.get(prefix + "topics").pipe( //onlin
291   //     retry(1),
292   //     map(this.extractData),
293   //     catchError(this.handleError)
294   //   );
295   // }
296
297   getTemplateTypeName(): Observable<any> {
298     return this.http.get(prefix + "designTypes").pipe( //onlin
299       retry(1),
300       map(this.extractData),
301       catchError(this.handleError)
302     );
303   }
304
305   DeleteTemplate(id): Observable<any> {
306     return this.http.delete(prefix + "designs/" + id).pipe( //online
307       retry(1),
308       map(this.extractData2),
309       catchError(this.handleError)
310     );
311   }
312   deployTemplateKibana(id, body): Observable<any> {
313     body.submitted = true;
314     return this.http.post(prefix + "designs/deploy/" + id, body).pipe(   //online
315       retry(1),
316       map(this.extractData2),
317       catchError(this.handleError)
318     );
319   }
320 }
321
322