2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 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 { MatDialogConfig, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
22 import * as uuid from 'uuid';
23 import { CreatePolicyInstance, PolicyInstance, PolicyTypeSchema } from '../../interfaces/policy.types';
24 import { PolicyService } from '../../services/policy/policy.service';
25 import { ErrorDialogService } from '../../services/ui/error-dialog.service';
26 import { NotificationService } from './../../services/ui/notification.service';
27 import { UiService } from '../../services/ui/ui.service';
28 import { HttpErrorResponse } from '@angular/common/http';
29 import { FormGroup, FormControl, Validators } from '@angular/forms';
30 import { Ric, Rics } from '../../interfaces/ric';
31 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 @ViewChild(TypedPolicyEditorComponent)
41 policyEditor: TypedPolicyEditorComponent;
42 instanceForm: FormGroup;
47 policyInstanceId: string; // null if not yet created
48 policyTypeName: string;
49 jsonSchemaObject: any = {};
53 console.log('fetchRics ' + this.policyTypeName);
54 const self: PolicyInstanceDialogComponent = this;
55 this.dataService.getRics(this.policyTypeName).subscribe(
58 self.allRics = value.rics;
65 private dataService: PolicyService,
66 private errorService: ErrorDialogService,
67 private notificationService: NotificationService,
68 @Inject(MAT_DIALOG_DATA) public data,
69 private dialogRef: MatDialogRef<PolicyInstanceDialogComponent>,
70 private ui: UiService) {
71 this.policyInstanceId = data.instanceId;
72 this.policyTypeName = data.name;
73 this.jsonSchemaObject = data.createSchema;
78 this.ui.darkModeState.subscribe((isDark) => {
79 this.darkMode = isDark;
81 this.instanceForm = new FormGroup({
82 'ricSelector': new FormControl(this.ric, [
86 if (!this.policyInstanceId) {
91 get ricSelector() { return this.instanceForm.get('ricSelector'); }
94 if (this.policyInstanceId == null) {
95 this.policyInstanceId = uuid.v4();
97 const policyJson: string = this.policyEditor.prettyLiveFormData;
98 const self: PolicyInstanceDialogComponent = this;
99 let createPolicyInstance: CreatePolicyInstance = this.createPolicyInstance(policyJson);
100 this.dataService.putPolicy(createPolicyInstance).subscribe(
103 self.notificationService.success('Policy ' + self.policyTypeName + ':' + self.policyInstanceId +
105 self.dialogRef.close();
107 error(error: HttpErrorResponse) {
108 self.errorService.displayError('Submit failed: ' + error.error);
114 private createPolicyInstance(policyJson: string) {
115 let createPolicyInstance = {} as CreatePolicyInstance;
116 createPolicyInstance.policy_data = JSON.parse(policyJson);
117 createPolicyInstance.policy_id = this.policyInstanceId;
118 createPolicyInstance.policytype_id = this.policyTypeName;
119 createPolicyInstance.ric_id = (!this.ricSelector.value.ric_id) ? this.ric : this.ricSelector.value.ric_id;
120 createPolicyInstance.service_id = 'controlpanel';
121 return createPolicyInstance;
125 this.dialogRef.close();
128 get isJsonFormValid(): boolean {
129 return this.policyEditor ? this.policyEditor.formIsValid : false;
133 export function getPolicyDialogProperties(policyTypeSchema: PolicyTypeSchema, instance: PolicyInstance, darkMode: boolean): MatDialogConfig {
134 const createSchema = policyTypeSchema.schemaObject;
135 const instanceId = instance ? instance.policy_id : null;
136 const instanceJson = instance ? instance.policy_data : null;
137 const name = policyTypeSchema.name;
138 const ric = instance ? instance.ric_id : null;
145 panelClass: darkMode ? 'dark-theme' : '',