Ric list in Policy instance Dialog Component
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy-control / no-type-policy-instance-dialog.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
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';
30
31 @Component({
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']
35 })
36 export class NoTypePolicyInstanceDialogComponent implements OnInit {
37   instanceForm: FormGroup;
38
39   policyInstanceId: string; // null if not yet created
40   policyJson: string;
41   darkMode: boolean;
42   ric: string;
43   allRics: Ric[];
44
45   constructor(
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) : '';
53     this.ric = data.ric;
54   }
55
56   ngOnInit() {
57     this.ui.darkModeState.subscribe((isDark) => {
58       this.darkMode = isDark;
59     });
60     this.instanceForm = new FormGroup({
61       'ricSelector': new FormControl(this.ric, [
62         Validators.required
63       ]),
64       'policyJsonTextArea': new FormControl(this.policyJson, [
65         Validators.required,
66         jsonValidator()
67       ])
68     });
69     if (!this.policyInstanceId) {
70       this.fetchRics();
71     }
72   }
73
74   get policyJsonTextArea() { return this.instanceForm.get('policyJsonTextArea'); }
75
76   get ricSelector() { return this.instanceForm.get('ricSelector'); }
77
78   onSubmit() {
79     if (this.policyInstanceId == null) {
80       this.policyInstanceId = uuid.v4();
81     }
82     const self: NoTypePolicyInstanceDialogComponent = this;
83     this.policySvc.putPolicy('', this.policyInstanceId, this.policyJsonTextArea.value, this.ric).subscribe(
84       {
85         next(_) {
86           self.notificationService.success('Policy without type:' + self.policyInstanceId + ' submitted');
87         },
88         error(error: HttpErrorResponse) {
89           self.errorService.displayError('Submit failed: ' + error.error);
90         },
91         complete() { }
92       });
93   }
94
95   private fetchRics() {
96     const self: NoTypePolicyInstanceDialogComponent = this;
97     this.policySvc.getRics('').subscribe(
98       {
99         next(value:Ric[]) {
100           self.allRics = value;
101           console.log(value);
102         },
103         error(error: HttpErrorResponse) {
104           self.errorService.displayError('Fetching of rics failed: ' + error.message);
105         },
106         complete() { }
107       });
108   }
109
110   private formatJsonString(jsonToFormat: any) {
111     return JSON.stringify(jsonToFormat, null, 2);
112   }
113
114   formatJsonInput() {
115     this.policyJson = this.formatJsonString(JSON.parse(this.policyJsonTextArea.value));
116   }
117 }
118
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;
123   };
124 }
125
126 export function isJsonValid(json: string) {
127   try {
128     if (json != null) {
129       JSON.parse(json);
130       return true;
131     }
132   } catch (jsonError) {
133     return false;
134   }
135 }