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 { Sort } from "@angular/material/sort";
22 import { Component, OnInit, Input } from "@angular/core";
23 import { MatDialog } from "@angular/material/dialog";
24 import { PolicyTypeSchema } from "@interfaces/policy.types";
25 import { NotificationService } from "@services/ui/notification.service";
26 import { PolicyService } from "@services/policy/policy.service";
27 import { ConfirmDialogService } from "@services/ui/confirm-dialog.service";
28 import { PolicyInstance } from "@interfaces/policy.types";
29 import { PolicyInstanceDialogComponent } from "../policy-instance-dialog/policy-instance-dialog.component";
30 import { getPolicyDialogProperties } from "../policy-instance-dialog/policy-instance-dialog.component";
31 import { HttpResponse } from "@angular/common/http";
32 import { BehaviorSubject, forkJoin } from "rxjs";
33 import { UiService } from "@services/ui/ui.service";
34 import { FormControl, FormGroup } from "@angular/forms";
35 import { MatTableDataSource } from "@angular/material/table";
36 import { finalize, mergeMap, map } from "rxjs/operators";
39 selector: "nrcp-policy-instance",
40 templateUrl: "./policy-instance.component.html",
41 styleUrls: ["./policy-instance.component.scss"],
43 export class PolicyInstanceComponent implements OnInit {
44 public slice: number = 1000;
45 @Input() policyTypeSchema: PolicyTypeSchema;
47 instanceDataSource: MatTableDataSource<PolicyInstance>;
48 policyInstanceForm: FormGroup;
49 private policyInstanceSubject = new BehaviorSubject<PolicyInstance[]>([]);
50 policyInstances: PolicyInstance[] = [];
51 private loadingSubject$ = new BehaviorSubject<boolean>(false);
52 public loading$ = this.loadingSubject$.asObservable();
53 public truncated = false;
56 private policySvc: PolicyService,
57 private dialog: MatDialog,
58 private notificationService: NotificationService,
59 private confirmDialogService: ConfirmDialogService,
62 this.policyInstanceForm = new FormGroup({
63 id: new FormControl(""),
64 target: new FormControl(""),
65 owner: new FormControl(""),
66 lastModified: new FormControl(""),
71 this.getPolicyInstances();
72 this.policyInstanceSubject.subscribe((data) => {
73 this.instanceDataSource = new MatTableDataSource<PolicyInstance>(data);
75 this.instanceDataSource.filterPredicate = ((
80 this.isDataIncluding(data.policy_id, filter.id) &&
81 this.isDataIncluding(data.ric_id, filter.target) &&
82 this.isDataIncluding(data.service_id, filter.owner) &&
83 this.isDataIncluding(data.lastModified, filter.lastModified)
85 }) as (data: PolicyInstance, filter: any) => boolean;
88 this.policyInstanceForm.valueChanges.subscribe((value) => {
91 id: value.id.trim().toLowerCase(),
93 this.instanceDataSource.filter = filter;
96 this.ui.darkModeState.subscribe((isDark) => {
97 this.darkMode = isDark;
101 getPolicyInstances() {
102 this.policyInstances = [] as PolicyInstance[];
103 this.loadingSubject$.next(true);
105 .getPolicyInstancesByType(this.policyTypeSchema.id)
108 if (data.policy_ids.length > this.slice) {
109 this.truncated = true;
110 data.policy_ids = data.policy_ids.slice(0, this.slice);
114 mergeMap((policyIds) =>
116 policyIds.policy_ids.map((id) => {
118 this.policySvc.getPolicyInstance(id),
119 this.policySvc.getPolicyStatus(id),
124 finalize(() => this.loadingSubject$.next(false))
126 .subscribe((res) => {
127 this.policyInstances = res.map((policy) => {
128 let policyInstance = <PolicyInstance>{};
129 policyInstance = policy[0];
130 policyInstance.lastModified = policy[1].last_modified;
131 return policyInstance;
133 this.policyInstanceSubject.next(this.policyInstances);
137 getSortedData(sort: Sort) {
138 const data = this.instanceDataSource.data;
139 data.sort((a: PolicyInstance, b: PolicyInstance) => {
140 const isAsc = sort.direction === "asc";
141 switch (sort.active) {
143 return compare(a.policy_id, b.policy_id, isAsc);
145 return compare(a.ric_id, b.ric_id, isAsc);
147 return compare(a.service_id, b.service_id, isAsc);
149 return compare(a.lastModified, b.lastModified, isAsc);
154 this.instanceDataSource.data = data;
157 stopSort(event: any) {
158 event.stopPropagation();
161 isDataIncluding(data: string, filter: string): boolean {
162 return !filter || data.toLowerCase().includes(filter);
165 createPolicyInstance(policyTypeSchema: PolicyTypeSchema): void {
166 this.openInstanceDialog(null);
169 modifyInstance(instance: PolicyInstance): void {
171 .getPolicyInstance(instance.policy_id)
172 .subscribe((refreshedJson: PolicyInstance) => {
173 this.openInstanceDialog(refreshedJson);
177 private openInstanceDialog(policy: PolicyInstance) {
178 const dialogData = getPolicyDialogProperties(
179 this.policyTypeSchema,
183 const dialogRef = this.dialog.open(
184 PolicyInstanceDialogComponent,
187 dialogRef.afterClosed().subscribe((ok: any) => {
188 if (ok) this.getPolicyInstances();
192 hasInstances(): boolean {
193 return this.instanceCount() > 0;
196 instanceCount(): number {
197 return this.policyInstances.length;
200 toLocalTime(utcTime: string): string {
201 const date = new Date(utcTime);
202 const toutc = date.toUTCString();
203 return new Date(toutc + " UTC").toLocaleString();
206 deleteInstance(instance: PolicyInstance): void {
207 this.confirmDialogService
210 "Are you sure you want to delete this policy instance?"
213 .subscribe((res: any) => {
216 .deletePolicy(instance.policy_id)
217 .subscribe((response: HttpResponse<Object>) => {
218 if (response.status === 204) {
219 this.notificationService.success("Delete succeeded!");
220 this.getPolicyInstances();
228 this.truncated = false;
229 this.getPolicyInstances();
233 function compare(a: string, b: string, isAsc: boolean) {
234 return (a < b ? -1 : 1) * (isAsc ? 1 : -1);