2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
27 } from "@angular/core";
28 import { FormGroup } from "@angular/forms";
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";
44 } from "../../interfaces/policy.types";
45 import { RicSelectorComponent } from "../ric-selector/ric-selector.component";
48 NoTypePolicyEditorComponent,
49 } from "../no-type-policy-editor/no-type-policy-editor.component";
50 import { TypedPolicyEditorComponent } from "../typed-policy-editor/typed-policy-editor.component";
53 selector: "nrcp-policy-instance-dialog",
54 templateUrl: "./policy-instance-dialog.component.html",
55 styleUrls: ["./policy-instance-dialog.component.scss"],
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
67 policyTypeName: string;
68 jsonSchemaObject: any;
71 allRicIds: string[] = [];
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,
82 this.policyInstanceId = data.instanceId;
83 this.policyTypeName = data.name;
84 this.policyJson = data.instanceJson;
85 this.jsonSchemaObject = data.createSchema;
90 this.ui.darkModeState.subscribe((isDark) => {
91 this.darkMode = isDark;
93 this.instanceForm = new FormGroup({});
94 this.formatNoTypePolicyJson();
97 // Do not remove! Needed to avoid "Expression has changed after it was checked" warning
99 this.cdr.detectChanges();
102 private formatNoTypePolicyJson() {
103 if (!this.typeHasSchema()) {
104 if (this.policyJson) {
105 this.policyJson = formatJsonString(this.policyJson);
107 this.policyJson = "{}";
113 if (this.policyInstanceId == null) {
114 this.policyInstanceId = uuid.v4();
116 const self: PolicyInstanceDialogComponent = this;
117 let policyData: string;
118 if (this.typeHasSchema()) {
119 policyData = this.typedPolicyEditor.prettyLiveFormData;
121 policyData = this.noTypePolicyEditor.policyJsonTextArea.value;
123 let createPolicyInstance: CreatePolicyInstance = this.createPolicyInstance(
126 this.policySvc.putPolicy(createPolicyInstance).subscribe({
128 self.notificationService.success(
129 "Policy without type:" + self.policyInstanceId + " submitted"
131 self.dialogRef.close();
133 error(error: HttpErrorResponse) {
134 self.errorService.displayError("Submit failed: " + error.error);
140 typeHasSchema(): boolean {
141 return this.jsonSchemaObject !== "{}";
144 isFormValid(): boolean {
145 let isValid: boolean = this.instanceForm.valid;
146 if (this.typeHasSchema()) {
148 isValid && this.typedPolicyEditor
149 ? this.typedPolicyEditor.formIsValid
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
163 createPolicyInstance.service_id = "controlpanel";
164 return createPolicyInstance;
168 export function getPolicyDialogProperties(
169 policyTypeSchema: PolicyTypeSchema,
170 instance: PolicyInstance,
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;
184 panelClass: darkMode ? "dark-theme" : "",