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 * as uuid from "uuid";
40 } from "@interfaces/policy.types";
43 selector: "nrcp-policy-instance-dialog",
44 templateUrl: "./policy-instance-dialog.component.html",
45 styleUrls: ["./policy-instance-dialog.component.scss"],
47 export class PolicyInstanceDialogComponent implements OnInit, AfterViewInit {
48 policyInstance = {} as CreatePolicyInstance;
50 jsonSchemaObject: any;
52 allRicIds: string[] = [];
55 private cdr: ChangeDetectorRef,
56 public dialogRef: MatDialogRef<PolicyInstanceDialogComponent>,
57 private policySvc: PolicyService,
58 private notificationService: NotificationService,
59 @Inject(MAT_DIALOG_DATA) private data,
62 this.policyInstance.policy_id = data.instanceId;
63 this.policyInstance.policytype_id = data.name ? data.name : "";
64 this.policyInstance.policy_data = data.instanceJson;
65 this.policyJson = data.instanceJson;
66 this.jsonSchemaObject = data.createSchema;
67 this.policyInstance.ric_id = data.ric;
68 this.policyInstance.service_id = "controlpanel";
72 this.ui.darkModeState.subscribe((isDark) => {
73 this.darkMode = isDark;
77 // Do not remove! Needed to avoid "Expression has changed after it was checked" warning
79 this.cdr.detectChanges();
82 onSelectedRicChanged(newRic: string): void {
83 this.policyInstance.ric_id = newRic;
86 onJsonChanged(newJson: string): void {
87 this.policyInstance.policy_data = newJson ? JSON.parse(newJson) : null;
91 if (this.policyInstance.policy_id == null) {
92 this.policyInstance.policy_id = uuid.v4();
94 const self: PolicyInstanceDialogComponent = this;
95 this.policySvc.putPolicy(this.policyInstance).subscribe({
97 self.notificationService.success(
98 "Policy " + self.policyInstance.policy_id + " submitted"
100 self.dialogRef.close("ok");
106 typeHasSchema(): boolean {
107 return this.jsonSchemaObject.description ? true : false;
110 isFormValid(): boolean {
112 this.policyInstance.ric_id !== null &&
113 this.policyInstance.policy_data !== null
118 export function getPolicyDialogProperties(
119 policyTypeSchema: PolicyTypeSchema,
120 instance: PolicyInstance,
123 const createSchema = policyTypeSchema.schemaObject;
124 const instanceId = instance ? instance.policy_id : null;
125 const instanceJson = instance ? instance.policy_data : null;
126 const name = policyTypeSchema.name;
127 const ric = instance ? instance.ric_id : null;
134 panelClass: darkMode ? "dark-theme" : "",
142 } as MatDialogConfig;