RIC Configuration from UI
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ric-config / ric-config.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 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, OnInit, Output } from "@angular/core";
21 import { RicConfig } from "@app/interfaces/ric.config";
22 import {
23   AbstractControl,
24   ControlContainer,
25   FormControl,
26   FormGroup,
27   FormGroupDirective,
28   ValidatorFn,
29   Validators,
30 } from "@angular/forms";
31 import { PolicyService } from "@services/policy/policy.service";
32 import { EventEmitter } from "@angular/core";
33 import { NotificationService } from "@app/services/ui/notification.service";
34 import { HttpResponse } from "@angular/common/http";
35
36 @Component({
37   selector: "nrcp-ric-config",
38   templateUrl: "./ric-config.component.html",
39   styleUrls: ["./ric-config.component.scss"],
40   viewProviders: [
41     { provide: ControlContainer, useExisting: FormGroupDirective },
42   ],
43 })
44
45 export class RicConfigComponent implements OnInit {
46   @Output() validJson: EventEmitter<string> = new EventEmitter<string>();
47   ricConfig: string;
48   ricConfigForm: FormGroup = new FormGroup({});
49
50   constructor(private policyService: PolicyService,
51     private notificationService: NotificationService) {
52   }
53
54   ngOnInit(): void {
55     let initialJson: RicConfig = "{}";
56     this.getConfig();
57     this.ricConfigForm.addControl(
58       "ricConfigInfo",
59       new FormControl(initialJson, [
60         Validators.required,
61         this.jsonValidator(),
62       ])
63     );
64   }
65
66   jsonValidator(): ValidatorFn {
67     return (control: AbstractControl): { [key: string]: any } | null => {
68       const notValid = !this.isJsonValid(control.value);
69       this.handleJsonChangeEvent(notValid, control.value);
70       return notValid ? { invalidJson: { value: control.value } } : null;
71     };
72   }
73
74   isJsonValid(json: string): boolean {
75     let valid = false as boolean;
76     try {
77       if (json != null) {
78         JSON.parse(json);
79         valid = true;
80       }
81     } catch (jsonError) {}
82     return valid;
83   }
84
85   handleJsonChangeEvent(notValid: boolean, newValue: string): void {
86     let json = newValue;
87     if (notValid) {
88       json = null;
89     }
90     this.validJson.emit(json);
91   }
92   get ricConfigInfo(): AbstractControl {
93     return this.ricConfigForm
94       ? this.ricConfigForm.get("ricConfigInfo")
95       : null;
96   }
97
98   formatJsonInput(): void {
99     let jsonBefore: string = this.ricConfigInfo.value;
100     let jsonAfter = formatJsonString(JSON.parse(jsonBefore));
101     this.ricConfigInfo.setValue(jsonAfter);
102   }
103
104   getConfig() {
105     this.policyService.getConfiguration().subscribe((ricConfig: RicConfig) => {
106       this.ricConfig = formatJsonString(ricConfig);
107       let initialJson: RicConfig;
108     if (this.ricConfig) {
109       initialJson = this.ricConfig;
110     } else {
111       initialJson = "{}";
112     }
113     this.ricConfigInfo.setValue(initialJson);
114     });
115   }
116
117   updateRicConfig() {
118     let updateRic = this.ricConfigInfo.value.replace(/\n/g, '');
119     this.policyService.updateConfiguration(updateRic).subscribe((response: HttpResponse<Object>) => {
120       if (response.status === 200) {
121         this.notificationService.success("RIC Configuration Updated!");
122       }
123     });
124   }
125 }
126
127 export function formatJsonString(jsonToFormat: any): string {
128   return JSON.stringify(jsonToFormat, null, 2);
129 }