blob: b62deb4fb03fc7b6e5c3d6d0a13c04cfad5b2fbf [file] [log] [blame]
PatrikBuhra2bc79c2019-10-29 13:39:00 +01001/*-
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
21import { MatSort } from '@angular/material';
22import { Component, OnInit, ViewChild, Input, AfterViewInit } from '@angular/core';
23import { MatDialog } from '@angular/material/dialog';
24import { PolicyType } from '../interfaces/policy.types';
25import { PolicyInstanceDataSource } from './policy-instance.datasource';
26import { ErrorDialogService } from '../services/ui/error-dialog.service';
27import { NotificationService } from '../services/ui/notification.service';
28import { PolicyService } from '../services/policy/policy.service';
29import { ConfirmDialogService } from './../services/ui/confirm-dialog.service';
30import { PolicyInstance } from '../interfaces/policy.types';
31import { PolicyInstanceDialogComponent } from './policy-instance-dialog.component';
32import { getPolicyDialogProperties } from './policy-instance-dialog.component';
33import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
34import { Observable } from 'rxjs';
PatrikBuhrdbb8eba2019-11-15 14:12:11 +010035import { UiService } from '../services/ui/ui.service';
PatrikBuhra2bc79c2019-10-29 13:39:00 +010036
37@Component({
38 selector: 'rd-policy-instance',
39 templateUrl: './policy-instance.component.html',
40 styleUrls: ['./policy-instance.component.scss']
41})
42
43
44export class PolicyInstanceComponent implements OnInit, AfterViewInit {
45 instanceDataSource: PolicyInstanceDataSource;
46 @Input() policyType: PolicyType;
47 @Input() expanded: Observable<boolean>;
48 @ViewChild(MatSort, { static: true }) sort: MatSort;
PatrikBuhrdbb8eba2019-11-15 14:12:11 +010049 darkMode: boolean;
PatrikBuhra2bc79c2019-10-29 13:39:00 +010050
51 constructor(
52 private policySvc: PolicyService,
53 private dialog: MatDialog,
54 private errorDialogService: ErrorDialogService,
55 private notificationService: NotificationService,
PatrikBuhrdbb8eba2019-11-15 14:12:11 +010056 private confirmDialogService: ConfirmDialogService,
57 private ui: UiService) {
PatrikBuhra2bc79c2019-10-29 13:39:00 +010058 }
PatrikBuhrdbb8eba2019-11-15 14:12:11 +010059
PatrikBuhra2bc79c2019-10-29 13:39:00 +010060 ngOnInit() {
61 this.instanceDataSource = new PolicyInstanceDataSource(this.policySvc, this.sort, this.notificationService, this.policyType);
62 this.expanded.subscribe((isExpanded: boolean) => this.onExpand(isExpanded));
PatrikBuhrdbb8eba2019-11-15 14:12:11 +010063 this.ui.darkModeState.subscribe((isDark) => {
64 this.darkMode = isDark;
65 });
PatrikBuhra2bc79c2019-10-29 13:39:00 +010066 }
PatrikBuhrdbb8eba2019-11-15 14:12:11 +010067
PatrikBuhra2bc79c2019-10-29 13:39:00 +010068 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.policy_type_id, instance.instanceId).subscribe(
80 (refreshedJson: any) => {
81 instance.instance = JSON.stringify(refreshedJson);
PatrikBuhrdbb8eba2019-11-15 14:12:11 +010082 this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(this.policyType, instance, this.darkMode));
PatrikBuhra2bc79c2019-10-29 13:39:00 +010083 },
84 (httpError: HttpErrorResponse) => {
85 this.notificationService.error('Could not refresh instance ' + httpError.message);
PatrikBuhrdbb8eba2019-11-15 14:12:11 +010086 this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(this.policyType, instance, this.darkMode));
PatrikBuhra2bc79c2019-10-29 13:39:00 +010087 }
88 );
89 }
90
91 hasInstances(): boolean {
92 return this.instanceDataSource.rowCount > 0;
93 }
94
95 deleteInstance(instance: PolicyInstance): void {
96 this.confirmDialogService
97 .openConfirmDialog('Are you sure you want to delete this policy instance?')
98 .afterClosed().subscribe(
99 (res: any) => {
100 if (res) {
101 this.policySvc.deletePolicy(this.policyType.policy_type_id, instance.instanceId)
102 .subscribe(
103 (response: HttpResponse<Object>) => {
104 switch (response.status) {
105 case 200:
106 this.notificationService.success('Delete succeeded!');
107 this.instanceDataSource.loadTable();
108 break;
109 default:
110 this.notificationService.warn('Delete failed.');
111 }
112 },
113 (error: HttpErrorResponse) => {
114 this.errorDialogService.displayError(error.message);
115 });
116 }
117 });
118 }
PatrikBuhrdbb8eba2019-11-15 14:12:11 +0100119
120
121
PatrikBuhra2bc79c2019-10-29 13:39:00 +0100122}