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 { 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 } 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';
34 selector: 'nrcp-no-type-policy-instance-dialog',
35 templateUrl: './no-type-policy-instance-dialog.component.html',
36 styleUrls: ['./no-type-policy-instance-dialog.component.scss']
38 export class NoTypePolicyInstanceDialogComponent implements OnInit {
39 instanceForm: FormGroup;
40 @ViewChild(RicSelectorComponent)
41 private ricSelectorComponent: RicSelectorComponent;
42 @ViewChild(NoTypePolicyEditorComponent)
43 private policyEditorComponent: NoTypePolicyEditorComponent;
44 policyInstanceId: string; // null if not yet created
48 allRicIds: string[] = [];
51 public dialogRef: MatDialogRef<NoTypePolicyInstanceDialogComponent>,
52 private policySvc: PolicyService,
53 private errorService: ErrorDialogService,
54 private notificationService: NotificationService,
55 @Inject(MAT_DIALOG_DATA) private data,
56 private ui: UiService) {
57 this.policyInstanceId = data.instanceId;
58 this.policyJson = data.instanceJson ? formatJsonString(data.instanceJson) : '{}';
63 this.ui.darkModeState.subscribe((isDark) => {
64 this.darkMode = isDark;
66 this.instanceForm = new FormGroup({});
70 if (this.policyInstanceId == null) {
71 this.policyInstanceId = uuid.v4();
73 const self: NoTypePolicyInstanceDialogComponent = this;
74 let createPolicyInstance: CreatePolicyInstance = this.createPolicyInstance(this.policyEditorComponent.policyJsonTextArea.value);
75 this.policySvc.putPolicy(createPolicyInstance).subscribe(
78 self.notificationService.success('Policy without type:' + self.policyInstanceId + ' submitted');
79 self.dialogRef.close();
81 error(error: HttpErrorResponse) {
82 self.errorService.displayError('Submit failed: ' + error.error);
88 private createPolicyInstance(policyJson: string): CreatePolicyInstance {
89 let createPolicyInstance = {} as CreatePolicyInstance;
90 createPolicyInstance.policy_data = JSON.parse(policyJson);
91 createPolicyInstance.policy_id = this.policyInstanceId;
92 createPolicyInstance.policytype_id = '';
93 createPolicyInstance.ric_id = this.ricSelectorComponent.selectedRic;
94 createPolicyInstance.service_id = 'controlpanel';
95 return createPolicyInstance;