adc6a6d7f4dbb5c5bfb4381818698744522b4b31
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy-control / policy-instance.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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 { MatSort } from '@angular/material';
22 import { Component, OnInit, ViewChild, Input, AfterViewInit } from '@angular/core';
23 import { MatDialog } from '@angular/material/dialog';
24 import { PolicyType } from '../interfaces/policy.types';
25 import { PolicyInstanceDataSource } from './policy-instance.datasource';
26 import { ErrorDialogService } from '../services/ui/error-dialog.service';
27 import { NotificationService } from '../services/ui/notification.service';
28 import { PolicyService } from '../services/policy/policy.service';
29 import { ConfirmDialogService } from './../services/ui/confirm-dialog.service';
30 import { PolicyInstance } from '../interfaces/policy.types';
31 import { PolicyInstanceDialogComponent } from './policy-instance-dialog.component';
32 import { getPolicyDialogProperties } from './policy-instance-dialog.component';
33 import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
34 import { Observable } from 'rxjs';
35 import { UiService } from '../services/ui/ui.service';
36
37 @Component({
38     selector: 'rd-policy-instance',
39     templateUrl: './policy-instance.component.html',
40     styleUrls: ['./policy-instance.component.scss']
41 })
42
43
44 export class PolicyInstanceComponent implements OnInit, AfterViewInit {
45     instanceDataSource: PolicyInstanceDataSource;
46     @Input() policyType: PolicyType;
47     @Input() expanded: Observable<boolean>;
48     @ViewChild(MatSort, { static: true }) sort: MatSort;
49     darkMode: boolean;
50
51     constructor(
52         private policySvc: PolicyService,
53         private dialog: MatDialog,
54         private errorDialogService: ErrorDialogService,
55         private notificationService: NotificationService,
56         private confirmDialogService: ConfirmDialogService,
57         private ui: UiService) {
58     }
59
60     ngOnInit() {
61         this.instanceDataSource = new PolicyInstanceDataSource(this.policySvc, this.sort, this.notificationService, this.policyType);
62         this.expanded.subscribe((isExpanded: boolean) => this.onExpand(isExpanded));
63         this.ui.darkModeState.subscribe((isDark) => {
64             this.darkMode = isDark;
65         });
66     }
67
68     ngAfterViewInit() {
69         this.instanceDataSource.sort = this.sort;
70     }
71
72     private onExpand(isExpanded: boolean) {
73         if (isExpanded) {
74             this.instanceDataSource.loadTable();
75         }
76     }
77
78     modifyInstance(instance: PolicyInstance): void {
79         this.policySvc.getPolicy(this.policyType.name, instance.id).subscribe(
80             (refreshedJson: any) => {
81                 instance.json = JSON.stringify(refreshedJson);
82                 this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(this.policyType, instance, this.darkMode));
83             },
84             (httpError: HttpErrorResponse) => {
85                 this.notificationService.error('Could not refresh instance ' + httpError.message);
86                 this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(this.policyType, instance, this.darkMode));
87             }
88         );
89     }
90
91     hasInstances(): boolean {
92         return this.instanceDataSource.rowCount > 0;
93     }
94
95     toLocalTime(utcTime: string): string {
96         const date = new Date(utcTime);
97         const toutc = date.toUTCString();
98         return new Date(toutc + ' UTC').toLocaleString();
99
100     }
101
102     deleteInstance(instance: PolicyInstance): void {
103         this.confirmDialogService
104             .openConfirmDialog('Are you sure you want to delete this policy instance?')
105             .afterClosed().subscribe(
106                 (res: any) => {
107                     if (res) {
108                         this.policySvc.deletePolicy(this.policyType.name, instance.id)
109                             .subscribe(
110                                 (response: HttpResponse<Object>) => {
111                                     switch (response.status) {
112                                         case 200:
113                                             this.notificationService.success('Delete succeeded!');
114                                             this.instanceDataSource.loadTable();
115                                             break;
116                                         default:
117                                             this.notificationService.warn('Delete failed.');
118                                     }
119                                 },
120                                 (error: HttpErrorResponse) => {
121                                     this.errorDialogService.displayError(error.message);
122                                 });
123                     }
124                 });
125     }
126
127
128
129 }