Create overall control with AppControl,RANControl
[portal/ric-dashboard.git] / webapp-frontend / src / app / app-control / app-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, XMDeployedApp, XMXappInstance } from '../interfaces/app-mgr.types';
29 import { AppMgrService } from '../services/app-mgr/app-mgr.service';
30
31 export class AppControlDataSource 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   emptyInstances: XMXappInstance =
40     { ip: null,
41       name: null,
42       port: null,
43       status: null,
44       rxMessages: [],
45       txMessages: [],
46     };
47
48   constructor(private appMgrSvc: AppMgrService, private sort: MatSort) {
49     super();
50   }
51
52   loadTable() {
53     this.loadingSubject.next(true);
54     this.appMgrSvc.getDeployed()
55       .pipe(
56         catchError(() => of([])),
57         finalize(() => this.loadingSubject.next(false))
58       )
59       .subscribe(xApps => this.xAppInstancesSubject.next(this.flatten(xApps)));
60   }
61
62   connect(collectionViewer: CollectionViewer): Observable<XappControlRow[]> {
63     const dataMutations = [
64       this.xAppInstancesSubject.asObservable(),
65       this.sort.sortChange
66     ];
67     return merge(...dataMutations).pipe(map(() => {
68       return this.getSortedData([...this.xAppInstancesSubject.getValue()]);
69     }));
70   }
71
72   disconnect(collectionViewer: CollectionViewer): void {
73     this.xAppInstancesSubject.complete();
74     this.loadingSubject.complete();
75   }
76
77   private flatten(allxappdata: XMDeployedApp[]) {
78     const xAppInstances: XappControlRow[] = [];
79     for (const xapp of allxappdata) {
80       if (!xapp.instances) {
81         const row: XappControlRow = {
82           xapp: xapp.name,
83           instance: this.emptyInstances
84         };
85         xAppInstances.push(row);
86       } else {
87         for (const ins of xapp.instances) {
88           const row: XappControlRow = {
89             xapp: xapp.name,
90             instance: ins
91           };
92           xAppInstances.push(row);
93         }
94       }
95     }
96     return xAppInstances;
97   }
98
99   private getSortedData(data: XappControlRow[]) {
100     if (!this.sort.active || this.sort.direction === '') {
101       return data;
102     }
103
104     return data.sort((a, b) => {
105       const isAsc = this.sort.direction === 'asc';
106       switch (this.sort.active) {
107         case 'xapp': return compare(a.xapp, b.xapp, isAsc);
108         case 'name': return compare(a.instance.name, b.instance.name, isAsc);
109         case 'status': return compare(a.instance.status, b.instance.status, isAsc);
110         case 'ip': return compare(a.instance.ip, b.instance.ip, isAsc);
111         case 'port': return compare(a.instance.port, b.instance.port, isAsc);
112         default: return 0;
113       }
114     });
115   }
116 }
117
118 function compare(a, b, isAsc) {
119   return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
120 }