2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 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, OnInit, Output } from "@angular/core";
21 import { RicConfig } from "@app/interfaces/ric.config";
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";
37 selector: "nrcp-ric-config",
38 templateUrl: "./ric-config.component.html",
39 styleUrls: ["./ric-config.component.scss"],
41 { provide: ControlContainer, useExisting: FormGroupDirective },
45 export class RicConfigComponent implements OnInit {
46 @Output() validJson: EventEmitter<string> = new EventEmitter<string>();
48 ricConfigForm: FormGroup = new FormGroup({});
50 constructor(private policyService: PolicyService,
51 private notificationService: NotificationService) {
55 let initialJson: RicConfig = "{}";
57 this.ricConfigForm.addControl(
59 new FormControl(initialJson, [
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;
74 isJsonValid(json: string): boolean {
75 let valid = false as boolean;
81 } catch (jsonError) {}
85 handleJsonChangeEvent(notValid: boolean, newValue: string): void {
90 this.validJson.emit(json);
92 get ricConfigInfo(): AbstractControl {
93 return this.ricConfigForm
94 ? this.ricConfigForm.get("ricConfigInfo")
98 formatJsonInput(): void {
99 let jsonBefore: string = this.ricConfigInfo.value;
100 let jsonAfter = formatJsonString(JSON.parse(jsonBefore));
101 this.ricConfigInfo.setValue(jsonAfter);
105 this.policyService.getConfiguration().subscribe((ricConfig: RicConfig) => {
106 this.ricConfig = formatJsonString(ricConfig);
107 let initialJson: RicConfig;
108 if (this.ricConfig) {
109 initialJson = this.ricConfig;
113 this.ricConfigInfo.setValue(initialJson);
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!");
127 export function formatJsonString(jsonToFormat: any): string {
128 return JSON.stringify(jsonToFormat, null, 2);