f7c360cc96b48e7447b1417ec043a4759e0d324f
[portal/ric-dashboard.git] / webapp-frontend / src / app / ac-xapp / ac-xapp.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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
21 import { Component, OnInit } from '@angular/core';
22 import { FormGroup, FormControl, Validators } from '@angular/forms';
23 import { ACAdmissionIntervalControl, ACAdmissionIntervalControlAck } from '../interfaces/ac-xapp.types';
24 import { ACXappService } from '../services/ac-xapp/ac-xapp.service';
25 import { ErrorDialogService } from '../services/ui/error-dialog.service';
26 import { NotificationService } from './../services/ui/notification.service';
27
28 @Component({
29   selector: 'rd-ac-xapp',
30   templateUrl: './ac-xapp.component.html',
31   styleUrls: ['./ac-xapp.component.scss']
32 })
33 export class AcXappComponent implements OnInit {
34
35   private acForm: FormGroup;
36
37   // this is probably the A1 version string
38   acVersion: string;
39
40   constructor(
41     private acXappService: ACXappService,
42     private errorDialogService: ErrorDialogService,
43     private notificationService: NotificationService) { }
44
45   ngOnInit() {
46     const windowLengthPattern = /^([0-9]{1}|[1-5][0-9]{1}|60)$/;
47     const blockingRatePattern = /^([0-9]{1,2}|100)$/;
48     const triggerPattern = /^([0-9]+)$/;
49     // No way to fetch current settings via A1 at present
50     this.acForm = new FormGroup({
51       // Names must match the ACAdmissionIntervalControl interface
52       enforce: new FormControl(true,  [Validators.required]),
53       window_length: new FormControl('', [Validators.required, Validators.pattern(windowLengthPattern)]),
54       blocking_rate: new FormControl('', [Validators.required, Validators.pattern(blockingRatePattern)]),
55       trigger_threshold: new FormControl('', [Validators.required, Validators.pattern(triggerPattern)])
56     });
57     this.acXappService.getVersion().subscribe((res: string) => this.acVersion = res);
58   }
59
60   updateAc = (acFormValue: ACAdmissionIntervalControl) => {
61     if (this.acForm.valid) {
62       this.acXappService.putPolicy(acFormValue).subscribe(
63         response => {
64           if (response.status === 200 ) {
65             this.notificationService.success('AC update policy succeeded!');
66           }
67         },
68         (error => {
69           this.errorDialogService.displayError('AC update policy failed: ' + error.message);
70         })
71       );
72     }
73   }
74
75   hasError(controlName: string, errorName: string) {
76     if (this.acForm.controls[controlName].hasError(errorName)) {
77       return true;
78     }
79     return false;
80   }
81
82   validateControl(controlName: string) {
83     if (this.acForm.controls[controlName].invalid && this.acForm.controls[controlName].touched) {
84       return true;
85     }
86     return false;
87   }
88
89 }