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 { AfterViewInit, 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 { ChangeDetectorRef } from '@angular/core';
31 import { Ric, Rics } from '../../interfaces/ric';
32 import { TypedPolicyEditorComponent } from '../typed-policy-editor/typed-policy-editor.component';
36 selector: 'nrcp-policy-instance-dialog',
37 templateUrl: './policy-instance-dialog.component.html',
38 styleUrls: ['./policy-instance-dialog.component.scss']
40 export class PolicyInstanceDialogComponent implements OnInit, AfterViewInit {
41 @ViewChild(TypedPolicyEditorComponent)
42 policyEditor: TypedPolicyEditorComponent;
43 instanceForm: FormGroup;
48 policyInstanceId: string; // null if not yet created
49 policyTypeName: string;
50 jsonSchemaObject: any = {};
54 console.log('fetchRics ' + this.policyTypeName);
55 const self: PolicyInstanceDialogComponent = this;
56 this.dataService.getRics(this.policyTypeName).subscribe(
59 self.allRics = value.rics;
66 private cdr: ChangeDetectorRef,
67 private dataService: PolicyService,
68 private errorService: ErrorDialogService,
69 private notificationService: NotificationService,
70 @Inject(MAT_DIALOG_DATA) public data,
71 private dialogRef: MatDialogRef<PolicyInstanceDialogComponent>,
72 private ui: UiService) {
73 this.policyInstanceId = data.instanceId;
74 this.policyTypeName = data.name;
75 this.jsonSchemaObject = data.createSchema;
80 this.ui.darkModeState.subscribe((isDark) => {
81 this.darkMode = isDark;
83 this.instanceForm = new FormGroup({
84 'ricSelector': new FormControl(this.ric, [
88 if (!this.policyInstanceId) {
94 this.cdr.detectChanges();
97 get ricSelector() { return this.instanceForm.get('ricSelector'); }
100 if (this.policyInstanceId == null) {
101 this.policyInstanceId = uuid.v4();
103 const policyJson: string = this.policyEditor.prettyLiveFormData;
104 const self: PolicyInstanceDialogComponent = this;
105 let createPolicyInstance: CreatePolicyInstance = this.createPolicyInstance(policyJson);
106 this.dataService.putPolicy(createPolicyInstance).subscribe(
109 self.notificationService.success('Policy ' + self.policyTypeName + ':' + self.policyInstanceId +
111 self.dialogRef.close();
113 error(error: HttpErrorResponse) {
114 self.errorService.displayError('Submit failed: ' + error.error);
120 private createPolicyInstance(policyJson: string) {
121 let createPolicyInstance = {} as CreatePolicyInstance;
122 createPolicyInstance.policy_data = JSON.parse(policyJson);
123 createPolicyInstance.policy_id = this.policyInstanceId;
124 createPolicyInstance.policytype_id = this.policyTypeName;
125 createPolicyInstance.ric_id = (!this.ricSelector.value.ric_id) ? this.ric : this.ricSelector.value.ric_id;
126 createPolicyInstance.service_id = 'controlpanel';
127 return createPolicyInstance;
131 this.dialogRef.close();
134 get isJsonFormValid(): boolean {
135 return this.policyEditor ? this.policyEditor.formIsValid : false;
139 export function getPolicyDialogProperties(policyTypeSchema: PolicyTypeSchema, instance: PolicyInstance, darkMode: boolean): MatDialogConfig {
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' : '',