1e0df3b5955340cce56a0b1b85d46991a9601a47
[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
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 import { HttpErrorResponse } from '@angular/common/http';
28
29 @Component({
30   selector: 'rd-ac-xapp',
31   templateUrl: './ac-xapp.component.html',
32   styleUrls: ['./ac-xapp.component.scss']
33 })
34 export class AcXappComponent implements OnInit {
35
36   private acForm: FormGroup;
37
38   constructor(
39     private acXappService: ACXappService,
40     private errorDialogService: ErrorDialogService,
41     private notificationService: NotificationService) { }
42
43   ngOnInit() {
44     const windowLengthPattern = /^([0-9]{1}|[1-5][0-9]{1}|60)$/;
45     const blockingRatePattern = /^([0-9]{1,2}|100)$/;
46     const triggerPattern = /^([0-9]+)$/;
47     this.acForm = new FormGroup({
48       // Names must match the ACAdmissionIntervalControl interface
49       enforce: new FormControl(true, [Validators.required]),
50       window_length: new FormControl('', [Validators.required, Validators.pattern(windowLengthPattern)]),
51       blocking_rate: new FormControl('', [Validators.required, Validators.pattern(blockingRatePattern)]),
52       trigger_threshold: new FormControl('', [Validators.required, Validators.pattern(triggerPattern)])
53     });
54     // TODO: show pending action indicator
55     this.acXappService.getPolicy().subscribe((res: ACAdmissionIntervalControl) => {
56       this.acForm.controls['enforce'].setValue(res.enforce);
57       this.acForm.controls['window_length'].setValue(res.window_length);
58       this.acForm.controls['blocking_rate'].setValue(res.blocking_rate);
59       this.acForm.controls['trigger_threshold'].setValue(res.trigger_threshold);
60       // TODO: clear pending action indicator
61     },
62       (error: HttpErrorResponse) => {
63         // TODO: clear pending action indicator
64         this.errorDialogService.displayError(error.message);
65       });
66   }
67
68   updateAc = (acFormValue: ACAdmissionIntervalControl) => {
69     if (this.acForm.valid) {
70       // convert strings to numbers using the plus operator
71       const acFormValueConverted = {
72         enforce: acFormValue.enforce,
73         window_length: +acFormValue.window_length,
74         blocking_rate: +acFormValue.blocking_rate,
75         trigger_threshold: +acFormValue.trigger_threshold
76       };
77       this.acXappService.putPolicy(acFormValueConverted).subscribe(
78         response => {
79           if (response.status === 200) {
80             this.notificationService.success('AC update policy succeeded!');
81           }
82         },
83         (error => {
84           this.errorDialogService.displayError('AC update policy failed: ' + error.message);
85         })
86       );
87     }
88   }
89
90   hasError(controlName: string, errorName: string) {
91     if (this.acForm.controls[controlName].hasError(errorName)) {
92       return true;
93     }
94     return false;
95   }
96
97   validateControl(controlName: string) {
98     if (this.acForm.controls[controlName].invalid && this.acForm.controls[controlName].touched) {
99       return true;
100     }
101     return false;
102   }
103
104 }