Sharable Error dialog
[portal/ric-dashboard.git] / webapp-frontend / src / app / control / control.component.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 import { Component, OnInit, ViewEncapsulation } from '@angular/core';
21 import { LocalDataSource } from 'ng2-smart-table';
22 import { XappMgrService } from '../services/xapp-mgr/xapp-mgr.service';
23 import { Router } from '@angular/router';
24 import { ConfirmDialogService } from './../services/ui/confirm-dialog.service'
25 import { NotificationService } from './../services/ui/notification.service'
26 import { XMXapp } from '../interfaces/xapp-mgr.types';
27
28
29 @Component({
30   selector: 'app-control',
31   templateUrl: './control.component.html',
32   styleUrls: ['./control.component.css'],
33   encapsulation: ViewEncapsulation.Emulated,
34 })
35 export class ControlComponent implements OnInit  {
36
37   settings = {
38     hideSubHeader: true,
39     actions: {
40       columnTitle: 'Actions',
41       add: false,
42       edit: false,
43       delete: false,
44       custom: [
45         { name: 'view', title: '<i class="material-icons">visibility</i>', },
46         { name: 'undeploy', title: '<i class="material-icons red-close">close</i>', },
47       ],
48       position: 'right'
49
50     },
51     columns: {
52       xapp: {
53         title: 'xApp Name',
54         type: 'string',
55       },
56       name: {
57         title: 'Instance Name',
58         type: 'string',
59       },
60       status: {
61         title: 'Status',
62         type: 'string',
63       },
64       ip: {
65         title: 'IP',
66         type: 'string',
67       },
68       port: {
69         title: 'Port',
70         type: 'integer',
71       },
72       txMessages: {
73         title: 'txMessages',
74         type: 'array',
75       },
76       rxMessages: {
77         title: 'rxMessages',
78         type: 'array',
79       },
80     },
81   };
82
83   source: LocalDataSource = new LocalDataSource();
84
85   constructor(
86     private xappMgrSvc: XappMgrService,
87     private router: Router,
88     private confirmDialogService: ConfirmDialogService,
89     private notification: NotificationService) { }
90
91   ngOnInit() {
92     this.xappMgrSvc.getAll().subscribe((xapps: XMXapp[]) => this.source.load(this.getInstance(xapps)));
93   }
94
95   onxAppControlAction(event) {
96     switch (event.action) {
97       case 'view':
98         this.view(event);
99         break;
100       case 'undeploy':
101         this.undeploy(event);
102         break;
103     }
104   }
105
106   view(event): void {
107     const url = '/xapp';
108     this.router.navigate([url, event]);
109   }
110
111   undeploy(event): void {
112     this.confirmDialogService.openConfirmDialog('Are you sure you want to undeploy this xApp ?')
113       .afterClosed().subscribe(res => {
114         if (res) {
115           this.xappMgrSvc.undeployXapp(event.data.xapp).subscribe(
116             response => {
117               this.xappMgrSvc.getAll().subscribe((xapps: XMXapp[]) => this.source.load(this.getInstance(xapps)));
118               switch (response.status) {
119                 case 200:
120                   this.notification.success('xApp undeployed successfully!');
121                   break;
122                 default:
123                   this.notification.warn('xApp undeploy failed.');
124               }
125             }
126           );
127         }
128       });
129   }
130
131   getInstance(allxappdata: XMXapp[]) {
132     const xAppInstances = [];
133     for (const xappindex in allxappdata) {
134       const instancelist = allxappdata[xappindex].instances;
135       for (const instanceindex in instancelist) {
136         var instance: any;
137         instance = instancelist[instanceindex];
138         instance.xapp = allxappdata[xappindex].name;
139         xAppInstances.push(instance);
140       }
141     }
142     return xAppInstances;
143   }
144
145
146 }