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';
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 { 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';
39 selector: 'rd-policy-instance',
40 templateUrl: './policy-instance.component.html',
41 styleUrls: ['./policy-instance.component.scss']
45 export class PolicyInstanceComponent implements OnInit, AfterViewInit {
46 instanceDataSource: PolicyInstanceDataSource;
47 @Input() policyTypeSchema: PolicyTypeSchema;
48 @Input() expanded: Observable<boolean>;
49 @ViewChild(MatSort, { static: true }) sort: MatSort;
53 private policySvc: PolicyService,
54 private dialog: MatDialog,
55 private errorDialogService: ErrorDialogService,
56 private notificationService: NotificationService,
57 private confirmDialogService: ConfirmDialogService,
58 private ui: UiService) {
62 this.instanceDataSource = new PolicyInstanceDataSource(this.policySvc, this.sort, this.notificationService,this.policyTypeSchema);
63 this.expanded.subscribe((isExpanded: boolean) => this.onExpand(isExpanded));
64 this.ui.darkModeState.subscribe((isDark) => {
65 this.darkMode = isDark;
70 this.instanceDataSource.sort = this.sort;
73 private onExpand(isExpanded: boolean) {
75 this.instanceDataSource.getPolicyInstances();
79 private isSchemaEmpty(): boolean {
80 return Object.keys(this.policyTypeSchema.schemaObject).length === 0;
83 modifyInstance(instance: PolicyInstance): void {
84 this.policySvc.getPolicy(this.policyTypeSchema.name, instance.policy_id).subscribe(
85 (refreshedJson: any) => {
86 instance.policy_data = JSON.stringify(refreshedJson);
87 if (this.isSchemaEmpty()) {
89 NoTypePolicyInstanceDialogComponent,
90 getPolicyDialogProperties(this.policyTypeSchema, instance, this.darkMode)).afterClosed().subscribe(
92 this.instanceDataSource.getPolicyInstances();
97 PolicyInstanceDialogComponent,
98 getPolicyDialogProperties(this.policyTypeSchema, instance, this.darkMode)).afterClosed().subscribe(
100 this.instanceDataSource.getPolicyInstances();
106 (httpError: HttpErrorResponse) => {
107 this.notificationService.error('Could not refresh instance. Please try again.' + httpError.message);
112 hasInstances(): boolean {
113 return this.instanceDataSource.rowCount > 0;
116 toLocalTime(utcTime: string): string {
117 const date = new Date(utcTime);
118 const toutc = date.toUTCString();
119 return new Date(toutc + ' UTC').toLocaleString();
123 deleteInstance(instance: PolicyInstance): void {
124 this.confirmDialogService
125 .openConfirmDialog('Are you sure you want to delete this policy instance?')
126 .afterClosed().subscribe(
129 this.policySvc.deletePolicy(this.policyTypeSchema.name, instance.policy_id)
131 (response: HttpResponse<Object>) => {
132 switch (response.status) {
134 this.notificationService.success('Delete succeeded!');
135 this.instanceDataSource.getPolicyInstances();
138 this.notificationService.warn('Delete failed ' + response.status + ' ' + response.body);
141 (error: HttpErrorResponse) => {
142 this.errorDialogService.displayError(error.statusText + ', ' + error.error);