30752efa9a9b9f890e3fe1a59ee0a87643ccd882
[portal/ric-dashboard.git] / webapp-frontend / src / app / control / control.datasource.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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 { MatSort } from '@angular/material';
23 import { merge } from 'rxjs';
24 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
25 import { Observable } from 'rxjs/Observable';
26 import { of } from 'rxjs/observable/of';
27 import { catchError, finalize, map } from 'rxjs/operators';
28 import { XappControlRow, XMXapp } from '../interfaces/xapp-mgr.types';
29 import { XappMgrService } from '../services/xapp-mgr/xapp-mgr.service';
30
31 export class ControlDataSource extends DataSource<XappControlRow> {
32
33   private xAppInstancesSubject = new BehaviorSubject<XappControlRow[]>([]);
34
35   private loadingSubject = new BehaviorSubject<boolean>(false);
36
37   public loading$ = this.loadingSubject.asObservable();
38
39   constructor(private xappMgrSvc: XappMgrService, private sort: MatSort) {
40     super();
41   };
42
43   loadTable() {
44     this.loadingSubject.next(true);
45     this.xappMgrSvc.getAll()
46       .pipe(
47         catchError(() => of([])),
48         finalize(() => this.loadingSubject.next(false))
49       )
50       .subscribe(xApps => this.xAppInstancesSubject.next(this.getInstance(xApps)) )
51   }
52
53   connect(collectionViewer: CollectionViewer): Observable<XappControlRow[]> {
54     const dataMutations = [
55       this.xAppInstancesSubject.asObservable(),
56       this.sort.sortChange
57     ];
58     return merge(...dataMutations).pipe(map(() => {
59       return this.getSortedData([...this.xAppInstancesSubject.getValue()]);
60     }));
61   }
62
63   disconnect(collectionViewer: CollectionViewer): void {
64     this.xAppInstancesSubject.complete();
65     this.loadingSubject.complete();
66   }
67
68   getInstance(allxappdata: XMXapp[]) {
69     const xAppInstances: XappControlRow[]= [];
70     for (const xappindex in allxappdata) {
71       const instancelist = allxappdata[xappindex].instances;
72       for (const instanceindex in instancelist) {
73         var instance: XappControlRow = {
74           xapp: allxappdata[xappindex].name,
75           instance: instancelist[instanceindex]
76         }
77         xAppInstances.push(instance);
78       }
79     }
80     return xAppInstances;
81   }
82
83   private getSortedData(data: XappControlRow[]) {
84     if (!this.sort.active || this.sort.direction === '') {
85       return data;
86     }
87
88     return data.sort((a, b) => {
89       const isAsc = this.sort.direction === 'asc';
90       switch (this.sort.active) {
91         case 'xapp': return compare(a.xapp, b.xapp, isAsc);
92         case 'name': return compare(a.instance.name, b.instance.name, isAsc);
93         case 'status': return compare(a.instance.status, b.instance.status, isAsc);
94         case 'ip': return compare(a.instance.ip, b.instance.ip, isAsc);
95         case 'port': return compare(a.instance.port, b.instance.port, isAsc);
96         default: return 0;
97       }
98     });
99   }
100 }
101
102 function compare(a, b, isAsc) {
103   return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
104 }