95313d06b77c728ce6db70af7595e203fa257c81
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / policy-instance / 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/sort';
22 import { Component, OnInit, ViewChild, Input, AfterViewInit } from '@angular/core';
23 import { MatDialog } from '@angular/material/dialog';
24 import { PolicyTypeSchema } 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 { NoTypePolicyInstanceDialogComponent } from '../no-type-policy-instance-dialog/no-type-policy-instance-dialog.component';
32 import { PolicyInstanceDialogComponent } from '../policy-instance-dialog/policy-instance-dialog.component';
33 import { getPolicyDialogProperties } from '../policy-instance-dialog/policy-instance-dialog.component';
34 import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
35 import { Observable } from 'rxjs';
36 import { UiService } from '../../services/ui/ui.service';
37
38 @Component({
39     selector: 'nrcp-policy-instance',
40     templateUrl: './policy-instance.component.html',
41     styleUrls: ['./policy-instance.component.scss']
42 })
43
44
45 export class PolicyInstanceComponent implements OnInit, AfterViewInit {
46     instanceDataSource: PolicyInstanceDataSource;
47     @Input() policyTypeSchema: PolicyTypeSchema;
48     @Input() expanded: Observable<boolean>;
49     @ViewChild(MatSort, { static: true }) sort: MatSort;
50     darkMode: boolean;
51
52     constructor(
53         private policySvc: PolicyService,
54         private dialog: MatDialog,
55         private errorDialogService: ErrorDialogService,
56         private notificationService: NotificationService,
57         private confirmDialogService: ConfirmDialogService,
58         private ui: UiService) {
59     }
60
61     ngOnInit() {
62         this.instanceDataSource = new PolicyInstanceDataSource(this.policySvc, this.sort, this.policyTypeSchema);
63         this.expanded.subscribe((isExpanded: boolean) => this.onExpand(isExpanded));
64         this.ui.darkModeState.subscribe((isDark) => {
65             this.darkMode = isDark;
66         });
67     }
68
69     ngAfterViewInit() {
70         this.instanceDataSource.sort = this.sort;
71     }
72
73     private onExpand(isExpanded: boolean) {
74         if (isExpanded) {
75             this.instanceDataSource.getPolicyInstances();
76         }
77     }
78
79     private isSchemaEmpty(): boolean {
80         return this.policyTypeSchema.schemaObject === '{}';
81     }
82
83     modifyInstance(instance: PolicyInstance): void {
84         this.policySvc.getPolicyInstance(instance.policy_id).subscribe(
85             (refreshedJson: any) => {
86                 instance = refreshedJson;
87                 if (this.isSchemaEmpty()) {
88                     this.dialog.open(
89                         NoTypePolicyInstanceDialogComponent,
90                         getPolicyDialogProperties(this.policyTypeSchema, instance, this.darkMode)).afterClosed().subscribe(
91                             (_: any) => {
92                                 this.instanceDataSource.getPolicyInstances();
93                             }
94                         );
95                 } else {
96                     this.dialog.open(
97                         PolicyInstanceDialogComponent,
98                         getPolicyDialogProperties(this.policyTypeSchema, instance, this.darkMode)).afterClosed().subscribe(
99                             (_: any) => {
100                                 this.instanceDataSource.getPolicyInstances();
101                             }
102                         );
103
104                 }
105             },
106             (httpError: HttpErrorResponse) => {
107                 this.notificationService.error('Could not refresh instance. Please try again.' + httpError.message);
108             }
109         );
110     }
111
112     hasInstances(): boolean {
113         return this.instanceDataSource.rowCount > 0;
114     }
115
116     toLocalTime(utcTime: string): string {
117         const date = new Date(utcTime);
118         const toutc = date.toUTCString();
119         return new Date(toutc + ' UTC').toLocaleString();
120
121     }
122
123     deleteInstance(instance: PolicyInstance): void {
124         this.confirmDialogService
125             .openConfirmDialog('Are you sure you want to delete this policy instance?')
126             .afterClosed().subscribe(
127                 (res: any) => {
128                     if (res) {
129                         this.policySvc.deletePolicy(instance.policy_id)
130                             .subscribe(
131                                 (response: HttpResponse<Object>) => {
132                                     switch (response.status) {
133                                         case 204:
134                                             this.notificationService.success('Delete succeeded!');
135                                             this.instanceDataSource.getPolicyInstances();
136                                             break;
137                                         default:
138                                             this.notificationService.warn('Delete failed ' + response.status + ' ' + response.body);
139                                     }
140                                 },
141                                 (error: HttpErrorResponse) => {
142                                     this.errorDialogService.displayError(error.statusText + ', ' + error.error);
143                                 });
144                     }
145                 });
146     }
147 }