3f94cf6a83347e9d32cc98ff9fa01189ad2b6c7c
[portal/ric-dashboard.git] / dashboard / 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
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 { MatSort } from '@angular/material/sort';
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 { XappControlRow, XMXapp, XMXappInstance } from '../interfaces/app-mgr.types';
30 import { AppMgrService } from '../services/app-mgr/app-mgr.service';
31 import { NotificationService } from '../services/ui/notification.service';
32
33 export class AppControlDataSource extends DataSource<XappControlRow> {
34
35   private appControlSubject = new BehaviorSubject<XappControlRow[]>([]);
36
37   private loadingSubject = new BehaviorSubject<boolean>(false);
38
39   public loading$ = this.loadingSubject.asObservable();
40
41   public rowCount = 1; // hide footer during intial load
42
43   private emptyInstances: XMXappInstance =
44     {
45       ip: null,
46       name: null,
47       port: null,
48       status: null,
49       rxMessages: [],
50       txMessages: [],
51       policies: [],
52     };
53
54   constructor(private appMgrSvc: AppMgrService,
55     private sort: MatSort,
56     private notificationService: NotificationService) {
57     super();
58   }
59
60   loadTable(instanceKey: string) {
61     this.loadingSubject.next(true);
62     this.appMgrSvc.getDeployed(instanceKey)
63       .pipe(
64         catchError((her: HttpErrorResponse) => {
65           console.log('AppControlDataSource failed: ' + her.message);
66           this.notificationService.error('Failed to get applications: ' + her.message);
67           return of([]);
68         }),
69         finalize(() => this.loadingSubject.next(false))
70       )
71       .subscribe((xApps: XMXapp[]) => {
72         this.rowCount = xApps.length;
73         const flattenedApps = this.flatten(xApps);
74         this.appControlSubject.next(flattenedApps);
75       });
76   }
77
78   connect(collectionViewer: CollectionViewer): Observable<XappControlRow[]> {
79     const dataMutations = [
80       this.appControlSubject.asObservable(),
81       this.sort.sortChange
82     ];
83     return merge(...dataMutations).pipe(map(() => {
84       return this.getSortedData([...this.appControlSubject.getValue()]);
85     }));
86   }
87
88   disconnect(collectionViewer: CollectionViewer): void {
89     this.appControlSubject.complete();
90     this.loadingSubject.complete();
91   }
92
93   private flatten(allxappdata: XMXapp[]): XappControlRow[] {
94     const xAppInstances: XappControlRow[] = [];
95     for (const xapp of allxappdata) {
96       if (!xapp.instances) {
97         const row: XappControlRow = {
98           xapp: xapp.name,
99           instance: this.emptyInstances
100         };
101         xAppInstances.push(row);
102       } else {
103         for (const ins of xapp.instances) {
104           const row: XappControlRow = {
105             xapp: xapp.name,
106             instance: ins
107           };
108           xAppInstances.push(row);
109         }
110       }
111     }
112     return xAppInstances;
113   }
114
115   private getSortedData(data: XappControlRow[]) {
116     if (!this.sort.active || this.sort.direction === '') {
117       return data;
118     }
119
120     return data.sort((a, b) => {
121       const isAsc = this.sort.direction === 'asc';
122       switch (this.sort.active) {
123         case 'xapp': return compare(a.xapp, b.xapp, isAsc);
124         case 'name': return compare(a.instance.name, b.instance.name, isAsc);
125         case 'status': return compare(a.instance.status, b.instance.status, isAsc);
126         case 'ip': return compare(a.instance.ip, b.instance.ip, isAsc);
127         case 'port': return compare(a.instance.port, b.instance.port, isAsc);
128         default: return 0;
129       }
130     });
131   }
132 }
133
134 function compare(a: any, b: any, isAsc: boolean) {
135   return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
136 }