Fix PolicyInstanceComponent and add test coverage
[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 } from "@angular/core";
27 import {
28   MatDialogConfig,
29   MatDialogRef,
30   MAT_DIALOG_DATA,
31 } from "@angular/material/dialog";
32 import { PolicyService } from "@services/policy/policy.service";
33 import { NotificationService } from "@services/ui/notification.service";
34 import { UiService } from "@services/ui/ui.service";
35 import * as uuid from "uuid";
36 import {
37   CreatePolicyInstance,
38   PolicyInstance,
39   PolicyTypeSchema,
40 } from "@interfaces/policy.types";
41
42 @Component({
43   selector: "nrcp-policy-instance-dialog",
44   templateUrl: "./policy-instance-dialog.component.html",
45   styleUrls: ["./policy-instance-dialog.component.scss"],
46 })
47 export class PolicyInstanceDialogComponent implements OnInit, AfterViewInit {
48   policyInstance = {} as CreatePolicyInstance;
49   policyJson: string;
50   jsonSchemaObject: any;
51   darkMode: boolean;
52   allRicIds: string[] = [];
53
54   constructor(
55     private cdr: ChangeDetectorRef,
56     public dialogRef: MatDialogRef<PolicyInstanceDialogComponent>,
57     private policySvc: PolicyService,
58     private notificationService: NotificationService,
59     @Inject(MAT_DIALOG_DATA) private data,
60     private ui: UiService
61   ) {
62     this.policyInstance.policy_id = data.instanceId;
63     this.policyInstance.policytype_id = data.name ? data.name : "";
64     this.policyInstance.policy_data = data.instanceJson;
65     this.policyJson = data.instanceJson;
66     this.jsonSchemaObject = data.createSchema;
67     this.policyInstance.ric_id = data.ric;
68     this.policyInstance.service_id = "controlpanel";
69   }
70
71   ngOnInit() {
72     this.ui.darkModeState.subscribe((isDark) => {
73       this.darkMode = isDark;
74     });
75   }
76
77   // Do not remove! Needed to avoid "Expression has changed after it was checked" warning
78   ngAfterViewInit() {
79     this.cdr.detectChanges();
80   }
81
82   onSelectedRicChanged(newRic: string): void {
83     this.policyInstance.ric_id = newRic;
84   }
85
86   onJsonChanged(newJson: string): void {
87     this.policyInstance.policy_data = newJson ? JSON.parse(newJson) : null;
88   }
89
90   onSubmit() {
91     if (this.policyInstance.policy_id == null) {
92       this.policyInstance.policy_id = uuid.v4();
93     }
94     const self: PolicyInstanceDialogComponent = this;
95     this.policySvc.putPolicy(this.policyInstance).subscribe({
96       next(_) {
97         self.notificationService.success(
98           "Policy " + self.policyInstance.policy_id + " submitted"
99         );
100         self.dialogRef.close("ok");
101       },
102       complete() {},
103     });
104   }
105
106   typeHasSchema(): boolean {
107     return this.jsonSchemaObject.description ? true : false;
108   }
109
110   isFormValid(): boolean {
111     return (
112       this.policyInstance.ric_id !== null &&
113       this.policyInstance.policy_data !== null
114     );
115   }
116 }
117
118 export function getPolicyDialogProperties(
119   policyTypeSchema: PolicyTypeSchema,
120   instance: PolicyInstance,
121   darkMode: boolean
122 ): MatDialogConfig {
123   const createSchema = policyTypeSchema.schemaObject;
124   const instanceId = instance ? instance.policy_id : null;
125   const instanceJson = instance ? instance.policy_data : null;
126   const name = policyTypeSchema.name;
127   const ric = instance ? instance.ric_id : null;
128   const data = {
129     maxWidth: "1200px",
130     maxHeight: "900px",
131     width: "900px",
132     role: "dialog",
133     disableClose: false,
134     panelClass: darkMode ? "dark-theme" : "",
135     data: {
136       createSchema,
137       instanceId,
138       instanceJson,
139       name,
140       ric,
141     },
142   } as MatDialogConfig;
143   return data;
144 }