support-multiple-ric-instances
[portal/ric-dashboard.git] / webapp-frontend / src / app / app-control / app-control.component.ts
index c97a195..35770dc 100644 (file)
@@ -21,15 +21,17 @@ import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
 import { Component, OnInit, ViewChild } from '@angular/core';
 import { MatSort } from '@angular/material/sort';
 import { Router } from '@angular/router';
+import { Subscription } from 'rxjs';
+import { finalize } from 'rxjs/operators';
 import { XappControlRow } from '../interfaces/app-mgr.types';
 import { AppMgrService } from '../services/app-mgr/app-mgr.service';
+import { InstanceSelectorService } from '../services/instance-selector/instance-selector.service';
 import { ConfirmDialogService } from '../services/ui/confirm-dialog.service';
 import { ErrorDialogService } from '../services/ui/error-dialog.service';
 import { LoadingDialogService } from '../services/ui/loading-dialog.service';
 import { NotificationService } from '../services/ui/notification.service';
 import { AppControlAnimations } from './app-control.animations';
 import { AppControlDataSource } from './app-control.datasource';
-import { finalize } from 'rxjs/operators';
 
 @Component({
   selector: 'rd-app-control',
@@ -41,7 +43,9 @@ export class AppControlComponent implements OnInit {
 
   displayedColumns: string[] = ['xapp', 'name', 'status', 'ip', 'port', 'action'];
   dataSource: AppControlDataSource;
-  @ViewChild(MatSort, {static: true}) sort: MatSort;
+  @ViewChild(MatSort, { static: true }) sort: MatSort;
+  private instanceChange: Subscription;
+  private instanceKey: string;
 
   constructor(
     private appMgrSvc: AppMgrService,
@@ -49,48 +53,59 @@ export class AppControlComponent implements OnInit {
     private confirmDialogService: ConfirmDialogService,
     private errorDialogService: ErrorDialogService,
     private loadingDialogService: LoadingDialogService,
+    public instanceSelectorService: InstanceSelectorService,
     private notificationService: NotificationService) { }
 
+
   ngOnInit() {
     this.dataSource = new AppControlDataSource(this.appMgrSvc, this.sort, this.notificationService);
-    this.dataSource.loadTable();
+    this.instanceChange = this.instanceSelectorService.getSelectedInstancekey().subscribe((instanceKey: string) => {
+      if (instanceKey) {
+        this.instanceKey = instanceKey;
+        this.dataSource.loadTable(instanceKey);
+      }
+    })
+  }
+
+  ngOnDestroy() {
+    this.instanceChange.unsubscribe();
   }
 
   controlApp(app: XappControlRow): void {
     // TODO: identify apps without hardcoding to names
-    const acAppPattern0 =  /[Aa][Dd][Mm][Ii][Nn]/;
-    const acAppPattern1 =  /[Aa][Dd][Mm][Ii][Ss]{2}[Ii][Oo][Nn]/;
+    const acAppPattern0 = /[Aa][Dd][Mm][Ii][Nn]/;
+    const acAppPattern1 = /[Aa][Dd][Mm][Ii][Ss]{2}[Ii][Oo][Nn]/;
     if (acAppPattern0.test(app.xapp) || acAppPattern1.test(app.xapp)) {
       this.router.navigate(['/ac']);
-    }  else {
+    } else {
       this.errorDialogService.displayError('No control available for ' + app.xapp + ' (yet)');
     }
   }
 
   onUndeployApp(app: XappControlRow): void {
     this.confirmDialogService.openConfirmDialog('Are you sure you want to undeploy App ' + app.xapp + '?')
-      .afterClosed().subscribe( (res: boolean) => {
+      .afterClosed().subscribe((res: boolean) => {
         if (res) {
           this.loadingDialogService.startLoading("Undeploying " + app.xapp);
-          this.appMgrSvc.undeployXapp(app.xapp)
+          this.appMgrSvc.undeployXapp(this.instanceKey, app.xapp)
             .pipe(
               finalize(() => this.loadingDialogService.stopLoading())
             )
             .subscribe(
-            httpResponse: HttpResponse<Object>) => {
-              // Answers 204/No content on success
-              this.notificationService.success('App undeployed successfully!');
-              this.dataSource.loadTable();
-            },
-            (her: HttpErrorResponse) => {
-              // the error field should have an ErrorTransport object
-              let msg = her.message;
-              if (her.error && her.error.message) {
-                msg = her.error.message;
-              }
-              this.notificationService.warn('App undeploy failed: ' + msg);
-            })
-          );
+              (httpResponse: HttpResponse<Object>) => {
+                // Answers 204/No content on success
+                this.notificationService.success('App undeployed successfully!');
+                this.dataSource.loadTable(this.instanceKey);
+              },
+              ((her: HttpErrorResponse) => {
+                // the error field should have an ErrorTransport object
+                let msg = her.message;
+                if (her.error && her.error.message) {
+                  msg = her.error.message;
+                }
+                this.notificationService.warn('App undeploy failed: ' + msg);
+              })
+            );
         }
       });
   }