PatrikBuhr | a2bc79c | 2019-10-29 13:39:00 +0100 | [diff] [blame] | 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'; |
PatrikBuhr | dbb8eba | 2019-11-15 14:12:11 +0100 | [diff] [blame^] | 35 | import { UiService } from '../services/ui/ui.service'; |
PatrikBuhr | a2bc79c | 2019-10-29 13:39:00 +0100 | [diff] [blame] | 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; |
PatrikBuhr | dbb8eba | 2019-11-15 14:12:11 +0100 | [diff] [blame^] | 49 | darkMode: boolean; |
PatrikBuhr | a2bc79c | 2019-10-29 13:39:00 +0100 | [diff] [blame] | 50 | |
| 51 | constructor( |
| 52 | private policySvc: PolicyService, |
| 53 | private dialog: MatDialog, |
| 54 | private errorDialogService: ErrorDialogService, |
| 55 | private notificationService: NotificationService, |
PatrikBuhr | dbb8eba | 2019-11-15 14:12:11 +0100 | [diff] [blame^] | 56 | private confirmDialogService: ConfirmDialogService, |
| 57 | private ui: UiService) { |
PatrikBuhr | a2bc79c | 2019-10-29 13:39:00 +0100 | [diff] [blame] | 58 | } |
PatrikBuhr | dbb8eba | 2019-11-15 14:12:11 +0100 | [diff] [blame^] | 59 | |
PatrikBuhr | a2bc79c | 2019-10-29 13:39:00 +0100 | [diff] [blame] | 60 | ngOnInit() { |
| 61 | this.instanceDataSource = new PolicyInstanceDataSource(this.policySvc, this.sort, this.notificationService, this.policyType); |
| 62 | this.expanded.subscribe((isExpanded: boolean) => this.onExpand(isExpanded)); |
PatrikBuhr | dbb8eba | 2019-11-15 14:12:11 +0100 | [diff] [blame^] | 63 | this.ui.darkModeState.subscribe((isDark) => { |
| 64 | this.darkMode = isDark; |
| 65 | }); |
PatrikBuhr | a2bc79c | 2019-10-29 13:39:00 +0100 | [diff] [blame] | 66 | } |
PatrikBuhr | dbb8eba | 2019-11-15 14:12:11 +0100 | [diff] [blame^] | 67 | |
PatrikBuhr | a2bc79c | 2019-10-29 13:39:00 +0100 | [diff] [blame] | 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.policy_type_id, instance.instanceId).subscribe( |
| 80 | (refreshedJson: any) => { |
| 81 | instance.instance = JSON.stringify(refreshedJson); |
PatrikBuhr | dbb8eba | 2019-11-15 14:12:11 +0100 | [diff] [blame^] | 82 | this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(this.policyType, instance, this.darkMode)); |
PatrikBuhr | a2bc79c | 2019-10-29 13:39:00 +0100 | [diff] [blame] | 83 | }, |
| 84 | (httpError: HttpErrorResponse) => { |
| 85 | this.notificationService.error('Could not refresh instance ' + httpError.message); |
PatrikBuhr | dbb8eba | 2019-11-15 14:12:11 +0100 | [diff] [blame^] | 86 | this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(this.policyType, instance, this.darkMode)); |
PatrikBuhr | a2bc79c | 2019-10-29 13:39:00 +0100 | [diff] [blame] | 87 | } |
| 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 | } |
PatrikBuhr | dbb8eba | 2019-11-15 14:12:11 +0100 | [diff] [blame^] | 119 | |
| 120 | |
| 121 | |
PatrikBuhr | a2bc79c | 2019-10-29 13:39:00 +0100 | [diff] [blame] | 122 | } |