70b8c453ddb14c7e5edc2061a4482e0c3fd436f8
[nonrtric.git] / dashboard / webapp-frontend / src / app / policy-control / policy-control.component.ts
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 import { Component, OnInit, ViewChild } from '@angular/core';
21 import { MatDialog, MatDialogConfig } from '@angular/material/dialog';
22 import { MatSort } from '@angular/material/sort';
23 import { animate, state, style, transition, trigger } from '@angular/animations';
24 import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
25
26 import { PolicyService } from '../services/policy/policy.service';
27 import { PolicyType } from '../interfaces/policy.types';
28 import { PolicyTypeDataSource } from './policy-type.datasource';
29 import { PolicyInstanceDataSource } from './policy-instance.datasource';
30 import { getPolicyDialogProperties } from './policy-instance-dialog.component';
31 import { PolicyInstanceDialogComponent } from './policy-instance-dialog.component';
32 import { PolicyInstance } from '../interfaces/policy.types';
33 import { NotificationService } from '../services/ui/notification.service';
34 import { ErrorDialogService } from '../services/ui/error-dialog.service';
35 import { ConfirmDialogService } from './../services/ui/confirm-dialog.service';
36 import { Subject } from 'rxjs';
37
38 class PolicyTypeInfo {
39     constructor(public type: PolicyType, public isExpanded: boolean) { }
40
41     isExpandedObservers: Subject<boolean> = new Subject<boolean>();
42 };
43
44 @Component({
45     selector: 'rd-policy-control',
46     templateUrl: './policy-control.component.html',
47     styleUrls: ['./policy-control.component.scss'],
48     animations: [
49         trigger('detailExpand', [
50             state('collapsed', style({ height: '0px', minHeight: '0', visibility: 'hidden' })),
51             state('expanded', style({ height: '*', visibility: 'visible' })),
52             transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
53         ]),
54     ],
55 })
56 export class PolicyControlComponent implements OnInit {
57
58     policyTypesDataSource: PolicyTypeDataSource;
59     @ViewChild(MatSort, { static: true }) sort: MatSort;
60
61     expandedTypes = new Map<string, PolicyTypeInfo>();
62
63     constructor(
64         private policySvc: PolicyService,
65         private dialog: MatDialog,
66         private errorDialogService: ErrorDialogService,
67         private notificationService: NotificationService,
68         private confirmDialogService: ConfirmDialogService) { }
69
70     ngOnInit() {
71         this.policyTypesDataSource = new PolicyTypeDataSource(this.policySvc, this.sort, this.notificationService);
72         this.policyTypesDataSource.loadTable();
73     }
74
75     createPolicyInstance(policyType: PolicyType): void {
76         const dialogRef = this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(policyType, null));
77         const info: PolicyTypeInfo = this.getPolicyTypeInfo(policyType);
78         dialogRef.afterClosed().subscribe(
79             (result: any) => {
80                 info.isExpandedObservers.next(info.isExpanded);
81             }
82         );
83     }
84
85     toggleListInstances(policyType: PolicyType): void {
86         let info = this.getPolicyTypeInfo(policyType);
87         info.isExpanded = !info.isExpanded;
88         info.isExpandedObservers.next(info.isExpanded);
89     }
90
91     getPolicyTypeInfo(policyType: PolicyType): PolicyTypeInfo {
92         let info: PolicyTypeInfo = this.expandedTypes.get(policyType.name);
93         if (!info) {
94             info = new PolicyTypeInfo(policyType, false);
95             this.expandedTypes.set(policyType.name, info);
96         }
97         return info;
98     }
99
100     isInstancesShown(policyType: PolicyType): boolean {
101         return this.getPolicyTypeInfo(policyType).isExpanded;
102     }
103
104     getPolicyTypeName(type: PolicyType): string {
105         const schema = JSON.parse(type.create_schema);
106         if (schema.title) {
107             return schema.title;
108         }
109         return type.name;
110     }
111
112     getObservable(policyType: PolicyType): Subject<boolean> {
113         return this.getPolicyTypeInfo(policyType).isExpandedObservers;
114     }
115 }