2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
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 { PolicyInstanceDialogComponent } from '../policy-instance-dialog/policy-instance-dialog.component';
32 import { getPolicyDialogProperties } from '../policy-instance-dialog/policy-instance-dialog.component';
33 import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
34 import { Observable } from 'rxjs';
35 import { UiService } from '../../services/ui/ui.service';
38 selector: 'nrcp-policy-instance',
39 templateUrl: './policy-instance.component.html',
40 styleUrls: ['./policy-instance.component.scss']
44 export class PolicyInstanceComponent implements OnInit, AfterViewInit {
45 instanceDataSource: PolicyInstanceDataSource;
46 @Input() policyTypeSchema: PolicyTypeSchema;
47 @Input() expanded: Observable<boolean>;
48 @ViewChild(MatSort, { static: true }) sort: MatSort;
52 private policySvc: PolicyService,
53 private dialog: MatDialog,
54 private errorDialogService: ErrorDialogService,
55 private notificationService: NotificationService,
56 private confirmDialogService: ConfirmDialogService,
57 private ui: UiService) {
61 this.instanceDataSource = new PolicyInstanceDataSource(this.policySvc, this.sort, this.policyTypeSchema);
62 this.expanded.subscribe((isExpanded: boolean) => this.onExpand(isExpanded));
63 this.ui.darkModeState.subscribe((isDark) => {
64 this.darkMode = isDark;
69 this.instanceDataSource.sort = this.sort;
72 private onExpand(isExpanded: boolean) {
74 this.instanceDataSource.getPolicyInstances();
78 private isSchemaEmpty(): boolean {
79 return this.policyTypeSchema.schemaObject === '{}';
82 modifyInstance(instance: PolicyInstance): void {
83 this.policySvc.getPolicyInstance(instance.policy_id).subscribe(
84 (refreshedJson: any) => {
85 instance = refreshedJson;
87 PolicyInstanceDialogComponent,
88 getPolicyDialogProperties(this.policyTypeSchema, instance, this.darkMode)).afterClosed().subscribe(
90 this.instanceDataSource.getPolicyInstances();
94 (httpError: HttpErrorResponse) => {
95 this.notificationService.error('Could not refresh instance. Please try again.' + httpError.message);
100 hasInstances(): boolean {
101 return this.instanceDataSource.rowCount > 0;
104 toLocalTime(utcTime: string): string {
105 const date = new Date(utcTime);
106 const toutc = date.toUTCString();
107 return new Date(toutc + ' UTC').toLocaleString();
111 deleteInstance(instance: PolicyInstance): void {
112 this.confirmDialogService
113 .openConfirmDialog('Are you sure you want to delete this policy instance?')
114 .afterClosed().subscribe(
117 this.policySvc.deletePolicy(instance.policy_id)
119 (response: HttpResponse<Object>) => {
120 switch (response.status) {
122 this.notificationService.success('Delete succeeded!');
123 this.instanceDataSource.getPolicyInstances();
126 this.notificationService.warn('Delete failed ' + response.status + ' ' + response.body);
129 (error: HttpErrorResponse) => {
130 this.errorDialogService.displayError(error.statusText + ', ' + error.error);