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 } from '@angular/core';
21 import { FormControl, FormGroup, Validators, ValidatorFn, AbstractControl } from '@angular/forms';
22 import { 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 { Ric } from '../interfaces/ric';
32 selector: 'rd-no-type-policy-instance-dialog',
33 templateUrl: './no-type-policy-instance-dialog.component.html',
34 styleUrls: ['./no-type-policy-instance-dialog.component.scss']
36 export class NoTypePolicyInstanceDialogComponent implements OnInit {
37 instanceForm: FormGroup;
39 policyInstanceId: string; // null if not yet created
46 private policySvc: PolicyService,
47 private errorService: ErrorDialogService,
48 private notificationService: NotificationService,
49 @Inject(MAT_DIALOG_DATA) private data,
50 private ui: UiService) {
51 this.policyInstanceId = data.instanceId;
52 this.policyJson = data.instanceJson ? this.formatJsonString(data.instanceJson) : '';
57 this.ui.darkModeState.subscribe((isDark) => {
58 this.darkMode = isDark;
60 this.instanceForm = new FormGroup({
61 'ricSelector': new FormControl(this.ric, [
64 'policyJsonTextArea': new FormControl(this.policyJson, [
69 if (!this.policyInstanceId) {
74 get policyJsonTextArea() { return this.instanceForm.get('policyJsonTextArea'); }
76 get ricSelector() { return this.instanceForm.get('ricSelector'); }
79 if (this.policyInstanceId == null) {
80 this.policyInstanceId = uuid.v4();
82 const self: NoTypePolicyInstanceDialogComponent = this;
83 this.policySvc.putPolicy('', this.policyInstanceId, this.policyJsonTextArea.value, this.ric).subscribe(
86 self.notificationService.success('Policy without type:' + self.policyInstanceId + ' submitted');
88 error(error: HttpErrorResponse) {
89 self.errorService.displayError('Submit failed: ' + error.error);
96 const self: NoTypePolicyInstanceDialogComponent = this;
97 this.policySvc.getRics('').subscribe(
100 self.allRics = value;
103 error(error: HttpErrorResponse) {
104 self.errorService.displayError('Fetching of rics failed: ' + error.message);
110 private formatJsonString(jsonToFormat: any) {
111 return JSON.stringify(jsonToFormat, null, 2);
115 this.policyJson = this.formatJsonString(JSON.parse(this.policyJsonTextArea.value));
119 export function jsonValidator(): ValidatorFn {
120 return (control: AbstractControl): { [key: string]: any } | null => {
121 const notValid = !isJsonValid(control.value);
122 return notValid ? { 'invalidJson': { value: control.value } } : null;
126 export function isJsonValid(json: string) {
132 } catch (jsonError) {