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===================================
26 } from "@angular/core";
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";
42 } from "../../interfaces/policy.types";
43 import { formatJsonString } from "../no-type-policy-editor/no-type-policy-editor.component";
46 selector: "nrcp-policy-instance-dialog",
47 templateUrl: "./policy-instance-dialog.component.html",
48 styleUrls: ["./policy-instance-dialog.component.scss"],
50 export class PolicyInstanceDialogComponent implements OnInit, AfterViewInit {
51 policyInstance = {} as CreatePolicyInstance;
53 jsonSchemaObject: any;
55 allRicIds: string[] = [];
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,
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;
75 this.ui.darkModeState.subscribe((isDark) => {
76 this.darkMode = isDark;
78 this.formatNoTypePolicyJson();
81 // Do not remove! Needed to avoid "Expression has changed after it was checked" warning
83 this.cdr.detectChanges();
86 private formatNoTypePolicyJson() {
87 if (!this.typeHasSchema()) {
88 if (this.policyInstance.policy_data) {
89 this.policyJson = formatJsonString(this.policyInstance.policy_data);
91 this.policyJson = "{}";
96 onSelectedRicChanged(newRic: string): void {
97 this.policyInstance.ric_id = newRic;
100 onJsonChanged(newJson: string): void {
101 this.policyInstance.policy_data = newJson;
105 if (this.policyInstance.policy_id == null) {
106 this.policyInstance.policy_id = uuid.v4();
108 const self: PolicyInstanceDialogComponent = this;
109 this.policySvc.putPolicy(this.policyInstance).subscribe({
111 self.notificationService.success(
112 "Policy " + self.policyInstance.policy_id + " submitted"
114 self.dialogRef.close();
116 error(error: HttpErrorResponse) {
117 self.errorService.displayError("Submit failed: " + error.error);
123 typeHasSchema(): boolean {
124 return this.jsonSchemaObject !== "{}";
127 isFormValid(): boolean {
129 this.policyInstance.ric_id !== null &&
130 this.policyInstance.policy_data !== null
135 export function getPolicyDialogProperties(
136 policyTypeSchema: PolicyTypeSchema,
137 instance: PolicyInstance,
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;
151 panelClass: darkMode ? "dark-theme" : "",