Possibility to create and edit typeless policies
[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 { NoTypePolicyInstanceDialogComponent } from './no-type-policy-instance-dialog.component';
32 import { PolicyInstanceDialogComponent } from './policy-instance-dialog.component';
33 import { getPolicyDialogProperties } from './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: 'rd-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() policyType: PolicyType;
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.notificationService, this.policyType);
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.loadTable();
76         }
77     }
78
79     private isSchemaEmpty(): boolean {
80         return Object.keys(this.policyType.schemaObject).length === 0;
81     }
82
83     modifyInstance(instance: PolicyInstance): void {
84         this.policySvc.getPolicy(this.policyType.name, instance.id).subscribe(
85             (refreshedJson: any) => {
86                 instance.json = JSON.stringify(refreshedJson);
87                 if (this.isSchemaEmpty()) {
88                     this.dialog.open(
89                         NoTypePolicyInstanceDialogComponent,
90                         getPolicyDialogProperties(this.policyType, instance, this.darkMode));
91                 } else {
92                     this.dialog.open(
93                         PolicyInstanceDialogComponent,
94                         getPolicyDialogProperties(this.policyType, instance, this.darkMode));
95                 }
96             },
97             (httpError: HttpErrorResponse) => {
98                 this.notificationService.error('Could not refresh instance. Please try again.' + httpError.message);
99             }
100         );
101     }
102
103     hasInstances(): boolean {
104         return this.instanceDataSource.rowCount > 0;
105     }
106
107     toLocalTime(utcTime: string): string {
108         const date = new Date(utcTime);
109         const toutc = date.toUTCString();
110         return new Date(toutc + ' UTC').toLocaleString();
111
112     }
113
114     deleteInstance(instance: PolicyInstance): void {
115         this.confirmDialogService
116             .openConfirmDialog('Are you sure you want to delete this policy instance?')
117             .afterClosed().subscribe(
118                 (res: any) => {
119                     if (res) {
120                         this.policySvc.deletePolicy(this.policyType.name, instance.id)
121                             .subscribe(
122                                 (response: HttpResponse<Object>) => {
123                                     switch (response.status) {
124                                         case 200:
125                                             this.notificationService.success('Delete succeeded!');
126                                             this.instanceDataSource.loadTable();
127                                             break;
128                                         default:
129                                             this.notificationService.warn('Delete failed ' + response.status + ' ' + response.body);
130                                     }
131                                 },
132                                 (error: HttpErrorResponse) => {
133                                     this.errorDialogService.displayError(error.statusText + ', ' + error.error);
134                                 });
135                     }
136                 });
137     }
138
139
140
141 }