f64991172a5e0ffe4e22251e0257043193cbc9cc
[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 { HttpErrorResponse } from "@angular/common/http";
36 import { ErrorDialogService } from "../../services/ui/error-dialog.service";
37 import * as uuid from "uuid";
38 import {
39   CreatePolicyInstance,
40   PolicyInstance,
41   PolicyTypeSchema,
42 } from "../../interfaces/policy.types";
43 import { formatJsonString } from "../no-type-policy-editor/no-type-policy-editor.component";
44
45 @Component({
46   selector: "nrcp-policy-instance-dialog",
47   templateUrl: "./policy-instance-dialog.component.html",
48   styleUrls: ["./policy-instance-dialog.component.scss"],
49 })
50 export class PolicyInstanceDialogComponent implements OnInit, AfterViewInit {
51   policyInstance = {} as CreatePolicyInstance;
52   policyJson: string;
53   jsonSchemaObject: any;
54   darkMode: boolean;
55   allRicIds: string[] = [];
56
57   constructor(
58     private cdr: ChangeDetectorRef,
59     public dialogRef: MatDialogRef<PolicyInstanceDialogComponent>,
60     private policySvc: PolicyService,
61     private errorService: ErrorDialogService,
62     private notificationService: NotificationService,
63     @Inject(MAT_DIALOG_DATA) private data,
64     private ui: UiService
65   ) {
66     this.policyInstance.policy_id = data.instanceId;
67     this.policyInstance.policytype_id = data.name;
68     this.policyInstance.policy_data = data.instanceJson;
69     this.policyJson = data.instanceJson;
70     this.jsonSchemaObject = data.createSchema;
71     this.policyInstance.ric_id = data.ric;
72   }
73
74   ngOnInit() {
75     this.ui.darkModeState.subscribe((isDark) => {
76       this.darkMode = isDark;
77     });
78     this.formatNoTypePolicyJson();
79   }
80
81   // Do not remove! Needed to avoid "Expression has changed after it was checked" warning
82   ngAfterViewInit() {
83     this.cdr.detectChanges();
84   }
85
86   private formatNoTypePolicyJson() {
87     if (!this.typeHasSchema()) {
88       if (this.policyInstance.policy_data) {
89         this.policyJson = formatJsonString(this.policyInstance.policy_data);
90       } else {
91         this.policyJson = "{}";
92       }
93     }
94   }
95
96   onSelectedRicChanged(newRic: string): void {
97     this.policyInstance.ric_id = newRic;
98   }
99
100   onJsonChanged(newJson: string): void {
101     this.policyInstance.policy_data = newJson;
102   }
103
104   onSubmit() {
105     if (this.policyInstance.policy_id == null) {
106       this.policyInstance.policy_id = uuid.v4();
107     }
108     const self: PolicyInstanceDialogComponent = this;
109     this.policySvc.putPolicy(this.policyInstance).subscribe({
110       next(_) {
111         self.notificationService.success(
112           "Policy " + self.policyInstance.policy_id + " submitted"
113         );
114         self.dialogRef.close();
115       },
116       error(error: HttpErrorResponse) {
117         self.errorService.displayError("Submit failed: " + error.error);
118       },
119       complete() {},
120     });
121   }
122
123   typeHasSchema(): boolean {
124     return this.jsonSchemaObject !== "{}";
125   }
126
127   isFormValid(): boolean {
128     return (
129       this.policyInstance.ric_id !== null &&
130       this.policyInstance.policy_data !== null
131     );
132   }
133 }
134
135 export function getPolicyDialogProperties(
136   policyTypeSchema: PolicyTypeSchema,
137   instance: PolicyInstance,
138   darkMode: boolean
139 ): 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 }