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