Upgrade component API versions to Amber
[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 { HttpErrorResponse } from '@angular/common/http';
22 import { Component, OnInit, OnDestroy } from '@angular/core';
23 import { FormControl, FormGroup, Validators } from '@angular/forms';
24 import { ACAdmissionIntervalControl } from '../interfaces/ac-xapp.types';
25 import { ACXappService } from '../services/ac-xapp/ac-xapp.service';
26 import { ErrorDialogService } from '../services/ui/error-dialog.service';
27 import { NotificationService } from './../services/ui/notification.service';
28 import { Subscription } from 'rxjs';
29 import { InstanceSelectorService } from '../services/instance-selector/instance-selector.service';
30
31 @Component({
32   selector: 'rd-ac-xapp',
33   templateUrl: './ac-xapp.component.html',
34   styleUrls: ['./ac-xapp.component.scss']
35 })
36 export class AcXappComponent implements OnInit, OnDestroy {
37
38   private acForm: FormGroup;
39   private instanceChange: Subscription;
40   private instanceKey: string;
41
42   constructor(
43     private acXappService: ACXappService,
44     private errorDialogService: ErrorDialogService,
45     private notificationService: NotificationService,
46     public instanceSelectorService: InstanceSelectorService, ) { }
47
48   ngOnInit() {
49     this.acForm = new FormGroup({
50       // Names must match the ACAdmissionIntervalControl interface
51       enforce: new FormControl(true, [Validators.required]),
52       class: new FormControl('', [Validators.required, Validators.min(1), Validators.max(256)]),
53       window_length: new FormControl('', [Validators.required, Validators.min(15), Validators.max(300)]),
54       blocking_rate: new FormControl('', [Validators.required, Validators.min(0), Validators.max(100)]),
55       trigger_threshold: new FormControl('', [Validators.required, Validators.min(1)])
56     });
57
58     this.instanceChange = this.instanceSelectorService.getSelectedInstancekey().subscribe((instanceKey: string) => {
59       if (instanceKey) {
60         // TODO: show pending action indicator
61         this.instanceKey = instanceKey;
62         this.acXappService.getPolicy(instanceKey).subscribe((res: ACAdmissionIntervalControl) => {
63           this.acForm.controls['class'].setValue(res.class);
64           this.acForm.controls['enforce'].setValue(res.enforce);
65           this.acForm.controls['window_length'].setValue(res.window_length);
66           this.acForm.controls['blocking_rate'].setValue(res.blocking_rate);
67           this.acForm.controls['trigger_threshold'].setValue(res.trigger_threshold);
68           // TODO: clear pending action indicator
69         },
70           (error: HttpErrorResponse) => {
71             // TODO: clear pending action indicator
72             this.errorDialogService.displayError(error.message);
73           });
74       }
75     });
76   }
77
78   ngOnDestroy() {
79     this.instanceChange.unsubscribe();
80   }
81
82   updateAc = (acFormValue: ACAdmissionIntervalControl) => {
83     if (this.acForm.valid) {
84       // convert strings to numbers using the plus operator
85       const acFormValueConverted = {
86         class: +acFormValue.class,
87         enforce: acFormValue.enforce,
88         window_length: +acFormValue.window_length,
89         blocking_rate: +acFormValue.blocking_rate,
90         trigger_threshold: +acFormValue.trigger_threshold
91       };
92       this.acXappService.putPolicy(this.instanceKey, acFormValueConverted).subscribe(
93         response => {
94           if (response.status === 200) {
95             this.notificationService.success('AC update policy succeeded!');
96           }
97         },
98         (error => {
99           this.errorDialogService.displayError('AC update policy failed: ' + error.message);
100         })
101       );
102     }
103   }
104
105   hasError(controlName: string, errorName: string) {
106     if (this.acForm.controls[controlName].hasError(errorName)) {
107       return true;
108     }
109     return false;
110   }
111
112   validateControl(controlName: string) {
113     if (this.acForm.controls[controlName].invalid && this.acForm.controls[controlName].touched) {
114       return true;
115     }
116     return false;
117   }
118
119 }