Add tests of PolicyInstanceDialogComponent
[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) 2020 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 {
21   AfterViewInit,
22   ChangeDetectorRef,
23   Component,
24   Inject,
25   OnInit,
26   ViewChild,
27 } from "@angular/core";
28 import { FormGroup } from "@angular/forms";
29 import {
30   MatDialogConfig,
31   MatDialogRef,
32   MAT_DIALOG_DATA,
33 } from "@angular/material/dialog";
34 import { PolicyService } from "../../services/policy/policy.service";
35 import { NotificationService } from "../../services/ui/notification.service";
36 import { UiService } from "../../services/ui/ui.service";
37 import { HttpErrorResponse } from "@angular/common/http";
38 import { ErrorDialogService } from "../../services/ui/error-dialog.service";
39 import * as uuid from "uuid";
40 import {
41   CreatePolicyInstance,
42   PolicyInstance,
43   PolicyTypeSchema,
44 } from "../../interfaces/policy.types";
45 import { RicSelectorComponent } from "../ric-selector/ric-selector.component";
46 import {
47   formatJsonString,
48   NoTypePolicyEditorComponent,
49 } from "../no-type-policy-editor/no-type-policy-editor.component";
50 import { TypedPolicyEditorComponent } from "../typed-policy-editor/typed-policy-editor.component";
51
52 @Component({
53   selector: "nrcp-policy-instance-dialog",
54   templateUrl: "./policy-instance-dialog.component.html",
55   styleUrls: ["./policy-instance-dialog.component.scss"],
56 })
57 export class PolicyInstanceDialogComponent implements OnInit, AfterViewInit {
58   instanceForm: FormGroup;
59   @ViewChild(RicSelectorComponent)
60   ricSelector: RicSelectorComponent;
61   @ViewChild(NoTypePolicyEditorComponent)
62   noTypePolicyEditor: NoTypePolicyEditorComponent;
63   @ViewChild(TypedPolicyEditorComponent)
64   typedPolicyEditor: TypedPolicyEditorComponent;
65   policyInstanceId: string; // null if not yet created
66   policyJson: string;
67   policyTypeName: string;
68   jsonSchemaObject: any;
69   darkMode: boolean;
70   ric: string;
71   allRicIds: string[] = [];
72
73   constructor(
74     private cdr: ChangeDetectorRef,
75     public dialogRef: MatDialogRef<PolicyInstanceDialogComponent>,
76     private policySvc: PolicyService,
77     private errorService: ErrorDialogService,
78     private notificationService: NotificationService,
79     @Inject(MAT_DIALOG_DATA) private data,
80     private ui: UiService
81   ) {
82     this.policyInstanceId = data.instanceId;
83     this.policyTypeName = data.name ? data.name : "< No Type >";
84     this.policyJson = data.instanceJson;
85     this.jsonSchemaObject = data.createSchema;
86     this.ric = data.ric;
87   }
88
89   ngOnInit() {
90     this.ui.darkModeState.subscribe((isDark) => {
91       this.darkMode = isDark;
92     });
93     this.instanceForm = new FormGroup({});
94     this.formatNoTypePolicyJson();
95   }
96
97   // Do not remove! Needed to avoid "Expression has changed after it was checked" warning
98   ngAfterViewInit() {
99     this.cdr.detectChanges();
100   }
101
102   private formatNoTypePolicyJson() {
103     if (!this.typeHasSchema()) {
104       if (this.policyJson) {
105         this.policyJson = formatJsonString(this.policyJson);
106       } else {
107         this.policyJson = "{}";
108       }
109     }
110   }
111
112   onSubmit() {
113     if (this.policyInstanceId == null) {
114       this.policyInstanceId = uuid.v4();
115     }
116     const self: PolicyInstanceDialogComponent = this;
117     let policyData: string;
118     if (this.typeHasSchema()) {
119       policyData = this.typedPolicyEditor.prettyLiveFormData;
120     } else {
121       policyData = this.noTypePolicyEditor.policyJsonTextArea.value;
122     }
123     let createPolicyInstance: CreatePolicyInstance = this.createPolicyInstance(
124       policyData
125     );
126     this.policySvc.putPolicy(createPolicyInstance).subscribe({
127       next(_) {
128         self.notificationService.success(
129           "Policy without type:" + self.policyInstanceId + " submitted"
130         );
131         self.dialogRef.close();
132       },
133       error(error: HttpErrorResponse) {
134         self.errorService.displayError("Submit failed: " + error.error);
135       },
136       complete() {},
137     });
138   }
139
140   typeHasSchema(): boolean {
141     return this.jsonSchemaObject.schemaObject !== "{}";
142   }
143
144   isFormValid(): boolean {
145     let isValid: boolean = this.instanceForm.valid;
146     if (this.typeHasSchema()) {
147       isValid =
148         isValid && this.typedPolicyEditor
149           ? this.typedPolicyEditor.formIsValid
150           : false;
151     }
152     return isValid;
153   }
154
155   private createPolicyInstance(policyJson: string): CreatePolicyInstance {
156     let createPolicyInstance = {} as CreatePolicyInstance;
157     createPolicyInstance.policy_data = JSON.parse(policyJson);
158     createPolicyInstance.policy_id = this.policyInstanceId;
159     createPolicyInstance.policytype_id = "";
160     createPolicyInstance.ric_id = this.ricSelector
161       ? this.ricSelector.selectedRic
162       : this.ric;
163     createPolicyInstance.service_id = "controlpanel";
164     return createPolicyInstance;
165   }
166 }
167
168 export function getPolicyDialogProperties(
169   policyTypeSchema: PolicyTypeSchema,
170   instance: PolicyInstance,
171   darkMode: boolean
172 ): MatDialogConfig {
173   const createSchema = policyTypeSchema.schemaObject;
174   const instanceId = instance ? instance.policy_id : null;
175   const instanceJson = instance ? instance.policy_data : null;
176   const name = policyTypeSchema.name;
177   const ric = instance ? instance.ric_id : null;
178   return {
179     maxWidth: "1200px",
180     maxHeight: "900px",
181     width: "900px",
182     role: "dialog",
183     disableClose: false,
184     panelClass: darkMode ? "dark-theme" : "",
185     data: {
186       createSchema,
187       instanceId,
188       instanceJson,
189       name,
190       ric,
191     },
192   };
193 }