5002522a1856d211eacfda858dbec7890101d1da
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy-control / policy-instance.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 { DataSource } from '@angular/cdk/collections';
22 import { HttpErrorResponse } from '@angular/common/http';
23 import { MatSort } from '@angular/material';
24 import { Observable } from 'rxjs/Observable';
25 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
26 import { merge } from 'rxjs';
27 import { of } from 'rxjs/observable/of';
28 import { catchError, finalize, map } from 'rxjs/operators';
29 import { PolicyInstance, PolicyTypeSchema } from '../interfaces/policy.types';
30 import { PolicyService } from '../services/policy/policy.service';
31 import { NotificationService } from '../services/ui/notification.service';
32
33 export class PolicyInstanceDataSource extends DataSource<PolicyInstance> {
34
35     policyInstances: PolicyInstance[] = [];
36
37     private policyInstanceSubject = new BehaviorSubject<PolicyInstance[]>([]);
38
39     private loadingSubject = new BehaviorSubject<boolean>(false);
40
41     public loading$ = this.loadingSubject.asObservable();
42
43     public rowCount = 1; // hide footer during intial load
44
45     constructor(
46         private policySvc: PolicyService,
47         public sort: MatSort,
48         private notificationService: NotificationService,
49         private policyTypeSchema: PolicyTypeSchema) {
50         super();
51     }
52
53     public getPolicyInstances() {
54         this.policyInstances = [] as PolicyInstance[];
55         this.policySvc.getPolicyInstancesByType(this.policyTypeSchema.id).subscribe(policies => {
56             if (policies.policy_ids.length != 0) {
57                 policies.policy_ids.forEach(policyId => {
58                     var policyInstance = {} as PolicyInstance
59                     this.policySvc.getPolicyInstance(policyId).subscribe(policyInstance => {
60                         this.policySvc.getPolicyStatus(policyId).subscribe(policyStatus => {
61                             policyInstance.lastModified = policyStatus.last_modified;
62                         })
63                         this.policyInstances.push(policyInstance);
64                     })
65                     this.policyInstanceSubject.next(this.policyInstances);
66                 })
67             }
68         })
69     }
70
71     connect(): Observable<PolicyInstance[]> {
72         const dataMutations = [
73             this.policyInstanceSubject.asObservable(),
74             this.sort.sortChange
75         ];
76         return merge(...dataMutations).pipe(map(() => {
77             return this.getSortedData([...this.policyInstanceSubject.getValue()]);
78         }));
79     }
80
81     disconnect(): void {
82         this.policyInstanceSubject.complete();
83         this.loadingSubject.complete();
84     }
85
86     private getSortedData(data: PolicyInstance[]) {
87         if (!this.sort || !this.sort.active || this.sort.direction === '') {
88             return data;
89         }
90
91         return data.sort((a, b) => {
92             const isAsc = this.sort.direction === 'asc';
93             switch (this.sort.active) {
94                 case 'instanceId': return compare(a.policy_id, b.policy_id, isAsc);
95                 case 'ric': return compare(a.ric_id, b.ric_id, isAsc);
96                 case 'service': return compare(a.service_id, b.service_id, isAsc);
97                 case 'lastModified': return compare(a.lastModified, b.lastModified, isAsc);
98                 default: return 0;
99             }
100         });
101     }
102 }
103
104 function compare(a: string, b: string, isAsc: boolean) {
105     return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
106 }