Create typed policy editor component
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / policy-instance-dialog / policy-instance-dialog.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 { AfterViewInit, Component, Inject, OnInit, ViewChild } from '@angular/core';
21 import { MatDialogConfig, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
22 import * as uuid from 'uuid';
23 import { CreatePolicyInstance, PolicyInstance, PolicyTypeSchema } from '../../interfaces/policy.types';
24 import { PolicyService } from '../../services/policy/policy.service';
25 import { ErrorDialogService } from '../../services/ui/error-dialog.service';
26 import { NotificationService } from './../../services/ui/notification.service';
27 import { UiService } from '../../services/ui/ui.service';
28 import { HttpErrorResponse } from '@angular/common/http';
29 import { FormGroup, FormControl, Validators } from '@angular/forms';
30 import { ChangeDetectorRef } from '@angular/core';
31 import { Ric, Rics } from '../../interfaces/ric';
32 import { TypedPolicyEditorComponent } from '../typed-policy-editor/typed-policy-editor.component';
33
34
35 @Component({
36     selector: 'nrcp-policy-instance-dialog',
37     templateUrl: './policy-instance-dialog.component.html',
38     styleUrls: ['./policy-instance-dialog.component.scss']
39 })
40 export class PolicyInstanceDialogComponent implements OnInit, AfterViewInit {
41     @ViewChild(TypedPolicyEditorComponent)
42     policyEditor: TypedPolicyEditorComponent;
43     instanceForm: FormGroup;
44
45
46     ric: string;
47     allRics: Ric[];
48     policyInstanceId: string; // null if not yet created
49     policyTypeName: string;
50     jsonSchemaObject: any = {};
51     darkMode: boolean;
52
53     private fetchRics() {
54         console.log('fetchRics ' + this.policyTypeName);
55         const self: PolicyInstanceDialogComponent = this;
56         this.dataService.getRics(this.policyTypeName).subscribe(
57             {
58                 next(value: Rics) {
59                     self.allRics = value.rics;
60                     console.log(value);
61                 }
62             });
63     }
64
65     constructor(
66         private cdr: ChangeDetectorRef,
67         private dataService: PolicyService,
68         private errorService: ErrorDialogService,
69         private notificationService: NotificationService,
70         @Inject(MAT_DIALOG_DATA) public data,
71         private dialogRef: MatDialogRef<PolicyInstanceDialogComponent>,
72         private ui: UiService) {
73         this.policyInstanceId = data.instanceId;
74         this.policyTypeName = data.name;
75         this.jsonSchemaObject = data.createSchema;
76         this.ric = data.ric;
77     }
78
79     ngOnInit() {
80         this.ui.darkModeState.subscribe((isDark) => {
81             this.darkMode = isDark;
82         });
83         this.instanceForm = new FormGroup({
84             'ricSelector': new FormControl(this.ric, [
85                 Validators.required
86             ])
87         });
88         if (!this.policyInstanceId) {
89             this.fetchRics();
90         }
91     }
92
93     ngAfterViewInit() {
94         this.cdr.detectChanges();
95     }
96
97     get ricSelector() { return this.instanceForm.get('ricSelector'); }
98
99     onSubmit() {
100         if (this.policyInstanceId == null) {
101             this.policyInstanceId = uuid.v4();
102         }
103         const policyJson: string = this.policyEditor.prettyLiveFormData;
104         const self: PolicyInstanceDialogComponent = this;
105         let createPolicyInstance: CreatePolicyInstance = this.createPolicyInstance(policyJson);
106         this.dataService.putPolicy(createPolicyInstance).subscribe(
107             {
108                 next(_) {
109                     self.notificationService.success('Policy ' + self.policyTypeName + ':' + self.policyInstanceId +
110                         ' submitted');
111                     self.dialogRef.close();
112                 },
113                 error(error: HttpErrorResponse) {
114                     self.errorService.displayError('Submit failed: ' + error.error);
115                 },
116                 complete() { }
117             });
118     }
119
120     private createPolicyInstance(policyJson: string) {
121         let createPolicyInstance = {} as CreatePolicyInstance;
122         createPolicyInstance.policy_data = JSON.parse(policyJson);
123         createPolicyInstance.policy_id = this.policyInstanceId;
124         createPolicyInstance.policytype_id = this.policyTypeName;
125         createPolicyInstance.ric_id = (!this.ricSelector.value.ric_id) ? this.ric : this.ricSelector.value.ric_id;
126         createPolicyInstance.service_id = 'controlpanel';
127         return createPolicyInstance;
128     }
129
130     onClose() {
131         this.dialogRef.close();
132     }
133
134     get isJsonFormValid(): boolean {
135         return this.policyEditor ? this.policyEditor.formIsValid : false;
136     }
137 }
138
139 export function getPolicyDialogProperties(policyTypeSchema: PolicyTypeSchema, instance: PolicyInstance, darkMode: boolean): MatDialogConfig {
140     const createSchema = policyTypeSchema.schemaObject;
141     const instanceId = instance ? instance.policy_id : null;
142     const instanceJson = instance ? instance.policy_data : null;
143     const name = policyTypeSchema.name;
144     const ric = instance ? instance.ric_id : null;
145     return {
146         maxWidth: '1200px',
147         maxHeight: '900px',
148         width: '900px',
149         role: 'dialog',
150         disableClose: false,
151         panelClass: darkMode ? 'dark-theme' : '',
152         data: {
153             createSchema,
154             instanceId,
155             instanceJson,
156             name,
157             ric
158         }
159     };
160 }
161