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";
45 selector: "nrcp-policy-instance-dialog",
46 templateUrl: "./policy-instance-dialog.component.html",
47 styleUrls: ["./policy-instance-dialog.component.scss"],
49 export class PolicyInstanceDialogComponent implements OnInit, AfterViewInit {
50 policyInstance = {} as CreatePolicyInstance;
52 jsonSchemaObject: any;
54 allRicIds: string[] = [];
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,
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";
75 this.ui.darkModeState.subscribe((isDark) => {
76 this.darkMode = isDark;
80 // Do not remove! Needed to avoid "Expression has changed after it was checked" warning
82 this.cdr.detectChanges();
85 onSelectedRicChanged(newRic: string): void {
86 this.policyInstance.ric_id = newRic;
89 onJsonChanged(newJson: string): void {
90 this.policyInstance.policy_data = newJson ? JSON.parse(newJson) : null;
94 if (this.policyInstance.policy_id == null) {
95 this.policyInstance.policy_id = uuid.v4();
97 const self: PolicyInstanceDialogComponent = this;
98 this.policySvc.putPolicy(this.policyInstance).subscribe({
100 self.notificationService.success(
101 "Policy " + self.policyInstance.policy_id + " submitted"
103 self.dialogRef.close();
105 error(error: HttpErrorResponse) {
106 self.errorService.displayError("Submit failed: " + error.error);
112 typeHasSchema(): boolean {
113 return this.jsonSchemaObject.description ? true : false;
116 isFormValid(): boolean {
118 this.policyInstance.ric_id !== null &&
119 this.policyInstance.policy_data !== null
124 export function getPolicyDialogProperties(
125 policyTypeSchema: PolicyTypeSchema,
126 instance: PolicyInstance,
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;
140 panelClass: darkMode ? "dark-theme" : "",