2e10f55affcaf8d7d20319114093a2db71c601e8
[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 { HttpErrorResponse } from '@angular/common/http';
22 import { CollectionViewer, DataSource } from '@angular/cdk/collections';
23 import { Injectable } from '@angular/core';
24 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
25 import { of } from 'rxjs/observable/of';
26 import { Observable } from 'rxjs/Observable';
27 import { catchError, finalize, map } from 'rxjs/operators';
28
29 import { PolicyType, PolicyTypes, PolicyTypeSchema } 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<PolicyTypeSchema> {
38
39     policyTypes: PolicyTypeSchema[] = [];
40
41     policyTypeSubject = new BehaviorSubject<PolicyTypeSchema[]>([]);
42
43     private loadingSubject = new BehaviorSubject<boolean>(false);
44
45     public rowCount = 1; // hide footer during intial load
46
47     constructor(public policySvc: PolicyService,
48         private notificationService: NotificationService) {
49         super();
50     }
51
52     public getPolicyTypes() {
53         this.policyTypes = [] as PolicyTypeSchema[];
54         this.policySvc.getPolicyTypes()
55             .pipe(
56                 catchError((httpError: HttpErrorResponse) => {
57                     this.notificationService.error('Failed to get policy types: ' + httpError.error);
58                     return of([]);
59                 }),
60                 finalize(() => this.loadingSubject.next(false))
61             )
62             .subscribe((policyType: PolicyTypes) => {
63                 this.rowCount = policyType.policytype_ids.length;
64                 if (policyType.policytype_ids.length != 0) {
65                     policyType.policytype_ids.forEach(policyTypeId => {
66                         var policyTypeSchema = {} as PolicyTypeSchema
67                         if (policyTypeId === "") {
68                             policyTypeSchema.id = '';
69                             policyTypeSchema.name = '';
70                             policyTypeSchema.schemaObject = '{}';
71                             this.policyTypes.push(policyTypeSchema);
72                         }
73                         else {
74                             this.policySvc.getPolicyType(policyTypeId)
75                                 .pipe(
76                                     catchError((httpError: HttpErrorResponse) => {
77                                         this.notificationService.error('Failed to get policy type: ' + httpError.error);
78                                         return of([]);
79                                     }),
80                                     finalize(() => this.loadingSubject.next(false))
81                                 )
82                                 .subscribe((policyType: PolicyType) => {
83                                     policyTypeSchema.id = policyTypeId;
84                                     policyTypeSchema.schemaObject = policyType.policy_schema;
85                                     policyTypeSchema.name = policyType.policy_schema.title;
86                                     this.policyTypes.push(policyTypeSchema);
87                                 })
88                         }
89                         this.policyTypeSubject.next(this.policyTypes);
90                     })
91                 }
92             })
93     }
94
95     connect(collectionViewer: CollectionViewer): Observable<PolicyTypeSchema[]> {
96         return of(this.policyTypeSubject.getValue());
97     }
98
99     disconnect(collectionViewer: CollectionViewer): void {
100         this.policyTypeSubject.complete();
101     }
102 }