Merge "Clean up A1 controller code"
[nonrtric.git] / dashboard / 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
36 @Component({
37     selector: 'rd-policy-instance',
38     templateUrl: './policy-instance.component.html',
39     styleUrls: ['./policy-instance.component.scss']
40 })
41
42
43 export class PolicyInstanceComponent implements OnInit, AfterViewInit {
44     instanceDataSource: PolicyInstanceDataSource;
45     @Input() policyType: PolicyType;
46     @Input() expanded: Observable<boolean>;
47     @ViewChild(MatSort, { static: true }) sort: MatSort;
48
49     constructor(
50         private policySvc: PolicyService,
51         private dialog: MatDialog,
52         private errorDialogService: ErrorDialogService,
53         private notificationService: NotificationService,
54         private confirmDialogService: ConfirmDialogService) {
55     }
56    
57     ngOnInit() {
58         this.instanceDataSource = new PolicyInstanceDataSource(this.policySvc, this.sort, this.notificationService, this.policyType);
59         this.expanded.subscribe((isExpanded: boolean) => this.onExpand(isExpanded));
60     }
61     
62     ngAfterViewInit() {
63         this.instanceDataSource.sort = this.sort;
64     }
65
66     private onExpand(isExpanded: boolean) {
67         if (isExpanded) {
68             this.instanceDataSource.loadTable();
69         }
70     }
71
72     modifyInstance(instance: PolicyInstance): void {
73         this.policySvc.getPolicy(this.policyType.policy_type_id, instance.instanceId).subscribe(
74             (refreshedJson: any) => {
75                 instance.instance = JSON.stringify(refreshedJson);
76                 this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(this.policyType, instance));
77             },
78             (httpError: HttpErrorResponse) => {
79                 this.notificationService.error('Could not refresh instance ' + httpError.message);
80                 this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(this.policyType, instance));
81             }
82         );
83     }
84
85     hasInstances(): boolean {
86         return this.instanceDataSource.rowCount > 0;
87     }
88
89     deleteInstance(instance: PolicyInstance): void {
90         this.confirmDialogService
91             .openConfirmDialog('Are you sure you want to delete this policy instance?')
92             .afterClosed().subscribe(
93                 (res: any) => {
94                     if (res) {
95                         this.policySvc.deletePolicy(this.policyType.policy_type_id, instance.instanceId)
96                             .subscribe(
97                                 (response: HttpResponse<Object>) => {
98                                     switch (response.status) {
99                                         case 200:
100                                             this.notificationService.success('Delete succeeded!');
101                                             this.instanceDataSource.loadTable();
102                                             break;
103                                         default:
104                                             this.notificationService.warn('Delete failed.');
105                                     }
106                                 },
107                                 (error: HttpErrorResponse) => {
108                                     this.errorDialogService.displayError(error.message);
109                                 });
110                     }
111                 });
112     }
113 }