Add creation test to PolicyControlComponent
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy-control / policy-type.datasource.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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 import { CollectionViewer, DataSource } from '@angular/cdk/collections';
22 import { HttpErrorResponse } from '@angular/common/http';
23 import { Injectable } from '@angular/core';
24 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
25 import { of } from 'rxjs/observable/of';
26 import { catchError, finalize, map } from 'rxjs/operators';
27 import { Observable } from 'rxjs/Observable';
28
29 import { PolicyType } from '../interfaces/policy.types';
30 import { PolicyService } from '../services/policy/policy.service';
31 import { NotificationService } from '../services/ui/notification.service';
32
33 @Injectable({
34     providedIn: 'root'
35 })
36
37 export class PolicyTypeDataSource extends DataSource<PolicyType> {
38
39     private policyTypeSubject = new BehaviorSubject<PolicyType[]>([]);
40
41     private loadingSubject = new BehaviorSubject<boolean>(false);
42
43     public loading$ = this.loadingSubject.asObservable();
44
45     public rowCount = 1; // hide footer during intial load
46
47     constructor(private policySvc: PolicyService,
48         private notificationService: NotificationService) {
49         super();
50     }
51
52     loadTable() {
53         this.loadingSubject.next(true);
54         this.policySvc.getPolicyTypes()
55             .pipe(
56                 catchError((her: HttpErrorResponse) => {
57                     this.notificationService.error('Failed to get policy types: ' + her.statusText + ', ' + her.error);
58                     return of([]);
59                 }),
60                 finalize(() => this.loadingSubject.next(false))
61             )
62             .subscribe((types: PolicyType[]) => {
63                 this.rowCount = types.length;
64                 for (let i = 0; i < types.length; i++) {
65                     const policyType = types[i];
66                     try {
67                         policyType.schemaObject = JSON.parse(policyType.schema);
68                     } catch (jsonError) {
69                         console.error('Could not parse schema: ' + policyType.schema);
70                         policyType.schemaObject = { description: 'Incorrect schema: ' + jsonError };
71                     }
72                 }
73                 this.policyTypeSubject.next(types);
74             });
75     }
76
77     connect(collectionViewer: CollectionViewer): Observable<PolicyType[]> {
78         return of(this.policyTypeSubject.getValue());
79     }
80
81     disconnect(collectionViewer: CollectionViewer): void {
82         this.policyTypeSubject.complete();
83         this.loadingSubject.complete();
84     }
85 }