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===================================
20 import { Component, Inject, OnInit, ViewChild } from '@angular/core';
21 import { FormGroup } from '@angular/forms';
22 import { MatDialogConfig, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
23 import { PolicyService } from '../../services/policy/policy.service';
24 import { NotificationService } from '../../services/ui/notification.service';
25 import { UiService } from '../../services/ui/ui.service';
26 import { HttpErrorResponse } from '@angular/common/http';
27 import { ErrorDialogService } from '../../services/ui/error-dialog.service';
28 import * as uuid from 'uuid';
29 import { CreatePolicyInstance, PolicyInstance, PolicyTypeSchema } from '../../interfaces/policy.types';
30 import { RicSelectorComponent } from '../ric-selector/ric-selector.component';
31 import { formatJsonString, NoTypePolicyEditorComponent } from '../no-type-policy-editor/no-type-policy-editor.component';
32 import { TypedPolicyEditorComponent } from '../typed-policy-editor/typed-policy-editor.component';
35 selector: 'nrcp-policy-instance-dialog',
36 templateUrl: './policy-instance-dialog.component.html',
37 styleUrls: ['./policy-instance-dialog.component.scss']
39 export class PolicyInstanceDialogComponent implements OnInit {
40 instanceForm: FormGroup;
41 @ViewChild(RicSelectorComponent)
42 ricSelector: RicSelectorComponent;
43 @ViewChild(NoTypePolicyEditorComponent)
44 noTypePolicyEditor: NoTypePolicyEditorComponent;
45 @ViewChild(TypedPolicyEditorComponent)
46 typedPolicyEditor: TypedPolicyEditorComponent;
47 policyInstanceId: string; // null if not yet created
49 policyTypeName: string;
50 jsonSchemaObject: any = {};
53 allRicIds: string[] = [];
56 public dialogRef: MatDialogRef<PolicyInstanceDialogComponent>,
57 private policySvc: PolicyService,
58 private errorService: ErrorDialogService,
59 private notificationService: NotificationService,
60 @Inject(MAT_DIALOG_DATA) private data,
61 private ui: UiService) {
62 this.policyInstanceId = data.instanceId;
63 this.policyTypeName = data.name ? data.name : '< No Type >';
64 this.policyJson = data.instanceJson;
65 this.jsonSchemaObject = data.createSchema;
70 this.ui.darkModeState.subscribe((isDark) => {
71 this.darkMode = isDark;
73 this.instanceForm = new FormGroup({});
74 this.formatNoTypePolicyJson();
77 private formatNoTypePolicyJson() {
78 if (!this.typeHasSchema()) {
79 if (this.policyJson) {
80 this.policyJson = formatJsonString(this.policyJson);
82 this.policyJson = '{}';
88 if (this.policyInstanceId == null) {
89 this.policyInstanceId = uuid.v4();
91 const self: PolicyInstanceDialogComponent = this;
92 let policyData: string;
93 if (this.typeHasSchema()) {
94 policyData = this.typedPolicyEditor.prettyLiveFormData;
96 policyData = this.noTypePolicyEditor.policyJsonTextArea.value;
98 let createPolicyInstance: CreatePolicyInstance = this.createPolicyInstance(policyData);
99 this.policySvc.putPolicy(createPolicyInstance).subscribe(
102 self.notificationService.success('Policy without type:' + self.policyInstanceId + ' submitted');
103 self.dialogRef.close();
105 error(error: HttpErrorResponse) {
106 self.errorService.displayError('Submit failed: ' + error.error);
112 typeHasSchema(): boolean {
113 return this.jsonSchemaObject !== '{}';
116 isFormValid(): boolean {
117 let isValid: boolean = this.instanceForm.valid;
118 if (this.typeHasSchema()) {
119 isValid = isValid && this.typedPolicyEditor ? this.typedPolicyEditor.formIsValid : false;
124 private createPolicyInstance(policyJson: string): CreatePolicyInstance {
125 let createPolicyInstance = {} as CreatePolicyInstance;
126 createPolicyInstance.policy_data = JSON.parse(policyJson);
127 createPolicyInstance.policy_id = this.policyInstanceId;
128 createPolicyInstance.policytype_id = '';
129 createPolicyInstance.ric_id = this.ricSelector ? this.ricSelector.selectedRic : this.ric;
130 createPolicyInstance.service_id = 'controlpanel';
131 return createPolicyInstance;
135 export function getPolicyDialogProperties(policyTypeSchema: PolicyTypeSchema, instance: PolicyInstance, darkMode: boolean): MatDialogConfig {
136 const createSchema = policyTypeSchema.schemaObject;
137 const instanceId = instance ? instance.policy_id : null;
138 const instanceJson = instance ? instance.policy_data : null;
139 const name = policyTypeSchema.name;
140 const ric = instance ? instance.ric_id : null;
147 panelClass: darkMode ? 'dark-theme' : '',